目录
1.looking状态下选leader,产出leader和follower
2.follower调用follower.followLeader()和leader调用leader.lead()
3.leader.lead()对每一个节点创建一个LearnerCnxAcceptor()
5.follower.followLeader()寻找、连接、注册leader
6.leader接收到follower注册信息,每个节点产生一个LearnerHandler()
2021SC@SDUSC
Follower和Leader状态同步-代码细节分析
1.looking状态下选leader,产出leader和follower
while (running) {
switch (getPeerState()) {
case LOOKING:
LOG.info("LOOKING");
if (Boolean.getBoolean("readonlymode.enabled")) {
LOG.info("Attempting to start ReadOnlyZooKeeperServer");
// Create read-only server but don't start it immediately
final ReadOnlyZooKeeperServer roZk =
new ReadOnlyZooKeeperServer(logFactory, this, this.zkDb);
// Instead of starting roZk immediately, wait some grace
// period before we decide we're partitioned.
//
// Thread is used here because otherwise it would require
// changes in each of election strategy classes which is
// unnecessary code coupling.
Thread roZkMgr = new Thread() {
public void run() {
try {
// lower-bound grace period to 2 secs
sleep(Math.max(2000, tickTime));
if (ServerState.LOOKING.equals(getPeerState())) {
roZk.startup();
}
} catch (InterruptedException e) {
LOG.info("Interrupted while attempting to start ReadOnlyZooKeeperServer, not started");
} catch (Exception e) {
LOG.error("FAILED to start ReadOnlyZooKeeperServer", e);
}
}
};
try {
roZkMgr.start();
reconfigFlagClear();
if (shuttingDownLE) {
shuttingDownLE = false;
startLeaderElection();
}
setCurrentVote(makeLEStrategy().lookForLeader());
2.follower调用follower.followLeader()和leader调用leader.lead()
case FOLLOWING:
try {
LOG.info("FOLLOWING");
setFollower(make