Class ElectionCandidateImpl

java.lang.Object
io.deephaven.enterprise.etcdclient.election.ElectionCandidateImpl
All Implemented Interfaces:
ElectionCandidate

public class ElectionCandidateImpl extends Object implements ElectionCandidate
Implementation of object to access etcd v3 Election API.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface io.deephaven.enterprise.etcdclient.election.ElectionCandidate

    ElectionCandidate.Null
  • Field Summary

    Fields inherited from interface io.deephaven.enterprise.etcdclient.election.ElectionCandidate

    NULL
  • Constructor Summary

    Constructors
    Constructor
    Description
    ElectionCandidateImpl(com.fishlib.io.logger.Logger log, io.deephaven.shadow.jetcd.io.etcd.jetcd.Client etcdClient, int leaseTtlSeconds, int leaseRenewSeconds, int leaseRenewRetryDelaySeconds, int leaseKeepaliveTimeoutMillis, byte[] uuid, DhService service, String idStringExtra, String electionKey, long rpcTimeoutMillis, long observeTimeoutMillis, Runnable lostLeadershipCb)
    Campaign for an election; will block until leadership is acquired.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close this object.
    io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequence
    Get the leadership opaque key associated to us having leadership, or null if we are not the leader.
    boolean
     
    boolean
    register(DhService service, int port)
    Register our address in DH Resolver.
    void
    Resign from this election and remove any DH Resolver registration.
    void
    setLostLeadershipCb(Runnable newLostLeadershipCb)
    Set a new lost leadership callback, replacing the previously assigned one during construction.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ElectionCandidateImpl

      public ElectionCandidateImpl(com.fishlib.io.logger.Logger log, io.deephaven.shadow.jetcd.io.etcd.jetcd.Client etcdClient, int leaseTtlSeconds, int leaseRenewSeconds, int leaseRenewRetryDelaySeconds, int leaseKeepaliveTimeoutMillis, byte[] uuid, DhService service, String idStringExtra, String electionKey, long rpcTimeoutMillis, long observeTimeoutMillis, Runnable lostLeadershipCb) throws ExecutionException, InterruptedException, TimeoutException
      Campaign for an election; will block until leadership is acquired. See the etcd documentation for the election API
      Parameters:
      log - The logger to use in case of errors
      etcdClient - The etcd client to use. Note we depend on the ability to observe and detect losing mastership, which means we can't risk a stuck channel connection rendering a streaming RPC for observe silent: this client should have been created with keepalives enabled.
      leaseTtlSeconds - the time to live for the lease, in seconds.
      leaseRenewSeconds - how long to wait to renew the lease, after a successful creation or renewal.
      leaseRenewRetryDelaySeconds - how long to wait to retry to renew the lease after a failed attempt
      leaseKeepaliveTimeoutMillis - the timeout for the gRPC unary call that tries to renew the lease on etcd; failures will be detected only after timeout, at which point the leaseRenewRetryDelaySeconds period will apply before retrying.
      uuid - A uuid (as obtained with, eg, GrpcAuthUtils.getRandomUUID(), used to identify this process instance in the election
      service - the service to register. This will determine the calculation for the host to register (potentially involving properties or environment variables associated with the service name if present, or defaults to localhost) and the service path associated to the service for etcd registration key path.
      idStringExtra - an additional string that may be useful to identify a particular election candidate in logs (eg, an IP address as a string)
      electionKey - The election key used to identify the election
      lostLeadershipCb - A runnable that will be invoked if leadership is lost after the constructor completes; note if the initial attempt to campaign fails, the constructor will throw and this callback will not be called.
      observeTimeoutMillis - a timeout in milliseconds for attempts to observe ourselves as leader; it is used as part of construction when the initial observe stream RPC is established, and any time it is dropped and needs to be restarted.
      rpcTimeoutMillis - A timeout in milliseconds as a budget for attempts to contact etcd.
      Throws:
      ExecutionException - if the creation of the lease handler fails due to async execution failing.
      InterruptedException - if the creation of the lease handler fails tue to being interrupted.
      TimeoutException - if the creation of the lease handler fails due to timeout.
  • Method Details

    • setLostLeadershipCb

      public void setLostLeadershipCb(Runnable newLostLeadershipCb)
      Description copied from interface: ElectionCandidate
      Set a new lost leadership callback, replacing the previously assigned one during construction. Note that if at the time this call is made leadership is already lost, the callback will be invoked from inside this method activation.
      Specified by:
      setLostLeadershipCb in interface ElectionCandidate
      Parameters:
      newLostLeadershipCb - a callback to run when leadership is lost.
    • register

      public boolean register(DhService service, int port)
      Description copied from interface: ElectionCandidate
      Register our address in DH Resolver. The registration is done as an etcd transaction conditional on our leadership key
      Specified by:
      register in interface ElectionCandidate
      Parameters:
      service - the service for the registration
      port - the port for the registration
      Returns:
      true if the registration succeeded in etcd, false otherwise, which implies leadership was lost.
    • isLeader

      public boolean isLeader()
      Specified by:
      isLeader in interface ElectionCandidate
      Returns:
      true if we are still the leader.
    • getLeadershipKey

      public io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequence getLeadershipKey()
      Description copied from interface: ElectionCandidate
      Get the leadership opaque key associated to us having leadership, or null if we are not the leader. This key can be used transactionally to do other operations conditional on us still being the leader.
      Specified by:
      getLeadershipKey in interface ElectionCandidate
      Returns:
      the leadership key
    • resign

      public void resign()
      Description copied from interface: ElectionCandidate
      Resign from this election and remove any DH Resolver registration.
      Specified by:
      resign in interface ElectionCandidate
    • close

      public void close()
      Description copied from interface: ElectionCandidate
      Close this object. Implies resigning if already leader.
      Specified by:
      close in interface ElectionCandidate