Zookeeper boot process 1. How to start
Zkserver.sh "Linux" or Zkserver.cmd "Windows"
Take Zkserver.cmd for example (too much content in zkserver.sh):
Can be clearly seen: called the Quorumpeermain this class, the passing parameters for%zoocfg% "in the Zkenv.cmd definition, is zoo.cfg."
To the Quorumpeermain class, there is a main method that accepts a parameter "Profile path":
Of course, accept the parameter is not a matter, but can not cluster, can only operate in stand-alone mode. The Zookeeper cluster boot process "Figure 120, Runfromconfig," is opened only if you accept a parameter as the profile path and this profile is not set to stand-alone mode.
2. Start process source Code Analysis
Runfromconfig:
As can be seen, the program moved to the Quorumpeer, first set a series of zoo.cfg in the attribute value, and then Start,quorumpeer inherited the thread class, naturally transferred to Quorumpeer.run ().
The Run method was too long, streamlined, and left only the skeleton:
@Override public void Run () {///JMX ... try {* * Main loop * * while (running) {switch (Getpeerstate ()) {case looking:/
... setcurrentvote (Makelestrategy (). Lookforleader ());
.. break;
Case Observing:try {setobserver (Makeobserver (logfactory));
Observer.observeleader ();
catch (Exception e) {} finally {Observer.shutdown ();
Setobserver (NULL);
Updateserverstate ();
} break;
Case Following:try {setfollower (Makefollower (logfactory));
Follower.followleader (); } catch (Exception e) {} finally {Follower.shutdown ();
Setfollower (NULL);
Updateserverstate ();
} break;
Case Leading:try {setleader (Makeleader (logfactory));
Leader.lead ();
Setleader (NULL);
The catch (Exception e) {} finally {if (leader!= null) {
Leader.shutdown ("forcing shutdown");
Setleader (NULL);
} updateserverstate ();
} break; }} finally {///clear JMX}}
As you can see, as long as there is no stop or no exception thrown, this thread has been running, there is no follow-up more operations, all in this cycle.
So far, this node in the Zookeeper cluster, "Peer", is started.
From the Run () method, you can see clearly that the nodes in zookeeper can have four states:
Looking observing following leading
Where state is initialized to looking in the Getpeerstate () method, each node is looking when it is started.
Next, is to participate in the vote, select the Zookeeper cluster leader, see the next article: Zookeeper Fastleaderelection algorithm.