Browse Source

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.
David Turner 3 years ago
parent
commit
33b3ccaabc

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

@@ -119,7 +119,7 @@ public class GoogleCloudStorageBlobStoreRepositoryTests extends ESMockAPIBasedRe
 
     public void testDeleteSingleItem() {
         final String repoName = createRepository(randomRepositoryName());
-        final RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
+        final RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
         final BlobStoreRepository repository = (BlobStoreRepository) repositoriesService.repository(repoName);
         PlainActionFuture.get(
             f -> repository.threadPool()

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

@@ -97,7 +97,7 @@ public class RestGetMappingsCancellationIT extends HttpSmokeTestCase {
         };
 
         PlainActionFuture<AcknowledgedResponse> future = PlainActionFuture.newFuture();
-        internalCluster().getMasterNodeInstance(ClusterService.class)
+        internalCluster().getAnyMasterNodeInstance(ClusterService.class)
             .submitStateUpdateTask("get_mappings_cancellation_test", new AckedClusterStateUpdateTask(ackedRequest, future) {
                 @Override
                 public ClusterState execute(ClusterState currentState) throws Exception {

+ 1 - 1
server/src/internalClusterTest/java/org/elasticsearch/action/admin/cluster/desirednodes/TransportDesiredNodesActionsIT.java

@@ -372,7 +372,7 @@ public class TransportDesiredNodesActionsIT extends ESIntegTestCase {
     private Runnable blockClusterStateUpdateThread() throws InterruptedException {
         final CountDownLatch unblockClusterStateUpdateTask = new CountDownLatch(1);
         final CountDownLatch blockingClusterStateUpdateTaskExecuting = new CountDownLatch(1);
-        final ClusterService clusterService = internalCluster().getCurrentMasterNodeInstance(ClusterService.class);
+        final ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
         clusterService.submitStateUpdateTask("blocking-task", new ClusterStateUpdateTask(Priority.IMMEDIATE) {
             @Override
             public ClusterState execute(ClusterState currentState) throws Exception {

+ 3 - 3
server/src/internalClusterTest/java/org/elasticsearch/cluster/coordination/UnsafeBootstrapAndDetachCommandIT.java

@@ -153,7 +153,7 @@ public class UnsafeBootstrapAndDetachCommandIT extends ESIntegTestCase {
         String node = internalCluster().startNode();
         Settings dataPathSettings = internalCluster().dataPathSettings(node);
         ensureStableCluster(1);
-        NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
+        NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
         internalCluster().stopRandomDataNode();
         Environment environment = TestEnvironment.newEnvironment(
             Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -168,7 +168,7 @@ public class UnsafeBootstrapAndDetachCommandIT extends ESIntegTestCase {
         String node = internalCluster().startNode();
         Settings dataPathSettings = internalCluster().dataPathSettings(node);
         ensureStableCluster(1);
-        NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
+        NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
         internalCluster().stopRandomDataNode();
         Environment environment = TestEnvironment.newEnvironment(
             Settings.builder().put(internalCluster().getDefaultSettings()).put(dataPathSettings).build()
@@ -257,7 +257,7 @@ public class UnsafeBootstrapAndDetachCommandIT extends ESIntegTestCase {
         expectThrows(() -> unsafeBootstrap(environmentMaster1), UnsafeBootstrapMasterCommand.FAILED_TO_OBTAIN_NODE_LOCK_MSG);
 
         logger.info("--> stop 1st master-eligible node and data-only node");
-        NodeEnvironment nodeEnvironment = internalCluster().getMasterNodeInstance(NodeEnvironment.class);
+        NodeEnvironment nodeEnvironment = internalCluster().getAnyMasterNodeInstance(NodeEnvironment.class);
         internalCluster().stopRandomNode(InternalTestCluster.nameFilter(masterNodes.get(0)));
         assertBusy(() -> internalCluster().getInstance(GatewayMetaState.class, dataNode).allPendingAsyncStatesWritten());
         internalCluster().stopRandomDataNode();

+ 1 - 1
server/src/internalClusterTest/java/org/elasticsearch/indices/recovery/IndexRecoveryIT.java

@@ -714,7 +714,7 @@ public class IndexRecoveryIT extends AbstractIndexRecoveryIntegTestCase {
         logger.info("--> request recoveries");
         RecoveryResponse response = client().admin().indices().prepareRecoveries(INDEX_NAME).execute().actionGet();
 
-        Repository repository = internalCluster().getMasterNodeInstance(RepositoriesService.class).repository(REPO_NAME);
+        Repository repository = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class).repository(REPO_NAME);
         final RepositoryData repositoryData = PlainActionFuture.get(repository::getRepositoryData);
         for (Map.Entry<String, List<RecoveryState>> indexRecoveryStates : response.shardRecoveryStates().entrySet()) {
 

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

@@ -281,9 +281,9 @@ public class ShardSnapshotsServiceIT extends ESIntegTestCase {
         createRepository(repositoryName, "fs", randomRepoPath(), true);
         createSnapshot(repositoryName, snapshotName, indexName);
 
-        RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
-        ThreadPool threadPool = internalCluster().getMasterNodeInstance(ThreadPool.class);
-        ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
+        RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
+        ThreadPool threadPool = internalCluster().getAnyMasterNodeInstance(ThreadPool.class);
+        ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
         ShardSnapshotsService shardSnapshotsService = new ShardSnapshotsService(client(), repositoriesService, threadPool, clusterService) {
             @Override
             protected boolean masterSupportsFetchingLatestSnapshots() {
@@ -305,9 +305,9 @@ public class ShardSnapshotsServiceIT extends ESIntegTestCase {
     }
 
     private ShardSnapshotsService getShardSnapshotsService() {
-        RepositoriesService repositoriesService = internalCluster().getMasterNodeInstance(RepositoriesService.class);
-        ThreadPool threadPool = internalCluster().getMasterNodeInstance(ThreadPool.class);
-        ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
+        RepositoriesService repositoriesService = internalCluster().getAnyMasterNodeInstance(RepositoriesService.class);
+        ThreadPool threadPool = internalCluster().getAnyMasterNodeInstance(ThreadPool.class);
+        ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
         return new ShardSnapshotsService(client(), repositoriesService, threadPool, clusterService);
     }
 

+ 1 - 1
server/src/internalClusterTest/java/org/elasticsearch/indices/settings/UpdateSettingsIT.java

@@ -812,7 +812,7 @@ public class UpdateSettingsIT extends ESIntegTestCase {
 
     public void testNoopUpdate() {
         internalCluster().ensureAtLeastNumDataNodes(2);
-        final ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
+        final ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
         assertAcked(
             client().admin()
                 .indices()

+ 1 - 1
server/src/internalClusterTest/java/org/elasticsearch/persistent/PersistentTaskInitializationFailureIT.java

@@ -53,7 +53,7 @@ public class PersistentTaskInitializationFailureIT extends ESIntegTestCase {
         startPersistentTaskFuture.actionGet();
 
         assertBusy(() -> {
-            final ClusterService clusterService = internalCluster().getMasterNodeInstance(ClusterService.class);
+            final ClusterService clusterService = internalCluster().getAnyMasterNodeInstance(ClusterService.class);
             final PersistentTasksCustomMetadata persistentTasks = clusterService.state()
                 .metadata()
                 .custom(PersistentTasksCustomMetadata.TYPE);

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

@@ -269,7 +269,7 @@ public abstract class ESBlobStoreRepositoryIntegTestCase extends ESIntegTestCase
     }
 
     protected BlobStore newBlobStore(String repository) {
-        final BlobStoreRepository blobStoreRepository = (BlobStoreRepository) internalCluster().getMasterNodeInstance(
+        final BlobStoreRepository blobStoreRepository = (BlobStoreRepository) internalCluster().getAnyMasterNodeInstance(
             RepositoriesService.class
         ).repository(repository);
         return PlainActionFuture.get(

+ 8 - 1
test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

@@ -1558,6 +1558,9 @@ public final class InternalTestCluster extends TestCluster {
         return getInstances(clazz, DATA_NODE_PREDICATE);
     }
 
+    /**
+     * Return the instance of the given class &gt;T&lt; from the elected master node. See also {@link #getAnyMasterNodeInstance}.
+     */
     public synchronized <T> T getCurrentMasterNodeInstance(Class<T> clazz) {
         return getInstance(clazz, new NodeNamePredicate(getMasterName()));
     }
@@ -1590,7 +1593,11 @@ public final class InternalTestCluster extends TestCluster {
         return getInstance(clazz, DATA_NODE_PREDICATE);
     }
 
-    public <T> T getMasterNodeInstance(Class<T> clazz) {
+    /**
+     * Return the instance of the given class &gt;T&lt; from any of the master-eligible nodes (not necessarily the elected master). See
+     * also {@link #getCurrentMasterNodeInstance}.
+     */
+    public <T> T getAnyMasterNodeInstance(Class<T> clazz) {
         return getInstance(clazz, MASTER_NODE_PREDICATE);
     }
 

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

@@ -83,27 +83,27 @@ public class GetAutoscalingCapacityRestCancellationIT extends AutoscalingIntegTe
             PlainActionFuture<Response> successFuture2 = new PlainActionFuture<>();
             Request getCapacityRequest = new Request("GET", "/_autoscaling/capacity");
             Cancellable cancellable = restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(cancelledFuture));
-            LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getMasterNodeInstance(PluginsService.class)
+            LocalStateAutoscaling.AutoscalingTestPlugin plugin = internalCluster().getAnyMasterNodeInstance(PluginsService.class)
                 .filterPlugins(LocalStateAutoscaling.class)
                 .get(0)
                 .testPlugin();
             plugin.syncWithDeciderService(() -> {
                 putAutoscalingPolicy(Map.of(AutoscalingCountTestDeciderService.NAME, Settings.EMPTY));
                 assertThat(
-                    internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
+                    internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
                     equalTo(1)
                 );
                 restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture1));
                 assertBusy(
                     () -> assertThat(
-                        internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
+                        internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
                         equalTo(2)
                     )
                 );
                 restClient.performRequestAsync(getCapacityRequest, wrapAsRestResponseListener(successFuture2));
                 assertBusy(
                     () -> assertThat(
-                        internalCluster().getMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
+                        internalCluster().getAnyMasterNodeInstance(TransportGetAutoscalingCapacityAction.class).responseCacheQueueSize(),
                         equalTo(3)
                     )
                 );
@@ -130,7 +130,7 @@ public class GetAutoscalingCapacityRestCancellationIT extends AutoscalingIntegTe
 
     private void waitForCancelledCapacityTask() throws Exception {
         assertBusy(() -> {
-            TransportService transportService = internalCluster().getMasterNodeInstance(TransportService.class);
+            TransportService transportService = internalCluster().getAnyMasterNodeInstance(TransportService.class);
             final TaskManager taskManager = transportService.getTaskManager();
             assertTrue(taskManager.assertCancellableTaskConsistency());
             for (CancellableTask cancellableTask : taskManager.getCancellableTasks().values()) {

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

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