public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implements LeaderElector
LeaderElector backed by a AsyncLeaderElector.DistributedPrimitive.Status, DistributedPrimitive.TypeDEFAULT_OPERATION_TIMEOUT_MILLIS, DEFAULT_OPERTATION_TIMEOUT_MILLIS| Constructor and Description |
|---|
DefaultLeaderElector(AsyncLeaderElector asyncElector,
long operationTimeoutMillis) |
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Registers a listener to be notified of Leadership changes for all topics.
|
void |
addStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
Registers a listener to be called when the primitive's status changes.
|
boolean |
anoint(java.lang.String topic,
NodeId nodeId)
Attempts to promote a node to leadership displacing the current leader.
|
void |
evict(NodeId nodeId)
Attempts to evict a node from all leadership elections it is registered for.
|
Leadership |
getLeadership(java.lang.String topic)
Returns the
Leadership for the specified topic. |
java.util.Map<java.lang.String,Leadership> |
getLeaderships()
Returns the current
Leaderships for all topics. |
boolean |
promote(java.lang.String topic,
NodeId nodeId)
Attempts to promote a node to top of candidate list.
|
void |
removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Unregisters a previously registered change notification listener.
|
void |
removeStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
Unregisters a previously registered listener to be called when the primitive's status changes.
|
Leadership |
run(java.lang.String topic,
NodeId nodeId)
Attempts to become leader for a topic.
|
java.util.Collection<java.util.function.Consumer<DistributedPrimitive.Status>> |
statusChangeListeners()
Returns the collection of status change listeners previously registered.
|
void |
withdraw(java.lang.String topic)
Withdraws from leadership race for a topic.
|
destroy, name, primitiveTypeclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitprimitiveTypeapplicationId, destroy, namepublic DefaultLeaderElector(AsyncLeaderElector asyncElector, long operationTimeoutMillis)
public Leadership run(java.lang.String topic, NodeId nodeId)
LeaderElectorrun in interface LeaderElectortopic - leadership topicnodeId - instance identifier of the nodepublic void withdraw(java.lang.String topic)
LeaderElectorwithdraw in interface LeaderElectortopic - leadership topicpublic boolean anoint(java.lang.String topic,
NodeId nodeId)
LeaderElectoranoint in interface LeaderElectortopic - leadership topicnodeId - instance identifier of the new leadertrue if leadership transfer was successfully executed; false if it failed.
This operation can return false if the node to be made new leader is not registered to
run for election for the topic.public boolean promote(java.lang.String topic,
NodeId nodeId)
LeaderElectorpromote in interface LeaderElectortopic - leadership topicnodeId - instance identifier of the new top candidatetrue if node is now the top candidate. This operation can fail (i.e. return
false) if the node is not registered to run for election for the topic.public void evict(NodeId nodeId)
LeaderElectorIf the node the current leader for a topic, this call will force the next candidate (if one exists) to be promoted to leadership.
evict in interface LeaderElectornodeId - node instance identifierpublic Leadership getLeadership(java.lang.String topic)
LeaderElectorLeadership for the specified topic.getLeadership in interface LeaderElectortopic - leadership topicpublic java.util.Map<java.lang.String,Leadership> getLeaderships()
LeaderElectorLeaderships for all topics.getLeaderships in interface LeaderElectorpublic void addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
LeaderElectoraddChangeListener in interface LeaderElectorconsumer - listener to addpublic void removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
LeaderElectorIf the specified listener was not previously registered, this operation will be a noop.
removeChangeListener in interface LeaderElectorconsumer - listener to removepublic void addStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitiveaddStatusChangeListener in interface DistributedPrimitivelistener - The listener to be called when the status changes.public void removeStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitiveremoveStatusChangeListener in interface DistributedPrimitivelistener - The listener to unregisterpublic java.util.Collection<java.util.function.Consumer<DistributedPrimitive.Status>> statusChangeListeners()
DistributedPrimitivestatusChangeListeners in interface DistributedPrimitive