核心枚举

public enum ServerState {
LOOKING, FOLLOWING, LEADING, OBSERVING;
}
zookeeper服务器状态:刚启动LOOKING,follower是FOLLOWING,leader是LEADING,observer是OBSERVING;
public enum LearnerType {
PARTICIPANT, OBSERVER;
}

简单来说,zookeeper启动的核心类是QuorumPeerMain,启动之后会加载配置,同时启动QuorumPeer,QuorumPeer会从本地数据目录加载数据,然后开始选举,选举的核心类是FastLeaderElection,选举完成之后会设置状态,根据状态的不同,启动不同的类,比如Leader、Follower、Observer,leader会设置一个新的epoch,同时会等待follower向自己注册,注册完成后follower会向leader同步最新的数据,而leader则定期ping所有的follower,一旦存活的follower不足半数,leader会shutdown,然后触发集群的重新选举。

具体代码以及调用如下:

org.apache.zookeeper.server.quorum.QuorumPeerMain

         initializeAndRun

                  runFromConfig

                          org.apache.zookeeper.server.quorum.QuorumPeer extends ZooKeeperThread

                                   initialize

                                   start

                                            loadDataBase

                                            org.apache.zookeeper.server.ServerCnxnFactory

                                                     start

                                            startLeaderElection

                                                     org.apache.zookeeper.server.quorum.QuorumPeer.ResponderThread

                                                             start

                                                     createElectionAlgorithm

                                            run

                                                     org.apache.zookeeper.server.quorum.FastLeaderElection implements Election

                                                             lookForLeader

                                                                      sendNotifications

                                                                      totalOrderPredicate

                                                                      termPredicate

                                                                               org.apache.zookeeper.server.quorum.flexible.QuorumMaj

                                                                                        containsQuorum

                                                                      QuorumPeer.setPeerState

                                                             1 org.apache.zookeeper.server.quorum.Leader extends Learner

                                                                      lead

                                                                               org.apache.zookeeper.server.quorum.Leader.LearnerCnxAcceptor extends ZooKeeperThread

                                                                                        run

                                                                                                 org.apache.zookeeper.server.quorum.LearnerHandler extends ZooKeeperThread

                                                                                                         start

                                                                                                                  receive Leader.FOLLOWERINFO

                                                                               epoch++

                                                                               setZxid

                                                                               setCurrentEpoch

                                                                               waitForNewLeaderAck

                                                                               startZkServer

                                                                               loop

                                                                                        org.apache.zookeeper.server.quorum.LearnerHandler

                                                                                                ping

                                                                                                         org.apache.zookeeper.server.quorum.LearnerHandler.SyncLimitCheck

                                                                                                                  check

                                                                                                         shutdown

                                                                                                                  Leader.removeLearnerHandler

                                                                                        containsQuorum

                                                                                        shutdown

                                                             2 org.apache.zookeeper.server.quorum.Follower extends Learner

                                                                      followLeader

                                                                               findLeader

                                                                               connectToLeader

                                                                               registerWithLeader

                                                                                        send Leader.FOLLOWERINFO

                                                                               syncWithLeader

                                                             3 org.apache.zookeeper.server.quorum.Observer extends Learner

                                                                      observeLeader

选举代码解析请见  大数据基础之Zookeeper(3)选举算法

【原创】大数据基础之Zookeeper(2)源代码解析的更多相关文章

  1. 【原创】大数据基础之Zookeeper(1)介绍、安装及使用

    zookeeper3.4.11 http://zookeeper.apache.org/ 一 简介 ZooKeeper is a centralized service for maintaining ...

  2. 【原创】大数据基础之Zookeeper(4)应用场景

    1 集群配置管理 应用服务器的配置通常会放到properties文件中,格式为: system1.module2.prop3=value4 然后启动的时候加载,这样带来的问题是启动后无法修改,想修改必 ...

  3. 【原创】大数据基础之Zookeeper(3)选举算法

    提到zookeeper选举算法,就不得不提Paxos算法,因为zookeeper选举算法是Paxos算法的一个变种: Paxos要解决的问题是:在一个分布式网络环境中有众多的参与者,但是每个参与者都不 ...

  4. 决战大数据之三-Apache ZooKeeper Standalone及复制模式安装及测试

    决战大数据之三-Apache ZooKeeper Standalone及复制模式安装及测试 [TOC] Apache ZooKeeper 单机模式安装 创建hadoop用户&赋予sudo权限, ...

  5. 大数据篇:Zookeeper

    Zookeeper 1 Zookeeper概念 Zookeeper是什么 是一个基于观察者设计模式的分布式服务管理框架,它负责和管理需要关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Z ...

  6. 【原创】大数据基础之Impala(1)简介、安装、使用

    impala2.12 官方:http://impala.apache.org/ 一 简介 Apache Impala is the open source, native analytic datab ...

  7. 【原创】大数据基础之词频统计Word Count

    对文件进行词频统计,是一个大数据领域的hello word级别的应用,来看下实现有多简单: 1 Linux单机处理 egrep -o "\b[[:alpha:]]+\b" test ...

  8. 【原创】大数据基础之Benchmark(2)TPC-DS

    tpc 官方:http://www.tpc.org/ 一 简介 The TPC is a non-profit corporation founded to define transaction pr ...

  9. 大数据基础知识问答----spark篇,大数据生态圈

    Spark相关知识点 1.Spark基础知识 1.Spark是什么? UCBerkeley AMPlab所开源的类HadoopMapReduce的通用的并行计算框架 dfsSpark基于mapredu ...

随机推荐

  1. JS--编码规范

    1. 请修复给定的 js 代码中,函数定义存在的问题 function functions(flag) { if (flag) { function getValue() { return 'a'; ...

  2. 599. Minimum Index Sum of Two Lists(easy)

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  3. Python 属性描述符和属性的查找过程

    属性描述符可以用来控制给属性赋值的时候的一些行为 import numbers class IntField: def __get__(self, instance, owner): return s ...

  4. 03-JavaScript之数据类型

    JavaScript之数据类型 1.介绍 JavaScript数据类型分为两类:原始类型(primitive type)和对象类型(object type) 2.原始类型 数字 - number.字符 ...

  5. ssh远程 和 上传/下载工具

    常用的ssh远程工具有: putty  : 软件体积小,开源免费. xshell  : 功能强大,亦有免费试用版本 SecureCRT  : 功能强大 ftp  : 该软件用于上传下载文件 通过ssh ...

  6. IntentService+BroadcastReceiver 实现定时任务

    效果图: AlramIntentService package com.example.admin.water; import android.app.AlarmManager;import andr ...

  7. PyCharm专业版的安装与破解

    1.下载pycharm:https://www.jetbrains.com/pycharm/download/#section=windows 2.这是下载好的文件,双击运行即可 3.选择安装路径 4 ...

  8. js字符串轉數組,數組轉字符串

    字符串轉數組:split(',') 數組轉字符串:join(‘,’) https://www.cnblogs.com/woodk/p/5714329.html

  9. BZOJ3498PA2009 Cakes——三元环

    题目描述 N个点m条边,每个点有一个点权a.对于任意一个三元环(j,j,k)(i<j<k),它的贡献为max(ai,aj,ak) 求所有三元环的贡献和.N<100000,,m< ...

  10. 题解:[APIO2007]风铃

    你需要选一个满足下面两个条件的风铃:(1) 所有的玩具都在同一层(也就是说,每个玩具到天花板之间的杆的个数是一样的)或至多相差一层.(2) 对于两个相差一层的玩具,左边的玩具比右边的玩具要更靠下一点. ...