Repository navigation
fix(storage): concurrent first opens of a new database all succeed (v0.4.0 RC) - #31
Merged
Merged
Conversation
Two daemons starting together on a new data directory could fail to open
the shared database, and the loser ran memory-only. Reproduced with 8
concurrent openers: 18-25 failures per 400 rounds. This is the cause of the
intermittent macOS CI failures in concurrent_first_open_applies_the_migration_once
and events_committed_by_another_daemon_stream_live_and_resume_after_restart.
Two causes:
- The identity check read page_count, application_id, user_version and the
table count in separate snapshots. An opener could see another process's
unstamped page 1 and then its new tables, and refuse TailFlow's own
database as ForeignDatabase. The four values are now read in one
statement, so one snapshot: tables are only ever visible together with
the application id stamp.
- PRAGMA journal_mode = WAL needs an exclusive lock to leave rollback mode
and returns SQLITE_BUSY at once, without calling the busy handler, while
another connection holds the file ("database is locked"). It is now
retried on BUSY/LOCKED with a short backoff for up to BUSY_TIMEOUT.
The regression test now runs 60 rounds of 8 openers released together by a
barrier and checks the final schema version and journal mode. It fails
without the WAL retry. With the fix: 0 failures in 3 x 400 rounds.
The NewerSchema check ran on the identity snapshot only. If a newer build migrated the same new file past this build's schema between that check and migrate(), this build returned Ok and wrote into the newer database, breaking the rule that newer databases are never modified. migrate() and each migration's write transaction now refuse a user_version above the newest migration this build knows, without writing. Found in the v0.4.0 release-candidate review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Found during the v0.4.0 release gate. The first CI attempt on
devfailed on macOS for both 2044163 and b4cf1e2 (they went green on rerun).concurrent_first_open_applies_the_migration_oncefailed withForeignDatabase { application_id: 0 }.events_committed_by_another_daemon_stream_live_and_resume_after_restartfailed with "stream open". Two daemons start on one new data dir, and the one that loses the race runs memory-only.Root cause (reproduced: 8 concurrent openers, 18–25 failures per 400 rounds)
page_count,application_id,user_versionand the table count were read in four separate statements. An opener could see another process's unstamped page 1 (written byPRAGMA auto_vacuum) and then its new tables, and refuse TailFlow's own file as foreign. They are now read in one statement, so one snapshot. Within a snapshot, tables only ever appear together with the stamp.journal_mode = WALignores the busy timeout. Leaving rollback mode needs an exclusive lock. While another connection holds the file, SQLite returnsSQLITE_BUSYat once without calling the busy handler. It is now retried on BUSY/LOCKED with a 1→50 ms backoff, for up toBUSY_TIMEOUT.migrate(), this build wrote into a newer database.migrate()and each migration transaction now re-check, and returnNewerSchemawithout writing.Evidence
concurrent_first_open_applies_the_migration_oncenow runs 60 rounds of 8 openers released together by a barrier, and checks the final version and journal mode.ForeignDatabase.a_schema_that_becomes_newer_before_migration_is_refusedfails.cargo fmt --check,cargo +stable clippy --all-targets --all-features -D warnings, andcargo test --workspace --locked(501 passed, 0 failed, before the third commit).tailflow-storage: all pass after it.No schema or file-format change; still v4.