Raft Snapshot Compaction

medium · distributed-systems, consensus, raft, storage

Raft Snapshot Compaction

A Raft leader or follower may compact its log after installing a snapshot. The snapshot includes the last included index and term.

Given the current log (terms per entry) and a snapshot index/term, discard all entries with index <= snapshotIndex and return the remaining log terms. Indices are 1-based.

Function signature

func CompactLog(logTerms []int, snapshotIndex int, snapshotTerm int) []int

Example

logTerms      = [1,1,2,2,3]
snapshotIndex = 3
snapshotTerm  = 2
result        = [2,3]   // entries 4..5 remain

Constraints

  • 0 <= len(logTerms) <= 100000
  • 0 <= snapshotIndex <= len(logTerms)
  • 0 <= snapshotTerm <= 1_000_000

Notes

  • If snapshotIndex == 0, return the log unchanged.
  • If snapshotIndex >= len(logTerms), return an empty log.
Run tests to see results
No issues detected