lease: a proven-empty fleet checkpoint releases (M1 review P1)
A fleet binary that never creates its database left its stop pending forever: litestream never initializes the DB and the replica sync can only fail. After a failed final sync CloseProven now checks two observed facts: the local database file does not exist, and the replica prefix holds no LTX file at any level (nothing was shipped there, so nothing was restored). Both true, it releases as Close does; any other failure stays ErrCheckpointUnproven. Witnesses: a real fleet binary that never opens its DB stops, releases and wakes again; a DB that exists but never synced stays pending.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+211
−2
internal/activator/pendingwake_linux_test.go+76 −1internal/lease/lease.go+50 −1internal/lease/release_test.go+85 −0
diff --git a/internal/activator/pendingwake_linux_test.go b/internal/activator/pendingwake_linux_test.go| index 5fad06770..2e46f3e12 100644 |
| --- a/internal/activator/pendingwake_linux_test.go |
| +++ b/internal/activator/pendingwake_linux_test.go |
| @@ -1405,7 +1405,7 @@ func (r *checkpointRig) wake(t *testing.T) *actEntry { |
| t.Helper() |
| release, err := r.a.beginRequest(context.Background(), r.rt) |
| if err != nil { |
| - t.Fatalf("wake: %v", err) |
| + t.Fatalf("wake: %v\n%s", err, r.log.String()) |
| } |
| release() |
| r.a.mu.Lock() |
| @@ -1492,6 +1492,81 @@ func TestFleetStopWithFailedCheckpointStaysPending(t *testing.T) { |
| } |
| } |
| +// removeDB removes the rig's database and its -wal and -shm files: an app |
| +// that has never created its database. |
| +func (r *checkpointRig) removeDB(t *testing.T) { |
| + t.Helper() |
| + for _, p := range []string{r.rt.DBPath, r.rt.DBPath + "-wal", r.rt.DBPath + "-shm"} { |
| + if err := os.Remove(p); err != nil && !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +// M1 review Astra P1-2: a fleet binary that never opens its database (the |
| +// fake app serves without touching it, and its prefix holds nothing) has |
| +// nothing to checkpoint. Its stop finishes and releases the lease, as a |
| +// proven-empty checkpoint, rather than staying pending forever with the |
| +// lease renewing; and it wakes again. |
| +func TestFleetStopOfANeverCreatedDatabaseFinishes(t *testing.T) { |
| + r := newCheckpointRig(t, registry.PlacementTarget("pi")) |
| + r.removeDB(t) |
| + for round := range 2 { |
| + // The fake app cannot bind over the socket file its killed |
| + // predecessor left (a real app unlinks it first). |
| + if err := os.Remove(r.rt.Socket); err != nil && !errors.Is(err, os.ErrNotExist) { |
| + t.Fatal(err) |
| + } |
| + e := r.wake(t) |
| + stopped := r.stop(e) |
| + select { |
| + case <-stopped: |
| + case <-r.kept: |
| + t.Fatalf("round %d: the stop was kept pending over a database that was never created:\n%s", round, r.log.String()) |
| + case <-time.After(30 * time.Second): |
| + t.Fatalf("round %d: the stop never finished:\n%s", round, r.log.String()) |
| + } |
| + if body := r.fx.LockBody(r.prefix); body != nil { |
| + t.Fatalf("round %d: the lease is still held after the stop: %s", round, body) |
| + } |
| + r.a.mu.Lock() |
| + outcome := e.stopOutcome |
| + r.a.mu.Unlock() |
| + if outcome == nil || !outcome.replicaClosed { |
| + t.Fatalf("round %d: the stop finished unclean (%+v)", round, outcome) |
| + } |
| + if _, err := os.Stat(r.rt.DBPath); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("round %d: the database exists (%v); the witness needs it never created", round, err) |
| + } |
| + } |
| + if strings.Contains(r.log.String(), c44Line) { |
| + t.Fatalf("a stop logged C44:\n%s", r.log.String()) |
| + } |
| +} |
| + |
| +// The proven-empty release needs the database to be missing: one that |
| +// exists but has never reached the bucket (every replica PUT refused since |
| +// before the wake) stays pending, the lease held and renewing. |
| +func TestFleetStopOfAnUnsyncedDatabaseStaysPending(t *testing.T) { |
| + r := newCheckpointRig(t, registry.PlacementTarget("pi")) |
| + t.Cleanup(r.fx.FailReplicaPuts(r.prefix)) |
| + e := r.wake(t) |
| + stopped := r.stop(e) |
| + select { |
| + case <-r.kept: |
| + case <-stopped: |
| + t.Fatal("the stop finished over a database the bucket never received") |
| + case <-time.After(30 * time.Second): |
| + t.Fatal("the stop neither finished nor was kept pending") |
| + } |
| + if r.fx.LockBody(r.prefix) == nil { |
| + t.Fatal("the lease was released over an unproven checkpoint") |
| + } |
| + if err := e.rep.Err(); err != nil { |
| + t.Fatalf("the lease is no longer held: %v", err) |
| + } |
| +} |
| + |
| // At shutdown an unproven checkpoint is not released: the replicator is |
| // abandoned, so the lease runs to its TTL, and an ERROR names the host. |
| func TestShutdownWithUnprovenCheckpointLeavesLeaseToTTL(t *testing.T) { |
diff --git a/internal/lease/lease.go b/internal/lease/lease.go| index 22ce699a0..484e07ad8 100644 |
| --- a/internal/lease/lease.go |
| +++ b/internal/lease/lease.go |
| @@ -806,7 +806,9 @@ var ErrCheckpointAbandoned = errors.New("lease: final checkpoint not confirmed a |
| // torn down and nothing released: the answer wraps ErrCheckpointUnproven |
| // and the cause, replication keeps running and the lease stays held and |
| // renewing, so the caller can call CloseProven again. When it succeeds, |
| -// CloseProven does exactly what Close does. |
| +// CloseProven does exactly what Close does. So it does over a failed sync |
| +// that is a proven-empty checkpoint (provenEmpty: no local database file, |
| +// and nothing ever shipped to the prefix), the one failure that releases. |
| // |
| // A failed sync over a lease whose renewal has already failed (Err) is |
| // different: that error latches on one failed renewal or a local pause, |
| @@ -837,6 +839,11 @@ func (r *Replicator) CloseProven(ctx context.Context) error { |
| // which answers for the lease. |
| return fmt.Errorf("%w: the final checkpoint failed and the replicator was torn down meanwhile: %w", ErrSpent, err) |
| } |
| + if empty, eerr := r.provenEmpty(ctx); eerr == nil && empty { |
| + // Nothing to checkpoint: a proven-empty checkpoint, released as |
| + // Close releases (M1 review Astra P1-2). |
| + return r.teardownAndRelease(ctx) |
| + } |
| r.endClose() |
| if lost := r.rn.Err(); lost != nil { |
| done, cancel := context.WithCancel(context.Background()) |
| @@ -851,6 +858,48 @@ func (r *Replicator) CloseProven(ctx context.Context) error { |
| return fmt.Errorf("%w: %w", ErrCheckpointUnproven, err) |
| } |
| +// provenEmpty reports whether a non-HA replicator positively has nothing |
| +// to checkpoint (M1 review Astra P1-2, controller ruling): a fleet binary |
| +// that never creates its database leaves litestream's DB uninitialized, |
| +// and its replica sync can never succeed (no position, waiting for data), |
| +// so without this its stop would stay pending forever. Both halves are |
| +// observed, never inferred from the sync's error: |
| +// |
| +// - the local database file does not exist now, at stop time (the |
| +// writer is already stopped): there is nothing on disk to ship; |
| +// - the replica prefix holds no LTX file at any level, 0 to |
| +// litestream.SnapshotLevel: nothing was ever shipped there, by this |
| +// lease or any other, so this lease neither shipped nor restored a |
| +// generation (a wake restores only from this prefix, and a restore |
| +// would have created the file). |
| +// |
| +// Any error, or any file found, is false: the checkpoint stays unproven. |
| +// It costs one LIST per level, and runs only after a failed final sync |
| +// over a missing database. |
| +func (r *Replicator) provenEmpty(ctx context.Context) (bool, error) { |
| + if _, err := os.Stat(r.db.Path()); !errors.Is(err, os.ErrNotExist) { |
| + return false, err |
| + } |
| + if r.db.Replica == nil || r.db.Replica.Client == nil { |
| + return false, fmt.Errorf("no replica configured") |
| + } |
| + for level := 0; level <= litestream.SnapshotLevel; level++ { |
| + itr, err := r.db.Replica.Client.LTXFiles(ctx, level, 0, false) |
| + if err != nil { |
| + return false, err |
| + } |
| + found := itr.Next() |
| + err = itr.Close() |
| + if found { |
| + return false, nil |
| + } |
| + if err != nil { |
| + return false, err |
| + } |
| + } |
| + return true, nil |
| +} |
| + |
| // guardedSync is litestream's DB.SyncAndWait, the database sync and then |
| // the replica sync, for a non-HA replicator, guarded against its teardown |
| // (syncGate): ErrSpent, with nothing synced, once a teardown has begun, |
diff --git a/internal/lease/release_test.go b/internal/lease/release_test.go| index 20ff7844f..23d13351c 100644 |
| --- a/internal/lease/release_test.go |
| +++ b/internal/lease/release_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "fmt" |
| "io" |
| "net/http" |
| + "os" |
| "path/filepath" |
| "slices" |
| "strings" |
| @@ -550,3 +551,87 @@ func TestCloseProvenAbandonsWhenTheLeaseIsLost(t *testing.T) { |
| t.Fatal("the replicator was not torn down") |
| } |
| } |
| + |
| +// M1 review Astra P1-2: a fleet binary that never creates its database |
| +// has nothing to checkpoint, and its stop must still finish. With no |
| +// local database file and nothing at all in the prefix (no LTX file at |
| +// any level: nothing was ever shipped there, so nothing was restored |
| +// from it either), CloseProven's failed sync is a proven-empty checkpoint |
| +// and it releases as Close does. |
| +func TestCloseProvenReleasesANeverCreatedDatabase(t *testing.T) { |
| + fx := leasetest.NewLockFixture(t) |
| + prefix := "instances/empty.example.com" |
| + h, err := lease.Acquire(ctx, fx.Bucket, prefix, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dbPath := filepath.Join(t.TempDir(), "app.db") // never created |
| + r, err := h.Replicate(ctx, dbPath, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := r.CloseProven(ctx); err != nil { |
| + t.Fatalf("CloseProven over a database that was never created = %v, want the lease released", err) |
| + } |
| + if body := fx.LockBody(prefix); body != nil { |
| + t.Fatalf("the lease is still held: %s", body) |
| + } |
| +} |
| + |
| +// The proven-empty answer needs both halves. A database that exists but |
| +// cannot sync stays unproven over an empty bucket; a missing database |
| +// stays unproven over a prefix that holds a backup. |
| +func TestCloseProvenEmptyNeedsBothHalves(t *testing.T) { |
| + // The cleanup's abandonment tries its failing final sync once. |
| + t.Cleanup(lease.SetShutdownSyncTimeoutForTest(0)) |
| + t.Run("database exists, bucket empty", func(t *testing.T) { |
| + fx := leasetest.NewLockFixture(t) |
| + prefix := "instances/unsynced.example.com" |
| + t.Cleanup(fx.FailReplicaPuts(prefix)) |
| + h, err := lease.Acquire(ctx, fx.Bucket, prefix, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + r, err := h.Replicate(ctx, tempDB(t), 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { r.Abandon(ctx) }) |
| + if err := r.CloseProven(ctx); !errors.Is(err, lease.ErrCheckpointUnproven) { |
| + t.Fatalf("CloseProven over an existing database that cannot sync = %v, want ErrCheckpointUnproven", err) |
| + } |
| + if fx.LockBody(prefix) == nil { |
| + t.Fatal("the lease was released over an unproven checkpoint") |
| + } |
| + }) |
| + t.Run("database missing, bucket holds a backup", func(t *testing.T) { |
| + fx := leasetest.NewLockFixture(t) |
| + prefix := "instances/backedup.example.com" |
| + h, err := lease.Acquire(ctx, fx.Bucket, prefix, "") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + dbPath := tempDB(t) |
| + r, err := h.Replicate(ctx, dbPath, 0) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { r.Abandon(ctx) }) |
| + if err := r.SyncAndWait(ctx); err != nil { |
| + t.Fatalf("first sync: %v", err) |
| + } |
| + t.Cleanup(fx.FailReplicaPuts(prefix)) |
| + addRows(t, dbPath, 1) // a write the bucket cannot take |
| + for _, p := range []string{dbPath, dbPath + "-wal", dbPath + "-shm"} { |
| + if err := os.Remove(p); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := r.CloseProven(ctx); !errors.Is(err, lease.ErrCheckpointUnproven) { |
| + t.Fatalf("CloseProven over a missing database with a backup = %v, want ErrCheckpointUnproven", err) |
| + } |
| + if fx.LockBody(prefix) == nil { |
| + t.Fatal("the lease was released over an unproven checkpoint") |
| + } |
| + }) |
| +} |