This commit is contained in:
Mikhail Filimonov 2020-10-22 09:37:03 +02:00
parent 67cbb55d63
commit 111b553ee5
No known key found for this signature in database
GPG Key ID: 6E49C2E9AF1220BE
2 changed files with 46 additions and 46 deletions

View File

@ -57,8 +57,8 @@ LocalServer::LocalServer() = default;
LocalServer::~LocalServer() LocalServer::~LocalServer()
{ {
if (context) if (global_context)
context->shutdown(); /// required for properly exception handling global_context->shutdown(); /// required for properly exception handling
} }
@ -95,9 +95,9 @@ void LocalServer::initialize(Poco::Util::Application & self)
} }
} }
void LocalServer::applyCmdSettings() void LocalServer::applyCmdSettings(Context & context)
{ {
context->applySettingsChanges(cmd_settings.changes()); context.applySettingsChanges(cmd_settings.changes());
} }
/// If path is specified and not empty, will try to setup server environment and load existing metadata /// If path is specified and not empty, will try to setup server environment and load existing metadata
@ -151,12 +151,12 @@ void LocalServer::tryInitPath()
if (path.back() != '/') if (path.back() != '/')
path += '/'; path += '/';
context->setPath(path); global_context->setPath(path);
context->setTemporaryStorage(path + "tmp"); global_context->setTemporaryStorage(path + "tmp");
context->setFlagsPath(path + "flags"); global_context->setFlagsPath(path + "flags");
context->setUserFilesPath(""); // user's files are everywhere global_context->setUserFilesPath(""); // user's files are everywhere
} }
@ -190,9 +190,9 @@ try
} }
shared_context = Context::createShared(); shared_context = Context::createShared();
context = std::make_unique<Context>(Context::createGlobal(shared_context.get())); global_context = std::make_unique<Context>(Context::createGlobal(shared_context.get()));
context->makeGlobalContext(); global_context->makeGlobalContext();
context->setApplicationType(Context::ApplicationType::LOCAL); global_context->setApplicationType(Context::ApplicationType::LOCAL);
tryInitPath(); tryInitPath();
std::optional<StatusFile> status; std::optional<StatusFile> status;
@ -214,32 +214,32 @@ try
/// Maybe useless /// Maybe useless
if (config().has("macros")) if (config().has("macros"))
context->setMacros(std::make_unique<Macros>(config(), "macros", log)); global_context->setMacros(std::make_unique<Macros>(config(), "macros", log));
/// Skip networking /// Skip networking
/// Sets external authenticators config (LDAP). /// Sets external authenticators config (LDAP).
context->setExternalAuthenticatorsConfig(config()); global_context->setExternalAuthenticatorsConfig(config());
setupUsers(); setupUsers();
/// Limit on total number of concurrently executing queries. /// Limit on total number of concurrently executing queries.
/// There is no need for concurrent queries, override max_concurrent_queries. /// There is no need for concurrent queries, override max_concurrent_queries.
context->getProcessList().setMaxSize(0); global_context->getProcessList().setMaxSize(0);
/// Size of cache for uncompressed blocks. Zero means disabled. /// Size of cache for uncompressed blocks. Zero means disabled.
size_t uncompressed_cache_size = config().getUInt64("uncompressed_cache_size", 0); size_t uncompressed_cache_size = config().getUInt64("uncompressed_cache_size", 0);
if (uncompressed_cache_size) if (uncompressed_cache_size)
context->setUncompressedCache(uncompressed_cache_size); global_context->setUncompressedCache(uncompressed_cache_size);
/// Size of cache for marks (index of MergeTree family of tables). It is necessary. /// Size of cache for marks (index of MergeTree family of tables). It is necessary.
/// Specify default value for mark_cache_size explicitly! /// Specify default value for mark_cache_size explicitly!
size_t mark_cache_size = config().getUInt64("mark_cache_size", 5368709120); size_t mark_cache_size = config().getUInt64("mark_cache_size", 5368709120);
if (mark_cache_size) if (mark_cache_size)
context->setMarkCache(mark_cache_size); global_context->setMarkCache(mark_cache_size);
/// Load global settings from default_profile and system_profile. /// Load global settings from default_profile and system_profile.
context->setDefaultProfiles(config()); global_context->setDefaultProfiles(config());
/** Init dummy default DB /** Init dummy default DB
* NOTE: We force using isolated default database to avoid conflicts with default database from server environment * NOTE: We force using isolated default database to avoid conflicts with default database from server environment
@ -247,34 +247,34 @@ try
* if such tables will not be dropped, clickhouse-server will not be able to load them due to security reasons. * if such tables will not be dropped, clickhouse-server will not be able to load them due to security reasons.
*/ */
std::string default_database = config().getString("default_database", "_local"); std::string default_database = config().getString("default_database", "_local");
DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared<DatabaseMemory>(default_database, *context)); DatabaseCatalog::instance().attachDatabase(default_database, std::make_shared<DatabaseMemory>(default_database, *global_context));
context->setCurrentDatabase(default_database); global_context->setCurrentDatabase(default_database);
applyCmdOptions(); applyCmdOptions(*global_context);
String path = context->getPath(); String path = global_context->getPath();
if (!path.empty()) if (!path.empty())
{ {
/// Lock path directory before read /// Lock path directory before read
status.emplace(context->getPath() + "status", StatusFile::write_full_info); status.emplace(global_context->getPath() + "status", StatusFile::write_full_info);
LOG_DEBUG(log, "Loading metadata from {}", path); LOG_DEBUG(log, "Loading metadata from {}", path);
Poco::File(path + "data/").createDirectories(); Poco::File(path + "data/").createDirectories();
Poco::File(path + "metadata/").createDirectories(); Poco::File(path + "metadata/").createDirectories();
loadMetadataSystem(*context); loadMetadataSystem(*global_context);
attachSystemTables(*context); attachSystemTables(*global_context);
loadMetadata(*context); loadMetadata(*global_context);
DatabaseCatalog::instance().loadDatabases(); DatabaseCatalog::instance().loadDatabases();
LOG_DEBUG(log, "Loaded metadata."); LOG_DEBUG(log, "Loaded metadata.");
} }
else else
{ {
attachSystemTables(*context); attachSystemTables(*global_context);
} }
processQueries(); processQueries();
context->shutdown(); global_context->shutdown();
context.reset(); global_context.reset();
status.reset(); status.reset();
cleanup(); cleanup();
@ -327,7 +327,7 @@ void LocalServer::processQueries()
String initial_create_query = getInitialCreateTableQuery(); String initial_create_query = getInitialCreateTableQuery();
String queries_str = initial_create_query + config().getRawString("query"); String queries_str = initial_create_query + config().getRawString("query");
const auto & settings = context->getSettingsRef(); const auto & settings = global_context->getSettingsRef();
std::vector<String> queries; std::vector<String> queries;
auto parse_res = splitMultipartQuery(queries_str, queries, settings.max_query_size, settings.max_parser_depth); auto parse_res = splitMultipartQuery(queries_str, queries, settings.max_query_size, settings.max_parser_depth);
@ -335,19 +335,19 @@ void LocalServer::processQueries()
if (!parse_res.second) if (!parse_res.second)
throw Exception("Cannot parse and execute the following part of query: " + String(parse_res.first), ErrorCodes::SYNTAX_ERROR); throw Exception("Cannot parse and execute the following part of query: " + String(parse_res.first), ErrorCodes::SYNTAX_ERROR);
/// we can't mutate global context (due to possible races), so we can't reuse it safely as a query context /// we can't mutate global global_context (can lead to races, as it was already passed to some background threads)
/// so we need a copy here /// so we can't reuse it safely as a query global_context and need a copy here
auto query_context = Context(context.get()); auto context = Context(*global_context);
query_context->makeSessionContext(); context.makeSessionContext();
query_context->makeQueryContext(); context.makeQueryContext();
query_context->setUser("default", "", Poco::Net::SocketAddress{}); context.setUser("default", "", Poco::Net::SocketAddress{});
query_context->setCurrentQueryId(""); context.setCurrentQueryId("");
applyCmdSettings(); applyCmdSettings(context);
/// Use the same query_id (and thread group) for all queries /// Use the same query_id (and thread group) for all queries
CurrentThread::QueryScope query_scope_holder(*query_context); CurrentThread::QueryScope query_scope_holder(context);
bool echo_queries = config().hasOption("echo") || config().hasOption("verbose"); bool echo_queries = config().hasOption("echo") || config().hasOption("verbose");
std::exception_ptr exception; std::exception_ptr exception;
@ -366,7 +366,7 @@ void LocalServer::processQueries()
try try
{ {
executeQuery(read_buf, write_buf, /* allow_into_outfile = */ true, *query_context, {}); executeQuery(read_buf, write_buf, /* allow_into_outfile = */ true, context, {});
} }
catch (...) catch (...)
{ {
@ -431,7 +431,7 @@ void LocalServer::setupUsers()
} }
if (users_config) if (users_config)
context->setUsersConfig(users_config); global_context->setUsersConfig(users_config);
else else
throw Exception("Can't load config for users", ErrorCodes::CANNOT_LOAD_CONFIG); throw Exception("Can't load config for users", ErrorCodes::CANNOT_LOAD_CONFIG);
} }
@ -585,10 +585,10 @@ void LocalServer::init(int argc, char ** argv)
argsToConfig(arguments, config(), 100); argsToConfig(arguments, config(), 100);
} }
void LocalServer::applyCmdOptions() void LocalServer::applyCmdOptions(Context & context)
{ {
context->setDefaultFormat(config().getString("output-format", config().getString("format", "TSV"))); context.setDefaultFormat(config().getString("output-format", config().getString("format", "TSV")));
applyCmdSettings(); applyCmdSettings(context);
} }
} }

View File

@ -36,15 +36,15 @@ private:
std::string getInitialCreateTableQuery(); std::string getInitialCreateTableQuery();
void tryInitPath(); void tryInitPath();
void applyCmdOptions(); void applyCmdOptions(Context & context);
void applyCmdSettings(); void applyCmdSettings(Context & context);
void processQueries(); void processQueries();
void setupUsers(); void setupUsers();
void cleanup(); void cleanup();
protected: protected:
SharedContextHolder shared_context; SharedContextHolder shared_context;
std::unique_ptr<Context> context; std::unique_ptr<Context> global_context;
/// Settings specified via command line args /// Settings specified via command line args
Settings cmd_settings; Settings cmd_settings;