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. 如何构建一个分布式爬虫:实战篇
  2. python函数每日一讲 - cmp(x,y)
  3. CentOS 编译Hadoop 2.6 32位
  4. 圆形渐变shader_Flutter 中渐变的高级用法
  5. 杭电 1284 钱币兑换问题【完全背包求方案总数】
  6. Java集合(7):散列与散列码
  7. 做不完了吧,做不出了吧!
  8. 组策略复制失败排错思路实例
  9. 《学习OpenCV3》第4章 图像和大型数组类型(持续更新)
  10. XFBAY学习笔记=++=实验十三 交换机和路由器组合实验
  11. 犯罪与健康的统计关系
  12. 做人好难,做好人更难,还是做猪吧!
  13. 如何下载Java API文档?
  14. 2019中国五大新兴制造业迁徙路径及产业发展趋势全景
  15. 如何对matlab .m代码文件进行加密
  16. DCDC中电感的计算
  17. echart各种显示数据的格式化
  18. PC与IOS outlook客户端配置大全——(163邮箱、QQ邮箱、谷歌gmail邮箱)
  19. 亚马逊多账号防关联的解决方式
  20. Python v / s PHP:2019年选择哪种编程语言?

热门文章

  1. 长图预警,全网最全的23个免费无背景PNG素材网站汇总,让你有用不完的资源!!
  2. 6.java项目-尚医通(6)
  3. 进口书可以到亚马逊买要便宜些
  4. Chapter4:菜单File-Settings之Notifications和KeyMap
  5. 学计算机的雷蛇与苹果,雷蛇做了什么让学霸苹果也抄袭他了
  6. React Native 手势触摸事件机制详解(进阶篇)
  7. FSMO角色之RID主机详解
  8. [转]WEB2.0中AJAX应用的详细探讨
  9. STM32DAC讲解及代码示例
  10. Nginx代理上传文件大小设置