harden(durability): fsync atomic writes before publishing the rename 💽
A power loss between rename and the data hitting disk could leave a truncated/ empty file despite the atomic rename — breaking 'never lose a byte'. Now both atomic-write paths f.Sync() the temp file before rename and best-effort fsync the parent dir after (so the rename entry is durable too): - internal/syncer/pull.go atomicWrite (pulled file content) - internal/hub/blobstore Disk.Put (stored blobs) Dir fsync is best-effort (unsupported on Windows). [fanned-out subagent: harden-fsync]
This commit is contained in:
parent
5a42661522
commit
4b6a4b97b2
3 changed files with 56 additions and 2 deletions
|
|
@ -103,12 +103,26 @@ func (d *Disk) Put(hash string, data []byte) error {
|
||||||
tmp.Close()
|
tmp.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Flush the blob's bytes to the platter before the rename publishes it, so a
|
||||||
|
// crash can't leave a truncated file masquerading as a complete blob.
|
||||||
|
if err := tmp.Sync(); err != nil {
|
||||||
|
tmp.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := tmp.Close(); err != nil {
|
if err := tmp.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// Rename to a non-existent final path: atomic on POSIX, and on Windows too
|
// Rename to a non-existent final path: atomic on POSIX, and on Windows too
|
||||||
// since we never rename over an existing file (Put is a no-op when present).
|
// since we never rename over an existing file (Put is a no-op when present).
|
||||||
return os.Rename(tmpName, d.path(hash))
|
if err := os.Rename(tmpName, d.path(hash)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// fsync the shard dir so the rename entry itself survives a crash.
|
||||||
|
if df, derr := os.Open(dir); derr == nil {
|
||||||
|
_ = df.Sync() // ponytail: dir fsync is best-effort; unsupported on Windows
|
||||||
|
df.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get returns the blob bytes for hash, or ErrNotFound.
|
// Get returns the blob bytes for hash, or ErrNotFound.
|
||||||
|
|
|
||||||
26
internal/syncer/fsync_test.go
Normal file
26
internal/syncer/fsync_test.go
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
package syncer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestAtomicWriteWithSync proves the fsync durability path doesn't break a write:
|
||||||
|
// the file lands with the exact content and mode, and returns no error. We can't
|
||||||
|
// simulate a power loss, so this is a regression guard against the sync calls
|
||||||
|
// corrupting or failing an ordinary write.
|
||||||
|
func TestAtomicWriteWithSync(t *testing.T) {
|
||||||
|
dst := filepath.Join(t.TempDir(), "hello.txt")
|
||||||
|
want := []byte("never lose a byte")
|
||||||
|
if err := atomicWrite(dst, want, 0o644); err != nil {
|
||||||
|
t.Fatalf("atomicWrite: %v", err)
|
||||||
|
}
|
||||||
|
got, err := os.ReadFile(dst)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read back: %v", err)
|
||||||
|
}
|
||||||
|
if string(got) != string(want) {
|
||||||
|
t.Fatalf("content = %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -437,11 +437,25 @@ func atomicWrite(path string, data []byte, perm os.FileMode) error {
|
||||||
f.Close()
|
f.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
// Flush the data to the platter before the rename publishes it: a power loss
|
||||||
|
// after the rename but before the bytes land would leave a truncated/empty file.
|
||||||
|
if err := f.Sync(); err != nil {
|
||||||
|
f.Close()
|
||||||
|
return err
|
||||||
|
}
|
||||||
if err := f.Close(); err != nil {
|
if err := f.Close(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := os.Chmod(tmp, perm); err != nil {
|
if err := os.Chmod(tmp, perm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Rename(tmp, path)
|
if err := os.Rename(tmp, path); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// fsync the parent dir so the rename entry itself survives a crash.
|
||||||
|
if dir, derr := os.Open(filepath.Dir(path)); derr == nil {
|
||||||
|
_ = dir.Sync() // ponytail: dir fsync is best-effort; unsupported on Windows
|
||||||
|
dir.Close()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue