mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: move logic to separate func
This commit is contained in:
parent
b07be74a82
commit
1ca03d8e9d
11
raft/raft.go
11
raft/raft.go
@ -265,9 +265,7 @@ func (r *raft) appendEntry(e pb.Entry) {
|
|||||||
|
|
||||||
// tickElection is ran by followers and candidates after r.electionTimeout.
|
// tickElection is ran by followers and candidates after r.electionTimeout.
|
||||||
func (r *raft) tickElection() {
|
func (r *raft) tickElection() {
|
||||||
// promotable indicates whether state machine can be promoted to leader,
|
if !r.promotable() {
|
||||||
// which is true when its own id is in progress list.
|
|
||||||
if _, promotable := r.prs[r.id]; !promotable {
|
|
||||||
r.elapsed = 0
|
r.elapsed = 0
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -536,6 +534,13 @@ func (r *raft) delProgress(id int64) {
|
|||||||
delete(r.prs, id)
|
delete(r.prs, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// promotable indicates whether state machine can be promoted to leader,
|
||||||
|
// which is true when its own id is in progress list.
|
||||||
|
func (r *raft) promotable() bool {
|
||||||
|
_, ok := r.prs[r.id]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
func (r *raft) loadEnts(ents []pb.Entry) {
|
func (r *raft) loadEnts(ents []pb.Entry) {
|
||||||
r.raftLog.load(ents)
|
r.raftLog.load(ents)
|
||||||
}
|
}
|
||||||
|
@ -1053,33 +1053,24 @@ func TestRemoveNode(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTickElectionElapsed(t *testing.T) {
|
func TestPromotable(t *testing.T) {
|
||||||
electionTimeout := 10
|
id := int64(1)
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
promotable bool
|
peers []int64
|
||||||
e int
|
wp bool
|
||||||
we int
|
|
||||||
}{
|
}{
|
||||||
{true, 0, 1},
|
{[]int64{1}, true},
|
||||||
{true, electionTimeout - 1, electionTimeout},
|
{[]int64{1, 2, 3}, true},
|
||||||
{true, electionTimeout, 0},
|
{[]int64{}, false},
|
||||||
{false, 0, 0},
|
{[]int64{2, 3}, false},
|
||||||
{false, 1, 0},
|
|
||||||
}
|
}
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
r := &raft{
|
r := &raft{id: id, prs: make(map[int64]*progress)}
|
||||||
id: 1,
|
for _, id := range tt.peers {
|
||||||
raftLog: newLog(),
|
r.prs[id] = &progress{}
|
||||||
prs: make(map[int64]*progress),
|
|
||||||
electionTimeout: electionTimeout,
|
|
||||||
elapsed: tt.e,
|
|
||||||
}
|
}
|
||||||
if tt.promotable {
|
if g := r.promotable(); g != tt.wp {
|
||||||
r.prs[r.id] = &progress{}
|
t.Errorf("#%d: promotable = %v, want %v", i, g, tt.wp)
|
||||||
}
|
|
||||||
r.tickElection()
|
|
||||||
if r.elapsed != tt.we {
|
|
||||||
t.Errorf("#%d: elapsed = %d, want %d", i, r.elapsed, tt.we)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user