From e51b0c92c759a11711339ee8904ac0cd6b5b88ee Mon Sep 17 00:00:00 2001 From: Tianli Feng Date: Mon, 3 Oct 2022 22:12:42 -0700 Subject: [PATCH] Add a new node role - remote searcher Signed-off-by: Tianli Feng --- .../main/java/org/opensearch/client/Node.java | 7 ++++++ .../java/org/opensearch/client/NodeTests.java | 7 ++++++ .../admin/cluster/stats/ClusterStatsIT.java | 1 + .../cluster/node/DiscoveryNode.java | 9 +++++++ .../cluster/node/DiscoveryNodeRole.java | 24 ++++++++++++++++++- .../cluster/node/DiscoveryNodeTests.java | 7 ++++++ 6 files changed, 54 insertions(+), 1 deletion(-) diff --git a/client/rest/src/main/java/org/opensearch/client/Node.java b/client/rest/src/main/java/org/opensearch/client/Node.java index c02ac6c68718f..58d1e6f6f19c5 100644 --- a/client/rest/src/main/java/org/opensearch/client/Node.java +++ b/client/rest/src/main/java/org/opensearch/client/Node.java @@ -239,6 +239,13 @@ public boolean isIngest() { return roles.contains("ingest"); } + /** + * Returns whether the node provides search capability for a remote shard. + */ + public boolean isRemoteSearcher() { + return roles.contains("remote_searcher"); + } + @Override public String toString() { return String.join(",", roles); diff --git a/client/rest/src/test/java/org/opensearch/client/NodeTests.java b/client/rest/src/test/java/org/opensearch/client/NodeTests.java index 352296fa3024a..992bb53b04927 100644 --- a/client/rest/src/test/java/org/opensearch/client/NodeTests.java +++ b/client/rest/src/test/java/org/opensearch/client/NodeTests.java @@ -48,7 +48,9 @@ import static java.util.Collections.singletonMap; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; +import static org.hamcrest.CoreMatchers.equalTo; public class NodeTests extends RestClientTestCase { public void testToString() { @@ -161,4 +163,9 @@ public void testEqualsAndHashCode() { ) ); } + + public void testIsRemoteSearcherNode() { + Roles remoteSearcherRole = new Roles(Collections.singleton("remote_searcher")); + assertThat(remoteSearcherRole.isRemoteSearcher(), equalTo(true)); + } } diff --git a/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java b/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java index a44cf05a4bdc4..42829107c7de9 100644 --- a/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java +++ b/server/src/internalClusterTest/java/org/opensearch/action/admin/cluster/stats/ClusterStatsIT.java @@ -89,6 +89,7 @@ public void testNodeCounts() { expectedCounts.put(DiscoveryNodeRole.CLUSTER_MANAGER_ROLE.roleName(), 1); expectedCounts.put(DiscoveryNodeRole.INGEST_ROLE.roleName(), 1); expectedCounts.put(DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE.roleName(), 1); + expectedCounts.put(DiscoveryNodeRole.REMOTE_SEARCHER_ROLE.roleName(), 0); expectedCounts.put(ClusterStatsNodes.Counts.COORDINATING_ONLY, 0); int numNodes = randomIntBetween(1, 5); diff --git a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java index 199d79070c050..32b474c512f60 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java @@ -498,6 +498,15 @@ public boolean isRemoteClusterClient() { return roles.contains(DiscoveryNodeRole.REMOTE_CLUSTER_CLIENT_ROLE); } + /** + * Returns whether the node can provide search capability for a remote shard. + * + * @return true if the node contains remote_searcher role, false otherwise + */ + public boolean isRemoteSearcherNode() { + return roles.contains(DiscoveryNodeRole.REMOTE_SEARCHER_ROLE); + } + /** * Returns a set of all the roles that the node has. The roles are returned in sorted order by the role name. *

diff --git a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java index 5685667c05b1a..7eba0d30c6b77 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java @@ -290,11 +290,33 @@ public Setting legacySetting() { }; + /** + * Represents the role for a remote searcher node. + */ + public static final DiscoveryNodeRole REMOTE_SEARCHER_ROLE = new DiscoveryNodeRole("remote_searcher", "s", true) { + + @Override + public Setting legacySetting() { + // remote_searcher role is added in 2.4 so doesn't need to configure legacy setting + return null; + } + + @Override + public DiscoveryNodeRole getCompatibilityRole(Version nodeVersion) { + if (nodeVersion.onOrAfter(Version.V_2_4_0)) { + return this; + } else { + return DiscoveryNodeRole.DATA_ROLE; + } + } + + }; + /** * The built-in node roles. */ public static SortedSet BUILT_IN_ROLES = Collections.unmodifiableSortedSet( - new TreeSet<>(Arrays.asList(DATA_ROLE, INGEST_ROLE, CLUSTER_MANAGER_ROLE, REMOTE_CLUSTER_CLIENT_ROLE)) + new TreeSet<>(Arrays.asList(DATA_ROLE, INGEST_ROLE, CLUSTER_MANAGER_ROLE, REMOTE_CLUSTER_CLIENT_ROLE, REMOTE_SEARCHER_ROLE)) ); /** diff --git a/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodeTests.java b/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodeTests.java index 70a64fc60bdb4..e9d722887e64b 100644 --- a/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodeTests.java +++ b/server/src/test/java/org/opensearch/cluster/node/DiscoveryNodeTests.java @@ -39,6 +39,7 @@ import org.opensearch.common.settings.Setting; import org.opensearch.common.settings.Settings; import org.opensearch.common.transport.TransportAddress; +import org.opensearch.test.NodeRoles; import org.opensearch.test.OpenSearchTestCase; import java.net.InetAddress; @@ -204,4 +205,10 @@ public void testGetRoleFromRoleNameIsCaseInsensitive() { assertEquals(dynamicRoleName.toLowerCase(Locale.ROOT), dynamicNodeRole.roleName()); assertEquals(dynamicRoleName.toLowerCase(Locale.ROOT), dynamicNodeRole.roleNameAbbreviation()); } + + public void testDiscoveryNodeIsRemoteSearcherNode() { + final Settings settingWithRemoteSearcherRole = NodeRoles.onlyRole(DiscoveryNodeRole.REMOTE_SEARCHER_ROLE); + final DiscoveryNode node = DiscoveryNode.createLocal(settingWithRemoteSearcherRole, buildNewFakeTransportAddress(), "node"); + assertThat(node.isRemoteSearcherNode(), equalTo(true)); + } }