Skip to content

Commit 68009b3

Browse files
DaveCTurnersarog
authored andcommitted
Clean up awaitClusterState overloads (elastic#133553)
The `logger` parameter is unused, it doesn't throw any checked exceptions, and there's no need for `protected` instance methods when they're also available as `public static`. Backport of elastic#132529 to 8.19
1 parent 867ccd5 commit 68009b3

File tree

8 files changed

+15
-56
lines changed

8 files changed

+15
-56
lines changed

qa/smoke-test-http/src/internalClusterTest/java/org/elasticsearch/http/snapshots/RestGetSnapshotsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ public void testSortAndPaginateWithInProgress() throws Exception {
206206
inProgressSnapshots.add(AbstractSnapshotIntegTestCase.startFullSnapshot(logger, repoName, snapshotName, false));
207207
}
208208
AbstractSnapshotIntegTestCase.awaitNumberOfSnapshotsInProgress(logger, inProgressCount);
209-
AbstractSnapshotIntegTestCase.awaitClusterState(logger, state -> {
209+
AbstractSnapshotIntegTestCase.awaitClusterState(state -> {
210210
final var snapshotsInProgress = SnapshotsInProgress.get(state);
211211
boolean firstIndexSuccessfullySnapshot = snapshotsInProgress.asStream()
212212
.flatMap(s -> s.shards().entrySet().stream())

server/src/internalClusterTest/java/org/elasticsearch/snapshots/DedicatedClusterSnapshotRestoreIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,6 @@ public void onRequestSent(
10721072
final ActionFuture<AcknowledgedResponse> deleteResponse = startDeleteSnapshot(repoName, snapshotName);
10731073

10741074
awaitClusterState(
1075-
logger,
10761075
otherDataNode,
10771076
state -> SnapshotsInProgress.get(state)
10781077
.forRepo(repoName)

test/framework/src/main/java/org/elasticsearch/snapshots/AbstractSnapshotIntegTestCase.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ protected void addBwCFailedSnapshot(String repoName, String snapshotName, Map<St
557557
);
558558
}
559559

560-
protected void awaitNDeletionsInProgress(int count) throws Exception {
560+
protected void awaitNDeletionsInProgress(int count) {
561561
logger.info("--> wait for [{}] deletions to show up in the cluster state", count);
562562
awaitClusterState(state -> SnapshotDeletionsInProgress.get(state).getEntries().size() == count);
563563
}
@@ -569,7 +569,6 @@ protected void awaitNoMoreRunningOperations() throws Exception {
569569
protected void awaitNoMoreRunningOperations(String viaNode) throws Exception {
570570
logger.info("--> verify no more operations in the cluster state");
571571
awaitClusterState(
572-
logger,
573572
viaNode,
574573
state -> SnapshotsInProgress.get(state).isEmpty() && SnapshotDeletionsInProgress.get(state).hasDeletionsInProgress() == false
575574
);
@@ -604,13 +603,13 @@ public static ActionFuture<CreateSnapshotResponse> startFullSnapshot(
604603
.execute();
605604
}
606605

607-
protected void awaitNumberOfSnapshotsInProgress(int count) throws Exception {
606+
protected void awaitNumberOfSnapshotsInProgress(int count) {
608607
awaitNumberOfSnapshotsInProgress(logger, count);
609608
}
610609

611-
public static void awaitNumberOfSnapshotsInProgress(Logger logger, int count) throws Exception {
610+
public static void awaitNumberOfSnapshotsInProgress(Logger logger, int count) {
612611
logger.info("--> wait for [{}] snapshots to show up in the cluster state", count);
613-
awaitClusterState(logger, state -> SnapshotsInProgress.get(state).count() == count);
612+
awaitClusterState(state -> SnapshotsInProgress.get(state).count() == count);
614613
}
615614

616615
protected SnapshotInfo assertSuccessful(ActionFuture<CreateSnapshotResponse> future) throws Exception {

test/framework/src/main/java/org/elasticsearch/test/ClusterServiceUtils.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@
88
*/
99
package org.elasticsearch.test;
1010

11-
import org.apache.logging.log4j.Logger;
1211
import org.apache.logging.log4j.core.util.Throwables;
1312
import org.elasticsearch.ElasticsearchException;
1413
import org.elasticsearch.action.ActionListener;
15-
import org.elasticsearch.action.support.PlainActionFuture;
1614
import org.elasticsearch.action.support.SubscribableListener;
1715
import org.elasticsearch.cluster.ClusterChangedEvent;
1816
import org.elasticsearch.cluster.ClusterName;
1917
import org.elasticsearch.cluster.ClusterState;
2018
import org.elasticsearch.cluster.ClusterStateListener;
21-
import org.elasticsearch.cluster.ClusterStateObserver;
2219
import org.elasticsearch.cluster.ClusterStatePublicationEvent;
2320
import org.elasticsearch.cluster.ClusterStateUpdateTask;
2421
import org.elasticsearch.cluster.NodeConnectionsService;
@@ -37,14 +34,12 @@
3734
import org.elasticsearch.common.settings.Settings;
3835
import org.elasticsearch.common.util.concurrent.EsExecutors;
3936
import org.elasticsearch.core.TimeValue;
40-
import org.elasticsearch.node.NodeClosedException;
4137
import org.elasticsearch.tasks.TaskManager;
4238
import org.elasticsearch.telemetry.tracing.Tracer;
4339
import org.elasticsearch.threadpool.ThreadPool;
4440

4541
import java.util.Collections;
4642
import java.util.concurrent.CountDownLatch;
47-
import java.util.concurrent.TimeUnit;
4843
import java.util.concurrent.atomic.AtomicReference;
4944
import java.util.function.Predicate;
5045

@@ -210,33 +205,8 @@ public static void setAllElapsedMillis(ClusterStatePublicationEvent clusterState
210205
clusterStatePublicationEvent.setMasterApplyElapsedMillis(0L);
211206
}
212207

213-
public static void awaitClusterState(Logger logger, Predicate<ClusterState> statePredicate, ClusterService clusterService)
214-
throws Exception {
215-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
216-
ClusterStateObserver.waitForState(
217-
clusterService,
218-
clusterService.getClusterApplierService().threadPool().getThreadContext(),
219-
new ClusterStateObserver.Listener() {
220-
@Override
221-
public void onNewClusterState(ClusterState state) {
222-
future.onResponse(null);
223-
}
224-
225-
@Override
226-
public void onClusterServiceClose() {
227-
future.onFailure(new NodeClosedException(clusterService.localNode()));
228-
}
229-
230-
@Override
231-
public void onTimeout(TimeValue timeout) {
232-
assert false : "onTimeout called with no timeout set";
233-
}
234-
},
235-
statePredicate,
236-
null,
237-
logger
238-
);
239-
future.get(30L, TimeUnit.SECONDS);
208+
public static void awaitClusterState(Predicate<ClusterState> statePredicate, ClusterService clusterService) {
209+
ESTestCase.safeAwait(addTemporaryStateListener(clusterService, statePredicate, TimeValue.THIRTY_SECONDS), TimeValue.THIRTY_SECONDS);
240210
}
241211

242212
public static void awaitNoPendingTasks(ClusterService clusterService) {

test/framework/src/main/java/org/elasticsearch/test/ESIntegTestCase.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
1818

1919
import org.apache.http.HttpHost;
20-
import org.apache.logging.log4j.Logger;
2120
import org.apache.lucene.search.Sort;
2221
import org.apache.lucene.search.TotalHits;
2322
import org.apache.lucene.tests.util.LuceneTestCase;
@@ -1140,16 +1139,12 @@ public static PendingClusterTasksResponse getClusterPendingTasks(Client client)
11401139
}
11411140
}
11421141

1143-
protected void awaitClusterState(Predicate<ClusterState> statePredicate) throws Exception {
1144-
awaitClusterState(logger, internalCluster().getMasterName(), statePredicate);
1142+
public static void awaitClusterState(Predicate<ClusterState> statePredicate) {
1143+
awaitClusterState(internalCluster().getMasterName(), statePredicate);
11451144
}
11461145

1147-
public static void awaitClusterState(Logger logger, Predicate<ClusterState> statePredicate) throws Exception {
1148-
awaitClusterState(logger, internalCluster().getMasterName(), statePredicate);
1149-
}
1150-
1151-
public static void awaitClusterState(Logger logger, String viaNode, Predicate<ClusterState> statePredicate) throws Exception {
1152-
ClusterServiceUtils.awaitClusterState(logger, statePredicate, internalCluster().getInstance(ClusterService.class, viaNode));
1146+
public static void awaitClusterState(String viaNode, Predicate<ClusterState> statePredicate) {
1147+
ClusterServiceUtils.awaitClusterState(statePredicate, internalCluster().getInstance(ClusterService.class, viaNode));
11531148
}
11541149

11551150
public static String getNodeId(String nodeName) {

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/MlSingleNodeTestCase.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ public void tearDown() throws Exception {
121121

122122
protected void waitForMlTemplates() throws Exception {
123123
// block until the templates are installed
124-
ClusterServiceUtils.awaitClusterState(
125-
logger,
126-
MachineLearning::criticalTemplatesInstalled,
127-
getInstanceFromNode(ClusterService.class)
128-
);
124+
ClusterServiceUtils.awaitClusterState(MachineLearning::criticalTemplatesInstalled, getInstanceFromNode(ClusterService.class));
129125
}
130126

131127
protected <T> void blockingCall(Consumer<ActionListener<T>> function, AtomicReference<T> response, AtomicReference<Exception> error)

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/support/BaseMlIntegTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
170170
}
171171

172172
@Before
173-
public void ensureTemplatesArePresent() throws Exception {
174-
awaitClusterState(logger, MachineLearning::criticalTemplatesInstalled);
173+
public void ensureTemplatesArePresent() {
174+
awaitClusterState(MachineLearning::criticalTemplatesInstalled);
175175
}
176176

177177
protected Job.Builder createJob(String id) {

x-pack/plugin/searchable-snapshots/src/internalClusterTest/java/org/elasticsearch/xpack/searchablesnapshots/SearchableSnapshotsCanMatchOnCoordinatorIntegTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,7 +1322,7 @@ private static IndexMetadata getIndexMetadata(String indexName) {
13221322
.index(indexName);
13231323
}
13241324

1325-
private void waitUntilAllShardsAreUnassigned(Index index) throws Exception {
1325+
private void waitUntilAllShardsAreUnassigned(Index index) {
13261326
awaitClusterState(state -> state.getRoutingTable().index(index).allPrimaryShardsUnassigned());
13271327
}
13281328

0 commit comments

Comments
 (0)