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-04-05 12:18:51 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2020-05-27 15:04:10 +00:00
|
|
|
/** DatabaseReplicated engine
|
|
|
|
* supports replication of metadata
|
|
|
|
* via DDL log being written to ZooKeeper
|
|
|
|
* and executed on all of the replicas
|
|
|
|
* for a given database.
|
|
|
|
*
|
|
|
|
* One Clickhouse server can have multiple
|
|
|
|
* replicated databases running and updating
|
|
|
|
* at the same time.
|
|
|
|
*
|
|
|
|
* The engine has two parameters ZooKeeper path and
|
|
|
|
* replica name.
|
|
|
|
* The same ZooKeeper path corresponds to the same
|
2020-06-20 15:39:58 +00:00
|
|
|
* database. Replica names MUST be different for all replicas
|
2020-05-27 15:04:10 +00:00
|
|
|
* of the same database.
|
|
|
|
*
|
|
|
|
* Using this engine, creation of Replicated tables
|
|
|
|
* requires no ZooKeeper path and replica name parameters.
|
|
|
|
* Table's replica name is the same as database replica name.
|
2020-06-20 15:39:58 +00:00
|
|
|
* Table's ZooKeeper path is a concatenation of database
|
2020-05-27 15:04:10 +00:00
|
|
|
* ZooKeeper path, /tables/, and UUID of the table.
|
2020-04-05 12:18:51 +00:00
|
|
|
*/
|
2020-05-13 14:44:01 +00:00
|
|
|
class DatabaseReplicated : public DatabaseAtomic
|
2020-04-05 12:18:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-05-05 14:16:59 +00:00
|
|
|
DatabaseReplicated(const String & name_, const String & metadata_path_, const String & zookeeper_path_, const String & replica_name_, Context & context);
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2020-06-20 15:39:58 +00:00
|
|
|
void drop(const Context & /*context*/) override;
|
|
|
|
|
2020-05-05 14:16:59 +00:00
|
|
|
String getEngineName() const override { return "Replicated"; }
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2020-05-05 14:16:59 +00:00
|
|
|
void propose(const ASTPtr & query) override;
|
2020-04-05 12:18:51 +00:00
|
|
|
|
2020-05-13 17:00:47 +00:00
|
|
|
String zookeeper_path;
|
|
|
|
String replica_name;
|
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
private:
|
2020-05-24 17:13:53 +00:00
|
|
|
void createDatabaseZKNodes();
|
2020-05-11 12:55:17 +00:00
|
|
|
|
2020-05-24 17:13:53 +00:00
|
|
|
void runBackgroundLogExecutor();
|
2020-06-20 15:39:58 +00:00
|
|
|
|
2020-05-24 17:13:53 +00:00
|
|
|
void executeFromZK(String & path);
|
2020-05-11 12:55:17 +00:00
|
|
|
|
2020-05-13 17:00:47 +00:00
|
|
|
void saveState();
|
2020-06-20 15:39:58 +00:00
|
|
|
|
2020-05-24 17:13:53 +00:00
|
|
|
void loadMetadataFromSnapshot();
|
2020-06-20 15:39:58 +00:00
|
|
|
void createSnapshot();
|
|
|
|
void RemoveOutdatedSnapshotsAndLog();
|
2020-05-13 17:00:47 +00:00
|
|
|
|
2020-05-11 12:55:17 +00:00
|
|
|
std::unique_ptr<Context> current_context; // to run executeQuery
|
|
|
|
|
2020-06-20 15:39:58 +00:00
|
|
|
int snapshot_period;
|
2020-05-11 12:55:17 +00:00
|
|
|
|
2020-06-07 11:20:05 +00:00
|
|
|
String last_executed_log_entry = "";
|
|
|
|
|
2020-05-24 17:13:53 +00:00
|
|
|
BackgroundSchedulePool::TaskHolder background_log_executor;
|
2020-05-11 12:55:17 +00:00
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
zkutil::ZooKeeperPtr current_zookeeper; /// Use only the methods below.
|
|
|
|
mutable std::mutex current_zookeeper_mutex; /// To recreate the session in the background thread.
|
|
|
|
|
|
|
|
zkutil::ZooKeeperPtr tryGetZooKeeper() const;
|
|
|
|
zkutil::ZooKeeperPtr getZooKeeper() const;
|
|
|
|
void setZooKeeper(zkutil::ZooKeeperPtr zookeeper);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|