Class ElectionCandidateImpl
java.lang.Object
io.deephaven.enterprise.etcdclient.election.ElectionCandidateImpl
- All Implemented Interfaces:
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
ConstructorsConstructorDescriptionElectionCandidateImpl(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 TypeMethodDescriptionvoidclose()Close this object.io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequenceGet the leadership opaque key associated to us having leadership, or null if we are not the leader.booleanisLeader()booleanRegister our address in DH Resolver.voidresign()Resign from this election and remove any DH Resolver registration.voidsetLostLeadershipCb(Runnable newLostLeadershipCb) Set a new lost leadership callback, replacing the previously assigned one during construction.
-
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 errorsetcdClient- 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 attemptleaseKeepaliveTimeoutMillis- 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 electionservice- 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 electionlostLeadershipCb- 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
Description copied from interface:ElectionCandidateSet 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:
setLostLeadershipCbin interfaceElectionCandidate- Parameters:
newLostLeadershipCb- a callback to run when leadership is lost.
-
register
Description copied from interface:ElectionCandidateRegister our address in DH Resolver. The registration is done as an etcd transaction conditional on our leadership key- Specified by:
registerin interfaceElectionCandidate- Parameters:
service- the service for the registrationport- 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:
isLeaderin interfaceElectionCandidate- Returns:
- true if we are still the leader.
-
getLeadershipKey
public io.deephaven.shadow.jetcd.io.etcd.jetcd.ByteSequence getLeadershipKey()Description copied from interface:ElectionCandidateGet 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:
getLeadershipKeyin interfaceElectionCandidate- Returns:
- the leadership key
-
resign
public void resign()Description copied from interface:ElectionCandidateResign from this election and remove any DH Resolver registration.- Specified by:
resignin interfaceElectionCandidate
-
close
public void close()Description copied from interface:ElectionCandidateClose this object. Implies resigning if already leader.- Specified by:
closein interfaceElectionCandidate
-