Package org.jgroups.blocks
Class ReplicatedTree
- java.lang.Object
-
- org.jgroups.blocks.ReplicatedTree
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classReplicatedTree.MyListenerstatic classReplicatedTree.Nodestatic interfaceReplicatedTree.ReplicatedTreeListenerprivate static classReplicatedTree.RequestClass used to multicast add(), remove() and set() methods to all members.private static classReplicatedTree.StringHolder
-
Field Summary
Fields Modifier and Type Field Description (package private) JChannelchannel(package private) java.lang.Stringgroupname(package private) static intINDENT(package private) booleanjmx(package private) java.util.List<ReplicatedTree.ReplicatedTreeListener>listenersprotected Loglog(package private) java.util.List<Address>members(package private) java.lang.Stringprops(package private) booleanremote_callsWhether or not to use remote calls.(package private) ReplicatedTree.Noderootprivate booleansend_messageDetermines when the updates have to be sent across the network, avoids sending unnecessary messages when there are no member in the groupstatic java.lang.StringSEPARATOR(package private) longstate_fetch_timeout
-
Constructor Summary
Constructors Constructor Description ReplicatedTree()ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout)Creates a channel with the given properties.ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout, boolean jmx)ReplicatedTree(JChannel channel)Expects an already connected channel.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void_put(java.lang.String fqn, java.lang.String key, java.lang.Object value)void_put(java.lang.String fqn, java.util.HashMap data)void_remove(java.lang.String fqn)void_remove(java.lang.String fqn, java.lang.String key)void_removeData(java.lang.String fqn)voidaddReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)booleanexists(java.lang.String fqn)Checks whether a given node exists in the treevoidfetchState(long timeout)Fetch the group state from the current coordinator.(package private) ReplicatedTree.NodefindNode(java.lang.String fqn)Returns the node at fqn.(package private) ReplicatedTree.NodefindParentNode(java.lang.String fqn, ReplicatedTree.StringHolder child_name, boolean create_if_not_exists)Find the node just above the one indicated byfqn.(package private) java.util.Map<java.lang.String,java.lang.Object>get(java.lang.String fqn)Returns the data hashmap for a given node.java.lang.Objectget(java.lang.String fqn, java.lang.String key)Finds a node given its name and returns the value associated with a given key in itsdatamap.JChannelgetChannel()Returns the Channel the DistributedTree is connected tojava.util.SetgetChildrenNames(java.lang.String fqn)Returns all children of a given nodeintgetGroupMembersNumber()Returns the number of current members joined to the groupjava.lang.StringgetGroupName()Returns the name of the group that the DistributedTree is connected tojava.util.SetgetKeys(java.lang.String fqn)Gets the keys of thedatamap.AddressgetLocalAddress()java.util.List<Address>getMembers()voidgetState(java.io.OutputStream ostream)Allows an application to write the state to an OutputStream.static voidmain(java.lang.String[] args)(package private) voidnotifyAllNodesCreated(ReplicatedTree.Node curr)Generates NodeAdded notifications for all nodes of the tree.(package private) voidnotifyNodeAdded(java.lang.String fqn)(package private) voidnotifyNodeModified(java.lang.String fqn)(package private) voidnotifyNodeRemoved(java.lang.String fqn)(package private) voidnotifyViewChange(View v)java.lang.Stringprint(java.lang.String fqn)Prints a representation of the node defined byfqn.voidput(java.lang.String fqn, java.lang.String key, java.lang.Object value)Adds a key and value to a given node.voidput(java.lang.String fqn, java.util.HashMap data)Adds a new node to the tree and sets its data.voidreceive(Message msg)Callback.voidremove(java.lang.String fqn)Removes the node from the tree.voidremove(java.lang.String fqn, java.lang.String key)Removeskeyfrom the node's hashmapvoidremoveReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)voidsetRemoteCalls(boolean flag)voidsetRootNode(ReplicatedTree.Node n)voidsetState(java.io.InputStream istream)Allows an application to read the state from an InputStream.voidstart()voidstop()java.lang.StringtoString()voidviewAccepted(View new_view)Called when a change in membership has occurred.
-
-
-
Field Detail
-
SEPARATOR
public static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
INDENT
static final int INDENT
- See Also:
- Constant Field Values
-
root
ReplicatedTree.Node root
-
listeners
final java.util.List<ReplicatedTree.ReplicatedTreeListener> listeners
-
channel
JChannel channel
-
groupname
java.lang.String groupname
-
members
final java.util.List<Address> members
-
state_fetch_timeout
long state_fetch_timeout
-
jmx
boolean jmx
-
log
protected final Log log
-
remote_calls
boolean remote_calls
Whether or not to use remote calls. If false, all methods will be invoked directly on this instance rather than sending a message to all replicas and only then invoking the method. Useful for testing
-
props
java.lang.String props
-
send_message
private boolean send_message
Determines when the updates have to be sent across the network, avoids sending unnecessary messages when there are no member in the group
-
-
Constructor Detail
-
ReplicatedTree
public ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout) throws java.lang.ExceptionCreates a channel with the given properties. Connects to the channel, then creates a PullPushAdapter and starts it- Throws:
java.lang.Exception
-
ReplicatedTree
public ReplicatedTree(java.lang.String groupname, java.lang.String props, long state_fetch_timeout, boolean jmx) throws java.lang.Exception- Throws:
java.lang.Exception
-
ReplicatedTree
public ReplicatedTree()
-
ReplicatedTree
public ReplicatedTree(JChannel channel) throws java.lang.Exception
Expects an already connected channel. Creates a PullPushAdapter and starts it- Throws:
java.lang.Exception
-
-
Method Detail
-
setRemoteCalls
public void setRemoteCalls(boolean flag)
-
setRootNode
public void setRootNode(ReplicatedTree.Node n)
-
getLocalAddress
public Address getLocalAddress()
-
getMembers
public java.util.List<Address> getMembers()
-
fetchState
public void fetchState(long timeout) throws java.lang.ExceptionFetch the group state from the current coordinator. If successful, this will trigger setState().- Throws:
java.lang.Exception
-
addReplicatedTreeListener
public void addReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
-
removeReplicatedTreeListener
public void removeReplicatedTreeListener(ReplicatedTree.ReplicatedTreeListener listener)
-
start
public final void start() throws java.lang.Exception- Throws:
java.lang.Exception
-
stop
public void stop()
-
put
public void put(java.lang.String fqn, java.util.HashMap data)Adds a new node to the tree and sets its data. If the node doesn not yet exist, it will be created. Also, parent nodes will be created if not existent. If the node already has data, then the new data will override the old one. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.- Parameters:
fqn- The fully qualified name of the new nodedata- The new data. May be null if no data should be set in the node.
-
put
public void put(java.lang.String fqn, java.lang.String key, java.lang.Object value)Adds a key and value to a given node. If the node doesn't exist, it will be created. If the node already existed, a nodeModified() notification will be generated. Otherwise a nodeCreated() motification will be emitted.- Parameters:
fqn- The fully qualified name of the nodekey- The keyvalue- The value
-
remove
public void remove(java.lang.String fqn)
Removes the node from the tree.- Parameters:
fqn- The fully qualified name of the node.
-
remove
public void remove(java.lang.String fqn, java.lang.String key)Removeskeyfrom the node's hashmap- Parameters:
fqn- The fullly qualified name of the nodekey- The key to be removed
-
exists
public boolean exists(java.lang.String fqn)
Checks whether a given node exists in the tree- Parameters:
fqn- The fully qualified name of the node- Returns:
- boolean Whether or not the node exists
-
getKeys
public java.util.Set getKeys(java.lang.String fqn)
Gets the keys of thedatamap. Returns all keys as Strings. Returns null if node does not exist.- Parameters:
fqn- The fully qualified name of the node- Returns:
- Set A set of keys (as Strings)
-
get
public java.lang.Object get(java.lang.String fqn, java.lang.String key)Finds a node given its name and returns the value associated with a given key in itsdatamap. Returns null if the node was not found in the tree or the key was not found in the hashmap.- Parameters:
fqn- The fully qualified name of the node.key- The key.
-
get
java.util.Map<java.lang.String,java.lang.Object> get(java.lang.String fqn)
Returns the data hashmap for a given node. This method can only be used by callers that are inside the same package. The reason is that callers must not modify the return value, as these modifications would not be replicated, thus rendering the replicas inconsistent.- Parameters:
fqn- The fully qualified name of the node- Returns:
- HashMap The data hashmap for the given node
-
print
public java.lang.String print(java.lang.String fqn)
Prints a representation of the node defined byfqn. Output includes name, fqn and data.
-
getChildrenNames
public java.util.Set getChildrenNames(java.lang.String fqn)
Returns all children of a given node- Parameters:
fqn- The fully qualified name of the node- Returns:
- Set A list of child names (as Strings)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getGroupName
public java.lang.String getGroupName()
Returns the name of the group that the DistributedTree is connected to- Returns:
- String
-
getChannel
public JChannel getChannel()
Returns the Channel the DistributedTree is connected to- Returns:
- Channel
-
getGroupMembersNumber
public int getGroupMembersNumber()
Returns the number of current members joined to the group- Returns:
- int
-
_put
public void _put(java.lang.String fqn, java.util.HashMap data)
-
_put
public void _put(java.lang.String fqn, java.lang.String key, java.lang.Object value)
-
_remove
public void _remove(java.lang.String fqn)
-
_remove
public void _remove(java.lang.String fqn, java.lang.String key)
-
_removeData
public void _removeData(java.lang.String fqn)
-
receive
public void receive(Message msg)
Callback. Process the contents of the message; typically an _add() or _set() request
-
getState
public void getState(java.io.OutputStream ostream) throws java.lang.ExceptionDescription copied from interface:ReceiverAllows an application to write the state to an OutputStream. After the state has been written, the OutputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
-
setState
public void setState(java.io.InputStream istream) throws java.lang.ExceptionDescription copied from interface:ReceiverAllows an application to read the state from an InputStream. After the state has been read, the InputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.
-
viewAccepted
public void viewAccepted(View new_view)
Description copied from interface:ReceiverCalled when a change in membership has occurred. No long running actions, sending of messages or anything that could block 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 whenJChannel.connect(String)returns.- Specified by:
viewAcceptedin interfaceReceiver
-
findParentNode
ReplicatedTree.Node findParentNode(java.lang.String fqn, ReplicatedTree.StringHolder child_name, boolean create_if_not_exists)
Find the node just above the one indicated byfqn. This is needed in many cases, e.g. to add a new node or remove an existing node.- Parameters:
fqn- The fully qualified name of the node.child_name- Will be filled with the name of the child when this method returns. The child name is the last relative name of thefqn, e.g. in "/a/b/c" it would be "c".create_if_not_exists- Create parent nodes along the way if they don't exist. Otherwise, this method will return when a node cannot be found.
-
findNode
ReplicatedTree.Node findNode(java.lang.String fqn)
Returns the node at fqn. This method should not be used by clients (therefore it is package-private): it is only used internally (for navigation). C++ 'friend' would come in handy here...- Parameters:
fqn- The fully qualified name of the node- Returns:
- Node The node at fqn
-
notifyNodeAdded
void notifyNodeAdded(java.lang.String fqn)
-
notifyNodeRemoved
void notifyNodeRemoved(java.lang.String fqn)
-
notifyNodeModified
void notifyNodeModified(java.lang.String fqn)
-
notifyViewChange
void notifyViewChange(View v)
-
notifyAllNodesCreated
void notifyAllNodesCreated(ReplicatedTree.Node curr)
Generates NodeAdded notifications for all nodes of the tree. This is called whenever the tree is initially retrieved (state transfer)
-
main
public static void main(java.lang.String[] args)
-
-