org.infinispan.distribution.ch
Class AbstractWheelConsistentHash

java.lang.Object
  extended by org.infinispan.distribution.ch.AbstractConsistentHash
      extended by org.infinispan.distribution.ch.AbstractWheelConsistentHash
All Implemented Interfaces:
ConsistentHash
Direct Known Subclasses:
DefaultConsistentHash, TopologyAwareConsistentHash

public abstract class AbstractWheelConsistentHash
extends AbstractConsistentHash

Abstract class for the wheel-based CH implementations.

This base class supports consistent hashses which wish to enable virtual nodes. To do this the implementation should override isVirtualNodesEnabled() and return true when virtual nodes should be enabled (we recommend at least that numVirtualNodes should be > 1 for virtual nodes to be enabled!). It is assumed that implementations of a consistent hash in which virtual nodes are enabled will take responsibility for ensuring that the fact virtual nodes are in use does not escape the consistent hash implementation.

The only consistent hash in Inifinispan to provide virtual node support is the topology aware consistent hash,

In order to do this, the implementation can use getRealAddress(Address) and getRealAddresses(Set) to convert the virtual addresses obtained from positions, addressToHashIds and AbstractConsistentHash.caches to real addresses. In particular an implementation should ensure that ConsistentHash.locate(Object, int), ConsistentHash.getStateProvidersOnLeave(Address, int) and ConsistentHash.getStateProvidersOnJoin(Address, int) do not return virtual addresses (as implementations of these methods are not provided by this abstract super class). The behavior of Infinispan if virtual addresses leak from the consistent hash implementation is not tested.

Since:
4.2
Author:
Mircea.Markus@jboss.com, Pete Muir

Nested Class Summary
static class AbstractWheelConsistentHash.Externalizer<T extends AbstractWheelConsistentHash>
           
 
Field Summary
protected  Map<Address,Integer> addressToHashIds
           
protected  Hash hashFunction
           
protected  Log log
           
protected  int numVirtualNodes
           
protected  SortedMap<Integer,Address> positions
           
protected  boolean trace
           
 
Fields inherited from class org.infinispan.distribution.ch.AbstractConsistentHash
caches, groupManager
 
Constructor Summary
protected AbstractWheelConsistentHash()
           
 
Method Summary
protected  void addNode(Address a)
           
protected  Address createVirtualAddress(Address realAddress, int id)
          Default implementation that creates a VirtualAddress from an Address.
 List<Address> getBackupsForNode(Address node, int replCount)
          Returns the nodes that backup data for the supplied node including the node itself.
 Set<Address> getCaches()
          Should return a collection of cache addresses in the cluster.
 int getHashId(Address a)
          Returns the value between 0 and the hash space limit, or hash id, for a particular address.
 int getHashSpace()
          Returns the hash space constant for this consistent hash algorithm class.
 int getNormalizedHash(Object key)
           
protected  Address getRealAddress(Address a)
           
protected  Set<Address> getRealAddresses(Set<Address> virtualAddresses)
           
protected  boolean isVirtualNodesEnabled()
           
 void setCaches(Set<Address> newCaches)
          Sets the collection of cache addresses in the cluster.
 void setHashFunction(Hash h)
           
 void setNumVirtualNodes(Integer numVirtualNodes)
           
 String toString()
           
 
Methods inherited from class org.infinispan.distribution.ch.AbstractConsistentHash
getGrouping, isKeyLocalToAddress, locateAll, setGroupManager
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.infinispan.distribution.ch.ConsistentHash
getStateProvidersOnJoin, getStateProvidersOnLeave, locate
 

Field Detail

log

protected final Log log

trace

protected final boolean trace

positions

protected SortedMap<Integer,Address> positions

addressToHashIds

protected Map<Address,Integer> addressToHashIds

hashFunction

protected Hash hashFunction

numVirtualNodes

protected int numVirtualNodes
Constructor Detail

AbstractWheelConsistentHash

protected AbstractWheelConsistentHash()
Method Detail

setHashFunction

public void setHashFunction(Hash h)

setNumVirtualNodes

public void setNumVirtualNodes(Integer numVirtualNodes)

setCaches

public void setCaches(Set<Address> newCaches)
Description copied from interface: ConsistentHash
Sets the collection of cache addresses in the cluster. The implementation should store these internally and use these to locate keys.

Specified by:
setCaches in interface ConsistentHash
Overrides:
setCaches in class AbstractConsistentHash
Parameters:
newCaches - A set of unique caches in cluster.

createVirtualAddress

protected Address createVirtualAddress(Address realAddress,
                                       int id)
Default implementation that creates a VirtualAddress from an Address.


getCaches

public Set<Address> getCaches()
Description copied from interface: ConsistentHash
Should return a collection of cache addresses in the cluster.

Specified by:
getCaches in interface ConsistentHash
Overrides:
getCaches in class AbstractConsistentHash
Returns:
set of unique of cache addresses

addNode

protected void addNode(Address a)

getRealAddress

protected Address getRealAddress(Address a)

getRealAddresses

protected Set<Address> getRealAddresses(Set<Address> virtualAddresses)

getBackupsForNode

public List<Address> getBackupsForNode(Address node,
                                       int replCount)
Description copied from interface: ConsistentHash
Returns the nodes that backup data for the supplied node including the node itself.


getHashSpace

public int getHashSpace()
Description copied from interface: ConsistentHash
Returns the hash space constant for this consistent hash algorithm class. This integer is often used as modulus for arithmetic operations within the algorithm, for example, limiting the range of possible hash values.

Returns:
A positive integer containing the hash space constant or 0 is not supported by implementation.

getHashId

public int getHashId(Address a)
Description copied from interface: ConsistentHash
Returns the value between 0 and the hash space limit, or hash id, for a particular address. If there's no such value for an address, this method will return -1.

Returns:
An int between 0 and hash space if the address is present in the hash wheel, otherwise it returns -1.

getNormalizedHash

public int getNormalizedHash(Object key)

toString

public String toString()
Overrides:
toString in class AbstractConsistentHash

isVirtualNodesEnabled

protected boolean isVirtualNodesEnabled()

-->

Copyright © 2011 JBoss, a division of Red Hat. All Rights Reserved.