Merge pull request #1887 from xiang90/raft_logging

raft: refactoring logging
This commit is contained in:
Xiang Li 2014-12-08 15:17:20 -08:00
commit a5ec7040e0

View File

@ -98,9 +98,7 @@ func (pr *progress) maybeDecrTo(to uint64) bool {
return true return true
} }
func (pr *progress) String() string { func (pr *progress) String() string { return fmt.Sprintf("next = %d, match = %d", pr.next, pr.match) }
return fmt.Sprintf("n=%d m=%d", pr.next, pr.match)
}
type raft struct { type raft struct {
pb.HardState pb.HardState
@ -136,7 +134,7 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage
if id == None { if id == None {
panic("cannot use none id") panic("cannot use none id")
} }
log := newLog(storage) raftlog := newLog(storage)
hs, cs, err := storage.InitialState() hs, cs, err := storage.InitialState()
if err != nil { if err != nil {
panic(err) // TODO(bdarnell) panic(err) // TODO(bdarnell)
@ -153,7 +151,7 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage
r := &raft{ r := &raft{
id: id, id: id,
lead: None, lead: None,
raftLog: log, raftLog: raftlog,
prs: make(map[uint64]*progress), prs: make(map[uint64]*progress),
electionTimeout: election, electionTimeout: election,
heartbeatTimeout: heartbeat, heartbeatTimeout: heartbeat,
@ -172,7 +170,7 @@ func newRaft(id uint64, peers []uint64, election, heartbeat int, storage Storage
nodesStrs = append(nodesStrs, fmt.Sprintf("%x", n)) nodesStrs = append(nodesStrs, fmt.Sprintf("%x", n))
} }
fmt.Printf("raft: newRaft %x [peers: [%s], term: %d, commit: %d, lastindex: %d, lastterm: %d]", log.Printf("raft: newRaft %x [peers: [%s], term: %d, commit: %d, lastindex: %d, lastterm: %d]",
r.id, strings.Join(nodesStrs, ","), r.Term, r.raftLog.committed, r.raftLog.lastIndex(), r.raftLog.lastTerm()) r.id, strings.Join(nodesStrs, ","), r.Term, r.raftLog.committed, r.raftLog.lastIndex(), r.raftLog.lastTerm())
return r return r
} }
@ -243,8 +241,8 @@ func (r *raft) sendAppend(to uint64) {
} }
m.Snapshot = snapshot m.Snapshot = snapshot
sindex, sterm := snapshot.Metadata.Index, snapshot.Metadata.Term sindex, sterm := snapshot.Metadata.Index, snapshot.Metadata.Term
log.Printf("raft: %x [firstindex: %d, commit: %d] sent snapshot[index: %d, term: %d] to %x [match: %d, next: %d]", log.Printf("raft: %x [firstindex: %d, commit: %d] sent snapshot[index: %d, term: %d] to %x [%s]",
r.id, r.raftLog.firstIndex(), r.Commit, sindex, sterm, to, pr.match, pr.next) r.id, r.raftLog.firstIndex(), r.Commit, sindex, sterm, to, pr)
} else { } else {
m.Type = pb.MsgApp m.Type = pb.MsgApp
m.Index = pr.next - 1 m.Index = pr.next - 1
@ -624,8 +622,8 @@ func (r *raft) restore(s pb.Snapshot) bool {
} else { } else {
match = 0 match = 0
} }
log.Printf("raft: %x restored progress of %x [match: %d, next: %d]", r.id, n, match, next)
r.setProgress(n, match, next) r.setProgress(n, match, next)
log.Printf("raft: %x restored progress of %x [%s]", r.id, n, r.prs[n])
} }
return true return true
} }