Class AeronCluster

java.lang.Object
io.aeron.cluster.client.AeronCluster
All Implemented Interfaces:
AutoCloseable

public final class AeronCluster extends Object implements AutoCloseable
Client for interacting with an Aeron Cluster.

A client will attempt to open a session and then offer ingress messages which are replicated to clustered services for reliability. If the clustered service responds then response messages and events are sent via the egress stream.

Note: Instances of this class are not threadsafe.

  • Field Details

    • SESSION_HEADER_LENGTH

      public static final int SESSION_HEADER_LENGTH
      Length of a session message header for cluster ingress or egress.
      See Also:
  • Method Details

    • connect

      public static AeronCluster connect()
      Connect to the cluster using default configuration.
      Returns:
      allocated cluster client if the connection is successful.
    • connect

      public static AeronCluster connect(AeronCluster.Context ctx)
      Connect to the cluster providing AeronCluster.Context for configuration.
      Parameters:
      ctx - for configuration.
      Returns:
      allocated cluster client if the connection is successful.
    • asyncConnect

      public static AeronCluster.AsyncConnect asyncConnect()
      Begin an attempt at creating a connection which can be completed by calling AeronCluster.AsyncConnect.poll() until it returns the client, before complete it will return null.
      Returns:
      the AeronCluster.AsyncConnect that can be polled for completion.
    • asyncConnect

      public static AeronCluster.AsyncConnect asyncConnect(AeronCluster.Context ctx)
      Begin an attempt at creating a connection which can be completed by calling AeronCluster.AsyncConnect.poll() until it returns the client, before complete it will return null.
      Parameters:
      ctx - for the cluster.
      Returns:
      the AeronCluster.AsyncConnect that can be polled for completion.
    • egressListenerExtension

      public void egressListenerExtension(EgressListenerExtension listenerExtension)
      An EgressListener for extension schemas.
      Parameters:
      listenerExtension - listener extension.
    • controlledEgressListenerExtension

      public void controlledEgressListenerExtension(ControlledEgressListenerExtension listenerExtension)
      A ControlledEgressListener for extension schemas.
      Parameters:
      listenerExtension - listener extension.
    • close

      public void close()
      Close session and release associated resources.
      Specified by:
      close in interface AutoCloseable
    • isClosed

      public boolean isClosed()
      Is the client closed? The client can be closed by calling close(), the cluster sending an event, or the client permanently losing a cluster connection.
      Returns:
      true if closed otherwise false.
    • context

      public AeronCluster.Context context()
      Get the context used to launch this cluster client.
      Returns:
      the context used to launch this cluster client.
    • clusterSessionId

      public long clusterSessionId()
      Cluster session id for the session that was opened as the result of a successful connect.
      Returns:
      session id for the session that was opened as the result of a successful connect.
    • leadershipTermId

      public long leadershipTermId()
      Leadership term identity for the cluster. Advances with changing leadership.
      Returns:
      leadership term identity for the cluster.
    • leaderMemberId

      public int leaderMemberId()
      Get the current leader member id for the cluster.
      Returns:
      the current leader member id for the cluster.
    • ingressPublication

      public Publication ingressPublication()
      Get the raw Publication for sending to the cluster.

      This can be wrapped with a IngressSessionDecorator for pre-pending the cluster session header to messages. SessionMessageHeaderEncoder should be used for raw access.

      Results of offering to this publication should be passed to trackIngressPublicationResult(long).

      Returns:
      the raw Publication for connecting to the cluster.
    • egressSubscription

      public Subscription egressSubscription()
      Get the raw Subscription for receiving from the cluster.

      This can be wrapped with a EgressAdapter for dispatching events from the cluster. SessionMessageHeaderDecoder should be used for raw access.

      Returns:
      the raw Subscription for receiving from the cluster.
    • tryClaim

      public 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. Once the message has been written then BufferClaim.commit() should be called thus making it available.

      On successful claim, the Cluster ingress header will be written to the start of the claimed buffer section. Clients MUST write into the claimed buffer region at offset + SESSION_HEADER_LENGTH.

      
           final DirectBuffer srcBuffer = acquireMessage();
      
           if (aeronCluster.tryClaim(length, bufferClaim) > 0L)
           {
               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();
               }
           }
       
      Parameters:
      length - of the range to claim in bytes. The additional bytes for the session header will be added.
      bufferClaim - to be populated if the claim succeeds.
      Returns:
      The new stream position, otherwise a negative error value as specified in Publication.tryClaim(int, BufferClaim).
      Throws:
      IllegalArgumentException - if the length is greater than Publication.maxPayloadLength().
      See Also:
    • offer

      public long offer(DirectBuffer buffer, int offset, int length)
      Non-blocking publish of a partial buffer containing a message plus session header to a cluster.

      This version of the method will set the timestamp value in the header to zero.

      Parameters:
      buffer - containing message.
      offset - offset in the buffer at which the encoded message begins.
      length - in bytes of the encoded message.
      Returns:
      the same as Publication.offer(DirectBuffer, int, int).
    • offer

      public long offer(DirectBufferVector[] vectors)
      Non-blocking publish by gathering buffer vectors into a message. The first vector will be replaced by the cluster session message header so must be left unused.
      Parameters:
      vectors - which make up the message.
      Returns:
      the same as Publication.offer(DirectBufferVector[]).
      See Also:
    • sendKeepAlive

      public boolean sendKeepAlive()
      Send a keep alive message to the cluster to keep this session open.

      Note: Sending keep-alive can fail during a leadership transition. The application should continue to call pollEgress() to ensure a connection to the new leader is established.

      Returns:
      true if successfully sent otherwise false if back pressured.
    • sendAdminRequestToTakeASnapshot

      public boolean sendAdminRequestToTakeASnapshot(long correlationId)
      Sends an admin request to initiate a snapshot action in the cluster. This request requires elevated privileges.
      Parameters:
      correlationId - for the request.
      Returns:
      true if the request was sent or false otherwise.
      See Also:
    • pollEgress

      public int pollEgress()
      Poll the egressSubscription() for session messages which are dispatched to AeronCluster.Context.egressListener(). Invoking this method, or controlledPollEgress(), frequently is important for detecting leadership changes in a cluster.

      Note: if AeronCluster.Context.egressListener() is not set then a ConfigurationException could result.

      Returns:
      0 if no work was available, or a positive number if work has been done, typically the number of fragments processed.
      See Also:
    • controlledPollEgress

      public int controlledPollEgress()
      Poll the egressSubscription() for session messages which are dispatched to AeronCluster.Context.controlledEgressListener(). Invoking this method, or pollEgress(), frequently is important for detecting leadership changes in a cluster.

      Note: if AeronCluster.Context.controlledEgressListener() is not set then a ConfigurationException could result.

      Returns:
      0 if no work was available, a positive number if work has been done, typically the number of fragments processed.
      See Also:
    • pollStateChanges

      public int pollStateChanges()
      Polls for client state changes. Needs to be called explicitly only by applications which use egressSubscription() directly instead of calling pollEgress() or controlledPollEgress().
      Returns:
      0 if state has not changed, a positive number otherwise.
    • onNewLeader

      public void onNewLeader(long clusterSessionId, long leadershipTermId, int leaderMemberId, String ingressEndpoints)
      To be called when a new leader event is delivered. This method needs to be called when using the EgressAdapter or EgressPoller rather than pollEgress() method.
      Parameters:
      clusterSessionId - which must match clusterSessionId().
      leadershipTermId - that identifies the term for which the new leader has been elected.
      leaderMemberId - which has become the new leader.
      ingressEndpoints - comma separated list of cluster ingress endpoints to connect to with the leader first.
    • trackIngressPublicationResult

      public void trackIngressPublicationResult(long result)
      Updates the state of this client based on ingress publication result. Should be called with every offer and tryClaim result when ingressPublication() is used directly. Methods of this class which send ingress messages call it automatically.
      Parameters:
      result - the result returned by the ingress publication.