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(Consumer<Change<Leadership>> consumer)
Registers a listener to be notified of Leadership changes for all topics.
|
void |
addStatusChangeListener(Consumer<DistributedPrimitive.Status> listener)
Registers a listener to be called when the primitive's status changes.
|
boolean |
anoint(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(String topic)
Returns the
Leadership for the specified topic. |
Map<String,Leadership> |
getLeaderships()
Returns the current
Leadership s for all topics. |
boolean |
promote(String topic,
NodeId nodeId)
Attempts to promote a node to top of candidate list.
|
void |
removeChangeListener(Consumer<Change<Leadership>> consumer)
Unregisters a previously registered change notification listener.
|
void |
removeStatusChangeListener(Consumer<DistributedPrimitive.Status> listener)
Unregisters a previously registered listener to be called when the primitive's status changes.
|
Leadership |
run(String topic,
NodeId nodeId)
Attempts to become leader for a topic.
|
Collection<Consumer<DistributedPrimitive.Status>> |
statusChangeListeners()
Returns the collection of status change listeners previously registered.
|
void |
withdraw(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(String topic, NodeId nodeId)
LeaderElector
run
in interface LeaderElector
topic
- leadership topicnodeId
- instance identifier of the nodepublic void withdraw(String topic)
LeaderElector
withdraw
in interface LeaderElector
topic
- leadership topicpublic boolean anoint(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(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(String topic)
LeaderElector
Leadership
for the specified topic.getLeadership
in interface LeaderElector
topic
- leadership topicpublic Map<String,Leadership> getLeaderships()
LeaderElector
Leadership
s for all topics.getLeaderships
in interface LeaderElector
public void addChangeListener(Consumer<Change<Leadership>> consumer)
LeaderElector
addChangeListener
in interface LeaderElector
consumer
- listener to addpublic void removeChangeListener(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(Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitive
addStatusChangeListener
in interface DistributedPrimitive
listener
- The listener to be called when the status changes.public void removeStatusChangeListener(Consumer<DistributedPrimitive.Status> listener)
DistributedPrimitive
removeStatusChangeListener
in interface DistributedPrimitive
listener
- The listener to unregisterpublic Collection<Consumer<DistributedPrimitive.Status>> statusChangeListeners()
DistributedPrimitive
statusChangeListeners
in interface DistributedPrimitive