Class ReplicatedHashMap<K extends Serializable,V extends Serializable>
- All Implemented Interfaces:
Serializable
,ConcurrentMap<K,
,V> Map<K,
,V> ReplicatedMap<K,
,V> ExtendedMembershipListener
,ExtendedMessageListener
,ExtendedReceiver
,MembershipListener
,MessageListener
,Receiver
ConcurrentHashMap
with replication of the contents across a cluster.
Any change to the hashmap (clear(), put(), remove() etc) will transparently be
propagated to all replicas in the group. All read-only methods will always access the local replica.
Keys and values added to the hashmap must be serializable, the reason
being that they will be sent across the network to all replicas of the group. Having said
this, it is now for example possible to add RMI remote objects to the hashtable as they
are derived from java.rmi.server.RemoteObject
which in turn is serializable.
This allows to lookup shared distributed objects by their name and invoke methods on them,
regardless of one's onw location. A ReplicatedHashMap
thus allows to
implement a distributed naming service in just a couple of lines.
An instance of this class will contact an existing member of the group to fetch its initial state.
This class combines both ReplicatedHashtable
(asynchronous replication) and
DistributedHashtable
(synchronous replication) into one class
- Version:
- $Id: ReplicatedHashMap.java,v 1.12.2.3 2008/05/13 12:02:23 vlada Exp $
- Author:
- Bela Ban
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
ReplicatedHashMap.Notification<K extends Serializable,
V extends Serializable> Nested classes/interfaces inherited from class java.util.concurrent.ConcurrentHashMap
ConcurrentHashMap.KeySetView<K,
V> Nested classes/interfaces inherited from class java.util.AbstractMap
AbstractMap.SimpleEntry<K,
V>, AbstractMap.SimpleImmutableEntry<K, V> -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected RpcDispatcher
protected final org.apache.commons.logging.Log
protected long
For blocking updates only: the max time to wait (0 == forever)protected int
Whether updates across the cluster should be asynchronous (default) or synchronous) -
Constructor Summary
ConstructorsConstructorDescriptionReplicatedHashMap
(String clustername, ChannelFactory factory, String properties, boolean persistent, long state_timeout) Creates a ReplicatedHashMap.ReplicatedHashMap
(String clustername, ChannelFactory factory, String properties, long state_timeout) Creates a ReplicatedHashMapReplicatedHashMap
(Channel channel) ReplicatedHashMap
(Channel channel, boolean persistent) -
Method Summary
Modifier and TypeMethodDescriptionvoid
_clear()
void
_putIfAbsent
(K key, V value) boolean
boolean
void
void
block()
Block sending and receiving of messages until ViewAccepted is calledvoid
clear()
Removes all of the mappings from this map.boolean
byte[]
getState()
Answers the group state; e.g., when joining.void
getState
(OutputStream ostream) Allows an application to write a state through a provided OutputStream.byte[]
Allows an application to provide a partial state as a byte arrayvoid
getState
(String state_id, OutputStream ostream) Allows an application to write a partial state through a provided OutputStream.long
The timeout (in milliseconds) for blocking updatesprotected final void
init()
boolean
Maps the specified key to the specified value in this table.void
Copies all of the mappings from the specified map to this one.putIfAbsent
(K key, V value) void
Called when a message is received.Removes the key (and its corresponding value) from this map.boolean
void
boolean
void
setBlockingUpdates
(boolean blocking_updates) Whether updates across the cluster should be asynchronous (default) or synchronous)void
setDeadlockDetection
(boolean flag) void
setPersistent
(boolean p) void
setState
(byte[] new_state) Sets the group state; e.g., when joining.void
setState
(InputStream istream) Allows an application to read a state through a provided InputStream.void
Allows an application to read a partial state indicated by state_id from a given state byte array parameter.void
setState
(String state_id, InputStream istream) Allows an application to read a partial state through a provided InputStream.void
setTimeout
(long timeout) Sets the cluster call timeout (until all acks have been received)final void
start
(long state_timeout) Fetches the statevoid
stop()
void
Called when a member is suspectedstatic <K extends Serializable,
V extends Serializable>
ReplicatedMap<K, V> synchronizedMap
(ReplicatedMap<K, V> map) Creates a synchronized facade for a ReplicatedMap.void
unblock()
Called after the FLUSH protocol has unblocked previously blocked senders, and messages can be sent again.void
viewAccepted
(View new_view) Called when a change in membership has occurred.Methods inherited from class java.util.concurrent.ConcurrentHashMap
compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, forEach, forEach, forEachEntry, forEachEntry, forEachKey, forEachKey, forEachValue, forEachValue, get, getOrDefault, hashCode, isEmpty, keys, keySet, keySet, mappingCount, merge, newKeySet, newKeySet, reduce, reduceEntries, reduceEntries, reduceEntriesToDouble, reduceEntriesToInt, reduceEntriesToLong, reduceKeys, reduceKeys, reduceKeysToDouble, reduceKeysToInt, reduceKeysToLong, reduceToDouble, reduceToInt, reduceToLong, reduceValues, reduceValues, reduceValuesToDouble, reduceValuesToInt, reduceValuesToLong, replaceAll, search, searchEntries, searchKeys, searchValues, size, toString, values
Methods inherited from class java.util.AbstractMap
clone
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.concurrent.ConcurrentMap
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, replaceAll
-
Field Details
-
methods
-
disp
-
state_promise
-
update_mode
protected int update_modeWhether updates across the cluster should be asynchronous (default) or synchronous) -
timeout
protected long timeoutFor blocking updates only: the max time to wait (0 == forever) -
log
protected final org.apache.commons.logging.Log log
-
-
Constructor Details
-
ReplicatedHashMap
public ReplicatedHashMap(String clustername, ChannelFactory factory, String properties, long state_timeout) throws ChannelException Creates a ReplicatedHashMap- Parameters:
clustername
- The name of the group to joinfactory
- The ChannelFactory which will be used to create a channelproperties
- The property string to be used to define the channel. This will override the properties of the factory. If null, then the factory properties will be usedstate_timeout
- The time to wait until state is retrieved in milliseconds. A value of 0 means wait forever.- Throws:
ChannelException
-
ReplicatedHashMap
public ReplicatedHashMap(String clustername, ChannelFactory factory, String properties, boolean persistent, long state_timeout) throws ChannelException Creates a ReplicatedHashMap. Optionally the contents can be saved to persistemt storage using thePersistenceManager
.- Parameters:
clustername
- Name of the group to joinfactory
- Instance of a ChannelFactory to create the channelproperties
- Protocol stack properties. This will override the properties of the factory. If null, then the factory properties will be usedpersistent
- Whether the contents should be persistedstate_timeout
- Max number of milliseconds to wait until the state is retrieved- Throws:
ChannelException
-
ReplicatedHashMap
-
ReplicatedHashMap
-
-
Method Details
-
init
protected final void init() -
isBlockingUpdates
public boolean isBlockingUpdates() -
setBlockingUpdates
public void setBlockingUpdates(boolean blocking_updates) Whether updates across the cluster should be asynchronous (default) or synchronous)- Parameters:
blocking_updates
-
-
getTimeout
public long getTimeout()The timeout (in milliseconds) for blocking updates -
setTimeout
public void setTimeout(long timeout) Sets the cluster call timeout (until all acks have been received)- Parameters:
timeout
- The timeout (in milliseconds) for blocking updates
-
start
public final void start(long state_timeout) throws ChannelClosedException, ChannelNotConnectedException Fetches the state- Parameters:
state_timeout
-- Throws:
ChannelClosedException
ChannelNotConnectedException
-
getLocalAddress
-
getClusterName
-
getChannel
-
getPersistent
public boolean getPersistent() -
setPersistent
public void setPersistent(boolean p) -
setDeadlockDetection
public void setDeadlockDetection(boolean flag) -
addNotifier
-
removeNotifier
-
stop
public void stop() -
put
Maps the specified key to the specified value in this table. Neither the key nor the value can be null.The value can be retrieved by calling the get method with a key that is equal to the original key.
- Specified by:
put
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
put
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with key, or null if there was no mapping for key
- Throws:
NullPointerException
- if the specified key or value is null
-
putIfAbsent
- Specified by:
putIfAbsent
in interfaceConcurrentMap<K extends Serializable,
V extends Serializable> - Specified by:
putIfAbsent
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
putIfAbsent
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
NullPointerException
- if the specified key or value is null
-
putAll
Copies all of the mappings from the specified map to this one. These mappings replace any mappings that this map had for any of the keys currently in the specified map.- Specified by:
putAll
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
putAll
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Parameters:
m
- mappings to be stored in this map
-
clear
public void clear()Removes all of the mappings from this map.- Specified by:
clear
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
clear
in classConcurrentHashMap<K extends Serializable,
V extends Serializable>
-
remove
Removes the key (and its corresponding value) from this map. This method does nothing if the key is not in the map.- Specified by:
remove
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
remove
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Parameters:
key
- the key that needs to be removed- Returns:
- the previous value associated with key, or null if there was no mapping for key
- Throws:
NullPointerException
- if the specified key is null
-
remove
- Specified by:
remove
in interfaceConcurrentMap<K extends Serializable,
V extends Serializable> - Specified by:
remove
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
remove
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Throws:
NullPointerException
- if the specified key is null
-
replace
- Specified by:
replace
in interfaceConcurrentMap<K extends Serializable,
V extends Serializable> - Specified by:
replace
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
replace
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Throws:
NullPointerException
- if any of the arguments are null
-
replace
- Specified by:
replace
in interfaceConcurrentMap<K extends Serializable,
V extends Serializable> - Specified by:
replace
in interfaceMap<K extends Serializable,
V extends Serializable> - Overrides:
replace
in classConcurrentHashMap<K extends Serializable,
V extends Serializable> - Returns:
- the previous value associated with the specified key, or null if there was no mapping for the key
- Throws:
NullPointerException
- if the specified key or value is null
-
_put
- Specified by:
_put
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_putIfAbsent
- Specified by:
_putIfAbsent
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_putAll
- Specified by:
_putAll
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable> - See Also:
-
_clear
public void _clear()- Specified by:
_clear
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_remove
- Specified by:
_remove
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_remove
- Specified by:
_remove
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_replace
- Specified by:
_replace
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
_replace
- Specified by:
_replace
in interfaceReplicatedMap<K extends Serializable,
V extends Serializable>
-
receive
Description copied from interface:MessageListener
Called when a message is received.- Specified by:
receive
in interfaceMessageListener
- Parameters:
msg
-
-
getState
public byte[] getState()Description copied from interface:MessageListener
Answers the group state; e.g., when joining.- Specified by:
getState
in interfaceMessageListener
- Returns:
- byte[]
-
setState
public void setState(byte[] new_state) Description copied from interface:MessageListener
Sets the group state; e.g., when joining.- Specified by:
setState
in interfaceMessageListener
- Parameters:
new_state
-
-
viewAccepted
Description copied from interface:MembershipListener
Called when a change in membership has occurred. No long running actions or sending of messages should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread. Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens whenChannel.connect(String)
returns.- Specified by:
viewAccepted
in interfaceMembershipListener
-
suspect
Called when a member is suspected- Specified by:
suspect
in interfaceMembershipListener
-
block
public void block()Block sending and receiving of messages until ViewAccepted is called- Specified by:
block
in interfaceMembershipListener
-
getState
Description copied from interface:ExtendedMessageListener
Allows an application to provide a partial state as a byte array- Specified by:
getState
in interfaceExtendedMessageListener
- Parameters:
state_id
- id of the partial state requested- Returns:
- partial state for the given state_id
-
getState
Description copied from interface:ExtendedMessageListener
Allows an application to write a state through a provided OutputStream. An application is obligated to always close the given OutputStream reference.- Specified by:
getState
in interfaceExtendedMessageListener
- Parameters:
ostream
- the OutputStream- See Also:
-
getState
Description copied from interface:ExtendedMessageListener
Allows an application to write a partial state through a provided OutputStream. An application is obligated to always close the given OutputStream reference.- Specified by:
getState
in interfaceExtendedMessageListener
- Parameters:
state_id
- id of the partial state requestedostream
- the OutputStream- See Also:
-
setState
Description copied from interface:ExtendedMessageListener
Allows an application to read a partial state indicated by state_id from a given state byte array parameter.- Specified by:
setState
in interfaceExtendedMessageListener
- Parameters:
state_id
- id of the partial state requestedstate
- partial state for the given state_id
-
setState
Description copied from interface:ExtendedMessageListener
Allows an application to read a state through a provided InputStream. An application is obligated to always close the given InputStream reference.- Specified by:
setState
in interfaceExtendedMessageListener
- Parameters:
istream
- the InputStream- See Also:
-
setState
Description copied from interface:ExtendedMessageListener
Allows an application to read a partial state through a provided InputStream. An application is obligated to always close the given InputStream reference.- Specified by:
setState
in interfaceExtendedMessageListener
- Parameters:
state_id
- id of the partial state requestedistream
- the InputStream- See Also:
-
unblock
public void unblock()Description copied from interface:ExtendedMembershipListener
Called after the FLUSH protocol has unblocked previously blocked senders, and messages can be sent again. This callback only needs to be implemented if we require a notification of that.- Specified by:
unblock
in interfaceExtendedMembershipListener
-
synchronizedMap
public static <K extends Serializable,V extends Serializable> ReplicatedMap<K,V> synchronizedMap(ReplicatedMap<K, V> map) Creates a synchronized facade for a ReplicatedMap. All methods which change state are invoked through a monitor. This is similar to , but also includes the replication methods (starting with an underscore).- Parameters:
map
-- Returns:
-