lease: the drain's release writes a snapshot at the final position
A cross-box take restored the moved host by replaying every L0 file written since the last compaction, one GET each. L1 runs hourly and a sleep only syncs, so a busy host's replica had over a thousand L0 files and the successor's restore took 27-29 s of the move. The drain's release now leaves a restore of one file. Replicator gains FinishSnapshot: Finish's forced full sync, then, only if that sync succeeded and before the store's close, a litestream snapshot (level 9) at the final position, inside the one serialized finalization, so no replica write follows the marker and finalize still runs once. The app is proven stopped by then, so the snapshot's TXID is the final TXID the marker records; a mismatch is reported as a snapshot failure. The snapshot gets half of the remaining sync budget, so a slow one never leaves the store's close a spent context. DrainStop marks the entry (drainRelease) when it begins the stop, and releaseHAClean calls FinishSnapshot only for such an entry. An idle sleep, or a stop already under way when the drain asks, finishes as before. A failed snapshot is logged and changes nothing else: the marker is clean as before and the restore replays the L0 files. Cost: one PUT of the database per moved host per planned handover; no steady-state requests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+409
−11
internal/activator/activator.go+4 −0internal/activator/hadrainmove.go+4 −0internal/activator/hadrainmove_test.go+99 −0internal/activator/harelease.go+17 −3internal/activator/hawake.go+9 −2internal/lease/lease.go+5 −2internal/lease/release.go+62 −4internal/lease/release_snapshot_test.go+209 −0
Intent
diff --git a/internal/activator/activator.go b/internal/activator/activator.go| index 1ccca9328..1e95f89d3 100644 |
| --- a/internal/activator/activator.go |
| +++ b/internal/activator/activator.go |
| @@ -600,6 +600,10 @@ type actEntry struct { |
| // haReleaseClean is the clean flag of the marker haReleased says was |
| // written (§5.2), for the drain report (§25.4). Under a.mu. |
| haReleaseClean bool |
| + // drainRelease is set when the drain's own stop began this entry's |
| + // stop (DrainStop): its HA release writes a snapshot at the final |
| + // position before the marker (releaseHAClean). Under a.mu. |
| + drainRelease bool |
| // Activity tracking: an instance is idle only when nothing is in flight |
| // AND the idle window has passed since the last request finished. In-flight |
diff --git a/internal/activator/hadrainmove.go b/internal/activator/hadrainmove.go| index 795916c6e..35f679080 100644 |
| --- a/internal/activator/hadrainmove.go |
| +++ b/internal/activator/hadrainmove.go |
| @@ -191,6 +191,10 @@ func (a *Service) DrainStop(ctx context.Context, host string) (DrainRelease, err |
| } |
| _, ha := haEntryRep(e) |
| if e.state == actAwake { |
| + // The drain's release leaves a one-file restore for the successor |
| + // (releaseHAClean). A stop already under way (an idle sleep) is |
| + // not the drain's and releases as it would have. |
| + e.drainRelease = true |
| a.beginStopLocked(e, StopDrain) |
| } |
| stopped := e.stopped |
diff --git a/internal/activator/hadrainmove_test.go b/internal/activator/hadrainmove_test.go| index 7aebab924..b44d60030 100644 |
| --- a/internal/activator/hadrainmove_test.go |
| +++ b/internal/activator/hadrainmove_test.go |
| @@ -225,3 +225,102 @@ func TestDrainReleaseLogKeepsAPausedStopsUncleanOutcome(t *testing.T) { |
| t.Fatalf("log after End: %+v", got) |
| } |
| } |
| + |
| +// snapshotSpyRep records which final sync the release asked for: Finish |
| +// (any release) or FinishSnapshot (the drain's, F2). |
| +type snapshotSpyRep struct { |
| + Rep |
| + haRep |
| + real *lease.Replicator |
| + snapErr error |
| + calls chan string |
| +} |
| + |
| +func (r *snapshotSpyRep) Finish(ctx context.Context) (uint64, error) { |
| + r.calls <- "finish" |
| + return r.haRep.Finish(ctx) |
| +} |
| + |
| +func (r *snapshotSpyRep) FinishSnapshot(ctx context.Context) (uint64, error, error) { |
| + r.calls <- "snapshot" |
| + txid, snapErr, err := r.real.FinishSnapshot(ctx) |
| + if r.snapErr != nil { |
| + snapErr = r.snapErr |
| + } |
| + return txid, snapErr, err |
| +} |
| + |
| +func spyOnRelease(t *testing.T, s *haSvc, e *actEntry, snapErr error) chan string { |
| + t.Helper() |
| + s.a.mu.Lock() |
| + defer s.a.mu.Unlock() |
| + real, ok := e.rep.(*lease.Replicator) |
| + if !ok { |
| + t.Fatalf("the entry's replica is a %T", e.rep) |
| + } |
| + calls := make(chan string, 4) |
| + e.rep = &snapshotSpyRep{Rep: real, haRep: real, real: real, snapErr: snapErr, calls: calls} |
| + return calls |
| +} |
| + |
| +func onlyCall(t *testing.T, calls chan string) string { |
| + t.Helper() |
| + var got []string |
| + for { |
| + select { |
| + case c := <-calls: |
| + got = append(got, c) |
| + default: |
| + if len(got) != 1 { |
| + t.Fatalf("the release's final syncs: %v, want one", got) |
| + } |
| + return got[0] |
| + } |
| + } |
| +} |
| + |
| +// The drain's release asks for the snapshot at the final position; an |
| +// idle sleep's does not (region HA §25.4 step 3.3, §25.10). |
| +func TestDrainStopReleasesWithASnapshot(t *testing.T) { |
| + s, e := haServiceEntry(t, "shop.example.com") |
| + calls := spyOnRelease(t, s, e, nil) |
| + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| + defer cancel() |
| + rel, err := s.a.DrainStop(ctx, s.rt.Host) |
| + if err != nil || !rel.Marker || !rel.Clean { |
| + t.Fatalf("drain stop: %+v %v", rel, err) |
| + } |
| + if c := onlyCall(t, calls); c != "snapshot" { |
| + t.Fatalf("the drain's release called %s, want the snapshot", c) |
| + } |
| +} |
| + |
| +func TestIdleStopReleasesWithoutASnapshot(t *testing.T) { |
| + s, e := haServiceEntry(t, "shop.example.com") |
| + calls := spyOnRelease(t, s, e, nil) |
| + s.stopForTest(e) |
| + if c := onlyCall(t, calls); c != "finish" { |
| + t.Fatalf("an idle sleep's release called %s, want the plain final sync", c) |
| + } |
| + if v := s.lockView(t); v.Lock.Release == nil || !v.Lock.Release.Clean { |
| + t.Fatalf("lock after the sleep: %+v", v.Lock.Release) |
| + } |
| +} |
| + |
| +// A snapshot that fails is logged; the release is clean as before. |
| +func TestDrainStopSnapshotFailureStaysClean(t *testing.T) { |
| + s, e := haServiceEntry(t, "shop.example.com") |
| + calls := spyOnRelease(t, s, e, errors.New("the store refused the snapshot")) |
| + ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) |
| + defer cancel() |
| + rel, err := s.a.DrainStop(ctx, s.rt.Host) |
| + if err != nil || !rel.Marker || !rel.Clean { |
| + t.Fatalf("drain stop: %+v %v, want a clean release", rel, err) |
| + } |
| + if c := onlyCall(t, calls); c != "snapshot" { |
| + t.Fatalf("the drain's release called %s", c) |
| + } |
| + if v := s.lockView(t); v.Lock.Release == nil || !v.Lock.Release.Clean { |
| + t.Fatalf("lock: %+v, want a clean marker", v.Lock.Release) |
| + } |
| +} |
diff --git a/internal/activator/harelease.go b/internal/activator/harelease.go| index 232fc2b35..ca3a403ef 100644 |
| --- a/internal/activator/harelease.go |
| +++ b/internal/activator/harelease.go |
| @@ -161,7 +161,11 @@ func haEntryRep(e *actEntry) (haRep, bool) { |
| // written and errHostSliceUnproven is returned: the lease stays held |
| // and renewing, and the stop stays pending; |
| // 3. stopFiles, then Final; |
| -// 4. Finish, then the local release record; |
| +// 4. Finish, then the local release record. The drain's release |
| +// (DrainStop) finishes with FinishSnapshot instead: the same final |
| +// sync, then a snapshot at the final position before the store's |
| +// close, so the successor restores one file (§25.4 step 3.3). A |
| +// snapshot that fails is logged and changes nothing else; |
| // 5. ReleaseMarker, clean only when Final was complete, Finish succeeded |
| // and the record was written. |
| // |
| @@ -170,7 +174,7 @@ func haEntryRep(e *actEntry) (haRep, bool) { |
| // (*lease.ObligationError) and retries it. |
| func (a *Service) releaseHAClean(ctx context.Context, e *actEntry) error { |
| a.mu.Lock() |
| - rt, rep := e.route, e.rep |
| + rt, rep, drain := e.route, e.rep, e.drainRelease |
| a.mu.Unlock() |
| hr, ok := rep.(haRep) |
| if !ok || !hr.HA() { |
| @@ -198,7 +202,17 @@ func (a *Service) releaseHAClean(ctx context.Context, e *actEntry) error { |
| // a long one must not leave it a spent context, and the database's |
| // last writes unsynced. |
| sctx, scancel := a.haReleaseContext(ctx, haFinalSyncBudget, haShutdownMarkerReserve) |
| - txid, finErr := hr.Finish(sctx) |
| + var txid uint64 |
| + var finErr error |
| + if sr, ok := hr.(haSnapshotRep); ok && drain { |
| + var snapErr error |
| + txid, snapErr, finErr = sr.FinishSnapshot(sctx) |
| + if snapErr != nil && finErr == nil { |
| + log.Printf("activator: %s: no snapshot at the drain's release (%v); the successor's restore replays the replica's files instead", rt.Host, snapErr) |
| + } |
| + } else { |
| + txid, finErr = hr.Finish(sctx) |
| + } |
| scancel() |
| m := hb.haReleaseBase(rt) |
| m.TXID, m.FilesGen, m.At = txid, gen, time.Now().UTC() |
diff --git a/internal/activator/hawake.go b/internal/activator/hawake.go| index 5f06f3fae..147892b34 100644 |
| --- a/internal/activator/hawake.go |
| +++ b/internal/activator/hawake.go |
| @@ -54,9 +54,16 @@ type haRep interface { |
| ReleaseMarker(ctx context.Context, m hafresh.Marker) error |
| } |
| +// haSnapshotRep is an HA replica whose final sync can end with a |
| +// snapshot at the final position: the drain's release (releaseHAClean). |
| +type haSnapshotRep interface { |
| + FinishSnapshot(ctx context.Context) (txid uint64, snapErr, err error) |
| +} |
| + |
| var ( |
| - _ haLease = (*execLease)(nil) |
| - _ haRep = (*lease.Replicator)(nil) |
| + _ haLease = (*execLease)(nil) |
| + _ haRep = (*lease.Replicator)(nil) |
| + _ haSnapshotRep = (*lease.Replicator)(nil) |
| ) |
| func (l *execLease) HA() bool { return l.held.HA() } |
diff --git a/internal/lease/lease.go b/internal/lease/lease.go| index 1c7e7b7c6..389bf4804 100644 |
| --- a/internal/lease/lease.go |
| +++ b/internal/lease/lease.go |
| @@ -494,7 +494,10 @@ type Replicator struct { |
| finalized bool |
| finishTXID uint64 |
| finishErr error |
| - closeErr error |
| + // snapErr is the finalization's snapshot outcome (FinishSnapshot): |
| + // nil for a snapshot written at the final TXID. |
| + snapErr error |
| + closeErr error |
| } |
| // ReplicateWithLease acquires the single-writer lease at |
| @@ -551,7 +554,7 @@ func (r *Replicator) Abandon(ctx context.Context) { |
| // would skip; it ships nothing as long as the caller killed the writer |
| // first, and reclaims the replication goroutines either way. |
| if r.ha { |
| - _, _ = r.finalize(ctx, false) // the one teardown; a later release never syncs |
| + _, _ = r.finalize(ctx, false, false) // the one teardown; a later release never syncs |
| return |
| } |
| _ = r.store.Close(ctx) |
diff --git a/internal/lease/release.go b/internal/lease/release.go| index e50cfcfc6..59a2735f4 100644 |
| --- a/internal/lease/release.go |
| +++ b/internal/lease/release.go |
| @@ -12,10 +12,23 @@ import ( |
| awss3 "github.com/aws/aws-sdk-go-v2/service/s3" |
| "github.com/benbjohnson/litestream" |
| lss3 "github.com/benbjohnson/litestream/s3" |
| + "github.com/superfly/ltx" |
| "github.com/carlosframework/platform/internal/hafresh" |
| ) |
| +// snapshotWithin writes the release's snapshot within half of what is left |
| +// of ctx's budget, so a slow one never leaves the store's close (its own |
| +// final sync, which decides the release's clean flag) a spent context. |
| +func (r *Replicator) snapshotWithin(ctx context.Context) (*ltx.FileInfo, error) { |
| + if d, ok := ctx.Deadline(); ok { |
| + var cancel context.CancelFunc |
| + ctx, cancel = context.WithTimeout(ctx, time.Until(d)/2) |
| + defer cancel() |
| + } |
| + return r.db.Snapshot(ctx) |
| +} |
| + |
| // releaseWithMarker is the one HA release. lease never deletes an HA lock: |
| // it rewrites it as a release marker, MarkerBody(cur.Generation, m), If-Match |
| // cur's ETag. The caller has stopped the renewer and passes the lease it |
| @@ -150,9 +163,34 @@ func (r *Replicator) MonitorInterval() time.Duration { return r.db.MonitorInterv |
| // (finalize), shared with Close and ReleaseMarker: it runs once, and |
| // later calls return its answer without syncing again. |
| func (r *Replicator) Finish(ctx context.Context) (txid uint64, err error) { |
| - return r.finalize(ctx, true) |
| + return r.finalize(ctx, true, false) |
| +} |
| + |
| +// FinishSnapshot is Finish for a planned handover's release (the drain, |
| +// region HA §25.4 step 3.3): after the forced full sync has succeeded, |
| +// and before the store's close, it writes a snapshot of the database at |
| +// the final position (litestream's level 9), so the successor's restore |
| +// reads that one file and no L0 tail. Nothing writes the database by |
| +// then (the app is proven stopped), so the snapshot's TXID is the final |
| +// TXID the marker records; snapErr says if it is not. |
| +// |
| +// snapErr is the snapshot's own failure, and changes nothing else: txid |
| +// and err are Finish's, the release is as clean as it would have been |
| +// without it, and the restore replays the L0 files as before. Like Finish |
| +// it is the replicator's one finalization: if a finalization already ran, |
| +// it returns that answer and writes no snapshot (snapErr ErrNoSnapshot). |
| +func (r *Replicator) FinishSnapshot(ctx context.Context) (txid uint64, snapErr, err error) { |
| + txid, err = r.finalize(ctx, true, true) |
| + r.finMu.Lock() |
| + snapErr = r.snapErr |
| + r.finMu.Unlock() |
| + return txid, snapErr, err |
| } |
| +// ErrNoSnapshot is FinishSnapshot's snapErr when no snapshot was written: |
| +// the final sync failed, or the finalization had already run without one. |
| +var ErrNoSnapshot = errors.New("lease: no snapshot written at the release") |
| + |
| // errAbandoned is the finalization answer of a replicator Abandon tore |
| // down: no final sync ran, so no marker it writes is clean. |
| var errAbandoned = errors.New("lease: replication abandoned without a final sync") |
| @@ -167,7 +205,7 @@ var finalSyncHook = func() {} |
| // for one already running, so no replica write can follow the marker. |
| // Once it has run, nothing syncs again: later calls, SyncAndWait |
| // included, get its answer or ErrSpent. |
| -func (r *Replicator) finalize(ctx context.Context, sync bool) (uint64, error) { |
| +func (r *Replicator) finalize(ctx context.Context, sync, snapshot bool) (uint64, error) { |
| r.finMu.Lock() |
| defer r.finMu.Unlock() |
| if r.finalized { |
| @@ -179,9 +217,29 @@ func (r *Replicator) finalize(ctx context.Context, sync bool) (uint64, error) { |
| finalSyncHook() |
| syncErr = r.db.SyncAndWait(ctx) |
| } |
| + // The drain's snapshot (FinishSnapshot): only over a final sync that |
| + // succeeded, and before the store's close, which closes the database |
| + // the snapshot reads. Still inside the finalization, so it is never |
| + // after the marker. |
| + var snapTXID uint64 |
| + r.snapErr = ErrNoSnapshot |
| + if snapshot && syncErr == nil { |
| + info, err := r.snapshotWithin(ctx) |
| + switch { |
| + case err != nil: |
| + r.snapErr = fmt.Errorf("lease: snapshot at the release: %w", err) |
| + default: |
| + r.snapErr, snapTXID = nil, uint64(info.MaxTXID) |
| + } |
| + } |
| r.closeErr = r.store.Close(ctx) |
| r.finishTXID = uint64(r.db.Replica.Pos().TXID) |
| r.finishErr = errors.Join(syncErr, r.closeErr, r.rn.Err()) |
| + if r.snapErr == nil && snapTXID != r.finishTXID { |
| + // Harmless for a restore (the snapshot, then the L0 files after |
| + // it), but not the one-file restore this was for. |
| + r.snapErr = fmt.Errorf("lease: snapshot at txid %d, final txid %d", snapTXID, r.finishTXID) |
| + } |
| return r.finishTXID, r.finishErr |
| } |
| @@ -201,7 +259,7 @@ func (r *Replicator) ReleaseMarker(ctx context.Context, m hafresh.Marker) error |
| } |
| r.released = true |
| r.mu.Unlock() |
| - txid, err := r.finalize(ctx, true) |
| + txid, err := r.finalize(ctx, true, false) |
| m = withBase(r.base, m) |
| m.TXID = txid |
| if err != nil { |
| @@ -222,7 +280,7 @@ func (r *Replicator) closeHA(ctx context.Context) error { |
| } |
| r.released = true |
| r.mu.Unlock() |
| - txid, _ := r.finalize(ctx, true) |
| + txid, _ := r.finalize(ctx, true, false) |
| lease := r.rn.stop() |
| relCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| defer cancel() |
diff --git a/internal/lease/release_snapshot_test.go b/internal/lease/release_snapshot_test.go| new file mode 100644 |
| index 000000000..55eae69ef |
| --- /dev/null |
| +++ b/internal/lease/release_snapshot_test.go |
| @@ -0,0 +1,209 @@ |
| +package lease_test |
| + |
| +import ( |
| + "context" |
| + "database/sql" |
| + "errors" |
| + "io" |
| + "path/filepath" |
| + "sync" |
| + "testing" |
| + |
| + "github.com/benbjohnson/litestream" |
| + lsfile "github.com/benbjohnson/litestream/file" |
| + "github.com/superfly/ltx" |
| + |
| + "github.com/carlosframework/platform/internal/hafresh" |
| + "github.com/carlosframework/platform/internal/lease" |
| + leasetest "github.com/carlosframework/platform/internal/lease/locktest" |
| +) |
| + |
| +// The drain's release leaves a restore of one file (region HA §25.4 step |
| +// 3.3, §25.10): after the final sync and before the marker it writes a |
| +// snapshot at the final position, so the successor restores the snapshot |
| +// 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. |
| +type countingReplica struct { |
| + litestream.ReplicaClient |
| + mu sync.Mutex |
| + opens int |
| + writes []int |
| + snapshotTXID ltx.TXID |
| + failSnapshot bool |
| +} |
| + |
| +func (c *countingReplica) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { |
| + c.mu.Lock() |
| + c.opens++ |
| + c.mu.Unlock() |
| + return c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) |
| +} |
| + |
| +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 |
| + if level == litestream.SnapshotLevel { |
| + c.snapshotTXID = maxTXID |
| + } |
| + c.mu.Unlock() |
| + if fail { |
| + io.Copy(io.Discard, r) |
| + return nil, errors.New("the store refused the snapshot") |
| + } |
| + 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 { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + k := 0 |
| + for _, l := range c.writes[n:] { |
| + if l == litestream.SnapshotLevel { |
| + k++ |
| + } |
| + } |
| + return k |
| +} |
| + |
| +func (c *countingReplica) openCount() int { |
| + c.mu.Lock() |
| + defer c.mu.Unlock() |
| + return c.opens |
| +} |
| + |
| +// manyL0 replicates a fresh database to dir through rc and makes n small |
| +// transactions, each synced on its own: n L0 files, as a busy host's |
| +// replica has between compactions. |
| +func manyL0(t *testing.T, fx *leasetest.LockFixture, prefix string, rc litestream.ReplicaClient, n int) (*lease.Replicator, string) { |
| + t.Helper() |
| + h := haHeld(t, fx, prefix) |
| + lease.SetReplicaClientForTest(h, rc) |
| + path := tempDB(t) |
| + r, err := h.Replicate(ctx, path, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for i := 0; i < n; i++ { |
| + addRows(t, path, 1) |
| + if err := r.SyncAndWait(ctx); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + return r, path |
| +} |
| + |
| +// restoreCounting restores dir's replica into a new file, counting the |
| +// LTX files it reads. |
| +func restoreCounting(t *testing.T, dir string) (string, int) { |
| + t.Helper() |
| + c := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir)} |
| + out := filepath.Join(t.TempDir(), "restored.db") |
| + opt := litestream.NewRestoreOptions() |
| + opt.OutputPath = out |
| + if err := litestream.NewReplicaWithClient(nil, c).Restore(ctx, opt); err != nil { |
| + t.Fatal(err) |
| + } |
| + return out, c.openCount() |
| +} |
| + |
| +func dbRows(t *testing.T, path string) string { |
| + t.Helper() |
| + db, err := sql.Open("sqlite", path) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + defer db.Close() |
| + var n int |
| + var all sql.NullString |
| + if err := db.QueryRow(`SELECT count(*), group_concat(id || ':' || body, ',') FROM (SELECT * FROM t ORDER BY id)`).Scan(&n, &all); err != nil { |
| + t.Fatal(err) |
| + } |
| + return all.String |
| +} |
| + |
| +func TestDrainReleaseSnapshotRestoresFromOneFile(t *testing.T) { |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(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) |
| + 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 { |
| + 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) |
| + } |
| + out, reads := restoreCounting(t, dir) |
| + if reads > 2 { |
| + t.Fatalf("the successor's restore read %d files, want the snapshot alone", reads) |
| + } |
| + 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) |
| + } |
| +} |
| + |
| +// An idle sleep's release (Finish) writes no snapshot. |
| +func TestSleepReleaseWritesNoSnapshot(t *testing.T) { |
| + haBox(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 { |
| + t.Fatal(err) |
| + } |
| + if err := r.ReleaseMarker(ctx, 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) |
| + } |
| +} |
| + |
| +// A snapshot that fails changes nothing else: the release is clean as |
| +// before, and the restore replays the L0 files. |
| +func TestDrainReleaseSnapshotFailureStaysClean(t *testing.T) { |
| + haBox(t) |
| + fx := leasetest.NewLockFixture(t) |
| + dir := t.TempDir() |
| + rc := &countingReplica{ReplicaClient: lsfile.NewReplicaClient(dir)} |
| + r, path := manyL0(t, fx, "instances/h", rc, 20) |
| + rc.mu.Lock() |
| + rc.failSnapshot = true |
| + rc.mu.Unlock() |
| + txid, snapErr, err := r.FinishSnapshot(ctx) |
| + if err != nil || snapErr == nil || txid == 0 { |
| + 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) |
| + } |
| + out, _ := restoreCounting(t, dir) |
| + if got, want := dbRows(t, out), dbRows(t, path); got != want { |
| + t.Fatalf("restored %q, want %q", got, want) |
| + } |
| +} |