diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java index 2a9ad724c2..39b401dda1 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java @@ -282,10 +282,8 @@ private void runImpl() { } private boolean shouldRun() { - synchronized (server) { - final DivisionInfo info = server.getInfo(); - return lifeCycle.getCurrentState().isRunning() && info.isCandidate() && info.isAlive(); - } + final DivisionInfo info = server.getInfo(); + return lifeCycle.getCurrentState().isRunning() && info.isCandidate() && info.isAlive(); } private boolean shouldRun(long electionTerm) { 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 0acbdc0dec..cfcdd1519d 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 @@ -528,7 +528,7 @@ public void close() { LOG.warn("{}: Failed to shutdown LeaderElection", getMemberId(), e); } try{ - role.shutdownLeaderState(true); + role.shutdownLeaderState(true).join(); } catch (Exception e) { LOG.warn("{}: Failed to shutdown LeaderState monitor", getMemberId(), e); } @@ -620,7 +620,7 @@ synchronized CompletableFuture changeToFollowerAndPersistMetadata( synchronized void changeToLeader() { Preconditions.assertTrue(getInfo().isCandidate()); - CompletableFuture future = role.shutdownLeaderElection(); + role.shutdownLeaderElection(); setRole(RaftPeerRole.LEADER, "changeToLeader"); final LeaderStateImpl leader = role.updateLeaderState(this); state.becomeLeader();