ClickHouse/dbms/Databases/DatabaseAtomic.cpp

259 lines
9.3 KiB
C++
Raw Normal View History

2019-10-23 13:46:38 +00:00
#include <Databases/DatabaseAtomic.h>
2019-10-30 12:17:52 +00:00
#include <Databases/DatabaseOnDisk.h>
2019-11-11 11:34:03 +00:00
#include <Poco/File.h>
#include <IO/ReadHelpers.h>
#include <IO/WriteHelpers.h>
2020-01-23 19:10:09 +00:00
#include <Common/Stopwatch.h>
2020-01-27 20:31:39 +00:00
#include <Parsers/formatAST.h>
2020-03-23 00:12:13 +00:00
#include <Common/rename.h>
2019-10-23 13:46:38 +00:00
namespace DB
{
2019-11-11 11:34:03 +00:00
namespace ErrorCodes
{
extern const int UNKNOWN_TABLE;
extern const int TABLE_ALREADY_EXISTS;
2019-11-11 14:28:28 +00:00
extern const int FILE_DOESNT_EXIST;
2020-03-23 00:12:13 +00:00
extern const int CANNOT_ASSIGN_ALTER;
2019-11-11 11:34:03 +00:00
}
2019-10-23 13:46:38 +00:00
2020-04-01 22:41:29 +00:00
class AtomicDatabaseTablesSnapshotIterator final : public DatabaseTablesSnapshotIterator
{
public:
AtomicDatabaseTablesSnapshotIterator(DatabaseTablesSnapshotIterator && base)
: DatabaseTablesSnapshotIterator(std::move(base)) {}
UUID uuid() const override { return table()->getStorageID().uuid; }
};
2020-01-22 11:30:11 +00:00
DatabaseAtomic::DatabaseAtomic(String name_, String metadata_path_, Context & context_)
2019-10-23 13:46:38 +00:00
: DatabaseOrdinary(name_, metadata_path_, context_)
{
2019-11-11 11:34:03 +00:00
data_path = "store/";
2020-03-19 21:14:52 +00:00
log = &Logger::get("DatabaseAtomic (" + name_ + ")");
2019-11-11 11:34:03 +00:00
}
String DatabaseAtomic::getTableDataPath(const String & table_name) const
2019-11-11 11:34:03 +00:00
{
2019-12-02 19:11:18 +00:00
std::lock_guard lock(mutex);
2019-11-11 11:34:03 +00:00
auto it = table_name_to_path.find(table_name);
if (it == table_name_to_path.end())
throw Exception("Table " + table_name + " not found in database " + getDatabaseName(), ErrorCodes::UNKNOWN_TABLE);
2020-01-16 18:13:18 +00:00
assert(it->second != data_path && !it->second.empty());
return it->second;
2019-11-11 11:34:03 +00:00
}
String DatabaseAtomic::getTableDataPath(const ASTCreateQuery & query) const
2019-11-11 11:34:03 +00:00
{
2020-03-19 21:14:52 +00:00
auto tmp = data_path + getPathForUUID(query.uuid);
2020-01-16 18:13:18 +00:00
assert(tmp != data_path && !tmp.empty());
return tmp;
2019-11-11 11:34:03 +00:00
}
void DatabaseAtomic::drop(const Context &)
{
2020-01-27 20:31:39 +00:00
Poco::File(getMetadataPath()).remove(true);
2019-11-11 11:34:03 +00:00
}
void DatabaseAtomic::attachTable(const String & name, const StoragePtr & table, const String & relative_table_path)
{
2020-01-16 18:13:18 +00:00
assert(relative_table_path != data_path && !relative_table_path.empty());
2019-11-11 11:34:03 +00:00
std::lock_guard lock(mutex);
assertDetachedTableNotInUse(table->getStorageID().uuid);
2020-03-23 00:12:13 +00:00
DatabaseWithDictionaries::attachTableUnlocked(name, table, relative_table_path);
2019-12-02 19:11:18 +00:00
table_name_to_path.emplace(std::make_pair(name, relative_table_path));
2019-10-23 13:46:38 +00:00
}
2019-11-11 11:34:03 +00:00
StoragePtr DatabaseAtomic::detachTable(const String & name)
{
2020-03-23 00:12:13 +00:00
std::lock_guard lock(mutex);
auto table = DatabaseWithDictionaries::detachTableUnlocked(name);
table_name_to_path.erase(name);
detached_tables.emplace(table->getStorageID().uuid, table);
2020-03-31 20:38:05 +00:00
cleenupDetachedTables();
2020-03-23 00:12:13 +00:00
return table;
2019-11-11 11:34:03 +00:00
}
2019-10-23 13:46:38 +00:00
2020-03-20 00:07:52 +00:00
void DatabaseAtomic::dropTable(const Context &, const String & table_name, bool no_delay)
2020-01-22 11:30:11 +00:00
{
String table_metadata_path = getObjectMetadataPath(table_name);
2020-03-23 00:12:13 +00:00
String table_metadata_path_drop;
StoragePtr table;
{
std::lock_guard lock(mutex);
table = getTableUnlocked(table_name);
table_metadata_path_drop = DatabaseCatalog::instance().getPathForDroppedMetadata(table->getStorageID());
Poco::File(table_metadata_path).renameTo(table_metadata_path_drop);
DatabaseWithDictionaries::detachTableUnlocked(table_name); /// Should never throw
table_name_to_path.erase(table_name);
}
2020-03-20 00:07:52 +00:00
DatabaseCatalog::instance().enqueueDroppedTableCleanup(table->getStorageID(), table, table_metadata_path_drop, no_delay);
2020-01-22 11:30:11 +00:00
}
2019-11-11 14:28:28 +00:00
void DatabaseAtomic::renameTable(const Context & context, const String & table_name, IDatabase & to_database,
2020-03-31 20:38:05 +00:00
const String & to_table_name, bool exchange)
2019-11-11 14:28:28 +00:00
{
if (typeid(*this) != typeid(to_database))
2019-12-02 19:11:18 +00:00
{
if (!typeid_cast<DatabaseOrdinary *>(&to_database))
throw Exception("Moving tables between databases of different engines is not supported", ErrorCodes::NOT_IMPLEMENTED);
2020-03-31 20:38:05 +00:00
/// Allow moving tables between Atomic and Ordinary (with table lock)
DatabaseOnDisk::renameTable(context, table_name, to_database, to_table_name, exchange);
2019-12-02 19:11:18 +00:00
return;
}
2020-03-31 20:38:05 +00:00
auto & other_db = dynamic_cast<DatabaseAtomic &>(to_database);
2019-11-11 14:28:28 +00:00
StoragePtr table = tryGetTable(context, table_name);
2020-03-31 20:38:05 +00:00
StoragePtr other_table;
if (exchange)
other_table = other_db.tryGetTable(context, to_table_name);
2019-11-11 14:28:28 +00:00
if (!table)
throw Exception("Table " + backQuote(getDatabaseName()) + "." + backQuote(table_name) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
2020-03-31 20:38:05 +00:00
if (exchange && !other_table)
throw Exception("Table " + backQuote(other_db.getDatabaseName()) + "." + backQuote(to_table_name) + " doesn't exist.", ErrorCodes::UNKNOWN_TABLE);
2019-11-11 14:28:28 +00:00
2020-03-23 00:12:13 +00:00
String old_metadata_path = getObjectMetadataPath(table_name);
2020-03-23 22:40:40 +00:00
String new_metadata_path = to_database.getObjectMetadataPath(to_table_name);
2019-11-11 14:28:28 +00:00
2020-03-31 20:38:05 +00:00
auto detach = [](DatabaseAtomic & db, const String & table_name_)
2020-03-23 00:12:13 +00:00
{
2020-03-31 20:38:05 +00:00
auto table_data_path_ = db.table_name_to_path.find(table_name_)->second;
db.tables.erase(table_name_);
db.table_name_to_path.erase(table_name_);
return table_data_path_;
};
auto attach = [](DatabaseAtomic & db, const String & table_name_, const String & table_data_path_, const StoragePtr & table_)
{
db.tables.emplace(table_name_, table_);
db.table_name_to_path.emplace(table_name_, table_data_path_);
};
String table_data_path;
String other_table_data_path;
bool inside_database = this == &other_db;
if (inside_database && table_name == to_table_name)
return;
std::unique_lock<std::mutex> db_lock;
std::unique_lock<std::mutex> other_db_lock;
if (inside_database)
db_lock = std::unique_lock{mutex};
else if (this < &other_db)
{
db_lock = std::unique_lock{mutex};
other_db_lock = std::unique_lock{other_db.mutex};
2020-03-23 00:12:13 +00:00
}
else
{
2020-03-31 20:38:05 +00:00
other_db_lock = std::unique_lock{other_db.mutex};
db_lock = std::unique_lock{mutex};
2020-03-23 00:12:13 +00:00
}
2020-03-31 20:38:05 +00:00
if (exchange)
renameExchange(old_metadata_path, new_metadata_path);
else
renameNoReplace(old_metadata_path, new_metadata_path);
table_data_path = detach(*this, table_name);
if (exchange)
other_table_data_path = detach(other_db, to_table_name);
table->renameInMemory(other_db.getDatabaseName(), to_table_name);
if (exchange)
other_table->renameInMemory(getDatabaseName(), table_name);
if (!inside_database)
{
DatabaseCatalog::instance().updateUUIDMapping(table->getStorageID().uuid, other_db.shared_from_this(), table);
if (exchange)
DatabaseCatalog::instance().updateUUIDMapping(other_table->getStorageID().uuid, shared_from_this(), other_table);
}
attach(other_db, to_table_name, table_data_path, table);
if (exchange)
attach(*this, table_name, other_table_data_path, other_table);
2019-11-11 14:28:28 +00:00
}
2020-01-22 11:30:11 +00:00
void DatabaseAtomic::loadStoredObjects(Context & context, bool has_force_restore_data_flag)
{
DatabaseOrdinary::loadStoredObjects(context, has_force_restore_data_flag);
}
void DatabaseAtomic::shutdown()
{
DatabaseWithDictionaries::shutdown();
}
2020-03-23 00:12:13 +00:00
void DatabaseAtomic::commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table,
const String & table_metadata_tmp_path, const String & table_metadata_path)
{
auto table_data_path = getTableDataPath(query);
try
{
std::lock_guard lock{mutex};
assertDetachedTableNotInUse(query.uuid);
2020-03-23 00:12:13 +00:00
renameNoReplace(table_metadata_tmp_path, table_metadata_path);
attachTableUnlocked(query.table, table, table_data_path); /// Should never throw
table_name_to_path.emplace(query.table, table_data_path);
}
catch (...)
{
Poco::File(table_metadata_tmp_path).remove();
throw;
}
}
void DatabaseAtomic::commitAlterTable(const StorageID & table_id, const String & table_metadata_tmp_path, const String & table_metadata_path)
{
SCOPE_EXIT({ Poco::File(table_metadata_tmp_path).remove(); });
std::lock_guard lock{mutex};
auto actual_table_id = getTableUnlocked(table_id.table_name)->getStorageID();
if (table_id.uuid != actual_table_id.uuid)
throw Exception("Cannot alter table because it was renamed", ErrorCodes::CANNOT_ASSIGN_ALTER);
renameExchange(table_metadata_tmp_path, table_metadata_path);
}
void DatabaseAtomic::assertDetachedTableNotInUse(const UUID & uuid)
{
cleenupDetachedTables();
if (detached_tables.count(uuid))
throw Exception("Cannot attach table with UUID " + toString(uuid) +
", because it was detached but still used by come query. Retry later.", ErrorCodes::TABLE_ALREADY_EXISTS);
}
void DatabaseAtomic::cleenupDetachedTables()
{
auto it = detached_tables.begin();
while (it != detached_tables.end())
{
if (it->second.unique())
it = detached_tables.erase(it);
else
++it;
}
}
2020-04-01 22:41:29 +00:00
DatabaseTablesIteratorPtr DatabaseAtomic::getTablesIterator(const IDatabase::FilterByNameFunction & filter_by_table_name)
{
auto base_iter = DatabaseWithOwnTablesBase::getTablesIterator(filter_by_table_name);
return std::make_unique<AtomicDatabaseTablesSnapshotIterator>(std::move(typeid_cast<DatabaseTablesSnapshotIterator &>(*base_iter)));
}
DatabaseTablesIteratorPtr DatabaseAtomic::getTablesWithDictionaryTablesIterator(const IDatabase::FilterByNameFunction & filter_by_dictionary_name)
{
auto base_iter = DatabaseWithDictionaries::getTablesWithDictionaryTablesIterator(filter_by_dictionary_name);
return std::make_unique<AtomicDatabaseTablesSnapshotIterator>(std::move(typeid_cast<DatabaseTablesSnapshotIterator &>(*base_iter)));
}
2019-10-23 13:46:38 +00:00
}