From e0c82c01ca63d68f63cf8df7667f0e3b2429a43b Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Mon, 10 Apr 2023 17:14:59 +0800 Subject: [PATCH] Allow TiFlash server run without a config file (#7251) ref pingcap/tiflash#4809 --- contrib/tiflash-proxy | 2 +- dbms/src/Common/ComputeLabelHolder.cpp | 2 +- dbms/src/Common/Config/ConfigProcessor.cpp | 8 - dbms/src/Common/Config/ConfigProcessor.h | 2 - dbms/src/Common/Config/ConfigReloader.cpp | 6 +- dbms/src/Common/FileChangesTracker.h | 5 + dbms/src/Common/UnifiedLogFormatter.cpp | 254 +++++++++++++----- dbms/src/Common/UnifiedLogFormatter.h | 31 +-- dbms/src/Common/tests/bench_logger.cpp | 2 +- dbms/src/Common/tests/gtest_logger.cpp | 2 +- dbms/src/Encryption/FileProvider.cpp | 12 +- dbms/src/Interpreters/Context.cpp | 1 - .../ExternalLoaderConfigRepository.cpp | 3 +- dbms/src/Server/DTTool/DTTool.h | 2 +- dbms/src/Server/RaftConfigParser.cpp | 10 +- dbms/src/Server/Server.cpp | 90 ++++--- dbms/src/Server/Server.h | 6 + dbms/src/Server/StorageConfigParser.cpp | 16 +- dbms/src/Server/UserConfigParser.cpp | 6 + dbms/src/Storages/DeltaMerge/File/DMFile.cpp | 6 +- .../Storages/Page/workload/PSStressEnv.cpp | 2 +- dbms/src/Storages/Transaction/ProxyFFI.cpp | 8 +- .../Transaction/ProxyFFIStatusService.cpp | 6 +- dbms/src/TestUtils/TiFlashTestEnv.cpp | 2 +- libs/libdaemon/src/BaseDaemon.cpp | 34 ++- 25 files changed, 341 insertions(+), 177 deletions(-) diff --git a/contrib/tiflash-proxy b/contrib/tiflash-proxy index 8d13ac0a371..5bd6c2b2b5f 160000 --- a/contrib/tiflash-proxy +++ b/contrib/tiflash-proxy @@ -1 +1 @@ -Subproject commit 8d13ac0a37114d6d4417be48d3da5e9ca7906a11 +Subproject commit 5bd6c2b2b5ff6a9f90de900c8e8c1eb4cfecc129 diff --git a/dbms/src/Common/ComputeLabelHolder.cpp b/dbms/src/Common/ComputeLabelHolder.cpp index cc3d973c966..af8b068e32b 100644 --- a/dbms/src/Common/ComputeLabelHolder.cpp +++ b/dbms/src/Common/ComputeLabelHolder.cpp @@ -33,7 +33,7 @@ void ComputeLabelHolder::init(const Poco::Util::LayeredConfiguration & conf) std::lock_guard lock(mu); RUNTIME_ASSERT(!label_got, log, "Can't init after label got"); cluster_id = conf.getString(cluster_id_key, "unknown"); - auto service_addr = conf.getString("flash.service_addr", "unknown"); + auto service_addr = conf.getString("flash.service_addr", "0.0.0.0:3930"); std::replace(service_addr.begin(), service_addr.end(), ':', '_'); std::replace(service_addr.begin(), service_addr.end(), '.', '_'); process_id = fmt::format("compute_{}_{}", service_addr, microsecondsUTC()); diff --git a/dbms/src/Common/Config/ConfigProcessor.cpp b/dbms/src/Common/Config/ConfigProcessor.cpp index c9a0d25afcb..40bd0b4c096 100644 --- a/dbms/src/Common/Config/ConfigProcessor.cpp +++ b/dbms/src/Common/Config/ConfigProcessor.cpp @@ -84,11 +84,3 @@ ConfigProcessor::LoadedConfig ConfigProcessor::loadConfig() return LoadedConfig{configuration, false, config_doc}; } - -void ConfigProcessor::savePreprocessedConfig(const LoadedConfig & loaded_config) -{ - std::ofstream out(preprocessed_path); - cpptoml::toml_writer writer(out); - loaded_config.preprocessed_conf->accept(std::move(writer)); - out.close(); -} diff --git a/dbms/src/Common/Config/ConfigProcessor.h b/dbms/src/Common/Config/ConfigProcessor.h index 7618f58e915..e315459f780 100644 --- a/dbms/src/Common/Config/ConfigProcessor.h +++ b/dbms/src/Common/Config/ConfigProcessor.h @@ -63,8 +63,6 @@ class ConfigProcessor LoadedConfig loadConfig(); - void savePreprocessedConfig(const LoadedConfig & loaded_config); - public: /// Is the file named as result of config preprocessing, not as original files. static bool isPreprocessedFile(const std::string & config_path); diff --git a/dbms/src/Common/Config/ConfigReloader.cpp b/dbms/src/Common/Config/ConfigReloader.cpp index baf716b44cd..11f4d9ab3c9 100644 --- a/dbms/src/Common/Config/ConfigReloader.cpp +++ b/dbms/src/Common/Config/ConfigReloader.cpp @@ -85,7 +85,9 @@ void ConfigReloader::reloadIfNewer(bool force, bool throw_on_error) } } - if (force || new_files.isDifferOrNewerThan(files) || config_object_updated) + if (force + || (new_files.valid() && new_files.isDifferOrNewerThan(files)) + || config_object_updated) { ConfigProcessor config_processor(path); ConfigProcessor::LoadedConfig loaded_config; @@ -103,8 +105,6 @@ void ConfigReloader::reloadIfNewer(bool force, bool throw_on_error) return; } - config_processor.savePreprocessedConfig(loaded_config); - /** We should remember last modification time if and only if config was sucessfully loaded * Otherwise a race condition could occur during config files update: * File is contain raw (and non-valid) data, therefore config is not applied. diff --git a/dbms/src/Common/FileChangesTracker.h b/dbms/src/Common/FileChangesTracker.h index 10af6c6f6be..edd2fdd0802 100644 --- a/dbms/src/Common/FileChangesTracker.h +++ b/dbms/src/Common/FileChangesTracker.h @@ -43,6 +43,11 @@ struct FilesChangesTracker { std::set files; + bool valid() const + { + return !files.empty(); + } + void addIfExists(const std::string & path) { if (!path.empty() && Poco::File(path).exists()) diff --git a/dbms/src/Common/UnifiedLogFormatter.cpp b/dbms/src/Common/UnifiedLogFormatter.cpp index 18ded330cf0..5a9e88f9a67 100644 --- a/dbms/src/Common/UnifiedLogFormatter.cpp +++ b/dbms/src/Common/UnifiedLogFormatter.cpp @@ -23,55 +23,100 @@ #include #include +#define RESET_FONT "\033[0m" +#define BOLD_FONT "\033[1m" +#define BLACK "\033[30m" +#define RED "\033[31m" +#define GREEN "\033[32m" +#define YELLOW "\033[33m" +#define BLUE "\033[34m" +#define MAGENTA "\033[35m" +#define CYAN "\033[36m" +#define WHITE "\033[37m" +#define BRIGHT_BLACK "\033[90m" +#define BRIGHT_RED "\033[91m" +#define BRIGHT_GREEN "\033[92m" +#define BRIGHT_YELLOW "\033[93m" +#define BRIGHT_BLUE "\033[94m" +#define BRIGHT_MAGENTA "\033[95m" +#define BRIGHT_CYAN "\033[96m" +#define BRIGHT_WHITE "\033[97m" + namespace DB { -void UnifiedLogFormatter::format(const Poco::Message & msg, std::string & text) + +enum class JsonEncodeKind { - FmtBuffer buf; + /** + * No need to encode, just copy the text + */ + DirectCopy, - // Timestamp - { - buf.append('['); - writeTimestamp(buf); - buf.append("] "); - } - // Priority - { - buf.append('['); - writePriority(buf, msg.getPriority()); - buf.append("] "); - } - // Source File - { - if (unlikely(!msg.getSourceFile())) - buf.append("[] "); - else - buf.fmtAppend(FMT_COMPILE("[{}:{}] "), msg.getSourceFile(), msg.getSourceLine()); - } - // Message - { - buf.append('['); - writeEscapedString(buf, msg.getText()); - buf.append("] "); - } - // Source and Identifiers + /** + * Add double quotes around the text is sufficient + */ + AddQuoteAndCopy, + + /** + * Need full JSON string encode + */ + Encode, +}; + +void writePriorityColor(FmtBuffer & buf, const Poco::Message::Priority & priority) +{ + switch (priority) { - const std::string & source = msg.getSource(); - if (!source.empty()) - { - buf.append("[source="); - writeEscapedString(buf, source); - buf.append("] "); - } + case Poco::Message::Priority::PRIO_TRACE: + buf.append(BRIGHT_BLACK); + break; + case Poco::Message::Priority::PRIO_DEBUG: + buf.append(BRIGHT_BLACK); + break; + case Poco::Message::Priority::PRIO_INFORMATION: + buf.append(BRIGHT_BLUE); + break; + case Poco::Message::Priority::PRIO_WARNING: + buf.append(YELLOW); + break; + case Poco::Message::Priority::PRIO_ERROR: + buf.append(RED); + break; + case Poco::Message::Priority::PRIO_FATAL: + buf.append(RED); + break; + case Poco::Message::Priority::PRIO_CRITICAL: + buf.append(RED); + break; + case Poco::Message::Priority::PRIO_NOTICE: + break; + default: + break; } - // Thread ID +} + +void writeMessageColor(FmtBuffer & buf, const Poco::Message::Priority & priority) +{ + switch (priority) { - buf.fmtAppend(FMT_COMPILE("[thread_id={}]"), Poco::ThreadNumber::get()); + case Poco::Message::Priority::PRIO_WARNING: + buf.append(YELLOW); + break; + case Poco::Message::Priority::PRIO_ERROR: + buf.append(RED); + break; + case Poco::Message::Priority::PRIO_FATAL: + buf.append(RED); + break; + case Poco::Message::Priority::PRIO_CRITICAL: + buf.append(RED); + break; + default: + break; } - text = buf.toString(); } -void UnifiedLogFormatter::writePriority(FmtBuffer & buf, const Poco::Message::Priority & priority) +void writePriority(FmtBuffer & buf, const Poco::Message::Priority & priority) { switch (priority) { @@ -105,7 +150,7 @@ void UnifiedLogFormatter::writePriority(FmtBuffer & buf, const Poco::Message::Pr } } -void UnifiedLogFormatter::writeTimestamp(FmtBuffer & buf) +void writeTimestamp(FmtBuffer & buf) { // The format is "yyyy/MM/dd HH:mm:ss.SSS ZZZZZ" auto time_point = std::chrono::system_clock::now(); @@ -151,26 +196,7 @@ void UnifiedLogFormatter::writeTimestamp(FmtBuffer & buf) buf.fmtAppend(FMT_COMPILE("{0:02d}:{1:02d}"), offset_tm->tm_hour, offset_tm->tm_min); } -void UnifiedLogFormatter::writeEscapedString(FmtBuffer & buf, const std::string & str) -{ - auto encode_kind = needJsonEncode(str); - switch (encode_kind) - { - case JsonEncodeKind::DirectCopy: - buf.append(str); - break; - case JsonEncodeKind::AddQuoteAndCopy: - buf.append('"'); - buf.append(str); - buf.append('"'); - break; - case JsonEncodeKind::Encode: - writeJSONString(buf, str); - break; - } -} - -UnifiedLogFormatter::JsonEncodeKind UnifiedLogFormatter::needJsonEncode(const std::string & src) +JsonEncodeKind needJsonEncode(const std::string & src) { bool needs_quote = false; bool json_encode_cannot_copy = false; @@ -192,7 +218,7 @@ UnifiedLogFormatter::JsonEncodeKind UnifiedLogFormatter::needJsonEncode(const st return JsonEncodeKind::DirectCopy; } -void UnifiedLogFormatter::writeJSONString(FmtBuffer & buf, const std::string & str) +void writeJSONString(FmtBuffer & buf, const std::string & str) { buf.append('"'); @@ -226,4 +252,110 @@ void UnifiedLogFormatter::writeJSONString(FmtBuffer & buf, const std::string & s buf.append('"'); } +void writeEscapedString(FmtBuffer & buf, const std::string & str) +{ + auto encode_kind = needJsonEncode(str); + switch (encode_kind) + { + case JsonEncodeKind::DirectCopy: + buf.append(str); + break; + case JsonEncodeKind::AddQuoteAndCopy: + buf.append('"'); + buf.append(str); + buf.append('"'); + break; + case JsonEncodeKind::Encode: + writeJSONString(buf, str); + break; + } +} + +template +void UnifiedLogFormatter::format(const Poco::Message & msg, std::string & text) +{ + FmtBuffer buf; + + // Timestamp + { + if constexpr (enable_color) + buf.append(BRIGHT_GREEN); + + buf.append('['); + writeTimestamp(buf); + buf.append("] "); + + if constexpr (enable_color) + buf.append(RESET_FONT); + } + + // Priority + { + if constexpr (enable_color) + { + buf.append(BOLD_FONT); + writePriorityColor(buf, msg.getPriority()); + } + + buf.append('['); + writePriority(buf, msg.getPriority()); + buf.append("] "); + + if constexpr (enable_color) + buf.append(RESET_FONT); + } + + // Source File + { + if constexpr (enable_color) + buf.append(MAGENTA); + + if (unlikely(!msg.getSourceFile())) + buf.append("[] "); + else + buf.fmtAppend(FMT_COMPILE("[{}:{}] "), msg.getSourceFile(), msg.getSourceLine()); + + if constexpr (enable_color) + buf.append(RESET_FONT); + } + // Message + { + if constexpr (enable_color) + writeMessageColor(buf, msg.getPriority()); + + buf.append('['); + writeEscapedString(buf, msg.getText()); + buf.append("] "); + + if constexpr (enable_color) + buf.append(RESET_FONT); + } + + // Source and Identifiers + if constexpr (enable_color) + buf.append(BRIGHT_BLACK); + + { + const std::string & source = msg.getSource(); + if (!source.empty()) + { + buf.append("[source="); + writeEscapedString(buf, source); + buf.append("] "); + } + } + // Thread ID + { + buf.fmtAppend(FMT_COMPILE("[thread_id={}]"), Poco::ThreadNumber::get()); + } + + if constexpr (enable_color) + buf.append(RESET_FONT); + + text = buf.toString(); +} + +template class UnifiedLogFormatter; +template class UnifiedLogFormatter; + } // namespace DB diff --git a/dbms/src/Common/UnifiedLogFormatter.h b/dbms/src/Common/UnifiedLogFormatter.h index 89b6ed09019..471b0c6917c 100644 --- a/dbms/src/Common/UnifiedLogFormatter.h +++ b/dbms/src/Common/UnifiedLogFormatter.h @@ -22,44 +22,15 @@ namespace DB { -class WriteBuffer; /// https://github.com/tikv/rfcs/blob/ed764d7d014c420ee0cbcde99597020c4f75346d/text/0018-unified-log-format.md +template class UnifiedLogFormatter : public Poco::Formatter { public: UnifiedLogFormatter() = default; void format(const Poco::Message & msg, std::string & text) override; - -private: - enum class JsonEncodeKind - { - /** - * No need to encode, just copy the text - */ - DirectCopy, - - /** - * Add double quotes around the text is sufficient - */ - AddQuoteAndCopy, - - /** - * Need full JSON string encode - */ - Encode, - }; - - static void writePriority(FmtBuffer & buf, const Poco::Message::Priority & priority); - - static void writeTimestamp(FmtBuffer & buf); - - static JsonEncodeKind needJsonEncode(const std::string & src); - - static void writeJSONString(FmtBuffer & buf, const std::string & str); - - static void writeEscapedString(FmtBuffer & buf, const std::string & str); }; } // namespace DB diff --git a/dbms/src/Common/tests/bench_logger.cpp b/dbms/src/Common/tests/bench_logger.cpp index 4c2b0b02cfc..f420dbbcdd6 100644 --- a/dbms/src/Common/tests/bench_logger.cpp +++ b/dbms/src/Common/tests/bench_logger.cpp @@ -72,7 +72,7 @@ class LogBM : public benchmark::Fixture void SetUp(const ::benchmark::State & /*state*/) { Poco::AutoPtr channel(new TestChannel()); - Poco::AutoPtr formatter(new UnifiedLogFormatter()); + Poco::AutoPtr formatter(new UnifiedLogFormatter()); Poco::AutoPtr formatting_channel(new Poco::FormattingChannel(formatter, channel)); Poco::Logger::root().setChannel(formatting_channel); Poco::Logger::root().setLevel(Poco::Message::PRIO_TRACE); diff --git a/dbms/src/Common/tests/gtest_logger.cpp b/dbms/src/Common/tests/gtest_logger.cpp index 3538e4b7e0e..49aef68054d 100644 --- a/dbms/src/Common/tests/gtest_logger.cpp +++ b/dbms/src/Common/tests/gtest_logger.cpp @@ -33,7 +33,7 @@ class LogMacroTest : public testing::Test void SetUp() override { RUNTIME_CHECK(channel_backup == nullptr); - Poco::AutoPtr formatter(new UnifiedLogFormatter()); + Poco::AutoPtr formatter(new UnifiedLogFormatter()); Poco::AutoPtr formatting_channel(new Poco::FormattingChannel(formatter, channel)); channel_backup = Poco::Logger::root().getChannel(); Poco::Logger::root().setChannel(formatting_channel); diff --git a/dbms/src/Encryption/FileProvider.cpp b/dbms/src/Encryption/FileProvider.cpp index 767678e8f85..33677f51bbf 100644 --- a/dbms/src/Encryption/FileProvider.cpp +++ b/dbms/src/Encryption/FileProvider.cpp @@ -43,7 +43,11 @@ RandomAccessFilePtr FileProvider::newRandomAccessFile( } else { - RUNTIME_CHECK(Poco::Path(file_path_).isAbsolute(), file_path_); + // Unrecognized xx:// protocol. + RUNTIME_CHECK_MSG( + file_path_.find("://") == std::string::npos, + "Unsupported protocol in path {}", + file_path_); file = std::make_shared(file_path_, flags, read_limiter); } auto encryption_info = key_manager->getFile(encryption_path_.full_path); @@ -70,7 +74,11 @@ WritableFilePtr FileProvider::newWritableFile( } else { - RUNTIME_CHECK(Poco::Path(file_path_).isAbsolute(), file_path_); + // Unrecognized xx:// protocol. + RUNTIME_CHECK_MSG( + file_path_.find("://") == std::string::npos, + "Unsupported protocol in path {}", + file_path_); file = std::make_shared(file_path_, truncate_if_exists_, flags, mode, write_limiter_); } if (encryption_enabled && create_new_encryption_info_) diff --git a/dbms/src/Interpreters/Context.cpp b/dbms/src/Interpreters/Context.cpp index 6c09f056716..6eea2d6a913 100644 --- a/dbms/src/Interpreters/Context.cpp +++ b/dbms/src/Interpreters/Context.cpp @@ -620,7 +620,6 @@ ConfigurationPtr Context::getUsersConfig() void Context::setSecurityConfig(Poco::Util::AbstractConfiguration & config, const LoggerPtr & log) { - LOG_INFO(log, "Setting secuirty config."); auto lock = getLock(); shared->security_config = std::make_shared(log); shared->security_config->init(config); diff --git a/dbms/src/Interpreters/ExternalLoaderConfigRepository.cpp b/dbms/src/Interpreters/ExternalLoaderConfigRepository.cpp index ae923c76397..0b291818d9d 100644 --- a/dbms/src/Interpreters/ExternalLoaderConfigRepository.cpp +++ b/dbms/src/Interpreters/ExternalLoaderConfigRepository.cpp @@ -49,7 +49,7 @@ ExternalLoaderConfigRepository::Files ExternalLoaderConfigRepository::list( Poco::Glob::glob(pattern, files, 0); } - for (Files::iterator it = files.begin(); it != files.end();) + for (auto it = files.begin(); it != files.end();) { if (ConfigProcessor::isPreprocessedFile(*it)) files.erase(it++); @@ -76,7 +76,6 @@ Poco::AutoPtr ExternalLoaderConfigRepository: { ConfigProcessor config_processor{config_file}; ConfigProcessor::LoadedConfig preprocessed = config_processor.loadConfig(); - config_processor.savePreprocessedConfig(preprocessed); return preprocessed.configuration; } diff --git a/dbms/src/Server/DTTool/DTTool.h b/dbms/src/Server/DTTool/DTTool.h index fe0d4c16af4..51e57dab31f 100644 --- a/dbms/src/Server/DTTool/DTTool.h +++ b/dbms/src/Server/DTTool/DTTool.h @@ -127,7 +127,7 @@ class ImitativeEnv static void setupLogger() { Poco::AutoPtr channel = new Poco::ConsoleChannel(std::cout); - Poco::AutoPtr formatter(new UnifiedLogFormatter()); + Poco::AutoPtr formatter(new UnifiedLogFormatter()); Poco::AutoPtr formatting_channel(new Poco::FormattingChannel(formatter, channel)); Poco::Logger::root().setChannel(formatting_channel); Poco::Logger::root().setLevel("trace"); diff --git a/dbms/src/Server/RaftConfigParser.cpp b/dbms/src/Server/RaftConfigParser.cpp index c53a0c44a30..c2dc1e78532 100644 --- a/dbms/src/Server/RaftConfigParser.cpp +++ b/dbms/src/Server/RaftConfigParser.cpp @@ -35,8 +35,14 @@ TiFlashRaftConfig TiFlashRaftConfig::parseSettings(Poco::Util::AbstractConfigura TiFlashRaftConfig res; res.flash_server_addr = config.getString("flash.service_addr", "0.0.0.0:3930"); - if (!config.has("raft")) - return res; + { + // Check by `raft` prefix instead of check by `config.has("raft")`, + // because when sub keys are set from cli args, `raft` will not exist. + Poco::Util::AbstractConfiguration::Keys keys; + config.keys("raft", keys); + if (keys.empty()) + return res; + } if (config.has("raft.pd_addr")) { diff --git a/dbms/src/Server/Server.cpp b/dbms/src/Server/Server.cpp index 239ec3e795b..432aa3c1aec 100644 --- a/dbms/src/Server/Server.cpp +++ b/dbms/src/Server/Server.cpp @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -236,6 +237,31 @@ void Server::initialize(Poco::Util::Application & self) logger().information("starting up"); } +void Server::defineOptions(Poco::Util::OptionSet & options) +{ + options.addOption( + Poco::Util::Option("help", "h", "show help and exit") + .required(false) + .repeatable(false) + .binding("help")); + BaseDaemon::defineOptions(options); +} + +int Server::run() +{ + if (config().hasOption("help")) + { + Poco::Util::HelpFormatter help_formatter(Server::options()); + auto header_str = fmt::format("{} server [OPTION] [-- [POSITIONAL_ARGS]...]\n" + "POSITIONAL_ARGS can be used to rewrite config properties, for example, --http_port=8010", + commandName()); + help_formatter.setHeader(header_str); + help_formatter.format(std::cout); + return 0; + } + return BaseDaemon::run(); +} + std::string Server::getDefaultCorePath() const { return getCanonicalPath(config().getString("path")) + "cores"; @@ -243,19 +269,11 @@ std::string Server::getDefaultCorePath() const struct TiFlashProxyConfig { - static const std::string config_prefix; std::vector args; std::unordered_map val_map; bool is_proxy_runnable = false; // TiFlash Proxy will set the default value of "flash.proxy.addr", so we don't need to set here. - const String engine_store_version = "engine-version"; - const String engine_store_git_hash = "engine-git-hash"; - const String engine_store_address = "engine-addr"; - const String engine_store_advertise_address = "advertise-engine-addr"; - const String pd_endpoints = "pd-endpoints"; - const String engine_label = "engine-label"; - const String engine_role_label = "engine-role-label"; void addExtraArgs(const std::string & k, const std::string & v) { @@ -273,38 +291,41 @@ struct TiFlashProxyConfig // tiflash_compute doesn't need proxy. // todo: remove after AutoScaler is stable. if (disaggregated_mode == DisaggregatedMode::Compute && useAutoScaler(config)) + { + LOG_WARNING(Logger::get(), "TiFlash Proxy will not start because AutoScale Disaggregated Compute Mode is specified."); return; + } + + Poco::Util::AbstractConfiguration::Keys keys; + config.keys("flash.proxy", keys); + + if (!config.has("raft.pd_addr")) + { + LOG_WARNING(Logger::get(), "TiFlash Proxy will not start because `raft.pd_addr` is not configured."); + if (!keys.empty()) + LOG_WARNING(Logger::get(), "`flash.proxy.*` is ignored because TiFlash Proxy will not start."); - if (!config.has(config_prefix)) return; + } - Poco::Util::AbstractConfiguration::Keys keys; - config.keys(config_prefix, keys); { std::unordered_map args_map; for (const auto & key : keys) - { - const auto k = config_prefix + "." + key; - args_map[key] = config.getString(k); - } - args_map[pd_endpoints] = config.getString("raft.pd_addr"); - args_map[engine_store_version] = TiFlashBuildInfo::getReleaseVersion(); - args_map[engine_store_git_hash] = TiFlashBuildInfo::getGitHash(); - if (!args_map.count(engine_store_address)) - args_map[engine_store_address] = config.getString("flash.service_addr"); - else - args_map[engine_store_advertise_address] = args_map[engine_store_address]; + args_map[key] = config.getString("flash.proxy." + key); - args_map[engine_label] = getProxyLabelByDisaggregatedMode(disaggregated_mode); + args_map["pd-endpoints"] = config.getString("raft.pd_addr"); + args_map["engine-version"] = TiFlashBuildInfo::getReleaseVersion(); + args_map["engine-git-hash"] = TiFlashBuildInfo::getGitHash(); + if (!args_map.contains("engine-addr")) + args_map["engine-addr"] = config.getString("flash.service_addr", "0.0.0.0:3930"); + else + args_map["advertise-engine-addr"] = args_map["engine-addr"]; + args_map["engine-label"] = getProxyLabelByDisaggregatedMode(disaggregated_mode); if (disaggregated_mode != DisaggregatedMode::Compute && has_s3_config) - { - args_map[engine_role_label] = DISAGGREGATED_MODE_WRITE_ENGINE_ROLE; - } + args_map["engine-role-label"] = DISAGGREGATED_MODE_WRITE_ENGINE_ROLE; for (auto && [k, v] : args_map) - { val_map.emplace("--" + k, std::move(v)); - } } args.push_back("TiFlash Proxy"); @@ -317,8 +338,6 @@ struct TiFlashProxyConfig } }; -const std::string TiFlashProxyConfig::config_prefix = "flash.proxy"; - pingcap::ClusterConfig getClusterConfig(TiFlashSecurityConfigPtr security_config, const TiFlashRaftConfig & raft_config, const int api_version, const LoggerPtr & log) { pingcap::ClusterConfig config; @@ -984,6 +1003,10 @@ int Server::main(const std::vector & /*args*/) else LOG_INFO(log, "encryption is disabled"); } + else + { + LOG_WARNING(log, "Skipped initialize TiFlash Proxy"); + } SCOPE_EXIT({ if (!proxy_conf.is_proxy_runnable) @@ -1327,7 +1350,9 @@ int Server::main(const std::vector & /*args*/) /// Reload config in SYSTEM RELOAD CONFIG query. global_context->setConfigReloadCallback([&]() { main_config_reloader->reload(); - users_config_reloader->reload(); + + if (users_config_reloader) + users_config_reloader->reload(); }); /// Limit on total number of concurrently executed queries. @@ -1529,7 +1554,8 @@ int Server::main(const std::vector & /*args*/) main_config_reloader->addConfigObject(global_context->getSecurityConfig()); main_config_reloader->start(); - users_config_reloader->start(); + if (users_config_reloader) + users_config_reloader->start(); { // on ARM processors it can show only enabled at current moment cores diff --git a/dbms/src/Server/Server.h b/dbms/src/Server/Server.h index fc8141dbd9d..0f28e6c6858 100644 --- a/dbms/src/Server/Server.h +++ b/dbms/src/Server/Server.h @@ -34,6 +34,8 @@ class Server : public BaseDaemon , public IServer { public: + using ServerApplication::run; + Poco::Util::LayeredConfiguration & config() const override { return BaseDaemon::config(); @@ -54,7 +56,11 @@ class Server : public BaseDaemon return BaseDaemon::isCancelled(); } + void defineOptions(Poco::Util::OptionSet & _options) override; + protected: + int run() override; + void initialize(Application & self) override; void uninitialize() override; diff --git a/dbms/src/Server/StorageConfigParser.cpp b/dbms/src/Server/StorageConfigParser.cpp index 338ed179a03..d1617d31aea 100644 --- a/dbms/src/Server/StorageConfigParser.cpp +++ b/dbms/src/Server/StorageConfigParser.cpp @@ -270,13 +270,13 @@ bool TiFlashStorageConfig::parseFromDeprecatedConfiguration(Poco::Util::LayeredC if (!config.has("path")) return false; - LOG_WARNING(log, "The configuration \"path\" is deprecated. Check [storage] section for new style."); + LOG_WARNING(log, "The configuration `path` is deprecated. Check [storage] section for new style."); String paths = config.getString("path"); Poco::trimInPlace(paths); if (paths.empty()) throw Exception( - fmt::format("The configuration \"path\" is empty! [path={}]", config.getString("path")), + fmt::format("The configuration `path` is empty! [path={}]", config.getString("path")), ErrorCodes::INVALID_CONFIG_PARAMETER); Strings all_normal_path; Poco::StringTokenizer string_tokens(paths, ","); @@ -309,7 +309,7 @@ bool TiFlashStorageConfig::parseFromDeprecatedConfiguration(Poco::Util::LayeredC String str_kvstore_path; if (config.has("raft.kvstore_path")) { - LOG_WARNING(log, "The configuration \"raft.kvstore_path\" is deprecated. Check [storage.raft] section for new style."); + LOG_WARNING(log, "The configuration `raft.kvstore_path` is deprecated. Check [storage.raft] section for new style."); str_kvstore_path = config.getString("raft.kvstore_path"); } if (str_kvstore_path.empty()) @@ -348,9 +348,9 @@ std::tuple TiFlashStorageConfig::parseSettings(Poc if (config.has("storage.main")) { if (config.has("path")) - LOG_WARNING(log, "The configuration \"path\" is ignored when \"storage\" is defined."); + LOG_WARNING(log, "The configuration `path` is ignored when `storage` is defined."); if (config.has("capacity")) - LOG_WARNING(log, "The configuration \"capacity\" is ignored when \"storage\" is defined."); + LOG_WARNING(log, "The configuration `capacity` is ignored when `storage` is defined."); storage_config.parseStoragePath(config.getString("storage"), log); @@ -360,7 +360,7 @@ std::tuple TiFlashStorageConfig::parseSettings(Poc String deprecated_kvstore_path = config.getString("raft.kvstore_path"); if (!deprecated_kvstore_path.empty()) { - LOG_WARNING(log, "The configuration \"raft.kvstore_path\" is deprecated. Check \"storage.raft.dir\" for new style."); + LOG_WARNING(log, "The configuration `raft.kvstore_path` is deprecated. Check `storage.raft.dir` for new style."); kvstore_paths.clear(); kvstore_paths.emplace_back(getNormalizedPath(deprecated_kvstore_path)); for (auto & kvstore_path : kvstore_paths) @@ -379,7 +379,7 @@ std::tuple TiFlashStorageConfig::parseSettings(Poc // capacity if (config.has("capacity")) { - LOG_WARNING(log, "The configuration \"capacity\" is deprecated. Check [storage] section for new style."); + LOG_WARNING(log, "The configuration `capacity` is deprecated. Check [storage] section for new style."); // TODO: support human readable format for capacity, mark_cache_size, minmax_index_cache_size // eg. 100GiB, 10MiB String capacities = config.getString("capacity"); @@ -402,7 +402,7 @@ std::tuple TiFlashStorageConfig::parseSettings(Poc if (!storage_config.parseFromDeprecatedConfiguration(config, log)) { // Can not parse from the deprecated configuration "path". - String msg = "The configuration \"storage.main\" section is not defined. Please check your configuration file."; + String msg = "The configuration `storage.main` section is not defined. Please check your configuration file."; LOG_ERROR(log, "{}", msg); throw Exception(msg, ErrorCodes::INVALID_CONFIG_PARAMETER); } diff --git a/dbms/src/Server/UserConfigParser.cpp b/dbms/src/Server/UserConfigParser.cpp index 07f22790b14..0edde9ce598 100644 --- a/dbms/src/Server/UserConfigParser.cpp +++ b/dbms/src/Server/UserConfigParser.cpp @@ -67,6 +67,12 @@ ConfigReloaderPtr parseSettings( if (load_from_main_config_path) users_config_path = config_path; + if (users_config_path.empty()) + { + global_context->setUsersConfig(new Poco::Util::LayeredConfiguration()); + return nullptr; + } + LOG_INFO(log, "Set users config file to: {}", users_config_path); return std::make_unique( diff --git a/dbms/src/Storages/DeltaMerge/File/DMFile.cpp b/dbms/src/Storages/DeltaMerge/File/DMFile.cpp index dba3a36a218..ad79c05e37a 100644 --- a/dbms/src/Storages/DeltaMerge/File/DMFile.cpp +++ b/dbms/src/Storages/DeltaMerge/File/DMFile.cpp @@ -156,7 +156,11 @@ DMFilePtr DMFile::restore( auto is_s3_file = S3::S3FilenameView::fromKeyWithPrefix(parent_path).isDataFile(); if (!is_s3_file) { - RUNTIME_CHECK(Poco::Path(parent_path).isAbsolute(), parent_path); + // Unrecognized xx:// protocol. + RUNTIME_CHECK_MSG( + parent_path.find("://") == std::string::npos, + "Unsupported protocol in path {}", + parent_path); String path = getPathByStatus(parent_path, file_id, DMFile::Status::READABLE); // The path may be dropped by another thread in some cases auto poco_file = Poco::File(path); diff --git a/dbms/src/Storages/Page/workload/PSStressEnv.cpp b/dbms/src/Storages/Page/workload/PSStressEnv.cpp index 49a651e7388..236bf837b33 100644 --- a/dbms/src/Storages/Page/workload/PSStressEnv.cpp +++ b/dbms/src/Storages/Page/workload/PSStressEnv.cpp @@ -35,7 +35,7 @@ Poco::Logger * StressEnv::logger; void StressEnv::initGlobalLogger() { Poco::AutoPtr channel = new Poco::ConsoleChannel(std::cerr); - Poco::AutoPtr formatter(new DB::UnifiedLogFormatter); + Poco::AutoPtr formatter(new DB::UnifiedLogFormatter()); Poco::AutoPtr formatting_channel(new Poco::FormattingChannel(formatter, channel)); Poco::Logger::root().setChannel(formatting_channel); Poco::Logger::root().setLevel("trace"); diff --git a/dbms/src/Storages/Transaction/ProxyFFI.cpp b/dbms/src/Storages/Transaction/ProxyFFI.cpp index 912646e7eed..a837a6395e1 100644 --- a/dbms/src/Storages/Transaction/ProxyFFI.cpp +++ b/dbms/src/Storages/Transaction/ProxyFFI.cpp @@ -298,7 +298,7 @@ CppStrWithView HandleReadPage(const EngineStoreServerWrap * server, BaseBuffView else { LOG_TRACE(&Poco::Logger::get("ProxyFFI"), fmt::format("FFI read page {} fail", UniversalPageId(page_id.data, page_id.len))); - return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}; + return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}; } } catch (...) @@ -361,7 +361,7 @@ CppStrWithView HandleGetLowerBound(const EngineStoreServerWrap * server, BaseBuf else { LOG_TRACE(&Poco::Logger::get("ProxyFFI"), fmt::format("FFI get lower bound for page {} fail", UniversalPageId(raw_page_id.data, raw_page_id.len))); - return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}; + return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}; } } catch (...) @@ -779,7 +779,7 @@ CppStrWithView GetConfig(EngineStoreServerWrap * server, [[maybe_unused]] uint8_ config_file_path = server->tmt->getContext().getConfigRef().getString("config-file"); std::ifstream stream(config_file_path); if (!stream) - return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}; + return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}; auto * s = RawCppString::New((std::istreambuf_iterator(stream)), std::istreambuf_iterator()); stream.close(); @@ -793,7 +793,7 @@ CppStrWithView GetConfig(EngineStoreServerWrap * server, [[maybe_unused]] uint8_ } catch (...) { - return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}; + return CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}; } } diff --git a/dbms/src/Storages/Transaction/ProxyFFIStatusService.cpp b/dbms/src/Storages/Transaction/ProxyFFIStatusService.cpp index 46d77e6f23c..5fa3f1c0bc2 100644 --- a/dbms/src/Storages/Transaction/ProxyFFIStatusService.cpp +++ b/dbms/src/Storages/Transaction/ProxyFFIStatusService.cpp @@ -47,7 +47,7 @@ HttpRequestRes HandleHttpRequestSyncStatus( { LOG_ERROR(log, "invalid SyncStatus request: {}", query); status = HttpRequestStatus::ErrorParam; - return HttpRequestRes{.status = status, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}}; + return HttpRequestRes{.status = status, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}}; } @@ -69,7 +69,7 @@ HttpRequestRes HandleHttpRequestSyncStatus( } if (status != HttpRequestStatus::Ok) - return HttpRequestRes{.status = status, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}}; + return HttpRequestRes{.status = status, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}}; } std::stringstream ss; @@ -168,7 +168,7 @@ HttpRequestRes HandleHttpRequest(EngineStoreServerWrap * server, BaseBuffView pa return method(server, path, str, std::string_view(query.data, query.len), std::string_view(body.data, body.len)); } } - return HttpRequestRes{.status = HttpRequestStatus::ErrorParam, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{}}}; + return HttpRequestRes{.status = HttpRequestStatus::ErrorParam, .res = CppStrWithView{.inner = GenRawCppPtr(), .view = BaseBuffView{nullptr, 0}}}; } } // namespace DB diff --git a/dbms/src/TestUtils/TiFlashTestEnv.cpp b/dbms/src/TestUtils/TiFlashTestEnv.cpp index 5a031f001d1..c6b8f73a963 100644 --- a/dbms/src/TestUtils/TiFlashTestEnv.cpp +++ b/dbms/src/TestUtils/TiFlashTestEnv.cpp @@ -210,7 +210,7 @@ void TiFlashTestEnv::shutdown() void TiFlashTestEnv::setupLogger(const String & level, std::ostream & os) { Poco::AutoPtr channel = new Poco::ConsoleChannel(os); - Poco::AutoPtr formatter(new UnifiedLogFormatter()); + Poco::AutoPtr formatter(new UnifiedLogFormatter()); Poco::AutoPtr formatting_channel(new Poco::FormattingChannel(formatter, channel)); Poco::Logger::root().setChannel(formatting_channel); Poco::Logger::root().setLevel(level); diff --git a/libs/libdaemon/src/BaseDaemon.cpp b/libs/libdaemon/src/BaseDaemon.cpp index bfe7cbc125d..630324c4f70 100644 --- a/libs/libdaemon/src/BaseDaemon.cpp +++ b/libs/libdaemon/src/BaseDaemon.cpp @@ -695,6 +695,14 @@ static std::string normalize(const std::string & log_level) void BaseDaemon::reloadConfiguration() { + // when config-file is not specified and config.toml does not exist, we do not load config. + if (!config().has("config-file")) + { + Poco::File f("config.toml"); + if (!f.exists()) + return; + } + /** If the program is not run in daemon mode and 'config-file' is not specified, * then we use config from 'config.toml' file in current directory, * but will log to console (or use parameters --log-file, --errorlog-file from command line) @@ -749,12 +757,12 @@ void BaseDaemon::wakeup() void BaseDaemon::buildLoggers(Poco::Util::AbstractConfiguration & config) { - auto current_logger = config.getString("logger"); + auto current_logger = config.getString("logger", "(null)"); if (config_logger == current_logger) return; config_logger = current_logger; - bool is_daemon = config.getBool("application.runAsDaemon", true); + bool is_daemon = config.getBool("application.runAsDaemon", false); // Split log, error log and tracing log. Poco::AutoPtr split = new Poco::ReloadableSplitterChannel; @@ -767,7 +775,7 @@ void BaseDaemon::buildLoggers(Poco::Util::AbstractConfiguration & config) std::cerr << "Logging " << log_level << " to " << log_path << std::endl; // Set up two channel chains. - Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); + Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); Poco::AutoPtr log = new FormattingChannel(pf); log_file = new Poco::TiFlashLogFileChannel; log_file->setProperty(Poco::FileChannel::PROP_PATH, Poco::Path(log_path).absolute().toString()); @@ -790,7 +798,7 @@ void BaseDaemon::buildLoggers(Poco::Util::AbstractConfiguration & config) std::cerr << "Logging errors to " << errorlog_path << std::endl; Poco::AutoPtr level = new Poco::LevelFilterChannel; level->setLevel(Message::PRIO_NOTICE); - Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); + Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); Poco::AutoPtr errorlog = new FormattingChannel(pf); error_log_file = new Poco::TiFlashLogFileChannel; error_log_file->setProperty(Poco::FileChannel::PROP_PATH, Poco::Path(errorlog_path).absolute().toString()); @@ -815,7 +823,7 @@ void BaseDaemon::buildLoggers(Poco::Util::AbstractConfiguration & config) /// to filter the tracing log. Poco::AutoPtr source = new Poco::SourceFilterChannel; source->setSource(DB::tracing_log_source); - Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); + Poco::AutoPtr pf = new DB::UnifiedLogFormatter(); Poco::AutoPtr tracing_log = new FormattingChannel(pf); tracing_log_file = new Poco::TiFlashLogFileChannel; tracing_log_file->setProperty(Poco::FileChannel::PROP_PATH, Poco::Path(tracing_log_path).absolute().toString()); @@ -846,14 +854,20 @@ void BaseDaemon::buildLoggers(Poco::Util::AbstractConfiguration & config) syslog_channel->open(); } - if (config.getBool("logger.console", false) || (!config.hasProperty("logger.console") && !is_daemon && (isatty(STDIN_FILENO) || isatty(STDERR_FILENO)))) + bool should_log_to_console = isatty(STDIN_FILENO) || isatty(STDERR_FILENO); + bool enable_colors = isatty(STDERR_FILENO); + + if (config.getBool("logger.console", false) + || (!config.hasProperty("logger.console") && !is_daemon && should_log_to_console)) { Poco::AutoPtr file = new ConsoleChannel; - Poco::AutoPtr pf = new OwnPatternFormatter(this); - pf->setProperty("times", "local"); + Poco::AutoPtr pf; + if (enable_colors) + pf = new DB::UnifiedLogFormatter(); + else + pf = new DB::UnifiedLogFormatter(); Poco::AutoPtr log = new FormattingChannel(pf); log->setChannel(file); - logger().warning("Logging " + log_level + " to console"); split->addChannel(log); } @@ -1002,8 +1016,6 @@ void BaseDaemon::initialize(Application & self) umask(umask_num); } - ConfigProcessor(config_path).savePreprocessedConfig(loaded_config); - /// Write core dump on crash. { struct rlimit rlim