chatman commented on a change in pull request #403: URL: https://github.com/apache/solr/pull/403#discussion_r779825974
########## File path: solr/core/src/test/org/apache/solr/cloud/NodeRolesTest.java ########## @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.cloud; + +import java.lang.invoke.MethodHandles; +import java.util.Collection; +import java.util.Collections; + +import java.util.Map; + +import org.apache.solr.client.solrj.embedded.JettySolrRunner; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.core.NodeRoles; +import org.junit.After; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NodeRolesTest extends SolrCloudTestCase { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Before + public void setupCluster() throws Exception { + configureCluster(1) + .addConfig("conf", configset("cloud-minimal")) + .configure(); + } + + @After + public void tearDownCluster() throws Exception { + shutdownCluster(); + } + + @SuppressWarnings("rawtypes") + public void testRoleIntegration() throws Exception { + JettySolrRunner j0 = cluster.getJettySolrRunner(0); + testSupportedRolesAPI(); + + // Start a dedicated overseer node + JettySolrRunner j1 = startNodeWithRoles("overseer:preferred,data:off"); + validateNodeRoles(j1.getNodeName(), "node-roles/overseer/preferred", j1.getNodeName(), "node-roles/data/off"); + + V2Response rsp; + OverseerRolesTest.waitForNewOverseer(20, j1.getNodeName(), true); + + // Start another node that is allowed or preferred overseer but has data + String overseerModeOnDataNode = random().nextBoolean() ? "preferred" : "allowed"; + JettySolrRunner j2 = startNodeWithRoles("overseer:" + overseerModeOnDataNode + ",data:on"); + validateNodeRoles(j2.getNodeName(), "node-roles/overseer/" + overseerModeOnDataNode, j2.getNodeName(), "node-roles/data/on"); + + // validate the preferred overseers + validateNodeRoles(j2.getNodeName(), "node-roles/overseer/" + overseerModeOnDataNode, j1.getNodeName(), "node-roles/overseer/preferred"); + + String COLLECTION_NAME = "TEST_ROLES"; + CollectionAdminRequest + .createCollection(COLLECTION_NAME, "conf", 3, 1) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(COLLECTION_NAME, 3, 3); + + // Assert that no replica was placed on the dedicated overseer node + String dedicatedOverseer = j1.getNodeName(); + cluster.getSolrClient().getClusterStateProvider().getCollection(COLLECTION_NAME) + .forEachReplica((s, replica) -> assertNotEquals(replica.node, dedicatedOverseer)); + + // Shutdown the dedicated overseer, make sure that node disappears from the roles output + j1.stop(); + + // Wait and make sure that another node picks up overseer responsibilities + OverseerRolesTest.waitForNewOverseer(20, it -> !dedicatedOverseer.equals(it), false); + + // Make sure the stopped node no longer has the role assigned + rsp = new V2Request.Builder("/cluster/node-roles/role/overseer/" + overseerModeOnDataNode).GET().build().process(cluster.getSolrClient()); + assertFalse(((Collection) rsp._get("node-roles/overseer/" + overseerModeOnDataNode, null)).contains(j1.getNodeName())); + } + + @SuppressWarnings("rawtypes") + private void validateNodeRoles(String... nodenamePaths) throws org.apache.solr.client.solrj.SolrServerException, java.io.IOException { + V2Response rsp = new V2Request.Builder("/cluster/node-roles").GET().build().process(cluster.getSolrClient()); + for (int i = 0; i < nodenamePaths.length; i += 2) { + String nodename = nodenamePaths[i]; + String path = nodenamePaths[i + 1]; + assertTrue("Didn't find " + nodename + " at " + path + ". Full response: " + rsp.jsonStr(), + ((Collection) rsp._get(path, Collections.emptyList())).contains(nodename)); + } + } + + @SuppressWarnings("unchecked") + private void testSupportedRolesAPI() throws Exception { + V2Response rsp = new V2Request.Builder("/cluster/node-roles/supported").GET().build().process(cluster.getSolrClient()); + Map<String, Object> l = (Map<String, Object>) rsp._get("supported-roles", Collections.emptyMap()); + assertTrue(l.containsKey("data")); + assertTrue(l.containsKey("overseer")); + } + + private JettySolrRunner startNodeWithRoles(String roles) throws Exception { Review comment: Each instance of this test runs on a separate JVM, so it shouldn't be a problem running such tests in parallel. ########## File path: solr/core/src/test/org/apache/solr/cloud/NodeRolesTest.java ########## @@ -0,0 +1,123 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.solr.cloud; + +import java.lang.invoke.MethodHandles; +import java.util.Collection; +import java.util.Collections; + +import java.util.Map; + +import org.apache.solr.client.solrj.embedded.JettySolrRunner; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.V2Request; +import org.apache.solr.client.solrj.response.V2Response; +import org.apache.solr.core.NodeRoles; +import org.junit.After; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NodeRolesTest extends SolrCloudTestCase { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + @Before + public void setupCluster() throws Exception { + configureCluster(1) + .addConfig("conf", configset("cloud-minimal")) + .configure(); + } + + @After + public void tearDownCluster() throws Exception { + shutdownCluster(); + } + + @SuppressWarnings("rawtypes") + public void testRoleIntegration() throws Exception { + JettySolrRunner j0 = cluster.getJettySolrRunner(0); + testSupportedRolesAPI(); + + // Start a dedicated overseer node + JettySolrRunner j1 = startNodeWithRoles("overseer:preferred,data:off"); + validateNodeRoles(j1.getNodeName(), "node-roles/overseer/preferred", j1.getNodeName(), "node-roles/data/off"); + + V2Response rsp; + OverseerRolesTest.waitForNewOverseer(20, j1.getNodeName(), true); + + // Start another node that is allowed or preferred overseer but has data + String overseerModeOnDataNode = random().nextBoolean() ? "preferred" : "allowed"; + JettySolrRunner j2 = startNodeWithRoles("overseer:" + overseerModeOnDataNode + ",data:on"); + validateNodeRoles(j2.getNodeName(), "node-roles/overseer/" + overseerModeOnDataNode, j2.getNodeName(), "node-roles/data/on"); + + // validate the preferred overseers + validateNodeRoles(j2.getNodeName(), "node-roles/overseer/" + overseerModeOnDataNode, j1.getNodeName(), "node-roles/overseer/preferred"); + + String COLLECTION_NAME = "TEST_ROLES"; + CollectionAdminRequest + .createCollection(COLLECTION_NAME, "conf", 3, 1) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(COLLECTION_NAME, 3, 3); + + // Assert that no replica was placed on the dedicated overseer node + String dedicatedOverseer = j1.getNodeName(); + cluster.getSolrClient().getClusterStateProvider().getCollection(COLLECTION_NAME) + .forEachReplica((s, replica) -> assertNotEquals(replica.node, dedicatedOverseer)); + + // Shutdown the dedicated overseer, make sure that node disappears from the roles output + j1.stop(); + + // Wait and make sure that another node picks up overseer responsibilities + OverseerRolesTest.waitForNewOverseer(20, it -> !dedicatedOverseer.equals(it), false); + + // Make sure the stopped node no longer has the role assigned + rsp = new V2Request.Builder("/cluster/node-roles/role/overseer/" + overseerModeOnDataNode).GET().build().process(cluster.getSolrClient()); + assertFalse(((Collection) rsp._get("node-roles/overseer/" + overseerModeOnDataNode, null)).contains(j1.getNodeName())); + } + + @SuppressWarnings("rawtypes") Review comment: Fixed, thanks. ########## File path: solr/core/src/java/org/apache/solr/handler/ClusterAPI.java ########## @@ -57,92 +65,191 @@ import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_EDIT_PERM; import static org.apache.solr.security.PermissionNameProvider.Name.CONFIG_READ_PERM; -/** All V2 APIs that have a prefix of /api/cluster/ - * +/** + * All V2 APIs that have a prefix of /api/cluster/ */ public class ClusterAPI { private final CollectionsHandler collectionsHandler; private final ConfigSetsHandler configSetsHandler; - public final Commands commands = new Commands(); - public final ConfigSetCommands configSetCommands = new ConfigSetCommands(); + public final Commands commands = new Commands(); + public final ConfigSetCommands configSetCommands = new ConfigSetCommands(); public ClusterAPI(CollectionsHandler ch, ConfigSetsHandler configSetsHandler) { this.collectionsHandler = ch; this.configSetsHandler = configSetsHandler; } @EndPoint(method = GET, - path = "/cluster/aliases", - permission = COLL_READ_PERM) + path = "/cluster/node-roles", + permission = COLL_READ_PERM) + public void roles(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception { + rsp.add("node-roles", readRecursive(ZkStateReader.NODE_ROLES, + collectionsHandler.getCoreContainer().getZkController().getSolrCloudManager().getDistribStateManager(), 3)); + } + + Object readRecursive(String path, DistribStateManager zk, int depth) { + if (depth == 0) return null; + Map<String, Object> result; + try { + List<String> children = zk.listData(path); + if (children != null && !children.isEmpty()) { + result = new HashMap<>(); + } else { + return depth >= 1 ? Collections.emptySet(): null; Review comment: Fixed, thanks. ########## File path: solr/core/src/java/org/apache/solr/core/NodeRoles.java ########## @@ -0,0 +1,146 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.core; + +import java.lang.invoke.MethodHandles; +import java.util.*; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.StringUtils; +import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.common.util.StrUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NodeRoles { + public static final String NODE_ROLES_PROP = "solr.node.roles"; + + /** + * Roles to be assumed on nodes that don't have roles specified for them at startup + */ + public static final String DEFAULT_ROLES_STRING = "data:on,overseer:allowed"; + + // Map of roles to mode that are applicable for this node. + private Map<Role, Mode> nodeRoles; + + public NodeRoles(String rolesString) { + Map<Role, Mode> roles = new EnumMap<>(Role.class); + if (StringUtils.isEmpty(rolesString)) { + rolesString = DEFAULT_ROLES_STRING; + } + List<String> rolesList = StrUtils.splitSmart(rolesString, ','); + for (String s: rolesList) { + List<String> roleMode = StrUtils.splitSmart(s,':'); + Role r = Role.getRole(roleMode.get(0)); + Mode m = Mode.valueOf(roleMode.get(1).toUpperCase(Locale.ROOT)); + if (r.supportedModes().contains(m)) { + roles.put(r, m); + } else { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, + "Unknown role mode '" + roleMode.get(1) + "' for role '" + r + "'"); + } + } + for(Role r: Role.values()) { + if (!roles.containsKey(r)) { + roles.put(r, r.modeWhenRoleIsAbsent()); + } + } + nodeRoles = Collections.unmodifiableMap(roles); + } + + public Map<Role, Mode> getRoles() { + return nodeRoles; + } + + public Mode getRoleMode(Role role) { + return nodeRoles.get(role); + } + + public boolean isOverseerAllowedOrPreferred() { + Mode roleMode = nodeRoles.get(Role.OVERSEER); + return Mode.ALLOWED.equals(roleMode) || Mode.PREFERRED.equals(roleMode); + } + + public enum Mode { + ON, OFF, ALLOWED, PREFERRED, DISALLOWED; Review comment: Fixed, thanks. Changed from using strings to enum based on Ilan's suggestion, but moved back to using strings now. ########## File path: solr/core/src/java/org/apache/solr/cloud/api/collections/OverseerRoleCmd.java ########## @@ -64,6 +64,10 @@ public void call(ClusterState state, ZkNodeProps message, NamedList<Object> resu SolrZkClient zkClient = zkStateReader.getZkClient(); Map<String, List<String>> roles = null; String node = message.getStr("node"); + if ("false".equals(message.getStr("persist"))) {// no need to persist to roles.json Review comment: This isn't a new feature, just a parameter added to the ADDROLE. ########## File path: solr/core/src/java/org/apache/solr/cloud/ZkController.java ########## @@ -2211,6 +2225,15 @@ public void checkOverseerDesignate() { } } + public void setPreferredOverseer() throws KeeperException, InterruptedException { + ZkNodeProps props = new ZkNodeProps(Overseer.QUEUE_OPERATION, ADDROLE.toString().toLowerCase(Locale.ROOT), + "node", getNodeName(), + "role", "overseer", + "persist", "false"); + log.info("Going to add role {} ", props); Review comment: Fixed, thanks. ########## File path: solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java ########## @@ -244,13 +235,28 @@ private static boolean existCoreName(String coreName, Slice slice) { Collections.shuffle(nodeList, random); } } else { - nodeList = new ArrayList<>(liveNodes); + nodeList = new ArrayList<>(filterNonDataNodes(zk, liveNodes)); Collections.shuffle(nodeList, random); } return nodeList; } + public static Collection<String> filterNonDataNodes(DistribStateManager zk, Collection<String> liveNodes) { Review comment: I don't see any other use here for filtering on a generic role. In case we need later, we can add so. ########## File path: solr/core/src/java/org/apache/solr/cloud/api/collections/Assign.java ########## @@ -21,20 +21,7 @@ import java.io.IOException; import java.lang.invoke.MethodHandles; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedHashSet; -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.*; Review comment: Fixed, thanks. ########## File path: solr/core/src/java/org/apache/solr/cluster/Cluster.java ########## @@ -31,6 +31,7 @@ */ Set<Node> getLiveNodes(); + Set<Node> getLiveNodes(boolean filterNonDataNodes) ; Review comment: Fixed, thanks. ########## File path: solr/solrj/src/java/org/apache/solr/common/cloud/ZkStateReader.java ########## @@ -103,6 +103,16 @@ public static final String STATE_TIMESTAMP_PROP = "stateTimestamp"; public static final String COLLECTIONS_ZKNODE = "/collections"; public static final String LIVE_NODES_ZKNODE = "/live_nodes"; + + /** + * The following, node_roles and roles.json are for assigning roles to + * nodes. The node_roles is the preferred way (using -Dsolr.node.roles param), + * and roles.json is used by legacy ADDROLE API command. + * TODO: Deprecate and remove support for roles.json in an upcoming release. Review comment: Fixed, thanks. ########## File path: solr/solr-ref-guide/src/node-roles.adoc ########## @@ -0,0 +1,196 @@ += Node Roles +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +A node in Solr is usually capable of performing various types of operations, e.g. hosting replicas, performing indexing and querying on them, collection management tasks etc. However, if someone wants to setup a cluster where these functionalities are isolated to certain dedicated nodes, then this can be achieved leveraging the concept of node roles. + +== Definitions + +=== Node role + +A role is a designation of a node that indicates that the node may perform a certain functionality that is governed by the role. Review comment: Fixed, thanks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
