Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.client.cluster;
  import java.util.*;
Helper class we use to encapsulate details of finding ordered sequence of servers to contact, to locate entry with given key.
 public final class HashRouter<K extends EntryKey>
     private final KeySpace _keyspace;
     private final EntryKeyConverter<K> _keyConverter;

Monotonically increasing counter we use for lazily constructing and invalidating routing information, mapping from key hashes to com.fasterxml.clustermate.client.NodesForKey objects.
     private final AtomicInteger _version = new AtomicInteger(1);
     private final AtomicReferenceArray<NodesForKey_routing;

Since we will need to iterate over server nodes, let's use pre-calculated array. Parent will update it, we just need to access it in thread-safe manner.
             new ClusterServerNode[0]);
     public HashRouter(KeySpace keyspaceEntryKeyConverter<K> keyConverter,
             AtomicReference<ClusterServerNode[]> states)
          = keyspace;
          = keyConverter;
          = new AtomicReferenceArray<NodesForKey>(keyspace.getLength());
          = states;
     public void invalidateRouting() {
     public NodesForKey getNodesFor(K key)
         int fullHash = .routingHashFor(key);
         KeyHash hash = new KeyHash(fullHash.getLength());
         int currVersion = .get();
         int modulo = hash.getModuloHash();
         NodesForKey nodes = .get(modulo);
         // fast (and common) case: pre-calculated, valid info exists:
         if (nodes != null && nodes.version() == currVersion) {
             return nodes;
         NodesForKey newNodes = _calculateNodes(currVersionhash);
         return newNodes;
     protected NodesForKey _calculateNodes(int versionKeyHash keyHash) {
         return _calculateNodes(versionkeyHash.get());
     // separate method for testing
     protected NodesForKey _calculateNodes(int versionKeyHash keyHash,
             ClusterServerNode[] allNodes)
         final int allCount = allNodes.length;
         // First: simply collect all applicable nodes:
         ArrayList<ClusterServerNodeappl = new ArrayList<ClusterServerNode>();
         for (int i = 0; i < allCount; ++i) {
             ClusterServerNode state = allNodes[i];
             if (state.getTotalRange().contains(keyHash)) {
         return _sortNodes(versionkeyHashappl);
     protected NodesForKey _sortNodes(int versionKeyHash keyHash,
         // edge case: no matching
         if (appl.isEmpty()) {
             return NodesForKey.empty(version);
         // otherwise need to sort
         ClusterServerNodeImpl[] matching = appl.toArray(new ClusterServerNodeImpl[appl.size()]);
         Arrays.sort(matching, 0, appl.size(), new NodePriorityComparator(keyHash));
         return new NodesForKey(versionmatching);
    /* Helper classes
Comparator that orders server in decreasing priority, that is, starts with the closest enabled match, ending with disabled entries.
    private final static class NodePriorityComparator implements Comparator<ClusterServerNodeImpl>
        private final KeyHash _keyHash;
        public NodePriorityComparator(KeyHash keyHash) {
             = keyHash;
        public int compare(ClusterServerNodeImpl node1ClusterServerNodeImpl node2)
            return node1.calculateSortingDistance() - node2.calculateSortingDistance();
New to GrepCode? Check out our FAQ X