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

rong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iotdb.git


The following commit(s) were added to refs/heads/master by this push:
     new b7bd94dee4a Fixed ConfigNode startup error in SimpleConsensus (#12143)
b7bd94dee4a is described below

commit b7bd94dee4ae9aaff435ca9ef9abcc1d134b2db6
Author: Potato <[email protected]>
AuthorDate: Fri Mar 8 23:13:03 2024 +0800

    Fixed ConfigNode startup error in SimpleConsensus (#12143)
    
    SimpleConsensus triggers the start and notifyLeaderReady functions of 
SimpleConsensusImpl before the consensusGroup in the map has been written. In 
the initialization logic of some asynchronous calls, if it is executed fast 
enough, SimpleConsensus may still have a map size of 0, causing an error to 
fail startup.
    
    The fix is as simple as calling start after a successful put.
    
    Signed-off-by: OneSizeFitQuorum <[email protected]>
---
 .../org/apache/iotdb/consensus/simple/SimpleConsensus.java   | 10 ++++++----
 .../iotdb/consensus/simple/SimpleConsensusServerImpl.java    | 12 ++++++++----
 2 files changed, 14 insertions(+), 8 deletions(-)

diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
index 59f6cccd6f8..c7d6141ceba 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensus.java
@@ -168,11 +168,13 @@ class SimpleConsensus implements IConsensus {
                     return null;
                   }
 
-                  SimpleConsensusServerImpl impl =
-                      new SimpleConsensusServerImpl(peers.get(0), 
registry.apply(groupId));
-                  impl.start();
-                  return impl;
+                  return new SimpleConsensusServerImpl(peers.get(0), 
registry.apply(groupId));
                 }))
+        .map(
+            impl -> {
+              impl.start();
+              return impl;
+            })
         .orElseThrow(
             () ->
                 new ConsensusException(
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensusServerImpl.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensusServerImpl.java
index 758d4f31081..22b74b74538 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensusServerImpl.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/simple/SimpleConsensusServerImpl.java
@@ -26,11 +26,13 @@ import org.apache.iotdb.consensus.common.Peer;
 import org.apache.iotdb.consensus.common.request.IConsensusRequest;
 
 import java.io.File;
+import java.util.concurrent.atomic.AtomicBoolean;
 
 public class SimpleConsensusServerImpl implements IStateMachine {
 
   private final Peer peer;
   private final IStateMachine stateMachine;
+  private final AtomicBoolean initialized = new AtomicBoolean(false);
 
   public SimpleConsensusServerImpl(Peer peer, IStateMachine stateMachine) {
     this.peer = peer;
@@ -47,10 +49,12 @@ public class SimpleConsensusServerImpl implements 
IStateMachine {
 
   @Override
   public void start() {
-    stateMachine.start();
-    // Notify itself as the leader
-    stateMachine.event().notifyLeaderChanged(peer.getGroupId(), 
peer.getNodeId());
-    stateMachine.event().notifyLeaderReady();
+    if (initialized.compareAndSet(false, true)) {
+      stateMachine.start();
+      // Notify itself as the leader
+      stateMachine.event().notifyLeaderChanged(peer.getGroupId(), 
peer.getNodeId());
+      stateMachine.event().notifyLeaderReady();
+    }
   }
 
   @Override

Reply via email to