Skip to content

Commit f28945b

Browse files
authored
Merge pull request etcd-io#10279 from tbg/leader-progress-replicate
raft: ensure leader is in ProgressStateReplicate
2 parents 02a9810 + 5c209d6 commit f28945b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

raft/raft.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,11 @@ func (r *raft) becomeLeader() {
750750
r.tick = r.tickHeartbeat
751751
r.lead = r.id
752752
r.state = StateLeader
753+
// Followers enter replicate mode when they've been successfully probed
754+
// (perhaps after having received a snapshot as a result). The leader is
755+
// trivially in this state. Note that r.reset() has initialized this
756+
// progress with the last index already.
757+
r.prs[r.id].becomeReplicate()
753758

754759
// Conservatively set the pendingConfIndex to the last index in the
755760
// log. There may or may not be a pending config change, but it's

raft/raft_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,30 @@ func TestProgressResume(t *testing.T) {
267267
}
268268
}
269269

270+
func TestProgressLeader(t *testing.T) {
271+
r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage())
272+
r.becomeCandidate()
273+
r.becomeLeader()
274+
r.prs[2].becomeReplicate()
275+
276+
// Send proposals to r1. The first 5 entries should be appended to the log.
277+
propMsg := pb.Message{From: 1, To: 1, Type: pb.MsgProp, Entries: []pb.Entry{{Data: []byte("foo")}}}
278+
for i := 0; i < 5; i++ {
279+
if pr := r.prs[r.id]; pr.State != ProgressStateReplicate || pr.Match != uint64(i+1) || pr.Next != pr.Match+1 {
280+
t.Errorf("unexpected progress %v", pr)
281+
}
282+
if err := r.Step(propMsg); err != nil {
283+
t.Fatalf("proposal resulted in error: %v", err)
284+
}
285+
}
286+
}
287+
270288
// TestProgressResumeByHeartbeatResp ensures raft.heartbeat reset progress.paused by heartbeat response.
271289
func TestProgressResumeByHeartbeatResp(t *testing.T) {
272290
r := newTestRaft(1, []uint64{1, 2}, 5, 1, NewMemoryStorage())
273291
r.becomeCandidate()
274292
r.becomeLeader()
293+
275294
r.prs[2].Paused = true
276295

277296
r.Step(pb.Message{From: 1, To: 1, Type: pb.MsgBeat})

0 commit comments

Comments
 (0)