Skip to content

Commit 004ac67

Browse files
committed
RATIS-698. RaftServerImpl should not enforce minTimeout before first leaderElection cycle.
1 parent 89abd13 commit 004ac67

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import java.util.concurrent.ExecutionException;
4848
import java.util.concurrent.ThreadLocalRandom;
4949
import java.util.concurrent.TimeUnit;
50+
import java.util.concurrent.atomic.AtomicBoolean;
5051
import java.util.concurrent.atomic.AtomicReference;
5152
import java.util.function.Function;
5253
import java.util.function.Supplier;
@@ -90,6 +91,8 @@ public class RaftServerImpl implements RaftServerProtocol, RaftServerAsynchronou
9091

9192
private AtomicReference<TermIndex> inProgressInstallSnapshotRequest;
9293

94+
private final AtomicBoolean honorMinTimeoutMs = new AtomicBoolean();
95+
9396
RaftServerImpl(RaftGroup group, StateMachine stateMachine, RaftServerProxy proxy) throws IOException {
9497
final RaftPeerId id = proxy.getId();
9598
LOG.info("{}: new RaftServerImpl for {} with {}", id, group, stateMachine);
@@ -141,8 +144,12 @@ int getMaxTimeoutMs() {
141144
}
142145

143146
int getRandomTimeoutMs() {
144-
return minTimeoutMs + ThreadLocalRandom.current().nextInt(
145-
maxTimeoutMs - minTimeoutMs + 1);
147+
return (honorMinTimeoutMs.get() ? minTimeoutMs : 0) +
148+
ThreadLocalRandom.current().nextInt(maxTimeoutMs - minTimeoutMs + 1);
149+
}
150+
151+
void honorMinTimeoutMs() {
152+
honorMinTimeoutMs.set(Boolean.TRUE);
146153
}
147154

148155
int getSleepDeviationThresholdMs() {

ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ void setLeader(RaftPeerId newLeaderId, String op) {
251251
lastNoLeaderTime = null;
252252
suffix = ", leader elected after " + previous.elapsedTimeMs() + "ms";
253253
server.getStateMachine().notifyLeaderChanged(getMemberId(), newLeaderId);
254+
server.honorMinTimeoutMs();
254255
}
255256
LOG.info("{}: change Leader from {} to {} at term {} for {}{}",
256257
getMemberId(), leaderId, newLeaderId, getCurrentTerm(), op, suffix);

ratis-server/src/test/java/org/apache/ratis/statemachine/SimpleStateMachine4Testing.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.apache.ratis.proto.RaftProtos.RoleInfoProto;
2525
import org.apache.ratis.protocol.Message;
2626
import org.apache.ratis.protocol.RaftClientRequest;
27-
import org.apache.ratis.protocol.RaftGroup;
2827
import org.apache.ratis.protocol.RaftGroupId;
2928
import org.apache.ratis.protocol.RaftGroupMemberId;
3029
import org.apache.ratis.protocol.RaftPeerId;
@@ -428,7 +427,9 @@ public void notifyNotLeader(Collection<TransactionContext> pendingEntries)
428427

429428
@Override
430429
public void notifyLeaderChanged(RaftGroupMemberId groupMemberId, RaftPeerId raftPeerId) {
431-
430+
if (groupMemberId.getPeerId().equals(raftPeerId)) {
431+
notifiedAsLeader = true;
432+
}
432433
}
433434

434435
public boolean isNotifiedAsLeader() {

0 commit comments

Comments
 (0)