Package org.apache.solr.cloud
Class LeaderElector
- java.lang.Object
-
- org.apache.solr.cloud.LeaderElector
-
public class LeaderElector extends Object
Leader Election process. This class contains the logic by which a leader is chosen. First callsetup(ElectionContext)
to ensure the election process is init'd. Next calljoinElection(ElectionContext, boolean)
to start the leader election.The implementation follows the classic ZooKeeper recipe of creating an ephemeral, sequential node for each candidate and then looking at the set of such nodes - if the created node is the lowest sequential node, the candidate that created the node is the leader. If not, the candidate puts a watch on the next lowest node it finds, and if that node goes down, starts the whole process over by checking if it's the lowest sequential node, etc.
-
-
Field Summary
Fields Modifier and Type Field Description static Pattern
LEADER_SEQ
protected org.apache.solr.common.cloud.SolrZkClient
zkClient
-
Constructor Summary
Constructors Constructor Description LeaderElector(org.apache.solr.common.cloud.SolrZkClient zkClient)
LeaderElector(org.apache.solr.common.cloud.SolrZkClient zkClient, org.apache.solr.cloud.ZkController.ContextKey key, Map<org.apache.solr.cloud.ZkController.ContextKey,ElectionContext> electionContexts)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description ElectionContext
getContext()
static String
getNodeName(String nStringSequence)
static int
getSeq(String nStringSequence)
Returns int given String of form n_0000000001 or n_0000000003, etc.int
joinElection(ElectionContext context, boolean replacement)
int
joinElection(ElectionContext context, boolean replacement, boolean joinAtHead)
Begin participating in the election process.protected void
runIamLeaderProcess(ElectionContext context, boolean weAreReplacement)
void
setup(ElectionContext context)
Set up any ZooKeeper nodes needed for leader election.static void
sortSeqs(List<String> seqs)
Sort n string sequence list.
-
-
-
Field Detail
-
LEADER_SEQ
public static final Pattern LEADER_SEQ
-
zkClient
protected org.apache.solr.common.cloud.SolrZkClient zkClient
-
-
Constructor Detail
-
LeaderElector
public LeaderElector(org.apache.solr.common.cloud.SolrZkClient zkClient)
-
LeaderElector
public LeaderElector(org.apache.solr.common.cloud.SolrZkClient zkClient, org.apache.solr.cloud.ZkController.ContextKey key, Map<org.apache.solr.cloud.ZkController.ContextKey,ElectionContext> electionContexts)
-
-
Method Detail
-
getContext
public ElectionContext getContext()
-
runIamLeaderProcess
protected void runIamLeaderProcess(ElectionContext context, boolean weAreReplacement) throws org.apache.zookeeper.KeeperException, InterruptedException, IOException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
IOException
-
getSeq
public static int getSeq(String nStringSequence)
Returns int given String of form n_0000000001 or n_0000000003, etc.- Returns:
- sequence number
-
joinElection
public int joinElection(ElectionContext context, boolean replacement) throws org.apache.zookeeper.KeeperException, InterruptedException, IOException
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
IOException
-
joinElection
public int joinElection(ElectionContext context, boolean replacement, boolean joinAtHead) throws org.apache.zookeeper.KeeperException, InterruptedException, IOException
Begin participating in the election process. Gets a new sequential number and begins watching the node with the sequence number before it, unless it is the lowest number, in which case, initiates the leader process. If the node that is watched goes down, check if we are the new lowest node, else watch the next lowest numbered node.- Returns:
- sequential node number
- Throws:
org.apache.zookeeper.KeeperException
InterruptedException
IOException
-
setup
public void setup(ElectionContext context) throws InterruptedException, org.apache.zookeeper.KeeperException
Set up any ZooKeeper nodes needed for leader election.- Throws:
InterruptedException
org.apache.zookeeper.KeeperException
-
-