Class AbstractReplicatedMap<K,​V>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  AbstractReplicatedMap.MapEntry<K,​V>
      Represents an entry in the replicated map, including metadata about its role (primary, backup, proxy).
      static class  AbstractReplicatedMap.MapMessage
      Represents a message sent between replicated map instances.
      static interface  AbstractReplicatedMap.MapOwner
      Interface for the owner of this replicated map.
      • Nested classes/interfaces inherited from interface java.util.Map

        java.util.Map.Entry<K extends java.lang.Object,​V extends java.lang.Object>
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected long accessTimeout
      Since the map keeps internal membership this is the timeout for a ping message to be responded to If a remote map doesn't respond within this timeframe, its considered dead.
      protected Channel channel
      Reference to the channel for sending messages
      protected int channelSendOptions
      Our default send options
      protected int currentNode
      The node we are currently backing up data to, this index will rotate on a round robin basis
      static int DEFAULT_INITIAL_CAPACITY
      The default initial capacity - MUST be a power of two.
      static float DEFAULT_LOAD_FACTOR
      The load factor used when none specified in constructor.
      protected java.lang.ClassLoader[] externalLoaders
      External class loaders if serialization and deserialization is to be performed successfully.
      protected java.util.concurrent.ConcurrentMap<K,​AbstractReplicatedMap.MapEntry<K,​V>> innerMap
      The underlying concurrent map storing entries.
      protected byte[] mapContextName
      The Map context name makes this map unique, this allows us to have more than one map shared through one channel
      protected java.util.HashMap<Member,​java.lang.Long> mapMembers
      A list of members in our map
      protected java.lang.String mapname
      Readable string of the mapContextName value
      protected AbstractReplicatedMap.MapOwner mapOwner
      The owner of this map, ala a SessionManager for example
      protected RpcChannel rpcChannel
      The RpcChannel to send RPC messages through
      protected long rpcTimeout
      Timeout for RPC messages, how long we will wait for a reply
      protected static StringManager sm
      The string manager for packaging specific messages.
      protected java.lang.Object stateMutex
      Simple lock object for transfers
      protected boolean stateTransferred
      Has the state been transferred
    • Constructor Summary

      Constructors 
      Constructor Description
      AbstractReplicatedMap​(AbstractReplicatedMap.MapOwner owner, Channel channel, long timeout, java.lang.String mapContextName, int initialCapacity, float loadFactor, int channelSendOptions, java.lang.ClassLoader[] cls, boolean terminate)
      Creates a new map.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      boolean accept​(java.io.Serializable msg, Member sender)
      Accepts or rejects a message based on the map context.
      void breakdown()
      Breaks down the map, removing all entries and closing channels.
      protected void broadcast​(int msgtype, boolean rpc)
      Helper method to broadcast a message to all members in a channel
      void clear()  
      void clear​(boolean notify)
      Clears entries from the map.
      boolean containsKey​(java.lang.Object key)
      Returns true if the key has an entry in the map.
      boolean containsValue​(java.lang.Object value)  
      java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
      Returns a set view of the mappings contained in this map.
      java.util.Set<java.util.Map.Entry<K,​AbstractReplicatedMap.MapEntry<K,​V>>> entrySetFull()
      Returns the entire contents of the map Map.Entry.getValue() will return a LazyReplicatedMap.MapEntry object containing all the information about the object.
      boolean equals​(java.lang.Object o)
      Checks if this map is equal to another object.
      Member[] excludeFromSet​(Member[] mbrs, Member[] set)
      Excludes members from the given set.
      V get​(java.lang.Object key)  
      long getAccessTimeout()
      Gets the access timeout.
      Channel getChannel()
      Gets the channel used for communication.
      int getChannelSendOptions()
      Gets the channel send options.
      java.lang.ClassLoader[] getExternalLoaders()
      Gets the external class loaders.
      AbstractReplicatedMap.MapEntry<K,​V> getInternal​(java.lang.Object key)
      Gets the internal map entry for a key.
      byte[] getMapContextName()
      Gets the map context name.
      Member[] getMapMembers()
      Gets the current map members.
      Member[] getMapMembers​(java.util.HashMap<Member,​java.lang.Long> members)
      Gets the map members from the given map.
      Member[] getMapMembersExcl​(Member[] exclude)
      Gets the map members excluding the given members.
      AbstractReplicatedMap.MapOwner getMapOwner()
      Gets the map owner.
      int getNextBackupIndex()
      Gets the next backup index using round-robin rotation.
      Member getNextBackupNode()
      Gets the next backup node using round-robin rotation.
      protected abstract int getReplicateMessageType()
      Gets the replicate message type.
      RpcChannel getRpcChannel()
      Gets the RPC channel.
      long getRpcTimeout()
      Gets the RPC timeout.
      protected abstract int getStateMessageType()
      Gets the state message type.
      java.lang.Object getStateMutex()
      Gets the state mutex object.
      int hashCode()
      Returns the hash code for this map based on the map context name.
      void heartbeat()
      Sends a heartbeat to all members in the cluster.
      protected void init​(AbstractReplicatedMap.MapOwner owner, Channel channel, java.lang.String mapContextName, long timeout, int channelSendOptions, java.lang.ClassLoader[] cls, boolean terminate)
      Initializes the map by creating the RPC channel, registering itself as a channel listener This method is also responsible for initiating the state transfer
      boolean inSet​(Member m, Member[] set)
      Checks if a member is in the given set.
      boolean isEmpty()
      Checks if this map is empty.
      boolean isStateTransferred()
      Checks if state has been transferred.
      java.util.Set<K> keySet()
      Returns a set view of the keys contained in this map.
      java.util.Set<K> keySetFull()
      Gets the complete set of keys in the map.
      void leftOver​(java.io.Serializable msg, Member sender)
      Handles a left over membership message.
      void mapMemberAdded​(Member member)
      Adds a member to this map.
      void memberAdded​(Member member)
      Called when a member is added to the channel.
      protected void memberAlive​(Member member)
      We have received a member alive notification
      void memberDisappeared​(Member member)
      Called when a member disappears from the channel.
      void messageReceived​(java.io.Serializable msg, Member sender)
      Handles a received message.
      protected void ping​(long timeout)
      Sends a ping out to all the members in the cluster, not just map members that this map is alive.
      protected void printMap​(java.lang.String header)
      Prints the contents of the map for debugging purposes.
      protected abstract Member[] publishEntryInfo​(java.lang.Object key, java.lang.Object value)
      Publish info about a map pair (key/value) to other nodes in the cluster.
      V put​(K key, V value)
      Puts a key-value pair into the map.
      V put​(K key, V value, boolean notify)
      Puts a key-value pair into the map.
      void putAll​(java.util.Map<? extends K,​? extends V> m)
      Copies all mappings from the specified map to this map.
      V remove​(java.lang.Object key)
      Removes the entry for the specified key from the map.
      V remove​(java.lang.Object key, boolean notify)
      Removes the entry for the specified key from the map.
      void replicate​(boolean complete)
      This can be invoked by a periodic thread to replicate out any changes.
      void replicate​(K key, boolean complete)
      Replicates any changes to the object since the last time The object has to be primary, ie, if the object is a proxy or a backup, it will not be replicated
      java.io.Serializable replyRequest​(java.io.Serializable msg, Member sender)
      Handles a reply request message.
      void setAccessTimeout​(long accessTimeout)
      Sets the access timeout.
      void setChannelSendOptions​(int channelSendOptions)
      Sets the channel send options.
      void setExternalLoaders​(java.lang.ClassLoader[] externalLoaders)
      Sets the external class loaders.
      void setMapOwner​(AbstractReplicatedMap.MapOwner mapOwner)
      Sets the map owner.
      int size()
      Returns the number of active entries in this map.
      int sizeFull()
      Gets the complete size of the map.
      void transferState()
      Transfers the current state from another map in the cluster.
      java.util.Collection<V> values()
      Returns a collection view of the values contained in this map.
      protected Member[] wrap​(Member m)
      Helper methods, wraps a single member in an array
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Map

        compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
    • Field Detail

      • sm

        protected static final StringManager sm
        The string manager for packaging specific messages.
      • DEFAULT_INITIAL_CAPACITY

        public static final int DEFAULT_INITIAL_CAPACITY
        The default initial capacity - MUST be a power of two.
        See Also:
        Constant Field Values
      • DEFAULT_LOAD_FACTOR

        public static final float DEFAULT_LOAD_FACTOR
        The load factor used when none specified in constructor.
        See Also:
        Constant Field Values
      • rpcTimeout

        protected transient long rpcTimeout
        Timeout for RPC messages, how long we will wait for a reply
      • channel

        protected transient Channel channel
        Reference to the channel for sending messages
      • rpcChannel

        protected transient RpcChannel rpcChannel
        The RpcChannel to send RPC messages through
      • mapContextName

        protected transient byte[] mapContextName
        The Map context name makes this map unique, this allows us to have more than one map shared through one channel
      • stateTransferred

        protected transient boolean stateTransferred
        Has the state been transferred
      • stateMutex

        protected final transient java.lang.Object stateMutex
        Simple lock object for transfers
      • mapMembers

        protected final transient java.util.HashMap<Member,​java.lang.Long> mapMembers
        A list of members in our map
      • channelSendOptions

        protected transient int channelSendOptions
        Our default send options
      • externalLoaders

        protected transient java.lang.ClassLoader[] externalLoaders
        External class loaders if serialization and deserialization is to be performed successfully.
      • currentNode

        protected transient int currentNode
        The node we are currently backing up data to, this index will rotate on a round robin basis
      • accessTimeout

        protected transient long accessTimeout
        Since the map keeps internal membership this is the timeout for a ping message to be responded to If a remote map doesn't respond within this timeframe, its considered dead.
      • mapname

        protected transient java.lang.String mapname
        Readable string of the mapContextName value
    • Constructor Detail

      • AbstractReplicatedMap

        public AbstractReplicatedMap​(AbstractReplicatedMap.MapOwner owner,
                                     Channel channel,
                                     long timeout,
                                     java.lang.String mapContextName,
                                     int initialCapacity,
                                     float loadFactor,
                                     int channelSendOptions,
                                     java.lang.ClassLoader[] cls,
                                     boolean terminate)
        Creates a new map.
        Parameters:
        owner - The map owner
        channel - The channel to use for communication
        timeout - long - timeout for RPC messages
        mapContextName - String - unique name for this map, to allow multiple maps per channel
        initialCapacity - int - the size of this map, see HashMap
        loadFactor - float - load factor, see HashMap
        channelSendOptions - Send options
        cls - - a list of classloaders to be used for deserialization of objects.
        terminate - - Flag for whether to terminate this map that failed to start.
    • Method Detail

      • getStateMessageType

        protected abstract int getStateMessageType()
        Gets the state message type.
        Returns:
        the state message type
      • getReplicateMessageType

        protected abstract int getReplicateMessageType()
        Gets the replicate message type.
        Returns:
        the replicate message type
      • wrap

        protected Member[] wrap​(Member m)
        Helper methods, wraps a single member in an array
        Parameters:
        m - Member
        Returns:
        Member[]
      • init

        protected void init​(AbstractReplicatedMap.MapOwner owner,
                            Channel channel,
                            java.lang.String mapContextName,
                            long timeout,
                            int channelSendOptions,
                            java.lang.ClassLoader[] cls,
                            boolean terminate)
        Initializes the map by creating the RPC channel, registering itself as a channel listener This method is also responsible for initiating the state transfer
        Parameters:
        owner - Object
        channel - Channel
        mapContextName - String
        timeout - long
        channelSendOptions - int
        cls - ClassLoader[]
        terminate - - Flag for whether to terminate this map that failed to start.
      • ping

        protected void ping​(long timeout)
                     throws ChannelException
        Sends a ping out to all the members in the cluster, not just map members that this map is alive.
        Parameters:
        timeout - long
        Throws:
        ChannelException - Send error
      • memberAlive

        protected void memberAlive​(Member member)
        We have received a member alive notification
        Parameters:
        member - Member
      • broadcast

        protected void broadcast​(int msgtype,
                                 boolean rpc)
                          throws ChannelException
        Helper method to broadcast a message to all members in a channel
        Parameters:
        msgtype - int
        rpc - boolean
        Throws:
        ChannelException - Send error
      • breakdown

        public void breakdown()
        Breaks down the map, removing all entries and closing channels.
      • hashCode

        public int hashCode()
        Returns the hash code for this map based on the map context name.
        Specified by:
        hashCode in interface java.util.Map<K,​V>
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code
      • equals

        public boolean equals​(java.lang.Object o)
        Checks if this map is equal to another object.
        Specified by:
        equals in interface java.util.Map<K,​V>
        Overrides:
        equals in class java.lang.Object
        Parameters:
        o - the object to compare
        Returns:
        true if the maps have the same context name
      • getMapMembers

        public Member[] getMapMembers​(java.util.HashMap<Member,​java.lang.Long> members)
        Gets the map members from the given map.
        Parameters:
        members - The member map
        Returns:
        an array of members
      • getMapMembers

        public Member[] getMapMembers()
        Gets the current map members.
        Returns:
        an array of members
      • getMapMembersExcl

        public Member[] getMapMembersExcl​(Member[] exclude)
        Gets the map members excluding the given members.
        Parameters:
        exclude - Members to exclude from the result
        Returns:
        an array of members excluding the specified ones
      • replicate

        public void replicate​(K key,
                              boolean complete)
        Replicates any changes to the object since the last time The object has to be primary, ie, if the object is a proxy or a backup, it will not be replicated
        Parameters:
        key - The object to replicate
        complete - - if set to true, the object is replicated to its backup if set to false, only objects that implement ReplicatedMapEntry and the isDirty() returns true will be replicated
      • replicate

        public void replicate​(boolean complete)
        This can be invoked by a periodic thread to replicate out any changes. For maps that don't store objects that implement ReplicatedMapEntry, this method should be used infrequently to avoid large amounts of data transfer
        Parameters:
        complete - boolean
      • transferState

        public void transferState()
        Transfers the current state from another map in the cluster.
      • replyRequest

        public java.io.Serializable replyRequest​(java.io.Serializable msg,
                                                 Member sender)
        Handles a reply request message.
        Specified by:
        replyRequest in interface RpcCallback
        Parameters:
        msg - The message
        sender - The sender
        Returns:
        the reply message or null
      • leftOver

        public void leftOver​(java.io.Serializable msg,
                             Member sender)
        Handles a left over membership message.
        Specified by:
        leftOver in interface RpcCallback
        Parameters:
        msg - The message
        sender - The sender
      • messageReceived

        public void messageReceived​(java.io.Serializable msg,
                                    Member sender)
        Handles a received message.
        Specified by:
        messageReceived in interface ChannelListener
        Parameters:
        msg - The message
        sender - The sender
      • accept

        public boolean accept​(java.io.Serializable msg,
                              Member sender)
        Accepts or rejects a message based on the map context.
        Specified by:
        accept in interface ChannelListener
        Parameters:
        msg - The message
        sender - The sender
        Returns:
        true if the message is accepted
      • mapMemberAdded

        public void mapMemberAdded​(Member member)
        Adds a member to this map.
        Parameters:
        member - The member to add
      • inSet

        public boolean inSet​(Member m,
                             Member[] set)
        Checks if a member is in the given set.
        Parameters:
        m - The member to check
        set - The set to check against
        Returns:
        true if the member is in the set
      • excludeFromSet

        public Member[] excludeFromSet​(Member[] mbrs,
                                       Member[] set)
        Excludes members from the given set.
        Parameters:
        mbrs - The members to exclude
        set - The set to exclude from
        Returns:
        The resulting set after exclusion
      • memberAdded

        public void memberAdded​(Member member)
        Called when a member is added to the channel.
        Specified by:
        memberAdded in interface MembershipListener
        Parameters:
        member - The member that was added
      • getNextBackupIndex

        public int getNextBackupIndex()
        Gets the next backup index using round-robin rotation.
        Returns:
        The next backup index, or -1 if no members exist
      • getNextBackupNode

        public Member getNextBackupNode()
        Gets the next backup node using round-robin rotation.
        Returns:
        The next backup node, or null if no members exist
      • publishEntryInfo

        protected abstract Member[] publishEntryInfo​(java.lang.Object key,
                                                     java.lang.Object value)
                                              throws ChannelException
        Publish info about a map pair (key/value) to other nodes in the cluster.
        Parameters:
        key - Object
        value - Object
        Returns:
        Member - the backup node
        Throws:
        ChannelException - Cluster error
      • heartbeat

        public void heartbeat()
        Sends a heartbeat to all members in the cluster.
        Specified by:
        heartbeat in interface Heartbeat
      • remove

        public V remove​(java.lang.Object key)
        Removes the entry for the specified key from the map.
        Specified by:
        remove in interface java.util.Map<K,​V>
        Parameters:
        key - The key to remove
        Returns:
        The previous value associated with the key, or null
      • remove

        public V remove​(java.lang.Object key,
                        boolean notify)
        Removes the entry for the specified key from the map.
        Parameters:
        key - The key to remove
        notify - Whether to notify other members
        Returns:
        The previous value associated with the key, or null
      • getInternal

        public AbstractReplicatedMap.MapEntry<K,​V> getInternal​(java.lang.Object key)
        Gets the internal map entry for a key.
        Parameters:
        key - The key
        Returns:
        the internal map entry, or null if not found
      • get

        public V get​(java.lang.Object key)
        Specified by:
        get in interface java.util.Map<K,​V>
      • printMap

        protected void printMap​(java.lang.String header)
        Prints the contents of the map for debugging purposes.
        Parameters:
        header - Header string to print before the map contents
      • containsKey

        public boolean containsKey​(java.lang.Object key)
        Returns true if the key has an entry in the map. The entry can be a proxy or a backup entry, invoking get(key) will make this entry primary for the group
        Specified by:
        containsKey in interface java.util.Map<K,​V>
        Parameters:
        key - Object
        Returns:
        boolean
      • put

        public V put​(K key,
                     V value)
        Puts a key-value pair into the map.
        Specified by:
        put in interface java.util.Map<K,​V>
        Parameters:
        key - The key
        value - The value
        Returns:
        The previous value associated with the key, or null
      • put

        public V put​(K key,
                     V value,
                     boolean notify)
        Puts a key-value pair into the map.
        Parameters:
        key - The key
        value - The value
        notify - Whether to notify other members
        Returns:
        The previous value associated with the key, or null
      • putAll

        public void putAll​(java.util.Map<? extends K,​? extends V> m)
        Copies all mappings from the specified map to this map.
        Specified by:
        putAll in interface java.util.Map<K,​V>
        Parameters:
        m - The map whose mappings are to be copied
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Map<K,​V>
      • clear

        public void clear​(boolean notify)
        Clears entries from the map.
        Parameters:
        notify - Whether to notify other members
      • containsValue

        public boolean containsValue​(java.lang.Object value)
        Specified by:
        containsValue in interface java.util.Map<K,​V>
      • entrySetFull

        public java.util.Set<java.util.Map.Entry<K,​AbstractReplicatedMap.MapEntry<K,​V>>> entrySetFull()
        Returns the entire contents of the map Map.Entry.getValue() will return a LazyReplicatedMap.MapEntry object containing all the information about the object.
        Returns:
        Set
      • keySetFull

        public java.util.Set<K> keySetFull()
        Gets the complete set of keys in the map.
        Returns:
        The complete set of keys
      • sizeFull

        public int sizeFull()
        Gets the complete size of the map.
        Returns:
        The complete size of the map
      • entrySet

        public java.util.Set<java.util.Map.Entry<K,​V>> entrySet()
        Returns a set view of the mappings contained in this map.
        Specified by:
        entrySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the mappings
      • keySet

        public java.util.Set<K> keySet()
        Returns a set view of the keys contained in this map.
        Specified by:
        keySet in interface java.util.Map<K,​V>
        Returns:
        a set view of the keys
      • size

        public int size()
        Returns the number of active entries in this map.
        Specified by:
        size in interface java.util.Map<K,​V>
        Returns:
        the number of active entries
      • isEmpty

        public boolean isEmpty()
        Checks if this map is empty.
        Specified by:
        isEmpty in interface java.util.Map<K,​V>
        Returns:
        true if the map is empty
      • values

        public java.util.Collection<V> values()
        Returns a collection view of the values contained in this map.
        Specified by:
        values in interface java.util.Map<K,​V>
        Returns:
        a collection view of the values
      • getChannel

        public Channel getChannel()
        Gets the channel used for communication.
        Returns:
        the channel
      • getMapContextName

        public byte[] getMapContextName()
        Gets the map context name.
        Returns:
        the map context name as bytes
      • getRpcChannel

        public RpcChannel getRpcChannel()
        Gets the RPC channel.
        Returns:
        the RPC channel
      • getRpcTimeout

        public long getRpcTimeout()
        Gets the RPC timeout.
        Returns:
        the RPC timeout in milliseconds
      • getStateMutex

        public java.lang.Object getStateMutex()
        Gets the state mutex object.
        Returns:
        the state mutex
      • isStateTransferred

        public boolean isStateTransferred()
        Checks if state has been transferred.
        Returns:
        true if state has been transferred
      • getExternalLoaders

        public java.lang.ClassLoader[] getExternalLoaders()
        Gets the external class loaders.
        Returns:
        the external class loaders
      • getChannelSendOptions

        public int getChannelSendOptions()
        Gets the channel send options.
        Returns:
        the channel send options
      • getAccessTimeout

        public long getAccessTimeout()
        Gets the access timeout.
        Returns:
        the access timeout in milliseconds
      • setExternalLoaders

        public void setExternalLoaders​(java.lang.ClassLoader[] externalLoaders)
        Sets the external class loaders.
        Parameters:
        externalLoaders - The external class loaders
      • setChannelSendOptions

        public void setChannelSendOptions​(int channelSendOptions)
        Sets the channel send options.
        Parameters:
        channelSendOptions - The channel send options
      • setAccessTimeout

        public void setAccessTimeout​(long accessTimeout)
        Sets the access timeout.
        Parameters:
        accessTimeout - The access timeout in milliseconds