Repository navigation
Cherry pick #118684 to 26.3: Load a Filesystem database whose directory is gone instead of refusing to start - #119688
Merged
Merged
Conversation
…g to start
`DatabaseFilesystem`'s constructor validated the existence of its backing
directory unconditionally. Server startup rebuilds every database by replaying
its stored `ATTACH DATABASE` statement (`loadDatabase` -> `executeCreateQuery`
-> `InterpreterCreateQuery` -> `DatabaseFactory::get` -> that constructor),
`loadMetadata` aborts on the first exception, and nothing catches around the
factory call. A directory removed after the database was created therefore made
the server refuse to start, recoverable only by hand-editing `metadata/`, with
no SQL escape hatch because the server never accepts a connection.
Skip the existence check only for an internal metadata replay,
`args.internal && args.mode >= LoadingStrictnessLevel::ATTACH`. This is the
predicate `DatabaseDataLake`, `DatabasePostgreSQL` and
`DatabaseMaterializedPostgreSQL` already use for the same requirement.
`isLoadingFromExistingMetadata` is the trap here: it matches only
`FORCE_ATTACH`/`FORCE_RESTORE`, and a database replay uses plain `ATTACH`
because `loadDatabase` passes `create = true`, so `setForceAttach(true)` is
never reached. A regression arm covers that distinction, and building the fix
with the narrower helper leaves the original failure in place.
Deferring the check into `beforeLoadingMetadata` (as `DatabaseBackup` and
`DatabaseReplicated` do) is unavailable: `supportsLoadingInTopologicalOrder()`
is false for this engine, so `TablesLoader` takes the `loadStoredObjects`
branch and never calls it. Deleting the check outright was rejected because it
would silently accept `Filesystem('/nonexistent')` from a user and break
`02722_database_filesystem`.
A tolerated database exposes no tables, since every table resolves its file on
access, and it can still be inspected and dropped. `DatabaseFilesystem` was the
only path-backed database engine that hard-failed on an unreachable location:
`DatabaseS3` does no validation, `DatabaseHDFS` checks URL syntax only, and
`DatabaseURL` checks only that a scheme is present. The `user_files`
containment check is deliberately left fail-closed on replay, because it is a
security boundary and `checkTableFilePath` re-checks containment per table
regardless of `throw_on_error`.
The failure was first observed as `Cannot start clickhouse-server` in a
`Stress test (arm_tsan)` job, where a SIGKILL landing between a test's
`DROP DATABASE` and its `rm -rf` of the directory leaves the metadata file
behind. No test is changed to accommodate this; the removal is required for
that test's re-runnability.
Co-Authored-By: Claude Opus 5 <[email protected]>
…the comment `05136_database_filesystem_missing_path_on_load` sent the second `clickhouse local` invocation's stderr to `/dev/null`, which discarded the only text that identifies the defect: without it a base-build failure is a bare missing-lines stdout diff, on the sole regression test for a "server cannot start" fix. The redirect suppressed nothing that a green run emits. A bare `clickhouse local` sets `logger.level` to `fatal` unless one of `logger.console`, `logger.level`, `log-level` or `logger.log` is given, so the tolerated path's `LOG_WARNING` never reaches stderr; `shell_config.sh` builds `--send_logs_level` into `CLICKHOUSE_CLIENT` only, never into `CLICKHOUSE_LOCAL`; and the first invocation in the same test already ran unredirected. Measured after removing it: stderr is empty on the fix build and 50 randomized runs stay green, while the base build now reports `Code: 36 ... Path does not exist: ... while loading database ... from file metadata/fsdb.sql. (BAD_ARGUMENTS)`. Also reword the second sentence of the `is_internal_metadata_replay` comment. It justified the exclusion by who supplies the path, which is false for the short `ATTACH DATABASE db` form: that reads `metadata/<db>.sql`, replays the stored definition and supplies no path of its own, yet still throws, because `args.internal` is false. Name the discriminator that actually decides it. `RESTORE` is internal too and is excluded by the mode term instead, so the wording claims only what the guard does. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118377&sha=58f7c10d1c3a06e4661e9b38aa3d45dc6b268a96&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29 #118377 Co-Authored-By: Claude Opus 5 <[email protected]>
…ssing path
The previous commit's guard was `args.internal && args.mode >= ATTACH`, on the
assumption that `internal` means the server generated the statement. It does
not. `InterpreterParallelWithQuery::executeSubquery` runs every `PARALLEL WITH`
child through `executeQuery(..., QueryFlags{ .internal = true })`, and
`InterpreterExecuteAsQuery::execute` does the same for `EXECUTE AS`, so a
statement a user writes arrives at `DatabaseFactory` with `internal` set.
`PARALLEL WITH` is gated by no setting and DDL children are its canonical use.
Measured on a server: `ATTACH DATABASE d ENGINE = Filesystem('<missing>')` is
rejected with `Code: 36`, while the same statement followed by
`PARALLEL WITH DROP TABLE IF EXISTS <absent>` was accepted, wrote
`metadata/d.sql`, and survived a restart. Both plain forms already threw, so
only the wrapped form reached the tolerant branch.
Add a third conjunct, `args.replays_stored_definition`, computed by the
interpreter one line above the factory call as `create.attach &&
!need_write_metadata`. That expression already means "the definition being
attached is the one stored in `metadata/`", so this reuses the interpreter's own
answer instead of inventing a second notion of replay. A definition supplied
with the query is never a stored one, wrapper or not. The change moves in one
direction only: strictly fewer statements reach the tolerant branch, and a
genuine startup replay still evaluates all three terms true, which
`05136_database_filesystem_missing_path_on_load` continues to assert.
`02921_database_filesystem_path_check` gains the wrapped `ATTACH` arm. Its
sibling is `DROP TABLE IF EXISTS`, whose pipeline is empty, so the assertion
does not depend on the order the children run in or on what `max_threads`
randomizes to. Dropping the new conjunct while keeping the plumbing reddens
that arm with `The query succeeded but the server error '36' was expected`.
The sibling engines that share the two-term predicate are left alone: the same
laundering reaches them, but that belongs with the wrapper rather than with each
engine.
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118377&sha=58f7c10d1c3a06e4661e9b38aa3d45dc6b268a96&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29
#118377
Co-Authored-By: Claude Opus 5 <[email protected]>
The previous commit's third conjunct was `create.attach && !need_write_metadata`,
which it described as "the definition being attached is the one stored in
`metadata/`". It does not test that. `need_write_metadata` is
`!create.attach || !existsFile(metadata/<name>.sql)`, so the conjunct only tests
whether the NAME already has a stored definition; whether the definition being
executed came from that file is never checked. The file is read solely in the
short-syntax branch (`!create.storage && create.attach`), which a long-syntax
`ATTACH DATABASE ... ENGINE = ...` skips. `DETACH DATABASE` leaves the metadata
file in place (`DatabaseCatalog` removes it only when dropping, and
`DatabaseFilesystem::shouldBeEmptyOnDetach()` is false), so two ordinary
statements arrange the precondition.
Measured on a server, twice, with `buildId()` asserted against the binary:
CREATE DATABASE d;
DETACH DATABASE d;
ATTACH DATABASE d ENGINE = Filesystem('<missing>')
PARALLEL WITH DROP TABLE IF EXISTS <absent>;
was accepted, and `system.databases` then reported `d` with engine `Filesystem`
over a path that does not exist, while `metadata/d.sql` still held the original
`ENGINE = Atomic` definition. The same shape via short syntax, after removing
the directory of a real `Filesystem` database, was likewise accepted. Both plain
forms threw `Code: 36`, so `internal`, laundered by the wrapper, was again the
term being defeated.
Feed the decision from the loader instead of deriving it from the statement.
`InterpreterCreateQuery` gains `setIsMetadataReplay`, called only from
`executeCreateQuery` in `loadMetadata.cpp`, beside the `setInternal(true)` that
startup metadata replay already performs. Nothing else can reach it: `QueryFlags`
has no route to an interpreter member, so no SQL sets it. A dedicated flag rather
than the adjacent `setLoadDatabaseWithoutTables` because that one means "defer
table loading", not "this is a replay".
With the loader answering directly, `args.internal` leaves the predicate: the
same call site sets both, so it decided nothing. `args.mode >= ATTACH` is kept as
the idiom the sibling engines use; it is not independently provable by mutation
here, because a replayed `Filesystem` definition is always an `ATTACH DATABASE`
statement and therefore always that mode.
`isLoadingFromExistingMetadata` remains unusable for this: a database is replayed
under plain `ATTACH`, unlike tables, which use `FORCE_ATTACH`, so the helper never
matches a database load.
`02921_database_filesystem_path_check` gains the name-aliasing arm in pure SQL,
and re-attaches before dropping, since a database left detached keeps a metadata
file that every later start replays. `05136_database_filesystem_missing_path_on_load`
gains the sharper arm: the definition a start has just replayed is refused to a
user who replays it by hand, plainly and wrapped, and the start after each
rejection attaches the database again. Restoring the previous predicate reddens
both arms, the wrapped `05136` line flipping while its plain line stays green,
which is exactly the statement class that was reachable before.
The sibling engines that share the two-term predicate are still left alone: the
laundering reaches them too, and belongs with the wrapper rather than with each
engine.
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=118377&sha=58f7c10d1c3a06e4661e9b38aa3d45dc6b268a96&name_0=PR&name_1=Stress%20test%20%28arm_tsan%29
#118377
Co-Authored-By: Claude Opus 5 <[email protected]>
…-tolerate-missing-path-on-load Load a `Filesystem` database whose directory is gone instead of refusing to start
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.
Original pull-request #118684
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job