carlos / platform Public

ops: only a rollback-journal header (1,1) earns current freshness

Astra R1 P1-2 on sqlite-integrity-safe. A source whose header bytes 18
and 19 were neither (2,2) nor (1,1), such as (1,2), counted as current
on the box drill and not_stale on the hibernate drill, though SQLite
reads byte 19 to decide whether to open a WAL. sourceHeader now returns
the pair; on the level-or-ahead path (1,1) is current, (2,2) is the WAL
unknown, and any other pair is unknown: "journal mode not recognised:
header bytes 18 and 19 are <w> and <r>" (new string, for copy review).
The lower-counter STALE rule is unchanged.

A mixed-version (1,2) fixture runs through TestFreshnessOutcomes, the
box drill (TestRestoreVerifyUnknownIsNotCurrent) and the hibernate
drill's mapping (passed/unknown).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 1ea6a1409c181cf028514ea2720e1722fc28cb3d parent 5fcaf9c
3 files changed, +81 −17
  • internal/ops/freshness.go +23 −16
  • internal/ops/freshness_test.go +45 −1
  • internal/ops/hibernate_verify_test.go +13 −0
diff --git a/internal/ops/freshness.go b/internal/ops/freshness.go
index 09b7f7516..53560214f 100644
--- a/internal/ops/freshness.go
+++ b/internal/ops/freshness.go
@@ -51,8 +51,8 @@ type freshness int
const (
// freshnessCurrent: the replica is level with or ahead of a source in
- // rollback-journal mode, whose change counter moves on every commit. The
- // only outcome that counts as current.
+ // rollback-journal mode (header bytes 18 and 19 both 1), whose change
+ // counter moves on every commit. The only outcome that counts as current.
freshnessCurrent freshness = iota
// freshnessStale: the source has been quiet for longer than staleGrace and
// the replica is STILL behind it. Replication is not happening.
@@ -62,9 +62,9 @@ const (
freshnessNoSource
// freshnessUnknown: the comparison proves nothing either way. One of the
// two headers could not be read, the source is in WAL mode (its counter
- // moves only at a checkpoint, so "level" is no evidence), or the replica
- // is behind a source written within staleGrace. Say so; never let
- // "could not tell" read as "fine".
+ // moves only at a checkpoint, so "level" is no evidence) or names no mode
+ // this code recognises, or the replica is behind a source written within
+ // staleGrace. Say so; never let "could not tell" read as "fine".
freshnessUnknown
)
@@ -102,7 +102,7 @@ func replicaFreshness(srcPath, restoredPath string, srcModTime time.Time) (fresh
// hibernation drill, where lease.VerifyReplica reads the counter and then
// sweeps its throwaway copy in the same call.
func replicaFreshnessCounter(srcPath string, restoredCounter uint32, srcModTime time.Time) (freshness, string) {
- srcCounter, wal, err := sourceHeader(srcPath)
+ srcCounter, mode, err := sourceHeader(srcPath)
if os.IsNotExist(err) {
return freshnessNoSource, "source database is not on disk — this replica can never advance again"
}
@@ -110,13 +110,19 @@ func replicaFreshnessCounter(srcPath string, restoredCounter uint32, srcModTime
return freshnessUnknown, fmt.Sprintf("source header unreadable: %v", err)
}
if restoredCounter >= srcCounter {
- if wal {
+ switch mode {
+ case [2]byte{1, 1}:
+ // Rollback journal: every commit moves the counter.
+ return freshnessCurrent, ""
+ case [2]byte{2, 2}:
// In WAL mode a commit does not move the counter; only a
// checkpoint does. A copy level with it may still lack every
// write since the last checkpoint.
return freshnessUnknown, "WAL mode, where the change counter does not track writes"
+ default:
+ // Only (1,1) is evidence that commits move the counter.
+ return freshnessUnknown, fmt.Sprintf("journal mode not recognised: header bytes 18 and 19 are %d and %d", mode[0], mode[1])
}
- return freshnessCurrent, ""
}
if quiet := time.Since(srcModTime); quiet <= staleGrace {
// Behind, but the source was written within the grace window. That
@@ -130,24 +136,25 @@ func replicaFreshnessCounter(srcPath string, restoredCounter uint32, srcModTime
}
// sourceHeader reads the source database's change counter (header bytes
-// 24-27, as lease.ChangeCounter) and whether it is in WAL mode (header bytes
-// 18 and 19, the write and read versions, both 2), in one read. An error
-// opening the file is returned as it is, so os.IsNotExist sees it.
-func sourceHeader(path string) (counter uint32, wal bool, err error) {
+// 24-27, as lease.ChangeCounter) and its journal mode (header bytes 18 and
+// 19, the file format write and read versions: 1 and 1 for a rollback
+// journal, 2 and 2 for WAL), in one read. An error opening the file is
+// returned as it is, so os.IsNotExist sees it.
+func sourceHeader(path string) (counter uint32, mode [2]byte, err error) {
f, err := os.Open(path)
if err != nil {
- return 0, false, err
+ return 0, mode, err
}
defer f.Close()
var header [28]byte
if _, err := io.ReadFull(f, header[:]); err != nil {
- return 0, false, fmt.Errorf("read header: %w", err)
+ return 0, mode, fmt.Errorf("read header: %w", err)
}
if string(header[:15]) != "SQLite format 3" {
- return 0, false, errors.New("not a SQLite database")
+ return 0, mode, errors.New("not a SQLite database")
}
- return binary.BigEndian.Uint32(header[24:28]), header[18] == 2 && header[19] == 2, nil
+ return binary.BigEndian.Uint32(header[24:28]), [2]byte{header[18], header[19]}, nil
}
// sourceModTime is the source database's mtime, or the zero time when it
diff --git a/internal/ops/freshness_test.go b/internal/ops/freshness_test.go
index 28f55b322..f6f8955d6 100644
--- a/internal/ops/freshness_test.go
+++ b/internal/ops/freshness_test.go
@@ -35,6 +35,26 @@ func writeWALCounterDB(t *testing.T, path string, writes int) {
}
}
+// writeMixedCounterDB is writeCounterDB's source with header bytes 18 and 19
+// (the file format write and read versions) set to 1 and 2: neither the
+// rollback-journal pair (1,1) nor the WAL pair (2,2). SQLite reads byte 19
+// to decide whether to open a WAL, so such a file is no evidence of
+// rollback-journal mode.
+func writeMixedCounterDB(t *testing.T, path string, writes int) {
+ t.Helper()
+ writeCounterDB(t, path, writes)
+ f, err := os.OpenFile(path, os.O_RDWR, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if _, err := f.WriteAt([]byte{1, 2}, 18); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.Close(); err != nil {
+ t.Fatal(err)
+ }
+}
+
// Every row of the freshness table (spec §5.3): only affirmative evidence
// is current. A copy behind a recently written source, and any copy of a
// WAL-mode source that is not behind, could not be judged.
@@ -44,6 +64,8 @@ func TestFreshnessOutcomes(t *testing.T) {
writeCounterDB(t, journal, 9)
wal := filepath.Join(dir, "wal.db")
writeWALCounterDB(t, wal, 9)
+ mixed := filepath.Join(dir, "mixed.db")
+ writeMixedCounterDB(t, mixed, 9)
behind := filepath.Join(dir, "behind.db")
writeCounterDB(t, behind, 1)
ahead := filepath.Join(dir, "ahead.db")
@@ -57,6 +79,7 @@ func TestFreshnessOutcomes(t *testing.T) {
recent := time.Now()
const lag = "behind a write made in the last 15 minutes"
const walWhy = "WAL mode, where the change counter does not track writes"
+ const mixedWhy = "journal mode not recognised: header bytes 18 and 19 are 1 and 2"
for _, c := range []struct {
name, src, restored string
modTime time.Time
@@ -70,6 +93,10 @@ func TestFreshnessOutcomes(t *testing.T) {
{"WAL, level", wal, wal, long, freshnessUnknown, walWhy},
{"WAL, ahead", wal, ahead, long, freshnessUnknown, walWhy},
{"WAL, level, written recently", wal, wal, recent, freshnessUnknown, walWhy},
+ {"mixed versions, behind, quiet", mixed, behind, long, freshnessStale, ""},
+ {"mixed versions, behind, written recently", mixed, behind, recent, freshnessUnknown, lag},
+ {"mixed versions, level", mixed, mixed, long, freshnessUnknown, mixedWhy},
+ {"mixed versions, ahead", mixed, ahead, long, freshnessUnknown, mixedWhy},
{"journal, level", journal, journal, long, freshnessCurrent, ""},
{"journal, ahead", journal, ahead, long, freshnessCurrent, ""},
{"journal, level, written recently", journal, journal, recent, freshnessCurrent, ""},
@@ -102,9 +129,11 @@ func TestRestoreVerifyUnknownIsNotCurrent(t *testing.T) {
t.Fatal(err)
}
writeCounterDB(t, busy, 5)
+ mixed := filepath.Join(srcDir, "mixed.db")
+ writeMixedCounterDB(t, mixed, 3)
copies := map[string][]byte{busy: busyCopy}
- for _, p := range []string{journal, wal} {
+ for _, p := range []string{journal, wal, mixed} {
b, err := os.ReadFile(p)
if err != nil {
t.Fatal(err)
@@ -156,6 +185,21 @@ func TestRestoreVerifyUnknownIsNotCurrent(t *testing.T) {
}
}
+ // A source whose header names neither journal mode is not current,
+ // level or not.
+ out, err = run(journal, mixed)
+ if err != nil {
+ t.Fatalf("RestoreVerify with a mixed-version source = %v, want nil", err)
+ }
+ for _, line := range []string{
+ "ok " + mixed + " (restored clean, freshness unknown: journal mode not recognised: header bytes 18 and 19 are 1 and 2)",
+ "restore-verify: all 2 replicas restore clean; 1 are current, 1 could not be judged",
+ } {
+ if !strings.Contains(out, line+"\n") {
+ t.Errorf("output lacks %q:\n%s", line, out)
+ }
+ }
+
out, err = run(journal)
if err != nil {
t.Fatalf("RestoreVerify of a current replica = %v", err)
diff --git a/internal/ops/hibernate_verify_test.go b/internal/ops/hibernate_verify_test.go
index bcc3b245c..cf916ac2b 100644
--- a/internal/ops/hibernate_verify_test.go
+++ b/internal/ops/hibernate_verify_test.go
@@ -1035,6 +1035,19 @@ func TestVerifyHibernatingFreshnessOnlyOnLeaseHolder(t *testing.T) {
}
return p, n
},
+ "mixed-version source, level": func(t *testing.T) (string, uint32) {
+ p := filepath.Join(t.TempDir(), "mixed.db")
+ writeMixedCounterDB(t, p, 30)
+ old := time.Now().Add(-2 * time.Hour)
+ if err := os.Chtimes(p, old, old); err != nil {
+ t.Fatal(err)
+ }
+ n, err := ChangeCounter(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return p, n
+ },
"behind a recent write": func(t *testing.T) (string, uint32) {
p := filepath.Join(t.TempDir(), "busy.db")
writeCounterDB(t, p, 30)