2020-04-05 12:18:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-13 14:44:01 +00:00
|
|
|
#include <Databases/DatabaseAtomic.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>
|
2020-07-04 16:32:23 +00:00
|
|
|
#include <DataStreams/BlockIO.h>
|
|
|
|
#include <DataStreams/OneBlockInputStream.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>;
|
|
|
|
|
2020-05-13 14:44:01 +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_,
|
2020-11-27 14:04:03 +00:00
|
|
|
const Context & 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"; }
|
|
|
|
|
|
|
|
/// If current query is initial, then the following methods add metadata updating ZooKeeper operations to current MetadataTransaction.
|
2020-11-29 11:45:32 +00:00
|
|
|
void dropTable(const Context &, const String & table_name, bool no_delay) override;
|
|
|
|
void renameTable(const Context & context, const String & table_name, IDatabase & to_database,
|
|
|
|
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,
|
|
|
|
const Context & query_context) override;
|
|
|
|
void commitAlterTable(const StorageID & table_id,
|
|
|
|
const String & table_metadata_tmp_path, const String & table_metadata_path,
|
|
|
|
const String & statement, const Context & query_context) override;
|
2021-02-08 09:46:30 +00:00
|
|
|
void createDictionary(const Context & context,
|
|
|
|
const String & dictionary_name,
|
|
|
|
const ASTPtr & query) override;
|
|
|
|
void removeDictionary(const Context & context, const String & dictionary_name) override;
|
2021-02-10 20:30:40 +00:00
|
|
|
void detachTablePermanently(const Context & context, const String & table_name) override;
|
2020-11-29 11:45:32 +00:00
|
|
|
|
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.
|
2021-02-04 19:41:44 +00:00
|
|
|
BlockIO propose(const ASTPtr & query, const Context & query_context);
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2021-02-04 19:41:44 +00:00
|
|
|
void stopReplication();
|
2020-07-04 16:32:23 +00:00
|
|
|
|
2021-02-08 09:14:17 +00:00
|
|
|
String getFullReplicaName() const;
|
|
|
|
static std::pair<String, String> parseFullReplicaName(const String & name);
|
|
|
|
|
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-02-15 20:00:59 +00:00
|
|
|
void drop(const Context & /*context*/) override;
|
|
|
|
|
|
|
|
void loadStoredObjects(Context & context, bool has_force_restore_data_flag, bool force_attach) override;
|
|
|
|
void shutdown() override;
|
|
|
|
|
2020-11-27 14:04:03 +00:00
|
|
|
friend struct DatabaseReplicatedTask;
|
|
|
|
friend class DatabaseReplicatedDDLWorker;
|
2020-04-05 12:18:51 +00:00
|
|
|
private:
|
2021-02-15 00:04:46 +00:00
|
|
|
void tryConnectToZooKeeper(bool force_attach);
|
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
|
|
|
|
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
|
|
|
|
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;
|
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;
|
2020-11-27 14:04:03 +00:00
|
|
|
std::unique_ptr<DatabaseReplicatedDDLWorker> ddl_worker;
|
2020-04-05 12:18:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|