Package net.spy.memcached
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:
- RJ's blog post
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<MemcachedNode>
allNodes
private KetamaNodeLocatorConfiguration
config
private HashAlgorithm
hashAlg
private boolean
isWeightedKetama
private java.util.TreeMap<java.lang.Long,MemcachedNode>
ketamaNodes
private java.util.Map<java.net.InetSocketAddress,java.lang.Integer>
weights
-
Constructor Summary
Constructors Modifier Constructor Description KetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg)
Create a new KetamaNodeLocator using specified nodes and the specifed hash algorithm.KetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg, java.util.Map<java.net.InetSocketAddress,java.lang.Integer> nodeWeights, KetamaNodeLocatorConfiguration configuration)
Create a new KetamaNodeLocator with specific nodes, hash, node key format, and weightKetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg, KetamaNodeKeyFormatter.Format nodeKeyFormat, java.util.Map<java.net.InetSocketAddress,java.lang.Integer> weights)
Create a new KetamaNodeLocator with specific nodes, hash, node key format, and weightKetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg, KetamaNodeLocatorConfiguration conf)
Create a new KetamaNodeLocator using specified nodes and the specifed hash algorithm and configuration.private
KetamaNodeLocator(java.util.TreeMap<java.lang.Long,MemcachedNode> smn, java.util.Collection<MemcachedNode> an, HashAlgorithm alg, java.util.Map<java.net.InetSocketAddress,java.lang.Integer> nodeWeights, KetamaNodeLocatorConfiguration conf)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Collection<MemcachedNode>
getAll()
Get all memcached nodes.protected java.util.TreeMap<java.lang.Long,MemcachedNode>
getKetamaNodes()
(package private) long
getMaxKey()
(package private) MemcachedNode
getNodeForKey(long hash)
MemcachedNode
getPrimary(java.lang.String k)
Get the primary location for the given key.NodeLocator
getReadonlyCopy()
Create a read-only copy of this NodeLocator.java.util.Iterator<MemcachedNode>
getSequence(java.lang.String k)
Get an iterator over the sequence of nodes that make up the backup locations for a given key.private java.util.List<java.lang.Long>
ketamaNodePositionsAtIteration(MemcachedNode node, int iteration)
protected void
setKetamaNodes(java.util.List<MemcachedNode> nodes)
Setup the KetamaNodeLocator with the list of nodes it should use.void
updateLocator(java.util.List<MemcachedNode> nodes)
Update locator status.
-
-
-
Field Detail
-
ketamaNodes
private volatile java.util.TreeMap<java.lang.Long,MemcachedNode> ketamaNodes
-
allNodes
private volatile java.util.Collection<MemcachedNode> allNodes
-
hashAlg
private final HashAlgorithm hashAlg
-
weights
private final java.util.Map<java.net.InetSocketAddress,java.lang.Integer> weights
-
isWeightedKetama
private final boolean isWeightedKetama
-
config
private final KetamaNodeLocatorConfiguration config
-
-
Constructor Detail
-
KetamaNodeLocator
public KetamaNodeLocator(java.util.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 continuumalg
- The hash algorithm to use when choosing a node in the Ketama consistent hash continuum
-
KetamaNodeLocator
public KetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg, KetamaNodeKeyFormatter.Format nodeKeyFormat, java.util.Map<java.net.InetSocketAddress,java.lang.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 continuumalg
- The hash algorithm to use when choosing a node in the Ketama consistent hash continuumnodeKeyFormat
- the format used to name the nodes in Ketama, either SPYMEMCACHED or LIBMEMCACHEDweights
- node weights for ketama, a map from InetSocketAddress to weight as Integer
-
KetamaNodeLocator
public KetamaNodeLocator(java.util.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 continuumalg
- The hash algorithm to use when choosing a node in the Ketama consistent hash continuumconf
-
-
KetamaNodeLocator
public KetamaNodeLocator(java.util.List<MemcachedNode> nodes, HashAlgorithm alg, java.util.Map<java.net.InetSocketAddress,java.lang.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 continuumalg
- The hash algorithm to use when choosing a node in the Ketama consistent hash continuumweights
- node weights for ketama, a map from InetSocketAddress to weight as Integerconfiguration
- node locator configuration
-
KetamaNodeLocator
private KetamaNodeLocator(java.util.TreeMap<java.lang.Long,MemcachedNode> smn, java.util.Collection<MemcachedNode> an, HashAlgorithm alg, java.util.Map<java.net.InetSocketAddress,java.lang.Integer> nodeWeights, KetamaNodeLocatorConfiguration conf)
-
-
Method Detail
-
getAll
public java.util.Collection<MemcachedNode> getAll()
Description copied from interface:NodeLocator
Get all memcached nodes. This is useful for broadcasting messages.- Specified by:
getAll
in interfaceNodeLocator
-
getPrimary
public MemcachedNode getPrimary(java.lang.String k)
Description copied from interface:NodeLocator
Get the primary location for the given key.- Specified by:
getPrimary
in interfaceNodeLocator
- 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 java.util.Iterator<MemcachedNode> getSequence(java.lang.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 interfaceNodeLocator
- 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 interfaceNodeLocator
-
updateLocator
public void updateLocator(java.util.List<MemcachedNode> nodes)
Description copied from interface:NodeLocator
Update locator status.- Specified by:
updateLocator
in interfaceNodeLocator
- Parameters:
nodes
- New locator nodes.
-
getKetamaNodes
protected java.util.TreeMap<java.lang.Long,MemcachedNode> getKetamaNodes()
- Returns:
- the ketamaNodes
-
setKetamaNodes
protected void setKetamaNodes(java.util.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 java.util.List<java.lang.Long> ketamaNodePositionsAtIteration(MemcachedNode node, int iteration)
-
-