solrCloud选举leader的逻辑分析

转贴请声明原文:http://blog.csdn.net/duck_genuine/article/details/8491901

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上的信息节点存在。

 /*** 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序列号)

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 againif (tries++ > 20) {throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR,"", e);}cont = true;try {Thread.sleep(50);} catch (InterruptedException e2) {Thread.currentThread().interrupt();}}}//得到leader的seq,并检查自己是不是leaderint seq = getSeq(leaderSeqPath);checkIfIamLeader(seq, context, false);return seq;}

转贴请声明原文:http://blog.csdn.net/duck_genuine/article/details/8491901

<a href="https://plus.google.com/110033444156027673423?rel=author">Google</a>

solrCloud选举leader的逻辑分析相关推荐

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

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

  2. Apache ZooKeeper - 选举Leader源码流程深度解析

    文章目录 流程图 Round Leader 选举 服务启动时的 Leader 选举 发起投票 接收投票 统计投票 Leader 崩溃触发的 Leader 选举 变更服务器状态 发起投票 接收投票 统计 ...

  3. zookeeper的集群内部选举leader

    zookeeper选举leader的情形有两种,第一种是集群刚启动的时候,第二种是集群运行中leader脑裂,导致集群中半数follower与leader心跳检测不到,这些情况下需要选举leader ...

  4. ETCD频繁选举leader

    问题:节点负载过高,etcd心跳超时,重新选举leader,导致etcd服务或某些组件不可用 解决:加长etcd心跳检测时间 结果:

  5. Nacos无法选举leader

    Nacos无法选举leader 虚机部署3节点nacos集群 集群状态异常 登录控制台发现所有节点状态均为follower 服务注册异常 查看日志 解决方法 虚机部署3节点nacos集群 参考博客:h ...

  6. 使用Zookeeper实现leader选举-Leader Latch

    参与选举的所有节点,会创建一个顺序节点,其中最小的节点会设置为master节点, 没抢到Leader的节点都监听前一个节点的删除事件,在前一个节点删除后进行重新抢主,当master节点手动调用clos ...

  7. java leader 选举_简述ZK的fastleaderelection选举leader的算法

    假设有三个server,server1,server2,server3 服务启动时期的选举 1.每个Server发出一个投票 投票包含(myid,ZXID),刚开始server都是投自己的,即Serv ...

  8. ceph monitor 选举leader和peon的过程

    ceph中monitor 可以分为leader节点和peon节点,leader节点是分解rank值来决定,rank值又和ip地址有关. 在ceph/mon/elector.cc 这个文件中实现的Ele ...

  9. 面试官:说说你对ZooKeeper集群与Leader选举的理解?

    作者:TalkingData 史天舒 来自:TalkingData ZooKeeper是一个开源分布式协调服务.分布式数据一致性解决方案.可基于ZooKeeper实现命名服务.集群管理.Master选 ...

最新文章

  1. 找回 macOS Sierra 中的“任何来源”选项
  2. 函数重载和覆盖(重写)的区别
  3. OkHttpClient源码分析(五)—— ConnectInterceptor和CallServerInterceptor
  4. axios拦截器_78.1K 的 Axios 项目有哪些值得借鉴的地方
  5. C++的三大特性之一继承
  6. 类的静态成员函数和静态成员变量的使用
  7. 谈一谈对旋转矩阵的理解
  8. 我悲惨的人生,该死的UPX壳,谁能救救我
  9. nvidia显卡驱动,cuda,和cudnn版本
  10. java的本质_Java线程本质
  11. axure数据报表元件库_axure图表元件库 axure自制的组件库(包括数据组件)
  12. 量化选股模型—资金流模型
  13. c语言 约分最简分式
  14. 优麒麟搜狗输入法简繁切换
  15. 移动端瀑布流/信息流布局以及交互
  16. 南昌工学院计算机科学与技术专业,南昌工程学院特色专业介绍_计算机科学与技术_专业课程介绍_专业排名_就业方向...
  17. 文件指纹修改工具 Hash Modifier
  18. Unity 之 Ping类简析尝试使用
  19. 华为5g cpe 虚拟服务器,购买华为5G CPE Pro 2之前你需要知道这些
  20. SQL-常用SQL语句

热门文章

  1. 查询您的 Windows 11 产品密钥
  2. 原生js——实现ios辅助触控的悬浮球案例
  3. LaTeX在线公式编辑
  4. Partition Magic错误信息与解决方法
  5. 救助:Word文档页面上下方黑线如何去除-非页眉页角
  6. 前端 --- HTML
  7. PyPI 仓库被曝多个 typosquatting 库,可触发供应链攻击
  8. 2023第十六届“认证杯”数学建模网络挑战赛第一阶段比赛经历分享
  9. jquery回弹_jQuery实现移动端下拉展现新的内容回弹动画
  10. 轻松无广告:推荐一款高效提醒软件