2019-01-11 19:12:36 +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>
|
|
|
|
|
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>
|
2018-06-05 19:46:49 +00:00
|
|
|
#include <IO/ReadHelpers.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/escapeForFileName.h>
|
2013-01-17 19:36:34 +00:00
|
|
|
|
2019-01-14 18:15:04 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
2020-07-02 20:39:31 +00:00
|
|
|
#include <Common/StringUtils/StringUtils.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,
|
|
|
|
bool has_force_restore_data_flag)
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
ParserCreateQuery parser;
|
2020-04-15 20:28:05 +00:00
|
|
|
ASTPtr ast = parseQuery(parser, query.data(), query.data() + query.size(), "in file " + file_name, 0, context.getSettingsRef().max_parser_depth);
|
2011-10-30 11:30:52 +00:00
|
|
|
|
2019-03-15 16:14:13 +00:00
|
|
|
auto & ast_create_query = ast->as<ASTCreateQuery &>();
|
|
|
|
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);
|
2020-08-06 11:56:49 +00:00
|
|
|
interpreter.setForceAttach(true);
|
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,
|
|
|
|
bool force_restore_data)
|
|
|
|
{
|
|
|
|
String database_attach_query;
|
|
|
|
String database_metadata_file = database_path + ".sql";
|
|
|
|
|
|
|
|
if (Poco::File(database_metadata_file).exists())
|
|
|
|
{
|
2020-03-17 23:51:35 +00:00
|
|
|
/// There is .sql file with database creation statement.
|
2017-06-18 05:43:29 +00:00
|
|
|
ReadBufferFromFile in(database_metadata_file, 1024);
|
|
|
|
readStringUntilEOF(database_attach_query, in);
|
|
|
|
}
|
2019-11-28 19:40:51 +00:00
|
|
|
else if (Poco::File(database_path).exists())
|
|
|
|
{
|
|
|
|
/// Database exists, but .sql file is absent. It's old-style Ordinary database (e.g. system or default)
|
|
|
|
database_attach_query = "ATTACH DATABASE " + backQuoteIfNeed(database) + " ENGINE = Ordinary";
|
|
|
|
}
|
2017-06-18 05:43:29 +00:00
|
|
|
else
|
2019-11-28 19:40:51 +00:00
|
|
|
{
|
|
|
|
/// It's first server run and we need create default and system databases.
|
|
|
|
/// .sql file with database engine will be written for CREATE query.
|
|
|
|
database_attach_query = "CREATE DATABASE " + backQuoteIfNeed(database);
|
|
|
|
}
|
2017-06-18 05:43:29 +00:00
|
|
|
|
2020-08-10 01:06:06 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
executeCreateQuery(database_attach_query, context, database,
|
|
|
|
database_metadata_file, force_restore_data);
|
|
|
|
}
|
|
|
|
catch (Exception & e)
|
|
|
|
{
|
2020-08-10 02:58:08 +00:00
|
|
|
e.addMessage(fmt::format("while loading database {} from path {}", backQuote(database), database_path));
|
2020-08-10 01:06:06 +00:00
|
|
|
throw;
|
|
|
|
}
|
2017-06-18 05:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define SYSTEM_DATABASE "system"
|
|
|
|
|
|
|
|
|
2019-11-28 19:40:51 +00:00
|
|
|
void loadMetadata(Context & context, const String & default_database_name)
|
2011-10-30 11:30:52 +00:00
|
|
|
{
|
2020-08-10 02:58:08 +00:00
|
|
|
Poco::Logger * log = &Poco::Logger::get("loadMetadata");
|
|
|
|
|
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
|
|
|
/// 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)
|
|
|
|
{
|
2020-07-17 17:54:24 +00:00
|
|
|
if (it->isLink())
|
|
|
|
continue;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!it->isDirectory())
|
2020-07-02 20:39:31 +00:00
|
|
|
{
|
|
|
|
if (endsWith(it.name(), ".sql"))
|
|
|
|
{
|
|
|
|
String db_name = it.name().substr(0, it.name().size() - 4);
|
|
|
|
if (db_name != SYSTEM_DATABASE)
|
|
|
|
databases.emplace(unescapeForFileName(db_name), path + "/" + db_name);
|
|
|
|
}
|
2020-08-10 02:58:08 +00:00
|
|
|
|
|
|
|
/// Temporary fails may be left from previous server runs.
|
|
|
|
if (endsWith(it.name(), ".tmp"))
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "Removing temporary file {}", it->path());
|
|
|
|
try
|
|
|
|
{
|
|
|
|
it->remove();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
/// It does not prevent server to startup.
|
|
|
|
tryLogCurrentException(log);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
continue;
|
2020-07-02 20:39:31 +00:00
|
|
|
}
|
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());
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:00:43 +00:00
|
|
|
/// clickhouse-local creates DatabaseMemory as default database by itself
|
|
|
|
/// For clickhouse-server we need create default database
|
|
|
|
bool create_default_db_if_not_exists = !default_database_name.empty();
|
|
|
|
bool metadata_dir_for_default_db_already_exists = databases.count(default_database_name);
|
|
|
|
if (create_default_db_if_not_exists && !metadata_dir_for_default_db_already_exists)
|
2019-12-12 12:30:31 +00:00
|
|
|
databases.emplace(default_database_name, path + "/" + escapeForFileName(default_database_name));
|
2019-11-28 19:40:51 +00:00
|
|
|
|
2019-08-03 11:02:40 +00:00
|
|
|
for (const auto & [name, db_path] : databases)
|
|
|
|
loadDatabase(context, name, db_path, has_force_restore_data_flag);
|
2017-05-23 17:35:05 +00:00
|
|
|
|
2017-06-18 05:43:29 +00:00
|
|
|
if (has_force_restore_data_flag)
|
2018-12-10 18:56:37 +00:00
|
|
|
{
|
2018-12-09 15:11:46 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
force_restore_data_flag_file.remove();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2020-01-11 09:50:41 +00:00
|
|
|
tryLogCurrentException("Load metadata", "Can't remove force restore file to enable data sanity checks");
|
2018-12-09 15:11:46 +00:00
|
|
|
}
|
2018-12-10 18:56:37 +00:00
|
|
|
}
|
2017-06-18 05:43:29 +00:00
|
|
|
}
|
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.
|
2019-07-19 14:22:57 +00:00
|
|
|
loadDatabase(context, SYSTEM_DATABASE, path, 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
|
|
|
|
2020-01-14 19:01:56 +00:00
|
|
|
auto system_database = std::make_shared<DatabaseOrdinary>(SYSTEM_DATABASE, global_path + "metadata/" SYSTEM_DATABASE "/", context);
|
2020-07-08 14:28:07 +00:00
|
|
|
DatabaseCatalog::instance().attachDatabase(SYSTEM_DATABASE, system_database);
|
2017-06-15 20:08:26 +00:00
|
|
|
}
|
|
|
|
|
2011-10-30 11:30:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|