Package org.onosproject.store.primitives
Class DefaultLeaderElector
- java.lang.Object
-
- org.onosproject.store.service.Synchronous<AsyncLeaderElector>
-
- org.onosproject.store.primitives.DefaultLeaderElector
-
- All Implemented Interfaces:
DistributedPrimitive
,LeaderElector
public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implements LeaderElector
Default implementation for aLeaderElector
backed by aAsyncLeaderElector
.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.onosproject.store.service.DistributedPrimitive
DistributedPrimitive.Status, DistributedPrimitive.Type
-
-
Field Summary
-
Fields inherited from interface org.onosproject.store.service.DistributedPrimitive
DEFAULT_OPERATION_TIMEOUT_MILLIS
-
-
Constructor Summary
Constructors Constructor Description DefaultLeaderElector(AsyncLeaderElector asyncElector, long operationTimeoutMillis)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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 theLeadership
for the specified topic.java.util.Map<java.lang.String,Leadership>
getLeaderships()
Returns the currentLeadership
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.-
Methods inherited from class org.onosproject.store.service.Synchronous
destroy, name, primitiveType
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.onosproject.store.service.DistributedPrimitive
applicationId, destroy, name
-
Methods inherited from interface org.onosproject.store.service.LeaderElector
primitiveType
-
-
-
-
Constructor Detail
-
DefaultLeaderElector
public DefaultLeaderElector(AsyncLeaderElector asyncElector, long operationTimeoutMillis)
-
-
Method Detail
-
run
public Leadership run(java.lang.String topic, NodeId nodeId)
Description copied from interface:LeaderElector
Attempts to become leader for a topic.- Specified by:
run
in interfaceLeaderElector
- Parameters:
topic
- leadership topicnodeId
- instance identifier of the node- Returns:
- current Leadership state of the topic
-
withdraw
public void withdraw(java.lang.String topic)
Description copied from interface:LeaderElector
Withdraws from leadership race for a topic.- Specified by:
withdraw
in interfaceLeaderElector
- Parameters:
topic
- leadership topic
-
anoint
public boolean anoint(java.lang.String topic, NodeId nodeId)
Description copied from interface:LeaderElector
Attempts to promote a node to leadership displacing the current leader.- Specified by:
anoint
in interfaceLeaderElector
- Parameters:
topic
- leadership topicnodeId
- instance identifier of the new leader- Returns:
true
if leadership transfer was successfully executed;false
if it failed. This operation can returnfalse
if the node to be made new leader is not registered to run for election for the topic.
-
promote
public boolean promote(java.lang.String topic, NodeId nodeId)
Description copied from interface:LeaderElector
Attempts to promote a node to top of candidate list.- Specified by:
promote
in interfaceLeaderElector
- Parameters:
topic
- leadership topicnodeId
- instance identifier of the new top candidate- Returns:
true
if node is now the top candidate. This operation can fail (i.e. returnfalse
) if the node is not registered to run for election for the topic.
-
evict
public void evict(NodeId nodeId)
Description copied from interface:LeaderElector
Attempts to evict a node from all leadership elections it is registered for.If the node the current leader for a topic, this call will force the next candidate (if one exists) to be promoted to leadership.
- Specified by:
evict
in interfaceLeaderElector
- Parameters:
nodeId
- node instance identifier
-
getLeadership
public Leadership getLeadership(java.lang.String topic)
Description copied from interface:LeaderElector
Returns theLeadership
for the specified topic.- Specified by:
getLeadership
in interfaceLeaderElector
- Parameters:
topic
- leadership topic- Returns:
- current Leadership state of the topic
-
getLeaderships
public java.util.Map<java.lang.String,Leadership> getLeaderships()
Description copied from interface:LeaderElector
Returns the currentLeadership
s for all topics.- Specified by:
getLeaderships
in interfaceLeaderElector
- Returns:
- topic name to Leadership mapping
-
addChangeListener
public void addChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Description copied from interface:LeaderElector
Registers a listener to be notified of Leadership changes for all topics.- Specified by:
addChangeListener
in interfaceLeaderElector
- Parameters:
consumer
- listener to add
-
removeChangeListener
public void removeChangeListener(java.util.function.Consumer<Change<Leadership>> consumer)
Description copied from interface:LeaderElector
Unregisters a previously registered change notification listener.If the specified listener was not previously registered, this operation will be a noop.
- Specified by:
removeChangeListener
in interfaceLeaderElector
- Parameters:
consumer
- listener to remove
-
addStatusChangeListener
public void addStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
Description copied from interface:DistributedPrimitive
Registers a listener to be called when the primitive's status changes.- Specified by:
addStatusChangeListener
in interfaceDistributedPrimitive
- Parameters:
listener
- The listener to be called when the status changes.
-
removeStatusChangeListener
public void removeStatusChangeListener(java.util.function.Consumer<DistributedPrimitive.Status> listener)
Description copied from interface:DistributedPrimitive
Unregisters a previously registered listener to be called when the primitive's status changes.- Specified by:
removeStatusChangeListener
in interfaceDistributedPrimitive
- Parameters:
listener
- The listener to unregister
-
statusChangeListeners
public java.util.Collection<java.util.function.Consumer<DistributedPrimitive.Status>> statusChangeListeners()
Description copied from interface:DistributedPrimitive
Returns the collection of status change listeners previously registered.- Specified by:
statusChangeListeners
in interfaceDistributedPrimitive
- Returns:
- collection of status change listeners
-
-