Interface ClusteredService
The cluster object should only be used to send messages to the cluster or schedule timers in
response to other messages and timers. Sending messages and timers should not happen from cluster lifecycle
methods like onStart(Cluster, Image), onRoleChange(Cluster.Role) or
onTakeSnapshot(ExclusivePublication), or onTerminate(Cluster), except the session lifecycle
methods.
-
Method Summary
Modifier and TypeMethodDescriptiondefault intdoBackgroundWork(long nowNs) Implement this method to perform background tasks that are not related to the deterministic state machine model, such as keeping external connections alive to the cluster.default voidonNewLeadershipTermEvent(long leadershipTermId, long logPosition, long timestamp, long termBaseLogPosition, int leaderMemberId, int logSessionId, TimeUnit timeUnit, int appVersion) An election has been successful and a leader has entered a new term.voidonRoleChange(Cluster.Role newRole) Notify that the cluster node has changed role.voidonSessionClose(ClientSession session, long timestamp, io.aeron.cluster.codecs.CloseReason closeReason) A session has been closed for a client to the cluster.voidonSessionMessage(ClientSession session, long timestamp, DirectBuffer buffer, int offset, int length, Header header) A message has been received to be processed by a clustered service.voidonSessionOpen(ClientSession session, long timestamp) A session has been opened for a client to the cluster.voidStart event where the service can perform any initialisation required and load snapshot state.voidonTakeSnapshot(ExclusivePublication snapshotPublication) The service should take a snapshot and store its state to the provided archiveExclusivePublication.voidonTerminate(Cluster cluster) Called when the container is going to terminate but only after a successful start.voidonTimerEvent(long correlationId, long timestamp) A scheduled timer has expired.
-
Method Details
-
onStart
Start event where the service can perform any initialisation required and load snapshot state. The snapshot image can be null if no previous snapshot exists.Note: As this is a potentially long-running operation the implementation should use
Cluster.idleStrategy()and then occasionally callIdleStrategy.idle()orIdleStrategy.idle(int), especially when polling theImagereturns 0.- Parameters:
cluster- with which the service can interact.snapshotImage- from which the service can load its archived state which can be null when no snapshot.
-
onSessionOpen
A session has been opened for a client to the cluster.- Parameters:
session- for the client which have been opened.timestamp- at which the session was opened.
-
onSessionClose
void onSessionClose(ClientSession session, long timestamp, io.aeron.cluster.codecs.CloseReason closeReason) A session has been closed for a client to the cluster.- Parameters:
session- that has been closed.timestamp- at which the session was closed.closeReason- the session was closed.
-
onSessionMessage
void onSessionMessage(ClientSession session, long timestamp, DirectBuffer buffer, int offset, int length, Header header) A message has been received to be processed by a clustered service.- Parameters:
session- for the client which sent the message. This can be null if the client was a service.timestamp- for when the message was received.buffer- containing the message.offset- in the buffer at which the message is encoded.length- of the encoded message.header- aeron header for the incoming message.
-
onTimerEvent
void onTimerEvent(long correlationId, long timestamp) A scheduled timer has expired.- Parameters:
correlationId- for the expired timer.timestamp- at which the timer expired.
-
onTakeSnapshot
The service should take a snapshot and store its state to the provided archiveExclusivePublication.Note: As this is a potentially long-running operation the implementation should use
Cluster.idleStrategy()and then occasionally callIdleStrategy.idle()orIdleStrategy.idle(int), especially when the snapshotExclusivePublicationreturnsPublication.BACK_PRESSURED.- Parameters:
snapshotPublication- to which the state should be recorded.
-
onRoleChange
Notify that the cluster node has changed role.- Parameters:
newRole- that the node has assumed.
-
onTerminate
Called when the container is going to terminate but only after a successful start.- Parameters:
cluster- with which the service can interact.
-
onNewLeadershipTermEvent
default void onNewLeadershipTermEvent(long leadershipTermId, long logPosition, long timestamp, long termBaseLogPosition, int leaderMemberId, int logSessionId, TimeUnit timeUnit, int appVersion) An election has been successful and a leader has entered a new term.- Parameters:
leadershipTermId- identity for the new leadership term.logPosition- position the log has reached as the result of this message.timestamp- for the new leadership term.termBaseLogPosition- position at the beginning of the leadership term.leaderMemberId- who won the election.logSessionId- session id for the publication of the log.timeUnit- for the timestamps in the coming leadership term.appVersion- for the application configured in the consensus module.
-
doBackgroundWork
default int doBackgroundWork(long nowNs) Implement this method to perform background tasks that are not related to the deterministic state machine model, such as keeping external connections alive to the cluster. This method must not be used to directly, or indirectly, update the service state. This method cannot be used for making calls onClusterwhich could update the log such asCluster.scheduleTimer(long, long)orCluster.offer(DirectBuffer, int, int).This method is not for long-running operations. Time taken can impact latency and should only be used for short constant time operations.
- Parameters:
nowNs- which can be used for measuring elapsed time and be used in the same way asSystem.nanoTime(). This is notCluster.time().- Returns:
- 0 if no work is done otherwise a positive number.
- Since:
- 1.40.0
-