public interface AsyncLeaderElector extends DistributedPrimitive
AsyncLeaderElector facilitates mutually exclusive access to a shared resource by various cluster members.
Each resource is identified by a unique topic name and members register their desire to access the resource by
calling the run method. Access is grated on a FIFO basis. An instance can
unregister itself from the leadership election by calling withdraw method.
If an instance currently holding the resource dies then the next instance waiting to be leader (in FIFO order)
will be automatically granted access to the resource.
One can register listeners to be notified when a leadership change occurs. The Listeners are notified via a
Leadership change subject.
Additionally, AsyncLeaderElector provides methods to query the current state of leadership for topics.
All methods of this interface return a future immediately after a successful invocation.
The operation itself is executed asynchronous and the returned future will be
completed when the operation finishes.
DistributedPrimitive.Status, DistributedPrimitive.TypeDEFAULT_OPERATION_TIMEOUT_MILLIS, DEFAULT_OPERTATION_TIMEOUT_MILLIS| Modifier and Type | Method and Description |
|---|---|
java.util.concurrent.CompletableFuture<java.lang.Void> |
addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Registers a listener to be notified of Leadership changes for all topics.
|
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
anoint(java.lang.String topic,
NodeId nodeId)
Attempts to promote a node to leadership displacing the current leader.
|
default LeaderElector |
asLeaderElector()
Returns a new
LeaderElector that is backed by this instance and with a default operation timeout. |
default LeaderElector |
asLeaderElector(long timeoutMillis)
Returns a new
LeaderElector that is backed by this instance. |
java.util.concurrent.CompletableFuture<java.lang.Void> |
evict(NodeId nodeId)
Attempts to evict a node from all leadership elections it is registered for.
|
java.util.concurrent.CompletableFuture<Leadership> |
getLeadership(java.lang.String topic)
Returns the
Leadership for the specified topic. |
java.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,Leadership>> |
getLeaderships()
Returns the current
Leaderships for all topics. |
default DistributedPrimitive.Type |
primitiveType()
Returns the type of primitive.
|
java.util.concurrent.CompletableFuture<java.lang.Boolean> |
promote(java.lang.String topic,
NodeId nodeId)
Attempts to promote a node to top of candidate list without displacing the current leader.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Unregisters a previously registered change notification listener.
|
java.util.concurrent.CompletableFuture<Leadership> |
run(java.lang.String topic,
NodeId nodeId)
Attempts to become leader for a topic.
|
java.util.concurrent.CompletableFuture<java.lang.Void> |
withdraw(java.lang.String topic)
Withdraws from leadership race for a topic.
|
addStatusChangeListener, applicationId, destroy, name, removeStatusChangeListener, statusChangeListenersdefault DistributedPrimitive.Type primitiveType()
DistributedPrimitiveprimitiveType in interface DistributedPrimitivejava.util.concurrent.CompletableFuture<Leadership> run(java.lang.String topic, NodeId nodeId)
topic - leadership topicnodeId - instance identifier of the nodejava.util.concurrent.CompletableFuture<java.lang.Void> withdraw(java.lang.String topic)
topic - leadership topicjava.util.concurrent.CompletableFuture<java.lang.Boolean> anoint(java.lang.String topic,
NodeId nodeId)
topic - leadership topicnodeId - instance identifier of the new leaderjava.util.concurrent.CompletableFuture<java.lang.Void> evict(NodeId nodeId)
If the node is the current leader for a topic, this call will promote the next top candidate (if one exists) to leadership.
nodeId - node instance identifierjava.util.concurrent.CompletableFuture<java.lang.Boolean> promote(java.lang.String topic,
NodeId nodeId)
topic - leadership topicnodeId - instance identifier of the new top candidatejava.util.concurrent.CompletableFuture<Leadership> getLeadership(java.lang.String topic)
Leadership for the specified topic.topic - leadership topicjava.util.concurrent.CompletableFuture<java.util.Map<java.lang.String,Leadership>> getLeaderships()
Leaderships for all topics.java.util.concurrent.CompletableFuture<java.lang.Void> addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
consumer - listener to notifyjava.util.concurrent.CompletableFuture<java.lang.Void> removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
If the specified listener was not previously registered, this operation will be a noop.
consumer - listener to removedefault LeaderElector asLeaderElector(long timeoutMillis)
LeaderElector that is backed by this instance.timeoutMillis - timeout duration for the returned LeaderElector operationsLeaderElector instancedefault LeaderElector asLeaderElector()
LeaderElector that is backed by this instance and with a default operation timeout.LeaderElector instance