Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed May 25, 2023
1 parent 0200669 commit b2d8885
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions memiavl/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,14 @@ func Load(dir string, opts Options) (*DB, error) {
}
}

if opts.TargetVersion > 0 && int64(opts.TargetVersion) < mtree.Version() {
return nil, fmt.Errorf("target version %d is pruned", opts.TargetVersion)
}

wal, err := wal.Open(walPath(dir), &wal.Options{NoCopy: true, NoSync: true})
if err != nil {
return nil, err
}

if opts.TargetVersion == 0 || int64(opts.TargetVersion) > mtree.Version() {
if err := mtree.CatchupWAL(wal, int64(opts.TargetVersion)); err != nil {
_ = wal.Close()
return nil, err
return nil, errors.Join(err, wal.Close())
}
}

Expand Down Expand Up @@ -168,8 +163,7 @@ func Load(dir string, opts Options) (*DB, error) {
upgrades = append(upgrades, &TreeNameUpgrade{Name: name})
}
if err := db.ApplyUpgrades(upgrades); err != nil {
_ = db.Close()
return nil, err
return nil, errors.Join(err, db.Close())
}
}

Expand Down Expand Up @@ -314,6 +308,10 @@ func (db *DB) pruneSnapshots() {

// truncate WAL until the earliest remaining snapshot
earliestVersion, err := firstSnapshotVersion(db.dir)
if err != nil {
db.logger.Error("failed to find first snapshot", "err", err)
}

if err := db.wal.TruncateFront(uint64(earliestVersion + 1)); err != nil {
db.logger.Error("failed to truncate wal", "err", err, "version", earliestVersion+1)
}
Expand Down Expand Up @@ -488,15 +486,15 @@ type snapshotResult struct {
err error
}

// RewriteSnapshotBackground rewrite snapshot in a background goroutine,
// `Commit` will check the complete status, and switch to the new snapshot.
func (db *DB) RewriteSnapshotBackground() error {
db.mtx.Lock()
defer db.mtx.Unlock()

return db.rewriteSnapshotBackground()
}

// rewriteSnapshotBackground rewrite snapshot in a background goroutine,
// `Commit` will check the complete status, and switch to the new snapshot.
func (db *DB) rewriteSnapshotBackground() error {
if db.snapshotRewriteChan != nil {
return errors.New("there's another ongoing snapshot rewriting process")
Expand Down

0 comments on commit b2d8885

Please sign in to comment.