Skip to content

fix(migrate): report cutover lock attempts on a stopped copy-and-swap - #628

Draft
Kiran01bm wants to merge 2 commits into
mainfrom
kiran01bm/cas-failure-attempts
Draft

Kiran01bm wants to merge 2 commits into
mainfrom
kiran01bm/cas-failure-attempts

Conversation

@Kiran01bm

Copy link
Copy Markdown
Collaborator

A copy-and-swap run that stops because its cutover spent every lock attempt now reports how many attempts it made in the verdict's attempts field.

Why

A copy-and-swap run builds a full copy of the table and then swaps it in, which needs a brief exclusive lock. If a reader or writer holds the table through every try, the run stops with code: budget-lock-exceeded so it can be resumed later. The verdict's attempts field exists to tell automation how many bounded tries ran, but it was only filled for native statements, so a stopped copy-and-swap run always read as zero tries.

What

  • Fill Verdict.Attempts on a failed copy-and-swap verdict from the *schemachange.LockRetriesExhaustedError the swap returns. This covers migrate.Run and RunDesired, which share the same verdict builder.
  • A live run that a reader outlasts in its wait before the cutover makes no swap attempt, so it keeps attempts at zero (omitted), matching a native step that never asked for its lock.
  • Update the field's doc comment, the budget-lock-exceeded entry in docs/schemabot-integration.md, and the JSON example in docs/cli-output-examples.md.
  • Extend the reader-outlasted cutover integration tests and the verdict-builder unit test.

Before / after

A report keeps a transaction open on public.orders while a --block-writes run with --cutover-attempts 2 reaches its cutover. Both tries wait for the reader and give up, and the run stops. Nothing else about the stop changes (code, fence, checkpoint row, lock_holder). The only difference is that the JSON verdict now says two tries ran.

pg-sprite migrate --copy-and-swap --block-writes public.orders --cutover-attempts 2 --json
  --alter 'ALTER TABLE orders ALTER COLUMN qty TYPE bigint'

  reader holds public.orders ──▶ attempt 1 waits, gives up ──▶ attempt 2 waits, gives up
                                                                     │
                                         the run stops; the swap error says "after 2 attempts"
                                                                     │
               Before                                                ▼            After
  ┌──────────────────────────────────────┐          ┌──────────────────────────────────────┐
  │ "outcome": "failed",                 │          │ "outcome": "failed",                 │
  │ "code": "budget-lock-exceeded",      │          │ "code": "budget-lock-exceeded",      │
  │ (no "attempts": reads as zero tries) │          │ "attempts": 2,                       │
  │ "writes_blocked": true,              │          │ "writes_blocked": true,              │
  │ "lock_holder": { "pid": 4123, ... }  │          │ "lock_holder": { "pid": 4123, ... }  │
  └──────────────────────────────────────┘          └──────────────────────────────────────┘

A live run (no --block-writes) that the same reader outlasts before its cutover still has no attempts field, because it never made a swap attempt.

A copy-and-swap run whose cutover spent every lock attempt left the
verdict's attempts at zero, so automation could not tell an exhausted
bounded retry from a single cancelled attempt as it can for a native step.
…-attempts

* origin/main:
  executor: add table-lock-held for a run refused by a held table lock (#590)
  fix(preflight): check the table owner's schema access for copy-and-swap (#597)

# Conflicts:
#	docs/schemabot-integration.md
#	pkg/migrate/copy_swap.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant