1#include "DNSCacheUpdater.h"
2#include <Common/DNSResolver.h>
3#include <Interpreters/Context.h>
4#include <Core/BackgroundSchedulePool.h>
5
6namespace DB
7{
8
9DNSCacheUpdater::DNSCacheUpdater(Context & context_, Int32 update_period_seconds_)
10 : context(context_),
11 update_period_seconds(update_period_seconds_),
12 pool(context_.getSchedulePool())
13{
14 task_handle = pool.createTask("DNSCacheUpdater", [this]{ run(); });
15}
16
17void DNSCacheUpdater::run()
18{
19 auto & resolver = DNSResolver::instance();
20
21 /// Reload cluster config if IP of any host has been changed since last update.
22 if (resolver.updateCache())
23 {
24 LOG_INFO(&Poco::Logger::get("DNSCacheUpdater"),
25 "IPs of some hosts have been changed. Will reload cluster config.");
26 try
27 {
28 context.reloadClusterConfig();
29 }
30 catch (...)
31 {
32 tryLogCurrentException(__PRETTY_FUNCTION__);
33 }
34 }
35
36 /** DNS resolution may take a while and by this reason, actual update period will be longer than update_period_seconds.
37 * We intentionally allow this "drift" for two reasons:
38 * - automatically throttle when DNS requests take longer time;
39 * - add natural randomization on huge clusters - avoid sending all requests at the same moment of time from different servers.
40 */
41 task_handle->scheduleAfter(update_period_seconds * 1000);
42}
43
44void DNSCacheUpdater::start()
45{
46 task_handle->activateAndSchedule();
47}
48
49DNSCacheUpdater::~DNSCacheUpdater()
50{
51 task_handle->deactivate();
52}
53
54}
55