Skip to content

Commit

Permalink
cleanup old snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jul 12, 2023
1 parent a0beaa1 commit 8129ded
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions memiavl/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ type Options struct {

const (
SnapshotPrefix = "snapshot-"
TmpSuffix = "-tmp"
SnapshotDirLen = len(SnapshotPrefix) + 20
)

Expand Down Expand Up @@ -125,7 +126,7 @@ func Load(dir string, opts Options) (*DB, error) {
}

// cleanup any temporary directories left by interrupted snapshot rewrite
if err := removeTmpDirs(dir); err != nil {
if err := removeTmpDirs(dir, opts.TargetVersion); err != nil {
return nil, fmt.Errorf("fail to cleanup tmp directories: %w", err)
}

Expand Down Expand Up @@ -211,17 +212,23 @@ func Load(dir string, opts Options) (*DB, error) {
return db, nil
}

func removeTmpDirs(rootDir string) error {
func removeTmpDirs(rootDir string, targetVersion uint32) error {
entries, err := os.ReadDir(rootDir)
if err != nil {
return err
}

for _, entry := range entries {
if !entry.IsDir() || !strings.HasSuffix(entry.Name(), "-tmp") {
if !entry.IsDir() {
continue
}
version, err := parseTmpVersion(entry.Name())
if err != nil {
continue
}
if version > int64(targetVersion) {
continue
}

if err := os.RemoveAll(filepath.Join(rootDir, entry.Name())); err != nil {
return err
}
Expand Down Expand Up @@ -482,7 +489,7 @@ func (db *DB) RewriteSnapshot() error {
defer db.mtx.Unlock()

snapshotDir := snapshotName(db.lastCommitInfo.Version)
tmpDir := snapshotDir + "-tmp"
tmpDir := snapshotDir + TmpSuffix
path := filepath.Join(db.dir, tmpDir)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return err
Expand Down Expand Up @@ -693,6 +700,13 @@ func parseVersion(name string) (int64, error) {
return v, nil
}

func parseTmpVersion(name string) (int64, error) {
if !isSnapshotTmpName(name) {
return 0, fmt.Errorf("invalid snapshot tmp name %s", name)
}
return parseVersion(name[:len(name)-len(TmpSuffix)])
}

// seekSnapshot find the biggest snapshot version that's smaller than or equal to the target version,
// returns 0 if not found.
func seekSnapshot(root string, targetVersion uint32) (int64, error) {
Expand Down Expand Up @@ -809,7 +823,7 @@ func traverseSnapshots(dir string, ascending bool, callback func(int64) (bool, e

// atomicRemoveDir is equavalent to `mv snapshot snapshot-tmp && rm -r snapshot-tmp`
func atomicRemoveDir(path string) error {
tmpPath := path + "-tmp"
tmpPath := path + TmpSuffix
if err := os.Rename(path, tmpPath); err != nil {
return err
}
Expand All @@ -826,6 +840,10 @@ func isSnapshotName(name string) bool {
return strings.HasPrefix(name, SnapshotPrefix) && len(name) == SnapshotDirLen
}

func isSnapshotTmpName(name string) bool {
return strings.HasSuffix(name, TmpSuffix) && isSnapshotName(name[:len(name)-len(TmpSuffix)])
}

// GetLatestVersion finds the latest version number without loading the whole db,
// it's needed for upgrade module to check store upgrades,
// it returns 0 if db don't exists or is empty.
Expand Down

0 comments on commit 8129ded

Please sign in to comment.