Skip to content

Git designed for AI agents.

changelog: group appends, readers bounded by the durable end

Federico Elia52f25de855e7
diff --git a/internal/graft/changelog/log.go b/internal/graft/changelog/log.go
index cc751b9..a658e70 100644
--- a/internal/graft/changelog/log.go
+++ b/internal/graft/changelog/log.go
@@ -202,6 +202,13 @@ type logState struct {
fileSize int64
// rescans counts the times the cache was filled from the file. Tests use it.
rescans int
+ // untrusted is set when an append failed after writing and could not cut its
+ // bytes off again: the file from untrustedFrom on may hold bytes that are not
+ // durable although they read back (after a failed fsync, the kernel may have
+ // dropped them), so loading the state ignores them and the next append
+ // truncates them.
+ untrusted bool
+ untrustedFrom int64
// dirsSynced is true once this process has fsync'd the directories that hold
// the log (see syncLogDirs) and the append succeeded. It is reset when an
// append fails. While it is false every append syncs them, so that the
@@ -279,11 +286,36 @@ func evictIdleStates() {
}
}
+// Record is the updates of one reference transaction with the metadata of the
+// operation that performed them. AppendGroup records several of them at once.
+type Record struct {
+ Meta Meta
+ Updates []Update
+}
+
// Append records updates in the changeset log of the repository whose git
-// directory is repoPath and returns the recorded entries. The entries of one
-// call get consecutive sequences and are written with a single write. Appends
-// are serialized within the process and across processes with an flock on the
-// log's lock file, and are fsync'd before Append returns.
+// directory is repoPath and returns the recorded entries. It is AppendGroup with
+// one record.
+func Append(repoPath string, meta Meta, updates []Update) ([]Entry, error) {
+ entries, err := AppendGroup(repoPath, []Record{{Meta: meta, Updates: updates}})
+ if err != nil {
+ return nil, err
+ }
+
+ return entries[0], nil
+}
+
+// AppendGroup records the records in the changeset log of the repository whose
+// git directory is repoPath, in the order given, and returns the entries of each
+// record. The entries of all records get consecutive sequences and are written
+// with a single write and a single fsync. Appends are serialized within the
+// process and across processes with an flock on the log's lock file, and are
+// fsync'd before AppendGroup returns.
+//
+// The entries become visible to the readers of the log (Scan, List,
+// LastSequence) only once they are durable, so a reader never returns an entry
+// that a crash could take back. When AppendGroup fails, none of its entries is
+// durable or was visible, and the next append writes over whatever it wrote.
//
// A torn last line, left behind by a crashed writer, is ignored and truncated
// before the new entries are written.
@@ -296,17 +328,33 @@ func evictIdleStates() {
// reads may be routed to a replica whose log lags or differs.
// - The lock is taken without a timeout. A holder that never releases it blocks
// every append to the repository.
-func Append(repoPath string, meta Meta, updates []Update) ([]Entry, error) {
- if len(updates) == 0 {
- return nil, nil
- }
+func AppendGroup(repoPath string, records []Record) ([][]Entry, error) {
+ entries := make([][]Entry, len(records))
+
+ empty := true
+ for i := range records {
+ if len(records[i].Updates) > 0 {
+ empty = false
+ }
+
+ // The updates are already committed when they are recorded, so a malformed
+ // member must not fail the call and lose the entries of the whole batch.
+ // Such a member is dropped instead, and the entries say that their members
+ // are incomplete.
+ meta := &records[i].Meta
+ if members, dropped := validMembers(meta.Members); dropped {
+ meta.Members = members
+ meta.MembersTruncated = true
+ }
- // The updates are already committed when Append runs, so a malformed member must
- // not fail the call and lose the entries of the whole batch. Such a member is
- // dropped instead, and the entries say that their members are incomplete.
- if members, dropped := validMembers(meta.Members); dropped {
- meta.Members = members
- meta.MembersTruncated = true
+ // Proto strings, which serve these fields, must be valid UTF-8, and JSON
+ // would replace the invalid bytes anyway. Replace them here, so that the
+ // returned entries match the recorded ones.
+ meta.Operation = strings.ToValidUTF8(meta.Operation, string(utf8.RuneError))
+ meta.Actor = strings.ToValidUTF8(meta.Actor, string(utf8.RuneError))
+ }
+ if empty {
+ return entries, nil
}
path := Path(repoPath)
@@ -317,14 +365,8 @@ func Append(repoPath string, meta Meta, updates []Update) ([]Entry, error) {
state, release := acquireState(path)
defer release()
- // Proto strings, which serve these fields, must be valid UTF-8, and JSON would
- // replace the invalid bytes anyway. Replace them here, so that the returned
- // entries match the recorded ones.
- meta.Operation = strings.ToValidUTF8(meta.Operation, string(utf8.RuneError))
- meta.Actor = strings.ToValidUTF8(meta.Actor, string(utf8.RuneError))
-
state.mu.Lock()
- entries, err := state.appendLocked(repoPath, path, meta, updates)
+ err := state.appendLocked(repoPath, path, records, entries)
if err != nil {
// Whatever state we cached may be stale now, so force a rescan on the
// next append. The failed append may also have created the log or its
@@ -344,12 +386,15 @@ func Append(repoPath string, meta Meta, updates []Update) ([]Entry, error) {
return entries, nil
}
-// appendLocked writes the entries to the log at path, which belongs to the
-// repository whose git directory is repoPath. The caller must hold s.mu.
-func (s *logState) appendLocked(repoPath, path string, meta Meta, updates []Update) ([]Entry, error) {
+// appendLocked writes the entries of the records to the log at path, which
+// belongs to the repository whose git directory is repoPath, and stores the
+// entries of record i in entries[i]. The caller must hold s.mu. The cached state,
+// and so what readers see, moves past the new entries only after they are
+// durable.
+func (s *logState) appendLocked(repoPath, path string, records []Record, entries [][]Entry) error {
unlock, err := lockFile(path + lockSuffix)
if err != nil {
- return nil, err
+ return err
}
defer unlock()
@@ -361,77 +406,87 @@ func (s *logState) appendLocked(repoPath, path string, meta Meta, updates []Upda
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
if err != nil {
- return nil, fmt.Errorf("opening changelog: %w", err)
+ return fmt.Errorf("opening changelog: %w", err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
- return nil, fmt.Errorf("stat changelog: %w", err)
+ return fmt.Errorf("stat changelog: %w", err)
}
if !s.loaded || info.Size() != s.fileSize {
- if err := s.rescan(f, info.Size()); err != nil {
- return nil, err
+ if err := s.load(f, info.Size()); err != nil {
+ return err
}
}
- // Drop a torn tail left behind by a crashed writer. It is never valid, so
- // nothing that was acknowledged is lost.
+ // Drop a torn tail left behind by a crashed writer, or the bytes of an append
+ // that failed. They were never durable and never visible, so nothing that was
+ // acknowledged is lost.
if s.fileSize > s.validEnd {
- if err := f.Truncate(s.validEnd); err != nil {
- return nil, fmt.Errorf("truncating torn changelog tail: %w", err)
+ if err := truncateFile(f, s.validEnd); err != nil {
+ return fmt.Errorf("truncating torn changelog tail: %w", err)
}
s.fileSize = s.validEnd
+ s.untrusted = false
}
- recordedAt := meta.RecordedAt
- if recordedAt.IsZero() {
- recordedAt = time.Now()
- }
- recordedAt = recordedAt.UTC()
+ now := time.Now()
var (
buf bytes.Buffer
points []indexPoint
)
- entries := make([]Entry, 0, len(updates))
seq := s.lastSeq
- for _, update := range updates {
- seq++
- entry := Entry{
- Sequence: seq,
- Reference: update.Reference,
- OldOID: update.OldOID,
- NewOID: update.NewOID,
- Operation: meta.Operation,
- Actor: meta.Actor,
- RecordedAt: recordedAt,
- Rewritten: meta.Rewritten,
- Members: normalizeMembers(meta.Members),
- MembersTruncated: meta.MembersTruncated,
- }
-
- line, err := json.Marshal(entry)
- if err != nil {
- return nil, fmt.Errorf("encoding changelog entry: %w", err)
- }
+ for i, record := range records {
+ meta := record.Meta
+ recordedAt := meta.RecordedAt
+ if recordedAt.IsZero() {
+ recordedAt = now
+ }
+ recordedAt = recordedAt.UTC()
+
+ recorded := make([]Entry, 0, len(record.Updates))
+ for _, update := range record.Updates {
+ seq++
+ entry := Entry{
+ Sequence: seq,
+ Reference: update.Reference,
+ OldOID: update.OldOID,
+ NewOID: update.NewOID,
+ Operation: meta.Operation,
+ Actor: meta.Actor,
+ RecordedAt: recordedAt,
+ Rewritten: meta.Rewritten,
+ Members: normalizeMembers(meta.Members),
+ MembersTruncated: meta.MembersTruncated,
+ }
- if seq%indexInterval == 0 {
- points = append(points, indexPoint{sequence: seq, offset: s.validEnd + int64(buf.Len())})
- }
+ line, err := json.Marshal(entry)
+ if err != nil {
+ return fmt.Errorf("encoding changelog entry: %w", err)
+ }
- buf.Write(line)
- buf.WriteByte('\n')
- entries = append(entries, entry)
+ if seq%indexInterval == 0 {
+ points = append(points, indexPoint{sequence: seq, offset: s.validEnd + int64(buf.Len())})
+ }
+
+ buf.Write(line)
+ buf.WriteByte('\n')
+ recorded = append(recorded, entry)
+ }
+ entries[i] = recorded
}
if _, err := f.WriteAt(buf.Bytes(), s.validEnd); err != nil {
- return nil, fmt.Errorf("writing changelog: %w", err)
+ s.discardFrom(f, s.validEnd)
+ return fmt.Errorf("writing changelog: %w", err)
}
- if err := f.Sync(); err != nil {
- return nil, fmt.Errorf("syncing changelog: %w", err)
+ if err := syncFile(f); err != nil {
+ s.discardFrom(f, s.validEnd)
+ return fmt.Errorf("syncing changelog: %w", err)
}
// The entries are only durable once the directory entries of the log file and
@@ -442,7 +497,8 @@ func (s *logState) appendLocked(repoPath, path string, meta Meta, updates []Upda
// append.
if created || !s.dirsSynced {
if err := syncLogDirs(repoPath, path); err != nil {
- return nil, err
+ s.discardFrom(f, s.validEnd)
+ return err
}
s.dirsSynced = true
}
@@ -456,7 +512,38 @@ func (s *logState) appendLocked(repoPath, path string, meta Meta, updates []Upda
s.addIndexPoint(point.sequence, point.offset)
}
- return entries, nil
+ return nil
+}
+
+// discardFrom cuts off the bytes that a failed append wrote from end on. When
+// that fails too, the bytes are remembered as untrusted, so that no reader takes
+// them for entries. The caller must hold s.mu.
+func (s *logState) discardFrom(f *os.File, end int64) {
+ if err := truncateFile(f, end); err == nil {
+ return
+ }
+
+ if !s.untrusted || end < s.untrustedFrom {
+ s.untrusted = true
+ s.untrustedFrom = end
+ }
+}
+
+// load fills the cache from the file, whose size is size, ignoring the
+// untrusted bytes of a failed append (see discardFrom). The caller must hold
+// s.mu.
+func (s *logState) load(f *os.File, size int64) error {
+ scanned := size
+ if s.untrusted && scanned > s.untrustedFrom {
+ scanned = s.untrustedFrom
+ }
+
+ if err := s.rescan(f, scanned); err != nil {
+ return err
+ }
+ s.fileSize = size
+
+ return nil
}
// rescan fills the cache from the file, whose size is size. It reads backwards
@@ -609,6 +696,61 @@ func syncLogDirs(repoPath, path string) error {
return nil
}
+// syncFile fsyncs the log file f. Tests replace it.
+var syncFile = func(f *os.File) error {
+ return f.Sync()
+}
+
+// truncateFile cuts the log file f to size. Tests replace it.
+var truncateFile = func(f *os.File, size int64) error {
+ return f.Truncate(size)
+}
+
+// durableView returns the end of the durable entries of the log at path and the
+// sequence of the last of them. f is the log file, opened by the caller. Readers
+// read the log only up to that end.
+//
+// The cached state only moves past entries once they are durable, so while it
+// describes the file it is the answer; taking s.mu waits for an append of this
+// process that is between its write and its fsync. When the state is not loaded,
+// or the file changed since it was (an append of another process), the state is
+// loaded again under the log's lock file, which waits for an append of another
+// process, and the file is fsync'd first: bytes that a crashed writer left in the
+// page cache are durable once a reader may see them.
+func (s *logState) durableView(path string, f *os.File) (int64, uint64, error) {
+ s.mu.Lock()
+ defer s.mu.Unlock()
+
+ info, err := f.Stat()
+ if err != nil {
+ return 0, 0, fmt.Errorf("stat changelog: %w", err)
+ }
+ if s.loaded && info.Size() == s.fileSize {
+ return s.validEnd, s.lastSeq, nil
+ }
+
+ unlock, err := lockFile(path + lockSuffix)
+ if err != nil {
+ return 0, 0, err
+ }
+ defer unlock()
+
+ if err := syncFile(f); err != nil {
+ return 0, 0, fmt.Errorf("syncing changelog: %w", err)
+ }
+
+ info, err = f.Stat()
+ if err != nil {
+ return 0, 0, fmt.Errorf("stat changelog: %w", err)
+ }
+ if err := s.load(f, info.Size()); err != nil {
+ s.loaded = false
+ return 0, 0, err
+ }
+
+ return s.validEnd, s.lastSeq, nil
+}
+
// syncDir fsyncs the directory dir, so that the entries it holds are durable.
// Tests replace it.
var syncDir = func(dir string) error {
@@ -642,8 +784,8 @@ func lockFile(path string) (func(), error) {
// Scan calls fn for every entry of the log of the repository whose git
// directory is repoPath with a sequence greater than after, in ascending order.
-// Scanning stops when fn returns false or an error. A torn last line is
-// ignored, so Scan can run concurrently with Append.
+// Scanning stops when fn returns false or an error. Scan reads the durable
+// entries only (see AppendGroup), so it can run concurrently with appends.
//
// The scan starts at the closest index point before after, so its cost depends
// on the entries that follow after and not on the size of the log.
@@ -662,12 +804,17 @@ func Scan(repoPath string, after uint64, fn func(Entry) (bool, error)) error {
}
defer f.Close()
+ end, _, err := state.durableView(path, f)
+ if err != nil {
+ return err
+ }
+
offset := state.scanOffset(f, after)
- if _, err := f.Seek(offset, io.SeekStart); err != nil {
- return fmt.Errorf("seeking changelog: %w", err)
+ if offset >= end {
+ return nil
}
- r := bufio.NewReader(f)
+ r := bufio.NewReader(io.NewSectionReader(f, offset, end-offset))
for {
line, err := r.ReadBytes('\n')
if err != nil {
@@ -701,12 +848,11 @@ func Scan(repoPath string, after uint64, fn func(Entry) (bool, error)) error {
}
}
-// LastSequence returns the sequence of the last valid entry of the log of the
+// LastSequence returns the sequence of the last durable entry of the log of the
// repository whose git directory is repoPath, 0 when the log is empty or does
-// not exist. It uses the cached state of the log while the file size is
-// unchanged and otherwise reads the file backwards from its end, so its cost
-// does not depend on the size of the log. It takes no lock on the log: an
-// append that runs concurrently may or may not be included.
+// not exist. An entry whose append has not fsync'd it yet is not included (see
+// AppendGroup). It uses the cached state of the log while it describes the file,
+// so its cost does not depend on the size of the log.
func LastSequence(repoPath string) (uint64, error) {
path := Path(repoPath)
@@ -722,26 +868,12 @@ func LastSequence(repoPath string) (uint64, error) {
}
defer f.Close()
- info, err := f.Stat()
+ _, lastSeq, err := state.durableView(path, f)
if err != nil {
- return 0, fmt.Errorf("stat changelog: %w", err)
- }
-
- state.mu.Lock()
- if state.loaded && info.Size() == state.fileSize {
- defer state.mu.Unlock()
- return state.lastSeq, nil
- }
- state.mu.Unlock()
-
- // The cache is only filled by appends, which hold the log's lock file: the
- // file is read into a state of its own.
- var scratch logState
- if err := scratch.rescan(f, info.Size()); err != nil {
return 0, err
}
- return scratch.lastSeq, nil
+ return lastSeq, nil
}
// List returns up to limit entries with a sequence greater than after, in
diff --git a/internal/graft/changelog/log_durable_test.go b/internal/graft/changelog/log_durable_test.go
new file mode 100644
index 0000000..6dcb024
--- /dev/null
+++ b/internal/graft/changelog/log_durable_test.go
@@ -0,0 +1,278 @@
+package changelog
+
+import (
+ "errors"
+ "os"
+ "sync"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/stretchr/testify/require"
+)
+
+// The tests of this file replace syncFile and truncateFile, so they are not parallel.
+
+func durableUpdate(reference string) []Update {
+ return []Update{{
+ Reference: reference,
+ OldOID: "0000000000000000000000000000000000000000",
+ NewOID: "1111111111111111111111111111111111111111",
+ }}
+}
+
+// replaceSyncFile replaces syncFile for the test.
+func replaceSyncFile(t *testing.T, fn func(f *os.File) error) {
+ t.Helper()
+ original := syncFile
+ syncFile = fn
+ t.Cleanup(func() { syncFile = original })
+}
+
+func references(t *testing.T, repoPath string, after uint64) []string {
+ t.Helper()
+
+ var refs []string
+ require.NoError(t, Scan(repoPath, after, func(e Entry) (bool, error) {
+ refs = append(refs, e.Reference)
+ return true, nil
+ }))
+ return refs
+}
+
+// TestAppendGroupWritesOnceInOrder records three transactions with one fsync: their entries
+// follow each other in the order of the records, with consecutive sequences, and equal what Append
+// records for the same updates.
+func TestAppendGroupWritesOnceInOrder(t *testing.T) {
+ repoPath := t.TempDir()
+ _, err := Append(repoPath, Meta{Operation: "first"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+
+ var syncs atomic.Int32
+ replaceSyncFile(t, func(f *os.File) error {
+ syncs.Add(1)
+ return f.Sync()
+ })
+
+ recordedAt := time.Date(2026, 10, 18, 0, 0, 0, 0, time.UTC)
+ records := []Record{
+ {Meta: Meta{Operation: "one", Actor: "x", RecordedAt: recordedAt}, Updates: append(durableUpdate("refs/heads/b"), durableUpdate("refs/heads/c")...)},
+ {Meta: Meta{Operation: "empty"}},
+ {Meta: Meta{Operation: "two", Actor: "y", RecordedAt: recordedAt, Rewritten: true}, Updates: durableUpdate("refs/heads/d")},
+ }
+ entries, err := AppendGroup(repoPath, records)
+ require.NoError(t, err)
+ require.EqualValues(t, 1, syncs.Load(), "one fsync for the group")
+
+ require.Len(t, entries, 3)
+ require.Len(t, entries[0], 2)
+ require.Empty(t, entries[1])
+ require.Len(t, entries[2], 1)
+ require.Equal(t, []uint64{2, 3}, []uint64{entries[0][0].Sequence, entries[0][1].Sequence})
+ require.EqualValues(t, 4, entries[2][0].Sequence)
+ require.Equal(t, "two", entries[2][0].Operation)
+ require.True(t, entries[2][0].Rewritten)
+
+ listed, err := List(repoPath, 1, 10)
+ require.NoError(t, err)
+ require.Equal(t, append(entries[0], entries[2]...), listed)
+
+ // Append records the same entries as a group of one record.
+ other := t.TempDir()
+ _, err = Append(other, Meta{Operation: "first"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+ single, err := Append(other, records[0].Meta, records[0].Updates)
+ require.NoError(t, err)
+ require.Equal(t, entries[0], single)
+}
+
+// TestReadersSeeEntriesOnlyOnceDurable holds an append between its write and its fsync: the
+// readers of the log, in this process and from a state loaded anew, do not return the new entry
+// before the fsync returns.
+func TestReadersSeeEntriesOnlyOnceDurable(t *testing.T) {
+ repoPath := t.TempDir()
+ _, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+
+ inSync := make(chan struct{})
+ release := make(chan struct{})
+ var once sync.Once
+ replaceSyncFile(t, func(f *os.File) error {
+ once.Do(func() {
+ close(inSync)
+ <-release
+ })
+ return f.Sync()
+ })
+
+ appended := make(chan error, 1)
+ go func() {
+ _, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/b"))
+ appended <- err
+ }()
+ <-inSync
+
+ // The bytes of the entry are in the file now.
+ data, err := os.ReadFile(Path(repoPath))
+ require.NoError(t, err)
+ require.Contains(t, string(data), "refs/heads/b")
+
+ type result struct {
+ refs []string
+ last uint64
+ }
+ read := make(chan result, 1)
+ go func() {
+ var r result
+ r.refs = references(t, repoPath, 0)
+ r.last, _ = LastSequence(repoPath)
+ read <- r
+ }()
+
+ select {
+ case r := <-read:
+ require.Equal(t, []string{"refs/heads/a"}, r.refs, "a reader returned an entry before its fsync")
+ require.EqualValues(t, 1, r.last)
+ case <-time.After(200 * time.Millisecond):
+ // The reader waits for the append, which is what the test expects.
+ }
+
+ close(release)
+ require.NoError(t, <-appended)
+
+ r := <-read
+ if len(r.refs) == 2 {
+ require.Equal(t, []string{"refs/heads/a", "refs/heads/b"}, r.refs)
+ require.EqualValues(t, 2, r.last)
+ }
+ require.Equal(t, []string{"refs/heads/a", "refs/heads/b"}, references(t, repoPath, 0))
+}
+
+// TestFailedAppendIsNeverVisible is the case of a crash before the fsync, as a failed fsync shows
+// it: the entry of the failed append is cut off and never visible, the next append takes its
+// sequence for another update, and a reader that follows the log with a cursor misses nothing.
+// Before the durable end, a reader could return the entry before its fsync and then skip the one
+// that took its sequence.
+func TestFailedAppendIsNeverVisible(t *testing.T) {
+ repoPath := t.TempDir()
+ _, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+
+ var cursor uint64
+ require.NoError(t, Scan(repoPath, 0, func(e Entry) (bool, error) { cursor = e.Sequence; return true, nil }))
+ require.EqualValues(t, 1, cursor)
+
+ failed := true
+ replaceSyncFile(t, func(f *os.File) error {
+ if failed {
+ failed = false
+ return errors.New("injected fsync failure")
+ }
+ return f.Sync()
+ })
+
+ _, err = Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/lost"))
+ require.ErrorContains(t, err, "injected fsync failure")
+
+ data, err := os.ReadFile(Path(repoPath))
+ require.NoError(t, err)
+ require.NotContains(t, string(data), "refs/heads/lost", "the bytes of the failed append are cut off")
+ require.Empty(t, references(t, repoPath, cursor))
+ last, err := LastSequence(repoPath)
+ require.NoError(t, err)
+ require.EqualValues(t, 1, last)
+
+ // A restart, then the next append takes sequence 2.
+ dropState(repoPath)
+ entries, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/c"))
+ require.NoError(t, err)
+ require.EqualValues(t, 2, entries[0].Sequence)
+
+ require.Equal(t, []string{"refs/heads/c"}, references(t, repoPath, cursor))
+}
+
+// TestFailedAppendThatCannotBeCutOffIsIgnored fails the fsync of an append and the truncation
+// that would cut its bytes off: they stay in the file, but no reader returns them, and the next
+// append truncates them and takes their sequence.
+func TestFailedAppendThatCannotBeCutOffIsIgnored(t *testing.T) {
+ repoPath := t.TempDir()
+ _, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+
+ failSync := true
+ replaceSyncFile(t, func(f *os.File) error {
+ if failSync {
+ failSync = false
+ return errors.New("injected fsync failure")
+ }
+ return f.Sync()
+ })
+ originalTruncate := truncateFile
+ failTruncate := true
+ truncateFile = func(f *os.File, size int64) error {
+ if failTruncate {
+ failTruncate = false
+ return errors.New("injected truncate failure")
+ }
+ return originalTruncate(f, size)
+ }
+ t.Cleanup(func() { truncateFile = originalTruncate })
+
+ _, err = Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/lost"))
+ require.ErrorContains(t, err, "injected fsync failure")
+
+ data, err := os.ReadFile(Path(repoPath))
+ require.NoError(t, err)
+ require.Contains(t, string(data), "refs/heads/lost", "the truncation failed, so the bytes stay")
+
+ require.Equal(t, []string{"refs/heads/a"}, references(t, repoPath, 0))
+ last, err := LastSequence(repoPath)
+ require.NoError(t, err)
+ require.EqualValues(t, 1, last)
+
+ entries, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/c"))
+ require.NoError(t, err)
+ require.EqualValues(t, 2, entries[0].Sequence)
+
+ data, err = os.ReadFile(Path(repoPath))
+ require.NoError(t, err)
+ require.NotContains(t, string(data), "refs/heads/lost")
+ require.Equal(t, []string{"refs/heads/a", "refs/heads/c"}, references(t, repoPath, 0))
+}
+
+// TestLoadingTheLogFsyncsItFirst reads a log whose last entry a crashed process wrote without
+// fsyncing it: the reader fsyncs the file before it returns the entry, so the entry is durable
+// once it is visible.
+func TestLoadingTheLogFsyncsItFirst(t *testing.T) {
+ repoPath := t.TempDir()
+ _, err := Append(repoPath, Meta{Operation: "test"}, durableUpdate("refs/heads/a"))
+ require.NoError(t, err)
+
+ // What a crashed process left: a complete line after the last fsync.
+ entries, err := Append(t.TempDir(), Meta{Operation: "test"}, durableUpdate("refs/heads/b"))
+ require.NoError(t, err)
+ entries[0].Sequence = 2
+ line, err := entries[0].MarshalJSON()
+ require.NoError(t, err)
+ f, err := os.OpenFile(Path(repoPath), os.O_APPEND|os.O_WRONLY, 0)
+ require.NoError(t, err)
+ _, err = f.Write(append(line, '\n'))
+ require.NoError(t, err)
+ require.NoError(t, f.Close())
+ dropState(repoPath)
+
+ var syncs atomic.Int32
+ replaceSyncFile(t, func(f *os.File) error {
+ syncs.Add(1)
+ return f.Sync()
+ })
+
+ require.Equal(t, []string{"refs/heads/a", "refs/heads/b"}, references(t, repoPath, 0))
+ require.EqualValues(t, 1, syncs.Load(), "the reader fsyncs the log when it loads it")
+
+ // The state is loaded now: further reads do not fsync.
+ last, err := LastSequence(repoPath)
+ require.NoError(t, err)
+ require.EqualValues(t, 2, last)
+ require.EqualValues(t, 1, syncs.Load())
+}
diff --git a/internal/graft/changelog/log_test.go b/internal/graft/changelog/log_test.go
index 526af7b..2ed0590 100644
--- a/internal/graft/changelog/log_test.go
+++ b/internal/graft/changelog/log_test.go
@@ -820,9 +820,12 @@ func TestAppend_syncsDirectoriesUntilAnAppendSucceeds(t *testing.T) {
_, err = os.Stat(Path(repoPath))
require.NoError(t, err)
+ // The failed append cut its entry off again: the entry was never durable, since the
+ // directories that hold the log were not, so no reader saw it and the next append takes
+ // its sequence.
entries, err := Append(repoPath, Meta{}, update)
require.NoError(t, err)
- require.Equal(t, uint64(2), entries[0].Sequence, "the entry of the failed append was written")
+ require.Equal(t, uint64(1), entries[0].Sequence, "the entry of the failed append was cut off")
require.Equal(t, []string{graftDir, repoPath}, takeSynced(), "the directories are synced again")
_, err = Append(repoPath, Meta{}, update)