Skip to content

Commit c39a08f

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 dea02fa commit c39a08f

File tree

13 files changed

+32
-25
lines changed

13 files changed

+32
-25
lines changed

plugins/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
@@ -135,7 +135,7 @@ protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) {
135135

136136
public void testDeleteSingleItem() {
137137
final String repoName = createRepository(randomRepositoryName());
138-
final RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
138+
final RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
139139
final BlobStoreRepository repository = (BlobStoreRepository) repositoriesService.repository(repoName);
140140
PlainActionFuture.get(
141141
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
@@ -159,7 +159,7 @@ public void testBootstrapNoClusterState() throws IOException {
159159
String node = internalCluster().startNode();
160160
Settings dataPathSettings = internalCluster().dataPathSettings(node);
161161
ensureStableCluster(1);
162-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
162+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
163163
internalCluster().stopRandomDataNode();
164164
Environment environment = TestEnvironment.newEnvironment(
165165
Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -174,7 +174,7 @@ public void testDetachNoClusterState() throws IOException {
174174
String node = internalCluster().startNode();
175175
Settings dataPathSettings = internalCluster().dataPathSettings(node);
176176
ensureStableCluster(1);
177-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
177+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
178178
internalCluster().stopRandomDataNode();
179179
Environment environment = TestEnvironment.newEnvironment(
180180
Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -265,7 +265,7 @@ public void test3MasterNodes2Failed() throws Exception {
265265
expectThrows(() -> unsafeBootstrap(environmentMaster1), UnsafeBootstrapMasterCommand.FAILED_TO_OBTAIN_NODE_LOCK_MSG);
266266

267267
logger.info("--> stop 1st master-eligible node and data-only node");
268-
NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
268+
NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
269269
internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(0)));
270270
assertBusy(() -> internalCluster().getInstance(GatewayMetaState.class, dataNode).allPendingAsyncStatesWritten());
271271
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
@@ -720,7 +720,7 @@ public void testSnapshotRecovery() throws Exception {
720720
logger.info("--> request recoveries");
721721
RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
722722

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

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
@@ -811,7 +811,7 @@ private void runTestDefaultNumberOfReplicasTest(final boolean closeIndex) {
811811

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ public void testCreateSnapshotLegacyPath() throws Exception {
11081108
createRepository(repoName, "fs");
11091109
createIndex("some-index");
11101110

1111-
final SnapshotsService snapshotsService = internalCluster().getMasterNodeInstance(SnapshotsService.class);
1111+
final SnapshotsService snapshotsService = internalCluster().getAnyMasterNodeInstance(SnapshotsService.class);
11121112
final Snapshot snapshot1 = PlainActionFuture.get(
11131113
f -> snapshotsService.createSnapshotLegacy(new CreateSnapshotRequest(repoName, "snap-1"), f)
11141114
);

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
@@ -1755,6 +1755,9 @@ public <T> Iterable<T> getDataNodeInstances(Class<T> clazz) {
17551755
return getInstances(clazz, DATA_NODE_PREDICATE);
17561756
}
17571757

1758+
/**
1759+
* Return the instance of the given class &gt;T&lt; from the elected master node. See also {@link #getAnyMasterNodeInstance}.
1760+
*/
17581761
public synchronized <T> T getCurrentMasterNodeInstance(Class<T> clazz) {
17591762
return getInstance(clazz, new NodeNamePredicate(getMasterName()));
17601763
}
@@ -1787,7 +1790,11 @@ public <T> T getDataNodeInstance(Class<T> clazz) {
17871790
return getInstance(clazz, DATA_NODE_PREDICATE);
17881791
}
17891792

1790-
public <T> T getMasterNodeInstance(Class<T> clazz) {
1793+
/**
1794+
* Return the instance of the given class &gt;T&lt; from any of the master-eligible nodes (not necessarily the elected master). See
1795+
* also {@link #getCurrentMasterNodeInstance}.
1796+
*/
1797+
public <T> T getAnyMasterNodeInstance(Class<T> clazz) {
17911798
return getInstance(clazz, MASTER_NODE_PREDICATE);
17921799
}
17931800

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
@@ -82,27 +82,27 @@ public void testCapacityRestCancellationAndResponse() throws Exception {
8282
PlainActionFuture<Response> successFuture2 = new PlainActionFuture<>();
8383
Request getCapacityRequest = new Request("GET", "/_autoscaling/capacity");
8484
Cancellable cancellable = restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(cancelledFuture));
85-
LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getMasterNodeInstance(PluginsService.class)
85+
LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getAnyMasterNodeInstance(PluginsService.class)
8686
.filterPlugins(LocalStateAutoscaling.class)
8787
.get(0)
8888
.testPlugin();
8989
plugin.syncWithDeciderService(() -> {
9090
putAutoscalingPolicy(org.elasticsearch.core.Map.of(AutoscalingCountTestDeciderService.NAME, Settings.EMPTY));
9191
assertThat(
92-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
92+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
9393
equalTo(1)
9494
);
9595
restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture1));
9696
assertBusy(
9797
() -> assertThat(
98-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
98+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
9999
equalTo(2)
100100
)
101101
);
102102
restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture2));
103103
assertBusy(
104104
() -> assertThat(
105-
internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
105+
internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
106106
equalTo(3)
107107
)
108108
);
@@ -129,7 +129,7 @@ public void testCapacityRestCancellationAndResponse() throws Exception {
129129

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -740,7 +740,7 @@ private void createLeaderSystemIndex(String indexName) {
740740
}
741741

742742
private List<String> getFollowerAutoFollowedSystemIndices() {
743-
final ClusterService followerClusterService = getFollowerCluster().getMasterNodeInstance(ClusterService.class);
743+
final ClusterService followerClusterService = getFollowerCluster().getAnyMasterNodeInstance(ClusterService.class);
744744
PlainActionFuture<List<String>> future = PlainActionFuture.newFuture();
745745
CcrAutoFollowInfoFetcher.getAutoFollowedSystemIndices(followerClient(), followerClusterService.state(), future);
746746
return future.actionGet();

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();

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ServerTransportFilterIntegrationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testThatConnectionToServerTypeConnectionWorks() throws IOException,
9393
Path xpackConf = home.resolve("config");
9494
Files.createDirectories(xpackConf);
9595

96-
Transport transport = internalCluster().getMasterNodeInstance(Transport.class);
96+
Transport transport = internalCluster().getAnyMasterNodeInstance(Transport.class);
9797
TransportAddress transportAddress = transport.boundAddress().publishAddress();
9898
String unicastHost = NetworkAddress.format(transportAddress.address());
9999

@@ -133,7 +133,7 @@ public void testThatConnectionToClientTypeConnectionIsRejected() throws IOExcept
133133
writeFile(xpackConf, "users_roles", configUsersRoles());
134134
writeFile(xpackConf, "roles.yml", configRoles());
135135

136-
Transport transport = internalCluster().getMasterNodeInstance(Transport.class);
136+
Transport transport = internalCluster().getAnyMasterNodeInstance(Transport.class);
137137
TransportAddress transportAddress = transport.profileBoundAddresses().get("client").publishAddress();
138138
String unicastHost = NetworkAddress.format(transportAddress.address());
139139

0 commit comments

Comments
 (0)