First call *setup(ElectionContext) to ensure the election process is in it'd.

 
 Next calljoinElection(ElectionContext) to start the leader election. 
 
The implementation follows the classic ZooKeeper recipe of creating an ephemeral, sequential node for each candidate and then looking at the set of such nodes -
 
 if the created node is the lowest sequential node, the candidate that created the node is the leader. 
 
If not, the candidate puts a watch on the next lowest node it finds, and if that node goes down, starts the whole process over by checking if it's the lowest sequential node, etc.
 
 
org.apache.solr.cloud.LeaderElector实现选举leader的逻辑。
 
首先调用setup方法保证选举初始化,主要是保证写在zookeeper上的信息节点存在。
  
[java]  
/** 
  * Set up any ZooKeeper nodes needed for leader election. 
  */  
 public void setup(final ElectionContext context) throws InterruptedException,  
     KeeperException {  
   String electZKPath = context.electionPath + LeaderElector.ELECTION_NODE;  
    
   zkCmdExecutor.ensureExists(electZKPath, zkClient);  
 }  
  
 
加入选举队列实现
每个shard进入集群后,会在zookeeper上注册一个序列号类似,n_0000000001 or n_0000000003
应该是以active的状态记录,每次进入选举的队列里,都会先取得新的序列号,先进序列号越小,这个序列号对于选举leader很重要,每次选举leader会从最小的序列号做为leader,初次创建的时候,就会作为首选 的leader。
至于每次有leader发生故障的时候,看检查自己是不是最小的那个序列号,如果是,则可以做一下leader的初始化工作,如果不是,至以当前第二小的做为新的leader看齐。
挂掉的leader的shard再成功起来的时候,照道理应该是改为最大的序列号,变为followe者。
加入选举队列实现主要代码 :(返回选举后的leader序列号)
 [java]  
public int joinElection(ElectionContext context) throws KeeperException, InterruptedException, IOException {  
    final String shardsElectZkPath = context.electionPath + LeaderElector.ELECTION_NODE;  
      
    long sessionId = zkClient.getSolrZooKeeper().getSessionId();  
    String id = sessionId + "-" + context.id;  
    String leaderSeqPath = null;  
    boolean cont = true;  
    int tries = 0;  
    while (cont) {  
      try {  
      //取出shard片对应的leader seq信息。  
        leaderSeqPath = zkClient.create(shardsElectZkPath + "/" + id + "-n_", null,  
            CreateMode.EPHEMERAL_SEQUENTIAL, false);  
        context.leaderSeqPath = leaderSeqPath;  
        cont = false;  
      } catch (ConnectionLossException e) {  
        // we don't know if we made our node or not...  
        List<String> entries = zkClient.getChildren(shardsElectZkPath, null, true);  
        //检查自己是否在这个选 举的队列里  
        boolean foundId = false;  
        for (String entry : entries) {  
          String nodeId = getNodeId(entry);  
          if (id.equals(nodeId)) {  
            // we did create our node...  
            foundId  = true;  
            break;  
          }  
        }  
        //没找到则跳出微循环,如果重试已超过20次则抛出异常  
        if (!foundId) {  
  
          cont = true;  
          if (tries++ > 20) {  
            throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,  
                "", e);  
          }  
          try {  
            Thread.sleep(50);  
          } catch (InterruptedException e2) {  
            Thread.currentThread().interrupt();  
          }  
        }  
  
      } catch (KeeperException.NoNodeException e) {  
        // we must have failed in creating the election node - someone else must  
        // be working on it, lets try again  
        if (tries++ > 20) {  
          throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,  
              "", e);  
        }  
        cont = true;  
        try {  
          Thread.sleep(50);  
        } catch (InterruptedException e2) {  
          Thread.currentThread().interrupt();  
        }  
      }  
    }  
    //得到leader的seq,并检查自己是不是leader  
    int seq = getSeq(leaderSeqPath);  
    checkIfIamLeader(seq, context, false);  
      
    return seq;  
  }  
 

solrCloud选举leader的逻辑分析的更多相关文章

  1. Raft协议实战之Redis Sentinel的选举Leader源码解析

    这可能是我看过的写的最详细的关于redis 选举的文章了, 原文链接 Raft协议是用来解决分布式系统一致性问题的协议,在很长一段时间,Paxos被认为是解决分布式系统一致性的代名词.但是Paxos难 ...

  2. kafka备份机制——zk选举leader,leader在broker里负责备份

    Kafka架构 如上图所示,一个典型的kafka集群中包含若干producer(可以是web前端产生的page view,或者是服务器日志,系统CPU.memory等),若干broker(Kafka支 ...

  3. zookeeper 选举leader详解

    一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...

  4. 【分布式】Zookeeper的Leader选举

    一.前言 前面学习了Zookeeper服务端的相关细节,其中对于集群启动而言,很重要的一部分就是Leader选举,接着就开始深入学习Leader选举. 二.Leader选举 2.1 Leader选举概 ...

  5. Ceph剖析:Leader选举

    作者:吴香伟 发表于 2014/09/11 版权声明:可以任意转载,转载时务必以超链接形式标明文章原始出处和作者信息以及版权声明 Paxos算法存在活锁问题.从节点中选出Leader,然后将所有对数据 ...

  6. 第四章 Leader选举算法分析

    Leader选举 学习leader选举算法,主要是从选举概述,算法分析与源码分析(后续章节写)三个方面进行. Leader选举概述 服务器启动时期的Leader选举 选举的隐式条件便是ZooKeepe ...

  7. 基于库zkclient 的leader选举代码实现

    利用了zookeeper临时节点,在当连接或session断掉时被删除这一特性来做选举.(简单简单互斥锁) 查了下网上的做法. 大致流程: <1>判定是否存在/wzgtest路径 < ...

  8. zookeeper leader选举算法源码

    服务器状态 在QuorumPeer中有定义,这个类是一个线程. LOOKING:寻找Leader状态.处于该状态时,它会认为当前集群中没有Leader,进入选举流程. FOLLOWING: LEADI ...

  9. kafka知识体系-kafka设计和原理分析-kafka leader选举

    kafka leader选举 一条消息只有被ISR中的所有follower都从leader复制过去才会被认为已提交.这样就避免了部分数据被写进了leader,还没来得及被任何follower复制就宕机 ...

随机推荐

  1. GIS的核心价值——服务

    注:本观点仅代表个人观点,与任何人员或者组织无任何关系,如有雷同或者相似之处,只能说明我们对GIS的理解相同,无抄袭之嫌.     写这个题目,源于前两天接到的一个电话面试时候问道的一个面试题.当时, ...

  2. 使用tor实现匿名扫描/SSH登录

    你要做坏事时,最先应该想到匿名.扫描网站/主机,或利用漏洞:甚至在大天朝发帖都有风险,为了防止半夜鬼敲门,我们可以使用tor实现匿名. 如果你不知道tor是什么,看:https://zh.wikipe ...

  3. python 中出现 “IndentationError: expected an indented block” 问题

    python 学习 在定义Python函数的时候如下 >>>def hello() . . .print "hello" 这样会报错的,报错如下: Indenta ...

  4. BZOJ - 2460 :元素 (贪心&线性基)

    相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石.一般地,矿石越多则法力越强,但物极必反:有时,人们 ...

  5. 使用Metaspoit攻击MS08-067

    kali视频学习请看 http://www.cnblogs.com/lidong20179210/p/8909569.html 使用Metaspoit攻击MS08-067 MS08-067漏洞的全称为 ...

  6. Redis设计与实现 (三): 字典

     哈希表 结构定义dict.h/dictht /* * 哈希表 * * 每个字典都使用两个哈希表,从而实现渐进式 rehash . */ typedef struct dictht { // 哈希表数 ...

  7. matlab中hdl coder 的使用

    今天摸索了一下hdl coder的使用方法,各个步骤主要是照猫画虎,有些地方还是不理解,先总结一下: 1.要想调用quartus或者Xilinx综合布局布线需要先设置,设置的方法有两种,命令窗口输入 ...

  8. Jam的计数法

    Jam的计数法 题目描述 Description Jam是个喜欢标新立异的科学怪人.他不使用阿拉伯数字计数,而是使用小写英文字母计数,他觉得这样做,会使世界更加丰富多彩.在他的计数法中,每个数字的位数 ...

  9. 使用appassembler-maven-plugin插件生成启动脚本

    appassembler-maven-plugin可以自动生成跨平台的启动脚本,省去了手工写脚本的麻烦,而且还可以生成jsw的后台运行程序. 首先pom引入相关依赖 <build> < ...

  10. mysql 统计sql

    1.按照月份统计数据 SELECT DATE_FORMAT(d.create_time,'%Y-%m') months,COUNT(id) AS scannum FROM detail d GROUP ...