Skip to content

Fix: possible race condition on EXCHANGE TABLES query - #61135

Closed
yakov-olkhovskiy wants to merge 27 commits into
masterfrom
fix-exchange-tables
Closed

yakov-olkhovskiy wants to merge 27 commits into
masterfrom
fix-exchange-tables

Conversation

@yakov-olkhovskiy

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Fixed possible race condition when EXCHANGE TABLES executed in parallel.

closes #61072

@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-improvement Pull request with some product improvements label Mar 10, 2024
@robot-ch-test-poll3

robot-ch-test-poll3 commented Mar 10, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit d205601 with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR❌ failure
Cloud fork sync (only for ClickHouse Inc. employees)If it fails, ask a maintainer for help❌ failure
Integration testsThe integration tests report. In parenthesis the package type is given, and in square brackets are the optional part/total tests❌ failure
Mergeable CheckChecks if all other necessary checks are successful❌ failure
Successful checks
Check nameDescriptionStatus
ClickHouse build checkBuilds ClickHouse in various configurations for use in further steps. You have to fix the builds that fail. Build logs often has enough information to fix the error, but you might have to reproduce the failure locally. The cmake options can be found in the build log, grepping for cmake. Use these options and follow the general build process✅ success
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
Flaky testsChecks if new added or modified tests are flaky by running them repeatedly, in parallel, with more randomization. Functional tests are run 100 times with address sanitizer, and additional randomization of thread scheduling. Integration tests are run up to 10 times. If at least once a new test has failed, or was too long, this check will be red. We don't allow flaky tests, read the doc✅ success
PR CheckChecks correctness of the PR's body✅ success
Stateful testsRuns stateful functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Stateless testsRuns stateless functional tests for ClickHouse binaries built in various configurations -- release, debug, with sanitizers, etc✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success
Unit testsRuns the unit tests for different release types✅ success

Comment thread src/Interpreters/Context.h Resolved
@alexey-milovidov alexey-milovidov self-assigned this Mar 10, 2024
@alexey-milovidov

Copy link
Copy Markdown
Member

Add a test that has a chance to reproduce the problem at least occasionally.

Comment thread src/Storages/IStorage_fwd.h Outdated

using ConstStoragePtr = std::shared_ptr<const IStorage>;
using StoragePtr = std::shared_ptr<IStorage>;
using StorageWeakPtr = std::weak_ptr<IStorage>;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not allowed to use std::weak_ptr<IStorage> because we rely on std::shared_ptr<IStorage>::unique()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of saving StoragePtr, we can save StorageID with resolved UUID

@yakov-olkhovskiy yakov-olkhovskiy Mar 19, 2024 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's exactly the point to not keep shared_ptr here - since on HTTP requests session can be preserved it would block database from DROP - making it weak_ptr doesn't block it and we don't need to keep it cached beyond session so it should be safe

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a race condition

@tavplubix tavplubix self-assigned this Mar 20, 2024
Comment thread src/Interpreters/Context.cpp Outdated
return shared->filesystem_cache_user;
}

DatabaseAndTable Context::getOrCacheStorage(const StorageID & id, std::function<DatabaseAndTable()> f, std::optional<Exception> * exception) const

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could cache it in Context::resolveStorageID, but a new method is fine too

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to enforce this cache everywhere where we call to Context::resolveStorageID - at least having a query context is a condition we need to use this cache.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can check this condition in resolveStorageID

Comment thread src/Interpreters/Context.cpp Outdated
return storage;
}

auto storage = f();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider a better name

Comment thread src/Interpreters/Context.cpp Outdated
{
DatabaseAndTable storage = DatabaseCatalog::instance().tryGetByUUID(storage_id->uuid);
if (exception && !storage.second)
exception->emplace(Exception(ErrorCodes::UNKNOWN_TABLE, "Table {} does not exist anymore - maybe it was dropped", id.getNameForLogs()));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently, a query will fail if it requests a table the second time, but the table was dropped after the first get. But we can improve it later

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to improve it - if table was dropped concurrently then query is better to be interrupted than trying to reposes the table from drop queue - at this point we don't even know who is holding it from physical deletion...

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do know, see addStorageHolder

@tavplubix

Copy link
Copy Markdown
Member

@yakov-olkhovskiy, restarting CI by merging master is pointless. It will not fix the failures. For two reasons:

  • Many tests are broken in master after enabling the new analyzer. They are not fixed yet and will fail again. But even unrelated failures are worth attention.
  • Some failures are actually related to the changes in this PR. Please check all CI failures on 0eae42a and 242dc60

@tavplubix tavplubix added the do not test disable testing on pull request label Apr 3, 2024
@yakov-olkhovskiy

Copy link
Copy Markdown
Member Author

@tavplubix Mergeable check was failing because build failures like this:
https://github.com/ClickHouse/ClickHouse/actions/runs/8530457581/job/23379706220#step:9:107
I got an impression that there is something wrong with CI and restarting would help

Some failures are actually related to the changes in this PR. Please check all CI failures on 0eae42a and 242dc60

I'm not sure I see anything related... could you point it out please?

@clickhouse-gh

clickhouse-gh Bot commented Jul 30, 2024

Copy link
Copy Markdown
Contributor

Dear @tavplubix, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2024

Copy link
Copy Markdown
Contributor

Dear @tavplubix, this PR hasn't been updated for a while. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@tavplubix tavplubix self-assigned this Sep 3, 2024
@yakov-olkhovskiy

Copy link
Copy Markdown
Member Author

@tavplubix I don't like this solution involving additional locks - makes it too fragile. I'll think on maybe different solution if possible. But don't close this PR - maybe will continue here

@yakov-olkhovskiy
yakov-olkhovskiy marked this pull request as draft September 3, 2024 13:40
@al13n321

al13n321 commented Sep 6, 2024

Copy link
Copy Markdown
Contributor

Does a similar race condition exist for ALTER too? The cache in Context would need to contain a StorageSnapshotPtr/StorageMetadataPtr to fix it. And along the way it can also have a TableLockHolder to fix #67646

But also TableNode already has all of these things. I would've assumed that preventing this kind of race condition is already the purpose of TableNode. Was it not the purpose, or is something broken? Did the crash happen with analyzer enabled?

@KochetovNicolai , do you know? Tl;dr: #61072 , #67646 - is TableNode intended to prevent these race conditions by doing table lookup+locking+snapshotting at most once per query?

@yakov-olkhovskiy

Copy link
Copy Markdown
Member Author

@al13n321 I don't think so - the problem with EXCHANGE is that DatabaseCatalog will return different storages during different stages of some operation.

@clickhouse-gh

clickhouse-gh Bot commented Nov 12, 2024

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

1 similar comment
@clickhouse-gh

clickhouse-gh Bot commented Jan 14, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Mar 18, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

2 similar comments
@clickhouse-gh

clickhouse-gh Bot commented May 20, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c7fba0b]

Summary: ✅

@clickhouse-gh

clickhouse-gh Bot commented Sep 23, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

3 similar comments
@clickhouse-gh

clickhouse-gh Bot commented Nov 25, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Dec 2, 2025

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh

clickhouse-gh Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

Dear @yakov-olkhovskiy, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

alexey-milovidov and others added 2 commits May 16, 2026 17:07
# Conflicts:
#	src/Interpreters/Context.cpp
#	src/Interpreters/Context.h
#	src/Interpreters/DatabaseCatalog.cpp
…e_tables_race.sh

The C++ fix in `Context.cpp` / `Context.h` / `DatabaseCatalog.cpp` was reimplemented
with a sharded cache and merged into master via #97411
(#97411). The accompanying test was
committed under the new name `03915_exchange_tables_race.sh`, so the original
`03007_exchange_tables_race.sh` would be a duplicate.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@yakov-olkhovskiy

Copy link
Copy Markdown
Member Author

fixed in #97411

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EXCHANGE TABLES query can produce race condition

5 participants