ops: only damage fails; a vanished planned object replans (#898)
Final fix wave items 4 and 5 (Fable P2-5, Astra P2-3). - failed/restore_error only for a copy that restored and failed its check (lease.ErrCheckFailed) or replica bytes that would not decode. - Planning and listing errors, and restore errors from the storage (AWS API/operation, network, URL) or this box's disk (path, link, syscall, errno: ENOSPC, EACCES) are unverified/storage_unavailable. - A planned object gone before its header or contents are read (os.ErrNotExist, the S3 client's missing key) takes the same bounded replan as ErrTxNotAvailable; still missing on the second plan is unverified/plan_moved. - Tests through the real planner (dbdoor.PlanFrom) and the real downloader and restore (lease.VerifyClientAt): header gone once or for good, contents gone once or for good; and a table of damage versus storage/disk errors. Both fail against the previous drill.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2 files changed,
+206
−9
internal/ops/hibernate_verify.go+62 −9internal/ops/hibernate_verify_restore_test.go+144 −0
diff --git a/internal/ops/hibernate_verify.go b/internal/ops/hibernate_verify.go| index b8b911a7d..1a3b55498 100644 |
| --- a/internal/ops/hibernate_verify.go |
| +++ b/internal/ops/hibernate_verify.go |
| @@ -21,12 +21,17 @@ import ( |
| "context" |
| "errors" |
| "fmt" |
| + "io/fs" |
| + "net" |
| + "net/url" |
| "os" |
| "slices" |
| "sort" |
| "sync" |
| + "syscall" |
| "time" |
| + "github.com/aws/smithy-go" |
| "github.com/benbjohnson/litestream" |
| "github.com/carlosframework/platform/internal/activator" |
| @@ -101,12 +106,18 @@ const ( |
| ReasonRecordUnreadable = "record_unreadable" |
| // ReasonNothingReplicated: the replica holds nothing to restore yet. |
| ReasonNothingReplicated = "nothing_replicated" |
| - // ReasonPlanMoved: the planned position was compacted away before it |
| - // could be restored, twice running. |
| + // ReasonPlanMoved: the planned position, or an object it needed, was |
| + // compacted away before it could be read, twice running. |
| ReasonPlanMoved = "plan_moved" |
| - // ReasonRestoreError: the replica would not plan, restore or pass its |
| - // integrity check (a failed verdict). |
| + // ReasonRestoreError: the replica restored into a copy that failed its |
| + // integrity check, or its bytes would not decode into a database (a |
| + // failed verdict). Only damage is failed. |
| ReasonRestoreError = "restore_error" |
| + // ReasonStorageUnavailable: listing or planning failed, or the restore |
| + // failed on the storage (an API, network or credential error) or on |
| + // this box's own disk (out of space, permissions), which says nothing |
| + // about the copy. |
| + ReasonStorageUnavailable = "storage_unavailable" |
| // ReasonStale: on the box holding the lease, the replica restores but |
| // has stopped tracking the live database (a failed verdict). |
| ReasonStale = "stale" |
| @@ -313,6 +324,7 @@ const ( |
| restoreNothing |
| restorePlanMoved |
| restoreFailed |
| + restoreUnavailable |
| ) |
| type restoreResult struct { |
| @@ -400,6 +412,9 @@ func (d Drill) drillOne(ctx context.Context, rt HibernatingRoute, now func() tim |
| case restoreFailed: |
| v.Result, v.Reason, v.Detail = dbverify.ResultFailed, ReasonRestoreError, res.err.Error() |
| return v, true |
| + case restoreUnavailable: |
| + v.Result, v.Reason, v.Detail = dbverify.ResultUnverified, ReasonStorageUnavailable, res.err.Error() |
| + return v, true |
| } |
| v.TXID, v.WrittenAt, v.RestoredBytes = res.plan.TXID, res.plan.At, res.plan.RestoredBytes |
| v.Freshness = fresh |
| @@ -420,9 +435,17 @@ func bracketReason(code string) string { |
| } |
| // restore plans the newest copy and restores exactly the planned position. |
| -// Compaction between the two can make that position unrestorable; that is |
| -// not a failure, so it plans once more, and a second miss is |
| -// restorePlanMoved. |
| +// Compaction between the two can make that position unrestorable, or |
| +// remove an object the plan named before its header or contents are read |
| +// (os.ErrNotExist, as the S3 client reports a missing key); that is not a |
| +// failure, so it plans once more, and a second miss is restorePlanMoved. |
| +// |
| +// Only damage is restoreFailed: a copy that restored and failed its check |
| +// (lease.ErrCheckFailed), or a restore error that is neither the storage's |
| +// nor this box's disk's, which leaves the replica's bytes not decoding. A |
| +// planning error, and a restore error from the storage or the local disk |
| +// (storageError), is restoreUnavailable. An error once ctx has ended is |
| +// the run's, and the caller abandons the host. |
| func (d Drill) restore(ctx context.Context, prefix, dir string) restoreResult { |
| var last restoreResult |
| for attempt := 0; attempt < 2; attempt++ { |
| @@ -430,24 +453,54 @@ func (d Drill) restore(ctx context.Context, prefix, dir string) restoreResult { |
| switch { |
| case errors.Is(err, dbdoor.ErrNothingReplicated): |
| return restoreResult{kind: restoreNothing, err: err} |
| + case err != nil && ctx.Err() == nil && errors.Is(err, os.ErrNotExist): |
| + last = restoreResult{kind: restorePlanMoved, err: fmt.Errorf("plan: %w", err)} |
| + continue |
| case err != nil: |
| - return restoreResult{kind: restoreFailed, err: fmt.Errorf("plan: %w", err)} |
| + return restoreResult{kind: restoreUnavailable, err: fmt.Errorf("plan: %w", err)} |
| } |
| counter, err := verifyAt(ctx, d.Cfg.Bucket, prefix, d.Cfg.Region, dir, plan.TXID) |
| switch { |
| case err == nil: |
| return restoreResult{kind: restoreClean, plan: plan, counter: counter} |
| - case errors.Is(err, litestream.ErrTxNotAvailable) && ctx.Err() == nil: |
| + case ctx.Err() != nil: |
| + return restoreResult{kind: restoreUnavailable, plan: plan, err: err} |
| + case errors.Is(err, lease.ErrCheckFailed): |
| + return restoreResult{kind: restoreFailed, plan: plan, err: err} |
| + case errors.Is(err, litestream.ErrTxNotAvailable), errors.Is(err, os.ErrNotExist): |
| last = restoreResult{kind: restorePlanMoved, plan: plan, err: err} |
| continue |
| case errors.Is(err, lease.ErrNoReplica): |
| return restoreResult{kind: restoreNothing, err: err} |
| + case storageError(err): |
| + return restoreResult{kind: restoreUnavailable, plan: plan, err: err} |
| } |
| return restoreResult{kind: restoreFailed, plan: plan, err: err} |
| } |
| return last |
| } |
| +// storageError reports whether err came from reaching the copy's storage |
| +// (an AWS API or operation error, a network or URL error) or from this |
| +// box's own system calls (a path, link or syscall error, an errno: out of |
| +// space, permissions), rather than from the copy's bytes. litestream wraps |
| +// the storage's errors with %w, and lease's verify restore adds the last |
| +// error the replica gave a read where litestream drops it. |
| +func storageError(err error) bool { |
| + var ( |
| + api smithy.APIError |
| + op *smithy.OperationError |
| + nerr net.Error |
| + uerr *url.Error |
| + perr *fs.PathError |
| + lerr *os.LinkError |
| + serr *os.SyscallError |
| + errno syscall.Errno |
| + ) |
| + return errors.As(err, &api) || errors.As(err, &op) || errors.As(err, &nerr) || errors.As(err, &uerr) || |
| + errors.As(err, &perr) || errors.As(err, &lerr) || errors.As(err, &serr) || errors.As(err, &errno) |
| +} |
| + |
| func (d Drill) plan(ctx context.Context, prefix string) (dbdoor.RestorePlan, error) { |
| rd, err := openReplica(ctx, d.Cfg.Bucket, prefix, d.Cfg.Region) |
| if err != nil { |
diff --git a/internal/ops/hibernate_verify_restore_test.go b/internal/ops/hibernate_verify_restore_test.go| new file mode 100644 |
| index 000000000..b6b9b304f |
| --- /dev/null |
| +++ b/internal/ops/hibernate_verify_restore_test.go |
| @@ -0,0 +1,144 @@ |
| +package ops |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "fmt" |
| + "io" |
| + "io/fs" |
| + "net" |
| + "os" |
| + "sync" |
| + "syscall" |
| + "testing" |
| + |
| + "github.com/aws/smithy-go" |
| + lsfile "github.com/benbjohnson/litestream/file" |
| + "github.com/superfly/ltx" |
| + |
| + "github.com/carlosframework/platform/internal/dbdoor" |
| + "github.com/carlosframework/platform/internal/dbverify" |
| + "github.com/carlosframework/platform/internal/lease" |
| +) |
| + |
| +// Only damage is failed (Fable P2-5): a copy that restored and failed its |
| +// check, or replica bytes that would not decode. Anything the storage or |
| +// this box's own disk did is unverified, storage_unavailable, because it |
| +// says nothing about the copy, and a failed verdict pages as an incident. |
| +func TestVerifyHibernatingOnlyDamageFails(t *testing.T) { |
| + s3err := &smithy.OperationError{ServiceID: "S3", OperationName: "GetObject", |
| + Err: &smithy.GenericAPIError{Code: "SlowDown", Message: "reduce your request rate"}} |
| + for name, c := range map[string]struct { |
| + err error |
| + result string |
| + reason string |
| + }{ |
| + "check failed": {fmt.Errorf("%w: integrity_check: page 4 corrupt", lease.ErrCheckFailed), |
| + dbverify.ResultFailed, ReasonRestoreError}, |
| + "bytes would not decode": {errors.New("restore: decode database: unexpected pgno while decoding page: read 7, expected 6"), |
| + dbverify.ResultFailed, ReasonRestoreError}, |
| + "truncated object": {fmt.Errorf("restore: decode database: decode page 3: %w", io.ErrUnexpectedEOF), |
| + dbverify.ResultFailed, ReasonRestoreError}, |
| + "storage refused": {fmt.Errorf("restore: cannot calc restore plan: %w", s3err), |
| + dbverify.ResultUnverified, ReasonStorageUnavailable}, |
| + "connection reset": {fmt.Errorf("restore: decode database: decode header: EOF (the replica's last read error: %w)", |
| + &net.OpError{Op: "read", Net: "tcp", Err: syscall.ECONNRESET}), |
| + dbverify.ResultUnverified, ReasonStorageUnavailable}, |
| + "disk full": {fmt.Errorf("restore: decode database: write page 9: %w", |
| + &fs.PathError{Op: "write", Path: "/var/cache/carlos-verify/x/verify.db.tmp", Err: syscall.ENOSPC}), |
| + dbverify.ResultUnverified, ReasonStorageUnavailable}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + fx := newDrillFx(t, "a.example.com") |
| + fx.stubVerify(func(context.Context, string, string, uint64) (uint32, error) { return 0, c.err }) |
| + v := fx.run(context.Background(), fx.drill(), fx.routes("a.example.com"), nil)["a.example.com"] |
| + if v.Result != c.result || v.Reason != c.reason { |
| + t.Fatalf("verdict %s/%s, want %s/%s (%+v)", v.Result, v.Reason, c.result, c.reason, v) |
| + } |
| + if v.Detail == "" { |
| + t.Fatal("the verdict does not carry the error") |
| + } |
| + fx.scratchEmpty() |
| + }) |
| + } |
| + // A listing or planning error is the storage's, never the copy's. |
| + t.Run("planning", func(t *testing.T) { |
| + fx := newDrillFx(t, "a.example.com") |
| + swap(t, &openReplica, func(context.Context, string, string, string) (dbdoor.ReplicaReader, error) { |
| + return nil, fmt.Errorf("list: %w", s3err) |
| + }) |
| + v := fx.run(context.Background(), fx.drill(), fx.routes("a.example.com"), nil)["a.example.com"] |
| + if !v.Unverified() || v.Reason != ReasonStorageUnavailable { |
| + t.Fatalf("verdict %+v, want unverified/storage_unavailable", v) |
| + } |
| + }) |
| +} |
| + |
| +// missingClient is a file replica client whose LTX objects answer |
| +// os.ErrNotExist (what the S3 client returns for a missing key) while gone |
| +// says so: an object a listing named that compaction has since removed. |
| +type missingClient struct { |
| + *lsfile.ReplicaClient |
| + mu sync.Mutex |
| + gone func(level int, minTXID, maxTXID ltx.TXID, n int) bool |
| + opens int |
| +} |
| + |
| +func (c *missingClient) OpenLTXFile(ctx context.Context, level int, minTXID, maxTXID ltx.TXID, offset, size int64) (io.ReadCloser, error) { |
| + c.mu.Lock() |
| + c.opens++ |
| + gone := c.gone(level, minTXID, maxTXID, c.opens) |
| + c.mu.Unlock() |
| + if gone { |
| + return nil, os.ErrNotExist |
| + } |
| + return c.ReplicaClient.OpenLTXFile(ctx, level, minTXID, maxTXID, offset, size) |
| +} |
| + |
| +// A planned object that disappears before its header or contents are read |
| +// goes through the same bounded replan as an unreachable transaction |
| +// (Astra P2-3), through the real planner (dbdoor.PlanFrom) and the real |
| +// downloader and restore (lease.VerifyClientAt): a replan that finds the |
| +// history whole passes, and history still missing on the second plan is |
| +// unverified, plan_moved, never a failure. |
| +func TestVerifyHibernatingReplansAMissingObject(t *testing.T) { |
| + const host = "a.example.com" |
| + for name, c := range map[string]struct { |
| + // gone decides, per open, whether the object is missing. The |
| + // replica's files are 1-1, 2-2 and 3-3; the planner reads only the |
| + // last one's header, the restore reads all three. |
| + gone func(level int, minTXID, maxTXID ltx.TXID, n int) bool |
| + result string |
| + reason string |
| + }{ |
| + "header gone once": {func(_ int, _, max ltx.TXID, n int) bool { return max == 3 && n == 1 }, |
| + dbverify.ResultPassed, ""}, |
| + "contents gone once": {func(_ int, min, _ ltx.TXID, n int) bool { return min == 1 && n <= 2 }, |
| + dbverify.ResultPassed, ""}, |
| + "header gone for good": {func(_ int, _, max ltx.TXID, _ int) bool { return max == 3 }, |
| + dbverify.ResultUnverified, ReasonPlanMoved}, |
| + "contents gone for good": {func(_ int, min, _ ltx.TXID, _ int) bool { return min == 1 }, |
| + dbverify.ResultUnverified, ReasonPlanMoved}, |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + fx := newDrillFx(t, host) |
| + client := &missingClient{ReplicaClient: lsfile.NewReplicaClient(fx.replica), gone: c.gone} |
| + plans := 0 |
| + swap(t, &openReplica, func(context.Context, string, string, string) (dbdoor.ReplicaReader, error) { |
| + plans++ |
| + return client, nil |
| + }) |
| + swap(t, &verifyAt, func(ctx context.Context, _, prefix, _, dir string, txid uint64) (uint32, error) { |
| + return lease.VerifyClientAt(ctx, client, prefix, dir, txid) |
| + }) |
| + v := fx.run(context.Background(), fx.drill(), fx.routes(host), nil)[host] |
| + if v.Result != c.result || v.Reason != c.reason { |
| + t.Fatalf("verdict %s/%s, want %s/%s (%+v)", v.Result, v.Reason, c.result, c.reason, v) |
| + } |
| + if plans != 2 { |
| + t.Fatalf("planned %d times, want once and one replan", plans) |
| + } |
| + fx.scratchEmpty() |
| + }) |
| + } |
| +} |