Skip to content

Commit

Permalink
Do not write preprocessed configs to /etc/ (#2443)
Browse files Browse the repository at this point in the history
  • Loading branch information
proller authored Nov 27, 2018
1 parent 1ea31e0 commit f1791e9
Show file tree
Hide file tree
Showing 30 changed files with 133 additions and 75 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.draft.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
* Настройка `enable_optimize_predicate_expression` выключена по-умолчанию.

### Улучшения:
* Файлы *-preprocessed.xml записываются в директорию с данными (/var/lib/clickhouse/preprocessed_configs). Для /etc/clickhouse-server больше не нужен +w для пользователя clickhouse. Для удобства создан симлинк /var/lib/clickhouse/preprocessed_configs -> /etc/clickhouse-server/preprocessed
8 changes: 4 additions & 4 deletions dbms/programs/extract-from-config/ExtractFromConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ static void setupLogging(const std::string & log_level)
static std::string extractFromConfig(
const std::string & config_path, const std::string & key, bool process_zk_includes, bool try_get = false)
{
ConfigProcessor processor(config_path, /* throw_on_bad_incl = */ false, /* log_to_console = */ false);
DB::ConfigProcessor processor(config_path, /* throw_on_bad_incl = */ false, /* log_to_console = */ false);
bool has_zk_includes;
XMLDocumentPtr config_xml = processor.processConfig(&has_zk_includes);
DB::XMLDocumentPtr config_xml = processor.processConfig(&has_zk_includes);
if (has_zk_includes && process_zk_includes)
{
ConfigurationPtr bootstrap_configuration(new Poco::Util::XMLConfiguration(config_xml));
DB::ConfigurationPtr bootstrap_configuration(new Poco::Util::XMLConfiguration(config_xml));
zkutil::ZooKeeperPtr zookeeper = std::make_shared<zkutil::ZooKeeper>(
*bootstrap_configuration, "zookeeper");
zkutil::ZooKeeperNodeCache zk_node_cache([&] { return zookeeper; });
config_xml = processor.processConfig(&has_zk_includes, &zk_node_cache);
}
ConfigurationPtr configuration(new Poco::Util::XMLConfiguration(config_xml));
DB::ConfigurationPtr configuration(new Poco::Util::XMLConfiguration(config_xml));
// do not throw exception if not found
if (try_get)
return configuration->getString(key, "");
Expand Down
8 changes: 5 additions & 3 deletions dbms/programs/local/LocalServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ try
/// Load config files if exists
if (config().has("config-file") || Poco::File("config.xml").exists())
{
ConfigProcessor config_processor(config().getString("config-file", "config.xml"), false, true);
const auto config_path = config().getString("config-file", "config.xml");
ConfigProcessor config_processor(config_path, false, true);
config_processor.setConfigPath(Poco::Path(config_path).makeParent().toString());
auto loaded_config = config_processor.loadConfig();
config_processor.savePreprocessedConfig(loaded_config);
config_processor.savePreprocessedConfig(loaded_config, loaded_config.configuration->getString("path", DBMS_DEFAULT_PATH));
config().add(loaded_config.configuration.duplicate(), PRIO_DEFAULT, false);
}

Expand Down Expand Up @@ -348,7 +350,7 @@ void LocalServer::setupUsers()
const auto users_config_path = config().getString("users_config", config().getString("config-file", "config.xml"));
ConfigProcessor config_processor(users_config_path);
const auto loaded_config = config_processor.loadConfig();
config_processor.savePreprocessedConfig(loaded_config);
config_processor.savePreprocessedConfig(loaded_config, config().getString("path", DBMS_DEFAULT_PATH));
users_config = loaded_config.configuration;
}
else
Expand Down
8 changes: 5 additions & 3 deletions dbms/programs/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void Server::initialize(Poco::Util::Application & self)

std::string Server::getDefaultCorePath() const
{
return getCanonicalPath(config().getString("path")) + "cores";
return getCanonicalPath(config().getString("path", DBMS_DEFAULT_PATH)) + "cores";
}

int Server::main(const std::vector<std::string> & /*args*/)
Expand Down Expand Up @@ -129,7 +129,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
ConfigProcessor config_processor(config_path);
loaded_config = config_processor.loadConfigWithZooKeeperIncludes(
main_config_zk_node_cache, /* fallback_to_preprocessed = */ true);
config_processor.savePreprocessedConfig(loaded_config);
config_processor.savePreprocessedConfig(loaded_config, config().getString("path", DBMS_DEFAULT_PATH));
config().removeConfiguration(old_configuration.get());
config().add(loaded_config.configuration.duplicate(), PRIO_DEFAULT, false);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
}
#endif

std::string path = getCanonicalPath(config().getString("path"));
std::string path = getCanonicalPath(config().getString("path", DBMS_DEFAULT_PATH));
std::string default_database = config().getString("default_database", "default");

global_context->setPath(path);
Expand Down Expand Up @@ -301,6 +301,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
std::string include_from_path = config().getString("include_from", "/etc/metrika.xml");
auto main_config_reloader = std::make_unique<ConfigReloader>(config_path,
include_from_path,
config().getString("path", ""),
std::move(main_config_zk_node_cache),
[&](ConfigurationPtr config)
{
Expand All @@ -322,6 +323,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
}
auto users_config_reloader = std::make_unique<ConfigReloader>(users_config_path,
include_from_path,
config().getString("path", ""),
zkutil::ZooKeeperNodeCache([&] { return global_context->getZooKeeper(); }),
[&](ConfigurationPtr config) { global_context->setUsersConfig(config); },
/* already_loaded = */ false);
Expand Down
1 change: 0 additions & 1 deletion dbms/src/Client/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <Poco/Net/SecureStreamSocket.h>
#endif


namespace CurrentMetrics
{
extern const Metric SendExternalTables;
Expand Down
59 changes: 48 additions & 11 deletions dbms/src/Common/Config/ConfigProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

using namespace Poco::XML;

namespace DB
{

/// For cutting prerpocessed path to this base
std::string main_config_path;

/// Extracts from a string the first encountered number consisting of at least two digits.
static std::string numberFromHost(const std::string & s)
Expand All @@ -40,13 +45,6 @@ static std::string numberFromHost(const std::string & s)
return "";
}

static std::string preprocessedConfigPath(const std::string & path)
{
Poco::Path preprocessed_path(path);
preprocessed_path.setBaseName(preprocessed_path.getBaseName() + PREPROCESSED_SUFFIX);
return preprocessed_path.toString();
}

bool ConfigProcessor::isPreprocessedFile(const std::string & path)
{
return endsWith(Poco::Path(path).getBaseName(), PREPROCESSED_SUFFIX);
Expand All @@ -59,7 +57,6 @@ ConfigProcessor::ConfigProcessor(
bool log_to_console,
const Substitutions & substitutions_)
: path(path_)
, preprocessed_path(preprocessedConfigPath(path))
, throw_on_bad_incl(throw_on_bad_incl_)
, substitutions(substitutions_)
/// We need larger name pool to allow to support vast amount of users in users.xml files for ClickHouse.
Expand Down Expand Up @@ -522,7 +519,7 @@ ConfigProcessor::LoadedConfig ConfigProcessor::loadConfig(bool allow_zk_includes

ConfigurationPtr configuration(new Poco::Util::XMLConfiguration(config_xml));

return LoadedConfig{configuration, has_zk_includes, /* loaded_from_preprocessed = */ false, config_xml};
return LoadedConfig{configuration, has_zk_includes, /* loaded_from_preprocessed = */ false, config_xml, path};
}

ConfigProcessor::LoadedConfig ConfigProcessor::loadConfigWithZooKeeperIncludes(
Expand Down Expand Up @@ -556,11 +553,44 @@ ConfigProcessor::LoadedConfig ConfigProcessor::loadConfigWithZooKeeperIncludes(

ConfigurationPtr configuration(new Poco::Util::XMLConfiguration(config_xml));

return LoadedConfig{configuration, has_zk_includes, !processed_successfully, config_xml};
return LoadedConfig{configuration, has_zk_includes, !processed_successfully, config_xml, path};
}

void ConfigProcessor::savePreprocessedConfig(const LoadedConfig & loaded_config)
void ConfigProcessor::savePreprocessedConfig(const LoadedConfig & loaded_config, std::string preprocessed_dir)
{
if (preprocessed_path.empty())
{
auto new_path = loaded_config.config_path;
if (new_path.substr(0, main_config_path.size()) == main_config_path)
new_path.replace(0, main_config_path.size(), "");
std::replace(new_path.begin(), new_path.end(), '/', '_');

if (preprocessed_dir.empty())
{
if (!loaded_config.configuration->has("path"))
{
// Will use current directory
auto parent_path = Poco::Path(loaded_config.config_path).makeParent();
preprocessed_dir = parent_path.toString();
Poco::Path poco_new_path(new_path);
poco_new_path.setBaseName(poco_new_path.getBaseName() + PREPROCESSED_SUFFIX);
new_path = poco_new_path.toString();
}
else
{
preprocessed_dir = loaded_config.configuration->getString("path") + "/preprocessed_configs/";
}
}
else
{
preprocessed_dir += "/preprocessed_configs/";
}

preprocessed_path = preprocessed_dir + new_path;
auto path = Poco::Path(preprocessed_path).makeParent();
if (!path.toString().empty())
Poco::File(path).createDirectories();
}
try
{
DOMWriter().writeNode(preprocessed_path, loaded_config.preprocessed_xml);
Expand All @@ -570,3 +600,10 @@ void ConfigProcessor::savePreprocessedConfig(const LoadedConfig & loaded_config)
LOG_WARNING(log, "Couldn't save preprocessed config to " << preprocessed_path << ": " << e.displayText());
}
}

void ConfigProcessor::setConfigPath(const std::string & config_path)
{
main_config_path = config_path;
}

}
15 changes: 13 additions & 2 deletions dbms/src/Common/Config/ConfigProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ namespace zkutil
class ZooKeeperNodeCache;
}

namespace DB
{

using ConfigurationPtr = Poco::AutoPtr<Poco::Util::AbstractConfiguration>;
using XMLDocumentPtr = Poco::AutoPtr<Poco::XML::Document>;

Expand Down Expand Up @@ -72,6 +75,7 @@ class ConfigProcessor
bool has_zk_includes;
bool loaded_from_preprocessed;
XMLDocumentPtr preprocessed_xml;
std::string config_path;
};

/// If allow_zk_includes is true, expect that the configuration XML can contain from_zk nodes.
Expand All @@ -85,7 +89,12 @@ class ConfigProcessor
zkutil::ZooKeeperNodeCache & zk_node_cache,
bool fallback_to_preprocessed = false);

void savePreprocessedConfig(const LoadedConfig & loaded_config);
/// Save preprocessed config to specified directory.
/// If preprocessed_dir is empty - calculate from loaded_config.path + /preprocessed_configs/
void savePreprocessedConfig(const LoadedConfig & loaded_config, std::string preprocessed_dir);

/// Set path of main config.xml . It will be cutted from all configs placed to preprocessed_configs/
void setConfigPath(const std::string & config_path);

public:
using Files = std::vector<std::string>;
Expand All @@ -99,7 +108,7 @@ class ConfigProcessor

private:
const std::string path;
const std::string preprocessed_path;
std::string preprocessed_path;

bool throw_on_bad_incl;

Expand Down Expand Up @@ -127,3 +136,5 @@ class ConfigProcessor
zkutil::ZooKeeperNodeCache * zk_node_cache,
std::unordered_set<std::string> & contributing_zk_paths);
};

}
4 changes: 3 additions & 1 deletion dbms/src/Common/Config/ConfigReloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ constexpr decltype(ConfigReloader::reload_interval) ConfigReloader::reload_inter
ConfigReloader::ConfigReloader(
const std::string & path_,
const std::string & include_from_path_,
const std::string & preprocessed_dir_,
zkutil::ZooKeeperNodeCache && zk_node_cache_,
Updater && updater_,
bool already_loaded)
: path(path_), include_from_path(include_from_path_)
, preprocessed_dir(preprocessed_dir_)
, zk_node_cache(std::move(zk_node_cache_))
, updater(std::move(updater_))
{
Expand Down Expand Up @@ -98,7 +100,7 @@ void ConfigReloader::reloadIfNewer(bool force, bool throw_on_error, bool fallbac
tryLogCurrentException(log, "Error loading config from `" + path + "'");
return;
}
config_processor.savePreprocessedConfig(loaded_config);
config_processor.savePreprocessedConfig(loaded_config, preprocessed_dir);

/** We should remember last modification time if and only if config was sucessfully loaded
* Otherwise a race condition could occur during config files update:
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Common/Config/ConfigReloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ConfigReloader
ConfigReloader(
const std::string & path,
const std::string & include_from_path,
const std::string & preprocessed_dir,
zkutil::ZooKeeperNodeCache && zk_node_cache,
Updater && updater,
bool already_loaded);
Expand Down Expand Up @@ -70,6 +71,7 @@ class ConfigReloader

std::string path;
std::string include_from_path;
std::string preprocessed_dir;
FilesChangesTracker files;
zkutil::ZooKeeperNodeCache zk_node_cache;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ int main(int argc, char ** argv)
return 3;
}

ConfigProcessor processor(argv[1], false, true);
DB::ConfigProcessor processor(argv[1], false, true);
auto config = processor.loadConfig().configuration;
zkutil::ZooKeeper zk(*config, "zookeeper");
zkutil::EventPtr watch = std::make_shared<Poco::Event>();
Expand Down
2 changes: 2 additions & 0 deletions dbms/src/Core/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
/// the number is unmotivated
#define DEFAULT_COUNT_OF_HTTP_CONNECTIONS_PER_ENDPOINT 15

#define DBMS_DEFAULT_PATH "/var/lib/clickhouse/"

// more aliases: https://mailman.videolan.org/pipermail/x264-devel/2014-May/010660.html

#if defined(_MSC_VER)
Expand Down
11 changes: 6 additions & 5 deletions dbms/src/Interpreters/ExternalLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Interpreters/ExternalLoader.h>
#include "ExternalLoader.h"
#include <Core/Defines.h>
#include <Common/StringUtils/StringUtils.h>
#include <Common/MemoryTracker.h>
#include <Common/Exception.h>
Expand Down Expand Up @@ -42,12 +43,12 @@ void ExternalLoader::reloadPeriodically()
}


ExternalLoader::ExternalLoader(const Poco::Util::AbstractConfiguration & config,
ExternalLoader::ExternalLoader(const Poco::Util::AbstractConfiguration & config_main,
const ExternalLoaderUpdateSettings & update_settings,
const ExternalLoaderConfigSettings & config_settings,
std::unique_ptr<IExternalLoaderConfigRepository> config_repository,
Logger * log, const std::string & loadable_object_name)
: config(config)
: config_main(config_main)
, update_settings(update_settings)
, config_settings(config_settings)
, config_repository(std::move(config_repository))
Expand Down Expand Up @@ -214,7 +215,7 @@ void ExternalLoader::reloadAndUpdate(bool throw_on_error)

void ExternalLoader::reloadFromConfigFiles(const bool throw_on_error, const bool force_reload, const std::string & only_dictionary)
{
const auto config_paths = config_repository->list(config, config_settings.path_setting_name);
const auto config_paths = config_repository->list(config_main, config_settings.path_setting_name);

for (const auto & config_path : config_paths)
{
Expand Down Expand Up @@ -262,7 +263,7 @@ void ExternalLoader::reloadFromConfigFile(const std::string & config_path, const
const auto last_modified = config_repository->getLastModificationTime(config_path);
if (force_reload || last_modified > config_last_modified)
{
auto loaded_config = config_repository->load(config_path);
auto loaded_config = config_repository->load(config_path, config_main.getString("path", DBMS_DEFAULT_PATH));

loadable_objects_defined_in_config[config_path].clear();

Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Interpreters/ExternalLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class ExternalLoader
using ObjectsMap = std::unordered_map<std::string, LoadableInfo>;

/// Objects will be loaded immediately and then will be updated in separate thread, each 'reload_period' seconds.
ExternalLoader(const Configuration & config,
ExternalLoader(const Configuration & config_main,
const ExternalLoaderUpdateSettings & update_settings,
const ExternalLoaderConfigSettings & config_settings,
std::unique_ptr<IExternalLoaderConfigRepository> config_repository,
Expand Down Expand Up @@ -151,7 +151,7 @@ class ExternalLoader

pcg64 rnd_engine{randomSeed()};

const Configuration & config;
const Configuration & config_main;
const ExternalLoaderUpdateSettings & update_settings;
const ExternalLoaderConfigSettings & config_settings;

Expand Down
4 changes: 2 additions & 2 deletions dbms/src/Interpreters/ExternalLoaderConfigRepository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ Poco::Timestamp ExternalLoaderConfigRepository::getLastModificationTime(
}

Poco::AutoPtr<Poco::Util::AbstractConfiguration> ExternalLoaderConfigRepository::load(
const std::string & config_file) const
const std::string & config_file, const std::string & preprocessed_dir) const
{
ConfigProcessor config_processor{config_file};
ConfigProcessor::LoadedConfig preprocessed = config_processor.loadConfig();
config_processor.savePreprocessedConfig(preprocessed);
config_processor.savePreprocessedConfig(preprocessed, preprocessed_dir);
return preprocessed.configuration;
}

Expand Down
2 changes: 1 addition & 1 deletion dbms/src/Interpreters/ExternalLoaderConfigRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ExternalLoaderConfigRepository : public IExternalLoaderConfigRepository

Poco::Timestamp getLastModificationTime(const std::string & config_file) const override;

Poco::AutoPtr<Poco::Util::AbstractConfiguration> load(const std::string & config_file) const override;
Poco::AutoPtr<Poco::Util::AbstractConfiguration> load(const std::string & config_file, const std::string & preprocessed_dir = "") const override;
};

}
2 changes: 1 addition & 1 deletion dbms/src/Interpreters/IExternalLoaderConfigRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class IExternalLoaderConfigRepository

virtual Poco::Timestamp getLastModificationTime(const std::string & config_file) const = 0;

virtual Poco::AutoPtr<Poco::Util::AbstractConfiguration> load(const std::string & config_file) const = 0;
virtual Poco::AutoPtr<Poco::Util::AbstractConfiguration> load(const std::string & config_file, const std::string & preprocessed_dir = "") const = 0;

virtual ~IExternalLoaderConfigRepository() {}
};
Expand Down
Loading

0 comments on commit f1791e9

Please sign in to comment.