中文字幕日韩精品一区二区免费_精品一区二区三区国产精品无卡在_国精品无码专区一区二区三区_国产αv三级中文在线

nacosRaftCore中MasterElection的原理及應(yīng)用

本篇內(nèi)容主要講解“nacos RaftCore中MasterElection的原理及應(yīng)用”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“nacos RaftCore中MasterElection的原理及應(yīng)用”吧!

專(zhuān)注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)大方免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了上1000+企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過(guò)網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

本文主要研究一下nacos RaftCore的MasterElection

RaftCore

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java

@Component
public class RaftCore {
	
	//......

    @PostConstruct
    public void init() throws Exception {

        Loggers.RAFT.info("initializing Raft sub-system");

        executor.submit(notifier);

        long start = System.currentTimeMillis();

        raftStore.loadDatums(notifier, datums);

        setTerm(NumberUtils.toLong(raftStore.loadMeta().getProperty("term"), 0L));

        Loggers.RAFT.info("cache loaded, datum count: {}, current term: {}", datums.size(), peers.getTerm());

        while (true) {
            if (notifier.tasks.size() <= 0) {
                break;
            }
            Thread.sleep(1000L);
        }

        initialized = true;

        Loggers.RAFT.info("finish to load data from disk, cost: {} ms.", (System.currentTimeMillis() - start));

        GlobalExecutor.registerMasterElection(new MasterElection());
        GlobalExecutor.registerHeartbeat(new HeartBeat());

        Loggers.RAFT.info("timer started: leader timeout ms: {}, heart-beat timeout ms: {}",
            GlobalExecutor.LEADER_TIMEOUT_MS, GlobalExecutor.HEARTBEAT_INTERVAL_MS);
    }

	//......
}
  • RaftCore的init方法通過(guò)GlobalExecutor.registerMasterElection(new MasterElection())注冊(cè)了MasterElection

GlobalExecutor

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/misc/GlobalExecutor.java

public class GlobalExecutor {

	//......

    public static final long TICK_PERIOD_MS = TimeUnit.MILLISECONDS.toMillis(500L);

    public static void registerMasterElection(Runnable runnable) {
        executorService.scheduleAtFixedRate(runnable, 0, TICK_PERIOD_MS, TimeUnit.MILLISECONDS);
    }

    //......
}
  • registerMasterElection方法每隔TICK_PERIOD_MS毫秒調(diào)度一次runnable

MasterElection

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftCore.java

    public class MasterElection implements Runnable {
        @Override
        public void run() {
            try {

                if (!peers.isReady()) {
                    return;
                }

                RaftPeer local = peers.local();
                local.leaderDueMs -= GlobalExecutor.TICK_PERIOD_MS;

                if (local.leaderDueMs > 0) {
                    return;
                }

                // reset timeout
                local.resetLeaderDue();
                local.resetHeartbeatDue();

                sendVote();
            } catch (Exception e) {
                Loggers.RAFT.warn("[RAFT] error while master election {}", e);
            }

        }

        public void sendVote() {

            RaftPeer local = peers.get(NetUtils.localServer());
            Loggers.RAFT.info("leader timeout, start voting,leader: {}, term: {}",
                JSON.toJSONString(getLeader()), local.term);

            peers.reset();

            local.term.incrementAndGet();
            local.voteFor = local.ip;
            local.state = RaftPeer.State.CANDIDATE;

            Map<String, String> params = new HashMap<>(1);
            params.put("vote", JSON.toJSONString(local));
            for (final String server : peers.allServersWithoutMySelf()) {
                final String url = buildURL(server, API_VOTE);
                try {
                    HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler<Integer>() {
                        @Override
                        public Integer onCompleted(Response response) throws Exception {
                            if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {
                                Loggers.RAFT.error("NACOS-RAFT vote failed: {}, url: {}", response.getResponseBody(), url);
                                return 1;
                            }

                            RaftPeer peer = JSON.parseObject(response.getResponseBody(), RaftPeer.class);

                            Loggers.RAFT.info("received approve from peer: {}", JSON.toJSONString(peer));

                            peers.decideLeader(peer);

                            return 0;
                        }
                    });
                } catch (Exception e) {
                    Loggers.RAFT.warn("error while sending vote to server: {}", server);
                }
            }
        }
    }
  • MasterElection實(shí)現(xiàn)了Runnable方法,其run方法在peers都是ready而且local.leaderDueMs減去TICK_PERIOD_MS小于等于0的時(shí)候會(huì)開(kāi)始選舉;它首先resetLeaderDue及resetHeartbeatDue,然后執(zhí)行sendVote方法;sendVote方法首先重置peers,遞增localPeer的term,并設(shè)置voteFor為自己,然后更新state為RaftPeer.State.CANDIDATE,最后遍歷peers.allServersWithoutMySelf(),將自己的vote信息異步post給其他peer;如果其他peer返回成功則執(zhí)行peers.decideLeader(peer),返回1,否則返回0

小結(jié)

RaftCore的init方法通過(guò)GlobalExecutor.registerMasterElection(new MasterElection())注冊(cè)了MasterElection;registerMasterElection方法每隔TICK_PERIOD_MS毫秒調(diào)度一次;MasterElection實(shí)現(xiàn)了Runnable方法,其run方法在peers都是ready而且local.leaderDueMs減去TICK_PERIOD_MS小于等于0的時(shí)候會(huì)開(kāi)始選舉;它首先resetLeaderDue及resetHeartbeatDue,然后執(zhí)行sendVote方法

到此,相信大家對(duì)“nacos RaftCore中MasterElection的原理及應(yīng)用”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!

文章名稱(chēng):nacosRaftCore中MasterElection的原理及應(yīng)用
網(wǎng)站URL:http://www.rwnh.cn/article22/gcgijc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)自適應(yīng)網(wǎng)站、網(wǎng)站維護(hù)用戶(hù)體驗(yàn)、網(wǎng)站導(dǎo)航域名注冊(cè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)
娄烦县| 会同县| 迁安市| 马公市| 象州县| 彭阳县| 延津县| 玉门市| 通化县| 平山县| 鹿泉市| 杨浦区| 江永县| 滦平县| 抚远县| 丹江口市| 富阳市| 普兰店市| 荃湾区| 光泽县| 新河县| 太湖县| 耒阳市| 工布江达县| 阿鲁科尔沁旗| 伊春市| 浠水县| 洪雅县| 喀什市| 彩票| 石楼县| 平昌县| 武隆县| 莫力| 墨竹工卡县| 十堰市| 内丘县| 石泉县| 钟山县| 固安县| 平远县|