2016-10-11 14:37:27 +00:00
|
|
|
#include "ConfigReloader.h"
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/File.h>
|
|
|
|
#include <common/logger_useful.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/setThreadName.h>
|
2018-02-28 20:34:25 +00:00
|
|
|
#include "ConfigProcessor.h"
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2016-10-16 20:01:38 +00:00
|
|
|
constexpr decltype(ConfigReloader::reload_interval) ConfigReloader::reload_interval;
|
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
ConfigReloader::ConfigReloader(
|
2017-04-01 07:20:54 +00:00
|
|
|
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)
|
|
|
|
: path(path_), include_from_path(include_from_path_)
|
2018-11-27 16:11:46 +00:00
|
|
|
, preprocessed_dir(preprocessed_dir_)
|
2017-04-01 07:20:54 +00:00
|
|
|
, zk_node_cache(std::move(zk_node_cache_))
|
2018-10-17 17:23:10 +00:00
|
|
|
, zk_changed_event(zk_changed_event_)
|
2017-04-01 07:20:54 +00:00
|
|
|
, updater(std::move(updater_))
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
if (!already_loaded)
|
|
|
|
reloadIfNewer(/* force = */ true, /* throw_on_error = */ true, /* fallback_to_preprocessed = */ true);
|
2018-03-03 14:39:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2018-03-03 14:39:16 +00:00
|
|
|
void ConfigReloader::start()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
thread = std::thread(&ConfigReloader::run, this);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2016-10-11 14:37:27 +00:00
|
|
|
ConfigReloader::~ConfigReloader()
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
quit = true;
|
2018-10-17 17:23:10 +00:00
|
|
|
zk_changed_event->set();
|
2018-03-03 14:39:16 +00:00
|
|
|
|
|
|
|
if (thread.joinable())
|
|
|
|
thread.join();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-11 14:37:27 +00:00
|
|
|
void ConfigReloader::run()
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
setThreadName("ConfigReloader");
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2018-01-15 12:24:48 +00:00
|
|
|
try
|
|
|
|
{
|
2018-10-17 17:23:10 +00:00
|
|
|
bool zk_changed = zk_changed_event->tryWait(std::chrono::milliseconds(reload_interval).count());
|
2018-01-15 12:24:48 +00:00
|
|
|
if (quit)
|
|
|
|
return;
|
2016-10-23 10:52:32 +00:00
|
|
|
|
2018-01-15 12:24:48 +00:00
|
|
|
reloadIfNewer(zk_changed, /* throw_on_error = */ false, /* fallback_to_preprocessed = */ false);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(log, __PRETTY_FUNCTION__);
|
2018-04-02 16:21:50 +00:00
|
|
|
std::this_thread::sleep_for(reload_interval);
|
2018-01-15 12:24:48 +00:00
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
void ConfigReloader::reloadIfNewer(bool force, bool throw_on_error, bool fallback_to_preprocessed)
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2018-03-13 10:41:47 +00:00
|
|
|
std::lock_guard<std::mutex> lock(reload_mutex);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FilesChangesTracker new_files = getNewFileList();
|
2018-12-27 17:23:49 +00:00
|
|
|
if (force || need_reload_from_zk || new_files.isDifferOrNewerThan(files))
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
2017-11-21 16:54:25 +00:00
|
|
|
ConfigProcessor config_processor(path);
|
2017-04-01 07:20:54 +00:00
|
|
|
ConfigProcessor::LoadedConfig loaded_config;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
LOG_DEBUG(log, "Loading config `" << path << "'");
|
|
|
|
|
2017-11-21 16:54:25 +00:00
|
|
|
loaded_config = config_processor.loadConfig(/* allow_zk_includes = */ true);
|
2017-04-01 07:20:54 +00:00
|
|
|
if (loaded_config.has_zk_includes)
|
2017-11-21 16:54:25 +00:00
|
|
|
loaded_config = config_processor.loadConfigWithZooKeeperIncludes(
|
2018-10-17 17:23:10 +00:00
|
|
|
zk_node_cache, zk_changed_event, fallback_to_preprocessed);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
2018-12-27 17:23:49 +00:00
|
|
|
catch (const Coordination::Exception & e)
|
|
|
|
{
|
|
|
|
if (Coordination::isHardwareError(e.code))
|
|
|
|
need_reload_from_zk = true;
|
|
|
|
|
|
|
|
if (throw_on_error)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
tryLogCurrentException(log, "ZooKeeper error when loading config from `" + path + "'");
|
|
|
|
return;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (throw_on_error)
|
|
|
|
throw;
|
|
|
|
|
|
|
|
tryLogCurrentException(log, "Error loading config from `" + path + "'");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-27 16:11:46 +00:00
|
|
|
config_processor.savePreprocessedConfig(loaded_config, preprocessed_dir);
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
/** We should remember last modification time if and only if config was sucessfully loaded
|
|
|
|
* Otherwise a race condition could occur during config files update:
|
|
|
|
* File is contain raw (and non-valid) data, therefore config is not applied.
|
|
|
|
* When file has been written (and contain valid data), we don't load new data since modification time remains the same.
|
|
|
|
*/
|
|
|
|
if (!loaded_config.loaded_from_preprocessed)
|
2018-12-27 17:23:49 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
files = std::move(new_files);
|
2018-12-27 17:23:49 +00:00
|
|
|
need_reload_from_zk = false;
|
|
|
|
}
|
2017-04-01 07:20:54 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
updater(loaded_config.configuration);
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
if (throw_on_error)
|
|
|
|
throw;
|
|
|
|
tryLogCurrentException(log, "Error updating configuration from `" + path + "' config.");
|
|
|
|
}
|
|
|
|
}
|
2017-03-17 00:44:00 +00:00
|
|
|
}
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
struct ConfigReloader::FileWithTimestamp
|
2016-10-10 08:44:52 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
std::string path;
|
|
|
|
time_t modification_time;
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
FileWithTimestamp(const std::string & path_, time_t modification_time_)
|
|
|
|
: path(path_), modification_time(modification_time_) {}
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
bool operator < (const FileWithTimestamp & rhs) const
|
|
|
|
{
|
|
|
|
return path < rhs.path;
|
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
static bool isTheSame(const FileWithTimestamp & lhs, const FileWithTimestamp & rhs)
|
|
|
|
{
|
|
|
|
return (lhs.modification_time == rhs.modification_time) && (lhs.path == rhs.path);
|
|
|
|
}
|
2017-03-17 00:44:00 +00:00
|
|
|
};
|
2016-10-10 08:44:52 +00:00
|
|
|
|
|
|
|
|
2018-08-26 02:29:42 +00:00
|
|
|
void ConfigReloader::FilesChangesTracker::addIfExists(const std::string & path_to_add)
|
2016-10-10 08:44:52 +00:00
|
|
|
{
|
2018-08-26 02:29:42 +00:00
|
|
|
if (!path_to_add.empty() && Poco::File(path_to_add).exists())
|
|
|
|
files.emplace(path_to_add, Poco::File(path_to_add).getLastModified().epochTime());
|
2017-03-17 00:44:00 +00:00
|
|
|
}
|
2016-10-10 08:44:52 +00:00
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
bool ConfigReloader::FilesChangesTracker::isDifferOrNewerThan(const FilesChangesTracker & rhs)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return (files.size() != rhs.files.size()) ||
|
|
|
|
!std::equal(files.begin(), files.end(), rhs.files.begin(), FileWithTimestamp::isTheSame);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 00:44:00 +00:00
|
|
|
ConfigReloader::FilesChangesTracker ConfigReloader::getNewFileList() const
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
FilesChangesTracker file_list;
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
file_list.addIfExists(path);
|
|
|
|
file_list.addIfExists(include_from_path);
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
for (const auto & merge_path : ConfigProcessor::getConfigMergeFiles(path))
|
|
|
|
file_list.addIfExists(merge_path);
|
2017-03-17 00:44:00 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return file_list;
|
2017-03-17 00:44:00 +00:00
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
}
|