public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implements LeaderElector
LeaderElector
backed by a AsyncLeaderElector
.DistributedPrimitive.Status, DistributedPrimitive.Type
DEFAULT_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
Leadership s 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, primitiveType
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
primitiveType
applicationId, destroy, name
public DefaultLeaderElector(AsyncLeaderElector asyncElector, long operationTimeoutMillis)
public Leadership run(java.lang.String topic, NodeId nodeId)
LeaderElector
run
in interface LeaderElector
topic
- leadership topicnodeId
- instance identifier of the nodepublic void withdraw(java.lang.String topic)
LeaderElector
withdraw
in interface LeaderElector
topic
- leadership topicpublic boolean anoint(java.lang.String topic, NodeId nodeId)
LeaderElector
anoint
in interface LeaderElector
topic
- 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)
LeaderElector
promote
in interface LeaderElector
topic
- 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)
LeaderElector
If 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 LeaderElector
nodeId
- node instance identifierpublic Leadership getLeadership(java.lang.String topic)
LeaderElector
Leadership
for the specified topic.getLeadership
in interface LeaderElector
topic
- leadership topicpublic java.util.Map<java.lang.String,Leadership> getLeaderships()
LeaderElector
Leadership
s for all topics.getLeaderships
in interface LeaderElector
public void addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
LeaderElector
addChangeListener
in interface LeaderElector
consumer
- listener to addpublic void removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
LeaderElector
If the specified listener was not previously registered, this operation will be a noop.
removeChangeListener
in interface LeaderElector
consumer
- listener to removepublic void addStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitive
addStatusChangeListener
in interface DistributedPrimitive
listener
- The listener to be called when the status changes.public void removeStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitive
removeStatusChangeListener
in interface DistributedPrimitive
listener
- The listener to unregisterpublic java.util.Collection<java.util.function.Consumer<DistributedPrimitive.Status>> statusChangeListeners()
DistributedPrimitive
statusChangeListeners
in interface DistributedPrimitive