carlos / platform Public

drill: don't test a replica a landed move's copy will replace (#898 R1)

After an offline move lands and before the first wake promotes the
staged copy, instances/<host>/ is not the instance's copy: empty, or on
a boomerang an orphan of an earlier stay that restores clean (a stale
pass). A promotion racing the restore could read a mixed lineage (a
false failed).

The drill now checks what the promotion itself trusts
(promoteStagedReplica): any object under moves/<host>/ in this box's
data bucket. One bounded, read-only listing (one key) at the bracket's
open and again at its close. Staged at either end: unverified, moving,
with no restore at the open. Unreadable: unverified,
storage_unavailable, never a pass or a failure.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 2a6daefacd39a5f9dd56d68dcd187f82b2b371db parent 4a9f46b
3 files changed, +365 −8
  • internal/ops/hibernate_verify.go +83 −3
  • internal/ops/hibernate_verify_staging_test.go +264 −0
  • internal/ops/hibernate_verify_test.go +18 −5
diff --git a/internal/ops/hibernate_verify.go b/internal/ops/hibernate_verify.go
index b786f67ae..cf2191ff7 100644
--- a/internal/ops/hibernate_verify.go
+++ b/internal/ops/hibernate_verify.go
@@ -27,6 +27,7 @@ import (
"os"
"slices"
"sort"
+ "strings"
"sync"
"syscall"
"time"
@@ -140,8 +141,22 @@ var (
// verifyAt restores the planned position through the planner's own
// listing (member backups 2a, R2): it holds a reader that can only list
// and open, and lists nothing the plan did not.
- verifyAt = lease.VerifyReaderAt
- lockState = lease.LockState
+ verifyAt = lease.VerifyReaderAt
+ lockState = lease.LockState
+ // openStaging opens this box's data bucket for the staged-copy check
+ // (stagedCopy): the bucket a move stages into and the activator's
+ // first wake promotes from. Once per run.
+ openStaging = func(ctx context.Context, bucket, region string) (release.PageLister, error) {
+ st, err := lease.NewFilesStore(ctx, bucket, region)
+ if err != nil {
+ return nil, err
+ }
+ pl, ok := st.(release.PageLister)
+ if !ok {
+ return nil, errors.New("the data bucket has no bounded listing")
+ }
+ return pl, nil
+ }
localFreshness = hibernateFreshness
// publishTicker is the run's publication clock: ticks every d, and a
// stop.
@@ -177,6 +192,11 @@ type Drill struct {
Budget time.Duration
Max int
PublishEvery time.Duration
+
+ // staging is the data bucket the staged-copy check lists, opened once
+ // by VerifyHibernating; stagingErr is why it could not be.
+ staging release.PageLister
+ stagingErr error
}
// VerifyHibernating restore-drills the given hosts in order and returns a
@@ -205,7 +225,8 @@ type Drill struct {
//
// Each host is bracketed (P-11): its record is read before planning and
// again after the last replica byte, and any difference discards what the
-// restore found. Freshness is judged only on the box that provably holds
+// restore found. So is its move staging (R1): a staged copy waiting for
+// the first wake's promotion at either end is unverified, moving. Freshness is judged only on the box that provably holds
// the live lease (P-14).
func VerifyHibernating(ctx context.Context, d Drill, hosts []HibernatingRoute, now func() time.Time, publish func(map[string]dbverify.Verdict) bool) map[string]dbverify.Verdict {
start := now()
@@ -214,6 +235,7 @@ func VerifyHibernating(ctx context.Context, d Drill, hosts []HibernatingRoute, n
runCtx, cancel = context.WithTimeout(ctx, d.Budget)
}
defer cancel()
+ d.staging, d.stagingErr = openStaging(runCtx, d.Cfg.Bucket, d.Cfg.Region)
var res runResults
if publish != nil && d.PublishEvery > 0 {
@@ -384,6 +406,18 @@ func (d Drill) drillOne(ctx context.Context, rt HibernatingRoute, now func() tim
case dbreads.CodeMoving:
return unverified(ReasonMoving, "the instance was moving when the test began")
}
+ // A move that has landed here and not yet been promoted by the first
+ // wake (R1): its record is at rest, but the copy the instance will wake
+ // from is the staged one, not the replica this would test. Not tested;
+ // and when the staging cannot be read, nothing is either.
+ switch staged, err := d.stagedCopy(ctx, rt.Host); {
+ case err != nil && ctx.Err() != nil:
+ return dbverify.Verdict{}, false
+ case err != nil:
+ return unverified(ReasonStorageUnavailable, err.Error())
+ case staged:
+ return unverified(ReasonMoving, "the instance was moving when the test began")
+ }
// Each host restores into its own scratch directory, removed whatever
// happens, so an abandoned download leaves nothing behind.
@@ -422,6 +456,17 @@ func (d Drill) drillOne(ctx context.Context, rt HibernatingRoute, now func() tim
if code != "" {
return unverified(bracketReason(code), "the instance changed during the test, so its result was discarded")
}
+ // The staging again: a move's copy that arrived during the test (a
+ // landing, or its promotion racing the restore) discards the result,
+ // so neither a stale pass nor a mixed-lineage failure can stand.
+ switch staged, err := d.stagedCopy(ctx, rt.Host); {
+ case err != nil && ctx.Err() != nil:
+ return dbverify.Verdict{}, false
+ case err != nil:
+ return unverified(ReasonStorageUnavailable, "the move staging could not be read again after the restore, so its result was discarded: "+err.Error())
+ case staged:
+ return unverified(ReasonMoving, "the instance changed during the test, so its result was discarded")
+ }
v.VerifiedAt = now().UTC()
switch res.kind {
@@ -448,6 +493,41 @@ func (d Drill) drillOne(ctx context.Context, rt HibernatingRoute, now func() tim
return v, true
}
+// stagedCopy reports whether a move's staged copy of host waits under
+// moves/<host>/ in this box's data bucket (member backups 2a, R1). That is
+// what the activator's first wake trusts (promoteStagedReplica): any
+// object there, of any name, is mirrored over instances/<host>/ before
+// the restore, because staging wins over the own prefix. While one waits,
+// the replica the drill would test is not the instance's: after an offline
+// move lands it is empty, or an orphan of an earlier stay on this box that
+// restores clean. The listing asks for one key, read-only, as the
+// console's restore staging check does (hostHasStaging); the trailing
+// slash keeps app.example.co from seeing app.example.com's keys. An error
+// is returned, never read as nothing staged.
+func (d Drill) stagedCopy(ctx context.Context, host string) (bool, error) {
+ if d.stagingErr != nil {
+ return false, fmt.Errorf("move staging: %w", d.stagingErr)
+ }
+ if d.staging == nil {
+ return false, errors.New("move staging: the data bucket was not opened")
+ }
+ prefix := activator.MovesPrefix(host)
+ if host == "" || !strings.HasSuffix(prefix, "/") {
+ return false, fmt.Errorf("move staging: no staging prefix for %q", host)
+ }
+ keys, err := d.staging.ListPage(ctx, prefix, "", 1)
+ if err != nil {
+ return false, fmt.Errorf("move staging: list %s: %w", prefix, err)
+ }
+ if len(keys) == 0 {
+ return false, nil
+ }
+ if !strings.HasPrefix(keys[0], prefix) {
+ return false, fmt.Errorf("move staging: the listing of %s returned %q, outside it", prefix, keys[0])
+ }
+ return true, nil
+}
+
// bracketReason is the verdict reason for a bracket code.
func bracketReason(code string) string {
if code == dbreads.CodeMoving {
diff --git a/internal/ops/hibernate_verify_staging_test.go b/internal/ops/hibernate_verify_staging_test.go
new file mode 100644
index 000000000..31831b3ef
--- /dev/null
+++ b/internal/ops/hibernate_verify_staging_test.go
@@ -0,0 +1,264 @@
+package ops
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "io/fs"
+ "os"
+ "path/filepath"
+ "testing"
+
+ "github.com/carlosframework/platform/internal/activator"
+ "github.com/carlosframework/platform/internal/dbdoor"
+ "github.com/carlosframework/platform/internal/dbverify"
+ "github.com/carlosframework/platform/internal/lease"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// Member backups 2a, R1: after an offline move lands on a box and before
+// the instance's first wake promotes the staged copy, the replica under
+// instances/<host>/ is not the instance's. On a box the instance stayed on
+// before it is an orphan of that stay, which restores clean. The drill
+// recognises "landed, awaiting promotion" by what the promotion itself
+// trusts (activator promoteStagedReplica): any object under moves/<host>/
+// in this box's data bucket, where staging wins over the own prefix.
+
+// stagingSpy is the fixture's data bucket as the staged-copy check reads
+// it: it records each listing's prefix, refuses an unbounded one, and
+// fails with fx.stagingErr when that is set.
+type stagingSpy struct{ fx *drillFx }
+
+func (s stagingSpy) ListPage(ctx context.Context, prefix, startAfter string, limit int) ([]string, error) {
+ s.fx.mu.Lock()
+ s.fx.stagingList = append(s.fx.stagingList, prefix)
+ err := s.fx.stagingErr
+ s.fx.mu.Unlock()
+ if limit != 1 || startAfter != "" {
+ s.fx.t.Errorf("staging listed %q after %q, limit %d; want one key from the start", prefix, startAfter, limit)
+ }
+ if err != nil {
+ return nil, err
+ }
+ return s.fx.staging.ListPage(ctx, prefix, startAfter, limit)
+}
+
+// stage puts a move's staged copy of host where the stager puts it: every
+// object of a real replica (the shared one), under activator.MovesPrefix,
+// at the same relative keys as the source's instances/<host>/ prefix.
+func (fx *drillFx) stage(host string) {
+ fx.t.Helper()
+ err := filepath.WalkDir(fx.replica, func(p string, de fs.DirEntry, err error) error {
+ if err != nil || de.IsDir() {
+ return err
+ }
+ rel, err := filepath.Rel(fx.replica, p)
+ if err != nil {
+ return err
+ }
+ body, err := os.ReadFile(p)
+ if err != nil {
+ return err
+ }
+ _, err = fx.staging.Put(context.Background(), activator.MovesPrefix(host)+filepath.ToSlash(rel), body, "")
+ return err
+ })
+ if err != nil {
+ fx.t.Fatal(err)
+ }
+ keys, err := fx.staging.List(context.Background(), activator.MovesPrefix(host))
+ if err != nil || len(keys) == 0 {
+ fx.t.Fatalf("fixture: staged %v, %v", keys, err)
+ }
+}
+
+// listedStaging is how many times the staged-copy check listed host's
+// staging prefix.
+func (fx *drillFx) listedStaging(host string) int {
+ fx.mu.Lock()
+ defer fx.mu.Unlock()
+ n := 0
+ for _, p := range fx.stagingList {
+ if p == activator.MovesPrefix(host) {
+ n++
+ }
+ }
+ return n
+}
+
+// countPlans counts the replica openings per prefix, so a test can see
+// that a host was never planned.
+func (fx *drillFx) countPlans() map[string]int {
+ plans := map[string]int{}
+ real := openReplica
+ swap(fx.t, &openReplica, func(ctx context.Context, bucket, prefix, region string) (dbdoor.ReplicaReader, error) {
+ fx.mu.Lock()
+ plans[prefix]++
+ fx.mu.Unlock()
+ return real(ctx, bucket, prefix, region)
+ })
+ return plans
+}
+
+// Landed, not promoted, with an old orphan copy that restores clean under
+// instances/<host>/: unverified, moving, and nothing is planned or
+// restored. The control host (no staging, the same orphan lineage) passes
+// through the production restore, so the moving verdict is the staging's
+// doing, not the replica's.
+func TestDrillLandedCopyAwaitingPromotionIsMoving(t *testing.T) {
+ const landed, control = "landed.example.com", "control.example.com"
+ fx := newDrillFx(t, landed, control)
+ swap(t, &verifyAt, lease.VerifyReaderAt)
+ plans := fx.countPlans()
+ fx.stage(landed)
+
+ got := fx.run(context.Background(), fx.drill(), fx.routes(landed, control), nil)
+
+ v := got[landed]
+ if v.Result != dbverify.ResultUnverified || v.Reason != ReasonMoving || v.Detail != "the instance was moving when the test began" {
+ t.Fatalf("landed host: verdict %+v, want unverified/moving with the bracket-open detail", v)
+ }
+ if v.TXID != 0 || v.RestoredBytes != 0 {
+ t.Fatalf("landed host: verdict carries a restore (%+v)", v)
+ }
+ if n := plans[fx.prefix(landed)]; n != 0 {
+ t.Fatalf("landed host planned %d times, want never", n)
+ }
+ if n := fx.listedStaging(landed); n != 1 {
+ t.Fatalf("landed host's staging listed %d times, want once (at the bracket's open)", n)
+ }
+ if c := got[control]; !c.OK() {
+ t.Fatalf("control host (same orphan lineage, nothing staged): verdict %+v, want passed", c)
+ }
+ fx.scratchEmpty()
+}
+
+// A move's staged copy that arrives while the host is under test (its
+// promotion racing the restore): whatever the restore found, damaged or
+// clean, the verdict is unverified, moving. Never failed, never passed.
+func TestDrillStagedCopyArrivingDuringTheTestIsMoving(t *testing.T) {
+ const host = "a.example.com"
+ mixed := fmt.Errorf("%w: integrity_check: page 4 corrupt", lease.ErrCheckFailed)
+ for name, c := range map[string]struct {
+ duringPlan bool // the copy lands while the plan lists, not while the restore reads
+ restoreErr error // what the restore reports
+ }{
+ "mixed lineage reads as damage": {restoreErr: mixed},
+ "old lineage restores clean": {},
+ "lands while planning": {duringPlan: true},
+ "lands while planning, damage": {duringPlan: true, restoreErr: mixed},
+ } {
+ t.Run(name, func(t *testing.T) {
+ fx := newDrillFx(t, host)
+ if c.duringPlan {
+ fx.onList[fx.prefix(host)] = func() { fx.stage(host) }
+ }
+ fx.stubVerify(func(context.Context, string, string, uint64) (uint32, error) {
+ if !c.duringPlan {
+ fx.stage(host)
+ }
+ return 1, c.restoreErr
+ })
+ v := fx.run(context.Background(), fx.drill(), fx.routes(host), nil)[host]
+ if v.Result != dbverify.ResultUnverified || v.Reason != ReasonMoving {
+ t.Fatalf("verdict %+v, want unverified/moving", v)
+ }
+ if len(fx.verifies) != 1 {
+ t.Fatalf("restored %d times, want once (the copy arrived after the bracket opened)", len(fx.verifies))
+ }
+ if n := fx.listedStaging(host); n != 2 {
+ t.Fatalf("staging listed %d times, want twice (open and close)", n)
+ }
+ fx.scratchEmpty()
+ })
+ }
+}
+
+// Promoted (the first wake mirrored the staged copy over instances/<host>/
+// and dropped the staging): the drill is what it was, through the
+// production restore, and the staging check costs one bounded listing at
+// each end of the bracket.
+func TestDrillPromotedCopyIsUnchanged(t *testing.T) {
+ const host = "a.example.com"
+ fx := newDrillFx(t, host)
+ swap(t, &verifyAt, lease.VerifyReaderAt)
+ v := fx.run(context.Background(), fx.drill(), fx.routes(host), nil)[host]
+ if !v.OK() || v.TXID != fx.plan().TXID {
+ t.Fatalf("verdict %+v, want passed at txid %d", v, fx.plan().TXID)
+ }
+ if n := fx.listedStaging(host); n != 2 {
+ t.Fatalf("staging listed %d times, want twice (open and close)", n)
+ }
+ fx.scratchEmpty()
+}
+
+// The staging prefix ends in a slash, so one host's staged copy is not
+// another's whose name it begins (app.example.co is not
+// app.example.com).
+func TestDrillStagingIsPerHost(t *testing.T) {
+ const staged, other = "a.example.com", "a.example.co"
+ fx := newDrillFx(t, staged, other)
+ fx.stage(staged)
+ got := fx.run(context.Background(), fx.drill(), fx.routes(staged, other), nil)
+ if v := got[staged]; v.Reason != ReasonMoving {
+ t.Fatalf("%s: verdict %+v, want moving", staged, v)
+ }
+ if v := got[other]; !v.OK() {
+ t.Fatalf("%s: verdict %+v, want passed (another host's staging)", other, v)
+ }
+}
+
+// When the staging cannot be read, whether the instance is awaiting
+// promotion is unknown: unverified, storage_unavailable, never a pass and
+// never a failure, at either end of the bracket, and with no restore when
+// it is the open that cannot tell.
+func TestDrillStagingUnreadableIsNeverAPass(t *testing.T) {
+ const host = "a.example.com"
+ denied := errors.New("AccessDenied: list moves/")
+
+ t.Run("at the open", func(t *testing.T) {
+ fx := newDrillFx(t, host)
+ fx.stagingErr = denied
+ v := fx.run(context.Background(), fx.drill(), fx.routes(host), nil)[host]
+ if v.Result != dbverify.ResultUnverified || v.Reason != ReasonStorageUnavailable || v.Detail == "" {
+ t.Fatalf("verdict %+v, want unverified/storage_unavailable with the error", v)
+ }
+ if len(fx.verifies) != 0 {
+ t.Fatal("restored a host whose staging could not be read")
+ }
+ })
+ for name, restoreErr := range map[string]error{
+ "at the close, clean restore": nil,
+ "at the close, damaged restore": fmt.Errorf("%w: integrity_check: page 4 corrupt", lease.ErrCheckFailed),
+ } {
+ t.Run(name, func(t *testing.T) {
+ fx := newDrillFx(t, host)
+ fx.stubVerify(func(context.Context, string, string, uint64) (uint32, error) {
+ fx.mu.Lock()
+ fx.stagingErr = denied
+ fx.mu.Unlock()
+ return 1, restoreErr
+ })
+ v := fx.run(context.Background(), fx.drill(), fx.routes(host), nil)[host]
+ if v.Result != dbverify.ResultUnverified || v.Reason != ReasonStorageUnavailable {
+ t.Fatalf("verdict %+v, want unverified/storage_unavailable", v)
+ }
+ fx.scratchEmpty()
+ })
+ }
+ t.Run("the bucket cannot be opened", func(t *testing.T) {
+ fx := newDrillFx(t, host, "b.example.com")
+ swap(t, &openStaging, func(context.Context, string, string) (release.PageLister, error) {
+ return nil, errors.New("load aws config: no region")
+ })
+ got := fx.run(context.Background(), fx.drill(), fx.routes(host, "b.example.com"), nil)
+ for h, v := range got {
+ if v.Result != dbverify.ResultUnverified || v.Reason != ReasonStorageUnavailable {
+ t.Fatalf("%s: verdict %+v, want unverified/storage_unavailable", h, v)
+ }
+ }
+ if len(got) != 2 || len(fx.verifies) != 0 {
+ t.Fatalf("verdicts %d, restores %d; want 2 and none", len(got), len(fx.verifies))
+ }
+ })
+}
diff --git a/internal/ops/hibernate_verify_test.go b/internal/ops/hibernate_verify_test.go
index db1dc9d71..a0736fc92 100644
--- a/internal/ops/hibernate_verify_test.go
+++ b/internal/ops/hibernate_verify_test.go
@@ -185,11 +185,17 @@ type drillFx struct {
cfg *activator.Config
tmp string
- mu sync.Mutex
- replicaFor map[string]string // prefix -> replica dir
- onList map[string]func() // prefix -> hook run when planning lists it
- verifies []uint64 // the txids verifyAt was asked for
- localReads atomic.Int32
+ // staging is this box's data bucket as the staged-copy check reads
+ // it: a move's staged copy lands under moves/<host>/ here.
+ staging *release.DirStore
+
+ mu sync.Mutex
+ stagingErr error // when set, every staged-copy listing fails with it
+ stagingList []string // the prefixes the staged-copy check listed, in order
+ replicaFor map[string]string // prefix -> replica dir
+ onList map[string]func() // prefix -> hook run when planning lists it
+ verifies []uint64 // the txids verifyAt was asked for
+ localReads atomic.Int32
}
var (
@@ -237,6 +243,7 @@ func newDrillFx(t *testing.T, hosts ...string) *drillFx {
tmp: t.TempDir(),
replicaFor: map[string]string{},
onList: map[string]func(){},
+ staging: release.NewDirStore(t.TempDir()),
}
for _, h := range hosts {
fx.putRecord(fxAccount, h, time.Date(2026, 9, 1, 0, 0, 0, 0, time.UTC), nil)
@@ -260,6 +267,12 @@ func newDrillFx(t *testing.T, hosts ...string) *drillFx {
swap(t, &lockState, func(context.Context, string, string, string) (bool, string, time.Time, error) {
return false, "", time.Time{}, nil
})
+ swap(t, &openStaging, func(_ context.Context, bucket, region string) (release.PageLister, error) {
+ if bucket != fx.cfg.Bucket || region != fx.cfg.Region {
+ t.Errorf("staging opened in %s/%s, want %s/%s", bucket, region, fx.cfg.Bucket, fx.cfg.Region)
+ }
+ return stagingSpy{fx: fx}, nil
+ })
real := localFreshness
swap(t, &localFreshness, func(dbPath string, counter uint32) (freshness, string) {
fx.localReads.Add(1)