2016-01-17 13:34:36 +00:00
|
|
|
#pragma once
|
|
|
|
|
2018-02-28 20:34:25 +00:00
|
|
|
#include "ConfigProcessor.h"
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2017-06-19 20:06:35 +00:00
|
|
|
#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>
|
2016-10-10 08:44:52 +00:00
|
|
|
#include <list>
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace Poco { class Logger; }
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
2016-10-14 15:06:46 +00:00
|
|
|
/** Every two seconds checks configuration files for update.
|
|
|
|
* If configuration is changed, then config will be reloaded by ConfigProcessor
|
2017-03-17 00:44:00 +00:00
|
|
|
* 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
|
|
|
*/
|
2016-10-11 14:37:27 +00:00
|
|
|
class ConfigReloader
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Updater = std::function<void(ConfigurationPtr)>;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** 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,
|
2018-11-27 16:11:46 +00:00
|
|
|
const std::string & preprocessed_dir,
|
2017-04-01 07:20:54 +00:00
|
|
|
zkutil::ZooKeeperNodeCache && zk_node_cache,
|
2018-10-17 17:23:10 +00:00
|
|
|
const zkutil::EventPtr & zk_changed_event,
|
2017-04-01 07:20:54 +00:00
|
|
|
Updater && updater,
|
|
|
|
bool already_loaded);
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~ConfigReloader();
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2018-03-03 14:39:16 +00:00
|
|
|
/// Call this method to run the backround thread.
|
|
|
|
void start();
|
|
|
|
|
2018-03-13 10:41:47 +00:00
|
|
|
/// 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:
|
2017-04-01 07:20:54 +00:00
|
|
|
void run();
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void reloadIfNewer(bool force, bool throw_on_error, bool fallback_to_preprocessed);
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct FileWithTimestamp;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
struct FilesChangesTracker
|
|
|
|
{
|
|
|
|
std::set<FileWithTimestamp> files;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2018-08-26 02:29:42 +00:00
|
|
|
void addIfExists(const std::string & path_to_add);
|
2017-04-01 07:20:54 +00:00
|
|
|
bool isDifferOrNewerThan(const FilesChangesTracker & rhs);
|
|
|
|
};
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FilesChangesTracker getNewFileList() const;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static constexpr auto reload_interval = std::chrono::seconds(2);
|
2016-10-14 15:06:46 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Logger * log = &Logger::get("ConfigReloader");
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
|
|
|
std::string include_from_path;
|
2018-11-27 16:11:46 +00:00
|
|
|
std::string preprocessed_dir;
|
2017-04-01 07:20:54 +00:00
|
|
|
FilesChangesTracker files;
|
|
|
|
zkutil::ZooKeeperNodeCache zk_node_cache;
|
2018-12-27 17:23:49 +00:00
|
|
|
bool need_reload_from_zk = false;
|
2018-10-17 17:23:10 +00:00
|
|
|
zkutil::EventPtr zk_changed_event = std::make_shared<Poco::Event>();
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Updater updater;
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::atomic<bool> quit{false};
|
2019-01-14 19:22:09 +00:00
|
|
|
ThreadFromGlobalPool thread;
|
2018-03-13 10:41:47 +00:00
|
|
|
|
2018-03-13 23:01:03 +00:00
|
|
|
/// Locked inside reloadIfNewer.
|
2018-03-13 10:41:47 +00:00
|
|
|
std::mutex reload_mutex;
|
2016-01-17 13:34:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|