public 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)
.
Modifier and Type | Interface and Description |
---|---|
static class |
Cluster.Role
Role of the node in the cluster.
|
Modifier and Type | Method and Description |
---|---|
Aeron |
aeron()
Get the
Aeron client used by the cluster. |
boolean |
cancelTimer(long correlationId)
Cancel a previously scheduled timer.
|
Collection<ClientSession> |
clientSessions()
Get the current collection of cluster client sessions.
|
boolean |
closeClientSession(long clusterSessionId)
Request the close of a
ClientSession by sending the request to the consensus module. |
ClusteredServiceContainer.Context |
context()
Get the
ClusteredServiceContainer.Context under which the container is running. |
void |
forEachClientSession(Consumer<? super ClientSession> action)
For each iterator over
ClientSession s using the most efficient method possible. |
ClientSession |
getClientSession(long clusterSessionId)
Get the
ClientSession for a given cluster session id. |
IdleStrategy |
idleStrategy()
IdleStrategy which should be used by the service when it experiences back-pressure on egress,
closing sessions, making timer requests, or any long-running actions. |
long |
logPosition()
Position the log has reached in bytes as of the current message.
|
int |
memberId()
The unique id for the hosting member of the cluster.
|
long |
offer(DirectBuffer buffer,
int offset,
int length)
Offer a message as ingress to the cluster for sequencing.
|
long |
offer(DirectBufferVector[] vectors)
Offer a message as ingress to the cluster for sequencing.
|
Cluster.Role |
role()
The role the cluster node is playing.
|
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.
|
long |
time()
Cluster time as
timeUnit() s since 1 Jan 1970 UTC. |
TimeUnit |
timeUnit()
The unit of time applied when timestamping and
time() operations. |
long |
tryClaim(int length,
BufferClaim bufferClaim)
Try to claim a range in the publication log into which a message can be written with zero copy semantics.
|
int memberId()
Cluster.Role role()
long logPosition()
Aeron aeron()
Aeron
client used by the cluster.Aeron
client used by the cluster.ClusteredServiceContainer.Context context()
ClusteredServiceContainer.Context
under which the container is running.ClusteredServiceContainer.Context
under which the container is running.ClientSession getClientSession(long clusterSessionId)
ClientSession
for a given cluster session id.clusterSessionId
- to be looked up.ClientSession
that matches the clusterSessionId.Collection<ClientSession> clientSessions()
The Iterator
on this class does not support nested iteration. It reuses the iterator to
avoid allocation.
void forEachClientSession(Consumer<? super ClientSession> action)
ClientSession
s using the most efficient method possible.action
- to be taken for each ClientSession
in turn.boolean closeClientSession(long clusterSessionId)
ClientSession
by sending the request to the consensus module.clusterSessionId
- to be closed.ClusterException
- if the clusterSessionId is not recognised.long time()
timeUnit()
s since 1 Jan 1970 UTC.timeUnit()
s since 1 Jan 1970 UTC.TimeUnit timeUnit()
time()
operations.time()
operations.boolean scheduleTimer(long correlationId, long deadline)
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)
, or
ClusteredService.onSessionClose(ClientSession, long, CloseReason)
.
If applied to other events then they are not guaranteed to be reliable.
Callers of this method should 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.correlationId
- to identify the timer when it expires. Long.MAX_VALUE
not supported.deadline
- time after which the timer will fire. Long.MAX_VALUE
not supported.cancelTimer(long)
boolean cancelTimer(long correlationId)
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)
, or
ClusteredService.onSessionClose(ClientSession, long, CloseReason)
.
If applied to other events then they are not guaranteed to be reliable.
Callers of this method should loop until the method succeeds, see scheduleTimer(long, long)
for an example.
correlationId
- for the timer provided when it was scheduled. Long.MAX_VALUE
not supported.scheduleTimer(long, long)
long offer(DirectBuffer buffer, int offset, int length)
ClusteredServiceContainer.Configuration.SERVICE_ID_PROP_NAME
.
Callers of this method should loop until the method succeeds.
private Cluster cluster;
// Lines omitted...
cluster.idleStrategy().reset();
do
{
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();
}
while (true);
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.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.Publication.offer(DirectBuffer, int, int)
long offer(DirectBufferVector[] vectors)
ClusteredServiceContainer.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.
vectors
- containing the message parts with the first left to be filled.Publication.offer(DirectBufferVector[])
long tryClaim(int length, BufferClaim bufferClaim)
BufferClaim.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
.
final DirectBuffer srcBuffer = acquireMessage();
if (cluster.tryClaim(length, bufferClaim))
{
try
{
final MutableDirectBuffer buffer = bufferClaim.buffer();
final int offset = bufferClaim.offset();
// ensure that data is written at the correct offset
buffer.putBytes(offset + AeronCluster.SESSION_HEADER_LENGTH, srcBuffer, 0, length);
}
finally
{
bufferClaim.commit();
}
}
Callers of this method should loop until the method succeeds, see
offer(DirectBuffer, int, int)
for an example.
length
- of the range to claim, in bytes.bufferClaim
- to be populated if the claim succeeds.IllegalArgumentException
- if the length is greater than Publication.maxPayloadLength()
.Publication.tryClaim(int, BufferClaim)
,
BufferClaim.commit()
,
BufferClaim.abort()
IdleStrategy idleStrategy()
IdleStrategy
which should be used by the service when it experiences back-pressure on egress,
closing sessions, making timer requests, or any long-running actions.IdleStrategy
which should be used by the service when it experiences back-pressure.Copyright © 2014-2022 Real Logic Limited. All Rights Reserved.