raft: ins: []*index -> []index

It could make raft faster, use less storage.
This commit is contained in:
Yicheng Qin 2014-05-28 16:34:40 -07:00
parent 1170c21f89
commit c1c45575be
2 changed files with 7 additions and 8 deletions

View File

@ -95,7 +95,7 @@ type stateMachine struct {
// the log
log *log
ins []*index
ins []index
state stateType
@ -178,9 +178,9 @@ func (sm *stateMachine) reset() {
sm.lead = none
sm.vote = none
sm.votes = make(map[int]bool)
sm.ins = make([]*index, sm.k)
sm.ins = make([]index, sm.k)
for i := range sm.ins {
sm.ins[i] = &index{next: sm.log.lastIndex() + 1}
sm.ins[i] = index{next: sm.log.lastIndex() + 1}
}
}
@ -273,12 +273,11 @@ func (sm *stateMachine) Step(m Message) {
case stateLeader:
switch m.Type {
case msgAppResp:
in := sm.ins[m.From]
if m.Index < 0 {
in.decr()
sm.ins[m.From].decr()
sm.sendAppend()
} else {
in.update(m.Index)
sm.ins[m.From].update(m.Index)
if sm.maybeCommit() {
sm.sendAppend()
}

View File

@ -331,9 +331,9 @@ func TestCommit(t *testing.T) {
}
for i, tt := range tests {
ins := make([]*index, len(tt.matches))
ins := make([]index, len(tt.matches))
for j := 0; j < len(ins); j++ {
ins[j] = &index{tt.matches[j], tt.matches[j] + 1}
ins[j] = index{tt.matches[j], tt.matches[j] + 1}
}
sm := &stateMachine{log: &log{ents: tt.logs}, ins: ins, k: len(ins), term: tt.smTerm}
sm.maybeCommit()