public class LeaderSelector extends Object implements Closeable
Abstraction to select a "leader" amongst multiple contenders in a group of JMVs connected to a Zookeeper cluster. If a group of N thread/processes contends for leadership, one will be assigned leader until it releases leadership at which time another one from the group will be chosen.
Note that this class uses an underlying InterProcessMutex
and as a result leader
election is "fair" - each user will become leader in the order originally requested
(from ZK's point of view).
Constructor and Description |
---|
LeaderSelector(CuratorFramework client,
String leaderPath,
CloseableExecutorService executorService,
LeaderSelectorListener listener) |
LeaderSelector(CuratorFramework client,
String leaderPath,
ExecutorService executorService,
LeaderSelectorListener listener) |
LeaderSelector(CuratorFramework client,
String leaderPath,
LeaderSelectorListener listener) |
LeaderSelector(CuratorFramework client,
String leaderPath,
ThreadFactory threadFactory,
Executor executor,
LeaderSelectorListener listener)
Deprecated.
This constructor was poorly thought out. Custom executor is useless. Use this version instead:
LeaderSelector(CuratorFramework, String, ExecutorService, LeaderSelectorListener) |
Modifier and Type | Method and Description |
---|---|
void |
autoRequeue()
By default, when
LeaderSelectorListener.takeLeadership(CuratorFramework) returns, this
instance is not requeued. |
void |
close()
Shutdown this selector and remove yourself from the leadership group
|
String |
getId()
Return the ID that was set via
setId(String) |
Participant |
getLeader()
Return the id for the current leader.
|
Collection<Participant> |
getParticipants()
Returns the set of current participants in the leader selection
|
boolean |
hasLeadership()
Return true if leadership is currently held by this instance
|
void |
interruptLeadership()
Attempt to cancel and interrupt the current leadership if this instance has leadership
|
boolean |
requeue()
Re-queue an attempt for leadership.
|
void |
setId(String id)
Sets the ID to store for this leader.
|
void |
start()
Attempt leadership.
|
public LeaderSelector(CuratorFramework client, String leaderPath, LeaderSelectorListener listener)
client
- the clientleaderPath
- the path for this leadership grouplistener
- listener@Deprecated public LeaderSelector(CuratorFramework client, String leaderPath, ThreadFactory threadFactory, Executor executor, LeaderSelectorListener listener)
LeaderSelector(CuratorFramework, String, ExecutorService, LeaderSelectorListener)
client
- the clientleaderPath
- the path for this leadership groupthreadFactory
- factory to use for making internal threadsexecutor
- the executor to run inlistener
- listenerpublic LeaderSelector(CuratorFramework client, String leaderPath, ExecutorService executorService, LeaderSelectorListener listener)
client
- the clientleaderPath
- the path for this leadership groupexecutorService
- thread pool to uselistener
- listenerpublic LeaderSelector(CuratorFramework client, String leaderPath, CloseableExecutorService executorService, LeaderSelectorListener listener)
client
- the clientleaderPath
- the path for this leadership groupexecutorService
- thread pool to uselistener
- listenerpublic void autoRequeue()
LeaderSelectorListener.takeLeadership(CuratorFramework)
returns, this
instance is not requeued. Calling this method puts the leader selector into a mode where it
will always requeue itself.public void setId(String id)
getParticipants()
is called. IMPORTANT: must be called
prior to start()
to have effect.id
- IDpublic String getId()
setId(String)
public void start()
requeue()
for this purpose.public boolean requeue()
The attempt will finish after session error, leadership release. This method is inherently
hard to use as there is no public API to guarantee successful requeue. Try autoRequeue()
if you are in doubt.
public void close()
close
in interface Closeable
close
in interface AutoCloseable
public Collection<Participant> getParticipants() throws Exception
Returns the set of current participants in the leader selection
NOTE - this method polls the ZK server. Therefore it can possibly
return a value that does not match hasLeadership()
as hasLeadership
uses a local field of the class.
Exception
- ZK errors, interruptions, etc.public Participant getLeader() throws Exception
Return the id for the current leader. If for some reason there is no current leader, a dummy participant is returned.
NOTE - this method polls the ZK server. Therefore it can possibly
return a value that does not match hasLeadership()
as hasLeadership
uses a local field of the class.
Exception
- ZK errors, interruptions, etc.public boolean hasLeadership()
public void interruptLeadership()
Copyright © 2011–2023 The Apache Software Foundation. All rights reserved.