janhoy commented on a change in pull request #403: URL: https://github.com/apache/solr/pull/403#discussion_r772762468
########## File path: solr/core/src/java/org/apache/solr/core/NodeRoles.java ########## @@ -0,0 +1,129 @@ +/* + * 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.MapWriter; +import org.apache.solr.common.SolrException; +import org.apache.solr.common.StringUtils; +import org.apache.solr.common.util.StrUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NodeRoles implements MapWriter { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public static final String NODE_ROLES_PROP = "solr.node.roles"; + + public static final String ON = "on"; + public static final String OFF = "off"; + public static final String ALLOWED = "allowed"; + public static final String DISALLOWED = "disallowed"; + public static final String PREFERRED = "preferred"; + public static final Set<String> OVERSEER_MODES = Set.of(ALLOWED, PREFERRED, DISALLOWED); + public static final Set<String> ON_OFF = Set.of(ON,OFF); + + 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, String> nodeRoles; + + public NodeRoles(String rolesString) { + Map<Role, String> 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)); + if (r.supportedModes().contains(roleMode.get(1))) { + roles.put(r, roleMode.get(1)); + } else { + throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown role mode: " + roleMode.get(0)); + } + } + for(Role r: Role.values()) { + if (!roles.containsKey(r)) { + roles.put(r, r.defaultIfAbsent()); + } + } + nodeRoles = Collections.unmodifiableMap(roles); + } + + public Map<Role, String> getRoles() { + return nodeRoles; + } + + public String getRoleMode(Role role) { + return nodeRoles.get(role); + } + + @Override + public void writeMap(EntryWriter ew) { + nodeRoles.forEach((role, s) -> ew.putNoEx(role.roleName, s)); + } + + public boolean isOverseerAllowed() { + String roleMode = nodeRoles.get(Role.OVERSEER); + return ALLOWED.equals(roleMode) || PREFERRED.equals(roleMode); + } + + public enum Role { + DATA("data"), + OVERSEER("overseer") { Review comment: You could imagine CDCR re-appearing as a package, and that it needs dedicated nodes to sync with other clusters. Or a new Tika package which wants to manage a set of nodes exposing (managed) TikaServer for parsing PDFs etc. Of course packages could re-invent their own role concept, and tag some nodes with `NODE_ROLES=[]` to tell solr-core to not assign anything else to those nodes. But I was more thinking that a role is just a namespace, and solr-core has some roles ootb, but other new roles could be registered without editing a global enum? Note, I have not sat down to try to design this - feels like a topic like whether roles should be pluggable should be discussed on POC level. -- 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]
