Skip to content

Commit

Permalink
When installing new snapshot remove last inline if present.
Browse files Browse the repository at this point in the history
Previously we were cleaning up all old ones and doing it 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 <derek@nats.io>
  • Loading branch information
derekcollison committed Jun 18, 2024
1 parent fb839a3 commit 3a56e5a
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 3a56e5a

Please sign in to comment.