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

Commit 02785c9

Browse files
committed
checkpoint: clean up orphaned checkpoints.
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()`.
1 parent 74d2cff commit 02785c9

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

pkg/checkpoint/manifest.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io/ioutil"
66
"os"
77
"path/filepath"
8+
"strings"
89

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

2425
for _, f := range fi {
2526
manifest := filepath.Join(path, f.Name())
27+
28+
// Check for leftover temporary checkpoints.
29+
if strings.HasPrefix(filepath.Base(manifest), ".") {
30+
glog.V(4).Infof("Found temporary checkpoint %s, removing.", manifest)
31+
if err := os.Remove(manifest); err != nil {
32+
glog.V(4).Infof("Error removing temporary checkpoint %s: %v.", manifest, err)
33+
}
34+
continue
35+
}
36+
2637
b, err := ioutil.ReadFile(manifest)
2738
if err != nil {
2839
glog.Errorf("Error reading manifest: %v", err)
@@ -71,9 +82,19 @@ func writeManifestIfDifferent(path, name string, data []byte) (bool, error) {
7182
}
7283

7384
func writeAndAtomicRename(path string, data []byte, perm os.FileMode) error {
74-
tmpfile := filepath.Join(filepath.Dir(path), "."+filepath.Base(path))
75-
if err := ioutil.WriteFile(tmpfile, data, perm); err != nil {
85+
// Ensure that the temporary file is on the same filesystem so that os.Rename() does not error.
86+
tmpfile, err := ioutil.TempFile(filepath.Dir(path), ".")
87+
if err != nil {
88+
return err
89+
}
90+
if _, err := tmpfile.Write(data); err != nil {
91+
return err
92+
}
93+
if err := tmpfile.Close(); err != nil {
94+
return err
95+
}
96+
if err := tmpfile.Chmod(perm); err != nil {
7697
return err
7798
}
78-
return os.Rename(tmpfile, path)
99+
return os.Rename(tmpfile.Name(), path)
79100
}

0 commit comments

Comments
 (0)