Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: possible race condition on EXCHANGE TABLES query #61135

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
558d764
add storage cache to context and use it during query analysis and exe…
yakov-olkhovskiy Mar 10, 2024
f8b4ef0
test added, comment fixed
yakov-olkhovskiy Mar 11, 2024
2de26fa
style check typo
yakov-olkhovskiy Mar 11, 2024
2f051c7
fix for query having no query context
yakov-olkhovskiy Mar 11, 2024
067a8c8
move to DatabaseCatalog
yakov-olkhovskiy Mar 11, 2024
2825656
fix
yakov-olkhovskiy Mar 11, 2024
064eca6
use weak_ptr
yakov-olkhovskiy Mar 12, 2024
c66d8e1
use StorageID instead of database.table
yakov-olkhovskiy Mar 12, 2024
792a298
Merge remote-tracking branch 'origin/master' into fix-exchange-tables
yakov-olkhovskiy Mar 13, 2024
b24629d
Merge remote-tracking branch 'origin/master' into fix-exchange-tables
yakov-olkhovskiy Mar 16, 2024
5955cc5
add no-ordinary-database tag
yakov-olkhovskiy Mar 20, 2024
e121665
cache storage by UUID
yakov-olkhovskiy Mar 22, 2024
190823f
cleanup
yakov-olkhovskiy Mar 22, 2024
4eeff80
Merge branch 'master' into fix-exchange-tables
yakov-olkhovskiy Mar 22, 2024
c67a898
fix
yakov-olkhovskiy Mar 22, 2024
0eae42a
better argument name
yakov-olkhovskiy Mar 30, 2024
242dc60
Merge branch 'master' into fix-exchange-tables
yakov-olkhovskiy Apr 2, 2024
57627ec
Merge branch 'master' into fix-exchange-tables
yakov-olkhovskiy Apr 3, 2024
856cb21
add mutex to storage_cache
yakov-olkhovskiy Apr 6, 2024
1bd25d0
Merge branch 'master' into fix-exchange-tables
yakov-olkhovskiy Apr 6, 2024
c84b702
fix clang tidy
yakov-olkhovskiy Apr 6, 2024
f0d55ea
Merge branch 'master' into fix-exchange-tables
yakov-olkhovskiy Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Interpreters/Context.cpp
Expand Up @@ -906,6 +906,16 @@ String Context::getFilesystemCacheUser() const
return shared->filesystem_cache_user;
}

StoragePtr Context::getOrCacheStorage(const String & name, std::function<StoragePtr()> f) const
{
if (auto storage = storage_cache.find(name); storage != storage_cache.end())
return storage->second;
auto storage = f();
Copy link
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

if (storage)
storage_cache.insert({name, storage});
return storage;
}

Strings Context::getWarnings() const
{
Strings common_warnings;
Expand Down
6 changes: 6 additions & 0 deletions src/Interpreters/Context.h
Expand Up @@ -33,6 +33,7 @@
#include <memory>
#include <mutex>
#include <optional>
#include <unordered_map>


namespace Poco::Net { class IPAddress; }
Expand Down Expand Up @@ -424,6 +425,9 @@ class ContextData
/// mutation tasks of one mutation executed against different parts of the same table.
PreparedSetsCachePtr prepared_sets_cache;

/// Cache for storages resolved during query processing.
yakov-olkhovskiy marked this conversation as resolved.
Show resolved Hide resolved
mutable std::unordered_map<String, StoragePtr> storage_cache;

public:
/// Some counters for current query execution.
/// Most of them are workarounds and should be removed in the future.
Expand Down Expand Up @@ -518,6 +522,8 @@ class Context: public ContextData, public std::enable_shared_from_this<Context>
String getFilesystemCachesPath() const;
String getFilesystemCacheUser() const;

StoragePtr getOrCacheStorage(const String & name, std::function<StoragePtr()> f) const;

/// A list of warnings about server configuration to place in `system.warnings` table.
Strings getWarnings() const;

Expand Down
4 changes: 2 additions & 2 deletions src/Interpreters/JoinedTables.cpp
Expand Up @@ -232,7 +232,7 @@ StoragePtr JoinedTables::getLeftTableStorage()
}

/// Read from table. Even without table expression (implicit SELECT ... FROM system.one).
return DatabaseCatalog::instance().getTable(table_id, context);
return context->getQueryContext()->getOrCacheStorage(table_id.getFullNameNotQuoted(), [&](){ return DatabaseCatalog::instance().getTable(table_id, context); });
}

bool JoinedTables::resolveTables()
Expand Down Expand Up @@ -318,7 +318,7 @@ std::shared_ptr<TableJoin> JoinedTables::makeTableJoin(const ASTSelectQuery & se
if (table_to_join.database_and_table_name)
{
auto joined_table_id = context->resolveStorageID(table_to_join.database_and_table_name);
StoragePtr storage = DatabaseCatalog::instance().tryGetTable(joined_table_id, context);
StoragePtr storage = context->getQueryContext()->getOrCacheStorage(joined_table_id.getFullNameNotQuoted(), [&](){ return DatabaseCatalog::instance().tryGetTable(joined_table_id, context); });
if (storage)
{
if (auto storage_join = std::dynamic_pointer_cast<StorageJoin>(storage); storage_join)
Expand Down