2015-02-10 17:40:40 +00:00
|
|
|
#pragma once
|
|
|
|
|
2015-06-09 16:12:51 +00:00
|
|
|
#include <DB/Dictionaries/IDictionary.h>
|
2015-10-05 01:35:28 +00:00
|
|
|
#include <DB/Common/Exception.h>
|
2015-09-24 18:54:21 +00:00
|
|
|
#include <DB/Common/setThreadName.h>
|
2016-07-31 03:53:16 +00:00
|
|
|
#include <DB/Common/randomSeed.h>
|
2015-09-29 19:19:54 +00:00
|
|
|
#include <common/MultiVersion.h>
|
|
|
|
#include <common/logger_useful.h>
|
2015-02-10 17:40:40 +00:00
|
|
|
#include <Poco/Event.h>
|
2015-06-09 16:12:51 +00:00
|
|
|
#include <unistd.h>
|
2015-02-10 17:40:40 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <chrono>
|
|
|
|
#include <random>
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class Context;
|
|
|
|
|
|
|
|
/** Manages user-defined dictionaries.
|
|
|
|
* Monitors configuration file and automatically reloads dictionaries in a separate thread.
|
|
|
|
* The monitoring thread wakes up every @check_period_sec seconds and checks
|
|
|
|
* modification time of dictionaries' configuration file. If said time is greater than
|
|
|
|
* @config_last_modified, the dictionaries are created from scratch using configuration file,
|
|
|
|
* possibly overriding currently existing dictionaries with the same name (previous versions of
|
|
|
|
* overridden dictionaries will live as long as there are any users retaining them).
|
|
|
|
*
|
|
|
|
* Apart from checking configuration file for modifications, each non-cached dictionary
|
|
|
|
* has a lifetime of its own and may be updated if it's source reports that it has been
|
|
|
|
* modified. The time of next update is calculated by choosing uniformly a random number
|
|
|
|
* distributed between lifetime.min_sec and lifetime.max_sec.
|
|
|
|
* If either of lifetime.min_sec and lifetime.max_sec is zero, such dictionary is never updated.
|
|
|
|
*/
|
|
|
|
class ExternalDictionaries
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
static const auto check_period_sec = 5;
|
|
|
|
|
2015-03-24 09:46:07 +00:00
|
|
|
friend class StorageSystemDictionaries;
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
mutable std::mutex dictionaries_mutex;
|
2015-03-18 16:07:15 +00:00
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
using DictionaryPtr = std::shared_ptr<MultiVersion<IDictionaryBase>>;
|
|
|
|
struct DictionaryInfo final
|
2015-05-26 11:53:58 +00:00
|
|
|
{
|
2016-08-07 09:09:18 +00:00
|
|
|
DictionaryPtr dict;
|
2015-05-26 11:53:58 +00:00
|
|
|
std::string origin;
|
|
|
|
std::exception_ptr exception;
|
|
|
|
};
|
|
|
|
|
2016-08-07 09:09:18 +00:00
|
|
|
struct FailedDictionaryInfo final
|
2015-06-09 16:12:51 +00:00
|
|
|
{
|
2015-07-13 17:10:16 +00:00
|
|
|
std::unique_ptr<IDictionaryBase> dict;
|
2015-06-09 16:12:51 +00:00
|
|
|
std::chrono::system_clock::time_point next_attempt_time;
|
2016-10-26 22:27:38 +00:00
|
|
|
UInt64 error_count;
|
2015-06-09 16:12:51 +00:00
|
|
|
};
|
|
|
|
|
2016-10-18 19:32:32 +00:00
|
|
|
/** name -> dictionary.
|
2015-08-12 03:57:32 +00:00
|
|
|
*/
|
2016-08-07 09:09:18 +00:00
|
|
|
std::unordered_map<std::string, DictionaryInfo> dictionaries;
|
2015-08-12 03:57:32 +00:00
|
|
|
|
2016-10-18 19:32:32 +00:00
|
|
|
/** Here are dictionaries, that has been never loaded sussessfully.
|
|
|
|
* They are also in 'dictionaries', but with nullptr as 'dict'.
|
2015-08-12 03:57:32 +00:00
|
|
|
*/
|
2016-08-07 09:09:18 +00:00
|
|
|
std::unordered_map<std::string, FailedDictionaryInfo> failed_dictionaries;
|
2015-08-12 03:57:32 +00:00
|
|
|
|
2016-10-18 19:32:32 +00:00
|
|
|
/** Both for dictionaries and failed_dictionaries.
|
2015-08-12 03:57:32 +00:00
|
|
|
*/
|
|
|
|
std::unordered_map<std::string, std::chrono::system_clock::time_point> update_times;
|
|
|
|
|
2016-07-31 03:53:16 +00:00
|
|
|
std::mt19937_64 rnd_engine{randomSeed()};
|
2015-02-10 17:40:40 +00:00
|
|
|
|
|
|
|
Context & context;
|
|
|
|
|
|
|
|
std::thread reloading_thread;
|
|
|
|
Poco::Event destroy;
|
|
|
|
|
|
|
|
Logger * log;
|
|
|
|
|
2015-03-18 16:07:15 +00:00
|
|
|
std::unordered_map<std::string, Poco::Timestamp> last_modification_times;
|
2015-02-10 17:40:40 +00:00
|
|
|
|
2015-03-27 13:11:22 +00:00
|
|
|
void reloadImpl(bool throw_on_error = false);
|
|
|
|
void reloadFromFile(const std::string & config_path, bool throw_on_error);
|
2015-02-10 17:40:40 +00:00
|
|
|
|
|
|
|
void reloadPeriodically()
|
|
|
|
{
|
2015-09-24 18:54:21 +00:00
|
|
|
setThreadName("ExterDictReload");
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (destroy.tryWait(check_period_sec * 1000))
|
|
|
|
return;
|
|
|
|
|
|
|
|
reloadImpl();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2016-10-18 19:32:32 +00:00
|
|
|
/// Dictionaries will be loaded immediately and then will be updated in separate thread, each 'reload_period' seconds.
|
2015-03-27 13:11:22 +00:00
|
|
|
ExternalDictionaries(Context & context, const bool throw_on_error)
|
2015-02-10 17:40:40 +00:00
|
|
|
: context(context), log(&Logger::get("ExternalDictionaries"))
|
|
|
|
{
|
2015-09-09 00:54:17 +00:00
|
|
|
{
|
2016-10-18 19:32:32 +00:00
|
|
|
/** During synchronous loading of external dictionaries at moment of query execution,
|
|
|
|
* we should not use per query memory limit.
|
2015-09-09 00:54:17 +00:00
|
|
|
*/
|
2016-10-25 14:22:10 +00:00
|
|
|
TemporarilyDisableMemoryTracker temporarily_disable_memory_tracker;
|
2015-09-09 00:54:17 +00:00
|
|
|
|
|
|
|
reloadImpl(throw_on_error);
|
|
|
|
}
|
|
|
|
|
2015-02-10 17:40:40 +00:00
|
|
|
reloading_thread = std::thread{&ExternalDictionaries::reloadPeriodically, this};
|
|
|
|
}
|
|
|
|
|
|
|
|
~ExternalDictionaries()
|
|
|
|
{
|
|
|
|
destroy.set();
|
|
|
|
reloading_thread.join();
|
|
|
|
}
|
|
|
|
|
2015-07-13 17:10:16 +00:00
|
|
|
MultiVersion<IDictionaryBase>::Version getDictionary(const std::string & name) const;
|
2015-02-10 17:40:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|