oficina / docs Public

Live push 1b: two starts close together keep the log epoch (#175 finding 6)

A start that read the database after another start's bump but before its
file rename saw boot n+1 against a file saying n and rotated, costing every
client one resync. The read, compare and bump now run in one write
transaction, and a file exactly one boot behind with the same epoch (that
window) keeps it; a database older than the file (a restore from before
the last start) still rotates.

TestLogEpochSurvivesAStartBetweenAnotherStartsTwoWrites puts the database
one boot ahead of the file, as that window leaves it, and starts: the
epoch is kept. It rotated before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev abe631d0b04a9d6fd1f41fa13192a8b24deefbfb parent b597ebd
2 files changed, +60 −20
  • internal/docs/logepoch.go +33 −20
  • internal/docs/push_test.go +27 −0
diff --git a/internal/docs/logepoch.go b/internal/docs/logepoch.go
index eceac45..c1cbace 100644
--- a/internal/docs/logepoch.go
+++ b/internal/docs/logepoch.go
@@ -59,29 +59,42 @@ func newLogEpoch() (string, error) {
// serves.
func loadLogEpoch(ctx context.Context, db *gorm.DB, dbPath string) (string, error) {
sidecar := dbPath + ".log-epoch"
- var row logEpochRow
- err := db.WithContext(ctx).Where("id = 1").Limit(1).Find(&row).Error
- if err != nil {
- return "", fmt.Errorf("docs: read log epoch: %w", err)
- }
- keep := false
- if row.ID == 1 {
- if raw, err := os.ReadFile(sidecar); err == nil {
- epoch, boot, ok := strings.Cut(strings.TrimSpace(string(raw)), " ")
- n, err := strconv.ParseInt(boot, 10, 64)
- keep = ok && err == nil && epoch == row.Epoch && n == row.Boot
+ var fileEpoch string
+ fileBoot := int64(-1)
+ if raw, err := os.ReadFile(sidecar); err == nil {
+ epoch, boot, ok := strings.Cut(strings.TrimSpace(string(raw)), " ")
+ if n, err := strconv.ParseInt(boot, 10, 64); ok && err == nil {
+ fileEpoch, fileBoot = epoch, n
}
}
- if !keep {
- epoch, err := newLogEpoch()
- if err != nil {
- return "", fmt.Errorf("docs: draw log epoch: %w", err)
+ // Read, compare and bump in one write transaction, so two starts close
+ // together are serialised on the database (#175 finding 6). The file is
+ // written after the commit, so a start can land between another start's
+ // commit and its file write and see the file exactly one boot behind,
+ // with the same epoch: that is a neighbour, not a restore, and keeps it.
+ // A database older than the file (a restore from before the last start)
+ // has a lower boot and rotates.
+ var row logEpochRow
+ err := db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
+ if err := tx.Where("id = 1").Limit(1).Find(&row).Error; err != nil {
+ return fmt.Errorf("docs: read log epoch: %w", err)
}
- row = logEpochRow{ID: 1, Epoch: epoch, Boot: row.Boot}
- }
- row.Boot++
- if err := db.WithContext(ctx).Save(&row).Error; err != nil {
- return "", fmt.Errorf("docs: write log epoch: %w", err)
+ keep := row.ID == 1 && fileEpoch == row.Epoch && (fileBoot == row.Boot || fileBoot == row.Boot-1)
+ if !keep {
+ epoch, err := newLogEpoch()
+ if err != nil {
+ return fmt.Errorf("docs: draw log epoch: %w", err)
+ }
+ row = logEpochRow{ID: 1, Epoch: epoch, Boot: row.Boot}
+ }
+ row.Boot++
+ if err := tx.Save(&row).Error; err != nil {
+ return fmt.Errorf("docs: write log epoch: %w", err)
+ }
+ return nil
+ })
+ if err != nil {
+ return "", err
}
tmp, err := os.CreateTemp(filepath.Dir(sidecar), ".log-epoch-*")
if err != nil {
diff --git a/internal/docs/push_test.go b/internal/docs/push_test.go
index f2530a2..d947e4f 100644
--- a/internal/docs/push_test.go
+++ b/internal/docs/push_test.go
@@ -1195,3 +1195,30 @@ func TestPushCursorAheadOfTheLogResyncs(t *testing.T) {
t.Fatalf("%+v, want resync 1", fr)
}
}
+
+// #175 finding 6: two starts close together. The first has bumped the
+// database's boot but not yet rewritten the file beside it when the second
+// reads both: the file is one boot behind, with the same epoch. That is
+// not a restore, and must not rotate.
+func TestLogEpochSurvivesAStartBetweenAnotherStartsTwoWrites(t *testing.T) {
+ dir := t.TempDir()
+ cfg := Config{DBPath: filepath.Join(dir, "docs.db"), Env: "development"}
+ a, err := New(cfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ epoch := a.store.logEpoch
+ // The window: another start has saved boot+1 and not yet renamed.
+ if err := a.store.db.Exec("UPDATE docs_log_epoch SET boot = boot + 1 WHERE id = 1").Error; err != nil {
+ t.Fatal(err)
+ }
+ b, err := New(cfg)
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer b.Close()
+ defer a.Close()
+ if b.store.logEpoch != epoch {
+ t.Fatal("a start between another start's database write and its file write rotated the epoch")
+ }
+}