Package org.apache.catalina.tribes.tipis
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
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
static interface
-
Field Summary
Modifier and TypeFieldDescriptionprotected long
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
Reference to the channel for sending messagesprotected int
Our default send optionsprotected int
The node we are currently backing up data to, this index will rotate on a round robin basisstatic final int
The default initial capacity - MUST be a power of two.static final float
The 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>> 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 String
Readable string of the mapContextName valueprotected AbstractReplicatedMap.MapOwner
The owner of this map, ala a SessionManager for exampleprotected RpcChannel
The RpcChannel to send RPC messages throughprotected long
Timeout for RPC messages, how long we will wait for a replyprotected static final StringManager
protected final Object
Simple lock object for transfersprotected boolean
Has the state been transferred -
Constructor Summary
ConstructorDescriptionAbstractReplicatedMap
(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 TypeMethodDescriptionboolean
accept
(Serializable msg, Member sender) Invoked by the channel to determine if the listener will process this message or not.void
protected void
broadcast
(int msgtype, boolean rpc) Helper method to broadcast a message to all members in a channelvoid
clear()
void
clear
(boolean notify) boolean
containsKey
(Object key) Returns true if the key has an entry in the map.boolean
containsValue
(Object value) entrySet()
Returns the entire contents of the map Map.Entry.getValue() will return a LazyReplicatedMap.MapEntry object containing all the information about the object.boolean
Member[]
excludeFromSet
(Member[] mbrs, Member[] set) long
int
getInternal
(Object key) byte[]
Member[]
Member[]
getMapMembers
(HashMap<Member, Long> members) Member[]
getMapMembersExcl
(Member[] exclude) int
protected abstract int
long
protected abstract int
int
hashCode()
void
Heartbeat invocation for resources cleanup etcprotected 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 transferboolean
boolean
isEmpty()
boolean
keySet()
void
leftOver
(Serializable msg, Member sender) If the reply has already been sent to the requesting thread, the rpc callback can handle any data that comes in after the fact.void
mapMemberAdded
(Member member) void
memberAdded
(Member member) A member was added to the groupprotected void
memberAlive
(Member member) We have received a member alive notificationvoid
memberDisappeared
(Member member) A member was removed from the group
If the member left voluntarily, the Member.getCommand will contain the Member.SHUTDOWN_PAYLOAD datavoid
messageReceived
(Serializable msg, Member sender) Receive a message from the channelprotected 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
protected abstract Member[]
publishEntryInfo
(Object key, Object value) Publish info about a map pair (key/value) to other nodes in the cluster.void
void
replicate
(boolean complete) This can be invoked by a periodic thread to replicate out any changes.void
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 replicatedreplyRequest
(Serializable msg, Member sender) Allows sending a response to a received message.void
setAccessTimeout
(long accessTimeout) void
setChannelSendOptions
(int channelSendOptions) void
setExternalLoaders
(ClassLoader[] externalLoaders) void
setMapOwner
(AbstractReplicatedMap.MapOwner mapOwner) int
size()
int
sizeFull()
void
values()
protected Member[]
Helper methods, wraps a single member in an arrayMethods 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 Details
-
sm
-
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
-
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
A list of members in our map -
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() -
getReplicateMessageType
protected abstract int getReplicateMessageType() -
wrap
Helper methods, wraps a single member in an array- Parameters:
m
- Member- Returns:
- Member[]
-
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() -
hashCode
public int 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() -
replyRequest
Description copied from interface:RpcCallback
Allows sending a response to a received message.- Specified by:
replyRequest
in interfaceRpcCallback
- Parameters:
msg
- The messagesender
- Member- Returns:
- Serializable object,
null
if no reply should be sent
-
leftOver
Description copied from interface:RpcCallback
If the reply has already been sent to the requesting thread, the rpc callback can handle any data that comes in after the fact.- Specified by:
leftOver
in interfaceRpcCallback
- Parameters:
msg
- The messagesender
- Member
-
messageReceived
Description copied from interface:ChannelListener
Receive a message from the channel- Specified by:
messageReceived
in interfaceChannelListener
- Parameters:
msg
- Serializablesender
- - the source of the message
-
accept
Description copied from interface:ChannelListener
Invoked by the channel to determine if the listener will process this message or not.- Specified by:
accept
in interfaceChannelListener
- Parameters:
msg
- Serializablesender
- Member- Returns:
- boolean
-
mapMemberAdded
-
inSet
-
excludeFromSet
-
memberAdded
Description copied from interface:MembershipListener
A member was added to the group- Specified by:
memberAdded
in interfaceMembershipListener
- Parameters:
member
- Member - the member that was added
-
memberDisappeared
Description copied from interface:MembershipListener
A member was removed from the group
If the member left voluntarily, the Member.getCommand will contain the Member.SHUTDOWN_PAYLOAD data- Specified by:
memberDisappeared
in interfaceMembershipListener
- Parameters:
member
- Member- See Also:
-
getNextBackupIndex
public int getNextBackupIndex() -
getNextBackupNode
-
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
public void heartbeat()Description copied from interface:Heartbeat
Heartbeat invocation for resources cleanup etc -
remove
-
remove
-
getInternal
-
get
-
printMap
-
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:
containsKey
in interfaceMap<K,
V> - Parameters:
key
- Object- Returns:
- boolean
-
put
-
put
-
putAll
-
clear
public void clear() -
clear
public void clear(boolean notify) -
containsValue
- Specified by:
containsValue
in interfaceMap<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
-
sizeFull
public int sizeFull() -
entrySet
-
keySet
-
size
public int size() -
isEmpty
public boolean isEmpty() -
values
-
getChannel
-
getMapContextName
public byte[] getMapContextName() -
getRpcChannel
-
getRpcTimeout
public long getRpcTimeout() -
getStateMutex
-
isStateTransferred
public boolean isStateTransferred() -
getMapOwner
-
getExternalLoaders
-
getChannelSendOptions
public int getChannelSendOptions() -
getAccessTimeout
public long getAccessTimeout() -
setMapOwner
-
setExternalLoaders
-
setChannelSendOptions
public void setChannelSendOptions(int channelSendOptions) -
setAccessTimeout
public void setAccessTimeout(long accessTimeout)
-