| 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)) |
| + } |
| + }) |
| +} |