2015-01-21 11:39:48 +00:00
|
|
|
#include <DB/Interpreters/Dictionaries.h>
|
|
|
|
#include <DB/Dictionaries/DictionaryFactory.h>
|
2015-02-03 17:03:35 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-01-26 16:53:44 +00:00
|
|
|
#include <DB/Dictionaries/config_ptr_t.h>
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-02-04 13:06:56 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
std::string getDictionariesConfigPath(const Poco::Util::AbstractConfiguration & config)
|
|
|
|
{
|
|
|
|
const auto path = config.getString("dictionaries_config");
|
|
|
|
if (path.empty())
|
|
|
|
return path;
|
|
|
|
|
|
|
|
if (path[0] != '/')
|
|
|
|
{
|
|
|
|
const auto app_config_path = config.getString("config-file", "config.xml");
|
|
|
|
const auto config_dir = Poco::Path{app_config_path}.parent().toString();
|
|
|
|
const auto absolute_path = config_dir + path;
|
|
|
|
if (Poco::File{absolute_path}.exists())
|
|
|
|
return absolute_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
void Dictionaries::reloadExternals()
|
2015-01-21 11:39:48 +00:00
|
|
|
{
|
2015-02-04 13:06:56 +00:00
|
|
|
const auto config_path = getDictionariesConfigPath(Poco::Util::Application::instance().config());
|
2015-01-22 14:32:38 +00:00
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
const auto last_modified = Poco::File{config_path}.getLastModified();
|
|
|
|
if (last_modified > dictionaries_last_modified)
|
|
|
|
{
|
2015-01-30 15:18:13 +00:00
|
|
|
/// definitions of dictionaries may have changed, recreate all of them
|
2015-01-26 15:27:51 +00:00
|
|
|
dictionaries_last_modified = last_modified;
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
const config_ptr_t<Poco::Util::XMLConfiguration> config{new Poco::Util::XMLConfiguration{config_path}};
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
/// get all dictionaries' definitions
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config->keys(keys);
|
|
|
|
|
|
|
|
/// for each dictionary defined in xml config
|
|
|
|
for (const auto & key : keys)
|
2015-01-21 11:39:48 +00:00
|
|
|
{
|
2015-01-30 13:43:16 +00:00
|
|
|
try
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-01-30 13:43:16 +00:00
|
|
|
if (0 != strncmp(key.data(), "dictionary", strlen("dictionary")))
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "unknown node in dictionaries file: '" + key + "', 'dictionary'");
|
|
|
|
continue;
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
const auto & prefix = key + '.';
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-01-30 13:43:16 +00:00
|
|
|
const auto & name = config->getString(prefix + "name");
|
|
|
|
if (name.empty())
|
|
|
|
{
|
|
|
|
LOG_WARNING(log, "dictionary name cannot be empty");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-01-30 15:18:13 +00:00
|
|
|
auto dict_ptr = DictionaryFactory::instance().create(name, *config, prefix, context);
|
2015-02-03 11:36:07 +00:00
|
|
|
if (!dict_ptr->isCached())
|
|
|
|
{
|
|
|
|
const auto & lifetime = dict_ptr->getLifetime();
|
|
|
|
std::uniform_int_distribution<std::uint64_t> distribution{lifetime.min_sec, lifetime.max_sec};
|
|
|
|
update_times[name] = std::chrono::system_clock::now() +
|
|
|
|
std::chrono::seconds{distribution(rnd_engine)};
|
|
|
|
}
|
|
|
|
|
2015-01-26 15:27:51 +00:00
|
|
|
auto it = external_dictionaries.find(name);
|
2015-01-30 15:18:13 +00:00
|
|
|
/// add new dictionary or update an existing version
|
2015-01-26 15:27:51 +00:00
|
|
|
if (it == std::end(external_dictionaries))
|
2015-02-03 11:36:07 +00:00
|
|
|
{
|
|
|
|
const std::lock_guard<std::mutex> lock{external_dictionaries_mutex};
|
2015-01-26 15:27:51 +00:00
|
|
|
external_dictionaries.emplace(name, std::make_shared<MultiVersion<IDictionary>>(dict_ptr.release()));
|
2015-02-03 11:36:07 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
else
|
2015-01-30 15:18:13 +00:00
|
|
|
it->second->set(dict_ptr.release());
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-01-30 13:43:16 +00:00
|
|
|
catch (...)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
|
|
|
handleException();
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-30 15:18:13 +00:00
|
|
|
/// periodic update
|
2015-01-26 15:27:51 +00:00
|
|
|
for (auto & dictionary : external_dictionaries)
|
2015-01-21 11:39:48 +00:00
|
|
|
{
|
2015-01-26 15:27:51 +00:00
|
|
|
try
|
|
|
|
{
|
2015-01-29 15:47:21 +00:00
|
|
|
auto current = dictionary.second->get();
|
2015-02-03 11:36:07 +00:00
|
|
|
/// update only non-cached dictionaries
|
|
|
|
if (!current->isCached())
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-03 11:36:07 +00:00
|
|
|
auto & update_time = update_times[current->getName()];
|
|
|
|
|
|
|
|
/// check that timeout has passed
|
|
|
|
if (std::chrono::system_clock::now() < update_time)
|
2015-01-29 15:47:21 +00:00
|
|
|
continue;
|
|
|
|
|
2015-02-03 11:36:07 +00:00
|
|
|
/// check source modified
|
|
|
|
if (current->getSource()->isModified())
|
|
|
|
{
|
|
|
|
/// create new version of dictionary
|
|
|
|
auto new_version = current->clone();
|
|
|
|
dictionary.second->set(new_version.release());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// calculate next update time
|
|
|
|
const auto & lifetime = current->getLifetime();
|
|
|
|
std::uniform_int_distribution<std::uint64_t> distribution{lifetime.min_sec, lifetime.max_sec};
|
|
|
|
update_time = std::chrono::system_clock::now() + std::chrono::seconds{distribution(rnd_engine)};
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-30 13:43:16 +00:00
|
|
|
catch (...)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
|
|
|
handleException();
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
}
|