Skip to content

Commit

Permalink
raft: Address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Nov 10, 2017
1 parent beb8931 commit d20cbf6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
20 changes: 10 additions & 10 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1211,16 +1211,6 @@ func (r *raft) handleSnapshot(m pb.Message) {
// restore recovers the state machine from a snapshot. It restores the log and the
// configuration of state machine.
func (r *raft) restore(s pb.Snapshot) bool {
// The normal peer can't become learner.
if !r.isLearner {
for _, id := range s.Metadata.ConfState.Learners {
if id == r.id {
r.logger.Errorf("%x can't become learner when restores snapshot [index: %d, term: %d]", r.id, s.Metadata.Index, s.Metadata.Term)
return false
}
}
}

if s.Metadata.Index <= r.raftLog.committed {
return false
}
Expand All @@ -1231,6 +1221,16 @@ func (r *raft) restore(s pb.Snapshot) bool {
return false
}

// The normal peer can't become learner.
if !r.isLearner {
for _, id := range s.Metadata.ConfState.Learners {
if id == r.id {
r.logger.Errorf("%x can't become learner when restores snapshot [index: %d, term: %d]", r.id, s.Metadata.Index, s.Metadata.Term)
return false
}
}
}

r.logger.Infof("%x [commit: %d, lastindex: %d, lastterm: %d] starts to restore snapshot [index: %d, term: %d]",
r.id, r.raftLog.committed, r.raftLog.lastIndex(), r.raftLog.lastTerm(), s.Metadata.Index, s.Metadata.Term)

Expand Down
7 changes: 3 additions & 4 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ func TestLearnerCannotVote(t *testing.T) {
n2.Step(pb.Message{From: 1, To: 2, Term: 2, Type: pb.MsgVote, LogTerm: 11, Index: 11})

if len(n2.msgs) != 0 {
t.Error("n2 is learner, can't vote")
t.Errorf("expect learner not to vote, but received %v messages", n2.msgs)
}
}

Expand Down Expand Up @@ -706,9 +706,8 @@ func TestLearnerLogReplication(t *testing.T) {
if n1.state != StateLeader {
t.Errorf("peer 1 state: %s, want %s", n1.state, StateLeader)
}

if !n2.isLearner {
t.Error("peer 2 is not learner, want yes")
t.Error("peer 2 state: not learner, want yes")
}

nextCommitted := n1.raftLog.committed + 1
Expand All @@ -718,7 +717,7 @@ func TestLearnerLogReplication(t *testing.T) {
}

if n1.raftLog.committed != n2.raftLog.committed {
t.Error("peer 2 must receive the entry from leader, but not")
t.Errorf("peer 2 wants committed to %d, but still %d", n1.raftLog.committed, n2.raftLog.committed)
}

match := n1.getProgress(2).Match
Expand Down

0 comments on commit d20cbf6

Please sign in to comment.