2013-06-15 06:16:17 +00:00
|
|
|
#include <iomanip>
|
2015-09-26 01:02:07 +00:00
|
|
|
#include <thread>
|
|
|
|
#include <future>
|
|
|
|
|
2017-06-23 20:22:35 +00:00
|
|
|
#include <common/ThreadPool.h>
|
2013-06-15 06:16:17 +00:00
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
#include <Poco/DirectoryIterator.h>
|
|
|
|
#include <Poco/FileStream.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ParserCreateQuery.h>
|
|
|
|
#include <Parsers/ASTCreateQuery.h>
|
|
|
|
#include <Parsers/parseQuery.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/InterpreterCreateQuery.h>
|
2017-05-23 17:35:05 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Interpreters/loadMetadata.h>
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2017-06-15 20:08:26 +00:00
|
|
|
#include <Databases/DatabaseOrdinary.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <IO/ReadBufferFromFile.h>
|
|
|
|
#include <Common/escapeForFileName.h>
|
2013-01-17 19:36:34 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Stopwatch.h>
|
2013-06-15 06:14:41 +00:00
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-03-19 01:18:49 +00:00
|
|
|
static void executeCreateQuery(
|
2017-04-01 07:20:54 +00:00
|
|
|
const String & query,
|
|
|
|
Context & context,
|
|
|
|
const String & database,
|
|
|
|
const String & file_name,
|
2017-06-18 05:43:29 +00:00
|
|
|
ThreadPool * pool,
|
2017-04-01 07:20:54 +00:00
|
|
|
bool has_force_restore_data_flag)
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserCreateQuery parser;
|
|
|
|
ASTPtr ast = parseQuery(parser, query.data(), query.data() + query.size(), "in file " + file_name);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTCreateQuery & ast_create_query = typeid_cast<ASTCreateQuery &>(*ast);
|
|
|
|
ast_create_query.attach = true;
|
|
|
|
ast_create_query.database = database;
|
2012-03-09 03:06:09 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
InterpreterCreateQuery interpreter(ast, context);
|
2018-01-18 23:40:32 +00:00
|
|
|
interpreter.setInternal(true);
|
2017-06-18 05:43:29 +00:00
|
|
|
if (pool)
|
|
|
|
interpreter.setDatabaseLoadingThreadpool(*pool);
|
2017-04-01 07:20:54 +00:00
|
|
|
interpreter.setForceRestoreData(has_force_restore_data_flag);
|
|
|
|
interpreter.execute();
|
2015-12-13 14:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
static void loadDatabase(
|
|
|
|
Context & context,
|
|
|
|
const String & database,
|
|
|
|
const String & database_path,
|
|
|
|
ThreadPool * thread_pool,
|
|
|
|
bool force_restore_data)
|
|
|
|
{
|
|
|
|
/// There may exist .sql file with database creation statement.
|
|
|
|
/// Or, if it is absent, then database with default engine is created.
|
|
|
|
|
|
|
|
String database_attach_query;
|
|
|
|
String database_metadata_file = database_path + ".sql";
|
|
|
|
|
|
|
|
if (Poco::File(database_metadata_file).exists())
|
|
|
|
{
|
|
|
|
ReadBufferFromFile in(database_metadata_file, 1024);
|
|
|
|
readStringUntilEOF(database_attach_query, in);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
database_attach_query = "ATTACH DATABASE " + backQuoteIfNeed(database);
|
|
|
|
|
|
|
|
executeCreateQuery(database_attach_query, context, database, database_metadata_file, thread_pool, force_restore_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define SYSTEM_DATABASE "system"
|
|
|
|
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
void loadMetadata(Context & context)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
String path = context.getPath() + "metadata";
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** There may exist 'force_restore_data' file, that means,
|
|
|
|
* skip safety threshold on difference of data parts while initializing tables.
|
|
|
|
* This file is deleted after successful loading of tables.
|
|
|
|
* (flag is "one-shot")
|
|
|
|
*/
|
|
|
|
Poco::File force_restore_data_flag_file(context.getFlagsPath() + "force_restore_data");
|
|
|
|
bool has_force_restore_data_flag = force_restore_data_flag_file.exists();
|
2016-08-09 21:48:05 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// For parallel tables loading.
|
|
|
|
ThreadPool thread_pool(SettingMaxThreads().getAutoValue());
|
2015-09-29 20:47:15 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Loop over databases.
|
2017-06-15 20:08:26 +00:00
|
|
|
std::map<String, String> databases;
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::DirectoryIterator dir_end;
|
|
|
|
for (Poco::DirectoryIterator it(path); it != dir_end; ++it)
|
|
|
|
{
|
|
|
|
if (!it->isDirectory())
|
|
|
|
continue;
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// For '.svn', '.gitignore' directory and similar.
|
|
|
|
if (it.name().at(0) == '.')
|
|
|
|
continue;
|
2012-03-09 03:56:12 +00:00
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
if (it.name() == SYSTEM_DATABASE)
|
|
|
|
continue;
|
|
|
|
|
2017-06-15 20:08:26 +00:00
|
|
|
databases.emplace(unescapeForFileName(it.name()), it.path().toString());
|
|
|
|
}
|
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
for (const auto & elem : databases)
|
|
|
|
loadDatabase(context, elem.first, elem.second, &thread_pool, has_force_restore_data_flag);
|
2015-09-26 01:02:07 +00:00
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
thread_pool.wait();
|
2017-05-23 17:35:05 +00:00
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
if (has_force_restore_data_flag)
|
|
|
|
force_restore_data_flag_file.remove();
|
|
|
|
}
|
2017-05-23 17:35:05 +00:00
|
|
|
|
2017-06-15 20:08:26 +00:00
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
void loadMetadataSystem(Context & context)
|
|
|
|
{
|
|
|
|
String path = context.getPath() + "metadata/" SYSTEM_DATABASE;
|
|
|
|
if (Poco::File(path).exists())
|
2017-06-15 20:08:26 +00:00
|
|
|
{
|
2017-06-18 05:43:29 +00:00
|
|
|
/// 'has_force_restore_data_flag' is true, to not fail on loading query_log table, if it is corrupted.
|
|
|
|
loadDatabase(context, SYSTEM_DATABASE, path, nullptr, true);
|
2017-06-15 20:08:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/// Initialize system database manually
|
|
|
|
String global_path = context.getPath();
|
2017-06-18 05:43:29 +00:00
|
|
|
Poco::File(global_path + "data/" SYSTEM_DATABASE).createDirectories();
|
|
|
|
Poco::File(global_path + "metadata/" SYSTEM_DATABASE).createDirectories();
|
2017-06-15 20:08:26 +00:00
|
|
|
|
2017-11-03 19:53:10 +00:00
|
|
|
auto system_database = std::make_shared<DatabaseOrdinary>(SYSTEM_DATABASE, global_path + "metadata/" SYSTEM_DATABASE, context);
|
2017-06-18 05:43:29 +00:00
|
|
|
context.addDatabase(SYSTEM_DATABASE, system_database);
|
2017-06-15 20:08:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|