zoukankan      html  css  js  c++  java
  • zookeeper 选举和同步

    节点状态:

    // org.apache.zookeeper.server.quorum.QuorumPeer.ServerState
    public enum ServerState {
        LOOKING, FOLLOWING, LEADING, OBSERVING;
    }

    测试环境可以用 2 个节点组成集群。2 个节点的集群中,节点需要得到 2 票,才能当选为 leader。

    假定 2 个节点的 id 分别为 1 和 2,以 FastLeaderElection 为例,描述选举过程:
    开始, 2 个节点均处于 LOOKING 状态,投票进行选举,2 个节点分别为自己投票,这样均只得 1 票,无法选举出 leader。
    开始下一轮选举,节点 1 发现节点 2 的 id 比自己大,于是给节点 2 投票,随之节点 2 成为 leader。

    投票信息
    FastLeaderElection.Notification

    选举 leader
    FastLeaderElection.lookForLeader

    比较 epoch,zxid,id。判断是否要更新当前节点的投票信息
    FastLeaderElection.totalOrderPredicate

    计票,判断选举是否结束
    FastLeaderElection.termPredicate

    选举完成后的同步

    QuorumPeer.run() 代码片段:

    case FOLLOWING:
        try {
            LOG.info("FOLLOWING");
            setFollower(makeFollower(logFactory));
            follower.followLeader();
        } catch (Exception e) {
            LOG.warn("Unexpected exception",e);
        } finally {
            follower.shutdown();
            setFollower(null);
            setPeerState(ServerState.LOOKING);
        }
        break;

    Follower.followLeader() 代码片段:

    syncWithLeader(newEpochZxid);                
    QuorumPacket qp = new QuorumPacket();
    while (self.isRunning()) {
        readPacket(qp);
        processPacket(qp);
    }

    follower 无限 while 循环。

  • 相关阅读:
    ubuntu 12.04 配置iscsi共享及挂载iscsi共享
    python_数据类型
    python_基本操作
    shell习题第5题:批量更改文件后缀名
    shell习题第4题:监控ip地址存活
    shell习题第3题:统计内存大小
    shell习题第2题:统计ip访问量
    shell习题第1题:每日一文件
    IIS网站的应用程序与虚拟目录的区别及应用
    http状态码
  • 原文地址:https://www.cnblogs.com/allenwas3/p/9287996.html
Copyright © 2011-2022 走看看