From 26d7877a143be42a09eee19aba48944db410ff78 Mon Sep 17 00:00:00 2001 From: Lei Ni Date: Sat, 8 Aug 2020 15:08:22 +0800 Subject: [PATCH] dragonboat: fixed snapshot compction Previously, snapshot compaction is triggerred everytime when an incoming snapshot is saved. This can potentially delete snapshots that are about to be restored. --- execengine.go | 3 --- node.go | 23 ++++++++++------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/execengine.go b/execengine.go index ed5c2c6c9..551994342 100644 --- a/execengine.go +++ b/execengine.go @@ -977,9 +977,6 @@ func (s *execEngine) onSnapshotSaved(updates []pb.Update, if err := node.removeSnapshotFlagFile(ud.Snapshot.Index); err != nil { return err } - if err := node.compactSnapshots(ud.Snapshot.Index); err != nil { - return err - } } } return nil diff --git a/node.go b/node.go index d64146114..25dab80fd 100644 --- a/node.go +++ b/node.go @@ -700,7 +700,7 @@ func (n *node) compactSnapshot(req rsm.SSRequest, index uint64) error { if index > compactionOverhead { n.ss.setCompactLogTo(index - compactionOverhead) } - return n.snapshotter.Compact(index) + return n.compactSnapshots(index) } func (n *node) getCompactionOverhead(req rsm.SSRequest) uint64 { @@ -771,7 +771,7 @@ func (n *node) recoverFromSnapshot(rec rsm.Task) (uint64, error) { return 0, err } } - if err := n.snapshotter.Compact(index); err != nil { + if err := n.compactSnapshots(index); err != nil { plog.Errorf("%s snapshotter.Compact failed %v", n.id(), err) return 0, err } @@ -857,18 +857,15 @@ func (n *node) shrinkSnapshots(index uint64) error { } func (n *node) compactSnapshots(index uint64) error { - if n.snapshotLock.TryLock() { - defer n.snapshotLock.Unlock() - if err := n.snapshotter.Compact(index); err != nil { - return err - } - n.sysEvents.Publish(server.SystemEvent{ - Type: server.SnapshotCompacted, - ClusterID: n.clusterID, - NodeID: n.nodeID, - Index: index, - }) + if err := n.snapshotter.Compact(index); err != nil { + return err } + n.sysEvents.Publish(server.SystemEvent{ + Type: server.SnapshotCompacted, + ClusterID: n.clusterID, + NodeID: n.nodeID, + Index: index, + }) return nil }