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

Shard now clamps the settings from the initiator. #9447

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
75 changes: 41 additions & 34 deletions dbms/programs/server/TCPHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -875,48 +875,55 @@ void TCPHandler::receiveQuery()
query_context->setCurrentQueryId(state.query_id);

/// Client info
{
ClientInfo & client_info = query_context->getClientInfo();
if (client_revision >= DBMS_MIN_REVISION_WITH_CLIENT_INFO)
client_info.read(*in, client_revision);
ClientInfo & client_info = query_context->getClientInfo();
if (client_revision >= DBMS_MIN_REVISION_WITH_CLIENT_INFO)
client_info.read(*in, client_revision);

/// For better support of old clients, that does not send ClientInfo.
if (client_info.query_kind == ClientInfo::QueryKind::NO_QUERY)
{
client_info.query_kind = ClientInfo::QueryKind::INITIAL_QUERY;
client_info.client_name = client_name;
client_info.client_version_major = client_version_major;
client_info.client_version_minor = client_version_minor;
client_info.client_version_patch = client_version_patch;
client_info.client_revision = client_revision;
}
/// For better support of old clients, that does not send ClientInfo.
if (client_info.query_kind == ClientInfo::QueryKind::NO_QUERY)
{
client_info.query_kind = ClientInfo::QueryKind::INITIAL_QUERY;
client_info.client_name = client_name;
client_info.client_version_major = client_version_major;
client_info.client_version_minor = client_version_minor;
client_info.client_version_patch = client_version_patch;
client_info.client_revision = client_revision;
}

/// Set fields, that are known apriori.
client_info.interface = ClientInfo::Interface::TCP;
/// Set fields, that are known apriori.
client_info.interface = ClientInfo::Interface::TCP;

if (client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
{
/// 'Current' fields was set at receiveHello.
client_info.initial_user = client_info.current_user;
client_info.initial_query_id = client_info.current_query_id;
client_info.initial_address = client_info.current_address;
}
else
{
query_context->setInitialRowPolicy();
}
if (client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
{
/// 'Current' fields was set at receiveHello.
client_info.initial_user = client_info.current_user;
client_info.initial_query_id = client_info.current_query_id;
client_info.initial_address = client_info.current_address;
}
else
{
query_context->setInitialRowPolicy();
}

/// Per query settings.
Settings custom_settings{};
/// Per query settings are also passed via TCP.
/// We need to check them before applying due to they can violate the settings constraints.
auto settings_format = (client_revision >= DBMS_MIN_REVISION_WITH_SETTINGS_SERIALIZED_AS_STRINGS) ? SettingsBinaryFormat::STRINGS
: SettingsBinaryFormat::OLD;
custom_settings.deserialize(*in, settings_format);
auto settings_changes = custom_settings.changes();
query_context->checkSettingsConstraints(settings_changes);
Settings passed_settings;
passed_settings.deserialize(*in, settings_format);
auto settings_changes = passed_settings.changes();
if (client_info.query_kind == ClientInfo::QueryKind::INITIAL_QUERY)
{
/// Throw an exception if the passed settings violate the constraints.
query_context->checkSettingsConstraints(settings_changes);
}
else
{
/// Quietly clamp to the constraints if it's not an initial query.
query_context->clampToSettingsConstraints(settings_changes);
}
query_context->applySettingsChanges(settings_changes);

Settings & settings = query_context->getSettingsRef();
const Settings & settings = query_context->getSettingsRef();

/// Sync timeouts on client and server during current query to avoid dangling queries on server
/// NOTE: We use settings.send_timeout for the receive timeout and vice versa (change arguments ordering in TimeoutSetter),
Expand Down
71 changes: 71 additions & 0 deletions dbms/src/Access/SettingsConstraints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,77 @@ void SettingsConstraints::check(const Settings & current_settings, const Setting
}


void SettingsConstraints::clamp(const Settings & current_settings, SettingChange & change) const
{
const String & name = change.name;
size_t setting_index = Settings::findIndex(name);
if (setting_index == Settings::npos)
return;

Field new_value = Settings::valueToCorrespondingType(setting_index, change.value);
Field current_value = current_settings.get(setting_index);

/// Setting isn't checked if value wasn't changed.
if (current_value == new_value)
return;

if (!current_settings.allow_ddl && name == "allow_ddl")
{
change.value = current_value;
return;
}

/** The `readonly` value is understood as follows:
* 0 - everything allowed.
* 1 - only read queries can be made; you can not change the settings.
* 2 - You can only do read queries and you can change the settings, except for the `readonly` setting.
*/
if (current_settings.readonly == 1)
{
change.value = current_value;
return;
}

if (current_settings.readonly > 1 && name == "readonly")
{
change.value = current_value;
return;
}

const Constraint * constraint = tryGetConstraint(setting_index);
if (constraint)
{
if (constraint->read_only)
{
change.value = current_value;
return;
}

if (!constraint->min_value.isNull() && (new_value < constraint->min_value))
{
if (!constraint->max_value.isNull() && (constraint->min_value > constraint->max_value))
change.value = current_value;
else
change.value = constraint->min_value;
return;
}

if (!constraint->max_value.isNull() && (new_value > constraint->max_value))
{
change.value = constraint->max_value;
return;
}
}
}


void SettingsConstraints::clamp(const Settings & current_settings, SettingsChanges & changes) const
{
for (auto & change : changes)
clamp(current_settings, change);
}


SettingsConstraints::Constraint & SettingsConstraints::getConstraintRef(size_t index)
{
auto it = constraints_by_index.find(index);
Expand Down
5 changes: 5 additions & 0 deletions dbms/src/Access/SettingsConstraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ class SettingsConstraints

Infos getInfo() const;

/// Checks whether `change` violates these constraints and throws an exception if so.
void check(const Settings & current_settings, const SettingChange & change) const;
void check(const Settings & current_settings, const SettingsChanges & changes) const;

/// Checks whether `change` violates these and clamps the `change` if so.
void clamp(const Settings & current_settings, SettingChange & change) const;
void clamp(const Settings & current_settings, SettingsChanges & changes) const;

/** Set multiple settings from "profile" (in server configuration file (users.xml), profiles contain groups of multiple settings).
* The profile can also be set using the `set` functions, like the profile setting.
*/
Expand Down
18 changes: 15 additions & 3 deletions dbms/src/Interpreters/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,20 +1216,32 @@ void Context::applySettingsChanges(const SettingsChanges & changes)
}


void Context::checkSettingsConstraints(const SettingChange & change)
void Context::checkSettingsConstraints(const SettingChange & change) const
{
if (settings_constraints)
settings_constraints->check(settings, change);
}


void Context::checkSettingsConstraints(const SettingsChanges & changes)
void Context::checkSettingsConstraints(const SettingsChanges & changes) const
{
if (settings_constraints)
settings_constraints->check(settings, changes);
}


void Context::clampToSettingsConstraints(SettingChange & change) const
{
if (settings_constraints)
settings_constraints->clamp(settings, change);
}

void Context::clampToSettingsConstraints(SettingsChanges & changes) const
{
if (settings_constraints)
settings_constraints->clamp(settings, changes);
}


String Context::getCurrentDatabase() const
{
return current_database;
Expand Down
6 changes: 4 additions & 2 deletions dbms/src/Interpreters/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,10 @@ class Context
void applySettingsChanges(const SettingsChanges & changes);

/// Checks the constraints.
void checkSettingsConstraints(const SettingChange & change);
void checkSettingsConstraints(const SettingsChanges & changes);
void checkSettingsConstraints(const SettingChange & change) const;
void checkSettingsConstraints(const SettingsChanges & changes) const;
void clampToSettingsConstraints(SettingChange & change) const;
void clampToSettingsConstraints(SettingsChanges & changes) const;

/// Returns the current constraints (can return null).
std::shared_ptr<const SettingsConstraints> getSettingsConstraints() const { return settings_constraints; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
<replica>
<host>node1</host>
<port>9000</port>
<user>distributed</user>
<user>normal</user>
</replica>
</shard>
<shard>
<replica>
<host>node2</host>
<port>9000</port>
<user>distributed</user>
<user>readonly</user>
</replica>
</shard>
</test_cluster>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
<yandex>
<profiles>
<default>
<max_memory_usage>10000000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
<load_balancing>random</load_balancing>
</default>
<distributed_profile>
<max_memory_usage>1000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
<load_balancing>random</load_balancing>
<readonly>2</readonly>
<normal>
<max_memory_usage>50000000</max_memory_usage>
<constraints>
<max_memory_usage>
<min>1</min>
<max>1000000</max>
<min>11111111</min>
<max>99999999</max>
</max_memory_usage>
</constraints>
</distributed_profile>
</normal>
<readonly>
<readonly>1</readonly>
</readonly>
</profiles>

<users>
<default>
<password></password>
<profile>default</profile>
<quota>default</quota>
<password></password>
</default>
<distributed>
<normal>
<profile>normal</profile>
<password></password>
</normal>
<readonly>
<profile>readonly</profile>
<password></password>
<profile>distributed_profile</profile>
<quota>default</quota>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
</distributed>
</readonly>
</users>

<quotas>
<default>
</default>
</quotas>
</yandex>
Original file line number Diff line number Diff line change
@@ -1,34 +1,36 @@
<yandex>
<profiles>
<default>
<max_memory_usage>1000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
<load_balancing>random</load_balancing>
</default>
<remote_profile>
<max_memory_usage>2000000000</max_memory_usage>
<normal>
<max_memory_usage>80000000</max_memory_usage>
<use_uncompressed_cache>0</use_uncompressed_cache>
<load_balancing>random</load_balancing>
</remote_profile>
</normal>
<wasteful>
<max_memory_usage>2000000000</max_memory_usage>
</wasteful>
<readonly>
<readonly>1</readonly>
</readonly>
</profiles>

<users>
<default>
<password></password>
<profile>default</profile>
<quota>default</quota>
<password></password>
</default>
<remote>
<normal>
<profile>normal</profile>
<password></password>
</normal>
<wasteful>
<profile>wasteful</profile>
<password></password>
</wasteful>
<readonly>
<profile>readonly</profile>
<password></password>
<profile>remote_profile</profile>
<quota>default</quota>
<networks incl="networks" replace="replace">
<ip>::/0</ip>
</networks>
</remote>
</readonly>
</users>

<quotas>
<default>
</default>
</quotas>
</yandex>