In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you what the role of DataSyncer in nacos is, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
DataSyncer
Nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java
@ Component@DependsOn ("serverListManager") public class DataSyncer {@ Autowired private DataStore dataStore; @ Autowired private GlobalConfig partitionConfig; @ Autowired private Serializer serializer; @ Autowired private ServerListManager serverListManager; private Map taskMap = new ConcurrentHashMap (); @ PostConstruct public void init () {startTimedSync () } public void submit (SyncTask task, long delay) {/ / If it's a new task: if (task.getRetryCount () = = 0) {Iterator iterator = task.getKeys () .iterator (); while (iterator.hasNext ()) {String key = iterator.next () If (StringUtils.isNotBlank (taskMap.putIfAbsent (buildKey (key, task.getTargetServer (), key) {/ / associated key already exist: if (Loggers.DISTRO.isDebugEnabled ()) {Loggers.DISTRO.debug ("sync already in process, key: {}", key) } iterator.remove ();} if (task.getKeys () .isEmpty ()) {/ / all keys are removed: return } GlobalExecutor.submitDataSync (new Runnable () {@ Override public void run () {try {if (getServers () = = null | | getServers () .isEmpty ()) {Loggers.SRV_LOG.warn ("try to sync data but server list is empty."); return } List keys = task.getKeys (); if (Loggers.DISTRO.isDebugEnabled ()) {Loggers.DISTRO.debug ("sync keys: {}", keys);} Map datumMap = dataStore.batchGet (keys) If (datumMap = = null | | datumMap.isEmpty ()) {/ / clear all flags of this task: for (String key: task.getKeys ()) {taskMap.remove (buildKey (key, task.getTargetServer ();} return } byte [] data = serializer.serialize (datumMap); long timestamp = System.currentTimeMillis (); boolean success = NamingProxy.syncData (data, task.getTargetServer ()); if (! success) {SyncTask syncTask = new SyncTask () SyncTask.setKeys (task.getKeys ()); syncTask.setRetryCount (task.getRetryCount () + 1); syncTask.setLastExecuteTime (timestamp); syncTask.setTargetServer (task.getTargetServer ()); retrySync (syncTask) } else {/ / clear all flags of this task: for (String key: task.getKeys ()) {taskMap.remove (buildKey (key, task.getTargetServer () } catch (Exception e) {Loggers.DISTRO.error ("sync data failed.", e);}, delay);} public void retrySync (SyncTask syncTask) {Server server = new Server () Server.setIp (syncTask.getTargetServer (). Split (":") [0]); server.setServePort (Integer.parseInt (syncTask.getTargetServer (). Split (":") [1]); if (! getServers (). Contains (server)) {/ / if server is no longer in healthy server list, ignore this task: return;} / / TODO may choose other retry policy. Submit (syncTask, partitionConfig.getSyncRetryDelay ());} public void startTimedSync () {GlobalExecutor.schedulePartitionDataTimedSync (new TimedSync ());} / /. Public List getServers () {return serverListManager.getHealthyServers ();} public String buildKey (String key, String targetServer) {return key + UtilsAndCommons.CACHE_KEY_SPLITER + targetServer;}}
DataSyncer defines submit, retrySync, startTimedSync, getServers and other methods, and its init method executes startTimedSync.
The submit method determines whether the taskMap exists for a task with a retryCount of 0. Remove the taskKey if it exists, and then use GlobalExecutor.submitDataSync to submit a sync task. It is mainly synchronized through NamingProxy.syncData, removed if successful, and retried using retrySync if it is unsuccessful.
RetrySync reconstructs server and calls submit for execution; startTimedSync method submits TimedSync tasks using GlobalExecutor.schedulePartitionDataTimedSync; getServers returns healthy instances through serverListManager.getHealthyServers ()
TimedSync
Nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/DataSyncer.java
Public class TimedSync implements Runnable {@ Override public void run () {try {if (Loggers.DISTRO.isDebugEnabled ()) {Loggers.DISTRO.debug ("server list is: {}", getServers ());} / / send local timestamps to other servers: Map keyChecksums = new HashMap (64) For (String key: dataStore.keys ()) {if (! distroMapper.responsible (KeyBuilder.getServiceName (key) {continue;} keyChecksums.put (key, dataStore.get (key). Value.getChecksum ()) } if (keyChecksums.isEmpty ()) {return;} if (Loggers.DISTRO.isDebugEnabled ()) {Loggers.DISTRO.debug ("sync checksums: {}", keyChecksums) } for (Server member: getServers ()) {if (NetUtils.localServer (). Equals (member.getKey () {continue;} NamingProxy.syncCheckSums (keyChecksums, member.getKey ()) } catch (Exception e) {Loggers.DISTRO.error ("timed sync task failed.", e);}
TimedSync uses NamingProxy.syncCheckSums synchronous keyChecksums for verification
Summary
DataSyncer defines submit, retrySync, startTimedSync, getServers and other methods, and its init method executes startTimedSync.
The submit method determines whether the taskMap exists for a task with a retryCount of 0. Remove the taskKey if it exists, and then use GlobalExecutor.submitDataSync to submit a sync task. It is mainly synchronized through NamingProxy.syncData, removed if successful, and retried using retrySync if it is unsuccessful.
RetrySync reconstructs server and calls submit for execution; startTimedSync method submits TimedSync tasks using GlobalExecutor.schedulePartitionDataTimedSync; getServers returns healthy instances through serverListManager.getHealthyServers ()
The above content is what is the role of DataSyncer in nacos. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
Realization of API setting based on AccessToken
© 2024 shulou.com SLNews company. All rights reserved.