ClickHouse/dbms/src/Interpreters/DDLWorker.h

112 lines
3.1 KiB
C++
Raw Normal View History

#pragma once
#include <Interpreters/Context.h>
#include <Interpreters/Cluster.h>
#include <DataStreams/BlockIO.h>
#include <Common/CurrentThread.h>
#include <common/logger_useful.h>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>
namespace DB
{
class ASTAlterQuery;
struct DDLLogEntry;
struct DDLTask;
/// Pushes distributed DDL query to the queue
BlockIO executeDDLQueryOnCluster(const ASTPtr & query_ptr, const Context & context, const NameSet & query_databases);
class DDLWorker
{
public:
DDLWorker(const std::string & zk_root_dir, Context & context_, const Poco::Util::AbstractConfiguration * config, const String & prefix);
~DDLWorker();
2017-04-21 12:39:28 +00:00
/// Pushes query into DDL queue, returns path to created node
String enqueueQuery(DDLLogEntry & entry);
/// Host ID (name:port) for logging purposes
2017-07-28 16:14:49 +00:00
/// Note that in each task hosts are identified individually by name:port from initiator server cluster config
std::string getCommonHostID() const
{
return host_fqdn_id;
}
private:
void processTasks();
/// Reads entry and check that the host belongs to host list of the task
/// Returns true and sets current_task if entry parsed and the check is passed
bool initAndCheckTask(const String & entry_name, String & out_reason);
2017-07-28 16:14:49 +00:00
void processTask(DDLTask & task);
void processTaskAlter(
DDLTask & task,
2017-07-28 16:14:49 +00:00
const ASTAlterQuery * ast_alter,
const String & rewritten_query,
const String & node_path);
2017-07-28 16:14:49 +00:00
void parseQueryAndResolveHost(DDLTask & task);
bool tryExecuteQuery(const String & query, const DDLTask & task, ExecutionStatus & status);
2017-04-19 14:21:27 +00:00
/// Checks and cleanups queue's nodes
void cleanupQueue();
/// Init task node
2017-04-25 15:21:03 +00:00
void createStatusDirs(const std::string & node_name);
2017-07-28 16:14:49 +00:00
void run();
void attachToThreadGroup();
private:
Context & context;
Logger * log;
2018-08-27 11:03:22 +00:00
std::unique_ptr<Context> current_context;
std::string host_fqdn; /// current host domain name
std::string host_fqdn_id; /// host_name:port
std::string queue_dir; /// dir with queue of queries
/// Name of last task that was skipped or successfully executed
2017-07-28 16:14:49 +00:00
std::string last_processed_task_name;
std::shared_ptr<zkutil::ZooKeeper> zookeeper;
/// Save state of executed task to avoid duplicate execution on ZK error
2017-07-28 16:14:49 +00:00
using DDLTaskPtr = std::unique_ptr<DDLTask>;
DDLTaskPtr current_task;
std::shared_ptr<Poco::Event> event_queue_updated;
2017-05-31 14:01:08 +00:00
std::atomic<bool> stop_flag{false};
std::thread thread;
Int64 last_cleanup_time_seconds = 0;
2017-05-31 14:01:08 +00:00
/// Cleaning starts after new node event is received if the last cleaning wasn't made sooner than N seconds ago
Int64 cleanup_delay_period = 60; // minute (in seconds)
/// Delete node if its age is greater than that
Int64 task_max_lifetime = 7 * 24 * 60 * 60; // week (in seconds)
/// How many tasks could be in the queue
size_t max_tasks_in_queue = 1000;
2017-04-19 14:21:27 +00:00
ThreadGroupStatusPtr thread_group;
friend class DDLQueryStatusInputSream;
friend struct DDLTask;
};
}