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

The principle and usage of signalPublish in nacos RaftCore

2025-01-22 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 use of signalPublish in nacos RaftCore". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the principle and use of signalPublish in nacos RaftCore".

Order

This paper mainly studies the signalPublish of nacos RaftCore.

RaftCore

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

@ Componentpublic class RaftCore {public static final String API_VOTE = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/vote"; public static final String API_BEAT = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/beat"; public static final String API_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/datum"; public static final String API_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/datum"; public static final String API_GET = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/datum" Public static final String API_ON_PUB = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/datum/commit"; public static final String API_ON_DEL = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/datum/commit"; public static final String API_GET_PEER = UtilsAndCommons.NACOS_NAMING_CONTEXT + "/ raft/peer" Private ScheduledExecutorService executor = new ScheduledThreadPoolExecutor (1, new ThreadFactory () {@ Override public Thread newThread (Runnable r) {Thread t = newThread (r); t.setDaemon (true); t.setName ("com.alibaba.nacos.naming.raft.notifier"); return t;}}); public static final Lock OPERATE_LOCK = new ReentrantLock () Public static final int PUBLISH_TERM_INCREASE_COUNT = 100; private volatile Map listeners = new ConcurrentHashMap (); private volatile ConcurrentMap datums = new ConcurrentHashMap (); @ Autowired private RaftPeerSet peers; @ Autowired private SwitchDomain switchDomain; @ Autowired private GlobalConfig globalConfig; @ Autowired private RaftStore raftStore; public volatile Notifier notifier @ Autowired private RaftStore raftStore; public volatile Notifier notifier = new Notifier (); private boolean initialized = false @ 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 () source.term.get ()) {/ / set leader term: getLeader () .term.set (source.term.get ()) Local.term.set (getLeader (). Term.get ());} else {local.term.addAndGet (PUBLISH_TERM_INCREASE_COUNT);}} raftStore.updateTerm (local.term.get ()); notifier.addTask (datum.key, ApplyAction.CHANGE) Loggers.RAFT.info ("data added/updated, key= {}, term= {}", datum.key, local.term);} /.}

The onPublish method first determines whether the requested node is leader. If not, IllegalStateException; throws IllegalStateException if source.term is less than local.term.

Then execute local.resetLeaderDue (), and raftStore.write (datum), datums.put (datum.key, datum); execute local.term.addAndGet (PUBLISH_TERM_INCREASE_COUNT) for leader nodes, and update leader term and local.term for non-leader nodes

Finally, execute raftStore.updateTerm (local.term.get ()) and notifier.addTask (datum.key, ApplyAction.CHANGE)

Summary

The signalPublish method determines whether the current node is a leader, and if not, forwards the publish to the / v1/ns/raft/datum interface of the leader node

If it is leader, construct CountDownLatch of datum and peers.majorityCount () size, then traverse peers.allServersIncludeMyself (), for leader node directly latch.countDown (), for non-leader node send asynchronous request, request / v1/ns/raft/datum/commit interface, in onCompleted, if the request executes latch.countDown () successfully

Finally, throw an IllegalStateException for a CountDownLatch that fails to return in RAFT_PUBLISH_TIMEOUT

Thank you for reading, the above is the content of "the principle and use of signalPublish in nacos RaftCore". After the study of this article, I believe you have a deeper understanding of the principle and use of signalPublish in nacos RaftCore, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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