From f8d8f0bdec53bac678381719034f00aed39edf81 Mon Sep 17 00:00:00 2001 From: Varun Bansal Date: Thu, 9 May 2024 16:28:29 +0530 Subject: [PATCH] add new role for Offline Nodes Signed-off-by: Varun Bansal --- .../opensearch/cluster/node/DiscoveryNode.java | 4 ++++ .../cluster/node/DiscoveryNodeRole.java | 12 ++++++++++++ .../java/org/opensearch/env/NodeEnvironment.java | 15 +++++++++++++++ 3 files changed, 31 insertions(+) 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 5226e9570ac14..a47c74228a0f5 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNode.java @@ -128,6 +128,10 @@ public static boolean isSearchNode(Settings settings) { return hasRole(settings, DiscoveryNodeRole.SEARCH_ROLE); } + public static boolean isOfflineNode(Settings settings) { + return hasRole(settings, DiscoveryNodeRole.OFFLINE_ROLE); + } + private final String nodeName; private final String nodeId; private final String ephemeralId; 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 0d2b08656c38d..c238921b5f1fa 100644 --- a/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java +++ b/server/src/main/java/org/opensearch/cluster/node/DiscoveryNodeRole.java @@ -310,6 +310,18 @@ public Setting legacySetting() { }; + /** + * Represents the role for a Offline node, which is dedicated only to run background tasks. + */ + public static final DiscoveryNodeRole OFFLINE_ROLE = new DiscoveryNodeRole("offline", "o") { + + @Override + public Setting legacySetting() { + // offline role is added in 2.15 so doesn't need to configure legacy setting + return null; + } + }; + /** * The built-in node roles. */ diff --git a/server/src/main/java/org/opensearch/env/NodeEnvironment.java b/server/src/main/java/org/opensearch/env/NodeEnvironment.java index 2748938d8b761..3181a7071c217 100644 --- a/server/src/main/java/org/opensearch/env/NodeEnvironment.java +++ b/server/src/main/java/org/opensearch/env/NodeEnvironment.java @@ -94,6 +94,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import java.util.Random; import java.util.Set; import java.util.concurrent.Semaphore; @@ -391,6 +392,20 @@ public NodeEnvironment(Settings settings, Environment environment, IndexStoreLis ensureNoFileCacheData(fileCacheNodePath); } + if (DiscoveryNode.isOfflineNode(settings) && DiscoveryNode.getRolesFromSettings(settings).size() > 1) { + final String message = String.format( + Locale.ROOT, + "Offline Nodes cannot be used with any other roles, only %s role should be provided. Please remove %s roles", + DiscoveryNodeRole.OFFLINE_ROLE.roleName(), + DiscoveryNode.getRolesFromSettings(settings) + .stream() + .map(DiscoveryNodeRole::roleName) + .filter(role1 -> Objects.equals(role1, DiscoveryNodeRole.OFFLINE_ROLE.roleName())) + .collect(Collectors.joining(",")) + ); + throw new IllegalStateException(message); + } + this.nodeMetadata = loadNodeMetadata(settings, logger, nodePaths); this.indexStoreListener = indexStoreListener; success = true;