From c829e4170adff3b0bf5284140ad9ba14cabd6feb Mon Sep 17 00:00:00 2001 From: Jeremy Dahlgren Date: Wed, 2 Jul 2025 17:06:12 -0400 Subject: [PATCH] Remove redundant method for getting the remote cluster names This change removes RemoteClusterService.getRemoteClusterNames() since getRegisteredRemoteClusterNames() provides the same functionality. The comment in getRegisteredRemoteClusterNames() was removed since it is no longer accurate after the change in PR #47891. --- .../transport/RemoteClusterService.java | 13 ++----- .../transport/RemoteClusterServiceTests.java | 38 ++++++++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java b/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java index fdb597b47c137..d1816c7fc1687 100644 --- a/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java +++ b/server/src/main/java/org/elasticsearch/transport/RemoteClusterService.java @@ -247,16 +247,16 @@ public Map groupIndices(Set remoteClusterNames, } public Map groupIndices(IndicesOptions indicesOptions, String[] indices, boolean returnLocalAll) { - return groupIndices(getRemoteClusterNames(), indicesOptions, indices, returnLocalAll); + return groupIndices(getRegisteredRemoteClusterNames(), indicesOptions, indices, returnLocalAll); } public Map groupIndices(IndicesOptions indicesOptions, String[] indices) { - return groupIndices(getRemoteClusterNames(), indicesOptions, indices, true); + return groupIndices(getRegisteredRemoteClusterNames(), indicesOptions, indices, true); } @Override public Set getConfiguredClusters() { - return getRemoteClusterNames(); + return getRegisteredRemoteClusterNames(); } /** @@ -270,7 +270,6 @@ boolean isRemoteClusterRegistered(String clusterName) { * Returns the registered remote cluster names. */ public Set getRegisteredRemoteClusterNames() { - // remoteClusters is unmodifiable so its key set will be unmodifiable too return remoteClusters.keySet(); } @@ -355,10 +354,6 @@ public RemoteClusterConnection getRemoteClusterConnection(String cluster) { return connection; } - Set getRemoteClusterNames() { - return this.remoteClusters.keySet(); - } - @Override public void listenForUpdates(ClusterSettings clusterSettings) { super.listenForUpdates(clusterSettings); @@ -648,7 +643,7 @@ public RemoteClusterClient getRemoteClusterClient( "this node does not have the " + DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName() + " role" ); } - if (transportService.getRemoteClusterService().getRemoteClusterNames().contains(clusterAlias) == false) { + if (transportService.getRemoteClusterService().getRegisteredRemoteClusterNames().contains(clusterAlias) == false) { throw new NoSuchRemoteClusterException(clusterAlias); } return new RemoteClusterAwareClient(transportService, clusterAlias, responseExecutor, switch (disconnectedStrategy) { diff --git a/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java b/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java index 3633128c45bfa..ebed5328107c0 100644 --- a/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java +++ b/server/src/test/java/org/elasticsearch/transport/RemoteClusterServiceTests.java @@ -170,7 +170,7 @@ public void testGroupClusterIndices() throws IOException { assertFalse(service.isRemoteClusterRegistered("foo")); { Map> perClusterIndices = service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), new String[] { "cluster_1:bar", "cluster_2:foo:bar", @@ -191,7 +191,7 @@ public void testGroupClusterIndices() throws IOException { expectThrows( NoSuchRemoteClusterException.class, () -> service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), new String[] { "foo:bar", "cluster_1:bar", "cluster_2:foo:bar", "cluster_1:test", "cluster_2:foo*", "foo" } ) ); @@ -199,7 +199,7 @@ public void testGroupClusterIndices() throws IOException { expectThrows( NoSuchRemoteClusterException.class, () -> service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), new String[] { "cluster_1:bar", "cluster_2:foo:bar", "cluster_1:test", "cluster_2:foo*", "does_not_exist:*" } ) ); @@ -208,7 +208,10 @@ public void testGroupClusterIndices() throws IOException { { String[] indices = shuffledList(List.of("cluster*:foo*", "foo", "-cluster_1:*", "*:boo")).toArray(new String[0]); - Map> perClusterIndices = service.groupClusterIndices(service.getRemoteClusterNames(), indices); + Map> perClusterIndices = service.groupClusterIndices( + service.getRegisteredRemoteClusterNames(), + indices + ); assertEquals(2, perClusterIndices.size()); List localIndexes = perClusterIndices.get(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY); assertNotNull(localIndexes); @@ -223,7 +226,10 @@ public void testGroupClusterIndices() throws IOException { { String[] indices = shuffledList(List.of("*:*", "-clu*_1:*", "*:boo")).toArray(new String[0]); - Map> perClusterIndices = service.groupClusterIndices(service.getRemoteClusterNames(), indices); + Map> perClusterIndices = service.groupClusterIndices( + service.getRegisteredRemoteClusterNames(), + indices + ); assertEquals(1, perClusterIndices.size()); List cluster2 = perClusterIndices.get("cluster_2"); @@ -236,7 +242,10 @@ public void testGroupClusterIndices() throws IOException { new String[0] ); - Map> perClusterIndices = service.groupClusterIndices(service.getRemoteClusterNames(), indices); + Map> perClusterIndices = service.groupClusterIndices( + service.getRegisteredRemoteClusterNames(), + indices + ); assertEquals(1, perClusterIndices.size()); List localIndexes = perClusterIndices.get(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY); assertNotNull(localIndexes); @@ -246,7 +255,10 @@ public void testGroupClusterIndices() throws IOException { { String[] indices = shuffledList(List.of("cluster*:*", "foo", "-*:*")).toArray(new String[0]); - Map> perClusterIndices = service.groupClusterIndices(service.getRemoteClusterNames(), indices); + Map> perClusterIndices = service.groupClusterIndices( + service.getRegisteredRemoteClusterNames(), + indices + ); assertEquals(1, perClusterIndices.size()); List localIndexes = perClusterIndices.get(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY); assertNotNull(localIndexes); @@ -257,7 +269,7 @@ public void testGroupClusterIndices() throws IOException { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), // -cluster_1:foo* is not allowed, only -cluster_1:* new String[] { "cluster_1:bar", "-cluster_2:foo*", "cluster_1:test", "cluster_2:foo*", "foo" } ) @@ -271,7 +283,7 @@ public void testGroupClusterIndices() throws IOException { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), // -cluster_1:* will fail since cluster_1 was never included in order to qualify to be excluded new String[] { "-cluster_1:*", "cluster_2:foo*", "foo" } ) @@ -287,7 +299,7 @@ public void testGroupClusterIndices() throws IOException { { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> service.groupClusterIndices(service.getRemoteClusterNames(), new String[] { "-cluster_1:*" }) + () -> service.groupClusterIndices(service.getRegisteredRemoteClusterNames(), new String[] { "-cluster_1:*" }) ); assertThat( e.getMessage(), @@ -300,7 +312,7 @@ public void testGroupClusterIndices() throws IOException { { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> service.groupClusterIndices(service.getRemoteClusterNames(), new String[] { "-*:*" }) + () -> service.groupClusterIndices(service.getRegisteredRemoteClusterNames(), new String[] { "-*:*" }) ); assertThat( e.getMessage(), @@ -315,7 +327,7 @@ public void testGroupClusterIndices() throws IOException { IllegalArgumentException e = expectThrows( IllegalArgumentException.class, - () -> service.groupClusterIndices(service.getRemoteClusterNames(), indices) + () -> service.groupClusterIndices(service.getRegisteredRemoteClusterNames(), indices) ); assertThat( e.getMessage(), @@ -394,7 +406,7 @@ public void testGroupIndices() throws IOException { expectThrows( NoSuchRemoteClusterException.class, () -> service.groupClusterIndices( - service.getRemoteClusterNames(), + service.getRegisteredRemoteClusterNames(), new String[] { "foo:bar", "cluster_1:bar", "cluster_2:foo:bar", "cluster_1:test", "cluster_2:foo*", "foo" } ) );