From 5cd7b584993bf412aac758ea4d81096a846b5853 Mon Sep 17 00:00:00 2001 From: Vitaly Baranov Date: Tue, 31 Mar 2020 17:45:46 +0300 Subject: [PATCH] Fix reusing connection after changing user's default settings via ALTER USER or ALTER SETTINGS PROFILE. This fixes integration test "test_settings_constraints_distributed". --- dbms/programs/server/TCPHandler.cpp | 4 ++++ dbms/src/Access/ContextAccess.cpp | 4 ++-- dbms/src/Interpreters/Context.cpp | 17 +++++++++++++++++ dbms/src/Interpreters/Context.h | 4 ++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/dbms/programs/server/TCPHandler.cpp b/dbms/programs/server/TCPHandler.cpp index d82c6e31528..725aa8453b3 100644 --- a/dbms/programs/server/TCPHandler.cpp +++ b/dbms/programs/server/TCPHandler.cpp @@ -147,6 +147,10 @@ void TCPHandler::runImpl() if (server.isCancelled() || in->eof()) break; + /// receiveHello() has set the default settings for the current user, + /// but this default itself could change while we were waiting for a packet from the client. + connection_context.resetSettingsToDefault(); + /// Set context of request. query_context = connection_context; diff --git a/dbms/src/Access/ContextAccess.cpp b/dbms/src/Access/ContextAccess.cpp index 628e56d8542..0c370a6926c 100644 --- a/dbms/src/Access/ContextAccess.cpp +++ b/dbms/src/Access/ContextAccess.cpp @@ -540,14 +540,14 @@ std::shared_ptr ContextAccess::getFullAccess() std::shared_ptr ContextAccess::getDefaultSettings() const { std::lock_guard lock{mutex}; - return enabled_settings->getSettings(); + return enabled_settings ? enabled_settings->getSettings() : nullptr; } std::shared_ptr ContextAccess::getSettingsConstraints() const { std::lock_guard lock{mutex}; - return enabled_settings->getConstraints(); + return enabled_settings ? enabled_settings->getConstraints() : nullptr; } } diff --git a/dbms/src/Interpreters/Context.cpp b/dbms/src/Interpreters/Context.cpp index c19608c749e..4dc72948f8a 100644 --- a/dbms/src/Interpreters/Context.cpp +++ b/dbms/src/Interpreters/Context.cpp @@ -909,6 +909,7 @@ void Context::setSettings(const Settings & settings_) auto old_allow_introspection_functions = settings.allow_introspection_functions; settings = settings_; + active_default_settings = nullptr; if ((settings.readonly != old_readonly) || (settings.allow_ddl != old_allow_ddl) || (settings.allow_introspection_functions != old_allow_introspection_functions)) calculateAccessRights(); @@ -918,6 +919,7 @@ void Context::setSettings(const Settings & settings_) void Context::setSetting(const StringRef & name, const String & value) { auto lock = getLock(); + active_default_settings = nullptr; if (name == "profile") { setProfile(value); @@ -933,6 +935,7 @@ void Context::setSetting(const StringRef & name, const String & value) void Context::setSetting(const StringRef & name, const Field & value) { auto lock = getLock(); + active_default_settings = nullptr; if (name == "profile") { setProfile(value.safeGet()); @@ -959,6 +962,20 @@ void Context::applySettingsChanges(const SettingsChanges & changes) } +void Context::resetSettingsToDefault() +{ + auto lock = getLock(); + auto default_settings = getAccess()->getDefaultSettings(); + if (default_settings && (default_settings == active_default_settings)) + return; + if (default_settings) + setSettings(*default_settings); + else + setSettings(Settings{}); + active_default_settings = default_settings; +} + + void Context::checkSettingsConstraints(const SettingChange & change) const { if (auto settings_constraints = getSettingsConstraints()) diff --git a/dbms/src/Interpreters/Context.h b/dbms/src/Interpreters/Context.h index 331c89294d0..e5b33e43614 100644 --- a/dbms/src/Interpreters/Context.h +++ b/dbms/src/Interpreters/Context.h @@ -151,6 +151,7 @@ private: bool use_default_roles = false; std::shared_ptr access; std::shared_ptr initial_row_policy; + std::shared_ptr active_default_settings; String current_database; Settings settings; /// Setting for query execution. using ProgressCallback = std::function; @@ -345,6 +346,9 @@ public: void applySettingChange(const SettingChange & change); void applySettingsChanges(const SettingsChanges & changes); + /// Reset settings to the default values for the current user. + void resetSettingsToDefault(); + /// Checks the constraints. void checkSettingsConstraints(const SettingChange & change) const; void checkSettingsConstraints(const SettingsChanges & changes) const;