Skip to content

Commit

Permalink
When installing a new snapshot remove last inline if present. (#5558)
Browse files Browse the repository at this point in the history
Previously (but not in released code) we were cleaning up all old ones
in a go routine. However this could cause multiple go routines to race
and delete the wrong snapshot leaving none available.

Signed-off-by: Derek Collison <[email protected]>
  • Loading branch information
derekcollison authored Jun 18, 2024
2 parents fb839a3 + 3a56e5a commit 39e972d
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions server/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1074,23 +1074,16 @@ func (n *raft) installSnapshot(snap *snapshot) error {
return err
}

// Delete our previous snapshot file if it exists.
if n.snapfile != _EMPTY_ && n.snapfile != sfile {
os.Remove(n.snapfile)
}
// Remember our latest snapshot file.
n.snapfile = sfile
if _, err := n.wal.Compact(snap.lastIndex + 1); err != nil {
n.setWriteErrLocked(err)
return err
}
// Remove any old snapshots.
// Do this in a go routine.
go func() {
psnaps, _ := os.ReadDir(snapDir)
for _, fi := range psnaps {
pn := fi.Name()
if pn != sn {
os.Remove(filepath.Join(snapDir, pn))
}
}
}()

return nil
}
Expand Down

0 comments on commit 39e972d

Please sign in to comment.