raft: index{} -> progress{}

release-2.0
Blake Mizerany 2014-08-23 16:58:19 -07:00 committed by Yicheng Qin
parent 1b37f313a6
commit 3c483e19d7
2 changed files with 14 additions and 14 deletions

View File

@ -84,22 +84,22 @@ func (m Message) String() string {
m.Type, m.From, m.To, m.Term, m.LogTerm, m.Index, m.Commit, len(m.Entries))
}
type index struct {
type progress struct {
match, next int64
}
func (in *index) update(n int64) {
func (in *progress) update(n int64) {
in.match = n
in.next = n + 1
}
func (in *index) decr() {
func (in *progress) decr() {
if in.next--; in.next < 1 {
in.next = 1
}
}
func (in *index) String() string {
func (in *progress) String() string {
return fmt.Sprintf("n=%d m=%d", in.next, in.match)
}
@ -132,7 +132,7 @@ type raft struct {
// the log
raftLog *raftLog
ins map[int64]*index
ins map[int64]*progress
state stateType
@ -156,9 +156,9 @@ func newStateMachine(id int64, peers []int64) *raft {
if id == none {
panic("cannot use none id")
}
sm := &raft{id: id, lead: none, raftLog: newLog(), ins: make(map[int64]*index)}
sm := &raft{id: id, lead: none, raftLog: newLog(), ins: make(map[int64]*progress)}
for _, p := range peers {
sm.ins[p] = &index{}
sm.ins[p] = &progress{}
}
sm.reset(0)
return sm
@ -279,7 +279,7 @@ func (sm *raft) reset(term int64) {
sm.setVote(none)
sm.votes = make(map[int64]bool)
for i := range sm.ins {
sm.ins[i] = &index{next: sm.raftLog.lastIndex() + 1}
sm.ins[i] = &progress{next: sm.raftLog.lastIndex() + 1}
if i == sm.id {
sm.ins[i].match = sm.raftLog.lastIndex()
}
@ -500,7 +500,7 @@ func (sm *raft) restore(s Snapshot) bool {
sm.raftLog.restore(s)
sm.LastIndex = sm.raftLog.lastIndex()
sm.ins = make(map[int64]*index)
sm.ins = make(map[int64]*progress)
for _, n := range s.Nodes {
if n == sm.id {
sm.addIns(n, sm.raftLog.lastIndex(), sm.raftLog.lastIndex()+1)
@ -541,7 +541,7 @@ func (sm *raft) setVote(vote int64) {
}
func (sm *raft) addIns(id, match, next int64) {
sm.ins[id] = &index{next: next, match: match}
sm.ins[id] = &progress{next: next, match: match}
sm.saveState()
}

View File

@ -437,9 +437,9 @@ func TestCommit(t *testing.T) {
}
for i, tt := range tests {
ins := make(map[int64]*index)
ins := make(map[int64]*progress)
for j := 0; j < len(tt.matches); j++ {
ins[int64(j)] = &index{tt.matches[j], tt.matches[j] + 1}
ins[int64(j)] = &progress{tt.matches[j], tt.matches[j] + 1}
}
sm := &raft{raftLog: &raftLog{ents: tt.logs}, ins: ins, State: State{Term: tt.smTerm}}
sm.maybeCommit()
@ -936,9 +936,9 @@ func newNetwork(peers ...Interface) *network {
npeers[nid] = sm
case *raft:
v.id = nid
v.ins = make(map[int64]*index)
v.ins = make(map[int64]*progress)
for i := 0; i < size; i++ {
v.ins[int64(i)] = &index{}
v.ins[int64(i)] = &progress{}
}
v.reset(0)
npeers[nid] = v