Merge pull request #1897 from xiang90/raft

raft: get rid of the using of defer in critical path
This commit is contained in:
Xiang Li 2014-12-10 21:24:38 -08:00
commit 89cba625d6
2 changed files with 9 additions and 5 deletions

View File

@ -272,11 +272,16 @@ func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
} else if err != nil { } else if err != nil {
panic(err) // TODO(bdarnell) panic(err) // TODO(bdarnell)
} }
ents = append(ents, storedEnts...) ents = storedEnts
} }
if hi > l.unstable.offset { if hi > l.unstable.offset {
unstable := l.unstable.slice(max(lo, l.unstable.offset), hi) unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
if len(ents) > 0 {
ents = append([]pb.Entry{}, ents...)
ents = append(ents, unstable...) ents = append(ents, unstable...)
} else {
ents = unstable
}
} }
return ents return ents
} }

View File

@ -410,12 +410,10 @@ func (r *raft) poll(id uint64, v bool) (granted int) {
} }
func (r *raft) Step(m pb.Message) error { func (r *raft) Step(m pb.Message) error {
// TODO(bmizerany): this likely allocs - prevent that.
defer func() { r.Commit = r.raftLog.committed }()
if m.Type == pb.MsgHup { if m.Type == pb.MsgHup {
log.Printf("raft: %x is starting a new election at term %d", r.id, r.Term) log.Printf("raft: %x is starting a new election at term %d", r.id, r.Term)
r.campaign() r.campaign()
r.Commit = r.raftLog.committed
return nil return nil
} }
@ -437,6 +435,7 @@ func (r *raft) Step(m pb.Message) error {
return nil return nil
} }
r.step(r, m) r.step(r, m)
r.Commit = r.raftLog.committed
return nil return nil
} }