Skip to content
This repository has been archived by the owner on Jul 30, 2021. It is now read-only.

Commit

Permalink
checkpoint: clean up orphaned checkpoints.
Browse files Browse the repository at this point in the history
If the checkpointer crashes during `writeAndAtomicRename()` then it
may leave temporary files that start with `.`. This checks for and
removes them.

Also changes `writeAndAtomicRename()` to use `ioutil.TempFile()`.
  • Loading branch information
diegs committed Nov 1, 2017
1 parent 74d2cff commit 02785c9
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions pkg/checkpoint/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/golang/glog"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -23,6 +24,16 @@ func getFileCheckpoints(path string) map[string]*v1.Pod {

for _, f := range fi {
manifest := filepath.Join(path, f.Name())

// Check for leftover temporary checkpoints.
if strings.HasPrefix(filepath.Base(manifest), ".") {
glog.V(4).Infof("Found temporary checkpoint %s, removing.", manifest)
if err := os.Remove(manifest); err != nil {
glog.V(4).Infof("Error removing temporary checkpoint %s: %v.", manifest, err)
}
continue
}

b, err := ioutil.ReadFile(manifest)
if err != nil {
glog.Errorf("Error reading manifest: %v", err)
Expand Down Expand Up @@ -71,9 +82,19 @@ func writeManifestIfDifferent(path, name string, data []byte) (bool, error) {
}

func writeAndAtomicRename(path string, data []byte, perm os.FileMode) error {
tmpfile := filepath.Join(filepath.Dir(path), "."+filepath.Base(path))
if err := ioutil.WriteFile(tmpfile, data, perm); err != nil {
// Ensure that the temporary file is on the same filesystem so that os.Rename() does not error.
tmpfile, err := ioutil.TempFile(filepath.Dir(path), ".")
if err != nil {
return err
}
if _, err := tmpfile.Write(data); err != nil {
return err
}
if err := tmpfile.Close(); err != nil {
return err
}
if err := tmpfile.Chmod(perm); err != nil {
return err
}
return os.Rename(tmpfile, path)
return os.Rename(tmpfile.Name(), path)
}

0 comments on commit 02785c9

Please sign in to comment.