您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“nacos RaftCore中MasterElection的原理及應用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“nacos RaftCore中MasterElection的原理及應用”吧!
本文主要研究一下nacos RaftCore的MasterElection
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方法通過GlobalExecutor.registerMasterElection(new MasterElection())注冊了MasterElection
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
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實現(xiàn)了Runnable方法,其run方法在peers都是ready而且local.leaderDueMs減去TICK_PERIOD_MS小于等于0的時候會開始選舉;它首先resetLeaderDue及resetHeartbeatDue,然后執(zhí)行sendVote方法;sendVote方法首先重置peers,遞增localPeer的term,并設置voteFor為自己,然后更新state為RaftPeer.State.CANDIDATE,最后遍歷peers.allServersWithoutMySelf(),將自己的vote信息異步post給其他peer;如果其他peer返回成功則執(zhí)行peers.decideLeader(peer),返回1,否則返回0
RaftCore的init方法通過GlobalExecutor.registerMasterElection(new MasterElection())注冊了MasterElection;registerMasterElection方法每隔TICK_PERIOD_MS毫秒調(diào)度一次;MasterElection實現(xiàn)了Runnable方法,其run方法在peers都是ready而且local.leaderDueMs減去TICK_PERIOD_MS小于等于0的時候會開始選舉;它首先resetLeaderDue及resetHeartbeatDue,然后執(zhí)行sendVote方法
到此,相信大家對“nacos RaftCore中MasterElection的原理及應用”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關內(nèi)容可以進入相關頻道進行查詢,關注我們,繼續(xù)學習!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。