Skip to content

Commit

Permalink
refactoring removeOldLogs to use common compactLogs code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmurret committed Mar 17, 2023
1 parent c12c1a2 commit 137d9a5
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,10 @@ func (r *Raft) takeSnapshot() (string, error) {
return sink.ID(), nil
}

// compactLogs takes the last inclusive index of a snapshot
// and trims the logs that are no longer needed.
func (r *Raft) compactLogs(snapIdx uint64) error {
defer metrics.MeasureSince([]string{"raft", "compactLogs"}, time.Now())
// compactLogsToTrailingLogIdx takes the last inclusive index of a snapshot
// and trailingLogs and trims the logs that are no longer needed up to the
// trailingLogIdx.
func (r *Raft) compactLogsToTrailingLogIdx(snapIdx uint64, trailingLogs uint64) error {
// Determine log ranges to compact
minLog, err := r.logs.FirstIndex()
if err != nil {
Expand All @@ -225,7 +225,6 @@ func (r *Raft) compactLogs(snapIdx uint64) error {

// Use a consistent value for trailingLogs for the duration of this method
// call to avoid surprising behaviour.
trailingLogs := r.config().TrailingLogs
if lastLogIdx <= trailingLogs {
return nil
}
Expand All @@ -250,28 +249,25 @@ func (r *Raft) compactLogs(snapIdx uint64) error {
return nil
}

// compactLogs takes the last inclusive index of a snapshot
// and trims the logs that are no longer needed.
func (r *Raft) compactLogs(snapIdx uint64) error {
defer metrics.MeasureSince([]string{"raft", "compactLogs"}, time.Now())
return r.compactLogsToTrailingLogIdx(snapIdx, r.config().TrailingLogs)
}

// removeOldLogs removes all old logs from the store. This is used for
// MonotonicLogStores after restore. Callers should verify that the store
// implementation is monotonic prior to calling.
func (r *Raft) removeOldLogs() error {
defer metrics.MeasureSince([]string{"raft", "removeOldLogs"}, time.Now())

// Determine log ranges to truncate
firstLogIdx, err := r.logs.FirstIndex()
if err != nil {
return fmt.Errorf("failed to get first log index: %w", err)
}

lastLogIdx, err := r.logs.LastIndex()
if err != nil {
return fmt.Errorf("failed to get last log index: %w", err)
}

r.logger.Info("removing all old logs from log store", "first", firstLogIdx, "last", lastLogIdx)

if err := r.logs.DeleteRange(firstLogIdx, lastLogIdx); err != nil {
return fmt.Errorf("log truncation failed: %v", err)
}
r.logger.Info("removing all old logs from log store")

return nil
return r.compactLogsToTrailingLogIdx(lastLogIdx, 0)
}

0 comments on commit 137d9a5

Please sign in to comment.