Class ReplicatedConsistentHash

  • All Implemented Interfaces:
    ConsistentHash

    public class ReplicatedConsistentHash
    extends Object
    implements ConsistentHash
    Special implementation of ConsistentHash for replicated caches. The hash-space has several segments owned by all members and the primary ownership of each segment is evenly spread between members.
    Since:
    5.2
    Author:
    Dan Berindei, anistor@redhat.com
    • Constructor Detail

      • ReplicatedConsistentHash

        public ReplicatedConsistentHash​(org.infinispan.commons.hash.Hash hashFunction,
                                        List<Address> members,
                                        int[] primaryOwners)
    • Method Detail

      • getNumSegments

        public int getNumSegments()
        Specified by:
        getNumSegments in interface ConsistentHash
        Returns:
        The actual number of hash space segments. Note that it may not be the same as the number of segments passed in at creation time.
      • getNumOwners

        public int getNumOwners()
        Specified by:
        getNumOwners in interface ConsistentHash
        Returns:
        The configured number of owners. Note that the actual number of owners of each key may be different.
      • getMembers

        public List<Address> getMembers()
        Description copied from interface: ConsistentHash
        Should return the addresses of the nodes used to create this consistent hash.
        Specified by:
        getMembers in interface ConsistentHash
        Returns:
        set of node addresses.
      • getSegment

        public int getSegment​(Object key)
        Specified by:
        getSegment in interface ConsistentHash
        Returns:
        The hash space segment that a key maps to.
      • locateOwnersForSegment

        public List<Address> locateOwnersForSegment​(int segmentId)
        Specified by:
        locateOwnersForSegment in interface ConsistentHash
        Returns:
        All the nodes that own a given hash space segment, first address is the primary owner. The returned list is unmodifiable.
      • locatePrimaryOwnerForSegment

        public Address locatePrimaryOwnerForSegment​(int segmentId)
        Specified by:
        locatePrimaryOwnerForSegment in interface ConsistentHash
        Returns:
        The primary owner of a given hash space segment. This is equivalent to locateOwnersForSegment(segmentId).get(0) but is more efficient
      • getSegmentsForOwner

        public Set<Integer> getSegmentsForOwner​(Address owner)
        Description copied from interface: ConsistentHash
        Returns the segments owned by a cache member.
        Specified by:
        getSegmentsForOwner in interface ConsistentHash
        Parameters:
        owner - the address of the member
        Returns:
        a non-null set of segment IDs, may or may not be unmodifiable, which shouldn't be modified by caller. The set is empty if owner is not a member of the consistent hash.
      • getPrimarySegmentsForOwner

        public Set<Integer> getPrimarySegmentsForOwner​(Address owner)
        Description copied from interface: ConsistentHash
        Returns the segments that this cache member is the primary owner for.
        Specified by:
        getPrimarySegmentsForOwner in interface ConsistentHash
        Parameters:
        owner - the address of the member
        Returns:
        a non-null set of segment IDs, may or may not be unmodifiable, which shouldn't be modified by caller. The set is empty if owner is not a member of the consistent hash.
      • isKeyLocalToNode

        public boolean isKeyLocalToNode​(Address nodeAddress,
                                        Object key)
        Description copied from interface: ConsistentHash
        Test to see whether a key is owned by a given node.
        Specified by:
        isKeyLocalToNode in interface ConsistentHash
        Parameters:
        nodeAddress - address of the node to test
        key - key to test
        Returns:
        true if the key is mapped to the address; false otherwise
      • isSegmentLocalToNode

        public boolean isSegmentLocalToNode​(Address nodeAddress,
                                            int segmentId)
        Description copied from interface: ConsistentHash
        Check if a segment is local to a given member.

        Implementation note: normally key-based method are implemented based on segment-based methods. Here, however, we need a default implementation for the segment-based method for backwards-compatibility reasons.

        Specified by:
        isSegmentLocalToNode in interface ConsistentHash
      • isReplicated

        public boolean isReplicated()
        Specified by:
        isReplicated in interface ConsistentHash
        Returns:
        true if every member owns every segment. This allows callers to skip computing the segment of a key in some cases.
      • toScopedState

        public void toScopedState​(ScopedPersistentState state)
        Description copied from interface: ConsistentHash
        Writes this ConsistentHash to the specified scoped state. Before invoking this method, the ConsistentHash addresses will have to be replaced with their corresponding PersistentUUIDs
        Specified by:
        toScopedState in interface ConsistentHash
        Parameters:
        state - the state to which this ConsistentHash will be written
      • remapAddresses

        public ConsistentHash remapAddresses​(UnaryOperator<Address> remapper)
        Description copied from interface: ConsistentHash
        Returns a new ConsistentHash with the addresses remapped according to the provided UnaryOperator. If an address cannot me remapped (i.e. the remapper returns null) this method should return null.
        Specified by:
        remapAddresses in interface ConsistentHash
        Parameters:
        remapper - the remapper which given an address replaces it with another one
        Returns:
        the remapped ConsistentHash or null if one of the remapped addresses is null
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object