Zookeeper源码学习

源码搭建

【ZooKeeper系列】3.ZooKeeper源码环境搭建

zookeeper源码分析之单机模式服务端启动

⼀、执⾏过程概述

单机模式的ZK服务端逻辑写在ZooKeeperServerMain类中,由⾥⾯的main函数启动,整个过程如下:
在这里插入图片描述

单机模式的委托启动类为:ZooKeeperServerMain

服务端启动过程

看下ZooKeeperServerMain⾥⾯的main函数代码:


   public static void main(String[] args) {
       ZooKeeperServerMain main = new ZooKeeperServerMain();
       main.initializeAndRun(args);
   }
   protected void initializeAndRun(String[] args)
           throws ConfigException, IOException, AdminServerException
   {
       ServerConfig config = new ServerConfig();
       //如果⼊参只有⼀个,则认为是配置⽂件的路径
       if (args.length == 1) {
           config.parse(args[0]);
       } else {
           //否则是各个参数
           config.parse(args);
       }
       runFromConfig(config);
   }

   //省略部分代码,只保留了核⼼逻辑


   public void runFromConfig(ServerConfig config) throws IOException,AdminServerException {
       FileTxnSnapLog txnLog = null; try {
       //初始化⽇志⽂件
           txnLog = new FileTxnSnapLog(config.dataLogDir, config.dataDir);
       //初始化ZkServer对象
           final ZooKeeperServer zkServer = new ZooKeeperServer(txnLog, config.tickTime, config.minSessionTimeout, config.maxSessionTimeout, null);
           txnLog.setServerStats(zkServer.serverStats());

           if (config.getClientPortAddress() != null) {
               //初始化server端IO对象,默认是NIOServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
               //初始化配置信息
               cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), false);
               //启动服务
               cnxnFactory.startup(zkServer);
               
           }

       //container ZNodes是3.6版本之后新增的节点类型,Container类型的节点会在它没有⼦节点时
       // 被删除(新创建的Container节点除外),该类就是⽤来周期性的进⾏检查清理⼯作
           containerManager = new ContainerManager(zkServer.getZKDatabase(),

                   zkServer.firstProcessor,
                   Integer.getInteger("znode.container.checkIntervalMs", (int) TimeUnit.MINUTES.toMillis(1)),
                   Integer.getInteger("znode.container.maxPerMinute", 10000)


           );
           containerManager.start();

       //省略关闭逻辑
       } catch (InterruptedException e) { LOG.warn("Server interrupted", e);
       } finally {
           if (txnLog != null) { txnLog.close();


           }
       }
   }

小结:
zk单机模式启动主要流程:

  1. 注册jmx
  2. 解析ServerConfig配置对象
  3. 根据配置对象,运⾏单机zk服务
  4. 创建管理事务⽇志和快照FileTxnSnapLog对象,zookeeperServer对象,并设置zkServer的统计对象
  5. 设置zk服务钩⼦,原理是通过设置CountDownLatch,调⽤ZooKeeperServerShutdownHandler的handle⽅法,可以将触发shutdownLatch.await⽅法继续执⾏,即调⽤shutdown关闭单机服务
  6. 基于jetty创建zk的admin服务
  7. 创建连接对象cnxnFactory和secureCnxnFactory(安全连接才创建该对象),⽤于处理客户端的请求
  8. 创建定时清除容器节点管理器,⽤于处理容器节点下不存在⼦节点的清理容器节点⼯作等

可以看到关键点在于解析配置跟启动两个⽅法,先来看下解析配置逻辑,对应上⾯的configure⽅法:

    //依旧省略掉了部分逻辑
    public void configure(InetSocketAddress addr, int maxcc, boolean secure) throws IOException {
        maxClientCnxns = maxcc;
        //会话超时时间
        sessionlessCnxnTimeout =
                Integer.getInteger(ZOOKEEPER_NIO_SESSIONLESS_CNXN_TIMEOUT, 10000);
        //过期队列
        cnxnExpiryQueue = new ExpiryQueue<NIOServerCnxn>(sessionlessCnxnTimeout);
        //过期线程,从cnxnExpiryQueue中读取数据,如果已经过期则关闭 expirerThread = new ConnectionExpirerThread();

        //根据CPU个数计算selector线程的数量
        int numCores = Runtime.getRuntime().availableProcessors(); numSelectorThreads =
                Integer.getInteger(ZOOKEEPER_NIO_NUM_SELECTOR_THREADS, Math.max((int) Math.sqrt((float) numCores/2), 1));
        if (numSelectorThreads < 1) {
            throw new IOException("numSelectorThreads must be at least 1");


        }

        //计算woker线程的数量
        numWorkerThreads = Integer.getInteger(ZOOKEEPER_NIO_NUM_WORKER_THREADS, 2
                * numCores);
        //worker线程关闭时间
        workerShutdownTimeoutMS = Long.getLong(ZOOKEEPER_NIO_SHUTDOWN_TIMEOUT, 5000);

        //初始化selector线程
        for(int i=0; i<numSelectorThreads; ++i) { selectorThreads.add(new SelectorThread(i));

        }

        this.ss = ServerSocketChannel.open();
        ss.socket().setReuseAddress(true);
        ss.socket().bind(addr);
        ss.configureBlocking(false);
        //初始化accept线程,这⾥看出accept线程只有⼀个,⾥⾯会注册监听ACCEPT事件
        acceptThread = new AcceptThread(ss, addr, selectorThreads);
    }
//再来看下启动逻辑:

    public void startup(ZooKeeperServer zkServer) throws IOException, InterruptedException {
        startup(zkServer, true);

    }
//启动分了好⼏块,⼀个⼀个看
	public void startup(ZooKeeperServer zks, boolean startServer) throws IOException, InterruptedException {
        start();
        setZooKeeperServer(zks);
        if (startServer) {
            zks.startdata(); zks.startup();

        }
	}


    //⾸先是start⽅法
    public void start() {
        stopped = false;
        //初始化worker线程池
        if (workerPool == null) {
            workerPool = new WorkerService("NIOWorker", numWorkerThreads, false);

        }
        //挨个启动select线程
        for(SelectorThread thread : selectorThreads) {
            if (thread.getState() == Thread.State.NEW) {
                thread.start();
            }
        }

        //启动acceptThread线程
        if (acceptThread.getState() == Thread.State.NEW) {
            acceptThread.start();
        }
        //启动expirerThread线程
        if (expirerThread.getState() == Thread.State.NEW) {
            expirerThread.start();
        }
    }

    //初始化数据结构
    public void startdata() throws IOException, InterruptedException {
        //初始化ZKDatabase,该数据结构⽤来保存ZK上⾯存储的所有数据


        if (zkDb == null) {
            //初始化数据数据,这⾥会加⼊⼀些原始节点,例如/zookeeper
            zkDb = new ZKDatabase(this.txnLogFactory);
        }
//加载磁盘上已经存储的数据,如果有的话
        if (!zkDb.isInitialized()) {
        loadData();

        }
    }

    //启动剩余项⽬
    public synchronized void startup() {
        //初始化session追踪器
        if (sessionTracker == null) {
            createSessionTracker();
        }
        //启动session追踪器
        startSessionTracker();
        //建⽴请求处理链路
        setupRequestProcessors();
        registerJMX();
        setState(State.RUNNING);
        notifyAll();
    }

    //这⾥可以看出,单机模式下请求的处理链路为:
    //PrepRequestProcessor -> SyncRequestProcessor -> FinalRequestProcessor
    protected void setupRequestProcessors() {
        RequestProcessor finalProcessor = new FinalRequestProcessor(this);
        RequestProcessor syncProcessor = new SyncRequestProcessor(this, finalProcessor);
        ((SyncRequestProcessor)syncProcessor).start();
        firstProcessor = new PrepRequestProcessor(this, syncProcessor);
        ((PrepRequestProcessor)firstProcessor).start();

    }

源码分析之Leader选举(⼀)

分析Zookeeper中⼀个核⼼的模块,Leader选举。
总体框架图
对于Leader选举,其总体框架图如下图所示
在这里插入图片描述

AuthFastLeaderElection,LeaderElection其在3.4.0之后的版本中已经不建议使⽤。
Election源码分析


public interface Election {
    public Vote lookForLeader() throws InterruptedException;
    public void shutdown();
}

说明:
  选举的⽗接⼝为Election,其定义了lookForLeader和shutdown两个⽅法,lookForLeader表示寻找Leader,shutdown则表示关闭,如关闭服务端之间的连接。

源码分析之Leader选举(⼆)之FastLeaderElection

刚刚介绍了Leader选举的总体框架,接着来学习Zookeeper中默认的选举策略,FastLeaderElection。
FastLeaderElection源码分析
类的继承关系

public class FastLeaderElection implements Election {}

说明:FastLeaderElection实现了Election接⼝,重写了接⼝中定义的lookForLeader⽅法和shutdown⽅法

在源码分析之前,⾸先介绍⼏个概念:

  • 外部投票:特指其他服务器发来的投票。
  • 内部投票:服务器⾃身当前的投票。
  • 选举轮次:ZooKeeper服务器Leader选举的轮次,即logical clock(逻辑时钟)。
  • PK:指对内部投票和外部投票进⾏⼀个对⽐来确定是否需要变更内部投票。 选票管理
  • sendqueue:选票发送队列,⽤于保存待发送的选票。
  • recvqueue:选票接收队列,⽤于保存接收到的外部投票。
    在这里插入图片描述

lookForLeader函数
当 ZooKeeper 服务器检测到当前服务器状态变成 LOOKING 时,就会触发 Leader选举,即调⽤lookForLeader⽅法来进⾏Leader选举。

在这里插入图片描述

    public Vote lookForLeader() throws InterruptedException {
        synchronized(this){
            // ⾸先会将逻辑时钟⾃增,每进⾏⼀轮新的leader选举,都需要更新逻辑时钟
            logicalclock++;
            // 更新选票(初始化选票)
            updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch());
        }


        LOG.info("New election. My id =  " + self.getId() +
                ", proposed zxid=0x" + Long.toHexString(proposedZxid));
        // 向其他服务器发送⾃⼰的选票(已更新的选票)
        sendNotifications();

之后每台服务器会不断地从recvqueue队列中获取外部选票。如果服务器发现⽆法获取到任何外部投 票,就⽴即确认⾃⼰是否和集群中其他服务器保持着有效的连接,如果没有连接,则⻢上建⽴连接,如果已经建⽴了连接,则再次发送⾃⼰当前的内部投票,其流程如下

// 从recvqueue接收队列中取出投票

   Notification n = recvqueue.poll(notTimeout, TimeUnit.MILLISECONDS);

       /*

        * Sends more notifications if haven't received enough.

        * Otherwise processes new notification.

        */

       if(n == null){ // ⽆法获取选票

               if(manager.haveDelivered()){ // manager已经发送了所有选票消息(表示有连接)

               // 向所有其他服务器发送消息

               sendNotifications();

       } else { // 还未发送所有消息(表示⽆连接)

               // 连接其他每个服务器

               manager.connectAll();

       }

       /*

        * Exponential backoff

        */

       int tmpTimeOut = notTimeout*2;

       notTimeout = (tmpTimeOut < maxNotificationInterval? tmpTimeOut : maxNotificationInterval);

       LOG.info("Notification time out: " + notTimeout);

  在发送完初始化选票之后,接着开始处理外部投票。在处理外部投票时,会根据选举轮次来进⾏不同的处理。

  • 外部投票的选举轮次⼤于内部投票。若服务器⾃身的选举轮次落后于该外部投票对应服务器的选举轮次,那么就会⽴即更新⾃⼰的选举轮次(logicalclock),并且清空所有已经收到的投票,然后使⽤初始化的投票来进⾏PK以确定是否变更内部投票。最终再将内部投票发送出去。
  • 外部投票的选举轮次⼩于内部投票。若服务器接收的外选票的选举轮次落后于⾃身的选举轮次,那么Zookeeper就会直接忽略该外部投票,不做任何处理。
  • 外部投票的选举轮次等于内部投票。此时可以开始进⾏选票PK,如果消息中的选票更优,则需要更新本服务器内部选票,再发送给其他服务器。

  之后再对选票进⾏归档操作,⽆论是否变更了投票,都会将刚刚收到的那份外部投票放⼊选票集合recvset中进⾏归档,其中recvset⽤于记录当前服务器在本轮次的Leader选举中收到的所有外部投票,然后开始统计投票,统计投票是为了统计集群中是否已经有过半的服务器认可了当前的内部投票,如果确定已经有过半服务器认可了该投票,然后再进⾏最后⼀次确认,判断是否⼜有更优的选票产⽣,若⽆,则终⽌投票,然后最终的选票,其流程如下

                            if (n.electionEpoch > logicalclock.get()) {//其选举周期⼤于逻辑时钟
                               // 重新赋值逻辑时钟
                               logicalclock.set(n.electionEpoch);
                               // 清空所有接收到的所有选票
                               recvset.clear();
                               if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                       getInitId(), getInitLastLoggedZxid(), getPeerEpoch())) {// 进⾏PK,选出较优的服务器
                                   updateProposal(n.leader, n.zxid, n.peerEpoch);
                               } else {// ⽆法选出较优的服务器
                                   // 更新选票
                                   updateProposal(getInitId(),
                                           getInitLastLoggedZxid(),
                                           getPeerEpoch());
                               }
                               // 发送本服务器的内部选票消息
                               sendNotifications();
                           } else if (n.electionEpoch < logicalclock.get()) { // 选举周期⼩于逻辑时钟,不做处理,直接忽略
                               if(LOG.isDebugEnabled()){
                                   LOG.debug("Notification election epoch is smaller than logicalclock. n.electionEpoch = 0x"
                                           + Long.toHexString(n.electionEpoch)
                                           + ", logicalclock=0x" + Long.toHexString(logicalclock.get()));
                               }
                               break;
                           } else if (totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                   proposedLeader, proposedZxid, proposedEpoch)) {// PK,选出较优的服务器
                               // 更新选票
                               updateProposal(n.leader, n.zxid, n.peerEpoch);
                               // 发送消息
                               sendNotifications();
                           }
   
                           if(LOG.isDebugEnabled()){
                               LOG.debug("Adding vote: from=" + n.sid +
                                       ", proposed leader=" + n.leader +
                                       ", proposed zxid=0x" + Long.toHexString(n.zxid) +
                                       ", proposed election epoch=0x" + Long.toHexString(n.electionEpoch));
                           }
                           // recvset⽤于记录当前服务器在本轮次的Leader选举中收到的所有外部投票
                           recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch));
   
                           if (termPredicate(recvset,
                                   new Vote(proposedLeader, proposedZxid,
                                           logicalclock.get(), proposedEpoch))) {
   
                               // Verify if there is any change in the proposed leader
                               while((n = recvqueue.poll(finalizeWait,
                                       TimeUnit.MILLISECONDS)) != null){// 遍历已经接收的投票集合
   
                                   if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                           proposedLeader, proposedZxid, proposedEpoch)){// 选票有变更,⽐之前提议的Leader有更好的选票加⼊
                                       // 将更优的选票放在recvset中
                                       recvqueue.put(n);
                                       break;
                                   }
                               }
   
                               /*
                                * This predicate is true once we don't read any new
                                * relevant message from the reception queue
                                */
                               if (n == null) {// 表示之前提议的Leader已经是最优的
                                   // 设置服务器状态
                                   self.setPeerState((proposedLeader == self.getId()) ?
                                           ServerState.LEADING: learningState());
                                   // 最终的选票
                                   Vote endVote = new Vote(proposedLeader,
                                           proposedZxid, proposedEpoch);
                                   // 清空recvqueue队列的选票
                                   leaveInstance(endVote);
                                   // 返回选票
                                   return endVote;
                               }
                           }
  1. ⾃增选举轮次。 在 FastLeaderElection 实现中,有⼀个 logicalclock 属性,⽤于标识当前Leader的选举轮次,ZooKeeper规定了所有有效的投票都必须在同⼀轮次中。ZooKeeper在开始新⼀轮的投票时,会⾸先对logicalclock进⾏⾃增操作。

  2. 初始化选票。 在开始进⾏新⼀轮的投票之前,每个服务器都会⾸先初始化⾃⼰的选票。在图7-33中我们已经讲解了 Vote 数据结构,初始化选票也就是对 Vote 属性的初始化。在初始化阶段,每台服务器都会将⾃⼰推举为Leader,表7-10展示了⼀个初始化的选票。

  3. 发送初始化选票。 在完成选票的初始化后,服务器就会发起第⼀次投票。ZooKeeper 会将刚刚初始化好的选票放⼊sendqueue队列中,由发送器WorkerSender负

  4. 接收外部投票。 每台服务器都会不断地从 recvqueue 队列中获取外部投票。如果服务器发现⽆法获取到任何的外部投票,那么就会⽴即确认⾃⼰是否和集群中其他服务器保持着有效连接。如果发现没有建⽴连接,那么就会⻢上建⽴连接。如果已经建⽴了连接,那么就再次发送⾃⼰当前的内部投票。

  5. 判断选举轮次。 当发送完初始化选票之后,接下来就要开始处理外部投票了。在处理外部投票的时候,会根据选举轮次来进⾏不同的处理。 · 外部投票的选举轮次⼤于内部投票。如果服务器发现⾃⼰的选举轮次已经落后于该外部投票对应服务器的选举轮次,那么就会⽴即更新⾃⼰的选举轮次(logicalclock),并且清空所有已经收到的投票,然后使⽤初始化的投票来进⾏PK以确定是否变更内部投票(关于P K的逻辑会在步骤6中统⼀讲解),最终再将内部投票发送出去。 · 外部投票的选举轮次⼩于内部投票。 如果接收到的选票的选举轮次落后于服务器⾃身的,那么ZooKeeper就会直接忽略该外部投票,不做任何处理,并返回步骤4外部投票的选举轮次和内部投票⼀致。 这也是绝⼤多数投票的场景,如外部投票的选举轮次和内部投票⼀致的话,那么就开始进⾏选票PK。 总的来说,只有在同⼀个选举轮次的投票才是有效的投票。

  6. 选票PK。 在步骤5中提到,在收到来⾃其他服务器有效的外部投票后,就要进⾏选票PK了——也就是
    FastLeaderElection.totalOrderPredicate⽅法的核⼼逻辑。选票PK的⽬的是为了确定当前服务器是否需要变更投票,主要从选举轮次、ZXID和 SID 三个因素来考虑,具体条件如下:在选票 PK 的时候依次判断,符合任意⼀个条件就需要进⾏投票变更。 · 如果外部投票中被推举的Leader服务器的选举轮次⼤于内部投票,那么就需要进⾏投票变更。 · 如果选举轮次⼀致的话,那么就对⽐两者的ZXID。如果外部投票的ZXID⼤于内部投票,那么就需要进⾏投票变更。 · 如果两者的 ZXID ⼀致,那么就对⽐两者的SID。如果外部投票的 SID ⼤于内部投票,那么就需要进⾏投票变更。

  7. 变更投票。 通过选票PK后,如果确定了外部投票优于内部投票(所谓的“优于”,是指外部投票所推举的服务器更适合成为Leader),那么就进⾏投票变更——使⽤外部投票的选票信息来覆盖内部投票。变更完成后,再次将这个变更后的内部投票发送出去。

  8. 选票归档。 ⽆论是否进⾏了投票变更,都会将刚刚收到的那份外部投票放⼊“选票集合”recvset中进⾏归档。recvset⽤于记录当前服务器在本轮次的Leader选举中收到的所有外部投票——按照服务器对应的SID来区分,例如,{(1,vote1),(2,vote2),…}。

  9. 统计投票。 完成了选票归档之后,就可以开始统计投票了。统计投票的过程就是为了统计集群中是否已经有过半的服务器认可了当前的内部投票。如果确定已经有过半的服务器认可了该内部投票,则终⽌投票。否则返回步骤4。

  10. 更新服务器状态。 统计投票后,如果已经确定可以终⽌投票,那么就开始更新服务器状态。服务器会⾸先判断当前被过半服务器认可的投票所对应的Leader服务器是否是⾃⼰,如果是⾃⼰的话,那么就会将⾃⼰的服务器状态更新为 LEADING。如果⾃⼰不是被选举产⽣的 Leader 的话,那么就会根据具体情况来确定⾃⼰是FOLLOWING或是OBSERVING。 以上 10 个步骤,就是 FastLeaderElection 选举算法的核⼼步骤,其中步骤 4~9 会经过⼏轮循环,直到Leader选举产⽣。另外还有⼀个细节需要注意,就是在完成步骤9之后,如果统计投票发现已经有过半的服务器认可了当前的选票,这个时候,ZooKeeper 并不会⽴即进⼊步骤 10 来更新服务器状态,⽽是会等待⼀段时间(默认是 200 毫秒)来确定是否有新的更优的投票

zookeeper源码分析之集群模式服务端

执⾏流程图
在这里插入图片描述
源码分析
集群模式下启动所有的ZK节点启动⼊⼝都是QuorumPeerMain类的main⽅法。 main⽅法加载配置⽂件以后,最终会调⽤到QuorumPeer的start⽅法,来看下:

    @Override
   public synchronized void start() {

       // 校验serverid如果不在peer列表中,抛异常
       if (!getView().containsKey(myid)) {
           throw new RuntimeException("My id " + myid + " not in the peer list");
        }

       // 加载zk数据库:载入之前持久化的一些信息
       loadDataBase();

       // 启动连接服务端
       startServerCnxnFactory();
       try {
           adminServer.start();
       } catch (AdminServerException e) {
           LOG.warn("Problem starting AdminServer", e);
           System.out.println(e);
       }
       // 启动之后马上进行选举,主要是创建选举必须的环境,比如:启动相关线程
       startLeaderElection();

       // 执行选举逻辑
       super.start();
   }

我们已经知道了当⼀个节点启动时需要先发起选举寻找Leader节点,然后再根据Leader节点的事务信息进⾏同步,最后开始对外提供服务,这⾥我们先来看下初始化选举的逻辑,即上⾯的
startLeaderElection⽅法:

    synchronized public void startLeaderElection() {
       try {

           // 所有节点启动的初始状态都是LOOKING,因此这里都会是创建一张投自己为Leader的票
           if (getPeerState() == ServerState.LOOKING) {
               currentVote = new Vote(myid, getLastLoggedZxid(), getCurrentEpoch());
           }
       } catch(IOException e) {
           RuntimeException re = new RuntimeException(e.getMessage());
           re.setStackTrace(e.getStackTrace());
           throw re;
       }

       // if (!getView().containsKey(myid)) {
      //      throw new RuntimeException("My id " + myid + " not in the peer list");
        //}
        if (electionType == 0) {
            try {
                udpSocket = new DatagramSocket(myQuorumAddr.getPort());
                responder = new ResponderThread();
                responder.start();
            } catch (SocketException e) {
                throw new RuntimeException(e);
            }
        }
        //初始化选举算法,electionType默认为3
        this.electionAlg = createElectionAlgorithm(electionType);
    }

    @SuppressWarnings("deprecation")
    protected Election createElectionAlgorithm(int electionAlgorithm){
        Election le=null;

        //TODO: use a factory rather than a switch
        switch (electionAlgorithm) {
        case 0:
            le = new LeaderElection(this);
            break;
        case 1:
            le = new AuthFastLeaderElection(this);
            break;
        case 2:
            le = new AuthFastLeaderElection(this, true);
            break;
        case 3:
            //electionAlgorithm默认是3,直接走到这里
            qcm = createCnxnManager();
            //监听选举事件的listener
            QuorumCnxManager.Listener listener = qcm.listener;
            if(listener != null){
                //开启监听器
                listener.start();
                //初始化选举算法
                FastLeaderElection fle = new FastLeaderElection(this, qcm);
                //发起选举
                fle.start();
                le = fle;
            } else {
                LOG.error("Null listener when initializing cnx manager");
            }
            break;
        default:
            assert false;
        }
        return le;
    }

接下来,回到QuorumPeer类中start⽅法的最后⼀⾏super.start(),QuorumPeer本身也是⼀个线程类,⼀起来看下它的run⽅法:

    @Override
    public void run() {
        updateThreadName();

        LOG.debug("Starting quorum peer");
        try {
            jmxQuorumBean = new QuorumBean(this);
            MBeanRegistry.getInstance().register(jmxQuorumBean, null);
            for(QuorumServer s: getView().values()){
                ZKMBeanInfo p;
                if (getId() == s.id) {
                    p = jmxLocalPeerBean = new LocalPeerBean(this);
                    try {
                        MBeanRegistry.getInstance().register(p, jmxQuorumBean);
                    } catch (Exception e) {
                        LOG.warn("Failed to register with JMX", e);
                        jmxLocalPeerBean = null;
                    }
                } else {
                    RemotePeerBean rBean = new RemotePeerBean(s);
                    try {
                        MBeanRegistry.getInstance().register(rBean, jmxQuorumBean);
                        jmxRemotePeerBean.put(s.id, rBean);
                    } catch (Exception e) {
                        LOG.warn("Failed to register with JMX", e);
                    }
                }
            }
        } catch (Exception e) {
            LOG.warn("Failed to register with JMX", e);
            jmxQuorumBean = null;
        }

        try {
            /*
             * Main loop
             */
            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());
                        } catch (Exception e) {
                            LOG.warn("Unexpected exception", e);
                            setPeerState(ServerState.LOOKING);
                        } finally {
                            // If the thread is in the the grace period, interrupt
                            // to come out of waiting.
                            roZkMgr.interrupt();
                            roZk.shutdown();
                        }
                    } else {
                        try {
                           reconfigFlagClear();
                            if (shuttingDownLE) {
                               shuttingDownLE = false;
                               startLeaderElection();
                               }
                            //寻找Leader节点
                            setCurrentVote(makeLEStrategy().lookForLeader());
                        } catch (Exception e) {
                            LOG.warn("Unexpected exception", e);
                            setPeerState(ServerState.LOOKING);
                        }                        
                    }
                    break;
                case OBSERVING:
                    try {
                        LOG.info("OBSERVING");
                        //当前节点启动模式为Observer
                        setObserver(makeObserver(logFactory));
                        observer.observeLeader();
                    } catch (Exception e) {
                        LOG.warn("Unexpected exception",e );
                    } finally {
                        observer.shutdown();
                        setObserver(null);  
                       updateServerState();
                    }
                    break;
                case FOLLOWING:
                    try {
                       LOG.info("FOLLOWING");
                        //当前节点启动模式为Follower
                        setFollower(makeFollower(logFactory));
                        follower.followLeader();
                    } catch (Exception e) {
                       LOG.warn("Unexpected exception",e);
                    } finally {
                       follower.shutdown();
                       setFollower(null);
                       updateServerState();
                    }
                    break;
                case LEADING:
                    LOG.info("LEADING");
                    try {
                        //当前节点启动模式为Leader 
                        setLeader(makeLeader(logFactory));
                        leader.lead();
                        setLeader(null);
                    } catch (Exception e) {
                        LOG.warn("Unexpected exception",e);
                    } finally {
                        if (leader != null) {
                            leader.shutdown("Forcing shutdown");
                            setLeader(null);
                        }
                        updateServerState();
                    }
                    break;
                }
                start_fle = Time.currentElapsedTime();
            }
        } finally {
            LOG.warn("QuorumPeer main thread exited");
            MBeanRegistry instance = MBeanRegistry.getInstance();
            instance.unregister(jmxQuorumBean);
            instance.unregister(jmxLocalPeerBean);

            for (RemotePeerBean remotePeerBean : jmxRemotePeerBean.values()) {
                instance.unregister(remotePeerBean);
            }

            jmxQuorumBean = null;
            jmxLocalPeerBean = null;
            jmxRemotePeerBean = null;
        }
    }

节点初始化的状态为LOOKING,因此启动时直接会调⽤lookForLeader⽅法发起Leader选举,⼀起看下:

    /**
     * Starts a new round of leader election. Whenever our QuorumPeer
     * changes its state to LOOKING, this method is invoked, and it
     * sends notifications to all other peers.
     */
    public Vote lookForLeader() throws InterruptedException {
        try {
            self.jmxLeaderElectionBean = new LeaderElectionBean();
            MBeanRegistry.getInstance().register(
                    self.jmxLeaderElectionBean, self.jmxLocalPeerBean);
        } catch (Exception e) {
            LOG.warn("Failed to register with JMX", e);
            self.jmxLeaderElectionBean = null;
        }
        if (self.start_fle == 0) {
           self.start_fle = Time.currentElapsedTime();
        }
        try {
            HashMap<Long, Vote> recvset = new HashMap<Long, Vote>();

            HashMap<Long, Vote> outofelection = new HashMap<Long, Vote>();

            int notTimeout = finalizeWait;

            synchronized(this){
                logicalclock.incrementAndGet();
                updateProposal(getInitId(), getInitLastLoggedZxid(), getPeerEpoch());
            }

            LOG.info("New election. My id =  " + self.getId() +
                    ", proposed zxid=0x" + Long.toHexString(proposedZxid));
            //向所有投票节点发送⾃⼰的投票信息
            sendNotifications();

            /*
             * Loop in which we exchange notifications until we find a leader
             */

            while ((self.getPeerState() == ServerState.LOOKING) &&
                    (!stop)){
                /*
                 * Remove next notification from queue, times out after 2 times
                 * the termination time
                 */
                //读取各个节点返回的投票信息
                Notification n = recvqueue.poll(notTimeout,
                        TimeUnit.MILLISECONDS);

                /*
                 * Sends more notifications if haven't received enough.
                 * Otherwise processes new notification.
                 */
                //超时重发
                if(n == null){
                    // 判断自己是否和其他服务器断开连接
                    if(manager.haveDelivered()){
                        // 没有断开,发送本身的投票信息
                        //如果前⾯待发送的消息已经全部发送,则重新发送
                        sendNotifications();
                    } else {
                        //否则尝试与各个节点建⽴连接
                        manager.connectAll();
                    }

                    /*
                     * Exponential backoff
                     */
                    //退避算法修改下次等待时间
                    int tmpTimeOut = notTimeout*2;
                    notTimeout = (tmpTimeOut < maxNotificationInterval?
                            tmpTimeOut : maxNotificationInterval);
                    LOG.info("Notification time out: " + notTimeout);
                }
                // 处理外部投票(判断选举轮次)
                else if (validVoter(n.sid) && validVoter(n.leader)) {
                    /*
                     * Only proceed if the vote comes from a replica in the current or next
                     * voting view for a replica in the current or next voting view.
                     */
                    switch (n.state) {
                    case LOOKING:
                        // If notification > current, replace and send messages out
                        if (n.electionEpoch > logicalclock.get()) {//其选举周期⼤于逻辑时钟
                            // 重新赋值逻辑时钟
                            logicalclock.set(n.electionEpoch);
                            // 清空所有接收到的所有选票
                            recvset.clear();
                            //两个节点根据epoch,zxid,serverId来判断新的投票信息
                            if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                    getInitId(), getInitLastLoggedZxid(), getPeerEpoch())) {// 进⾏PK,选出较优的服务器
                                updateProposal(n.leader, n.zxid, n.peerEpoch);
                            } else {// ⽆法选出较优的服务器
                                // 更新选票
                                updateProposal(getInitId(),
                                        getInitLastLoggedZxid(),
                                        getPeerEpoch());
                            }
                            //修改选举周期以及投票信息,发起新⼀轮投票
                            // 发送本服务器的内部选票消息
                            sendNotifications();
                        } else if (n.electionEpoch < logicalclock.get()) { // 选举周期⼩于逻辑时钟,不做处理,直接忽略
                            if(LOG.isDebugEnabled()){
                                LOG.debug("Notification election epoch is smaller than logicalclock. n.electionEpoch = 0x"
                                        + Long.toHexString(n.electionEpoch)
                                        + ", logicalclock=0x" + Long.toHexString(logicalclock.get()));
                            }
                            //这⾥的break是跳出switch语句,别跟循环弄混
                            break;
                        } else if (totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                proposedLeader, proposedZxid, proposedEpoch)) {// PK,选出较优的服务器
                            // 更新选票
                            //如果对⽅的epoch,zxid,serverId⽐⾃⼰⼤
                            //则更新⾃⼰的投票给n的投票节点
                            updateProposal(n.leader, n.zxid, n.peerEpoch);
                            // 发送消息
                            sendNotifications();
                        }

                        if(LOG.isDebugEnabled()){
                            LOG.debug("Adding vote: from=" + n.sid +
                                    ", proposed leader=" + n.leader +
                                    ", proposed zxid=0x" + Long.toHexString(n.zxid) +
                                    ", proposed election epoch=0x" + Long.toHexString(n.electionEpoch));
                        }
                        // recvset⽤于记录当前服务器在本轮次的Leader选举中收到的所有外部投票
                        //把节点的投票信息记录下
                        recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch));
                        //统计投票信息,判断当前选举是否可以结束,也就是收到的票数信息已经⾜够确认Leader
                        if (termPredicate(recvset,
                                new Vote(proposedLeader, proposedZxid,
                                        logicalclock.get(), proposedEpoch))) {

                            // Verify if there is any change in the proposed leader

                            while((n = recvqueue.poll(finalizeWait,
                                    TimeUnit.MILLISECONDS)) != null){// 遍历已经接收的投票集合

                                if(totalOrderPredicate(n.leader, n.zxid, n.peerEpoch,
                                        proposedLeader, proposedZxid, proposedEpoch)){// 选票有变更,⽐之前提议的Leader有更好的选票加⼊
                                    // 将更优的选票放在recvset中
                                    recvqueue.put(n);
                                    break;
                                }
                            }

                            /*
                             * This predicate is true once we don't read any new
                             * relevant message from the reception queue
                             */
                            //如果没有多余的投票信息则可以结束本次选举周期
                            if (n == null) {// 表示之前提议的Leader已经是最优的
                                // 设置服务器状态
                                self.setPeerState((proposedLeader == self.getId()) ?
                                        ServerState.LEADING: learningState());
                                // 最终的选票
                                Vote endVote = new Vote(proposedLeader,
                                        proposedZxid, proposedEpoch);
                                // 清空recvqueue队列的选票
                                leaveInstance(endVote);
                                // 返回选票
                                return endVote;
                            }
                        }
                        break;
                    case OBSERVING:
                        //Observer节点不参与投票,忽略
                        LOG.debug("Notification from observer: " + n.sid);
                        break;
                    case FOLLOWING:
                    case LEADING:
                        /*
                         * Consider all notifications from the same epoch
                         * together.
                         */
                        //如果周期相同,说明当前节点参与了这次选举
                        if(n.electionEpoch == logicalclock.get()){
                            //保存投票信息
                            recvset.put(n.sid, new Vote(n.leader, n.zxid, n.electionEpoch, n.peerEpoch));
                            //判断当前节点收到的票数是否可以结束选举
                            if(termPredicate(recvset, new Vote(n.leader,
                                            n.zxid, n.electionEpoch, n.peerEpoch, n.state))
                                            && checkLeader(outofelection, n.leader, n.electionEpoch)) {
                                self.setPeerState((n.leader == self.getId()) ?
                                        ServerState.LEADING: learningState());

                                Vote endVote = new Vote(n.leader, n.zxid, n.peerEpoch);
                                leaveInstance(endVote);
                                return endVote;
                            }
                        }

                        /*
                         * Before joining an established ensemble, verify that
                         * a majority are following the same leader.
                         * Only peer epoch is used to check that the votes come
                         * from the same ensemble. This is because there is at
                         * least one corner case in which the ensemble can be
                         * created with inconsistent zxid and election epoch
                         * info. However, given that only one ensemble can be
                         * running at a single point in time and that each 
                         * epoch is used only once, using only the epoch to 
                         * compare the votes is sufficient.
                         * 
                         * @see https://issues.apache.org/jira/browse/ZOOKEEPER-1732
                         */
                        //把Leader跟Follower的投票信息加⼊outofelection,确认下它们 的信息是否⼀致
                        outofelection.put(n.sid, new Vote(n.leader, 
                                IGNOREVALUE, IGNOREVALUE, n.peerEpoch, n.state));
                        if (termPredicate(outofelection, new Vote(n.leader,
                                IGNOREVALUE, IGNOREVALUE, n.peerEpoch, n.state))
                                && checkLeader(outofelection, n.leader, IGNOREVALUE)) {
                            synchronized(this){
                                logicalclock.set(n.electionEpoch);
                                self.setPeerState((n.leader == self.getId()) ?
                                        ServerState.LEADING: learningState());
                            }
                            Vote endVote = new Vote(n.leader, n.zxid, n.peerEpoch);
                            leaveInstance(endVote);
                            return endVote;
                        }
                        break;
                    default:
                        LOG.warn("Notification state unrecoginized: " + n.state
                              + " (n.state), " + n.sid + " (n.sid)");
                        break;
                    }
                } else {
                    if (!validVoter(n.leader)) {
                        LOG.warn("Ignoring notification for non-cluster member sid {} from sid {}", n.leader, n.sid);
                    }
                    if (!validVoter(n.sid)) {
                        LOG.warn("Ignoring notification for sid {} from non-quorum member sid {}", n.leader, n.sid);
                    }
                }
            }
            return null;
        } finally {
            try {
                if(self.jmxLeaderElectionBean != null){
                    MBeanRegistry.getInstance().unregister(
                            self.jmxLeaderElectionBean);
                }
            } catch (Exception e) {
                LOG.warn("Failed to unregister with JMX", e);
            }
            self.jmxLeaderElectionBean = null;
            LOG.debug("Number of connection processing threads: {}",
                    manager.getConnectionThreadCount());
        }
    }

经过上⾯的发起投票,统计投票信息最终每个节点都会确认⾃⼰的身份,节点根据类型的不同会执⾏以下逻辑:

  1. 如果是Leader节点,⾸先会想其他节点发送⼀条NEWLEADER信息,确认⾃⼰的身份,等到各个节点的ACK消息以后开始正式对外提供服务,同时开启新的监听器,处理新节点加⼊的逻辑。
  2. 如果是Follower节点,⾸先向Leader节点发送⼀条FOLLOWERINFO信息,告诉Leader节点⾃⼰已处理的事务的最⼤Zxid,然后Leader节点会根据⾃⼰的最⼤Zxid与Follower节点进⾏同步,如果Follower节点落后的不多则会收到Leader的DIFF信息通过内存同步,如果Follower节点落后的很多则会收到SNAP通过快照同步,如果Follower节点的Zxid⼤于Leader节点则会收到TRUNC信息忽略多余的事务。
  3. 如果是Observer节点,则与Follower节点相同
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值