Skip to content

Commit

Permalink
etcdserver: separate compact from snapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Clement <[email protected]>
  • Loading branch information
clement2026 committed Jun 26, 2024
1 parent 21e5876 commit 1998a71
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package etcdserver
import (
"context"
"encoding/json"
goerrors "errors"
"expvar"
"fmt"
"math"
Expand Down Expand Up @@ -963,6 +964,7 @@ func (s *EtcdServer) applyAll(ep *etcdProgress, apply *toApply) {
<-apply.notifyc

s.triggerSnapshot(ep)
s.compactRaftLog(ep.appliedi)
select {
// snapshot requested via send()
case m := <-s.r.msgSnapC:
Expand Down Expand Up @@ -2164,18 +2166,24 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) {
lg.Info("skip compaction since there is an inflight snapshot")
return
}
})
}

func (s *EtcdServer) compactRaftLog(appliedi uint64) {
s.GoAttach(func() {
lg := s.Logger()

// keep some in memory log entries for slow followers.
compacti := uint64(1)
if snapi > s.Cfg.SnapshotCatchUpEntries {
compacti = snapi - s.Cfg.SnapshotCatchUpEntries
compacti := uint64(0)
if appliedi > s.Cfg.SnapshotCatchUpEntries {
compacti = appliedi - s.Cfg.SnapshotCatchUpEntries
}

err = s.r.raftStorage.Compact(compacti)
err := s.r.raftStorage.Compact(compacti)
if err != nil {
// the compaction was done asynchronously with the progress of raft.
// raft log might already been compact.
if err == raft.ErrCompacted {
if goerrors.Is(err, raft.ErrCompacted) {
return
}
lg.Panic("failed to compact", zap.Error(err))
Expand Down

0 comments on commit 1998a71

Please sign in to comment.