2018-03-26 14:12:07 +00:00
|
|
|
#include "DNSCacheUpdater.h"
|
2018-04-19 13:56:14 +00:00
|
|
|
#include <Common/DNSResolver.h>
|
2018-03-26 14:12:07 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2019-04-06 01:09:15 +00:00
|
|
|
#include <Core/BackgroundSchedulePool.h>
|
2018-03-26 14:12:07 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
DNSCacheUpdater::DNSCacheUpdater(Context & context_, Int32 update_period_seconds_)
|
|
|
|
: context(context_),
|
|
|
|
update_period_seconds(update_period_seconds_),
|
|
|
|
pool(context_.getSchedulePool())
|
2018-03-26 14:12:07 +00:00
|
|
|
{
|
2019-04-06 01:09:15 +00:00
|
|
|
task_handle = pool.createTask("DNSCacheUpdater", [this]{ run(); });
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-04-06 01:09:15 +00:00
|
|
|
void DNSCacheUpdater::run()
|
2018-03-26 14:12:07 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
watch.restart();
|
|
|
|
auto & resolver = DNSResolver::instance();
|
|
|
|
|
|
|
|
/// Reload cluster config if IP of any host has been changed since last update.
|
|
|
|
if (resolver.updateCache())
|
2018-03-26 14:12:07 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
LOG_INFO(&Poco::Logger::get("DNSCacheUpdater"),
|
|
|
|
"IPs of some hosts have been changed. Will reload cluster config.");
|
2018-03-26 14:12:07 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
context.reloadClusterConfig();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2019-04-06 01:09:15 +00:00
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
auto interval_ms = std::max(0, update_period_seconds * 1000 - static_cast<Int32>(watch.elapsedMilliseconds()));
|
|
|
|
task_handle->scheduleAfter(interval_ms);
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
void DNSCacheUpdater::start()
|
2018-03-26 14:12:07 +00:00
|
|
|
{
|
2019-06-27 16:28:26 +00:00
|
|
|
task_handle->activateAndSchedule();
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
DNSCacheUpdater::~DNSCacheUpdater()
|
|
|
|
{
|
|
|
|
task_handle->deactivate();
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 16:28:26 +00:00
|
|
|
}
|