2015-02-10 17:40:40 +00:00
|
|
|
#include <DB/Interpreters/ExternalDictionaries.h>
|
2015-01-21 11:39:48 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryFactory.h>
|
2015-02-03 17:03:35 +00:00
|
|
|
#include <DB/Dictionaries/DictionaryStructure.h>
|
|
|
|
#include <DB/Dictionaries/IDictionarySource.h>
|
2015-02-06 10:35:35 +00:00
|
|
|
#include <statdaemons/ext/scope_guard.hpp>
|
2015-02-10 17:40:40 +00:00
|
|
|
#include <Poco/Util/Application.h>
|
2015-03-18 16:07:15 +00:00
|
|
|
#include <Poco/Glob.h>
|
2015-01-21 11:39:48 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2015-02-04 13:06:56 +00:00
|
|
|
namespace
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
std::set<std::string> getDictionariesConfigPaths(const Poco::Util::AbstractConfiguration & config)
|
2015-02-04 13:06:56 +00:00
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
auto pattern = config.getString("dictionaries_config", "");
|
|
|
|
if (pattern.empty())
|
|
|
|
return {};
|
2015-02-04 13:06:56 +00:00
|
|
|
|
2015-03-18 16:07:15 +00:00
|
|
|
std::set<std::string> files;
|
|
|
|
if (pattern[0] != '/')
|
2015-02-04 13:06:56 +00:00
|
|
|
{
|
|
|
|
const auto app_config_path = config.getString("config-file", "config.xml");
|
|
|
|
const auto config_dir = Poco::Path{app_config_path}.parent().toString();
|
2015-03-18 16:07:15 +00:00
|
|
|
const auto absolute_path = config_dir + pattern;
|
|
|
|
Poco::Glob::glob(absolute_path, files, 0);
|
|
|
|
if (!files.empty())
|
|
|
|
return files;
|
2015-02-04 13:06:56 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 16:07:15 +00:00
|
|
|
Poco::Glob::glob(pattern, files, 0);
|
|
|
|
|
|
|
|
return files;
|
2015-02-04 13:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:11:22 +00:00
|
|
|
void ExternalDictionaries::reloadImpl(const bool throw_on_error)
|
2015-01-21 11:39:48 +00:00
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
const auto config_paths = getDictionariesConfigPaths(Poco::Util::Application::instance().config());
|
|
|
|
|
|
|
|
for (const auto & config_path : config_paths)
|
2015-03-27 13:11:22 +00:00
|
|
|
reloadFromFile(config_path, throw_on_error);
|
2015-03-18 16:07:15 +00:00
|
|
|
|
|
|
|
/// periodic update
|
|
|
|
for (auto & dictionary : dictionaries)
|
|
|
|
{
|
2015-03-24 09:46:07 +00:00
|
|
|
const auto & name = dictionary.first;
|
|
|
|
|
2015-03-18 16:07:15 +00:00
|
|
|
try
|
|
|
|
{
|
2015-05-26 11:53:58 +00:00
|
|
|
if (!dictionary.second.dict)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto current = dictionary.second.dict->get();
|
2015-03-18 16:07:15 +00:00
|
|
|
const auto & lifetime = current->getLifetime();
|
|
|
|
|
|
|
|
/// do not update dictionaries with zero as lifetime
|
|
|
|
if (lifetime.min_sec == 0 || lifetime.max_sec == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/// update only non-cached dictionaries
|
|
|
|
if (!current->isCached())
|
|
|
|
{
|
|
|
|
auto & update_time = update_times[current->getName()];
|
|
|
|
|
|
|
|
/// check that timeout has passed
|
|
|
|
if (std::chrono::system_clock::now() < update_time)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SCOPE_EXIT(
|
|
|
|
/// calculate next update time
|
|
|
|
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)};
|
|
|
|
);
|
|
|
|
|
|
|
|
/// check source modified
|
|
|
|
if (current->getSource()->isModified())
|
|
|
|
{
|
|
|
|
/// create new version of dictionary
|
|
|
|
auto new_version = current->clone();
|
2015-05-26 11:53:58 +00:00
|
|
|
dictionary.second.dict->set(new_version.release());
|
2015-03-18 16:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-24 09:46:07 +00:00
|
|
|
|
|
|
|
/// erase stored exception on success
|
2015-05-26 11:53:58 +00:00
|
|
|
dictionary.second.exception = std::exception_ptr{};
|
2015-03-18 16:07:15 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2015-05-26 11:53:58 +00:00
|
|
|
dictionary.second.exception = std::current_exception();
|
2015-03-24 09:46:07 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
|
|
|
LOG_ERROR(log, "Cannot update external dictionary '" << name
|
|
|
|
<< "'! You must resolve this manually. " << e.displayText());
|
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
LOG_ERROR(log, "Cannot update external dictionary '" << name
|
|
|
|
<< "'! You must resolve this manually. " << e.what());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
LOG_ERROR(log, "Cannot update external dictionary '" << name
|
|
|
|
<< "'! You must resolve this manually.");
|
|
|
|
}
|
2015-03-18 16:07:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-27 13:11:22 +00:00
|
|
|
void ExternalDictionaries::reloadFromFile(const std::string & config_path, const bool throw_on_error)
|
2015-03-18 16:07:15 +00:00
|
|
|
{
|
2015-02-04 13:21:50 +00:00
|
|
|
const Poco::File config_file{config_path};
|
2015-01-22 14:32:38 +00:00
|
|
|
|
2015-02-09 10:10:25 +00:00
|
|
|
if (config_path.empty() || !config_file.exists())
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-02-04 13:21:50 +00:00
|
|
|
LOG_WARNING(log, "config file '" + config_path + "' does not exist");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
auto it = last_modification_times.find(config_path);
|
|
|
|
if (it == std::end(last_modification_times))
|
|
|
|
it = last_modification_times.emplace(config_path, Poco::Timestamp{0}).first;
|
|
|
|
auto & config_last_modified = it->second;
|
|
|
|
|
2015-02-04 13:21:50 +00:00
|
|
|
const auto last_modified = config_file.getLastModified();
|
2015-02-10 17:40:40 +00:00
|
|
|
if (last_modified > config_last_modified)
|
2015-02-04 13:21:50 +00:00
|
|
|
{
|
|
|
|
/// definitions of dictionaries may have changed, recreate all of them
|
2015-02-10 17:40:40 +00:00
|
|
|
config_last_modified = last_modified;
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
const auto config = new Poco::Util::XMLConfiguration{config_path};
|
2015-03-18 16:07:15 +00:00
|
|
|
SCOPE_EXIT(config->release());
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-02-04 13:21:50 +00:00
|
|
|
/// get all dictionaries' definitions
|
|
|
|
Poco::Util::AbstractConfiguration::Keys keys;
|
|
|
|
config->keys(keys);
|
2015-01-26 15:27:51 +00:00
|
|
|
|
2015-02-04 13:21:50 +00:00
|
|
|
/// for each dictionary defined in xml config
|
|
|
|
for (const auto & key : keys)
|
2015-01-26 15:27:51 +00:00
|
|
|
{
|
2015-03-06 16:13:59 +00:00
|
|
|
std::string name;
|
|
|
|
|
2015-03-18 09:55:02 +00:00
|
|
|
if (0 != strncmp(key.data(), "dictionary", strlen("dictionary")))
|
2015-01-30 13:43:16 +00:00
|
|
|
{
|
2015-03-23 13:27:33 +00:00
|
|
|
if (0 != strncmp(key.data(), "comment", strlen("comment")))
|
|
|
|
LOG_WARNING(log,
|
|
|
|
config_path << ": unknown node in dictionaries file: '" << key + "', 'dictionary'");
|
|
|
|
|
2015-03-18 09:55:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-03-18 09:55:02 +00:00
|
|
|
try
|
|
|
|
{
|
2015-03-06 16:13:59 +00:00
|
|
|
name = config->getString(key + ".name");
|
2015-02-04 13:21:50 +00:00
|
|
|
if (name.empty())
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
LOG_WARNING(log, config_path << ": dictionary name cannot be empty");
|
2015-02-04 13:21:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-01-30 13:43:16 +00:00
|
|
|
|
2015-03-18 16:07:15 +00:00
|
|
|
auto it = dictionaries.find(name);
|
|
|
|
if (it != std::end(dictionaries))
|
2015-05-26 11:53:58 +00:00
|
|
|
if (it->second.origin != config_path)
|
|
|
|
throw std::runtime_error{"Overriding dictionary from file " + it->second.origin};
|
2015-03-18 16:07:15 +00:00
|
|
|
|
2015-02-10 14:50:43 +00:00
|
|
|
auto dict_ptr = DictionaryFactory::instance().create(name, *config, key, context);
|
2015-02-04 13:21:50 +00:00
|
|
|
if (!dict_ptr->isCached())
|
|
|
|
{
|
|
|
|
const auto & lifetime = dict_ptr->getLifetime();
|
2015-02-04 15:18:29 +00:00
|
|
|
if (lifetime.min_sec != 0 && lifetime.max_sec != 0)
|
|
|
|
{
|
|
|
|
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-02-04 13:21:50 +00:00
|
|
|
}
|
2015-02-03 11:36:07 +00:00
|
|
|
|
2015-02-04 13:21:50 +00:00
|
|
|
/// add new dictionary or update an existing version
|
2015-02-10 17:40:40 +00:00
|
|
|
if (it == std::end(dictionaries))
|
2015-02-04 13:21:50 +00:00
|
|
|
{
|
2015-02-10 17:40:40 +00:00
|
|
|
const std::lock_guard<std::mutex> lock{dictionaries_mutex};
|
2015-05-26 11:53:58 +00:00
|
|
|
dictionaries.emplace(name, dictionary_info{
|
2015-03-18 16:07:15 +00:00
|
|
|
std::make_shared<MultiVersion<IDictionary>>(dict_ptr.release()),
|
|
|
|
config_path
|
|
|
|
});
|
2015-02-04 13:21:50 +00:00
|
|
|
}
|
|
|
|
else
|
2015-05-26 11:53:58 +00:00
|
|
|
{
|
|
|
|
if (it->second.dict)
|
|
|
|
it->second.dict->set(dict_ptr.release());
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const std::lock_guard<std::mutex> lock{dictionaries_mutex};
|
|
|
|
it->second.dict = std::make_shared<MultiVersion<IDictionary>>(dict_ptr.release());
|
|
|
|
}
|
2015-03-24 09:46:07 +00:00
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
/// erase stored exception on success
|
|
|
|
it->second.exception = std::exception_ptr{};
|
|
|
|
}
|
2015-02-04 13:21:50 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
2015-02-03 11:36:07 +00:00
|
|
|
{
|
2015-03-06 16:13:59 +00:00
|
|
|
if (!name.empty())
|
2015-05-26 11:53:58 +00:00
|
|
|
{
|
|
|
|
const auto exception_ptr = std::current_exception();
|
|
|
|
const auto it = dictionaries.find(name);
|
|
|
|
if (it == std::end(dictionaries))
|
|
|
|
{
|
|
|
|
const std::lock_guard<std::mutex> lock{dictionaries_mutex};
|
|
|
|
dictionaries.emplace(name, dictionary_info{nullptr, config_path, exception_ptr});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
it->second.exception = exception_ptr;
|
|
|
|
}
|
2015-03-06 16:13:59 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
catch (const Poco::Exception & e)
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
LOG_ERROR(log, config_path << ": cannot create external dictionary '" << name
|
2015-03-18 09:55:02 +00:00
|
|
|
<< "'! You must resolve this manually. " << e.displayText());
|
2015-03-06 16:13:59 +00:00
|
|
|
}
|
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
LOG_ERROR(log, config_path << ": cannot create external dictionary '" << name
|
2015-03-18 09:55:02 +00:00
|
|
|
<< "'! You must resolve this manually. " << e.what());
|
2015-03-06 16:13:59 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2015-03-18 16:07:15 +00:00
|
|
|
LOG_ERROR(log, config_path << ": cannot create external dictionary '" << name
|
2015-03-18 09:55:02 +00:00
|
|
|
<< "'! You must resolve this manually.");
|
2015-03-06 16:13:59 +00:00
|
|
|
}
|
2015-03-27 13:11:22 +00:00
|
|
|
|
|
|
|
/// propagate exception
|
|
|
|
if (throw_on_error)
|
2015-05-26 11:53:58 +00:00
|
|
|
throw;
|
2015-02-03 11:36:07 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
}
|
2015-01-26 15:27:51 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-21 11:39:48 +00:00
|
|
|
|
2015-05-26 11:53:58 +00:00
|
|
|
MultiVersion<IDictionary>::Version ExternalDictionaries::getDictionary(const std::string & name) const
|
|
|
|
{
|
|
|
|
const std::lock_guard<std::mutex> lock{dictionaries_mutex};
|
|
|
|
const auto it = dictionaries.find(name);
|
|
|
|
|
|
|
|
if (it == std::end(dictionaries))
|
|
|
|
throw Exception{
|
|
|
|
"No such dictionary: " + name,
|
|
|
|
ErrorCodes::BAD_ARGUMENTS
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!it->second.dict && it->second.exception)
|
|
|
|
std::rethrow_exception(it->second.exception);
|
|
|
|
|
|
|
|
return it->second.dict->get();
|
|
|
|
}
|
|
|
|
|
2015-01-21 11:39:48 +00:00
|
|
|
}
|