Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle and Application of MasterElection in nacos RaftCore

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "the principle and application of MasterElection in nacos RaftCore". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "the principle and application of MasterElection in nacos RaftCore".

Order

This paper mainly studies the MasterElection of nacos RaftCore.

RaftCore

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

@ Componentpublic 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) {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 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 () {@ 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 implements the Runnable method, whose run method starts the election when peers is ready and local.leaderDueMs minus TICK_PERIOD_MS is less than or equal to 0; it first resetLeaderDue and resetHeartbeatDue, and then executes the sendVote method; sendVote method first resets peers, increments localPeer's term, and sets voteFor to itself, then updates state to RaftPeer.State.CANDIDATE, and finally iterates through peers.allServersWithoutMySelf (), asynchronously post its vote information to other peer Execute peers.decideLeader (peer) if other peer returns success, return 1, otherwise return 0

Summary

RaftCore's init method registers the MasterElection;registerMasterElection method to schedule every TICK_PERIOD_MS millisecond through GlobalExecutor.registerMasterElection (new MasterElection ()); MasterElection implements the Runnable method, and its run method starts the election when peers is ready and local.leaderDueMs minus TICK_PERIOD_MS is less than or equal to 0; it first resetLeaderDue and resetHeartbeatDue, and then executes the sendVote method

At this point, I believe you have a deeper understanding of "the principle and application of MasterElection in nacos RaftCore". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report