Skip to content

Git designed for AI agents.

syncer: write the watermark of a dirty repository without fsync

Trigodil083997e0c23c
diff --git a/internal/cli/gitaly/serve.go b/internal/cli/gitaly/serve.go
index efac6ac..2ee5e8b 100644
--- a/internal/cli/gitaly/serve.go
+++ b/internal/cli/gitaly/serve.go
@@ -39,6 +39,7 @@ import (
"github.com/feder-cr/git-ai/internal/graft/corestore/checkpoint"
"github.com/feder-cr/git-ai/internal/graft/corestore/gate"
"github.com/feder-cr/git-ai/internal/graft/corestore/gc"
+ "github.com/feder-cr/git-ai/internal/graft/corestore/gitops"
"github.com/feder-cr/git-ai/internal/graft/corestore/localrefs"
storagemaintenance "github.com/feder-cr/git-ai/internal/graft/corestore/maintenance"
corestoreopen "github.com/feder-cr/git-ai/internal/graft/corestore/open"
@@ -989,7 +990,7 @@ func openLocalReferenceWrites(core *corestore.Core, locator storage.Locator, log
core.RefAdmission = localrefs.NewAdmission(lockTimeout, lockTimeout)
prometheus.MustRegister(core.RefAdmission)
- durability, err := localrefs.NewDurability(locator, localrefs.DurableStepIdle)
+ durability, err := localrefs.NewDurability(locator, localrefs.DurableStepIdle, localrefs.WithWatermark(gitops.WatermarkPath))
if err != nil {
return fmt.Errorf("graft storage core: %w", err)
}
diff --git a/internal/graft/corestore/coretest/coretest.go b/internal/graft/corestore/coretest/coretest.go
index e2dc835..612c55b 100644
--- a/internal/graft/corestore/coretest/coretest.go
+++ b/internal/graft/corestore/coretest/coretest.go
@@ -23,6 +23,7 @@ import (
"github.com/feder-cr/git-ai/internal/gitaly/config"
"github.com/feder-cr/git-ai/internal/graft/corestore"
"github.com/feder-cr/git-ai/internal/graft/corestore/blobstore"
+ "github.com/feder-cr/git-ai/internal/graft/corestore/gitops"
"github.com/feder-cr/git-ai/internal/graft/corestore/localrefs"
"github.com/feder-cr/git-ai/internal/graft/corestore/sqlstore"
"github.com/stretchr/testify/require"
@@ -212,7 +213,7 @@ func EnableLocalRefs(tb testing.TB, core *corestore.Core, cfg config.Cfg, opts .
require.NoError(tb, core.RefDurability.Close())
}
core.RefAdmission = localrefs.NewAdmission(10*time.Second, 10*time.Second)
- durability, err := localrefs.NewDurability(config.NewLocator(cfg), 0, append([]localrefs.DurabilityOption{localrefs.WithBootID(TestBootID), localrefs.WithRecoveryGrace(0)}, opts...)...)
+ durability, err := localrefs.NewDurability(config.NewLocator(cfg), 0, append([]localrefs.DurabilityOption{localrefs.WithBootID(TestBootID), localrefs.WithRecoveryGrace(0), localrefs.WithWatermark(gitops.WatermarkPath)}, opts...)...)
require.NoError(tb, err)
// A git process forked at the moment a previous durability closed its lock
// holds a copy of it until it execs (close-on-exec): the lock can be busy for
diff --git a/internal/graft/corestore/gitops/gitops.go b/internal/graft/corestore/gitops/gitops.go
index b20386e..1199b3c 100644
--- a/internal/graft/corestore/gitops/gitops.go
+++ b/internal/graft/corestore/gitops/gitops.go
@@ -892,11 +892,21 @@ func ReadWatermark(repoPath string) (w corestore.Watermark, ok bool, err error)
// WriteWatermark writes the watermark atomically (temporary file, fsync,
// rename, fsync of the directory), creating graft/storage when needed.
-func WriteWatermark(repoPath string, w corestore.Watermark) (returnedErr error) {
+func WriteWatermark(repoPath string, w corestore.Watermark) error {
dir := filepath.Dir(WatermarkPath(repoPath))
if err := os.MkdirAll(dir, mode.Directory); err != nil {
return fmt.Errorf("write watermark: %w", err)
}
+ return writeWatermark(repoPath, w, true)
+}
+
+// WriteWatermarkNoSync writes the watermark without fsync, into an existing directory.
+func WriteWatermarkNoSync(repoPath string, w corestore.Watermark) error {
+ return writeWatermark(repoPath, w, false)
+}
+
+func writeWatermark(repoPath string, w corestore.Watermark, sync bool) (returnedErr error) {
+ dir := filepath.Dir(WatermarkPath(repoPath))
data, err := json.Marshal(w)
if err != nil {
@@ -918,8 +928,10 @@ func WriteWatermark(repoPath string, w corestore.Watermark) (returnedErr error)
if _, err := tmp.Write(data); err != nil {
return fmt.Errorf("write watermark: %w", err)
}
- if err := tmp.Sync(); err != nil {
- return fmt.Errorf("sync watermark: %w", err)
+ if sync {
+ if err := tmp.Sync(); err != nil {
+ return fmt.Errorf("sync watermark: %w", err)
+ }
}
if err := tmp.Close(); err != nil {
return fmt.Errorf("close watermark: %w", err)
@@ -927,6 +939,9 @@ func WriteWatermark(repoPath string, w corestore.Watermark) (returnedErr error)
if err := os.Rename(tmp.Name(), WatermarkPath(repoPath)); err != nil {
return fmt.Errorf("rename watermark: %w", err)
}
+ if !sync {
+ return nil
+ }
d, err := os.Open(dir)
if err != nil {
diff --git a/internal/graft/corestore/gitops/gitops_test.go b/internal/graft/corestore/gitops/gitops_test.go
index b2ad2ce..a82bdcd 100644
--- a/internal/graft/corestore/gitops/gitops_test.go
+++ b/internal/graft/corestore/gitops/gitops_test.go
@@ -533,6 +533,24 @@ func TestWatermarkRoundTrip(t *testing.T) {
require.Equal(t, "applied", entries[0].Name())
}
+func TestWriteWatermarkNoSync(t *testing.T) {
+ t.Parallel()
+
+ repoPath := t.TempDir()
+ require.Error(t, gitops.WriteWatermarkNoSync(repoPath, corestore.Watermark{RepoID: 7, Seq: 1}), "it does not create the directory")
+
+ require.NoError(t, gitops.WriteWatermark(repoPath, corestore.Watermark{RepoID: 7, Seq: 1}))
+ require.NoError(t, gitops.WriteWatermarkNoSync(repoPath, corestore.Watermark{RepoID: 7, Seq: 2}))
+ w, ok, err := gitops.ReadWatermark(repoPath)
+ require.NoError(t, err)
+ require.True(t, ok)
+ require.Equal(t, corestore.Watermark{RepoID: 7, Seq: 2}, w)
+
+ entries, err := os.ReadDir(filepath.Dir(gitops.WatermarkPath(repoPath)))
+ require.NoError(t, err)
+ require.Len(t, entries, 1)
+}
+
func TestWatermarkCorrupt(t *testing.T) {
t.Parallel()
diff --git a/internal/graft/corestore/syncer/syncer.go b/internal/graft/corestore/syncer/syncer.go
index 8709bc2..8995b25 100644
--- a/internal/graft/corestore/syncer/syncer.go
+++ b/internal/graft/corestore/syncer/syncer.go
@@ -885,7 +885,7 @@ func (s *Syncer) advanceLocked(path string, wm corestore.Watermark) corestore.Wa
}
next := corestore.Watermark{RepoID: wm.RepoID, Seq: newSeq}
- if err := gitops.WriteWatermark(path, next); err != nil {
+ if err := s.writeWatermarkLocked(path, next); err != nil {
s.logger.WithError(err).WithField("repository_path", path).Error("storage core: write watermark")
return wm
}
@@ -1325,12 +1325,22 @@ func (s *Syncer) applyLocked(ctx context.Context, repo *localrepo.Repo, hash git
// reached. The caller holds the repository lock.
func (s *Syncer) commitWatermarkLocked(path string, repoID int64, seq uint64) (corestore.Watermark, error) {
wm := corestore.Watermark{RepoID: repoID, Seq: seq}
- if err := gitops.WriteWatermark(path, wm); err != nil {
+ if err := s.writeWatermarkLocked(path, wm); err != nil {
return corestore.Watermark{}, structerr.NewInternal("storage core: write watermark: %w", err)
}
return s.advanceLocked(path, wm), nil
}
+// Writes the watermark, without fsync while the repository is dirty (caller holds the lock).
+func (s *Syncer) writeWatermarkLocked(path string, wm corestore.Watermark) error {
+ if s.core.RefDurability.DefersWatermark(path) {
+ s.hook("watermark deferred")
+ return gitops.WriteWatermarkNoSync(path, wm)
+ }
+ s.hook("watermark")
+ return gitops.WriteWatermark(path, wm)
+}
+
// movedSince reports whether a reference of moves no longer has its old value.
func movedSince(ctx context.Context, repo *localrepo.Repo, moves map[string]move) (bool, error) {
names := make([]string, 0, len(moves))
diff --git a/internal/graft/corestore/syncer/syncer_localrefs_test.go b/internal/graft/corestore/syncer/syncer_localrefs_test.go
index 8afc50a..f8e8008 100644
--- a/internal/graft/corestore/syncer/syncer_localrefs_test.go
+++ b/internal/graft/corestore/syncer/syncer_localrefs_test.go
@@ -114,6 +114,69 @@ func TestSyncerCatchUpMarksTheRepositoryDirty(t *testing.T) {
e.verifySynced(t, repo)
}
+func TestSyncerWritesTheWatermarkOfADirtyRepositoryWithoutFsync(t *testing.T) {
+ t.Parallel()
+
+ e := newEnv(t, envOptions{})
+ coretest.EnableLocalRefs(t, e.core, e.cfg)
+ var points []string
+ e.sy.SetTestHook(func(point string) {
+ if strings.HasPrefix(point, "watermark") {
+ points = append(points, point)
+ }
+ })
+ src := e.newSource(t)
+ c1 := e.writeCommit(t, src)
+ repo := e.register(t, e.key(localRelPath), src, c1)
+ require.NoError(t, e.sy.EnsureFresh(e.ctx, e.pbRepo(localRelPath)))
+
+ c2 := e.advanceMain(t, repo.ID, src, c1)
+ e.sy.NoteCommitted(e.ctx, e.localPath(), repo.ID, 2)
+ require.Equal(t, []string{"watermark"}, points)
+
+ // The catch-up writes references without fsync, so the repository is dirty.
+ points = nil
+ c3 := e.advanceMain(t, repo.ID, src, c2)
+ require.NoError(t, e.sy.EnsureFresh(e.ctx, e.pbRepo(localRelPath)))
+ require.Equal(t, []string{"watermark deferred"}, points)
+ c4 := e.advanceMain(t, repo.ID, src, c3)
+ points = nil
+ e.sy.NoteCommitted(e.ctx, e.localPath(), repo.ID, 4)
+ require.Equal(t, []string{"watermark deferred"}, points)
+ w, ok, err := e.sy.Applied(e.ctx, e.pbRepo(localRelPath))
+ require.NoError(t, err)
+ require.True(t, ok)
+ require.Equal(t, uint64(4), w.Seq)
+
+ e.sy.DurableStep(e.ctx, e.localPath(), false)
+ require.Empty(t, e.markers(t))
+ points = nil
+ e.advanceMain(t, repo.ID, src, c4)
+ e.sy.NoteCommitted(e.ctx, e.localPath(), repo.ID, 5)
+ require.Equal(t, []string{"watermark"}, points)
+}
+
+func TestSyncerMovesAsideADirtyRepositoryWithATornWatermark(t *testing.T) {
+ t.Parallel()
+
+ for name, torn := range map[string][]byte{"empty": nil, "garbage": []byte(`{"repo_id":`)} {
+ t.Run(name, func(t *testing.T) {
+ t.Parallel()
+ e, repo, _ := caughtUpDirty(t, localrefs.WithBootID("boot-a"))
+ require.NoError(t, os.WriteFile(filepath.Join(e.localPath(), "graft", "storage", "applied"), torn, 0o644))
+
+ coretest.EnableLocalRefs(t, e.core, e.cfg, localrefs.WithBootID("boot-b"))
+ e.sy = syncer.New(e.core, e.logger, e.locator, e.repoFactory, e.gitCmdFactory)
+ require.NoError(t, e.sy.RecoverLocalReferences(e.ctx, []string{e.storageName}))
+
+ require.NoDirExists(t, e.localPath())
+ require.Empty(t, e.markers(t))
+ require.NoError(t, e.sy.EnsureFresh(e.ctx, e.pbRepo(localRelPath)))
+ e.verifySynced(t, repo)
+ })
+ }
+}
+
// TestSyncerMovesAsideARepositoryDirtyAtAKernelRestart is the recovery of the
// LOCAL REFERENCE WRITES RULE: a repository with references written without
// fsync when the kernel restarted is moved aside before anything reads it, and