Skip to content

Commit

Permalink
Merge pull request etcd-io#11624 from jpbetz/automated-cherry-pick-of…
Browse files Browse the repository at this point in the history
…-#11613-origin-release-3.2

Automated cherry pick of etcd-io#11613 to release-3.2
  • Loading branch information
wenjiaswe committed Feb 13, 2020
2 parents 2d861f3 + a0d497b commit 746c167
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
22 changes: 19 additions & 3 deletions mvcc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,21 +307,36 @@ func (b *backend) defrag() error {
b.batchTx.unsafeCommit(true)
b.batchTx.tx = nil

tmpdb, err := bolt.Open(b.db.Path()+".tmp", 0600, boltOpenOptions)
// Create a temporary file to ensure we start with a clean slate.
// Snapshotter.cleanupSnapdir cleans up any of these that are found during startup.
dir := filepath.Dir(b.db.Path())
temp, err := ioutil.TempFile(dir, "db.tmp.*")
if err != nil {
return err
}
// bbolt v1.3.1-coreos.5 does not provide options.OpenFile so we
// instead close the temp file and then let bolt reopen it.
tdbp := temp.Name()
if err := temp.Close(); err != nil {
return err
}
tmpdb, err := bolt.Open(tdbp, 0600, boltOpenOptions)
if err != nil {
return err
}

// gofail: var defragBeforeCopy struct{}
err = defragdb(b.db, tmpdb, defragLimit)

if err != nil {
tmpdb.Close()
os.RemoveAll(tmpdb.Path())
if rmErr := os.RemoveAll(tmpdb.Path()); rmErr != nil {
plog.Fatalf("failed to remove db.tmp after defragmentation completed: %v", rmErr)
}
return err
}

dbp := b.db.Path()
tdbp := tmpdb.Path()

err = b.db.Close()
if err != nil {
Expand All @@ -331,6 +346,7 @@ func (b *backend) defrag() error {
if err != nil {
plog.Fatalf("cannot close database (%s)", err)
}
// gofail: var defragBeforeRename struct{}
err = os.Rename(tdbp, dbp)
if err != nil {
plog.Fatalf("cannot rename database (%s)", err)
Expand Down
17 changes: 17 additions & 0 deletions snap/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (s *Snapshotter) snapNames() ([]string, error) {
if err != nil {
return nil, err
}
if err = s.cleanupSnapdir(names); err != nil {
return nil, err
}
snaps := checkSuffix(names)
if len(snaps) == 0 {
return nil, ErrNoSnapshot
Expand Down Expand Up @@ -202,3 +205,17 @@ func renameBroken(path string) {
plog.Warningf("cannot rename broken snapshot file %v to %v: %v", path, brokenPath, err)
}
}

// cleanupSnapdir removes any files that should not be in the snapshot directory:
// - db.tmp prefixed files that can be orphaned by defragmentation
func (s *Snapshotter) cleanupSnapdir(filenames []string) error {
for _, filename := range filenames {
if strings.HasPrefix(filename, "db.tmp") {
plog.Infof("found orphaned defragmentation file; deleting: %s", filename)
if rmErr := os.Remove(filepath.Join(s.dir, filename)); rmErr != nil && !os.IsNotExist(rmErr) {
return fmt.Errorf("failed to remove orphaned defragmentation file %s: %v", filename, rmErr)
}
}
}
return nil
}

0 comments on commit 746c167

Please sign in to comment.