| index 55eae69ef..a0228bc3c 100644 |
| --- a/internal/lease/release_snapshot_test.go |
| +++ b/internal/lease/release_snapshot_test.go |
| @@ -5,9 +5,12 @@ import ( |
| "database/sql" |
| "errors" |
| "io" |
| + "os" |
| "path/filepath" |
| + "runtime" |
| "sync" |
| "testing" |
| + "time" |
| |
| "github.com/benbjohnson/litestream" |
| lsfile "github.com/benbjohnson/litestream/file" |
| @@ -33,6 +36,9 @@ type countingReplica struct { |
| 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 |
| } |
| |
| func (c *countingReplica) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { |
| @@ -46,10 +52,14 @@ func (c *countingReplica) WriteLTXFile(ctx context.Context, level int, minTXID, |
| c.mu.Lock() |
| c.writes = append(c.writes, level) |
| fail := c.failSnapshot && level == litestream.SnapshotLevel |
| + early := c.refuseEarly && level == litestream.SnapshotLevel |
| if level == litestream.SnapshotLevel { |
| c.snapshotTXID = maxTXID |
| } |
| c.mu.Unlock() |
| + if early { |
| + return nil, errors.New("the store refused the snapshot at once") |
| + } |
| if fail { |
| io.Copy(io.Discard, r) |
| return nil, errors.New("the store refused the snapshot") |
| @@ -207,3 +217,49 @@ func TestDrainReleaseSnapshotFailureStaysClean(t *testing.T) { |
| t.Fatalf("restored %q, want %q", got, want) |
| } |
| } |
| + |
| +// F1.3 (Astra r1): a snapshot refused before any of it is read still |
| +// releases the snapshot encoder and its WAL descriptor; the release is |
| +// clean and the restore correct, as for any failed snapshot. |
| +func TestDrainReleaseSnapshotEarlyRefusalReleasesTheWAL(t *testing.T) { |
| + if runtime.GOOS != "linux" { |
| + t.Skip("reads /proc/self/fd") |
| + } |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + dir := t.TempDir() |
| + rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir), refuseEarly: true} |
| + r, path := manyL0(t, fx, "instances/h", rc, 5) |
| + txid, snapErr, err := r.FinishSnapshot(ctx) |
| + if err != nil || snapErr == nil { |
| + t.Fatalf("FinishSnapshot: txid %d, snapshot %v, %v; want the snapshot's failure alone", txid, snapErr, err) |
| + } |
| + if err := r.ReleaseMarker(ctx, 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 { |
| + t.Fatalf("marker %+v, want clean at %d", m, txid) |
| + } |
| + wal := path + "-wal" |
| + openWAL := func() bool { |
| + ents, err := os.ReadDir("/proc/self/fd") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, e := range ents { |
| + if target, err := os.Readlink(filepath.Join("/proc/self/fd", e.Name())); err == nil && target == wal { |
| + return true |
| + } |
| + } |
| + return false |
| + } |
| + for deadline := time.Now().Add(2 * time.Second); openWAL(); time.Sleep(10 * time.Millisecond) { |
| + if time.Now().After(deadline) { |
| + t.Fatalf("%s is still open after the release: the snapshot encoder was not released", wal) |
| + } |
| + } |
| + out, _ := restoreCounting(t, dir) |
| + if got, want := dbRows(t, out), dbRows(t, path); got != want { |
| + t.Fatalf("restored %q, want %q", got, want) |
| + } |
| +} |