Skip to content

fix(storage): concurrent migrate() runs converge instead of throwing (#309) - #310

Merged
Madraka merged 2 commits into
mainfrom
claude/migrate-race-309
Nov 28, 2026
Merged

Madraka merged 2 commits into
mainfrom
claude/migrate-race-309

Conversation

@Madraka

@Madraka Madraka commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Closes #309

What

Instances of a fresh installation that start together on one store all read _migrations before any of them applies a migration. The loser's batch fails (a duplicate column, or the bookkeeping row's primary key), rolls back as a whole and was thrown, even though the migration had just been applied. That made the first request on the losing instance fail with StorageError: migration "0004_audit_actor_name" failed (run 46188727714).

  • packages/storage/src/migrate.ts: when a migration's batch() fails, migrate() reads that migration's _migrations row again.
    • If a peer applied it, the run moves on (the name is not in applied).
    • Otherwise it throws as before.
    • At most once still rests on the primary-key row inside the same atomic batch. There is no interactive transaction and no lock (hard rule 4).
  • packages/cloudflare/src/admin-worker.ts: the retry-once wrappers in ensureMigrated and SchedulerClock are removed. SiteGraphStore.init() and the other callers get the fix through migrate().

Tests

  • packages/storage/test/migrate-concurrent.test.ts (better-sqlite3 and Miniflare D1). The runs are gated so they all read the same empty _migrations before any batch, the worst case.
    • Three runs over the real migrations all finish, each migration is applied by exactly one run, and a later start applies nothing.
    • Four runs over migrations that can't be run twice (CREATE TABLE, INSERT, ALTER TABLE ADD COLUMN) leave exactly one row, so nothing was re-run.
    • A migration that fails on its own still throws in every run, with the earlier ones applied.
    • The three better-sqlite3 tests fail without the fix and pass with it. That was checked locally; the D1 half runs in CI.
  • Cluster smoke tests:
    • scripts/swarm-smoke.sh fails if either instance's log contains a migration error.
    • scripts/helm-smoke.sh does the same with two instances and again after scaling to three (the StatefulSet uses podManagementPolicy: Parallel).
    • Both run in this PR's Docker workflow.

Changeset: @cloudhal/storage, @cloudhal/cloudflare (patch). Size baseline updated (createAdminWorker shrank).

🤖 Generated with Claude Code

Madraka and others added 2 commits November 29, 2026 00:24
…309)

Instances of a fresh installation that start together on one store all
read `_migrations` before any of them applies a migration. The loser's
batch fails (duplicate column, or the bookkeeping row's primary key),
rolls back as a whole and was thrown, although the migration was now
applied: the first request on that instance failed with
`StorageError: migration "0004_audit_actor_name" failed`.

When a migration's batch fails, migrate() now reads its `_migrations`
row again. If a peer applied it, the run moves on (the name is not in
`applied`); otherwise it throws as before. At most once still rests on
the primary-key row inside the same atomic batch: no interactive
transaction, no lock (hard rule 4).

- test/migrate-concurrent.test.ts: three and four runs gated to read the
  same empty state converge, on better-sqlite3 and Miniflare D1; each
  migration applies once (a non-idempotent INSERT is not doubled); a
  migration that fails on its own still throws.
- admin-worker: the retry-once wrappers in ensureMigrated and
  SchedulerClock are gone.
- helm-smoke.sh: fails when any instance logs a migration error, with
  two instances and after scaling to three (Parallel pod management).

Closes #309

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
…n error (#309)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com>
@Madraka
Madraka merged commit a493f74 into main Nov 28, 2026
12 checks passed
@Madraka
Madraka deleted the claude/migrate-race-309 branch November 28, 2026 21:42
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.

Self-host cluster: instances starting together race on migrations ("duplicate column name", uncaught)

1 participant