2018-03-26 14:12:07 +00:00
|
|
|
#include "DNSCacheUpdater.h"
|
2021-04-10 23:33:54 +00:00
|
|
|
|
2018-03-26 14:12:07 +00:00
|
|
|
#include <Interpreters/Context.h>
|
2021-04-10 23:33:54 +00:00
|
|
|
#include <Common/DNSResolver.h>
|
2021-01-10 02:48:57 +00:00
|
|
|
|
2018-03-26 14:12:07 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
DNSCacheUpdater::DNSCacheUpdater(ContextPtr context_, Int32 update_period_seconds_)
|
|
|
|
: WithContext(context_)
|
|
|
|
, update_period_seconds(update_period_seconds_)
|
|
|
|
, pool(getContext()->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
|
|
|
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
|
|
|
{
|
2020-05-23 22:24:01 +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
|
|
|
|
{
|
2021-04-10 23:33:54 +00:00
|
|
|
getContext()->reloadClusterConfig();
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2019-04-06 01:09:15 +00:00
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
2018-03-26 14:12:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-05 13:20:21 +00:00
|
|
|
/** DNS resolution may take a while and by this reason, actual update period will be longer than update_period_seconds.
|
|
|
|
* We intentionally allow this "drift" for two reasons:
|
|
|
|
* - automatically throttle when DNS requests take longer time;
|
|
|
|
* - add natural randomization on huge clusters - avoid sending all requests at the same moment of time from different servers.
|
|
|
|
*/
|
2021-02-08 11:41:58 +00:00
|
|
|
task_handle->scheduleAfter(size_t(update_period_seconds) * 1000);
|
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
|
|
|
{
|
2020-09-02 08:07:46 +00:00
|
|
|
LOG_INFO(&Poco::Logger::get("DNSCacheUpdater"), "Update period {} seconds", update_period_seconds);
|
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
|
|
|
}
|