keep recent log entries after snapshot

pull/820/head
Xiang Li 2013-07-16 13:16:33 -07:00
parent 10f8b9f836
commit 5c9cec3f74
2 changed files with 29 additions and 2 deletions

19
log.go
View File

@ -107,6 +107,25 @@ func (l *Log) lastCommandName() string {
return ""
}
// Get the log entry by index
func (l *Log) getLogEntry(index uint64) *LogEntry {
l.mutex.RLock()
defer l.mutex.RUnlock()
if index <= l.startIndex {
return nil
}
index = index - l.startIndex - 1
// copy the useful information of the log entry
return &LogEntry{
Index: l.entries[index].Index,
Term: l.entries[index].Term,
Command: l.entries[index].Command,
}
}
//--------------------------------------
// Log Terms
//--------------------------------------

View File

@ -28,7 +28,8 @@ const (
)
const (
MaxLogEntriesPerRequest = 200
MaxLogEntriesPerRequest = 200
NumberOfLogEntreisAfterSnapshot = 200
)
const (
@ -922,7 +923,14 @@ func (s *Server) takeSnapshot() error {
s.saveSnapshot()
s.log.compact(lastIndex, lastTerm)
// We keep some log entries after the snapshot
// We do not want to send the whole snapshot
// to the slightly slow machines
if lastIndex > NumberOfLogEntreisAfterSnapshot {
compactIndex := lastIndex - NumberOfLogEntreisAfterSnapshot
compactTerm := s.log.getLogEntry(compactIndex).Term
s.log.compact(compactIndex, compactTerm)
}
return nil
}