This is an automated email from the ASF dual-hosted git repository. tanxinyu pushed a commit to branch release-3.1.1 in repository https://gitbox.apache.org/repos/asf/ratis.git
commit 211278e5677799deb6a6e342a7b835783d78ff01 Author: Potato <[email protected]> AuthorDate: Tue Sep 3 11:42:14 2024 +0800 RATIS-2146. Fixed possible issues caused by concurrent deletion and election when member changes (#1140) --- .../main/java/org/apache/ratis/server/impl/RaftServerImpl.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java index 8d7246fcf..c682de8bf 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java @@ -17,6 +17,7 @@ */ package org.apache.ratis.server.impl; +import java.util.concurrent.CountDownLatch; import org.apache.ratis.client.impl.ClientProtoUtils; import org.apache.ratis.conf.RaftProperties; import org.apache.ratis.metrics.Timekeeper; @@ -228,6 +229,7 @@ class RaftServerImpl implements RaftServer.Division, private final RaftServerJmxAdapter jmxAdapter = new RaftServerJmxAdapter(this); private final LeaderElectionMetrics leaderElectionMetrics; private final RaftServerMetricsImpl raftServerMetrics; + private final CountDownLatch closeFinishedLatch = new CountDownLatch(1); // To avoid append entry before complete start() method // For example, if thread1 start(), but before thread1 startAsFollower(), thread2 receive append entry @@ -462,6 +464,13 @@ class RaftServerImpl implements RaftServer.Division, /* Shutdown is triggered here inorder to avoid any locked files. */ state.getStateMachineUpdater().setRemoving(); close(); + try { + closeFinishedLatch.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + LOG.warn("{}: Waiting closing interrupted, will not continue to remove group locally", getMemberId()); + return; + } getStateMachine().event().notifyGroupRemove(); if (deleteDirectory) { for (int i = 0; i < FileUtils.NUM_ATTEMPTS; i ++) { @@ -540,6 +549,7 @@ class RaftServerImpl implements RaftServer.Division, } catch (Exception e) { LOG.warn(getMemberId() + ": Failed to shutdown serverExecutor", e); } + closeFinishedLatch.countDown(); }); }
