Repository navigation
Description
What happened
In the Swarm smoke test of #308 (run 46188727714, the Build edge job's "Swarm smoke test" step), the two cloudhal instances of a fresh installation start together on one store. One of them logs:
chsmoke6430_cloudhal.1 | workerd/server/server/listen/mod.rs:393: error: Uncaught exception: workerd/jsg/value.h:1456: failed: remote.jsg.Error: StorageError: migration "0004_audit_actor_name" failed: batch failed: D1_ERROR: duplicate column name: actor_name: SQLITE_ERROR
The other instance applied 0004 first. The request that hit the loser failed with an uncaught exception. The installation then came up, because later requests see the migrations as done, so the smoke test was not blocked by this. But a first request can fail on any fresh multi-instance start, and the outcome depends on timing.
Why (code read on main)
packages/storage/src/migrate.ts,migrate():- It reads the applied set from
_migrationsonce. - Then it applies every pending migration as one atomic
batch()(its statements plusINSERT INTO _migrations (name, …), whosenameis the primary key). On any batch error it throwsStorageError: migration "<name>" failed. - Two instances that read the set at the same time both try the same migration. The winner commits. The loser's batch fails, rolls back as a whole, and is thrown, although the migration is now applied.
- It reads the applied set from
packages/cloudflare/src/admin-worker.ts:ensureMigrated(the admin Worker's per-isolate gate) andSchedulerClockcatch once and callmigrate()a second time ("a concurrent request or isolate applied them first").- With more than one migration still pending, the second pass can lose the next race (0005 after 0004) and throw. That is the uncaught exception above.
packages/core-ts/src/store.ts,SiteGraphStore.init(): it callsmigrate()with no retry at all.
Nothing is applied twice today. The primary-key row in the same atomic batch guarantees at most once. The bug is that losing the race surfaces as a failure.
Done when
- N instances converge: N instances (or N concurrent
migrate()calls) starting together on one empty store all finish with the same schema and_migrations. None throws, nothing crashes, and there is no restart loop. - Applied migrations are detected: a migration already applied, by an earlier run or by a peer during this one, is detected and not re-run. When a migration's batch fails,
migrate()re-reads_migrations. If the name is there, a peer applied it, and it moves on. Otherwise it throws as today. - Only
batch(): no interactive transactions (hard rule 4). At most once still rests on the_migrationsprimary key inside the atomic batch. - Tests:
- The storage conformance suite (better-sqlite3 and Miniflare D1) runs several
migrate()calls concurrently on one fresh database and checks they converge. - A test shows an already-applied migration is never re-run.
- CI's cluster smoke (the two-instance Swarm and Helm smoke tests) logs no migration error.
- The storage conformance suite (better-sqlite3 and Miniflare D1) runs several
- Simplified callers: the retry-once wrappers in
admin-worker.tsare no longer needed.
Found in: #308 (#306's fix), run 46188727714.
🤖 Generated with Claude Code
Activity
Merge pull request #310 from cloudhal-dev/claude/migrate-race-309