Skip to content

Commit

Permalink
mvcc/backend: Delete orphaned db.tmp files before defrag
Browse files Browse the repository at this point in the history
  • Loading branch information
jpbetz committed Feb 12, 2020
1 parent 61f2794 commit 07a26f0
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions mvcc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,16 @@ func (b *backend) defrag() error {

b.batchTx.tx = nil

tmpdb, err := bolt.Open(b.db.Path()+".tmp", 0600, boltOpenOptions)
tmpdbPath := b.db.Path() + ".tmp"
if _, err := os.Stat(tmpdbPath); err == nil {
// Ensure we start with a clean slate since
// db.tmp files can be orphaned if etcd is terminated
// before defrag completes
if rmErr := os.Remove(tmpdbPath); rmErr != nil && !os.IsNotExist(rmErr) {
return fmt.Errorf("failed to remove db.tmp before starting defragmentation: %v", err)
}
}
tmpdb, err := bolt.Open(tmpdbPath, 0600, boltOpenOptions)
if err != nil {
return err
}
Expand All @@ -376,12 +385,12 @@ func (b *backend) defrag() error {
zap.String("current-db-size-in-use", humanize.Bytes(uint64(sizeInUse1))),
)
}

// gofail: var defragBeforeCopy struct{}
err = defragdb(b.db, tmpdb, defragLimit)
if err != nil {
tmpdb.Close()
if rmErr := os.RemoveAll(tmpdb.Path()); rmErr != nil {
b.lg.Error("failed to remove dirs under tmpdb", zap.Error(rmErr))
b.lg.Error("failed to remove db.tmp after defragmentation completed", zap.Error(rmErr))
}
return err
}
Expand All @@ -394,6 +403,7 @@ func (b *backend) defrag() error {
if err != nil {
b.lg.Fatal("failed to close tmp database", zap.Error(err))
}
// gofail: var defragBeforeRename struct{}
err = os.Rename(tdbp, dbp)
if err != nil {
b.lg.Fatal("failed to rename tmp database", zap.Error(err))
Expand Down

0 comments on commit 07a26f0

Please sign in to comment.