diff --git a/server/src/main/java/org/elasticsearch/common/collect/HppcMaps.java b/server/src/main/java/org/elasticsearch/common/collect/HppcMaps.java index c48626cf7c1c9..4115d0295a9c6 100644 --- a/server/src/main/java/org/elasticsearch/common/collect/HppcMaps.java +++ b/server/src/main/java/org/elasticsearch/common/collect/HppcMaps.java @@ -43,17 +43,10 @@ public static ObjectObjectHashMap newMap(int expectedElements) { } /** - * Returns a new map with a default initial capacity. - */ - public static ObjectObjectHashMap newMap() { - return newMap(16); - } - - /** - * Returns a map like {@link #newMap()} that does not accept null keys + * Returns a map like {@link #newMap} that does not accept null keys */ public static ObjectObjectHashMap newNoNullKeysMap() { - return ensureNoNullKeys(16); + return newNoNullKeysMap(16); } /** @@ -64,17 +57,6 @@ public static ObjectObjectHashMap newNoNullKeysMap() { * expansion (inclusive). */ public static ObjectObjectHashMap newNoNullKeysMap(int expectedElements) { - return ensureNoNullKeys(expectedElements); - } - - /** - * Wraps the given map and prevent adding of null keys. - * - * @param expectedElements - * The expected number of elements guaranteed not to cause buffer - * expansion (inclusive). - */ - public static ObjectObjectHashMap ensureNoNullKeys(int expectedElements) { return new ObjectObjectHashMap(expectedElements) { @Override public V put(K key, V value) { diff --git a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenIntMap.java b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenIntMap.java index 74ee39db1f967..7fd3e8d53b5ee 100644 --- a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenIntMap.java +++ b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenIntMap.java @@ -28,7 +28,6 @@ import com.carrotsearch.hppc.ObjectContainer; import com.carrotsearch.hppc.cursors.IntCursor; import com.carrotsearch.hppc.cursors.IntObjectCursor; -import com.carrotsearch.hppc.cursors.ObjectCursor; import com.carrotsearch.hppc.predicates.IntObjectPredicate; import com.carrotsearch.hppc.predicates.IntPredicate; import com.carrotsearch.hppc.procedures.IntObjectProcedure; @@ -149,23 +148,7 @@ public ObjectContainer values() { * Returns a direct iterator over the keys. */ public Iterator valuesIt() { - final Iterator> iterator = map.values().iterator(); - return new Iterator() { - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public VType next() { - return iterator.next().value; - } - - @Override - public void remove() { - throw new UnsupportedOperationException(); - } - }; + return ImmutableOpenMap.iterator(map.values()); } @Override @@ -234,7 +217,7 @@ public Builder(ImmutableOpenIntMap map) { public ImmutableOpenIntMap build() { IntObjectHashMap map = this.map; this.map = null; // nullify the map, so any operation post build will fail! (hackish, but safest) - return new ImmutableOpenIntMap<>(map); + return map.isEmpty() ? of() : new ImmutableOpenIntMap<>(map); } /** diff --git a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java index 5c16e66a1d953..72673bd07c9c1 100644 --- a/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java +++ b/server/src/main/java/org/elasticsearch/common/collect/ImmutableOpenMap.java @@ -153,13 +153,17 @@ public ObjectContainer values() { * Returns a direct iterator over the keys. */ public Iterator valuesIt() { - final Iterator> iterator = map.values().iterator(); - return new Iterator() { + return iterator(map.values()); + } + + static Iterator iterator(ObjectCollection collection) { + final Iterator> iterator = collection.iterator(); + return new Iterator<>() { @Override public boolean hasNext() { return iterator.hasNext(); } @Override - public VType next() { + public T next() { return iterator.next().value; } @@ -244,11 +248,9 @@ public Builder(ImmutableOpenMap map) { public ImmutableOpenMap build() { ObjectObjectHashMap map = this.map; this.map = null; // nullify the map, so any operation post build will fail! (hackish, but safest) - return new ImmutableOpenMap<>(map); + return map.isEmpty() ? of() : new ImmutableOpenMap<>(map); } - - /** * Puts all the entries in the map to the builder. */ diff --git a/server/src/test/java/org/elasticsearch/action/bulk/TransportBulkActionIngestTests.java b/server/src/test/java/org/elasticsearch/action/bulk/TransportBulkActionIngestTests.java index 691c9bfe17eff..9ccdf19c14562 100644 --- a/server/src/test/java/org/elasticsearch/action/bulk/TransportBulkActionIngestTests.java +++ b/server/src/test/java/org/elasticsearch/action/bulk/TransportBulkActionIngestTests.java @@ -600,8 +600,9 @@ public void testFindDefaultPipelineFromTemplateMatch(){ Metadata metadata = mock(Metadata.class); when(state.metadata()).thenReturn(metadata); when(state.getMetadata()).thenReturn(metadata); - when(metadata.templates()).thenReturn(templateMetadataBuilder.build()); - when(metadata.getTemplates()).thenReturn(templateMetadataBuilder.build()); + final ImmutableOpenMap templateMetadata = templateMetadataBuilder.build(); + when(metadata.templates()).thenReturn(templateMetadata); + when(metadata.getTemplates()).thenReturn(templateMetadata); when(metadata.indices()).thenReturn(ImmutableOpenMap.of()); IndexRequest indexRequest = new IndexRequest("missing_index").id("id"); 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 7e8a83d03fe3a..360620b120e29 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 @@ -2043,9 +2043,7 @@ private static ClusterState createRemoteClusterState(String indexName, boolean e ShardRouting shardRouting = TestShardRouting.newShardRouting(indexName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); - - return csBuilder.build(); + return csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); } private static ClusterState createRemoteClusterState(final ClusterState previous, final String... indices) { @@ -2118,9 +2116,7 @@ private static ClusterState createRemoteClusterStateWithDataStream(String dataSt ShardRouting shardRouting = TestShardRouting.newShardRouting(dataStreamName, 0, "1", true, ShardRoutingState.INITIALIZING).moveToStarted(); IndexRoutingTable indexRoutingTable = IndexRoutingTable.builder(indexMetadata.getIndex()).addShard(shardRouting).build(); - csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); - - return csBuilder.build(); + return csBuilder.routingTable(RoutingTable.builder().add(indexRoutingTable).build()).build(); } } 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 71b59653f5f85..c84c4d8486da0 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 @@ -65,14 +65,15 @@ public void testPerformAction() { 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(sourceIndexMetadataBuilder).put(targetIndexMetadataBuilder).build() + Metadata.builder().put(sourceIndexMetadata, false).put(targetIndexMetadataBuilder).build() ).build(); CopySettingsStep copySettingsStep = new CopySettingsStep(randomStepKey(), randomStepKey(), indexPrefix, LifecycleSettings.LIFECYCLE_NAME); - ClusterState newClusterState = copySettingsStep.performAction(sourceIndexMetadataBuilder.build().getIndex(), clusterState); + ClusterState newClusterState = copySettingsStep.performAction(sourceIndexMetadata.getIndex(), clusterState); IndexMetadata newTargetIndexMetadata = newClusterState.metadata().index(targetIndex); assertThat(newTargetIndexMetadata.getSettings().get(LifecycleSettings.LIFECYCLE_NAME), is(policyName)); } 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 aec520387cbcf..c96aac4650fe2 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 @@ -66,11 +66,12 @@ public void testPerformAction() { IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); - ClusterState clusterState = - ClusterState.builder(emptyClusterState()).metadata(Metadata.builder().put(indexMetadataBuilder).build()).build(); + final IndexMetadata indexMetadata = indexMetadataBuilder.build(); + ClusterState clusterState = ClusterState.builder(emptyClusterState()) + .metadata(Metadata.builder().put(indexMetadata, false).build()).build(); GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); - ClusterState newClusterState = generateSnapshotNameStep.performAction(indexMetadataBuilder.build().getIndex(), clusterState); + ClusterState newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); assertThat("the " + GenerateSnapshotNameStep.NAME + " step must generate a snapshot name", executionState.getSnapshotName(), 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 929b274dc431e..d512d7fa6ee75 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 @@ -61,12 +61,13 @@ public void testPerformActionThrowsExceptionIfIndexIsNotPartOfDataStream() { 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(sourceIndexMetadataBuilder).build() + Metadata.builder().put(sourceIndexMetadata, false).build() ).build(); expectThrows(IllegalStateException.class, - () -> createRandomInstance().performAction(sourceIndexMetadataBuilder.build().getIndex(), clusterState)); + () -> createRandomInstance().performAction(sourceIndexMetadata.getIndex(), clusterState)); } public void testPerformActionThrowsExceptionIfIndexIsTheDataStreamWriteIndex() { 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 a986ee693bbe6..63f3e8f1c30d9 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 @@ -175,10 +175,11 @@ public void testIsAbleToRun() { csBuilder.metadata(metadata); DatafeedConfigAutoUpdater updater = new DatafeedConfigAutoUpdater(provider, indexNameExpressionResolver); - assertThat(updater.isAbleToRun(csBuilder.build()), is(true)); + final ClusterState clusterState = csBuilder.build(); + assertThat(updater.isAbleToRun(clusterState), is(true)); - metadata = new Metadata.Builder(csBuilder.build().metadata()); - routingTable = new RoutingTable.Builder(csBuilder.build().routingTable()); + metadata = new Metadata.Builder(clusterState.metadata()); + routingTable = new RoutingTable.Builder(clusterState.routingTable()); if (randomBoolean()) { routingTable.remove(MlConfigIndex.indexName()); } else { @@ -191,12 +192,13 @@ public void testIsAbleToRun() { .addIndexShard(new IndexShardRoutingTable.Builder(shardId).addShard(shardRouting).build())); } + csBuilder = ClusterState.builder(clusterState); csBuilder.routingTable(routingTable.build()); csBuilder.metadata(metadata); - assertThat(updater.isAbleToRun(csBuilder.build()), is(false)); + final ClusterState csUpdated = csBuilder.build(); + assertThat(updater.isAbleToRun(csUpdated), is(false)); - csBuilder.metadata(Metadata.EMPTY_METADATA); - assertThat(updater.isAbleToRun(csBuilder.build()), is(true)); + assertThat(updater.isAbleToRun(ClusterState.builder(csUpdated).metadata(Metadata.EMPTY_METADATA).build()), is(true)); } private void withDatafeed(String datafeedId, boolean aggsRewritten) { diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManagerTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManagerTests.java index a161ffc84c7af..2d7175e2be8a8 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManagerTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/datafeed/DatafeedManagerTests.java @@ -280,9 +280,9 @@ public void testStart_GivenNewlyCreatedJobLookBackAndRealtime() throws Exception public void testDatafeedTaskWaitsUntilJobIsOpened() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); - ClusterState.Builder cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - when(clusterService.state()).thenReturn(cs.build()); + ClusterState cs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); DatafeedTask task = createDatafeedTask(DATAFEED_ID, 0L, 60000L); @@ -294,10 +294,10 @@ public void testDatafeedTaskWaitsUntilJobIsOpened() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); - ClusterState.Builder anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); - capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs.build(), cs.build())); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); // Still no run verify(threadPool, never()).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -308,7 +308,7 @@ public void testDatafeedTaskWaitsUntilJobIsOpened() { .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs.build())); + 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); @@ -320,9 +320,9 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); - ClusterState.Builder cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - when(clusterService.state()).thenReturn(cs.build()); + ClusterState cs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); DatafeedTask task = createDatafeedTask(DATAFEED_ID, 0L, 60000L); @@ -334,10 +334,10 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); - ClusterState.Builder anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); - capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs.build(), cs.build())); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); // Still no run verify(threadPool, never()).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -345,7 +345,7 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { hasOpenAutodetectCommunicator.set(true); capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", cs.build(), anotherJobCs.build())); + 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); @@ -354,9 +354,9 @@ public void testDatafeedTaskWaitsUntilAutodetectCommunicatorIsOpen() { public void testDatafeedTaskWaitsUntilJobIsNotStale() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder, true); - ClusterState.Builder cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - when(clusterService.state()).thenReturn(cs.build()); + ClusterState cs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); DatafeedTask task = createDatafeedTask(DATAFEED_ID, 0L, 60000L); @@ -368,10 +368,10 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENED, tasksBuilder, true); addJobTask("another_job", "node_id", JobState.OPENED, tasksBuilder); - ClusterState.Builder anotherJobCs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); + ClusterState anotherJobCs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); - capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs.build(), cs.build())); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", anotherJobCs, cs)); // Still no run verify(threadPool, never()).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -382,7 +382,7 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); capturedClusterStateListener.getValue().clusterChanged( - new ClusterChangedEvent("_source", jobOpenedCs.build(), anotherJobCs.build())); + 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); @@ -391,9 +391,9 @@ public void testDatafeedTaskWaitsUntilJobIsNotStale() { public void testDatafeedTaskStopsBecauseJobFailedWhileOpening() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); - ClusterState.Builder cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - when(clusterService.state()).thenReturn(cs.build()); + ClusterState cs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); DatafeedTask task = createDatafeedTask(DATAFEED_ID, 0L, 60000L); @@ -407,7 +407,7 @@ public void testDatafeedTaskStopsBecauseJobFailedWhileOpening() { ClusterState.Builder updatedCs = ClusterState.builder(clusterService.state()) .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", updatedCs.build(), cs.build())); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", updatedCs.build(), cs)); // Verify task never run and got stopped verify(threadPool, never()).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); @@ -417,9 +417,9 @@ public void testDatafeedTaskStopsBecauseJobFailedWhileOpening() { public void testDatafeedGetsStoppedWhileWaitingForJobToOpen() { PersistentTasksCustomMetadata.Builder tasksBuilder = PersistentTasksCustomMetadata.builder(); addJobTask(JOB_ID, "node_id", JobState.OPENING, tasksBuilder); - ClusterState.Builder cs = ClusterState.builder(clusterService.state()) - .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - when(clusterService.state()).thenReturn(cs.build()); + ClusterState cs = ClusterState.builder(clusterService.state()) + .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())).build(); + when(clusterService.state()).thenReturn(cs); Consumer handler = mockConsumer(); DatafeedTask task = createDatafeedTask(DATAFEED_ID, 0L, 60000L); @@ -437,7 +437,7 @@ public void testDatafeedGetsStoppedWhileWaitingForJobToOpen() { ClusterState.Builder updatedCs = ClusterState.builder(clusterService.state()) .metadata(new Metadata.Builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasksBuilder.build())); - capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", cs.build(), updatedCs.build())); + capturedClusterStateListener.getValue().clusterChanged(new ClusterChangedEvent("_source", cs, updatedCs.build())); // Verify no datafeed was run verify(threadPool, never()).executor(MachineLearning.DATAFEED_THREAD_POOL_NAME); 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 d417019d3f158..68a4b0f74633f 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 @@ -58,7 +58,6 @@ public void testVerifyIndicesExistAndPrimaryShardsAreActive() { .routingTable(routingTable.build()) .metadata(metadata); - csBuilder.build(); assertThat(TrainedModelStatsService.verifyIndicesExistAndPrimaryShardsAreActive(csBuilder.build(), resolver), equalTo(false)); } diff --git a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeLoadDetectorTests.java b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeLoadDetectorTests.java index e1fbf0a5e9a1c..6e006acba0c5f 100644 --- a/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeLoadDetectorTests.java +++ b/x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/job/JobNodeLoadDetectorTests.java @@ -73,34 +73,31 @@ public void testNodeLoadDetection() { OpenJobPersistentTasksExecutorTests.addJobTask("job_id4", "_node_id4", JobState.OPENED, tasksBuilder); PersistentTasksCustomMetadata tasks = tasksBuilder.build(); - ClusterState.Builder cs = ClusterState.builder(new ClusterName("_name")); - cs.nodes(nodes); - Metadata.Builder metadata = Metadata.builder(); - metadata.putCustom(PersistentTasksCustomMetadata.TYPE, tasks); - cs.metadata(metadata); + final ClusterState cs = ClusterState.builder(new ClusterName("_name")).nodes(nodes) + .metadata(Metadata.builder().putCustom(PersistentTasksCustomMetadata.TYPE, tasks)).build(); - NodeLoad load = nodeLoadDetector.detectNodeLoad(cs.build(), true, nodes.get("_node_id1"), 10, 30, true, false); + NodeLoad load = nodeLoadDetector.detectNodeLoad(cs, true, nodes.get("_node_id1"), 10, 30, true, false); assertThat(load.getAssignedJobMemory(), equalTo(52428800L)); assertThat(load.getNumAllocatingJobs(), equalTo(2L)); assertThat(load.getNumAssignedJobs(), equalTo(2L)); assertThat(load.getMaxJobs(), equalTo(10)); assertThat(load.getMaxMlMemory(), equalTo(0L)); - load = nodeLoadDetector.detectNodeLoad(cs.build(), true, nodes.get("_node_id2"), 5, 30, true, false); + load = nodeLoadDetector.detectNodeLoad(cs, true, nodes.get("_node_id2"), 5, 30, true, false); assertThat(load.getAssignedJobMemory(), equalTo(41943040L)); assertThat(load.getNumAllocatingJobs(), equalTo(1L)); assertThat(load.getNumAssignedJobs(), equalTo(1L)); assertThat(load.getMaxJobs(), equalTo(5)); assertThat(load.getMaxMlMemory(), equalTo(0L)); - load = nodeLoadDetector.detectNodeLoad(cs.build(), true, nodes.get("_node_id3"), 5, 30, true, false); + load = nodeLoadDetector.detectNodeLoad(cs, true, nodes.get("_node_id3"), 5, 30, true, false); assertThat(load.getAssignedJobMemory(), equalTo(0L)); assertThat(load.getNumAllocatingJobs(), equalTo(0L)); assertThat(load.getNumAssignedJobs(), equalTo(0L)); assertThat(load.getMaxJobs(), equalTo(5)); assertThat(load.getMaxMlMemory(), equalTo(0L)); - load = nodeLoadDetector.detectNodeLoad(cs.build(), true, nodes.get("_node_id4"), 5, 30, true, false); + load = nodeLoadDetector.detectNodeLoad(cs, true, nodes.get("_node_id4"), 5, 30, true, false); assertThat(load.getAssignedJobMemory(), equalTo(41943040L)); assertThat(load.getNumAllocatingJobs(), equalTo(0L)); assertThat(load.getNumAssignedJobs(), equalTo(1L)); 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 4a92264a05f28..f0722e79deaaf 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 @@ -74,6 +74,7 @@ public void testVerifyIndicesPrimaryShardsAreActive() { .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, 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 0e672c906416f..4ba306f318fb4 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 @@ -174,7 +174,8 @@ public void testIndexHealthChangeListeners() throws Exception { final ClusterState.Builder clusterStateBuilder = createClusterState(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7, RestrictedIndicesNames.SECURITY_MAIN_ALIAS, TEMPLATE_NAME); markShardsAvailable(clusterStateBuilder); - manager.clusterChanged(event(clusterStateBuilder)); + final ClusterState clusterState = clusterStateBuilder.build(); + manager.clusterChanged(event(ClusterState.builder(clusterState))); assertTrue(listenerCalled.get()); assertNull(previousState.get().indexHealth); @@ -184,7 +185,7 @@ public void testIndexHealthChangeListeners() throws Exception { listenerCalled.set(false); previousState.set(null); currentState.set(null); - ClusterChangedEvent event = new ClusterChangedEvent("same index health", clusterStateBuilder.build(), clusterStateBuilder.build()); + ClusterChangedEvent event = new ClusterChangedEvent("same index health", clusterState, clusterState); manager.clusterChanged(event); assertFalse(listenerCalled.get()); @@ -195,9 +196,8 @@ public void testIndexHealthChangeListeners() throws Exception { listenerCalled.set(false); previousState.set(null); currentState.set(null); - ClusterState previousClusterState = clusterStateBuilder.build(); - Index prevIndex = previousClusterState.getRoutingTable().index(RestrictedIndicesNames.INTERNAL_SECURITY_MAIN_INDEX_7).getIndex(); - clusterStateBuilder.routingTable(RoutingTable.builder() + 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, @@ -206,11 +206,9 @@ public void testIndexHealthChangeListeners() throws Exception { .initialize(UUIDs.randomBase64UUID(random()), null, 0L) .moveToUnassigned(new UnassignedInfo(UnassignedInfo.Reason.ALLOCATION_FAILED, ""))) .build())) - .build()); + .build()).build(); - - - event = new ClusterChangedEvent("different index health", clusterStateBuilder.build(), previousClusterState); + event = new ClusterChangedEvent("different index health", newClusterState, clusterState); manager.clusterChanged(event); assertTrue(listenerCalled.get()); assertEquals(ClusterHealthStatus.GREEN, previousState.get().indexHealth); @@ -220,7 +218,7 @@ public void testIndexHealthChangeListeners() throws Exception { listenerCalled.set(false); previousState.set(null); currentState.set(null); - event = new ClusterChangedEvent("different index health swapped", previousClusterState, clusterStateBuilder.build()); + event = new ClusterChangedEvent("different index health swapped", clusterState, newClusterState); manager.clusterChanged(event); assertTrue(listenerCalled.get()); assertEquals(ClusterHealthStatus.RED, previousState.get().indexHealth); @@ -490,10 +488,10 @@ public void testMissingIndexIsIdentifiedAsUpToDate() throws IOException { final ClusterState.Builder clusterStateBuilder = ClusterState.builder(clusterName); String mappingString = "/" + SECURITY_MAIN_TEMPLATE_7 + ".json"; IndexTemplateMetadata.Builder templateMeta = getIndexTemplateMetadata(SECURITY_MAIN_TEMPLATE_7, mappingString); - Metadata.Builder builder = new Metadata.Builder(clusterStateBuilder.build().getMetadata()); + final ClusterState clusterState = clusterStateBuilder.build(); + Metadata.Builder builder = new Metadata.Builder(clusterState.getMetadata()); builder.put(templateMeta); - clusterStateBuilder.metadata(builder); - manager.clusterChanged(new ClusterChangedEvent("test-event", clusterStateBuilder.build() + manager.clusterChanged(new ClusterChangedEvent("test-event", ClusterState.builder(clusterState).metadata(builder).build() , EMPTY_CLUSTER_STATE)); assertThat(actions.size(), equalTo(0)); } @@ -514,12 +512,12 @@ private ClusterState.Builder createClusterStateWithMapping(String securityTempla } private ClusterState.Builder createClusterStateWithMappingAndTemplate(String securityTemplateString) throws IOException { - ClusterState.Builder clusterStateBuilder = createClusterStateWithMapping(securityTemplateString); - Metadata.Builder metadataBuilder = new Metadata.Builder(clusterStateBuilder.build().metadata()); + final ClusterState state = createClusterStateWithMapping(securityTemplateString).build(); + Metadata.Builder metadataBuilder = new Metadata.Builder(state.metadata()); String securityMappingString = "/" + SECURITY_MAIN_TEMPLATE_7 + ".json"; IndexTemplateMetadata.Builder securityTemplateMeta = getIndexTemplateMetadata(SECURITY_MAIN_TEMPLATE_7, securityMappingString); metadataBuilder.put(securityTemplateMeta); - return clusterStateBuilder.metadata(metadataBuilder); + return ClusterState.builder(state).metadata(metadataBuilder); } private static IndexMetadata.Builder createIndexMetadata(String indexName, String templateString) throws IOException { diff --git a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransportStopTransformActionTests.java b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransportStopTransformActionTests.java index fe7ef14389894..cfea98eb9147f 100644 --- a/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransportStopTransformActionTests.java +++ b/x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/action/TransportStopTransformActionTests.java @@ -77,11 +77,11 @@ public void testTaskStateValidationWithTransformTasks() { "failed-task", new TransformState(TransformTaskState.FAILED, IndexerState.STOPPED, null, 0L, "task has failed", null) ); - csBuilder = ClusterState.builder(new ClusterName("_name")).metadata(buildMetadata(pTasksBuilder.build())); + final ClusterState cs = ClusterState.builder(new ClusterName("_name")).metadata(buildMetadata(pTasksBuilder.build())).build(); - TransportStopTransformAction.validateTaskState(csBuilder.build(), Arrays.asList("non-failed-task", "failed-task"), true); + TransportStopTransformAction.validateTaskState(cs, Arrays.asList("non-failed-task", "failed-task"), true); - TransportStopTransformAction.validateTaskState(csBuilder.build(), Collections.singletonList("non-failed-task"), false); + TransportStopTransformAction.validateTaskState(cs, Collections.singletonList("non-failed-task"), false); ClusterState.Builder csBuilderFinal = ClusterState.builder(new ClusterName("_name")).metadata(buildMetadata(pTasksBuilder.build())); ElasticsearchStatusException ex = expectThrows( 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 ce7d513cc1aaa..bfd665ae8338f 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 @@ -203,6 +203,7 @@ public void testVerifyIndicesPrimaryShardsAreActive() { ); } + csBuilder = ClusterState.builder(cs); csBuilder.routingTable(routingTable.build()); csBuilder.metadata(metadata); List result = TransformPersistentTasksExecutor.verifyIndicesPrimaryShardsAreActive(