ClickHouse/dbms/src/Common/Config/ConfigReloader.h

91 lines
2.4 KiB
C++
Raw Normal View History

2016-01-17 13:34:36 +00:00
#pragma once
#include "ConfigProcessor.h"
#include <Common/ZooKeeper/Common.h>
#include <Common/ZooKeeper/ZooKeeperNodeCache.h>
2016-01-17 13:34:36 +00:00
#include <time.h>
#include <string>
#include <thread>
2016-10-23 10:52:32 +00:00
#include <mutex>
#include <condition_variable>
#include <list>
2016-01-17 13:34:36 +00:00
namespace Poco { class Logger; }
namespace DB
{
class Context;
/** Every two seconds checks configuration files for update.
* If configuration is changed, then config will be reloaded by ConfigProcessor
* and the reloaded config will be applied via Updater functor.
* It doesn't take into account changes of --config-file, <users_config> and <include_from> parameters.
2016-01-17 13:34:36 +00:00
*/
class ConfigReloader
2016-01-17 13:34:36 +00:00
{
public:
using Updater = std::function<void(ConfigurationPtr)>;
/** include_from_path is usually /etc/metrika.xml (i.e. value of <include_from> tag)
*/
ConfigReloader(
const std::string & path,
const std::string & include_from_path,
const std::string & preprocessed_dir,
zkutil::ZooKeeperNodeCache && zk_node_cache,
const zkutil::EventPtr & zk_changed_event,
Updater && updater,
bool already_loaded);
~ConfigReloader();
2016-01-17 13:34:36 +00:00
/// Call this method to run the backround thread.
void start();
/// Reload immediately. For SYSTEM RELOAD CONFIG query.
void reload() { reloadIfNewer(/* force */ true, /* throw_on_error */ true, /* fallback_to_preprocessed */ false); }
2016-01-17 13:34:36 +00:00
private:
void run();
2016-01-17 13:34:36 +00:00
void reloadIfNewer(bool force, bool throw_on_error, bool fallback_to_preprocessed);
2016-01-17 13:34:36 +00:00
struct FileWithTimestamp;
struct FilesChangesTracker
{
std::set<FileWithTimestamp> files;
2018-08-26 02:29:42 +00:00
void addIfExists(const std::string & path_to_add);
bool isDifferOrNewerThan(const FilesChangesTracker & rhs);
};
FilesChangesTracker getNewFileList() const;
private:
static constexpr auto reload_interval = std::chrono::seconds(2);
Poco::Logger * log = &Logger::get("ConfigReloader");
std::string path;
std::string include_from_path;
std::string preprocessed_dir;
FilesChangesTracker files;
zkutil::ZooKeeperNodeCache zk_node_cache;
bool need_reload_from_zk = false;
zkutil::EventPtr zk_changed_event = std::make_shared<Poco::Event>();
Updater updater;
std::atomic<bool> quit{false};
std::thread thread;
2018-03-13 23:01:03 +00:00
/// Locked inside reloadIfNewer.
std::mutex reload_mutex;
2016-01-17 13:34:36 +00:00
};
}