diff --git a/qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java b/qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java index 683990d51d4a8..eb3b95ff27595 100644 --- a/qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java +++ b/qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java @@ -204,7 +204,7 @@ public void testSortAndPaginateWithInProgress() throws Exception { inProgressSnapshots.add(AbstractSnapshotIntegTestCase.startFullSnapshot(logger, repoName, snapshotName, false)); } AbstractSnapshotIntegTestCase.awaitNumberOfSnapshotsInProgress(logger, inProgressCount); - AbstractSnapshotIntegTestCase.awaitClusterState(logger, state -> { + AbstractSnapshotIntegTestCase.awaitClusterState(state -> { final var snapshotsInProgress = SnapshotsInProgress.get(state); boolean firstIndexSuccessfullySnapshot = snapshotsInProgress.asStream() .flatMap(s -> s.shards().entrySet().stream()) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java index 9c5d96166b3d6..3098d1d558f99 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java @@ -213,7 +213,6 @@ public void runRepeatedlyWhileChangingMaster(Runnable runnable) throws Exception final String nonMasterNode = randomValueOtherThan(masterName, () -> randomFrom(internalCluster().getNodeNames())); awaitClusterState( - logger, nonMasterNode, state -> Optional.ofNullable(state.nodes().getMasterNode()).map(m -> m.getName().equals(masterName) == false).orElse(false) ); diff --git a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java index e0bfab595a318..fa2b47fda03a9 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java @@ -1077,7 +1077,6 @@ public void onRequestSent( final ActionFuture deleteResponse = startDeleteSnapshot(repoName, snapshotName); awaitClusterState( - logger, otherDataNode, state -> SnapshotsInProgress.get(state) .forRepo(repoName) diff --git a/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java index 63e77cfdc4523..185a0494183b3 100644 --- a/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java @@ -557,7 +557,7 @@ protected void addBwCFailedSnapshot(String repoName, String snapshotName, Map wait for [{}] deletions to show up in the cluster state", count); awaitClusterState(state -> SnapshotDeletionsInProgress.get(state).getEntries().size() == count); } @@ -569,7 +569,6 @@ protected void awaitNoMoreRunningOperations() throws Exception { protected void awaitNoMoreRunningOperations(String viaNode) throws Exception { logger.info("--> verify no more operations in the cluster state"); awaitClusterState( - logger, viaNode, state -> SnapshotsInProgress.get(state).isEmpty() && SnapshotDeletionsInProgress.get(state).hasDeletionsInProgress() == false ); @@ -604,13 +603,13 @@ public static ActionFuture startFullSnapshot( .execute(); } - protected void awaitNumberOfSnapshotsInProgress(int count) throws Exception { + protected void awaitNumberOfSnapshotsInProgress(int count) { awaitNumberOfSnapshotsInProgress(logger, count); } - public static void awaitNumberOfSnapshotsInProgress(Logger logger, int count) throws Exception { + public static void awaitNumberOfSnapshotsInProgress(Logger logger, int count) { logger.info("--> wait for [{}] snapshots to show up in the cluster state", count); - awaitClusterState(logger, state -> SnapshotsInProgress.get(state).count() == count); + awaitClusterState(state -> SnapshotsInProgress.get(state).count() == count); } protected SnapshotInfo assertSuccessful(ActionFuture future) throws Exception { diff --git a/test/framework/src/main/java/org/elasticsearch/test/ClusterServiceUtils.java b/test/framework/src/main/java/org/elasticsearch/test/ClusterServiceUtils.java index e0e7505191da9..e6ed4b489048a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ClusterServiceUtils.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ClusterServiceUtils.java @@ -8,17 +8,14 @@ */ package org.elasticsearch.test; -import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.util.Throwables; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.action.ActionListener; -import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.SubscribableListener; import org.elasticsearch.cluster.ClusterChangedEvent; import org.elasticsearch.cluster.ClusterName; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.ClusterStateListener; -import org.elasticsearch.cluster.ClusterStateObserver; import org.elasticsearch.cluster.ClusterStatePublicationEvent; import org.elasticsearch.cluster.ClusterStateUpdateTask; import org.elasticsearch.cluster.NodeConnectionsService; @@ -37,14 +34,12 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.util.concurrent.EsExecutors; import org.elasticsearch.core.TimeValue; -import org.elasticsearch.node.NodeClosedException; import org.elasticsearch.tasks.TaskManager; import org.elasticsearch.telemetry.tracing.Tracer; import org.elasticsearch.threadpool.ThreadPool; import java.util.Collections; import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Predicate; @@ -219,33 +214,8 @@ public static void setAllElapsedMillis(ClusterStatePublicationEvent clusterState clusterStatePublicationEvent.setMasterApplyElapsedMillis(0L); } - public static void awaitClusterState(Logger logger, Predicate statePredicate, ClusterService clusterService) - throws Exception { - final PlainActionFuture future = new PlainActionFuture<>(); - ClusterStateObserver.waitForState( - clusterService, - clusterService.getClusterApplierService().threadPool().getThreadContext(), - new ClusterStateObserver.Listener() { - @Override - public void onNewClusterState(ClusterState state) { - future.onResponse(null); - } - - @Override - public void onClusterServiceClose() { - future.onFailure(new NodeClosedException(clusterService.localNode())); - } - - @Override - public void onTimeout(TimeValue timeout) { - assert false : "onTimeout called with no timeout set"; - } - }, - statePredicate, - null, - logger - ); - future.get(30L, TimeUnit.SECONDS); + public static void awaitClusterState(Predicate statePredicate, ClusterService clusterService) { + ESTestCase.safeAwait(addTemporaryStateListener(clusterService, statePredicate, TimeValue.THIRTY_SECONDS), TimeValue.THIRTY_SECONDS); } public static void awaitNoPendingTasks(ClusterService clusterService) { diff --git a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java index aa7fc5058b532..1bf5e7d307777 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java @@ -17,7 +17,6 @@ import com.carrotsearch.randomizedtesting.generators.RandomPicks; import org.apache.http.HttpHost; -import org.apache.logging.log4j.Logger; import org.apache.lucene.search.Sort; import org.apache.lucene.search.TotalHits; import org.apache.lucene.tests.util.LuceneTestCase; @@ -1207,16 +1206,12 @@ public static PendingClusterTasksResponse getClusterPendingTasks(Client client) } } - protected void awaitClusterState(Predicate statePredicate) throws Exception { - awaitClusterState(logger, internalCluster().getMasterName(), statePredicate); + public static void awaitClusterState(Predicate statePredicate) { + awaitClusterState(internalCluster().getMasterName(), statePredicate); } - public static void awaitClusterState(Logger logger, Predicate statePredicate) throws Exception { - awaitClusterState(logger, internalCluster().getMasterName(), statePredicate); - } - - public static void awaitClusterState(Logger logger, String viaNode, Predicate statePredicate) throws Exception { - ClusterServiceUtils.awaitClusterState(logger, statePredicate, internalCluster().getInstance(ClusterService.class, viaNode)); + public static void awaitClusterState(String viaNode, Predicate statePredicate) { + ClusterServiceUtils.awaitClusterState(statePredicate, internalCluster().getInstance(ClusterService.class, viaNode)); } public static String getNodeId(String nodeName) { diff --git a/test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java b/test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java index 8b7fe57b94dea..453cddd504364 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java +++ b/test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java @@ -2064,7 +2064,7 @@ public String getMasterName(@Nullable String viaNode) { throw new AssertionError("Unable to get master name, no node found"); } try { - ClusterServiceUtils.awaitClusterState(logger, state -> state.nodes().getMasterNode() != null, clusterService(viaNode)); + ClusterServiceUtils.awaitClusterState(state -> state.nodes().getMasterNode() != null, clusterService(viaNode)); final ClusterState state = client(viaNode).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState(); final DiscoveryNode masterNode = state.nodes().getMasterNode(); if (masterNode == null) { 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 2db166a91cbbe..13c001ce4e499 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 @@ -488,7 +488,6 @@ public void doTestRunPolicyWithFailureToReadPolicy(boolean asyncAction, boolean // The cluster state can take a few extra milliseconds to update after the steps are executed ClusterServiceUtils.awaitClusterState( - logger, s -> s.metadata().getProject(state.projectId()).index(indexMetadata.getIndex()).getLifecycleExecutionState().stepInfo() != null, clusterService ); 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 8898cac495706..c8c78e13f5b09 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 @@ -121,11 +121,7 @@ public void tearDown() throws Exception { protected void waitForMlTemplates() throws Exception { // block until the templates are installed - ClusterServiceUtils.awaitClusterState( - logger, - MachineLearning::criticalTemplatesInstalled, - getInstanceFromNode(ClusterService.class) - ); + ClusterServiceUtils.awaitClusterState(MachineLearning::criticalTemplatesInstalled, getInstanceFromNode(ClusterService.class)); } protected void blockingCall(Consumer> function, AtomicReference response, AtomicReference error) 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 4473919130c83..ee966ec951826 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 @@ -169,9 +169,9 @@ protected Collection> getMockPlugins() { } @Before - public void ensureTemplatesArePresent() throws Exception { + public void ensureTemplatesArePresent() { if (cluster().size() > 0) { - awaitClusterState(logger, MachineLearning::criticalTemplatesInstalled); + awaitClusterState(MachineLearning::criticalTemplatesInstalled); } } 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 2d51303b1d939..c5bc29043114f 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 @@ -98,7 +98,7 @@ public void testMultipleNodes() throws Exception { }); } - private void waitForMonitoringIndices() throws Exception { + private void waitForMonitoringIndices() { final var indexNameExpressionResolver = internalCluster().getCurrentMasterNodeInstance(IndexNameExpressionResolver.class); final var indicesOptions = IndicesOptions.builder() .wildcardOptions(IndicesOptions.WildcardOptions.builder().allowEmptyExpressions(true)) diff --git a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java index 50e58befc4a0e..2a9ae3035ae9e 100644 --- a/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java +++ b/x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java @@ -1323,7 +1323,7 @@ private static IndexMetadata getIndexMetadata(String indexName) { .index(indexName); } - private void waitUntilAllShardsAreUnassigned(Index index) throws Exception { + private void waitUntilAllShardsAreUnassigned(Index index) { awaitClusterState(state -> state.getRoutingTable().index(index).allPrimaryShardsUnassigned()); }