日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Solr原理】Leader Shard选举

發(fā)布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Solr原理】Leader Shard选举 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Leader Shard的選舉原理主要是依靠LeaderElector.java類來實現(xiàn)的。

函數(shù)checkIfIamLeader開始真正的leader選舉,根據(jù)和Zookeeper上創(chuàng)建的znode的nodeName對比,判斷自己是否是leader。

  • 如果是leader,好了,更新的活以后該你去干了,執(zhí)行注冊leader的一系列更新操作。
  • 如果不是leader,而是replica的話,則在leader的znode上注冊watcher,關(guān)注leader znode的狀態(tài)。
  • 一旦發(fā)現(xiàn)leader znode消失,則會觸發(fā)下一次的leader選舉過程。
  • /*** Check if the candidate with the given n_* sequence number is the leader.* If it is, set the leaderId on the leader zk node. If it is not, start* watching the candidate that is in line before this one - if it goes down, check* if this candidate is the leader again.** @param replacement has someone else been the leader already?*/private void checkIfIamLeader(final ElectionContext context, boolean replacement) throws KeeperException,InterruptedException, IOException {context.checkIfIamLeaderFired();// get all other numbers...final String holdElectionPath = context.electionPath + ELECTION_NODE;List<String> seqs = zkClient.getChildren(holdElectionPath, null, true);sortSeqs(seqs);String leaderSeqNodeName = context.leaderSeqPath.substring(context.leaderSeqPath.lastIndexOf('/') + 1);if (!seqs.contains(leaderSeqNodeName)) {log.warn("Our node is no longer in line to be leader");return;}// If any double-registrations exist for me, remove all but this latest one!// TODO: can we even get into this state?String prefix = zkClient.getSolrZooKeeper().getSessionId() + "-" + context.id + "-";Iterator<String> it = seqs.iterator();while (it.hasNext()) {String elec = it.next();if (!elec.equals(leaderSeqNodeName) && elec.startsWith(prefix)) {try {String toDelete = holdElectionPath + "/" + elec;log.warn("Deleting duplicate registration: {}", toDelete);zkClient.delete(toDelete, -1, true);} catch (KeeperException.NoNodeException e) {// ignore}it.remove();}}if (leaderSeqNodeName.equals(seqs.get(0))) {// I am the leadertry {runIamLeaderProcess(context, replacement);} catch (KeeperException.NodeExistsException e) {log.error("node exists",e);retryElection(context, false);return;}} else {// I am not the leader - watch the node below meString toWatch = seqs.get(0);for (String node : seqs) {if (leaderSeqNodeName.equals(node)) {break;}toWatch = node;}try {String watchedNode = holdElectionPath + "/" + toWatch;zkClient.getData(watchedNode, watcher = new ElectionWatcher(context.leaderSeqPath, watchedNode, getSeq(context.leaderSeqPath), context), null, true);log.debug("Watching path {} to know if I could be the leader", watchedNode);} catch (KeeperException.SessionExpiredException e) {throw e;} catch (KeeperException.NoNodeException e) {// the previous node disappeared, check if we are the leader againcheckIfIamLeader(context, true);} catch (KeeperException e) {// we couldn't set our watch for some other reason, retrylog.warn("Failed setting watch", e);checkIfIamLeader(context, true);}}}

    Leader重新選舉

  • 正常運行的SolrCloud已產(chǎn)生一個leader(Znode編號最小,比如XXX_node1_0000001),后續(xù)的Replica后在leader節(jié)點上注冊Watcher。當(dāng)Leader下線時候,即短連接斷開,那么Zookeeper上的Znode(比如XXX_node1_0000001)就會被刪除。
  • 此時,所有Replica在Leader節(jié)點上的watcher就會監(jiān)控到這一變化,所有的Replica就會進行l(wèi)eader選舉,選舉的原則依然是判斷自己是不是目前注冊在/collections/collectionTest/leader_select/shard1/election下的Znode編號最小的那位,是的話就是Leader,否則就是Replica。
  • 如果判斷自己是Replica,就會繼續(xù)在leader的Znode上(這個時候的leader是XXX_node1_0000002)注冊watcher,等待leader下線再次觸發(fā)選舉leader。
  • 如果這個時候原先下線的leader上線了會怎么樣,它就會被當(dāng)做新的一個Solr節(jié)點注冊到Zookeeper上,并獲取一個比現(xiàn)有Znode更大的編號,在Leader Znode節(jié)點上注冊watcher,等待它的選舉機會。
  • 這篇文章講得很好,但它base的版本是比較老的Solr 4.x了。

    Reference
    http://quentinxxz.iteye.com/blog/2149891
    https://www.cnblogs.com/rcfeng/p/4082568.html
    https://www.cnblogs.com/saratearing/p/5690476.html
    https://blog.csdn.net/u011026968/article/details/50336709
    https://blog.csdn.net/iteye_16982/article/details/82574099

    Zookeeper簡介
    https://www.cnblogs.com/xinfang520/p/7717684.html

    SolrCloud Recovery原理及無法選舉分片Leader
    https://www.sohu.com/a/130752460_505885

    總結(jié)

    以上是生活随笔為你收集整理的【Solr原理】Leader Shard选举的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。