Interface Cluster
ClusteredService to interact with cluster hosting it.
This 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
ClusteredService.onStart(Cluster, Image), ClusteredService.onRoleChange(Cluster.Role) or
ClusteredService.onTakeSnapshot(ExclusivePublication), or ClusteredService.onTerminate(Cluster),
except the session lifecycle methods ClusteredService.onSessionOpen(ClientSession, long),
ClusteredService.onSessionClose(ClientSession, long, CloseReason),
and ClusteredService.onNewLeadershipTermEvent(long, long, long, long, int, int, TimeUnit, int).
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionaeron()Get theAeronclient used by the cluster.booleancancelTimer(long correlationId) Cancel a previously scheduled timer.Get the current collection of cluster client sessions.booleancloseClientSession(long clusterSessionId) Request the close of aClientSessionby sending the request to the consensus module.context()Get theClusteredServiceContainer.Contextunder which the container is running.voidforEachClientSession(Consumer<? super ClientSession> action) For each iterator overClientSessions using the most efficient method possible.getClientSession(long clusterSessionId) Get theClientSessionfor a given cluster session id.IdleStrategywhich should be used by the service when it experiences back-pressure on egress, closing sessions, making timer requests, or any long-running actions.longPosition the log has reached in bytes as of the current message.intmemberId()The unique id for the hosting member of the cluster.longoffer(DirectBufferVector[] vectors) Offer a message as ingress to the cluster for sequencing.longoffer(DirectBuffer buffer, int offset, int length) Offer a message as ingress to the cluster for sequencing.role()The role the cluster node is playing.booleanscheduleTimer(long correlationId, long deadline) Schedule a timer for a given deadline and provide a correlation id to identify the timer when it expires or for cancellation.longtime()Cluster time astimeUnit()s since 1 Jan 1970 UTC.timeUnit()The unit of time applied when timestamping andtime()operations.longtryClaim(int length, BufferClaim bufferClaim) Try to claim a range in the publication log into which a message can be written with zero copy semantics.
-
Method Details
-
memberId
int memberId()The unique id for the hosting member of the cluster. Useful only for debugging purposes.- Returns:
- unique id for the hosting member of the cluster.
-
role
Cluster.Role role()The role the cluster node is playing.- Returns:
- the role the cluster node is playing.
-
logPosition
long logPosition()Position the log has reached in bytes as of the current message.- Returns:
- position the log has reached in bytes as of the current message.
-
aeron
Aeron aeron()Get theAeronclient used by the cluster.- Returns:
- the
Aeronclient used by the cluster.
-
context
ClusteredServiceContainer.Context context()Get theClusteredServiceContainer.Contextunder which the container is running.- Returns:
- the
ClusteredServiceContainer.Contextunder which the container is running.
-
getClientSession
Get theClientSessionfor a given cluster session id.- Parameters:
clusterSessionId- to be looked up.- Returns:
- the
ClientSessionthat matches the clusterSessionId.
-
clientSessions
Collection<ClientSession> clientSessions()Get the current collection of cluster client sessions.The
Iteratoron this class does not support nested iteration. It reuses the iterator to avoid allocation.- Returns:
- the current collection of cluster client sessions.
-
forEachClientSession
For each iterator overClientSessions using the most efficient method possible.- Parameters:
action- to be taken for eachClientSessionin turn.
-
closeClientSession
boolean closeClientSession(long clusterSessionId) Request the close of aClientSessionby sending the request to the consensus module.- Parameters:
clusterSessionId- to be closed.- Returns:
- true if the event to close a session was sent or false if back pressure was applied.
- Throws:
ClusterException- if the clusterSessionId is not recognised.
-
time
long time()Cluster time astimeUnit()s since 1 Jan 1970 UTC.- Returns:
- time as
timeUnit()s since 1 Jan 1970 UTC.
-
timeUnit
TimeUnit timeUnit()The unit of time applied when timestamping andtime()operations.- Returns:
- the unit of time applied when timestamping and
time()operations.
-
scheduleTimer
boolean scheduleTimer(long correlationId, long deadline) Schedule a timer for a given deadline and provide a correlation id to identify the timer when it expires or for cancellation. This action is asynchronous, when rescheduling it will race with the timer expiring.If the correlationId is for an existing scheduled timer then it will be rescheduled to the new deadline. However, it is best to generate correlationIds in a monotonic fashion and be aware of potential clashes with other services in the same cluster. Service isolation can be achieved by using the upper bits for service id.
Timers should only be scheduled or cancelled in the context of processing a
ClusteredService.onSessionMessage(ClientSession, long, DirectBuffer, int, int, Header),ClusteredService.onTimerEvent(long, long),ClusteredService.onSessionOpen(ClientSession, long), orClusteredService.onSessionClose(ClientSession, long, CloseReason). If applied to other events then they are not guaranteed to be reliable.Callers of this method must loop until the method succeeds.
private Cluster cluster; // Lines omitted... cluster.idleStrategy().reset(); while (!cluster.scheduleTimer(correlationId, deadline)) { cluster.idleStrategy().idle(); }The cluster's idle strategy must be used in the body of the loop to allow for the clustered service to be shutdown if required.
- Parameters:
correlationId- to identify the timer when it expires.Long.MAX_VALUEnot supported.deadline- time after which the timer will fire.Long.MAX_VALUEnot supported.- Returns:
trueif the request to schedule a timer has been sent orfalsein case ofPublication.ADMIN_ACTIONorPublication.BACK_PRESSURED.- Throws:
ClusterException- if request fails with an error.- See Also:
-
cancelTimer
boolean cancelTimer(long correlationId) Cancel a previously scheduled timer. This action is asynchronous and will race with the timer expiring.Timers should only be scheduled or cancelled in the context of processing a
ClusteredService.onSessionMessage(ClientSession, long, DirectBuffer, int, int, Header),ClusteredService.onTimerEvent(long, long),ClusteredService.onSessionOpen(ClientSession, long), orClusteredService.onSessionClose(ClientSession, long, CloseReason). If applied to other events then they are not guaranteed to be reliable.Callers of this method must loop until the method succeeds.
private Cluster cluster; // Lines omitted... cluster.idleStrategy().reset(); while (!cluster.cancelTimer(correlationId)) { cluster.idleStrategy().idle(); }- Parameters:
correlationId- for the timer provided when it was scheduled.Long.MAX_VALUEnot supported.- Returns:
trueif the request to cancel a timer has been sent orfalsein case ofPublication.ADMIN_ACTIONorPublication.BACK_PRESSURED.- Throws:
ClusterException- if request fails with an error.- See Also:
-
offer
Offer a message as ingress to the cluster for sequencing. This will happen efficiently over IPC to the consensus module and have the cluster session of as theClusteredServiceContainer.Configuration.SERVICE_ID_PROP_NAME.Callers of this method must loop until the method succeeds.
private Cluster cluster; // Lines omitted... cluster.idleStrategy().reset(); while(true) { final long position = cluster.offer(buffer, offset, length); if (position > 0) { break; } else if (Publication.ADMIN_ACTION != position && Publication.BACK_PRESSURED != position) { throw new ClusterException("Internal offer failed: " + position); } cluster.idleStrategy.idle(); }The cluster's idle strategy must be used in the body of the loop to allow for the clustered service to be shutdown if required.
- Parameters:
buffer- containing the message to be offered.offset- in the buffer at which the encoded message begins.length- in the buffer of the encoded message.- Returns:
- positive value if successful.
- See Also:
-
offer
Offer a message as ingress to the cluster for sequencing. This will happen efficiently over IPC to the consensus module and have the cluster session of as theClusteredServiceContainer.Configuration.SERVICE_ID_PROP_NAME.The first vector must be left free to be filled in for the session message header.
Callers of this method should loop until the method succeeds, see
offer(DirectBuffer, int, int)for an example.- Parameters:
vectors- containing the message parts with the first left to be filled.- Returns:
- positive value if successful.
- See Also:
-
tryClaim
Try to claim a range in the publication log into which a message can be written with zero copy semantics. Once the message has been written thenBufferClaim.commit()should be called thus making it available.On successful claim, the Cluster session header will be written to the start of the claimed buffer section. Clients MUST write into the claimed buffer region at offset +
AeronCluster.SESSION_HEADER_LENGTH.Callers of this method must loop until the method succeeds.
private final BufferClaim bufferClaim = new BufferClaim(); private Cluster cluster; // Lines omitted... final DirectBuffer srcBuffer = acquireMessage(); cluster.idleStrategy().reset(); while(true) { final long position = cluster.tryClaim(length, bufferClaim); if (position > 0) { final MutableDirectBuffer buffer = bufferClaim.buffer(); final int offset = bufferClaim.offset(); // ensure that data is written after the session header buffer.putBytes(offset + AeronCluster.SESSION_HEADER_LENGTH, srcBuffer, 0, length); bufferClaim.commit(); break; } else if (Publication.ADMIN_ACTION != position && Publication.BACK_PRESSURED != position) { throw new ClusterException("Internal tryClaim failed: " + position); } cluster.idleStrategy.idle(); }- Parameters:
length- of the range to claim, in bytes.bufferClaim- to be populated if the claim succeeds.- Returns:
- positive value if successful.
- Throws:
IllegalArgumentException- if the length is greater thanPublication.maxPayloadLength().- See Also:
-
idleStrategy
IdleStrategy idleStrategy()IdleStrategywhich should be used by the service when it experiences back-pressure on egress, closing sessions, making timer requests, or any long-running actions.- Returns:
- the
IdleStrategywhich should be used by the service when it experiences back-pressure.
-