lease tests: count only the release's own snapshots (#897)
litestream's store monitor writes a fresh replica's first snapshot about a second after open; under load it landed inside a sleep's release and was counted as the release's. The tests count only snapshots on their release calls' context. Finish still never snapshots (32a59ad). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Squashed from lease-sleep-snapshot-count into main.
1 file changed,
+30
−31
internal/lease/release_snapshot_test.go+30 −31
CI cancelled — superseded by a newer push — run details
diff --git a/internal/lease/release_snapshot_test.go b/internal/lease/release_snapshot_test.go| index 3b2cafd61..7af076170 100644 |
| --- a/internal/lease/release_snapshot_test.go |
| +++ b/internal/lease/release_snapshot_test.go |
| @@ -27,22 +27,32 @@ import ( |
| // and no L0 tail. An idle sleep's release writes none. |
| // countingReplica is a replica client that counts the LTX files opened |
| -// (a restore's reads, one GET each on S3) and records the levels written, |
| -// and can refuse snapshot writes. |
| +// (a restore's reads, one GET each on S3), records the snapshots a release |
| +// writes (releaseCall), and can refuse snapshot writes. |
| // drainSnapshotOnly marks the context of the drain release's own snapshot, |
| // so a refusing fake can refuse that snapshot and no other. |
| type drainSnapshotOnly struct{} |
| +// releaseCall marks the context a test passes to a release (Finish, |
| +// FinishSnapshot, ReleaseMarker), so the fake can tell the release's own |
| +// snapshots from the store monitor's. litestream's snapshot monitor writes |
| +// the store's first snapshot about a second after a fresh replica opens, |
| +// on the store's context; on a loaded CI worker that landed inside a |
| +// release and was counted as the release's (#897). |
| +type releaseCall struct{} |
| + |
| type countingReplica struct { |
| litestream.ReplicaClient |
| mu sync.Mutex |
| opens int |
| - writes []int |
| snapshotTXID ltx.TXID |
| failSnapshot bool |
| // refuseEarly refuses a snapshot without reading any of it, as an |
| // upload that fails before it consumes the body. |
| refuseEarly bool |
| + // releaseSnaps are the TXIDs of the snapshots written on a releaseCall |
| + // context, in order. |
| + releaseSnaps []ltx.TXID |
| } |
| func (c *countingReplica) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { |
| @@ -54,7 +64,6 @@ func (c *countingReplica) OpenLTXFile(ctx context.Context, level int, minTXID, m |
| func (c *countingReplica) WriteLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, r io.Reader) (*ltx.FileInfo, error) { |
| c.mu.Lock() |
| - c.writes = append(c.writes, level) |
| fail := c.failSnapshot && level == litestream.SnapshotLevel |
| // Only the drain's own snapshot is refused: the test marks its context |
| // (drainSnapshotOnly). The store monitor's snapshots run on the store's |
| @@ -64,6 +73,9 @@ func (c *countingReplica) WriteLTXFile(ctx context.Context, level int, minTXID, |
| early := c.refuseEarly && level == litestream.SnapshotLevel && ctx.Value(drainSnapshotOnly{}) != nil |
| if level == litestream.SnapshotLevel { |
| c.snapshotTXID = maxTXID |
| + if ctx.Value(releaseCall{}) != nil { |
| + c.releaseSnaps = append(c.releaseSnaps, maxTXID) |
| + } |
| } |
| c.mu.Unlock() |
| if early { |
| @@ -76,22 +88,12 @@ func (c *countingReplica) WriteLTXFile(ctx context.Context, level int, minTXID, |
| return c.ReplicaClient.WriteLTXFile(ctx, level, minTXID, maxTXID, r) |
| } |
| -func (c *countingReplica) mark() int { |
| - c.mu.Lock() |
| - defer c.mu.Unlock() |
| - return len(c.writes) |
| -} |
| - |
| -func (c *countingReplica) snapshotsSince(n int) int { |
| +// releaseSnapshots are the snapshots written by the releases a test marked |
| +// with releaseCall, at their TXIDs. |
| +func (c *countingReplica) releaseSnapshots() []ltx.TXID { |
| c.mu.Lock() |
| defer c.mu.Unlock() |
| - k := 0 |
| - for _, l := range c.writes[n:] { |
| - if l == litestream.SnapshotLevel { |
| - k++ |
| - } |
| - } |
| - return k |
| + return append([]ltx.TXID(nil), c.releaseSnaps...) |
| } |
| func (c *countingReplica) openCount() int { |
| @@ -156,12 +158,12 @@ func TestDrainReleaseSnapshotRestoresFromOneFile(t *testing.T) { |
| dir := t.TempDir() |
| rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir)} |
| r, path := manyL0(t, fx, "instances/h", rc, 200) |
| - before := rc.mark() |
| - txid, snapErr, err := r.FinishSnapshot(ctx) |
| + rctx := context.WithValue(ctx, releaseCall{}, true) |
| + txid, snapErr, err := r.FinishSnapshot(rctx) |
| if err != nil || snapErr != nil || txid == 0 { |
| t.Fatalf("FinishSnapshot: txid %d, snapshot %v, %v", txid, snapErr, err) |
| } |
| - if err := r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true}); err != nil { |
| + if err := r.ReleaseMarker(rctx, hafresh.Marker{V: 1, Clean: true}); err != nil { |
| t.Fatal(err) |
| } |
| if m := lockView(t, fx, "instances/h").Lock.Release; m == nil || !m.Clean || m.TXID != txid { |
| @@ -174,11 +176,8 @@ func TestDrainReleaseSnapshotRestoresFromOneFile(t *testing.T) { |
| if got, want := dbRows(t, out), dbRows(t, path); got != want { |
| t.Fatalf("restored %q, want %q", got, want) |
| } |
| - if k := rc.snapshotsSince(before); k != 1 { |
| - t.Fatalf("%d snapshots written by the drain's release, want 1", k) |
| - } |
| - if uint64(rc.snapshotTXID) != txid { |
| - t.Fatalf("snapshot at txid %d, final txid %d", rc.snapshotTXID, txid) |
| + if snaps := rc.releaseSnapshots(); len(snaps) != 1 || uint64(snaps[0]) != txid { |
| + t.Fatalf("snapshots written by the drain's release at %v, want one at the final txid %d", snaps, txid) |
| } |
| } |
| @@ -188,15 +187,15 @@ func TestSleepReleaseWritesNoSnapshot(t *testing.T) { |
| fx := leasetest.NewLockFixture(t) |
| rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(t.TempDir())} |
| r, _ := manyL0(t, fx, "instances/h", rc, 5) |
| - before := rc.mark() |
| - if _, err := r.Finish(ctx); err != nil { |
| + rctx := context.WithValue(ctx, releaseCall{}, true) |
| + if _, err := r.Finish(rctx); err != nil { |
| t.Fatal(err) |
| } |
| - if err := r.ReleaseMarker(ctx, hafresh.Marker{V: 1, Clean: true}); err != nil { |
| + if err := r.ReleaseMarker(rctx, hafresh.Marker{V: 1, Clean: true}); err != nil { |
| t.Fatal(err) |
| } |
| - if k := rc.snapshotsSince(before); k != 0 { |
| - t.Fatalf("a sleep's release wrote %d snapshots", k) |
| + if snaps := rc.releaseSnapshots(); len(snaps) != 0 { |
| + t.Fatalf("a sleep's release wrote %d snapshots", len(snaps)) |
| } |
| } |