Class AbstractReplicatedMap<K,V>
java.lang.Object
org.apache.catalina.tribes.tipis.AbstractReplicatedMap<K,V>
- Type Parameters:
K- The type of KeyV- The type of Value
- All Implemented Interfaces:
Serializable, Map<K,V>, ChannelListener, RpcCallback, Heartbeat, MembershipListener
- Direct Known Subclasses:
LazyReplicatedMap, ReplicatedMap
public abstract class AbstractReplicatedMap<K,V>
extends Object
implements Map<K,V>, Serializable, RpcCallback, ChannelListener, MembershipListener, Heartbeat
An abstract replicated map implementation.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classRepresents an entry in the replicated map, including metadata about its role (primary, backup, proxy).static classRepresents a message sent between replicated map instances.static interfaceInterface for the owner of this replicated map. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longSince 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 ChannelReference to the channel for sending messagesprotected intOur default send optionsprotected intThe node we are currently backing up data to, this index will rotate on a round robin basisstatic final intThe default initial capacity - MUST be a power of two.static final floatThe load factor used when none specified in constructor.protected ClassLoader[]External class loaders if serialization and deserialization is to be performed successfully.protected final ConcurrentMap<K, AbstractReplicatedMap.MapEntry<K, V>> The underlying concurrent map storing entries.protected byte[]The Map context name makes this map unique, this allows us to have more than one map shared through one channelA list of members in our mapprotected StringReadable string of the mapContextName valueprotected AbstractReplicatedMap.MapOwnerThe owner of this map, ala a SessionManager for exampleprotected RpcChannelThe RpcChannel to send RPC messages throughprotected longTimeout for RPC messages, how long we will wait for a replyprotected static final StringManagerThe string manager for packaging specific messages.protected final ObjectSimple lock object for transfersprotected booleanHas the state been transferred -
Constructor Summary
ConstructorsConstructorDescriptionAbstractReplicatedMap(AbstractReplicatedMap.MapOwner owner, Channel channel, long timeout, String mapContextName, int initialCapacity, float loadFactor, int channelSendOptions, ClassLoader[] cls, boolean terminate) Creates a new map. -
Method Summary
Modifier and TypeMethodDescriptionbooleanaccept(Serializable msg, Member sender) Accepts or rejects a message based on the map context.voidBreaks down the map, removing all entries and closing channels.protected voidbroadcast(int msgtype, boolean rpc) Helper method to broadcast a message to all members in a channelvoidclear()voidclear(boolean notify) Clears entries from the map.booleancontainsKey(Object key) Returns true if the key has an entry in the map.booleancontainsValue(Object value) entrySet()Returns a set view of the mappings contained in this map.Returns the entire contents of the map Map.Entry.getValue() will return a LazyReplicatedMap.MapEntry object containing all the information about the object.booleanChecks if this map is equal to another object.Member[]excludeFromSet(Member[] mbrs, Member[] set) Excludes members from the given set.longGets the access timeout.Gets the channel used for communication.intGets the channel send options.Gets the external class loaders.getInternal(Object key) Gets the internal map entry for a key.byte[]Gets the map context name.Member[]Gets the current map members.Member[]getMapMembers(HashMap<Member, Long> members) Gets the map members from the given map.Member[]getMapMembersExcl(Member[] exclude) Gets the map members excluding the given members.Gets the map owner.intGets the next backup index using round-robin rotation.Gets the next backup node using round-robin rotation.protected abstract intGets the replicate message type.Gets the RPC channel.longGets the RPC timeout.protected abstract intGets the state message type.Gets the state mutex object.inthashCode()Returns the hash code for this map based on the map context name.voidSends a heartbeat to all members in the cluster.protected voidinit(AbstractReplicatedMap.MapOwner owner, Channel channel, String mapContextName, long timeout, int channelSendOptions, 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 transferbooleanChecks if a member is in the given set.booleanisEmpty()Checks if this map is empty.booleanChecks if state has been transferred.keySet()Returns a set view of the keys contained in this map.Gets the complete set of keys in the map.voidleftOver(Serializable msg, Member sender) Handles a left over membership message.voidmapMemberAdded(Member member) Adds a member to this map.voidmemberAdded(Member member) Called when a member is added to the channel.protected voidmemberAlive(Member member) We have received a member alive notificationvoidmemberDisappeared(Member member) Called when a member disappears from the channel.voidmessageReceived(Serializable msg, Member sender) Handles a received message.protected voidping(long timeout) Sends a ping out to all the members in the cluster, not just map members that this map is alive.protected voidPrints the contents of the map for debugging purposes.protected abstract Member[]publishEntryInfo(Object key, Object value) Publish info about a map pair (key/value) to other nodes in the cluster.Puts a key-value pair into the map.Puts a key-value pair into the map.voidCopies all mappings from the specified map to this map.Removes the entry for the specified key from the map.Removes the entry for the specified key from the map.voidreplicate(boolean complete) This can be invoked by a periodic thread to replicate out any changes.voidReplicates 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 replicatedreplyRequest(Serializable msg, Member sender) Handles a reply request message.voidsetAccessTimeout(long accessTimeout) Sets the access timeout.voidsetChannelSendOptions(int channelSendOptions) Sets the channel send options.voidsetExternalLoaders(ClassLoader[] externalLoaders) Sets the external class loaders.voidsetMapOwner(AbstractReplicatedMap.MapOwner mapOwner) Sets the map owner.intsize()Returns the number of active entries in this map.intsizeFull()Gets the complete size of the map.voidTransfers the current state from another map in the cluster.values()Returns a collection view of the values contained in this map.protected Member[]Helper methods, wraps a single member in an arrayMethods inherited from class Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
sm
The string manager for packaging specific messages. -
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITYThe default initial capacity - MUST be a power of two.- See Also:
-
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTORThe load factor used when none specified in constructor.- See Also:
-
innerMap
The underlying concurrent map storing entries. -
rpcTimeout
protected transient long rpcTimeoutTimeout for RPC messages, how long we will wait for a reply -
channel
Reference to the channel for sending messages -
rpcChannel
The RpcChannel to send RPC messages through -
mapContextName
protected transient byte[] mapContextNameThe Map context name makes this map unique, this allows us to have more than one map shared through one channel -
stateTransferred
protected transient boolean stateTransferredHas the state been transferred -
stateMutex
Simple lock object for transfers -
mapMembers
-
channelSendOptions
protected transient int channelSendOptionsOur default send options -
mapOwner
The owner of this map, ala a SessionManager for example -
externalLoaders
External class loaders if serialization and deserialization is to be performed successfully. -
currentNode
protected transient int currentNodeThe node we are currently backing up data to, this index will rotate on a round robin basis -
accessTimeout
protected transient long accessTimeoutSince 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
Readable string of the mapContextName value
-
-
Constructor Details
-
AbstractReplicatedMap
public AbstractReplicatedMap(AbstractReplicatedMap.MapOwner owner, Channel channel, long timeout, String mapContextName, int initialCapacity, float loadFactor, int channelSendOptions, ClassLoader[] cls, boolean terminate) Creates a new map.- Parameters:
owner- The map ownerchannel- The channel to use for communicationtimeout- long - timeout for RPC messagesmapContextName- String - unique name for this map, to allow multiple maps per channelinitialCapacity- int - the size of this map, see HashMaploadFactor- float - load factor, see HashMapchannelSendOptions- Send optionscls- - a list of classloaders to be used for deserialization of objects.terminate- - Flag for whether to terminate this map that failed to start.
-
-
Method Details
-
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
-
init
protected void init(AbstractReplicatedMap.MapOwner owner, Channel channel, String mapContextName, long timeout, int channelSendOptions, 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- Objectchannel- ChannelmapContextName- Stringtimeout- longchannelSendOptions- intcls- ClassLoader[]terminate- - Flag for whether to terminate this map that failed to start.
-
ping
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
We have received a member alive notification- Parameters:
member- Member
-
broadcast
Helper method to broadcast a message to all members in a channel- Parameters:
msgtype- intrpc- boolean- Throws:
ChannelException- Send error
-
breakdown
public void breakdown()Breaks down the map, removing all entries and closing channels. -
hashCode
-
equals
-
getMapMembers
-
getMapMembers
-
getMapMembersExcl
-
replicate
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 replicatecomplete- - 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
Handles a reply request message.- Specified by:
replyRequestin interfaceRpcCallback- Parameters:
msg- The messagesender- The sender- Returns:
- the reply message or
null
-
leftOver
Handles a left over membership message.- Specified by:
leftOverin interfaceRpcCallback- Parameters:
msg- The messagesender- The sender
-
messageReceived
Handles a received message.- Specified by:
messageReceivedin interfaceChannelListener- Parameters:
msg- The messagesender- The sender
-
accept
Accepts or rejects a message based on the map context.- Specified by:
acceptin interfaceChannelListener- Parameters:
msg- The messagesender- The sender- Returns:
trueif the message is accepted
-
mapMemberAdded
Adds a member to this map.- Parameters:
member- The member to add
-
inSet
-
excludeFromSet
-
memberAdded
Called when a member is added to the channel.- Specified by:
memberAddedin interfaceMembershipListener- Parameters:
member- The member that was added
-
memberDisappeared
Called when a member disappears from the channel.- Specified by:
memberDisappearedin interfaceMembershipListener- Parameters:
member- The member that disappeared- See Also:
-
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
Gets the next backup node using round-robin rotation.- Returns:
- The next backup node, or
nullif no members exist
-
publishEntryInfo
Publish info about a map pair (key/value) to other nodes in the cluster.- Parameters:
key- Objectvalue- Object- Returns:
- Member - the backup node
- Throws:
ChannelException- Cluster error
-
heartbeat
-
remove
-
remove
-
getInternal
Gets the internal map entry for a key.- Parameters:
key- The key- Returns:
- the internal map entry, or
nullif not found
-
get
-
printMap
Prints the contents of the map for debugging purposes.- Parameters:
header- Header string to print before the map contents
-
containsKey
Returns true if the key has an entry in the map. The entry can be a proxy or a backup entry, invokingget(key)will make this entry primary for the group- Specified by:
containsKeyin interfaceMap<K,V> - Parameters:
key- Object- Returns:
- boolean
-
put
-
put
-
putAll
-
clear
-
clear
public void clear(boolean notify) Clears entries from the map.- Parameters:
notify- Whether to notify other members
-
containsValue
- Specified by:
containsValuein interfaceMap<K,V>
-
entrySetFull
-
keySetFull
-
sizeFull
public int sizeFull()Gets the complete size of the map.- Returns:
- The complete size of the map
-
entrySet
-
keySet
-
size
-
isEmpty
-
values
-
getChannel
-
getMapContextName
public byte[] getMapContextName()Gets the map context name.- Returns:
- the map context name as bytes
-
getRpcChannel
-
getRpcTimeout
public long getRpcTimeout()Gets the RPC timeout.- Returns:
- the RPC timeout in milliseconds
-
getStateMutex
-
isStateTransferred
public boolean isStateTransferred()Checks if state has been transferred.- Returns:
trueif state has been transferred
-
getMapOwner
-
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
-
setMapOwner
Sets the map owner.- Parameters:
mapOwner- The map owner
-
setExternalLoaders
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
-