DDLWorker.h 1.8 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
    void enqueueQuery(DDLLogEntry & entry);
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

    /// Returns root/ path in ZooKeeper
    std::string getRoot() const
    {
        return root_dir;
    }

    std::string getMastersDir() const
    {
        return root_dir + "/masters";
    }

    std::string getCurrentMasterDir() const
    {
        return getMastersDir() + "/" + getHostName();
    }

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

51 52
private:
    void processTasks();
53 54 55
    bool processTask(const DDLLogEntry & node, const std::string & node_path);

    void createStatusDirs(const std::string & node_name);
56 57 58

    void processQueries();
    bool processQuery(const std::string & task);
59 60 61 62 63 64 65

    void run();

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

66 67 68
    std::string hostname;
    std::string root_dir;       /// common dir with queue of queries
    std::string assign_dir;     /// dir with tasks assigned to the server
69 70 71 72 73 74
    std::string master_dir;     /// dir with queries was initiated by the server

    std::string last_processed_node_name;

    std::shared_ptr<zkutil::ZooKeeper> zookeeper;
    std::shared_ptr<Poco::Event> queue_updated;
75 76 77 78 79 80 81 82

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

}