2020-04-05 12:18:51 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-11 12:55:17 +00:00
|
|
|
#include <Databases/DatabaseOrdinary.h>
|
2020-04-05 12:18:51 +00:00
|
|
|
#include <Common/randomSeed.h>
|
|
|
|
#include <Common/ZooKeeper/ZooKeeper.h>
|
|
|
|
|
2020-05-11 12:55:17 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
/** Replicated database engine.
|
|
|
|
* It stores tables list using list of .sql files,
|
|
|
|
* that contain declaration of table represented by SQL ATTACH TABLE query
|
|
|
|
* and operation log in zookeeper
|
|
|
|
*/
|
2020-05-11 12:55:17 +00:00
|
|
|
class DatabaseReplicated : public DatabaseOrdinary
|
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-05-11 12:55:17 +00:00
|
|
|
~DatabaseReplicated();
|
2020-04-05 12:18:51 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
private:
|
2020-05-11 12:55:17 +00:00
|
|
|
|
|
|
|
void runMainThread();
|
|
|
|
void runCleanupThread();
|
|
|
|
|
|
|
|
void attachToThreadGroup();
|
|
|
|
|
|
|
|
void executeLog(size_t n);
|
|
|
|
|
|
|
|
std::unique_ptr<Context> current_context; // to run executeQuery
|
|
|
|
|
2020-05-11 13:31:14 +00:00
|
|
|
std::atomic<size_t> current_log_entry_n = 0;
|
2020-05-11 12:55:17 +00:00
|
|
|
std::atomic<bool> stop_flag{false};
|
|
|
|
|
|
|
|
ThreadFromGlobalPool main_thread;
|
|
|
|
ThreadGroupStatusPtr thread_group;
|
|
|
|
|
2020-04-05 12:18:51 +00:00
|
|
|
String zookeeper_path;
|
|
|
|
String replica_name;
|
|
|
|
String replica_path;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|