raft: add sm.Index

release-2.0
Xiang Li 2014-07-10 23:07:22 -07:00 committed by Yicheng Qin
parent 0886e0ddf4
commit 2ff3ce74c5
2 changed files with 6 additions and 3 deletions

View File

@ -45,7 +45,7 @@ func (n *Node) Id() int64 {
return atomic.LoadInt64(&n.sm.id)
}
func (n *Node) Index() int64 { return n.sm.log.lastIndex() }
func (n *Node) Index() int64 { return n.sm.index.Get() }
func (n *Node) Term() int64 { return n.sm.term.Get() }

View File

@ -112,7 +112,8 @@ type stateMachine struct {
id int64
// the term we are participating in at any time
term atomicInt
term atomicInt
index atomicInt
// who we voted for in term
vote int64
@ -233,7 +234,7 @@ func (sm *stateMachine) q() int {
func (sm *stateMachine) appendEntry(e Entry) {
e.Term = sm.term.Get()
sm.log.append(sm.log.lastIndex(), e)
sm.index.Set(sm.log.append(sm.log.lastIndex(), e))
sm.ins[sm.id].update(sm.log.lastIndex())
sm.maybeCommit()
}
@ -319,6 +320,7 @@ func (sm *stateMachine) Step(m Message) (ok bool) {
func (sm *stateMachine) handleAppendEntries(m Message) {
if sm.log.maybeAppend(m.Index, m.LogTerm, m.Commit, m.Entries...) {
sm.index.Set(sm.log.lastIndex())
sm.send(Message{To: m.From, Type: msgAppResp, Index: sm.log.lastIndex()})
} else {
sm.send(Message{To: m.From, Type: msgAppResp, Index: -1})
@ -443,6 +445,7 @@ func (sm *stateMachine) restore(s Snapshot) {
}
sm.log.restore(s.Index, s.Term)
sm.index.Set(sm.log.lastIndex())
sm.ins = make(map[int64]*index)
for _, n := range s.Nodes {
sm.ins[n] = &index{next: sm.log.lastIndex() + 1}