2020-04-05 12:18:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-13 14:44:01 +00:00
|
|
|
#include <Databases/DatabaseAtomic.h>
|
2021-02-19 23:41:58 +00:00
|
|
|
#include <Databases/DatabaseReplicatedSettings.h>
|
2020-04-05 12:18:51 +00:00
|
|
|
#include <Common/ZooKeeper/ZooKeeper.h>
|
2020-05-13 17:00:47 +00:00
|
|
|
#include <Core/BackgroundSchedulePool.h>
|
2021-10-15 20:18:20 +00:00
|
|
|
#include <QueryPipeline/BlockIO.h>
|
2020-10-20 16:14:54 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2020-06-27 13:39:41 +00:00
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
2020-11-05 09:52:23 +00:00
|
|
|
|
2020-11-27 14:04:03 +00:00
|
|
|
class DatabaseReplicatedDDLWorker;
|
2020-11-19 10:34:45 +00:00
|
|
|
using ZooKeeperPtr = std::shared_ptr<zkutil::ZooKeeper>;
|
2020-11-05 09:52:23 +00:00
|
|
|
|
2021-02-08 09:14:17 +00:00
|
|
|
class Cluster;
|
|
|
|
using ClusterPtr = std::shared_ptr<Cluster>;
|
|
|
|
|
2021-11-11 20:35:26 +00:00
|
|
|
class DatabaseReplicated : public DatabaseAtomic
|
2020-04-05 12:18:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-10-27 09:19:45 +00:00
|
|
|
DatabaseReplicated(const String & name_, const String & metadata_path_, UUID uuid,
|
|
|
|
const String & zookeeper_path_, const String & shard_name_, const String & replica_name_,
|
2021-02-19 23:41:58 +00:00
|
|
|
DatabaseReplicatedSettings db_settings_,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context);
|
2020-06-27 13:39:41 +00:00
|
|
|
|
2020-11-05 09:52:23 +00:00
|
|
|
~DatabaseReplicated() override;
|
|
|
|
|
2021-02-15 20:00:59 +00:00
|
|
|
String getEngineName() const override { return "Replicated"; }
|
|
|
|
|
2021-02-19 23:41:58 +00:00
|
|
|
/// If current query is initial, then the following methods add metadata updating ZooKeeper operations to current ZooKeeperMetadataTransaction.
|
2022-06-23 07:59:13 +00:00
|
|
|
void dropTable(ContextPtr, const String & table_name, bool sync) override;
|
2021-04-10 23:33:54 +00:00
|
|
|
void renameTable(ContextPtr context, const String & table_name, IDatabase & to_database,
|
2020-11-29 11:45:32 +00:00
|
|
|
const String & to_table_name, bool exchange, bool dictionary) override;
|
|
|
|
void commitCreateTable(const ASTCreateQuery & query, const StoragePtr & table,
|
|
|
|
const String & table_metadata_tmp_path, const String & table_metadata_path,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr query_context) override;
|
2020-11-29 11:45:32 +00:00
|
|
|
void commitAlterTable(const StorageID & table_id,
|
|
|
|
const String & table_metadata_tmp_path, const String & table_metadata_path,
|
2021-04-10 23:33:54 +00:00
|
|
|
const String & statement, ContextPtr query_context) override;
|
|
|
|
void detachTablePermanently(ContextPtr context, const String & table_name) override;
|
|
|
|
void removeDetachedPermanentlyFlag(ContextPtr context, const String & table_name, const String & table_metadata_path, bool attach) const override;
|
2020-11-29 11:45:32 +00:00
|
|
|
|
2022-05-01 13:40:18 +00:00
|
|
|
bool waitForReplicaToProcessAllEntries(UInt64 timeout_ms);
|
|
|
|
|
2021-02-15 20:00:59 +00:00
|
|
|
/// Try to execute DLL query on current host as initial query. If query is succeed,
|
|
|
|
/// then it will be executed on all replicas.
|
2022-04-25 08:21:44 +00:00
|
|
|
BlockIO tryEnqueueReplicatedDDL(const ASTPtr & query, ContextPtr query_context, bool internal = false);
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2021-11-11 20:35:26 +00:00
|
|
|
bool hasReplicationThread() const override { return true; }
|
|
|
|
|
2021-11-10 13:13:27 +00:00
|
|
|
void stopReplication() override;
|
2020-07-04 16:32:23 +00:00
|
|
|
|
2021-11-23 09:41:54 +00:00
|
|
|
String getShardName() const { return shard_name; }
|
|
|
|
String getReplicaName() const { return replica_name; }
|
2021-02-08 09:14:17 +00:00
|
|
|
String getFullReplicaName() const;
|
|
|
|
static std::pair<String, String> parseFullReplicaName(const String & name);
|
|
|
|
|
2022-04-19 18:15:27 +00:00
|
|
|
const String & getZooKeeperPath() const { return zookeeper_path; }
|
|
|
|
|
2021-02-15 20:00:59 +00:00
|
|
|
/// Returns cluster consisting of database replicas
|
2021-02-08 09:14:17 +00:00
|
|
|
ClusterPtr getCluster() const;
|
2020-11-27 14:04:03 +00:00
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void drop(ContextPtr /*context*/) override;
|
2021-02-15 20:00:59 +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;
|
2021-08-31 08:53:48 +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;
|
|
|
|
|
2021-02-15 20:00:59 +00:00
|
|
|
void shutdown() override;
|
|
|
|
|
2022-06-24 19:29:38 +00:00
|
|
|
std::vector<std::pair<ASTPtr, StoragePtr>> getTablesForBackup(const FilterByNameFunction & filter, const ContextPtr & local_context) const override;
|
2022-06-22 22:56:41 +00:00
|
|
|
void createTableRestoredFromBackup(const ASTPtr & create_table_query, ContextMutablePtr local_context, std::shared_ptr<IRestoreCoordination> restore_coordination, UInt64 timeout_ms) override;
|
2022-05-31 09:33:23 +00:00
|
|
|
|
2020-11-27 14:04:03 +00:00
|
|
|
friend struct DatabaseReplicatedTask;
|
|
|
|
friend class DatabaseReplicatedDDLWorker;
|
2020-04-05 12:18:51 +00:00
|
|
|
private:
|
2022-05-13 13:15:39 +00:00
|
|
|
void tryConnectToZooKeeperAndInitDatabase(bool force_attach, bool is_create_query);
|
2020-11-19 10:34:45 +00:00
|
|
|
bool createDatabaseNodesInZooKeeper(const ZooKeeperPtr & current_zookeeper);
|
|
|
|
void createReplicaNodesInZooKeeper(const ZooKeeperPtr & current_zookeeper);
|
2020-05-11 12:55:17 +00:00
|
|
|
|
2022-03-24 23:14:26 +00:00
|
|
|
struct
|
|
|
|
{
|
|
|
|
String cluster_username{"default"};
|
|
|
|
String cluster_password;
|
|
|
|
String cluster_secret;
|
|
|
|
bool cluster_secure_connection{false};
|
|
|
|
} cluster_auth_info;
|
|
|
|
|
|
|
|
void fillClusterAuthInfo(String collection_name, const Poco::Util::AbstractConfiguration & config);
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void checkQueryValid(const ASTPtr & query, ContextPtr query_context) const;
|
2021-03-16 20:01:20 +00:00
|
|
|
|
2021-02-09 15:14:20 +00:00
|
|
|
void recoverLostReplica(const ZooKeeperPtr & current_zookeeper, UInt32 our_log_ptr, UInt32 max_log_ptr);
|
|
|
|
std::map<String, String> tryGetConsistentMetadataSnapshot(const ZooKeeperPtr & zookeeper, UInt32 & max_log_ptr);
|
2020-11-19 10:34:45 +00:00
|
|
|
|
2020-11-27 14:04:03 +00:00
|
|
|
ASTPtr parseQueryFromMetadataInZooKeeper(const String & node_name, const String & query);
|
2021-02-10 20:30:40 +00:00
|
|
|
String readMetadataFile(const String & table_name) const;
|
2020-11-27 14:04:03 +00:00
|
|
|
|
2021-03-09 17:05:24 +00:00
|
|
|
ClusterPtr getClusterImpl() const;
|
|
|
|
void setCluster(ClusterPtr && new_cluster);
|
|
|
|
|
2021-05-31 13:31:03 +00:00
|
|
|
void createEmptyLogEntry(const ZooKeeperPtr & current_zookeeper);
|
2021-03-09 17:05:24 +00:00
|
|
|
|
2022-04-13 12:38:20 +00:00
|
|
|
bool allowMoveTableToOtherDatabaseEngine(IDatabase & to_database) const override
|
|
|
|
{
|
|
|
|
return is_recovering && typeid_cast<DatabaseAtomic *>(&to_database);
|
|
|
|
}
|
|
|
|
|
2020-10-27 09:19:45 +00:00
|
|
|
String zookeeper_path;
|
|
|
|
String shard_name;
|
|
|
|
String replica_name;
|
2020-11-13 18:35:45 +00:00
|
|
|
String replica_path;
|
2021-02-19 23:41:58 +00:00
|
|
|
DatabaseReplicatedSettings db_settings;
|
2020-10-27 09:19:45 +00:00
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
zkutil::ZooKeeperPtr getZooKeeper() const;
|
|
|
|
|
2021-02-15 00:04:46 +00:00
|
|
|
std::atomic_bool is_readonly = true;
|
2022-05-13 13:15:39 +00:00
|
|
|
std::atomic_bool is_probably_dropped = false;
|
2022-04-14 12:48:24 +00:00
|
|
|
std::atomic_bool is_recovering = false;
|
2020-11-27 14:04:03 +00:00
|
|
|
std::unique_ptr<DatabaseReplicatedDDLWorker> ddl_worker;
|
2021-09-24 10:51:52 +00:00
|
|
|
UInt32 max_log_ptr_at_creation = 0;
|
2021-03-09 17:05:24 +00:00
|
|
|
|
|
|
|
mutable ClusterPtr cluster;
|
2020-04-05 12:18:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|