2017-01-21 04:24:28 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <thread>
|
2017-12-26 19:00:20 +00:00
|
|
|
#include <functional>
|
2019-01-11 19:12:36 +00:00
|
|
|
#include <Common/MultiVersion.h>
|
2019-01-14 19:22:09 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2017-01-21 04:24:28 +00:00
|
|
|
#include <Poco/Event.h>
|
|
|
|
|
|
|
|
|
2017-12-27 19:26:18 +00:00
|
|
|
namespace Poco { class Logger; namespace Util { class AbstractConfiguration; } }
|
2017-01-21 04:24:28 +00:00
|
|
|
|
|
|
|
class RegionsHierarchies;
|
|
|
|
class RegionsNames;
|
2019-09-26 16:12:15 +00:00
|
|
|
class GeoDictionariesLoader;
|
2017-01-21 04:24:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
|
|
|
|
|
|
|
/// Metrica's Dictionaries which can be used in functions.
|
|
|
|
|
|
|
|
class EmbeddedDictionaries
|
|
|
|
{
|
|
|
|
private:
|
2017-08-24 14:51:13 +00:00
|
|
|
Poco::Logger * log;
|
|
|
|
Context & context;
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
MultiVersion<RegionsHierarchies> regions_hierarchies;
|
|
|
|
MultiVersion<RegionsNames> regions_names;
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2019-09-26 16:12:15 +00:00
|
|
|
std::unique_ptr<GeoDictionariesLoader> geo_dictionaries_loader;
|
2017-11-28 22:15:06 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Directories' updating periodicity (in seconds).
|
|
|
|
int reload_period;
|
|
|
|
int cur_reload_period = 1;
|
|
|
|
bool is_fast_start_stage = true;
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-08-24 18:19:06 +00:00
|
|
|
mutable std::mutex mutex;
|
|
|
|
|
2019-01-14 19:22:09 +00:00
|
|
|
ThreadFromGlobalPool reloading_thread;
|
2017-04-01 07:20:54 +00:00
|
|
|
Poco::Event destroy;
|
2017-01-21 04:24:28 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void handleException(const bool throw_on_error) const;
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/** Updates directories (dictionaries) every reload_period seconds.
|
|
|
|
* If all dictionaries are not loaded at least once, try reload them with exponential delay (1, 2, ... reload_period).
|
|
|
|
* If all dictionaries are loaded, update them using constant reload_period delay.
|
|
|
|
*/
|
|
|
|
void reloadPeriodically();
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-08-24 18:19:06 +00:00
|
|
|
/// Updates dictionaries.
|
|
|
|
bool reloadImpl(const bool throw_on_error, const bool force_reload = false);
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Dictionary>
|
2017-11-28 22:15:06 +00:00
|
|
|
using DictionaryReloader = std::function<std::unique_ptr<Dictionary>(const Poco::Util::AbstractConfiguration & config)>;
|
|
|
|
|
|
|
|
template <typename Dictionary>
|
|
|
|
bool reloadDictionary(
|
|
|
|
MultiVersion<Dictionary> & dictionary,
|
|
|
|
DictionaryReloader<Dictionary> reload_dictionary,
|
|
|
|
const bool throw_on_error,
|
|
|
|
const bool force_reload);
|
2017-01-21 04:24:28 +00:00
|
|
|
|
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Every reload_period seconds directories are updated inside a separate thread.
|
2017-11-28 22:15:06 +00:00
|
|
|
EmbeddedDictionaries(
|
2019-09-26 16:12:15 +00:00
|
|
|
std::unique_ptr<GeoDictionariesLoader> geo_dictionaries_loader,
|
2017-11-28 22:15:06 +00:00
|
|
|
Context & context,
|
|
|
|
const bool throw_on_error);
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-08-24 18:19:06 +00:00
|
|
|
/// Forcibly reloads all dictionaries.
|
|
|
|
void reload();
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
~EmbeddedDictionaries();
|
2017-01-21 04:24:28 +00:00
|
|
|
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
MultiVersion<RegionsHierarchies>::Version getRegionsHierarchies() const
|
|
|
|
{
|
|
|
|
return regions_hierarchies.get();
|
|
|
|
}
|
2017-01-21 04:24:28 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
MultiVersion<RegionsNames>::Version getRegionsNames() const
|
|
|
|
{
|
|
|
|
return regions_names.get();
|
|
|
|
}
|
2017-01-21 04:24:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|