Skip to content

Commit

Permalink
etcdserver: skip compaction if there are snapshots being created
Browse files Browse the repository at this point in the history
Signed-off-by: Clement <[email protected]>
  • Loading branch information
clement2026 committed Jun 27, 2024
1 parent 8dc2a42 commit 641eb41
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ type Server interface {
type EtcdServer struct {
// inflightSnapshots holds count the number of snapshots currently inflight.
inflightSnapshots int64 // must use atomic operations to access; keep 64-bit aligned.
creatingSnapshots int64 // must use atomic operations to access; keep 64-bit aligned.
appliedIndex uint64 // must use atomic operations to access; keep 64-bit aligned.
committedIndex uint64 // must use atomic operations to access; keep 64-bit aligned.
term uint64 // must use atomic operations to access; keep 64-bit aligned.
Expand Down Expand Up @@ -2128,7 +2129,11 @@ func (s *EtcdServer) snapshot(snapi uint64, confState raftpb.ConfState) {
// the go routine created below.
s.KV().Commit()

atomic.AddInt64(&s.creatingSnapshots, 1)
s.GoAttach(func() {
defer func() {
atomic.AddInt64(&s.creatingSnapshots, -1)
}()
lg := s.Logger()

// For backward compatibility, generate v2 snapshot from v3 state.
Expand Down Expand Up @@ -2172,6 +2177,14 @@ func (s *EtcdServer) compactRaftLog(appliedi uint64) {
return
}

// If there are snapshots being created, skip compaction until they are done.
// This ensures `s.r.raftStorage.Compact` does not remove elements from `s.r.raftStorage.Ents`,
// preventing `s.r.raftStorage.CreateSnapshot` from causing a panic.
if atomic.LoadInt64(&s.creatingSnapshots) != 0 {
lg.Info("skip compaction since there are snapshots being created")
return
}

s.GoAttach(func() {
// keep some in memory log entries for slow followers.
compacti := uint64(0)
Expand All @@ -2188,7 +2201,7 @@ func (s *EtcdServer) compactRaftLog(appliedi uint64) {
}
lg.Panic("failed to compact", zap.Error(err))
}
lg.Debug(
lg.Info(
"compacted Raft logs",
zap.Uint64("compact-index", compacti),
)
Expand Down

0 comments on commit 641eb41

Please sign in to comment.