2017-04-13 13:42:29 +00:00
|
|
|
#pragma once
|
|
|
|
#include <Interpreters/Context.h>
|
2017-04-13 16:12:56 +00:00
|
|
|
#include <Interpreters/Cluster.h>
|
|
|
|
#include <DataStreams/BlockIO.h>
|
2017-04-13 13:42:29 +00:00
|
|
|
#include <common/logger_useful.h>
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
struct ASTAlterQuery;
|
|
|
|
struct DDLLogEntry;
|
2017-04-13 16:12:56 +00:00
|
|
|
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
BlockIO executeDDLQueryOnCluster(const ASTPtr & query_ptr, Context & context);
|
2017-04-17 17:04:31 +00:00
|
|
|
|
|
|
|
|
2017-04-13 13:42:29 +00:00
|
|
|
class DDLWorker
|
|
|
|
{
|
|
|
|
public:
|
2017-04-13 16:12:56 +00:00
|
|
|
DDLWorker(const std::string & zk_root_dir, Context & context_);
|
2017-04-13 13:42:29 +00:00
|
|
|
~DDLWorker();
|
|
|
|
|
2017-04-21 12:39:28 +00:00
|
|
|
/// Pushes query into DDL queue, returns path to created node
|
2017-04-18 15:44:31 +00:00
|
|
|
String enqueueQuery(DDLLogEntry & entry);
|
2017-04-13 16:12:56 +00:00
|
|
|
|
|
|
|
std::string getHostName() const
|
|
|
|
{
|
2017-04-25 15:21:03 +00:00
|
|
|
return host_id;
|
2017-04-13 16:12:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-13 13:42:29 +00:00
|
|
|
private:
|
|
|
|
void processTasks();
|
2017-04-27 15:19:11 +00:00
|
|
|
|
|
|
|
void processTask(const DDLLogEntry & node, const std::string & node_path);
|
|
|
|
|
|
|
|
void processTaskAlter(
|
|
|
|
const ASTAlterQuery * query_alter,
|
|
|
|
const String & rewritten_query,
|
|
|
|
const std::shared_ptr<Cluster> & cluster,
|
|
|
|
ssize_t shard_num,
|
|
|
|
const String & node_path);
|
2017-04-17 17:04:31 +00:00
|
|
|
|
2017-04-19 14:21:27 +00:00
|
|
|
/// Checks and cleanups queue's nodes
|
|
|
|
void cleanupQueue(const Strings * node_names_to_check = nullptr);
|
2017-04-13 13:42:29 +00:00
|
|
|
|
2017-04-25 15:21:03 +00:00
|
|
|
void createStatusDirs(const std::string & node_name);
|
|
|
|
ASTPtr getRewrittenQuery(const DDLLogEntry & node);
|
|
|
|
|
2017-04-13 13:42:29 +00:00
|
|
|
void run();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Context & context;
|
|
|
|
Logger * log = &Logger::get("DDLWorker");
|
|
|
|
|
2017-04-25 15:21:03 +00:00
|
|
|
std::string host_id; /// host_name:port
|
|
|
|
std::string host_name;
|
|
|
|
UInt16 port;
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
std::string queue_dir; /// dir with queue of queries
|
2017-04-17 17:04:31 +00:00
|
|
|
std::string master_dir; /// dir with queries was initiated by the server
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
/// Used to omit already processed nodes;
|
2017-04-17 17:04:31 +00:00
|
|
|
std::string last_processed_node_name;
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
std::shared_ptr<zkutil::ZooKeeper> zookeeper;
|
|
|
|
|
|
|
|
/// Save state of executed task to avoid duplicate execution on ZK error
|
|
|
|
std::string current_node = {};
|
|
|
|
bool current_node_was_executed = false;
|
|
|
|
ExecutionStatus current_node_execution_status;
|
2017-04-13 13:42:29 +00:00
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
std::shared_ptr<Poco::Event> event_queue_updated;
|
2017-04-13 13:42:29 +00:00
|
|
|
std::atomic<bool> stop_flag;
|
|
|
|
std::thread thread;
|
2017-04-18 15:44:31 +00:00
|
|
|
|
2017-04-19 14:21:27 +00:00
|
|
|
size_t last_cleanup_time_seconds = 0;
|
2017-04-25 15:21:03 +00:00
|
|
|
static constexpr size_t node_max_lifetime_seconds = 60; // 7 * 24 * 60 * 60;
|
2017-04-27 15:19:11 +00:00
|
|
|
static constexpr size_t cleanup_min_period_seconds = 60;
|
2017-04-19 14:21:27 +00:00
|
|
|
|
2017-04-18 15:44:31 +00:00
|
|
|
friend class DDLQueryStatusInputSream;
|
2017-04-13 13:42:29 +00:00
|
|
|
};
|
|
|
|
|
2017-04-27 15:19:11 +00:00
|
|
|
|
2017-04-13 13:42:29 +00:00
|
|
|
}
|