ClickHouse/src/Databases/DatabaseReplicated.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

148 lines
6.4 KiB
C++
Raw Normal View History

2020-04-05 12:18:51 +00:00
#pragma once
#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>
#include <Core/BackgroundSchedulePool.h>
2021-10-15 20:18:20 +00:00
#include <QueryPipeline/BlockIO.h>
#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-27 14:04:03 +00:00
class DatabaseReplicatedDDLWorker;
2020-11-19 10:34:45 +00:00
using ZooKeeperPtr = std::shared_ptr<zkutil::ZooKeeper>;
2021-02-08 09:14:17 +00:00
class Cluster;
using ClusterPtr = std::shared_ptr<Cluster>;
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_,
ContextPtr context);
2020-06-27 13:39:41 +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;
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,
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,
const String & statement, ContextPtr query_context) override;
void detachTablePermanently(ContextPtr context, const String & table_name) override;
2022-07-29 16:33:16 +00:00
void removeDetachedPermanentlyFlag(ContextPtr context, const String & table_name, const String & table_metadata_path, bool attach) override;
2020-11-29 11:45:32 +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-09-16 14:25:32 +00:00
BlockIO tryEnqueueReplicatedDDL(const ASTPtr & query, ContextPtr query_context, bool internal) override;
2020-04-05 12:18:51 +00:00
bool hasReplicationThread() const override { return true; }
void stopReplication() override;
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
2022-08-18 14:13:27 +00:00
ClusterPtr tryGetCluster() const;
2020-11-27 14:04:03 +00:00
void drop(ContextPtr /*context*/) override;
2021-02-15 20:00:59 +00:00
2022-07-20 20:54:43 +00:00
void loadStoredObjects(ContextMutablePtr context, LoadingStrictnessLevel mode, bool skip_startup_tables) override;
2021-08-31 08:53:48 +00:00
2022-07-20 20:54:43 +00:00
void beforeLoadingMetadata(ContextMutablePtr context, LoadingStrictnessLevel mode) override;
2021-08-31 08:53:48 +00:00
2022-07-20 20:54:43 +00:00
void startupTables(ThreadPool & thread_pool, LoadingStrictnessLevel mode) override;
2021-08-31 08:53:48 +00:00
2021-02-15 20:00:59 +00:00
void shutdown() override;
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;
bool shouldReplicateQuery(const ContextPtr & query_context, const ASTPtr & query_ptr) const override;
static void dropReplica(DatabaseReplicated * database, const String & database_zookeeper_path, const String & full_replica_name);
2020-11-27 14:04:03 +00:00
friend struct DatabaseReplicatedTask;
friend class DatabaseReplicatedDDLWorker;
2020-04-05 12:18:51 +00:00
private:
2022-07-20 20:54:43 +00:00
void tryConnectToZooKeeperAndInitDatabase(LoadingStrictnessLevel mode);
2020-11-19 10:34:45 +00:00
bool createDatabaseNodesInZooKeeper(const ZooKeeperPtr & current_zookeeper);
static bool looksLikeReplicatedDatabasePath(const ZooKeeperPtr & current_zookeeper, const String & path);
2020-11-19 10:34:45 +00:00
void createReplicaNodesInZooKeeper(const ZooKeeperPtr & current_zookeeper);
2020-05-11 12:55:17 +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);
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);
String readMetadataFile(const String & table_name) const;
2020-11-27 14:04:03 +00:00
ClusterPtr getClusterImpl() const;
void setCluster(ClusterPtr && new_cluster);
2021-05-31 13:31:03 +00:00
void createEmptyLogEntry(const ZooKeeperPtr & current_zookeeper);
bool allowMoveTableToOtherDatabaseEngine(IDatabase & to_database) const override
{
return is_recovering && typeid_cast<DatabaseAtomic *>(&to_database);
}
2022-07-29 16:33:16 +00:00
UInt64 getMetadataHash(const String & table_name) const;
2022-08-03 13:33:55 +00:00
bool checkDigestValid(const ContextPtr & local_context, bool debug_check = true) const TSA_REQUIRES(metadata_mutex);
2022-07-29 16:33:16 +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;
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;
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;
UInt32 max_log_ptr_at_creation = 0;
2022-07-29 16:33:16 +00:00
/// Usually operation with metadata are single-threaded because of the way replication works,
/// but StorageReplicatedMergeTree may call alterTable outside from DatabaseReplicatedDDLWorker causing race conditions.
std::mutex metadata_mutex;
2022-08-03 13:33:55 +00:00
/// Sum of hashes of pairs (table_name, table_create_statement).
/// We calculate this sum from local metadata files and compare it will value in ZooKeeper.
/// It allows to detect if metadata is broken and recover replica.
UInt64 tables_metadata_digest TSA_GUARDED_BY(metadata_mutex);
2022-07-29 16:33:16 +00:00
mutable ClusterPtr cluster;
2020-04-05 12:18:51 +00:00
};
}