public NodeLocator getReadonlyCopy() { TreeMap<Long, MemcachedNode> smn = new TreeMap<Long, MemcachedNode>(getKetamaNodes()); Collection<MemcachedNode> an = new ArrayList<MemcachedNode>(allNodes.size()); // Rewrite the values a copy of the map. for (Map.Entry<Long, MemcachedNode> me : smn.entrySet()) { smn.put(me.getKey(), new MemcachedNodeROImpl(me.getValue())); } // Copy the allNodes collection. for (MemcachedNode n : allNodes) { an.add(new MemcachedNodeROImpl(n)); } return new RefinedKetamaNodeLocator(smn, an, hashAlg, weights, config); }
public MemcachedNode getPrimary(final String k) { MemcachedNode rv = getNodeForKey(hashAlg.hash(k)); assert rv != null : "Found no node for key " + k; return rv; }
@Override public void updateLocator(List<MemcachedNode> nodes) { allNodes = nodes; setKetamaNodes(nodes); }
* 4); for (int i = 0; i < pointerPerServer / 4; i++) { for (long position : ketamaNodePositionsAtIteration(node, i)) { newNodeMap.put(position, node); getLogger().debug("Adding node %s with weight %s in position %d", node, thisWeight, position); for (long position : ketamaNodePositionsAtIteration(node, i)) { newNodeMap.put(position, node); getLogger().debug("Adding node %s in position %d", node, position);
long getMaxKey() { return getKetamaNodes().lastKey(); }
@Override public NodeLocator createLocator(List<MemcachedNode> nodes) { switch (locator) { case ARRAY_MOD: return new ArrayModNodeLocator(nodes, getHashAlg()); case CONSISTENT: return new RefinedKetamaNodeLocator(nodes, getHashAlg()); default: throw new IllegalStateException("Unhandled locator type: " + locator); } }
* 4); for (int i = 0; i < pointerPerServer / 4; i++) { for (long position : ketamaNodePositionsAtIteration(node, i)) { newNodeMap.put(position, node); getLogger().debug("Adding node %s with weight %s in position %d", node, thisWeight, position); for (long position : ketamaNodePositionsAtIteration(node, i)) { newNodeMap.put(position, node); getLogger().debug("Adding node %s in position %d", node, position);
MemcachedNode getNodeForKey(long hash) { final MemcachedNode rv; if (!ketamaNodes.containsKey(hash)) { // Java 1.6 adds a ceilingKey method, but I'm still stuck in 1.5 // in a lot of places, so I'm doing this myself. SortedMap<Long, MemcachedNode> tailMap = getKetamaNodes().tailMap(hash); if (tailMap.isEmpty()) { hash = getKetamaNodes().firstKey(); } else { hash = tailMap.firstKey(); } } rv = getKetamaNodes().get(hash); return rv; }
@Override public NodeLocator createLocator(List<MemcachedNode> nodes) { switch (locator) { case ARRAY_MOD: return new ArrayModNodeLocator(nodes, getHashAlg()); case CONSISTENT: return new RefinedKetamaNodeLocator(nodes, getHashAlg()); default: throw new IllegalStateException("Unhandled locator type: " + locator); } }
public NodeLocator getReadonlyCopy() { TreeMap<Long, MemcachedNode> smn = new TreeMap<Long, MemcachedNode>(getKetamaNodes()); Collection<MemcachedNode> an = new ArrayList<MemcachedNode>(allNodes.size()); // Rewrite the values a copy of the map. for (Map.Entry<Long, MemcachedNode> me : smn.entrySet()) { smn.put(me.getKey(), new MemcachedNodeROImpl(me.getValue())); } // Copy the allNodes collection. for (MemcachedNode n : allNodes) { an.add(new MemcachedNodeROImpl(n)); } return new RefinedKetamaNodeLocator(smn, an, hashAlg, weights, config); }
/** * the previous 7 may be too small to reduce the chance to get all down nodes * @param k * @return */ public Iterator<MemcachedNode> getSequence(String k) { // Seven searches gives us a 1 in 2^maxTry chance of hitting the // same dead node all of the time. int maxTry = config.getNodeRepetitions() + 1; if (maxTry < 20) { maxTry = 20; } return new KetamaIterator(k, maxTry, getKetamaNodes(), hashAlg); }
/** * Create a new KetamaNodeLocator with specific nodes, hash, node key format, * and weight * * @param nodes The List of nodes to use in the Ketama consistent hash * continuum * @param alg The hash algorithm to use when choosing a node in the Ketama * consistent hash continuum * @param nodeWeights node weights for ketama, a map from InetSocketAddress to * weight as Integer * @param configuration node locator configuration */ public RefinedKetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg, Map<InetSocketAddress, Integer> nodeWeights, KetamaNodeLocatorConfiguration configuration) { super(); allNodes = nodes; hashAlg = alg; config = configuration; weights = nodeWeights; isWeightedKetama = !weights.isEmpty(); setKetamaNodes(nodes); }
public MemcachedNode getPrimary(final String k) { MemcachedNode rv = getNodeForKey(hashAlg.hash(k)); assert rv != null : "Found no node for key " + k; return rv; }
long getMaxKey() { return getKetamaNodes().lastKey(); }
@Override public void updateLocator(List<MemcachedNode> nodes) { allNodes = nodes; setKetamaNodes(nodes); }
MemcachedNode getNodeForKey(long hash) { final MemcachedNode rv; if (!ketamaNodes.containsKey(hash)) { // Java 1.6 adds a ceilingKey method, but I'm still stuck in 1.5 // in a lot of places, so I'm doing this myself. SortedMap<Long, MemcachedNode> tailMap = getKetamaNodes().tailMap(hash); if (tailMap.isEmpty()) { hash = getKetamaNodes().firstKey(); } else { hash = tailMap.firstKey(); } } rv = getKetamaNodes().get(hash); return rv; }
/** * Create a new KetamaNodeLocator with specific nodes, hash, node key format, * and weight * * @param nodes The List of nodes to use in the Ketama consistent hash * continuum * @param alg The hash algorithm to use when choosing a node in the Ketama * consistent hash continuum * @param nodeWeights node weights for ketama, a map from InetSocketAddress to * weight as Integer * @param configuration node locator configuration */ public RefinedKetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg, Map<InetSocketAddress, Integer> nodeWeights, KetamaNodeLocatorConfiguration configuration) { super(); allNodes = nodes; hashAlg = alg; config = configuration; weights = nodeWeights; isWeightedKetama = !weights.isEmpty(); setKetamaNodes(nodes); }
/** * the previous 7 may be too small to reduce the chance to get all down nodes * @param k * @return */ public Iterator<MemcachedNode> getSequence(String k) { // Seven searches gives us a 1 in 2^maxTry chance of hitting the // same dead node all of the time. int maxTry = config.getNodeRepetitions() + 1; if (maxTry < 20) { maxTry = 20; } return new KetamaIterator(k, maxTry, getKetamaNodes(), hashAlg); }