You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by li...@apache.org on 2024/03/08 03:01:53 UTC

(incubator-seata) branch 2.x updated: optimize: determine the version before sync metadata and add retry mechanism (#6393)

This is an automated email from the ASF dual-hosted git repository.

liuqiufeng pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/incubator-seata.git


The following commit(s) were added to refs/heads/2.x by this push:
     new d47a5d5cf3 optimize: determine the version before sync metadata and add  retry mechanism (#6393)
d47a5d5cf3 is described below

commit d47a5d5cf34e96f405f9026632c1f565af1d7e81
Author: funkye <ji...@apache.org>
AuthorDate: Fri Mar 8 11:01:49 2024 +0800

    optimize: determine the version before sync metadata and add  retry mechanism (#6393)
---
 changes/en-us/2.x.md                               |  2 +
 changes/zh-cn/2.x.md                               |  2 +
 .../server/cluster/raft/RaftStateMachine.java      | 54 ++++++++++++++++++----
 .../processor/PutNodeInfoRequestProcessor.java     |  2 +
 4 files changed, 52 insertions(+), 8 deletions(-)

diff --git a/changes/en-us/2.x.md b/changes/en-us/2.x.md
index ba6d1b1d3d..c0680127c4 100644
--- a/changes/en-us/2.x.md
+++ b/changes/en-us/2.x.md
@@ -7,6 +7,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#6205](https://github.com/apache/incubator-seata/pull/6205)] mock server
 - [[#6169](https://github.com/apache/incubator-seata/pull/6169)] full support for states in the refactored state machine designer
 - [[#6230](https://github.com/apache/incubator-seata/pull/6230)] RocketMQ transaction are supported
+- [[#6326](https://github.com/apache/incubator-seata/pull/6326)] support raft node metadata sync
 
 ### bugfix:
 - [[#6090](https://github.com/apache/incubator-seata/pull/6090)] fix the TCC aspect exception handling process, do not wrapping the internal call exceptions
@@ -105,6 +106,7 @@ Add changes here for all PR submitted to the 2.x branch.
 - [[#6369](https://github.com/apache/incubator-seata/pull/6369)] optimize arm64 ci
 - [[#6386](https://github.com/apache/incubator-seata/pull/6386)] replace `byte-buddy` to JDK proxy in `ConfigurationCache`
 - [[#6391](https://github.com/apache/incubator-seata/pull/6091)] forbid duplicate registration of TCC resources
+- [[#6393](https://github.com/apache/incubator-seata/pull/6393)] determine the version before sync metadata and add retry mechanism
 
 ### refactor:
 - [[#6269](https://github.com/apache/incubator-seata/pull/6269)] standardize Seata Exception
diff --git a/changes/zh-cn/2.x.md b/changes/zh-cn/2.x.md
index 446dd5558e..5d3eaf99eb 100644
--- a/changes/zh-cn/2.x.md
+++ b/changes/zh-cn/2.x.md
@@ -7,6 +7,7 @@
 - [[#6205](https://github.com/apache/incubator-seata/pull/6205)] 提供mock server
 - [[#6169](https://github.com/apache/incubator-seata/pull/6169)] 支持新版本状态机设计器
 - [[#6230](https://github.com/apache/incubator-seata/pull/6230)] 支持RocketMQ消息事务
+- [[#6326](https://github.com/apache/incubator-seata/pull/6326)] 支持raft节点间的元数据同步
 
 ### bugfix:
 - [[#6090](https://github.com/apache/incubator-seata/pull/6090)] 修复tcc切面异常处理过程,不对内部调用异常做包装处理,直接向外抛出
@@ -109,6 +110,7 @@
 - [[#6369](https://github.com/apache/incubator-seata/pull/6369)] 优化 arm64 ci
 - [[#6386](https://github.com/apache/incubator-seata/pull/6386)] 在 `ConfigurationCache` 类中,将 `byte-buddy` 替换为JDK代理
 - [[#6391](https://github.com/apache/incubator-seata/pull/6091)] 禁止重复注册TCC资源
+- [[#6393](https://github.com/apache/incubator-seata/pull/6393)] 元数据同步前判断版本,并增加重试功能
 
 
 ### refactor:
diff --git a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java
index 23738aadf7..3f876b63f3 100644
--- a/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java
+++ b/server/src/main/java/org/apache/seata/server/cluster/raft/RaftStateMachine.java
@@ -25,6 +25,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicLong;
 import java.util.concurrent.locks.Lock;
@@ -46,11 +49,13 @@ import org.apache.seata.common.XID;
 import org.apache.seata.common.holder.ObjectHolder;
 import org.apache.seata.common.metadata.ClusterRole;
 import org.apache.seata.common.metadata.Node;
+import org.apache.seata.common.thread.NamedThreadFactory;
 import org.apache.seata.common.util.CollectionUtils;
 import org.apache.seata.common.util.StringUtils;
 import org.apache.seata.core.serializer.SerializerType;
 import org.apache.seata.server.cluster.raft.context.SeataClusterContext;
 import org.apache.seata.server.cluster.raft.processor.request.PutNodeMetadataRequest;
+import org.apache.seata.server.cluster.raft.processor.response.PutNodeMetadataResponse;
 import org.apache.seata.server.cluster.raft.snapshot.metadata.LeaderMetadataSnapshotFile;
 import org.apache.seata.server.cluster.raft.snapshot.session.SessionSnapshotFile;
 import org.apache.seata.server.cluster.raft.snapshot.StoreSnapshotFile;
@@ -105,7 +110,9 @@ public class RaftStateMachine extends StateMachineAdapter {
 
     private volatile RaftClusterMetadata raftClusterMetadata = new RaftClusterMetadata();
 
-    private Lock lock = new ReentrantLock();
+    private final Lock lock = new ReentrantLock();
+
+    private static final ScheduledThreadPoolExecutor RESYNC_METADATA_POOL = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("reSyncMetadataPool", 1, true));
 
     /**
      * Leader term
@@ -117,8 +124,10 @@ public class RaftStateMachine extends StateMachineAdapter {
      */
     private final AtomicLong currentTerm = new AtomicLong(-1);
 
-    private final AtomicBoolean init = new AtomicBoolean(false);
+    private final AtomicBoolean initSync = new AtomicBoolean(false);
 
+    private ScheduledFuture<?> scheduledFuture;
+    
     public boolean isLeader() {
         return this.leaderTerm.get() > 0;
     }
@@ -141,6 +150,8 @@ public class RaftStateMachine extends StateMachineAdapter {
             EXECUTES.put(REMOVE_GLOBAL_SESSION, new RemoveGlobalSessionExecute());
             EXECUTES.put(UPDATE_BRANCH_SESSION_STATUS, new UpdateBranchSessionExecute());
             EXECUTES.put(RELEASE_BRANCH_SESSION_LOCK, new BranchReleaseLockExecute());
+            this.scheduledFuture =
+                RESYNC_METADATA_POOL.scheduleAtFixedRate(() -> syncCurrentNodeInfo(group), 10, 10, TimeUnit.SECONDS);
         }
     }
 
@@ -244,7 +255,7 @@ public class RaftStateMachine extends StateMachineAdapter {
     public void onStartFollowing(final LeaderChangeContext ctx) {
         LOGGER.info("groupId: {}, onStartFollowing: {}.", group, ctx);
         this.currentTerm.set(ctx.getTerm());
-        CompletableFuture.runAsync(() -> syncCurrentNodeInfo(ctx.getLeaderId()));
+        CompletableFuture.runAsync(() -> syncCurrentNodeInfo(ctx.getLeaderId()), RESYNC_METADATA_POOL);
     }
 
     @Override
@@ -355,9 +366,26 @@ public class RaftStateMachine extends StateMachineAdapter {
         LOGGER.info("groupId: {}, refresh cluster metadata: {}", group, raftClusterMetadata);
     }
 
+    private void syncCurrentNodeInfo(String group) {
+        if (initSync.get()) {
+            return;
+        }
+        try {
+            RouteTable.getInstance().refreshLeader(RaftServerManager.getCliClientServiceInstance(), group, 1000);
+            PeerId peerId = RouteTable.getInstance().selectLeader(group);
+            if (peerId != null) {
+                syncCurrentNodeInfo(peerId);
+            }
+        } catch (Exception e) {
+            LOGGER.error(e.getMessage(), e);
+        }
+    }
+
     private void syncCurrentNodeInfo(PeerId leaderPeerId) {
-        if (init.compareAndSet(false, true)) {
-            try {
+        try {
+            // Ensure that the current leader must be version 2.1 or later to synchronize the operation
+            Node leader = raftClusterMetadata.getLeader();
+            if (leader != null && StringUtils.isNotBlank(leader.getVersion()) && initSync.compareAndSet(false, true)) {
                 RaftServer raftServer = RaftServerManager.getRaftServer(group);
                 PeerId cureentPeerId = raftServer.getServerId();
                 Node node = raftClusterMetadata.createNode(XID.getIpAddress(), XID.getPort(), cureentPeerId.getPort(),
@@ -379,15 +407,25 @@ public class RaftStateMachine extends StateMachineAdapter {
                 cliClientService.getRpcClient().invokeAsync(leaderPeerId.getEndpoint(), putNodeInfoRequest,
                     invokeContext, (result, err) -> {
                         if (err == null) {
-                            LOGGER.info("sync node info to leader: {}, result: {}", leaderPeerId, result);
+                            PutNodeMetadataResponse putNodeMetadataResponse = (PutNodeMetadataResponse)result;
+                            if (putNodeMetadataResponse.isSuccess()) {
+                                scheduledFuture.cancel(true);
+                                LOGGER.info("sync node info to leader: {}, result: {}", leaderPeerId, result);
+                            } else {
+                                initSync.compareAndSet(true, false);
+                                LOGGER.info(
+                                    "sync node info to leader: {}, result: {}, retry will be made at the time of the re-election or after 10 seconds",
+                                    leaderPeerId, result);
+                            }
                         } else {
+                            initSync.compareAndSet(true, false);
                             LOGGER.error("sync node info to leader: {}, error: {}", leaderPeerId, err.getMessage(),
                                 err);
                         }
                     }, 30000);
-            } catch (Exception e) {
-                LOGGER.error(e.getMessage(), e);
             }
+        } catch (Exception e) {
+            LOGGER.error(e.getMessage(), e);
         }
     }
 
diff --git a/server/src/main/java/org/apache/seata/server/cluster/raft/processor/PutNodeInfoRequestProcessor.java b/server/src/main/java/org/apache/seata/server/cluster/raft/processor/PutNodeInfoRequestProcessor.java
index b99666831e..de90cbd8c5 100644
--- a/server/src/main/java/org/apache/seata/server/cluster/raft/processor/PutNodeInfoRequestProcessor.java
+++ b/server/src/main/java/org/apache/seata/server/cluster/raft/processor/PutNodeInfoRequestProcessor.java
@@ -40,6 +40,8 @@ public class PutNodeInfoRequestProcessor implements RpcProcessor<PutNodeMetadata
             RaftStateMachine raftStateMachine = raftServer.getRaftStateMachine();
             raftStateMachine.changeNodeMetadata(node);
             rpcCtx.sendResponse(new PutNodeMetadataResponse(true));
+        } else {
+            rpcCtx.sendResponse(new PutNodeMetadataResponse(false));
         }
     }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org