Package com.jalios.jstore.jsync
Class JSyncReplica
- java.lang.Object
-
- com.jalios.jstore.jsync.JSyncReplica
-
- All Implemented Interfaces:
JSyncConstants
,JaliosConstants
public class JSyncReplica extends java.lang.Object implements JSyncConstants
This class provides supports to manage a replica, manages JSyncListener, send and process the JSyncMessage.- Author:
- Olivier Dedieu
-
-
Field Summary
-
Fields inherited from interface com.jalios.util.JaliosConstants
CRLF, MILLIS_IN_ONE_DAY, MILLIS_IN_ONE_HOUR, MILLIS_IN_ONE_MINUTE, MILLIS_IN_ONE_MONTH, MILLIS_IN_ONE_SECOND, MILLIS_IN_ONE_WEEK, MILLIS_IN_ONE_YEAR
-
Fields inherited from interface com.jalios.jstore.jsync.JSyncConstants
HEADER_SECRET, SC_BAD_ADDRESS, SC_BAD_LEADER, SC_BAD_LOG_PREFIX, SC_BAD_MESSAGE, SC_CHECK_COMMIT_WORK_FAILED, SC_COMMIT_WORK_FAILED, SC_DISCONNECTED, SC_EMPTY_PT, SC_INCOMPATIBLE_LOG_FORMAT, SC_INTERNAL_SERVER_ERROR, SC_JOIN_REJECTED, SC_LEADER_CANNOT_JOIN, SC_NO_ACK, SC_NO_SEND, SC_OK, SC_UNKNOWN, SC_UNKNOWN_URID, SC_URID_ALREADY_ADDED, STATUS_DISCONNECTED, STATUS_LEADER, STATUS_LONE, STATUS_MEMBER, STATUS_SUBLEADER
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addListener(JSyncListener listener)
Adds a JSync listenerCommitWorkResult
canCommitWork()
void
close()
Close all JSyncReplica resources when ending.CommitWorkResult
commitWork()
CommitWorkResult
commitWork(CommitWorkObserver obs)
int
disjoin()
Disjoins from the leader replica.static JSyncReplica
getInstance(JSyncSettings settings)
Gets a replicaReplicaInfo
getLeader()
java.lang.String
getLeaderUrl()
JSyncLogger
getLogger()
long
getMaxOpPerUpdate()
java.util.Set<java.lang.String>
getNotConnectedReplicaSet()
int
getPartialUpdateInterval()
Stamp
getProgressStamp()
StampTable
getProgressTable()
java.util.Map<java.lang.String,ReplicaInfo>
getReplicaMap()
int
getRequestTimeout()
java.lang.String
getStatus()
Return the status of this replica (STATUS_LONE, STATUS_MEMBER, STATUS_SUBLEADER or STATUS_LEADER)java.lang.String
getStatusLabel(int sc, java.util.Locale locale)
boolean
getSyncFiles()
Return true if file replication is enabledint
getUpdateRequestTimeout()
java.lang.String
getUrid()
java.lang.String
getUrl()
int
globalSuggestJoin()
Suggest all replica to join this replica as leader.boolean
isCanBeJoined()
boolean
isDisconnected()
Returns true if this replica is disconnected from its leader.boolean
isJoined()
boolean
isKnownReplica(java.lang.String urid)
Returns true if this replica is known (i.e.boolean
isLeader()
boolean
isSubLeader()
boolean
isSuperLeader()
int
join()
Joins a replica.boolean
needForUpdate()
boolean
processCommitWorkMessage(java.io.OutputStream out, CommitWorkMessage msg)
Process a CommitWorkMessageboolean
processDisjoinMessage(java.io.OutputStream out, DisjoinMessage msg)
Process a DisjoinMessageboolean
processJoinMessage(java.io.OutputStream out, JoinMessage msg)
Process a JoinMessageboolean
processMessage(java.io.OutputStream out, Message msg)
Dispatches on the right method to process the given messageboolean
processSuggestJoinMessage(java.io.OutputStream out, SuggestJoinMessage msg)
Process a SuggestJoinMessageboolean
processUpdateMessage(java.io.OutputStream out, UpdateMessage msg)
Process an UpdateMessagevoid
removeListener(JSyncListener listener)
Removes a JSync listenervoid
removeReplica(java.lang.String urid)
Removes the given replica of the group of this leadervoid
retryUpdateLeader()
void
setAutoUpdate(boolean autoUpdate)
Enable or disable update propagation when joiningvoid
setExplicitReplicaList(java.util.List<java.lang.String> explicitReplicaList)
void
setLeaderUrl(java.lang.String leaderUrl)
void
setMaxOpPerUpdate(long maxOpPerUpdate)
Set the maximum count of operations sent per updatevoid
setPartialUpdateInterval(int partialUpdateInterval)
void
setRequestTimeout(int requestTimeout)
void
setSyncFiles(boolean syncFiles)
Enable or disable file replicationvoid
setUpdateRequestTimeout(int updateRequestTimeout)
void
setUrl(java.lang.String url)
Set the url of this replicaint
suggestJoin(java.lang.String urlReplica)
Suggest one replica to join this replica as leader.java.lang.String
toString()
int
update()
Sends updates to the leader (if any) and to the group (if any)int
updateGroup()
Sends the update to each replica of the groupvoid
updateGroupAsync()
int
updateLeader()
Sends the updates to the leader replica.
-
-
-
Method Detail
-
getInstance
public static JSyncReplica getInstance(JSyncSettings settings)
Gets a replica- Parameters:
settings
- the settings- Returns:
- a replica
- Since:
- jcms-10.0.8
-
getUrid
public java.lang.String getUrid()
- Returns:
- the urid of this replica
- Since:
- jcms-4.1
- See Also:
Store.getUrid()
-
getUrl
public java.lang.String getUrl()
- Returns:
- the url of this replica
- Since:
- jcms-4.1
-
setUrl
public void setUrl(java.lang.String url)
Set the url of this replica- Parameters:
url
- the url of this replica- Since:
- jcms-4.1
-
getSyncFiles
public boolean getSyncFiles()
Return true if file replication is enabled- Returns:
- true if file replication is enabled
- Since:
- jcms-4.1
-
setSyncFiles
public void setSyncFiles(boolean syncFiles)
Enable or disable file replication- Parameters:
syncFiles
- true if files must be replicated- Since:
- jcms-4.1
-
setAutoUpdate
public void setAutoUpdate(boolean autoUpdate)
Enable or disable update propagation when joining- Parameters:
autoUpdate
- true if updates must be propagated at join- Since:
- jcms-5.0
-
setMaxOpPerUpdate
public void setMaxOpPerUpdate(long maxOpPerUpdate)
Set the maximum count of operations sent per update- Parameters:
maxOpPerUpdate
- the maximum count of operations sent by update- Since:
- jcms-4.1
-
getMaxOpPerUpdate
public long getMaxOpPerUpdate()
- Returns:
- return the maximum count of operations sent per update
- Since:
- jcms-4.1
-
setRequestTimeout
public void setRequestTimeout(int requestTimeout)
-
getRequestTimeout
public int getRequestTimeout()
-
setUpdateRequestTimeout
public void setUpdateRequestTimeout(int updateRequestTimeout)
-
getUpdateRequestTimeout
public int getUpdateRequestTimeout()
-
setPartialUpdateInterval
public void setPartialUpdateInterval(int partialUpdateInterval)
-
getPartialUpdateInterval
public int getPartialUpdateInterval()
-
isJoined
public boolean isJoined()
- Returns:
- true if this replica has joined its leader
- Since:
- jcms-4.1
-
getLeader
public ReplicaInfo getLeader()
- Returns:
- the leader this replica has joined
- Since:
- jcms-4.1
-
getProgressTable
public StampTable getProgressTable()
- Returns:
- the progress table of this replica
- Since:
- jcms-4.1
- See Also:
Store.getProgressTable()
-
getProgressStamp
public Stamp getProgressStamp()
- Returns:
- the greatest stamp of this replica
- Since:
- jcms-4.1
- See Also:
Store.getProgressStamp()
-
isLeader
public boolean isLeader()
- Returns:
- true if this replica is a leader (i.e. if other replica has joined it)
- Since:
- jcms-4.1
-
isSuperLeader
public boolean isSuperLeader()
- Returns:
- true if this replica is the super leader. (i.e. if other replica has joined it, and it has not joined any other leader)
- Since:
- jcms-5.0.1
-
isSubLeader
public boolean isSubLeader()
- Returns:
- true if this replica is a sub leader. (i.e. if other replica has joined it, and it has joined another leader)
- Since:
- jcms-5.0.1
-
isDisconnected
public boolean isDisconnected()
Returns true if this replica is disconnected from its leader.- Returns:
- true if this replica is disconnected from its leader.
- Since:
- jcms-10.0.1
-
isKnownReplica
public boolean isKnownReplica(java.lang.String urid)
Returns true if this replica is known (i.e. is my leader or a replica of my group)- Parameters:
urid
- the urid to check- Returns:
- true if this replica is known (i.e. is my leader or a replica of my group)
- Since:
- jcms-5.0.4
-
removeReplica
public void removeReplica(java.lang.String urid)
Removes the given replica of the group of this leader- Parameters:
urid
- the urid of the replica to be removed- Since:
- jcms-4.1
-
getReplicaMap
public java.util.Map<java.lang.String,ReplicaInfo> getReplicaMap()
- Returns:
- map of replica which has joined this leader
- Since:
- jcms-4.1
-
needForUpdate
public boolean needForUpdate()
- Returns:
- true if this replica is in advance on its leader
- Since:
- jcms-4.1
-
getLogger
public JSyncLogger getLogger()
- Returns:
- the JSyncLogger
- Since:
- jcms-4.1
-
getStatusLabel
public java.lang.String getStatusLabel(int sc, java.util.Locale locale)
- Parameters:
sc
- the status code (must be a new status code value (e.g. 1101, 1200, ...)locale
- the locale- Returns:
- the label for the given status code
- Since:
- jcms-4.1
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
- Returns:
- the URID of the replica
- Since:
- jcms-4.1
-
getStatus
public java.lang.String getStatus()
Return the status of this replica (STATUS_LONE, STATUS_MEMBER, STATUS_SUBLEADER or STATUS_LEADER)- Returns:
- the status of this replica
- Since:
- jcms-5.0.0
-
join
public int join()
Joins a replica. A JoinMessage is sent to the given leaderUrl. If this replica acknowledges, it becomes my leader.- Returns:
- the status code of the ack message
- Since:
- jcms-4.1
-
globalSuggestJoin
public int globalSuggestJoin()
Suggest all replica to join this replica as leader. A SuggestJoinMessage is sent to each replica.- Returns:
- the count of replicas that have failed.
- Since:
- jcms-10.0.8
-
getNotConnectedReplicaSet
public java.util.Set<java.lang.String> getNotConnectedReplicaSet()
- Returns:
- the set of known but not connected replicas.
- Since:
- jcms-10.0.8
-
suggestJoin
public int suggestJoin(java.lang.String urlReplica)
Suggest one replica to join this replica as leader. A SuggestJoinMessage is sent to this replica.- Parameters:
urlReplica
- url of one joined replica- Returns:
- the status code of the ack message
- Since:
- jcms-5.7.4
-
disjoin
public int disjoin()
Disjoins from the leader replica.- Returns:
- the status code of the ack message
- Since:
- jcms-4.1
-
update
public int update()
Sends updates to the leader (if any) and to the group (if any)- Returns:
- SC_OK (200) if the both update have succeeded. Otherwise returns the SC of the first update which failed.
- Since:
- jcms-4.1
- See Also:
updateLeader()
,updateGroup()
-
updateLeader
public int updateLeader()
Sends the updates to the leader replica.- Returns:
- the status code of the ack message
- Since:
- jcms-4.1
-
updateGroupAsync
public void updateGroupAsync()
-
updateGroup
public int updateGroup()
Sends the update to each replica of the group- Returns:
- the status code of the ack message
- Since:
- jcms-4.1
-
processMessage
public boolean processMessage(java.io.OutputStream out, Message msg) throws java.io.IOException
Dispatches on the right method to process the given message- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-4.1
-
processJoinMessage
public boolean processJoinMessage(java.io.OutputStream out, JoinMessage msg) throws java.io.IOException
Process a JoinMessage- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-4.1
-
processSuggestJoinMessage
public boolean processSuggestJoinMessage(java.io.OutputStream out, SuggestJoinMessage msg) throws java.io.IOException
Process a SuggestJoinMessage- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-5.7.4
-
processDisjoinMessage
public boolean processDisjoinMessage(java.io.OutputStream out, DisjoinMessage msg) throws java.io.IOException
Process a DisjoinMessage- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-4.1
-
processCommitWorkMessage
public boolean processCommitWorkMessage(java.io.OutputStream out, CommitWorkMessage msg) throws java.io.IOException
Process a CommitWorkMessage- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-10.0.8
-
processUpdateMessage
public boolean processUpdateMessage(java.io.OutputStream out, UpdateMessage msg) throws java.io.IOException
Process an UpdateMessage- Parameters:
out
- the output stream to replymsg
- the message to process- Returns:
- true if the message has been correctly processed
- Throws:
java.io.IOException
- if an error occurs- Since:
- jcms-4.1
-
addListener
public void addListener(JSyncListener listener)
Adds a JSync listener- Parameters:
listener
- the JSync listener to be added- Since:
- jcms-4.1
-
removeListener
public void removeListener(JSyncListener listener)
Removes a JSync listener- Parameters:
listener
- the JSync listener to be removed- Since:
- jcms-4.1
-
close
public void close()
Close all JSyncReplica resources when ending.
-
getLeaderUrl
public java.lang.String getLeaderUrl()
-
setLeaderUrl
public void setLeaderUrl(java.lang.String leaderUrl)
-
setExplicitReplicaList
public void setExplicitReplicaList(java.util.List<java.lang.String> explicitReplicaList)
-
isCanBeJoined
public boolean isCanBeJoined()
-
commitWork
public CommitWorkResult commitWork()
-
commitWork
public CommitWorkResult commitWork(CommitWorkObserver obs)
-
canCommitWork
public CommitWorkResult canCommitWork()
-
retryUpdateLeader
public void retryUpdateLeader()
-
-