DDLWorker.h 2.7 KB
Newer Older
1 2
#pragma once
#include <Interpreters/Context.h>
3 4
#include <Interpreters/Cluster.h>
#include <DataStreams/BlockIO.h>
5 6 7 8 9 10 11 12 13 14 15
#include <common/logger_useful.h>

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <thread>

namespace DB
{

16
class ASTAlterQuery;
17
struct DDLLogEntry;
18
struct DDLTask;
19 20


21
BlockIO executeDDLQueryOnCluster(const ASTPtr & query_ptr, Context & context);
22 23


24 25 26
class DDLWorker
{
public:
27
    DDLWorker(const std::string & zk_root_dir, Context & context_);
28 29
    ~DDLWorker();

30
    /// Pushes query into DDL queue, returns path to created node
31
    String enqueueQuery(DDLLogEntry & entry);
32

33
    /// Host ID (name:port) for logging purposes
34
    /// Note that in each task hosts are identified individually by name:port from initiator server cluster config
35
    std::string getCommonHostID() const
36
    {
37
        return host_fqdn_id;
38 39
    }

40 41
private:
    void processTasks();
42

43 44 45
    bool initAndCheckTask(DDLTask & task, const String & entry_name);


46
    void processTask(DDLTask & task);
47 48

    void processTaskAlter(
49
        DDLTask & task,
50
        const ASTAlterQuery * ast_alter,
51 52
        const String & rewritten_query,
        const String & node_path);
53

54 55
    void parseQueryAndResolveHost(DDLTask & task);

56 57
    bool tryExecuteQuery(const String & query, const DDLTask & task, ExecutionStatus & status);

58

59 60
    /// Checks and cleanups queue's nodes
    void cleanupQueue(const Strings * node_names_to_check = nullptr);
61

62

63 64 65
    void createStatusDirs(const std::string & node_name);
    ASTPtr getRewrittenQuery(const DDLLogEntry & node);

66

67 68 69 70 71 72
    void run();

private:
    Context & context;
    Logger * log = &Logger::get("DDLWorker");

73 74
    std::string host_fqdn;      /// current host domain name
    std::string host_fqdn_id;   /// host_name:port
75

76
    std::string queue_dir;      /// dir with queue of queries
77 78
    std::string master_dir;     /// dir with queries was initiated by the server

79 80
    /// Last task that was skipped or sucesfully executed
    std::string last_processed_task_name;
81

82 83 84
    std::shared_ptr<zkutil::ZooKeeper> zookeeper;

    /// Save state of executed task to avoid duplicate execution on ZK error
85 86
    using DDLTaskPtr = std::unique_ptr<DDLTask>;
    DDLTaskPtr current_task;
87

88
    std::shared_ptr<Poco::Event> event_queue_updated;
89
    std::atomic<bool> stop_flag{false};
90
    std::thread thread;
91

92
    size_t last_cleanup_time_seconds = 0;
93 94 95 96 97

    /// Delete node if its age is greater than that
    static const size_t node_max_lifetime_seconds;
    /// Cleaning starts after new node event is received if the last cleaning wasn't made sooner than N seconds ago
    static const size_t cleanup_min_period_seconds;
98

99
    friend class DDLQueryStatusInputSream;
100
    friend class DDLTask;
101 102
};

103

104
}