2017-07-28 12:58:24 +00:00
|
|
|
#include <Interpreters/ClusterProxy/executeQuery.h>
|
|
|
|
#include <Interpreters/ClusterProxy/IStreamFactory.h>
|
2019-03-22 12:08:30 +00:00
|
|
|
#include <Core/Settings.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/Context.h>
|
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <Interpreters/IInterpreter.h>
|
|
|
|
#include <Parsers/queryToString.h>
|
2017-08-29 13:23:04 +00:00
|
|
|
#include <Interpreters/ProcessList.h>
|
2020-01-31 08:54:57 +00:00
|
|
|
#include <Processors/Pipe.h>
|
2016-01-28 01:00:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace ClusterProxy
|
|
|
|
{
|
|
|
|
|
2020-06-18 18:45:39 +00:00
|
|
|
Context removeUserRestrictionsFromSettings(const Context & context, const Settings & settings, Poco::Logger * log)
|
2016-01-28 01:00:27 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
Settings new_settings = settings;
|
2018-03-11 00:15:26 +00:00
|
|
|
new_settings.queue_max_wait_ms = Cluster::saturate(new_settings.queue_max_wait_ms, settings.max_execution_time);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Does not matter on remote servers, because queries are sent under different user.
|
|
|
|
new_settings.max_concurrent_queries_for_user = 0;
|
2018-03-11 00:15:26 +00:00
|
|
|
new_settings.max_memory_usage_for_user = 0;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/// Set as unchanged to avoid sending to remote server.
|
2019-08-13 11:24:18 +00:00
|
|
|
new_settings.max_concurrent_queries_for_user.changed = false;
|
|
|
|
new_settings.max_memory_usage_for_user.changed = false;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-06-29 19:58:05 +00:00
|
|
|
if (settings.force_optimize_skip_unused_shards_nesting && settings.force_optimize_skip_unused_shards)
|
2020-03-22 22:42:44 +00:00
|
|
|
{
|
2020-06-21 11:29:54 +00:00
|
|
|
if (new_settings.force_optimize_skip_unused_shards_nesting == 1)
|
|
|
|
{
|
2020-06-21 18:29:11 +00:00
|
|
|
new_settings.force_optimize_skip_unused_shards = false;
|
2020-06-21 11:29:54 +00:00
|
|
|
new_settings.force_optimize_skip_unused_shards.changed = false;
|
|
|
|
|
|
|
|
if (log)
|
|
|
|
LOG_TRACE(log, "Disabling force_optimize_skip_unused_shards for nested queries (force_optimize_skip_unused_shards_nesting exceeded)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-21 18:29:11 +00:00
|
|
|
--new_settings.force_optimize_skip_unused_shards_nesting.value;
|
2020-06-21 11:29:54 +00:00
|
|
|
new_settings.force_optimize_skip_unused_shards_nesting.changed = true;
|
2020-06-18 18:45:39 +00:00
|
|
|
|
2020-06-21 11:29:54 +00:00
|
|
|
if (log)
|
|
|
|
LOG_TRACE(log, "force_optimize_skip_unused_shards_nesting is now {}", new_settings.force_optimize_skip_unused_shards_nesting);
|
|
|
|
}
|
2020-03-22 22:42:44 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 19:58:05 +00:00
|
|
|
if (settings.optimize_skip_unused_shards_nesting && settings.optimize_skip_unused_shards)
|
2020-06-16 19:02:06 +00:00
|
|
|
{
|
2020-06-21 11:29:54 +00:00
|
|
|
if (new_settings.optimize_skip_unused_shards_nesting == 1)
|
|
|
|
{
|
2020-06-21 18:29:11 +00:00
|
|
|
new_settings.optimize_skip_unused_shards = false;
|
2020-06-21 11:29:54 +00:00
|
|
|
new_settings.optimize_skip_unused_shards.changed = false;
|
|
|
|
|
|
|
|
if (log)
|
|
|
|
LOG_TRACE(log, "Disabling optimize_skip_unused_shards for nested queries (optimize_skip_unused_shards_nesting exceeded)");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-21 18:29:11 +00:00
|
|
|
--new_settings.optimize_skip_unused_shards_nesting.value;
|
2020-06-21 11:29:54 +00:00
|
|
|
new_settings.optimize_skip_unused_shards_nesting.changed = true;
|
2020-06-18 18:45:39 +00:00
|
|
|
|
2020-06-21 11:29:54 +00:00
|
|
|
if (log)
|
|
|
|
LOG_TRACE(log, "optimize_skip_unused_shards_nesting is now {}", new_settings.optimize_skip_unused_shards_nesting);
|
|
|
|
}
|
2020-06-16 19:02:06 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 14:07:40 +00:00
|
|
|
Context new_context(context);
|
|
|
|
new_context.setSettings(new_settings);
|
|
|
|
|
2019-01-09 12:21:04 +00:00
|
|
|
return new_context;
|
|
|
|
}
|
|
|
|
|
2020-08-03 11:33:11 +00:00
|
|
|
Pipe executeQuery(
|
2020-06-18 18:45:39 +00:00
|
|
|
IStreamFactory & stream_factory, const ClusterPtr & cluster, Poco::Logger * log,
|
2020-02-17 10:42:14 +00:00
|
|
|
const ASTPtr & query_ast, const Context & context, const Settings & settings, const SelectQueryInfo & query_info)
|
2019-01-09 12:21:04 +00:00
|
|
|
{
|
2020-06-18 18:45:39 +00:00
|
|
|
assert(log);
|
|
|
|
|
2020-01-31 08:54:57 +00:00
|
|
|
Pipes res;
|
2019-01-09 12:21:04 +00:00
|
|
|
|
|
|
|
const std::string query = queryToString(query_ast);
|
|
|
|
|
2020-06-18 18:45:39 +00:00
|
|
|
Context new_context = removeUserRestrictionsFromSettings(context, settings, log);
|
2019-01-09 12:21:04 +00:00
|
|
|
|
2017-08-29 13:23:04 +00:00
|
|
|
ThrottlerPtr user_level_throttler;
|
2020-04-22 06:01:33 +00:00
|
|
|
if (auto * process_list_element = context.getProcessListElement())
|
2018-03-29 13:24:36 +00:00
|
|
|
user_level_throttler = process_list_element->getUserNetworkThrottler();
|
2017-08-29 13:23:04 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Network bandwidth limit, if needed.
|
|
|
|
ThrottlerPtr throttler;
|
2018-03-11 00:15:26 +00:00
|
|
|
if (settings.max_network_bandwidth || settings.max_network_bytes)
|
2017-08-29 13:23:04 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
throttler = std::make_shared<Throttler>(
|
2018-03-11 00:15:26 +00:00
|
|
|
settings.max_network_bandwidth,
|
|
|
|
settings.max_network_bytes,
|
2017-08-29 13:23:04 +00:00
|
|
|
"Limit for bytes to send or receive over network exceeded.",
|
|
|
|
user_level_throttler);
|
|
|
|
}
|
2018-03-29 13:24:36 +00:00
|
|
|
else
|
2017-08-29 13:23:04 +00:00
|
|
|
throttler = user_level_throttler;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
for (const auto & shard_info : cluster->getShardsInfo())
|
2020-02-17 10:42:14 +00:00
|
|
|
stream_factory.createForShard(shard_info, query, query_ast, new_context, throttler, query_info, res);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
return res;
|
2016-01-28 01:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|