Merge pull request #43118 from yujuhong/sync_checkpoint
Automatic merge from submit-queue dockershim: call sync() after writing the checkpoint This ensures the checkpoint files are persisted. This fixes #43021
This commit is contained in:
		@@ -18,6 +18,7 @@ package dockershim
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"io"
 | 
				
			||||||
	"io/ioutil"
 | 
						"io/ioutil"
 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"path/filepath"
 | 
						"path/filepath"
 | 
				
			||||||
@@ -64,6 +65,24 @@ func NewFileStore(path string) (CheckpointStore, error) {
 | 
				
			|||||||
	return &FileStore{path: path}, nil
 | 
						return &FileStore{path: path}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// writeFileAndSync is copied from ioutil.WriteFile, with the extra File.Sync
 | 
				
			||||||
 | 
					// at the end to ensure file is written on the disk.
 | 
				
			||||||
 | 
					func writeFileAndSync(filename string, data []byte, perm os.FileMode) error {
 | 
				
			||||||
 | 
						f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						n, err := f.Write(data)
 | 
				
			||||||
 | 
						if err == nil && n < len(data) {
 | 
				
			||||||
 | 
							err = io.ErrShortWrite
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						f.Sync()
 | 
				
			||||||
 | 
						if err1 := f.Close(); err == nil {
 | 
				
			||||||
 | 
							err = err1
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return err
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (fstore *FileStore) Write(key string, data []byte) error {
 | 
					func (fstore *FileStore) Write(key string, data []byte) error {
 | 
				
			||||||
	if err := validateKey(key); err != nil {
 | 
						if err := validateKey(key); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
@@ -72,7 +91,7 @@ func (fstore *FileStore) Write(key string, data []byte) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	tmpfile := filepath.Join(fstore.path, fmt.Sprintf("%s%s%s", tmpPrefix, key, tmpSuffix))
 | 
						tmpfile := filepath.Join(fstore.path, fmt.Sprintf("%s%s%s", tmpPrefix, key, tmpSuffix))
 | 
				
			||||||
	if err := ioutil.WriteFile(tmpfile, data, 0644); err != nil {
 | 
						if err := writeFileAndSync(tmpfile, data, 0644); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return os.Rename(tmpfile, fstore.getCheckpointPath(key))
 | 
						return os.Rename(tmpfile, fstore.getCheckpointPath(key))
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user