Class KetamaNodeLocator

java.lang.Object
net.spy.memcached.compat.SpyObject
net.spy.memcached.KetamaNodeLocator
All Implemented Interfaces:
NodeLocator

public final class KetamaNodeLocator extends SpyObject implements NodeLocator
This is an implementation of the Ketama consistent hash strategy from last.fm. This implementation may not be compatible with libketama as hashing is considered separate from node location. Note that this implementation does not currently supported weighted nodes.
See Also:
  • Field Details

  • Constructor Details

    • KetamaNodeLocator

      public KetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg)
      Create a new KetamaNodeLocator using specified nodes and the specifed hash algorithm.
      Parameters:
      nodes - The List of nodes to use in the Ketama consistent hash continuum
      alg - The hash algorithm to use when choosing a node in the Ketama consistent hash continuum
    • KetamaNodeLocator

      public KetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg, KetamaNodeKeyFormatter.Format nodeKeyFormat, Map<InetSocketAddress,Integer> weights)
      Create a new KetamaNodeLocator with specific nodes, hash, node key format, and weight
      Parameters:
      nodes - The List of nodes to use in the Ketama consistent hash continuum
      alg - The hash algorithm to use when choosing a node in the Ketama consistent hash continuum
      nodeKeyFormat - the format used to name the nodes in Ketama, either SPYMEMCACHED or LIBMEMCACHED
      weights - node weights for ketama, a map from InetSocketAddress to weight as Integer
    • KetamaNodeLocator

      public KetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg, KetamaNodeLocatorConfiguration conf)
      Create a new KetamaNodeLocator using specified nodes and the specifed hash algorithm and configuration.
      Parameters:
      nodes - The List of nodes to use in the Ketama consistent hash continuum
      alg - The hash algorithm to use when choosing a node in the Ketama consistent hash continuum
      conf -
    • KetamaNodeLocator

      public KetamaNodeLocator(List<MemcachedNode> nodes, HashAlgorithm alg, Map<InetSocketAddress,Integer> nodeWeights, KetamaNodeLocatorConfiguration configuration)
      Create a new KetamaNodeLocator with specific nodes, hash, node key format, and weight
      Parameters:
      nodes - The List of nodes to use in the Ketama consistent hash continuum
      alg - The hash algorithm to use when choosing a node in the Ketama consistent hash continuum
      configuration - node locator configuration
      weights - node weights for ketama, a map from InetSocketAddress to weight as Integer
    • KetamaNodeLocator

  • Method Details

    • getAll

      public Collection<MemcachedNode> getAll()
      Description copied from interface: NodeLocator
      Get all memcached nodes. This is useful for broadcasting messages.
      Specified by:
      getAll in interface NodeLocator
    • getPrimary

      public MemcachedNode getPrimary(String k)
      Description copied from interface: NodeLocator
      Get the primary location for the given key.
      Specified by:
      getPrimary in interface NodeLocator
      Parameters:
      k - the object key
      Returns:
      the QueueAttachment containing the primary storage for a key
    • getMaxKey

      long getMaxKey()
    • getNodeForKey

      MemcachedNode getNodeForKey(long hash)
    • getSequence

      public Iterator<MemcachedNode> getSequence(String k)
      Description copied from interface: NodeLocator
      Get an iterator over the sequence of nodes that make up the backup locations for a given key.
      Specified by:
      getSequence in interface NodeLocator
      Parameters:
      k - the object key
      Returns:
      the sequence of backup nodes.
    • getReadonlyCopy

      public NodeLocator getReadonlyCopy()
      Description copied from interface: NodeLocator
      Create a read-only copy of this NodeLocator.
      Specified by:
      getReadonlyCopy in interface NodeLocator
    • updateLocator

      public void updateLocator(List<MemcachedNode> nodes)
      Description copied from interface: NodeLocator
      Update locator status.
      Specified by:
      updateLocator in interface NodeLocator
      Parameters:
      nodes - New locator nodes.
    • getKetamaNodes

      protected TreeMap<Long,MemcachedNode> getKetamaNodes()
      Returns:
      the ketamaNodes
    • setKetamaNodes

      protected void setKetamaNodes(List<MemcachedNode> nodes)
      Setup the KetamaNodeLocator with the list of nodes it should use.
      Parameters:
      nodes - a List of MemcachedNodes for this KetamaNodeLocator to use in its continuum
    • ketamaNodePositionsAtIteration

      private List<Long> ketamaNodePositionsAtIteration(MemcachedNode node, int iteration)