From 8ea1833f1c9a5571dc00064683d8e30179d785b8 Mon Sep 17 00:00:00 2001 From: OneSizeFitQuorum Date: Thu, 29 Aug 2024 11:24:59 +0800 Subject: [PATCH] fix Signed-off-by: OneSizeFitQuorum --- .../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 7ec94076fb..8833011f59 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; @@ -229,6 +230,7 @@ public long[] getFollowerNextIndices() { 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 @@ -463,6 +465,13 @@ void groupRemove(boolean deleteDirectory, boolean renameDirectory) { /* 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 ++) { @@ -541,6 +550,7 @@ public void close() { } catch (Exception e) { LOG.warn(getMemberId() + ": Failed to shutdown serverExecutor", e); } + closeFinishedLatch.countDown(); }); }