lease: fence fixes: short WAL headers, marker order in the test
Astra's Task 2 review, two P2s: - walCovered rejects any WAL header shorter than 32 bytes. A WAL cut to 24-31 bytes after the stat kept its salts, read no trailing frame and proved coverage. 24- and 31-byte cases in TestWalCovered and TestReadCoverage, next to the 10-byte one. - TestWriteFenceUnblocksAReleaseAlreadyWaiting logs every marker submission (the release's and each retry's, through a putMarker test hook) in the same log as the replica writes, and asserts no replica write follows the first, after the release and after the retries.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/lease/export_test.go+8 −0internal/lease/fence.go+3 −3internal/lease/fence_queue_test.go+2 −0internal/lease/fence_test.go+20 −5internal/lease/fencerun_test.go+13 −10internal/lease/release.go+6 −0
Intent
<task-notification> <task-id>br52ncras</task-id> <tool-use-id>toolu_01Xh9KB2GSgsDZYaDetpdXCA</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1be65f1d-61c8-4308-afb1-2c756edd9c6e/tasks/br52ncras.output</output-file> <status>completed</status> <summary>Background command "Run targeted activator HA tests on the fence branch" completed (exit code 0)</summary> </task-notification>
<task-notification> <task-id>b1xgcitm6</task-id> <tool-use-id>toolu_01K7JgBXm3uEKkrBQZu3ZauV</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/1be65f1d-61c8-4308-afb1-2c756edd9c6e/tasks/b1xgcitm6.output</output-file> <status>completed</status> <summary>Background command "Record activator result; wait for fence Task 2 review" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 15.9 g CO₂e (1,989,863 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on g6-fence, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/lease/export_test.go b/internal/lease/export_test.go| index 5cab556f6..4cbbba57f 100644 |
| --- a/internal/lease/export_test.go |
| +++ b/internal/lease/export_test.go |
| @@ -82,3 +82,11 @@ func CheckpointForTest(r *Replicator, mode string) error { |
| // FinalizingForTest reports whether a replica call's context is the |
| // finalization's (the fence gate lets those through). |
| var FinalizingForTest = finalizing |
| + |
| +// SetMarkerPutHookForTest runs hook as each release marker write is |
| +// submitted (the release's and every retry's), and returns the restore. |
| +func SetMarkerPutHookForTest(hook func()) func() { |
| + prev := markerPutHook |
| + markerPutHook = hook |
| + return func() { markerPutHook = prev } |
| +} |
diff --git a/internal/lease/fence.go b/internal/lease/fence.go| index 4b3eaa857..936c52d15 100644 |
| --- a/internal/lease/fence.go |
| +++ b/internal/lease/fence.go |
| @@ -148,7 +148,7 @@ func (q *fenceQueue) anyUnknown() bool { |
| // walCovered decides whether the WAL as it is now is all captured, and by |
| // which TXID. walHdr is the WAL file's header (salts big-endian at bytes |
| -// 16..23); frameAtEnd is whatever was read at the newest LTX file's |
| +// 16..23; shorter than walHeaderSize is never covered); frameAtEnd is whatever was read at the newest LTX file's |
| // captured end (h.WALOffset+h.WALSize; a frame header carries the salts |
| // at bytes 8..15). h is that LTX file's header, nil if there is none. l |
| // is the local TXID, returned when the WAL holds no frames. |
| @@ -156,8 +156,8 @@ func walCovered(w walState, walHdr, frameAtEnd []byte, h *ltx.Header, l uint64) |
| if !w.exists || w.size <= walHeaderSize { |
| return l, true |
| } |
| - if h == nil || len(walHdr) < 24 { |
| - return 0, false |
| + if h == nil || len(walHdr) < walHeaderSize { |
| + return 0, false // no LTX file, or a WAL header cut short (truncated since the stat) |
| } |
| salt1 := binary.BigEndian.Uint32(walHdr[16:20]) |
| salt2 := binary.BigEndian.Uint32(walHdr[20:24]) |
diff --git a/internal/lease/fence_queue_test.go b/internal/lease/fence_queue_test.go| index 2f79c651a..93b21008a 100644 |
| --- a/internal/lease/fence_queue_test.go |
| +++ b/internal/lease/fence_queue_test.go |
| @@ -352,6 +352,8 @@ func TestWalCovered(t *testing.T) { |
| {"empty file", walState{exists: true, size: 0}, nil, nil, nil, l, true}, |
| {"frames, no LTX header", framed, cur, nil, nil, 0, false}, |
| {"WAL header too short", framed, cur[:20], nil, h, 0, false}, |
| + {"WAL header of 24 bytes (salts present, header incomplete)", framed, cur[:24], nil, h, 0, false}, |
| + {"WAL header of 31 bytes", framed, cur[:31], nil, h, 0, false}, |
| {"salt 1 differs (WAL restarted since the capture)", framed, walHeader(h.WALSalt1+1, h.WALSalt2), nil, h, 0, false}, |
| {"salt 2 differs", framed, walHeader(h.WALSalt1, h.WALSalt2+1), nil, h, 0, false}, |
| {"WAL ends at the captured end", walState{exists: true, size: end}, cur, nil, h, 12, true}, |
diff --git a/internal/lease/fence_test.go b/internal/lease/fence_test.go| index 859313eaf..342fe9f1c 100644 |
| --- a/internal/lease/fence_test.go |
| +++ b/internal/lease/fence_test.go |
| @@ -563,6 +563,20 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| return n |
| } |
| putsBefore := lockPUTs() |
| + // Every marker submission (the release's and each retry's) goes in |
| + // the same log as the replica writes: none may follow the first. |
| + t.Cleanup(lease.SetMarkerPutHookForTest(func() { c.rec("marker attempt") })) |
| + noWriteAfterTheMarker := func(when string) { |
| + t.Helper() |
| + ev := c.log() |
| + i := slices.Index(ev, "marker attempt") |
| + if i < 0 { |
| + t.Fatalf("%s: no marker attempt recorded: %v", when, ev) |
| + } |
| + if slices.Contains(ev[i+1:], "replica write") { |
| + t.Fatalf("%s: a replica write after the first marker attempt: %v", when, ev) |
| + } |
| + } |
| released := make(chan error, 1) |
| go func() { |
| rc, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| @@ -594,7 +608,6 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| case <-time.After(8 * time.Second): |
| t.Fatal("the release did not return within 8 s of the trip") |
| } |
| - c.rec("release returned") |
| var oe *lease.ObligationError |
| if !errors.As(err, &oe) { |
| t.Fatalf("release = %v, want an ObligationError", err) |
| @@ -609,10 +622,7 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| t.Fatal("the finalization's own sync never reached the replica client") |
| } |
| time.Sleep(500 * time.Millisecond) |
| - ev := c.log() |
| - if i := slices.Index(ev, "release returned"); slices.Contains(ev[i+1:], "replica write") { |
| - t.Fatalf("a replica write after the marker attempt: %v", ev) |
| - } |
| + noWriteAfterTheMarker("after the release") |
| finalTXID, _ := g.r.Finish(ctx) // the finalization's cached answer |
| c.down.Store(false) |
| @@ -629,6 +639,11 @@ func TestWriteFenceUnblocksAReleaseAlreadyWaiting(t *testing.T) { |
| if err := lease.RetryPending(ctx); err != nil { |
| t.Fatalf("RetryPending: %v", err) |
| } |
| + time.Sleep(500 * time.Millisecond) |
| + noWriteAfterTheMarker("after the retries") |
| + if n := len(slices.DeleteFunc(c.log(), func(e string) bool { return e != "marker attempt" })); n < 2 { |
| + t.Fatalf("%d marker attempts recorded, want the release's and its retries'", n) |
| + } |
| v := lockView(t, fx, "instances/f") |
| if v.Lock.Release == nil || v.Lock.Release.Clean || v.Lock.Release.TXID != finalTXID { |
| t.Fatalf("marker %+v, want unclean at the finalization's TXID %d", v.Lock.Release, finalTXID) |
diff --git a/internal/lease/fencerun_test.go b/internal/lease/fencerun_test.go| index d086f1160..8c815e551 100644 |
| --- a/internal/lease/fencerun_test.go |
| +++ b/internal/lease/fencerun_test.go |
| @@ -2,6 +2,7 @@ package lease |
| import ( |
| "errors" |
| + "fmt" |
| "os" |
| "path/filepath" |
| "testing" |
| @@ -91,16 +92,18 @@ func TestReadCoverage(t *testing.T) { |
| }) |
| } |
| - t.Run("WAL truncated to 10 bytes after the stat", func(t *testing.T) { |
| - path, w := file(t, upToEnd(fullFrame(1, 2))) // covered as it was stat'd |
| - if err := os.Truncate(path, 10); err != nil { |
| - t.Fatal(err) |
| - } |
| - hdr, _ := headerIs(h, nil) |
| - if got, ok := readCoverage(path, w, l, hdr); ok { |
| - t.Fatalf("(%d, true), want not covered", got) |
| - } |
| - }) |
| + for _, size := range []int64{10, 24, 31} { |
| + t.Run(fmt.Sprintf("WAL truncated to %d bytes after the stat", size), func(t *testing.T) { |
| + path, w := file(t, upToEnd(fullFrame(1, 2))) // covered as it was stat'd |
| + if err := os.Truncate(path, size); err != nil { |
| + t.Fatal(err) |
| + } |
| + hdr, _ := headerIs(h, nil) |
| + if got, ok := readCoverage(path, w, l, hdr); ok { |
| + t.Fatalf("(%d, true), want not covered", got) |
| + } |
| + }) |
| + } |
| t.Run("WAL removed after the stat", func(t *testing.T) { |
| path, w := file(t, upToEnd(fullFrame(1, 2))) |
diff --git a/internal/lease/release.go b/internal/lease/release.go| index a674d4ec8..9341c89d0 100644 |
| --- a/internal/lease/release.go |
| +++ b/internal/lease/release.go |
| @@ -86,6 +86,7 @@ func releaseWithMarker(ctx context.Context, leaser *lss3.Leaser, region string, |
| // (RetryPending's spacing, AcquireHA's settle), so a 503 costs one request |
| // per attempt, not the SDK's three. |
| func putMarker(ctx context.Context, client lss3.S3API, o Obligation) error { |
| + markerPutHook() |
| _, err := client.PutObject(ctx, &awss3.PutObjectInput{ |
| Bucket: aws.String(o.Bucket), |
| Key: aws.String(o.Key), |
| @@ -102,6 +103,11 @@ func putMarker(ctx context.Context, client lss3.S3API, o Obligation) error { |
| return fmt.Errorf("lease: write release marker: %w", err) |
| } |
| +// markerPutHook runs as each marker write is submitted, the release's |
| +// and every retry's (a test seam for the order of replica writes and |
| +// marker attempts). |
| +var markerPutHook = func() {} |
| + |
| // noRetries turns the SDK's automatic retries off for one call. |
| func noRetries(o *awss3.Options) { o.Retryer = aws.NopRetryer{} } |