Skip to content

Commit c22f77e

Browse files
committed
Rename InternalTestCluster#getMasterNodeInstance (#83407)
This method's name is trappy: it is easy to misinterpret it as returning an instance from the elected master, but in fact it uses any master-eligible node. If you want an instance from the elected master, you have to use `getCurrentMasterNodeInstance()` instead. This commit renames the method to clarify that it might not get an instance from the elected master, and adds docs with cross-refs to help developers choose the right method.
1 parent 63498dd commit c22f77e

File tree

11 files changed

+29
-22
lines changed

11 files changed

+29
-22
lines changed

modules/repository-gcs/src/internalClusterTest/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStoreRepositoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
119119

120120
public void testDeleteSingleItem() {
121121
final String repoName = createRepository(randomRepositoryName());
122-
final RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
122+
final RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
123123
final BlobStoreRepository repository = (BlobStoreRepository) repositoriesService.repository(repoName);
124124
PlainActionFuture.get(
125125
f -> repository.threadPool()

qa/smoke-test-http/src/test/java/org/elasticsearch/http/RestGetMappingsCancellationIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public TimeValue masterNodeTimeout() {
9696
};
9797

9898
PlainActionFuture<AcknowledgedResponse> future = PlainActionFuture.newFuture();
99-
internalCluster().getMasterNodeInstance(ClusterService.class)
99+
internalCluster().getAnyMasterNodeInstance(ClusterService.class)
100100
.submitStateUpdateTask("get_mappings_cancellation_test", new AckedClusterStateUpdateTask(ackedRequest, future) {
101101
@Override
102102
public ClusterState execute(ClusterState currentState) throws Exception {

server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapAndDetachCommandIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ public void testBootstrapNoClusterState() throws IOException {
153153
String node = internalCluster().startNode();
154154
Settings dataPathSettings = internalCluster().dataPathSettings(node);
155155
ensureStableCluster(1);
156-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
156+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
157157
internalCluster().stopRandomDataNode();
158158
Environment environment = TestEnvironment.newEnvironment(
159159
Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -168,7 +168,7 @@ public void testDetachNoClusterState() throws IOException {
168168
String node = internalCluster().startNode();
169169
Settings dataPathSettings = internalCluster().dataPathSettings(node);
170170
ensureStableCluster(1);
171-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
171+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
172172
internalCluster().stopRandomDataNode();
173173
Environment environment = TestEnvironment.newEnvironment(
174174
Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -257,7 +257,7 @@ public void test3MasterNodes2Failed() throws Exception {
257257
expectThrows(() -> unsafeBootstrap(environmentMaster1), UnsafeBootstrapMasterCommand.FAILED_TO_OBTAIN_NODE_LOCK_MSG);
258258

259259
logger.info("--> stop 1st master-eligible node and data-only node");
260-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
260+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
261261
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(0)));
262262
assertBusy(() -> internalCluster().getInstance(GatewayMetaState.class, dataNode).allPendingAsyncStatesWritten());
263263
internalCluster().stopRandomDataNode();

server/src/internalClusterTest/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ public void testSnapshotRecovery() throws Exception {
698698
logger.info("--> request recoveries");
699699
RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
700700

701-
Repository repository = internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(REPO_NAME);
701+
Repository repository = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class).repository(REPO_NAME);
702702
final RepositoryData repositoryData = PlainActionFuture.get(repository::getRepositoryData);
703703
for (Map.Entry<String, List<RecoveryState>> indexRecoveryStates : response.shardRecoveryStates().entrySet()) {
704704

server/src/internalClusterTest/java/org/elasticsearch/indices/recovery/plan/ShardSnapshotsServiceIT.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ public void testFetchingInformationFromAnIncompatibleMasterNodeReturnsAnEmptyLis
281281
createRepository(repositoryName, "fs", randomRepoPath(), true);
282282
createSnapshot(repositoryName, snapshotName, indexName);
283283

284-
RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
285-
ThreadPool threadPool = internalCluster().getMasterNodeInstance(ThreadPool.class);
286-
ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
284+
RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
285+
ThreadPool threadPool = internalCluster().getAnyMasterNodeInstance(ThreadPool.class);
286+
ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
287287
ShardSnapshotsService shardSnapshotsService = new ShardSnapshotsService(client(), repositoriesService, threadPool, clusterService) {
288288
@Override
289289
protected boolean masterSupportsFetchingLatestSnapshots() {
@@ -305,9 +305,9 @@ private Optional<ShardSnapshot> getLatestShardSnapshot(ShardId shardId) throws E
305305
}
306306

307307
private ShardSnapshotsService getShardSnapshotsService() {
308-
RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
309-
ThreadPool threadPool = internalCluster().getMasterNodeInstance(ThreadPool.class);
310-
ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
308+
RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
309+
ThreadPool threadPool = internalCluster().getAnyMasterNodeInstance(ThreadPool.class);
310+
ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
311311
return new ShardSnapshotsService(client(), repositoriesService, threadPool, clusterService);
312312
}
313313

server/src/internalClusterTest/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ private void runTestDefaultNumberOfReplicasTest(final boolean closeIndex) {
812812

813813
public void testNoopUpdate() {
814814
internalCluster().ensureAtLeastNumDataNodes(2);
815-
final ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
815+
final ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
816816
assertAcked(
817817
client().admin()
818818
.indices()

server/src/internalClusterTest/java/org/elasticsearch/persistent/PersistentTaskInitializationFailureIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void testPersistentTasksThatFailDuringInitializationAreRemovedFromCluster
5353
startPersistentTaskFuture.actionGet();
5454

5555
assertBusy(() -> {
56-
final ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
56+
final ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
5757
final PersistentTasksCustomMetadata persistentTasks = clusterService.state()
5858
.metadata()
5959
.custom(PersistentTasksCustomMetadata.TYPE);

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/ESBlobStoreRepositoryIntegTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ protected BlobStore newBlobStore() {
269269
}
270270

271271
protected BlobStore newBlobStore(String repository) {
272-
final BlobStoreRepository blobStoreRepository = (BlobStoreRepository) internalCluster().getMasterNodeInstance(
272+
final BlobStoreRepository blobStoreRepository = (BlobStoreRepository) internalCluster().getAnyMasterNodeInstance(
273273
RepositoriesService.class
274274
).repository(repository);
275275
return PlainActionFuture.get(

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,9 @@ public <T> Iterable<T> getDataNodeInstances(Class<T> clazz) {
15451545
return getInstances(clazz, DATA_NODE_PREDICATE);
15461546
}
15471547

1548+
/**
1549+
* Return the instance of the given class &gt;T&lt; from the elected master node. See also {@link #getAnyMasterNodeInstance}.
1550+
*/
15481551
public synchronized <T> T getCurrentMasterNodeInstance(Class<T> clazz) {
15491552
return getInstance(clazz, new NodeNamePredicate(getMasterName()));
15501553
}
@@ -1577,7 +1580,11 @@ public <T> T getDataNodeInstance(Class<T> clazz) {
15771580
return getInstance(clazz, DATA_NODE_PREDICATE);
15781581
}
15791582

1580-
public <T> T getMasterNodeInstance(Class<T> clazz) {
1583+
/**
1584+
* Return the instance of the given class &gt;T&lt; from any of the master-eligible nodes (not necessarily the elected master). See
1585+
* also {@link #getCurrentMasterNodeInstance}.
1586+
*/
1587+
public <T> T getAnyMasterNodeInstance(Class<T> clazz) {
15811588
return getInstance(clazz, MASTER_NODE_PREDICATE);
15821589
}
15831590

x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/action/GetAutoscalingCapacityRestCancellationIT.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,27 +83,27 @@ public void testCapacityRestCancellationAndResponse() throws Exception {
8383
PlainActionFuture<Response> successFuture2 = new PlainActionFuture<>();
8484
Request getCapacityRequest = new Request("GET", "/_autoscaling/capacity");
8585
Cancellable cancellable = restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(cancelledFuture));
86-
LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getMasterNodeInstance(PluginsService.class)
86+
LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getAnyMasterNodeInstance(PluginsService.class)
8787
.filterPlugins(LocalStateAutoscaling.class)
8888
.get(0)
8989
.testPlugin();
9090
plugin.syncWithDeciderService(() -> {
9191
putAutoscalingPolicy(Map.of(AutoscalingCountTestDeciderService.NAME, Settings.EMPTY));
9292
assertThat(
93-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
93+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
9494
equalTo(1)
9595
);
9696
restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture1));
9797
assertBusy(
9898
() -> assertThat(
99-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
99+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
100100
equalTo(2)
101101
)
102102
);
103103
restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture2));
104104
assertBusy(
105105
() -> assertThat(
106-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
106+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
107107
equalTo(3)
108108
)
109109
);
@@ -130,7 +130,7 @@ public void testCapacityRestCancellationAndResponse() throws Exception {
130130

131131
private void waitForCancelledCapacityTask() throws Exception {
132132
assertBusy(() -> {
133-
TransportService transportService = internalCluster().getMasterNodeInstance(TransportService.class);
133+
TransportService transportService = internalCluster().getAnyMasterNodeInstance(TransportService.class);
134134
final TaskManager taskManager = transportService.getTaskManager();
135135
assertTrue(taskManager.assertCancellableTaskConsistency());
136136
for (CancellableTask cancellableTask : taskManager.getCancellableTasks().values()) {

x-pack/plugin/ccr/src/internalClusterTest/java/org/elasticsearch/xpack/ccr/RestartIndexFollowingIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public void testFollowIndex() throws Exception {
117117

118118
private void setupRemoteCluster() throws Exception {
119119
ClusterUpdateSettingsRequest updateSettingsRequest = new ClusterUpdateSettingsRequest().masterNodeTimeout(TimeValue.MAX_VALUE);
120-
String address = getLeaderCluster().getMasterNodeInstance(TransportService.class).boundAddress().publishAddress().toString();
120+
String address = getLeaderCluster().getAnyMasterNodeInstance(TransportService.class).boundAddress().publishAddress().toString();
121121
updateSettingsRequest.persistentSettings(Settings.builder().put("cluster.remote.leader_cluster.seeds", address));
122122
assertAcked(followerClient().admin().cluster().updateSettings(updateSettingsRequest).actionGet());
123123
List<RemoteConnectionInfo> infos = followerClient().execute(RemoteInfoAction.INSTANCE, new RemoteInfoRequest()).get().getInfos();

0 commit comments

Comments
 (0)