Skip to content

Git designed for AI agents.

blobstore: fsync only the bucket directories that need it

Trigodil6480f566cf8c
diff --git a/internal/graft/corestore/blobstore/blobstore.go b/internal/graft/corestore/blobstore/blobstore.go
index a3f53e6..30c7356 100644
--- a/internal/graft/corestore/blobstore/blobstore.go
+++ b/internal/graft/corestore/blobstore/blobstore.go
@@ -67,6 +67,7 @@ import (
"path/filepath"
"strconv"
"strings"
+ "sync"
"github.com/feder-cr/git-ai/internal/graft/corestore"
"gocloud.dev/blob"
@@ -85,6 +86,8 @@ type Store struct {
// root is the cleaned directory of a file bucket and empty for any other
// driver. Put makes the writes of a file bucket durable from it.
root string
+ // Directories whose entry in their parent was made durable, by path.
+ durableDirs sync.Map
}
var _ corestore.BlobStore = (*Store)(nil)
@@ -316,18 +319,31 @@ func syncFile(f *os.File) error {
return f.Sync()
}
-// syncDirs fsyncs dir and, when dir is inside the bucket root, each of its
-// ancestors up to the root: the directories that the driver may have created,
-// and the entry that the link created.
+// Fsyncs dir and its ancestors inside the bucket root, up to the first one an earlier
+// call made durable that is still the same directory.
func (s *Store) syncDirs(dir string) error {
inside := dir == s.root || strings.HasPrefix(dir, s.root+string(filepath.Separator))
+ var child string
+ var childInfo os.FileInfo
for d := dir; ; d = filepath.Dir(d) {
if err := syncDir(d); err != nil {
return err
}
+ if child != "" {
+ s.durableDirs.Store(child, childInfo)
+ }
if !inside || d == s.root {
return nil
}
+
+ info, err := os.Stat(d)
+ if err != nil {
+ return fmt.Errorf("stat directory: %w", err)
+ }
+ if known, ok := s.durableDirs.Load(d); ok && os.SameFile(known.(os.FileInfo), info) {
+ return nil
+ }
+ child, childInfo = d, info
}
}
diff --git a/internal/graft/corestore/blobstore/durable_internal_test.go b/internal/graft/corestore/blobstore/durable_internal_test.go
index b7b408a..b52da3a 100644
--- a/internal/graft/corestore/blobstore/durable_internal_test.go
+++ b/internal/graft/corestore/blobstore/durable_internal_test.go
@@ -70,6 +70,39 @@ func TestPutSyncsFileBeforeCommitAndSyncsDirectories(t *testing.T) {
require.Equal(t, content, data)
}
+func TestPutSyncsOnlyTheDirectoriesWhoseEntriesAreNotDurable(t *testing.T) {
+ dir := t.TempDir()
+ var dirSyncs []string
+ s := openFileStoreForTest(t, dir, func(path string, isDir bool) error {
+ if isDir {
+ dirSyncs = append(dirSyncs, path)
+ }
+ return nil
+ })
+
+ put := func(repoID int64) {
+ dirSyncs = nil
+ content := []byte("pack content")
+ require.NoError(t, s.Put(testhelper.Context(t), corestore.NewPackKey(repoID, sha256.Sum256(content)), bytes.NewReader(content)))
+ }
+ repoDir := filepath.Join(dir, "repos", "1")
+ packsDir := filepath.Join(repoDir, "packs")
+
+ put(1)
+ require.Equal(t, []string{packsDir, repoDir, filepath.Join(dir, "repos"), dir}, dirSyncs)
+
+ put(1)
+ require.Equal(t, []string{packsDir}, dirSyncs)
+
+ put(2)
+ require.Equal(t, []string{filepath.Join(dir, "repos", "2", "packs"), filepath.Join(dir, "repos", "2"), filepath.Join(dir, "repos")}, dirSyncs)
+
+ // Moving the directory aside keeps its inode in use, so the new directory gets another one.
+ require.NoError(t, os.Rename(repoDir, filepath.Join(t.TempDir(), "old")))
+ put(1)
+ require.Equal(t, []string{packsDir, repoDir, filepath.Join(dir, "repos")}, dirSyncs)
+}
+
func TestPutFailedFileSyncLeavesNoObject(t *testing.T) {
dir := t.TempDir()
content := []byte("pack content whose sync fails")