2019-10-23 13:46:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <Databases/DatabasesCommon.h>
|
|
|
|
#include <Databases/DatabaseOrdinary.h>
|
|
|
|
|
2021-01-10 02:48:57 +00:00
|
|
|
|
2019-10-23 13:46:38 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-04-22 20:43:10 +00:00
|
|
|
/// All tables in DatabaseAtomic have persistent UUID and store data in
|
|
|
|
/// /clickhouse_path/store/xxx/xxxyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy/
|
|
|
|
/// where xxxyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy is UUID of the table.
|
|
|
|
/// RENAMEs are performed without changing UUID and moving table data.
|
|
|
|
/// Tables in Atomic databases can be accessed by UUID through DatabaseCatalog.
|
|
|
|
/// On DROP TABLE no data is removed, DatabaseAtomic just marks table as dropped
|
|
|
|
/// by moving metadata to /clickhouse_path/metadata_dropped/ and notifies DatabaseCatalog.
|
|
|
|
/// Running queries still may use dropped table. Table will be actually removed when it's not in use.
|
|
|
|
/// Allows to execute RENAME and DROP without IStorage-level RWLocks
|
2019-11-11 11:34:03 +00:00
|
|
|
class DatabaseAtomic : public DatabaseOrdinary
|
2019-10-23 13:46:38 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-04-10 23:33:54 +00:00
|
|
|
DatabaseAtomic(String name_, String metadata_path_, UUID uuid, const String & logger_name, ContextPtr context_);
|
|
|
|
DatabaseAtomic(String name_, String metadata_path_, UUID uuid, ContextPtr context_);
|
2019-10-23 13:46:38 +00:00
|
|
|
|
|
|
|
String getEngineName() const override { return "Atomic"; }
|
2020-07-02 20:39:31 +00:00
|
|
|
UUID getUUID() const override { return db_uuid; }
|
2019-10-23 13:46:38 +00:00
|
|
|
|
2021-11-02 12:58:45 +00:00
|
|
|
void renameDatabase(ContextPtr query_context, const String & new_name) override;
|
2019-10-23 13:46:38 +00:00
|
|
|
|
2019-11-11 14:28:28 +00:00
|
|
|
void renameTable(
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2019-11-11 14:28:28 +00:00
|
|
|
const String & table_name,
|
|
|
|
IDatabase & to_database,
|
2020-03-31 20:38:05 +00:00
|
|
|
const String & to_table_name,
|
2020-07-16 21:41:26 +00:00
|
|
|
bool exchange,
|
|
|
|
bool dictionary) override;
|
2019-11-11 14:28:28 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void dropTable(ContextPtr context, const String & table_name, bool no_delay) override;
|
2020-01-22 11:30:11 +00:00
|
|
|
|
2020-03-20 12:45:06 +00:00
|
|
|
void attachTable(const String & name, const StoragePtr & table, const String & relative_table_path) override;
|
2019-11-11 11:34:03 +00:00
|
|
|
StoragePtr detachTable(const String & name) override;
|
|
|
|
|
2019-12-25 21:17:49 +00:00
|
|
|
String getTableDataPath(const String & table_name) const override;
|
|
|
|
String getTableDataPath(const ASTCreateQuery & query) const override;
|
2019-11-11 11:34:03 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void drop(ContextPtr /*context*/) override;
|
2019-11-11 11:34:03 +00:00
|
|
|
|
2021-08-03 09:18:09 +00:00
|
|
|
DatabaseTablesIteratorPtr getTablesIterator(ContextPtr context, const FilterByNameFunction & filter_by_table_name) const override;
|
2020-04-01 22:41:29 +00:00
|
|
|
|
2021-09-13 19:11:16 +00:00
|
|
|
void loadStoredObjects(ContextMutablePtr context, bool force_restore, bool force_attach, bool skip_startup_tables) override;
|
2020-04-10 00:08:43 +00:00
|
|
|
|
2021-09-13 19:11:16 +00:00
|
|
|
void beforeLoadingMetadata(ContextMutablePtr context, bool force_restore, bool force_attach) override;
|
2021-08-31 08:53:48 +00:00
|
|
|
|
|
|
|
void startupTables(ThreadPool & thread_pool, bool force_restore, bool force_attach) override;
|
|
|
|
|
2020-04-23 18:00:43 +00:00
|
|
|
/// Atomic database cannot be detached if there is detached table which still in use
|
2020-09-14 19:25:02 +00:00
|
|
|
void assertCanBeDetached(bool cleanup) override;
|
2020-04-10 23:02:15 +00:00
|
|
|
|
2020-04-13 14:09:56 +00:00
|
|
|
UUID tryGetTableUUID(const String & table_name) const override;
|
|
|
|
|
2020-11-01 17:38:43 +00:00
|
|
|
void tryCreateSymlink(const String & table_name, const String & actual_data_path, bool if_data_path_exist = false);
|
2020-07-06 08:30:11 +00:00
|
|
|
void tryRemoveSymlink(const String & table_name);
|
|
|
|
|
2021-04-20 11:22:02 +00:00
|
|
|
void waitDetachedTableNotInUse(const UUID & uuid) override;
|
|
|
|
void checkDetachedTableNotInUse(const UUID & uuid) override;
|
2021-02-11 22:23:40 +00:00
|
|
|
void setDetachedTableNotInUseForce(const UUID & uuid);
|
2020-09-29 13:42:58 +00:00
|
|
|
|
2020-11-20 16:06:27 +00:00
|
|
|
protected:
|
2021-04-10 23:33:54 +00:00
|
|
|
void commitAlterTable(const StorageID & table_id, const String & table_metadata_tmp_path, const String & table_metadata_path, const String & statement, ContextPtr query_context) override;
|
2020-03-23 00:12:13 +00:00
|
|
|
void commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table,
|
2021-04-10 23:33:54 +00:00
|
|
|
const String & table_metadata_tmp_path, const String & table_metadata_path, ContextPtr query_context) override;
|
2020-01-27 20:31:39 +00:00
|
|
|
|
2020-03-30 19:15:18 +00:00
|
|
|
void assertDetachedTableNotInUse(const UUID & uuid);
|
2021-04-10 23:33:54 +00:00
|
|
|
using DetachedTables = std::unordered_map<UUID, StoragePtr>;
|
2020-09-29 13:42:58 +00:00
|
|
|
[[nodiscard]] DetachedTables cleanupDetachedTables();
|
2020-03-30 19:15:18 +00:00
|
|
|
|
2020-07-07 12:11:58 +00:00
|
|
|
void tryCreateMetadataSymlink();
|
|
|
|
|
2020-01-27 20:31:39 +00:00
|
|
|
//TODO store path in DatabaseWithOwnTables::tables
|
2021-04-10 23:33:54 +00:00
|
|
|
using NameToPathMap = std::unordered_map<String, String>;
|
2020-04-10 00:08:43 +00:00
|
|
|
NameToPathMap table_name_to_path;
|
2020-01-27 20:31:39 +00:00
|
|
|
|
2020-04-06 23:22:44 +00:00
|
|
|
DetachedTables detached_tables;
|
2020-07-07 12:11:58 +00:00
|
|
|
String path_to_table_symlinks;
|
|
|
|
String path_to_metadata_symlink;
|
2020-07-02 20:39:31 +00:00
|
|
|
const UUID db_uuid;
|
2019-10-23 13:46:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|