Skip to content

Git designed for AI agents.

localrefs: make the watermark durable before a dirty marker goes

Trigodilc3578833e712
diff --git a/internal/graft/corestore/localrefs/durability.go b/internal/graft/corestore/localrefs/durability.go
index 37eb1b1..0071537 100644
--- a/internal/graft/corestore/localrefs/durability.go
+++ b/internal/graft/corestore/localrefs/durability.go
@@ -165,6 +165,8 @@ type Durability struct {
sync func(f *os.File) error
// crashAt is the crash point selected with CrashEnv.
crashAt string
+ // Returns the path of a repository's watermark.
+ watermark func(repoPath string) string
// ownerFiles hold the owner locks, taken by TryOwn. owner is set by
// Activate once the process has recovered the storages: from then on its
@@ -276,6 +278,11 @@ func WithSync(sync func(f *os.File) error) DurabilityOption {
return func(d *Durability) { d.sync = sync }
}
+// WithWatermark lets a dirty repository's watermark be written without fsync.
+func WithWatermark(path func(repoPath string) string) DurabilityOption {
+ return func(d *Durability) { d.watermark = path }
+}
+
// NewDurability returns the deferred durability of the node, or nil when the
// kernel does not expose its boot identity (not Linux) and no option sets one:
// the references are then written durably by every transaction.
@@ -595,6 +602,19 @@ func (d *Durability) Dirty() []string {
return paths
}
+// DefersWatermark reports whether the watermark at path may be written without fsync now: the marker
+// is durable and goes only after the watermark. The caller holds the syncer's repository lock.
+func (d *Durability) DefersWatermark(path string) bool {
+ if !d.Deferring() || d.watermark == nil {
+ return false
+ }
+
+ d.mu.Lock()
+ defer d.mu.Unlock()
+ st := d.repos[path]
+ return st != nil && st.dirty && st.untrusted == nil
+}
+
// RepoOf returns the repository whose state is kept for path.
func (d *Durability) RepoOf(path string) (RepoRef, bool) {
if d == nil {
@@ -699,7 +719,7 @@ func (d *Durability) settleLocked(path string, repo RepoRef) error {
st := d.lockState(path)
defer st.file.Unlock()
- if err := d.syncStack(path, st); err != nil {
+ if err := d.syncStackAndWatermark(path, st); err != nil {
return d.stepErrorLocked(st, repo, path, err)
}
if d.crashAt == crashDurableStep {
@@ -1065,6 +1085,27 @@ func (d *Durability) syncStack(path string, st *refState) error {
return nil
}
+// Makes the stack, then the watermark file and its directory, durable.
+func (d *Durability) syncStackAndWatermark(path string, st *refState) error {
+ if err := d.syncStack(path, st); err != nil {
+ return err
+ }
+ if d.watermark == nil {
+ return nil
+ }
+
+ file := d.watermark(path)
+ for _, p := range []string{file, filepath.Dir(file)} {
+ if err := d.syncPath(p); err != nil {
+ if errors.Is(err, fs.ErrNotExist) {
+ return nil
+ }
+ return err
+ }
+ }
+ return nil
+}
+
// Tracked returns the paths of the repositories that have a state: the dirty
// ones and the clean ones that a live git process still counts on. The durable
// steps visit them all, so that a clean state leaves the map once its last
@@ -1164,7 +1205,7 @@ func (d *Durability) forgetLocked(path string, st *refState, repo RepoRef) (bool
return false, err
}
dropped := false
- if err = d.syncStack(path, st); err != nil {
+ if err = d.syncStackAndWatermark(path, st); err != nil {
err = d.stepErrorLocked(st, repo, path, err)
} else {
dropped, err = d.dropMarker(path, st, true)
diff --git a/internal/graft/corestore/localrefs/durability_test.go b/internal/graft/corestore/localrefs/durability_test.go
index e13f786..7d0ef1a 100644
--- a/internal/graft/corestore/localrefs/durability_test.go
+++ b/internal/graft/corestore/localrefs/durability_test.go
@@ -405,6 +405,132 @@ func TestSettleReportsAFailedWriteAsUntrusted(t *testing.T) {
require.Equal(t, 0.0, testutil.ToFloat64(d.dirty))
}
+func testWatermarkPath(repoPath string) string {
+ return filepath.Join(repoPath, "graft", "storage", "applied")
+}
+
+func TestDefersWatermark(t *testing.T) {
+ t.Parallel()
+
+ t.Run("without the option", func(t *testing.T) {
+ t.Parallel()
+ f := setupDurability(t)
+ d := f.durability(t, "boot-a", 0)
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ require.False(t, d.DefersWatermark(f.path))
+ })
+
+ t.Run("while the repository is dirty", func(t *testing.T) {
+ t.Parallel()
+ f := setupDurability(t)
+ d := f.durability(t, "boot-a", 0, WithWatermark(testWatermarkPath))
+
+ require.False(t, d.DefersWatermark(f.path), "a clean repository")
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ require.True(t, d.DefersWatermark(f.path))
+ require.NoError(t, d.Settle(f.path, false))
+ require.False(t, d.DefersWatermark(f.path), "the marker is gone")
+ })
+
+ t.Run("an untrusted repository", func(t *testing.T) {
+ t.Parallel()
+ f := setupDurability(t)
+ d := f.durability(t, "boot-a", 0, WithWatermark(testWatermarkPath))
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ f.synced.fail(errors.New("input/output error"))
+ require.ErrorIs(t, d.Settle(f.path, false), ErrUntrusted)
+ require.False(t, d.DefersWatermark(f.path))
+ })
+
+ t.Run("a process that does not own the storage", func(t *testing.T) {
+ t.Parallel()
+ f := setupDurability(t)
+ d := f.unowned(t, "boot-a", 0, WithWatermark(testWatermarkPath))
+
+ require.False(t, d.DefersWatermark(f.path))
+ })
+
+ t.Run("a nil durability", func(t *testing.T) {
+ t.Parallel()
+ var d *Durability
+ require.False(t, d.DefersWatermark("/any"))
+ })
+}
+
+func TestSettleMakesTheWatermarkDurableBeforeTheMarkerGoes(t *testing.T) {
+ t.Parallel()
+
+ setup := func(t *testing.T) (durabilityFixture, *Durability, string, *bool) {
+ f := setupDurability(t)
+ watermark := testWatermarkPath(f.path)
+ require.NoError(t, os.MkdirAll(filepath.Dir(watermark), 0o755))
+ require.NoError(t, os.WriteFile(watermark, []byte(`{"repo_id":1,"seq":2}`), 0o644))
+
+ var markerAtSync bool
+ var d *Durability
+ d = f.durability(t, "boot-a", 0, WithWatermark(testWatermarkPath), WithSync(func(file *os.File) error {
+ if file.Name() == watermark {
+ _, err := os.Stat(f.markerPath(t, d))
+ markerAtSync = err == nil
+ }
+ return f.synced.sync(file)
+ }))
+ return f, d, watermark, &markerAtSync
+ }
+
+ t.Run("settle", func(t *testing.T) {
+ t.Parallel()
+ f, d, watermark, markerAtSync := setup(t)
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ require.NoError(t, d.Settle(f.path, false))
+
+ recorded := f.synced.recorded()
+ require.Contains(t, recorded, watermark)
+ require.Contains(t, recorded, filepath.Dir(watermark))
+ require.True(t, *markerAtSync, "the watermark is durable before the marker goes")
+ require.NoFileExists(t, f.markerPath(t, d))
+ })
+
+ t.Run("forget", func(t *testing.T) {
+ t.Parallel()
+ f, d, watermark, markerAtSync := setup(t)
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ require.NoError(t, d.Forget(f.ctx, f.repo, f.path))
+
+ require.Contains(t, f.synced.recorded(), watermark)
+ require.True(t, *markerAtSync)
+ require.NoFileExists(t, f.markerPath(t, d))
+ })
+
+ t.Run("a failed fsync of the watermark", func(t *testing.T) {
+ t.Parallel()
+ f, d, _, _ := setup(t)
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ f.synced.mu.Lock()
+ f.synced.failOnly = filepath.Join("graft", "storage")
+ f.synced.mu.Unlock()
+ f.synced.fail(errors.New("input/output error"))
+
+ require.ErrorIs(t, d.Settle(f.path, false), ErrUntrusted)
+ require.FileExists(t, f.markerPath(t, d))
+ })
+
+ t.Run("no watermark", func(t *testing.T) {
+ t.Parallel()
+ f, d, watermark, _ := setup(t)
+ require.NoError(t, os.RemoveAll(filepath.Dir(watermark)))
+
+ require.NoError(t, d.MarkDirty(f.ctx, f.repo, f.path))
+ require.NoError(t, d.Settle(f.path, false))
+ require.NoFileExists(t, f.markerPath(t, d))
+ })
+}
+
// TestUntrustedSurvivesAProcessRestart verifies that the untrusted state is in
// the marker: a restart of the process in the same boot, whose fsyncs then
// succeed, finds the repository untrusted and never settles it.