public static int computeNodeId(VoldemortConfig config, Cluster cluster) { HostMatcher matcher = config.getNodeIdImplementation(); return NodeIdUtils.findNodeId(cluster, matcher); }
private void validateNodeId(Cluster cluster, HostMatcher matcher, List<Integer> nodeIdsToValidate, int actualNodeId) { NodeIdUtils.validateNodeId(cluster, matcher, actualNodeId); for(Integer nodeId: nodeIdsToValidate) { if(nodeId == actualNodeId) { // validate one more time NodeIdUtils.validateNodeId(cluster, matcher, actualNodeId); continue; } try { // Increment node Id by 1 and make sure it fails NodeIdUtils.validateNodeId(cluster, matcher, nodeId); Assert.fail("Validation should have failed"); } catch(VoldemortException ex) { // Expected Ignore } } }
private void validateMatchFails(Cluster cluster, HostMatcher matcher) { try { NodeIdUtils.findNodeId(cluster, matcher); Assert.fail("multiple match should have failed"); } catch(VoldemortException ex) { // Ignore. } for(int nodeId: cluster.getNodeIds()) { try { NodeIdUtils.validateNodeId(cluster, matcher, nodeId); Assert.fail("multiple match should have failed"); } catch(VoldemortException ex) { // Ignore. } } }
public static int findNodeId(Cluster cluster, HostMatcher matcher) { logger.info(" Using Matcher " + matcher.getDebugInfo()); if(isSingleLocalCluster(cluster, matcher)) { int nodeId = cluster.getNodeIds().iterator().next(); logger.info(" Cluster is a single local node cluster. Node Id " + nodeId); return nodeId; } List<Node> matches = Lists.newArrayList(); for(Node node: cluster.getNodes()) { if(matcher.match(node)) { logger.info(node.briefToString() + " matched the current cluster "); matches.add(node); } } if(matches.isEmpty()) { throw new VoldemortApplicationException(" No nodes in the cluster matched the current node " + Arrays.toString(cluster.getNodes().toArray())); } else if ( matches.size() > 1) { String errorMessage = " More than one node matched " + Arrays.toString(matches.toArray()); logger.error(errorMessage); throw new VoldemortApplicationException(errorMessage); } else { logger.info(" computed node Id match successfully " + matches.get(0).briefToString()); return matches.get(0).getId(); } }
if(isSingleLocalCluster(cluster, matcher)) { return;
private void validateCluster(Cluster cluster, List<String> hostNames, List<Integer> nodeIds, final int NUM_NODES) { HostMatcher invalidMatcher = new MockHostMatcher("invalidHost"); try { NodeIdUtils.findNodeId(cluster, invalidMatcher); Assert.fail("None of the hosts should have matched"); } catch(VoldemortException ex) { // Ignore } for(int i = 0; i < NUM_NODES; i++) { String hostName = hostNames.get(i); int expectedNodeId = nodeIds.get(i); HostMatcher matcher = new MockHostMatcher(hostName); int actulaNodeId = NodeIdUtils.findNodeId(cluster, matcher); Assert.assertEquals("Node Id is different", expectedNodeId, actulaNodeId); validateNodeId(cluster, matcher, nodeIds, actulaNodeId); } }
public static void validateNodeId(VoldemortConfig config, Cluster cluster) { if(config.isValidateNodeId() || config.isEnableNodeIdDetection()) { HostMatcher matcher = config.getNodeIdImplementation(); NodeIdUtils.validateNodeId(cluster, matcher, config.getNodeId()); } else { logger.info("Node id Validation is disabled in the config."); } }
@Test public void testLocalHost() { for(int nodeId = 0; nodeId < 5; nodeId++) { Cluster localCluster = singleNodeCluster(nodeId); HostMatcher matcher = new HostMatcher(); int actual = NodeIdUtils.findNodeId(localCluster, matcher); Assert.assertEquals("Node Id is different", nodeId, actual); validateNodeId(localCluster, matcher, Arrays.asList(nodeId + 1), nodeId); } }