DDLWorker.h 1.5 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 17 18
BlockIO executeDDLQueryOnCluster(const String & query, const String & cluster_name, Context & context);


19 20 21
struct DDLLogEntry;


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

28 29
    /// Push query into DDL query, return path to created node
    String enqueueQuery(DDLLogEntry & entry);
30 31 32 33 34 35

    std::string getHostName() const
    {
        return hostname;
    }

36 37
private:
    void processTasks();
38 39 40
    bool processTask(const DDLLogEntry & node, const std::string & node_path);

    void createStatusDirs(const std::string & node_name);
41 42 43

    void processQueries();
    bool processQuery(const std::string & task);
44 45 46 47 48 49 50

    void run();

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

51 52
    std::string hostname;
    std::string root_dir;       /// common dir with queue of queries
53 54 55 56 57
    std::string master_dir;     /// dir with queries was initiated by the server

    std::string last_processed_node_name;

    std::shared_ptr<zkutil::ZooKeeper> zookeeper;
58
    std::shared_ptr<Poco::Event> event_queue_updated;
59 60 61 62 63

    std::atomic<bool> stop_flag;
    std::condition_variable cond_var;
    std::mutex lock;
    std::thread thread;
64 65

    friend class DDLQueryStatusInputSream;
66 67 68
};

}