Skip to content

Commit df1d811

Browse files
committed
Clean up awaitClusterState overloads
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 9.1
1 parent 8b83a90 commit df1d811

File tree

12 files changed

+17
-60
lines changed

12 files changed

+17
-60
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
@@ -204,7 +204,7 @@ public void testSortAndPaginateWithInProgress() throws Exception {
204204
inProgressSnapshots.add(AbstractSnapshotIntegTestCase.startFullSnapshot(logger, repoName, snapshotName, false));
205205
}
206206
AbstractSnapshotIntegTestCase.awaitNumberOfSnapshotsInProgress(logger, inProgressCount);
207-
AbstractSnapshotIntegTestCase.awaitClusterState(logger, state -> {
207+
AbstractSnapshotIntegTestCase.awaitClusterState(state -> {
208208
final var snapshotsInProgress = SnapshotsInProgress.get(state);
209209
boolean firstIndexSuccessfullySnapshot = snapshotsInProgress.asStream()
210210
.flatMap(s -> s.shards().entrySet().stream())

server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/state/TransportClusterStateActionDisruptionIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,6 @@ public void runRepeatedlyWhileChangingMaster(Runnable runnable) throws Exception
213213

214214
final String nonMasterNode = randomValueOtherThan(masterName, () -> randomFrom(internalCluster().getNodeNames()));
215215
awaitClusterState(
216-
logger,
217216
nonMasterNode,
218217
state -> Optional.ofNullable(state.nodes().getMasterNode()).map(m -> m.getName().equals(masterName) == false).orElse(false)
219218
);

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

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

10791079
awaitClusterState(
1080-
logger,
10811080
otherDataNode,
10821081
state -> SnapshotsInProgress.get(state)
10831082
.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

@@ -219,33 +214,8 @@ public static void setAllElapsedMillis(ClusterStatePublicationEvent clusterState
219214
clusterStatePublicationEvent.setMasterApplyElapsedMillis(0L);
220215
}
221216

222-
public static void awaitClusterState(Logger logger, Predicate<ClusterState> statePredicate, ClusterService clusterService)
223-
throws Exception {
224-
final PlainActionFuture<Void> future = new PlainActionFuture<>();
225-
ClusterStateObserver.waitForState(
226-
clusterService,
227-
clusterService.getClusterApplierService().threadPool().getThreadContext(),
228-
new ClusterStateObserver.Listener() {
229-
@Override
230-
public void onNewClusterState(ClusterState state) {
231-
future.onResponse(null);
232-
}
233-
234-
@Override
235-
public void onClusterServiceClose() {
236-
future.onFailure(new NodeClosedException(clusterService.localNode()));
237-
}
238-
239-
@Override
240-
public void onTimeout(TimeValue timeout) {
241-
assert false : "onTimeout called with no timeout set";
242-
}
243-
},
244-
statePredicate,
245-
null,
246-
logger
247-
);
248-
future.get(30L, TimeUnit.SECONDS);
217+
public static void awaitClusterState(Predicate<ClusterState> statePredicate, ClusterService clusterService) {
218+
ESTestCase.safeAwait(addTemporaryStateListener(clusterService, statePredicate, TimeValue.THIRTY_SECONDS), TimeValue.THIRTY_SECONDS);
249219
}
250220

251221
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;
@@ -1207,16 +1206,12 @@ public static PendingClusterTasksResponse getClusterPendingTasks(Client client)
12071206
}
12081207
}
12091208

1210-
protected void awaitClusterState(Predicate<ClusterState> statePredicate) throws Exception {
1211-
awaitClusterState(logger, internalCluster().getMasterName(), statePredicate);
1209+
public static void awaitClusterState(Predicate<ClusterState> statePredicate) {
1210+
awaitClusterState(internalCluster().getMasterName(), statePredicate);
12121211
}
12131212

1214-
public static void awaitClusterState(Logger logger, Predicate<ClusterState> statePredicate) throws Exception {
1215-
awaitClusterState(logger, internalCluster().getMasterName(), statePredicate);
1216-
}
1217-
1218-
public static void awaitClusterState(Logger logger, String viaNode, Predicate<ClusterState> statePredicate) throws Exception {
1219-
ClusterServiceUtils.awaitClusterState(logger, statePredicate, internalCluster().getInstance(ClusterService.class, viaNode));
1213+
public static void awaitClusterState(String viaNode, Predicate<ClusterState> statePredicate) {
1214+
ClusterServiceUtils.awaitClusterState(statePredicate, internalCluster().getInstance(ClusterService.class, viaNode));
12201215
}
12211216

12221217
public static String getNodeId(String nodeName) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2064,7 +2064,7 @@ public String getMasterName(@Nullable String viaNode) {
20642064
throw new AssertionError("Unable to get master name, no node found");
20652065
}
20662066
try {
2067-
ClusterServiceUtils.awaitClusterState(logger, state -> state.nodes().getMasterNode() != null, clusterService(viaNode));
2067+
ClusterServiceUtils.awaitClusterState(state -> state.nodes().getMasterNode() != null, clusterService(viaNode));
20682068
final ClusterState state = client(viaNode).admin().cluster().prepareState(TEST_REQUEST_TIMEOUT).setLocal(true).get().getState();
20692069
final DiscoveryNode masterNode = state.nodes().getMasterNode();
20702070
if (masterNode == null) {

x-pack/plugin/ilm/src/test/java/org/elasticsearch/xpack/ilm/IndexLifecycleRunnerTests.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ public void doTestRunPolicyWithFailureToReadPolicy(boolean asyncAction, boolean
488488

489489
// The cluster state can take a few extra milliseconds to update after the steps are executed
490490
ClusterServiceUtils.awaitClusterState(
491-
logger,
492491
s -> s.metadata().getProject(state.projectId()).index(indexMetadata.getIndex()).getLifecycleExecutionState().stepInfo() != null,
493492
clusterService
494493
);

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
@@ -169,9 +169,9 @@ protected Collection<Class<? extends Plugin>> getMockPlugins() {
169169
}
170170

171171
@Before
172-
public void ensureTemplatesArePresent() throws Exception {
172+
public void ensureTemplatesArePresent() {
173173
if (cluster().size() > 0) {
174-
awaitClusterState(logger, MachineLearning::criticalTemplatesInstalled);
174+
awaitClusterState(MachineLearning::criticalTemplatesInstalled);
175175
}
176176
}
177177

0 commit comments

Comments
 (0)