diff options
author | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-07 14:11:53 +0300 |
---|---|---|
committer | robot-piglet <robot-piglet@yandex-team.com> | 2024-12-07 14:24:06 +0300 |
commit | 10a3cab303733001fecf8fe2b3e1ec1b2e3e8467 (patch) | |
tree | 7514c603140fb1a9109f7a728e2d7dd1305dcae2 | |
parent | 86e09d742d9d49f23c5ea1b691b68e3a53f1f40d (diff) | |
download | ydb-10a3cab303733001fecf8fe2b3e1ec1b2e3e8467.tar.gz |
Intermediate changes
commit_hash:bce12427820484f881aa75468347f8dcf60112e8
-rw-r--r-- | yt/yt/library/profiling/solomon/tag_registry.cpp | 4 | ||||
-rw-r--r-- | yt/yt/library/profiling/solomon/tag_registry.h | 2 | ||||
-rw-r--r-- | yt/yt/library/program/program-inl.h | 8 | ||||
-rw-r--r-- | yt/yt/library/program/program.h | 6 | ||||
-rw-r--r-- | yt/yt/library/program/program_config_mixin.h | 144 | ||||
-rw-r--r-- | yt/yt/library/program/program_mixin.cpp | 21 | ||||
-rw-r--r-- | yt/yt/library/program/program_mixin.h | 22 | ||||
-rw-r--r-- | yt/yt/library/program/program_pdeathsig_mixin.cpp | 5 | ||||
-rw-r--r-- | yt/yt/library/program/program_pdeathsig_mixin.h | 7 | ||||
-rw-r--r-- | yt/yt/library/program/program_setsid_mixin.cpp | 5 | ||||
-rw-r--r-- | yt/yt/library/program/program_setsid_mixin.h | 7 | ||||
-rw-r--r-- | yt/yt/library/program/ya.make | 1 |
12 files changed, 151 insertions, 81 deletions
diff --git a/yt/yt/library/profiling/solomon/tag_registry.cpp b/yt/yt/library/profiling/solomon/tag_registry.cpp index 0bace2e5f1..a2a1326d10 100644 --- a/yt/yt/library/profiling/solomon/tag_registry.cpp +++ b/yt/yt/library/profiling/solomon/tag_registry.cpp @@ -183,12 +183,12 @@ void TTagRegistry::DumpTags(NProto::TSensorDump* dump) } } -TTagId TTagRegistry::TryEncodeSanitized(const TTag& tag) const +std::optional<TTagId> TTagRegistry::TryEncodeSanitized(const TTag& tag) const { if (auto it = TagByName_.find(tag); it != TagByName_.end()) { return it->second; } else { - return {}; + return std::nullopt; } } diff --git a/yt/yt/library/profiling/solomon/tag_registry.h b/yt/yt/library/profiling/solomon/tag_registry.h index d12459602c..884e7aea02 100644 --- a/yt/yt/library/profiling/solomon/tag_registry.h +++ b/yt/yt/library/profiling/solomon/tag_registry.h @@ -32,7 +32,7 @@ public: private: template <class TTagPerfect> TTagId EncodeSanitized(TTagPerfect&& tag); - TTagId TryEncodeSanitized(const TTag& tag) const; + std::optional<TTagId> TryEncodeSanitized(const TTag& tag) const; // TODO(prime@): maybe do something about the fact that tags are never freed. THashMap<TTag, TTagId> TagByName_; diff --git a/yt/yt/library/program/program-inl.h b/yt/yt/library/program/program-inl.h index 30ae62bd99..bff3bc2b1f 100644 --- a/yt/yt/library/program/program-inl.h +++ b/yt/yt/library/program/program-inl.h @@ -15,6 +15,14 @@ void TProgram::Abort(E exitCode) noexcept Abort(ToUnderlying(exitCode)); } +template <class E> + requires std::is_enum_v<E> +[[noreturn]] +void TProgram::Exit(E exitCode) noexcept +{ + Exit(ToUnderlying(exitCode)); +} + //////////////////////////////////////////////////////////////////////////////// } // namespace NYT diff --git a/yt/yt/library/program/program.h b/yt/yt/library/program/program.h index b476323ffd..c5578f215b 100644 --- a/yt/yt/library/program/program.h +++ b/yt/yt/library/program/program.h @@ -76,6 +76,12 @@ protected: [[noreturn]] void Exit(int code) noexcept; + //! A typed version of #Exit. + template <class E> + requires std::is_enum_v<E> + [[noreturn]] + void Exit(E exitCode) noexcept; + private: bool CrashOnError_ = false; diff --git a/yt/yt/library/program/program_config_mixin.h b/yt/yt/library/program/program_config_mixin.h index 06ceac32f8..8742b6f044 100644 --- a/yt/yt/library/program/program_config_mixin.h +++ b/yt/yt/library/program/program_config_mixin.h @@ -1,6 +1,6 @@ #pragma once -#include "program.h" +#include "program_mixin.h" #include <yt/yt/core/yson/writer.h> @@ -9,6 +9,8 @@ #include <library/cpp/yt/string/enum.h> +#include <library/cpp/yt/system/exit.h> + #include <util/stream/file.h> namespace NYT { @@ -17,6 +19,7 @@ namespace NYT { template <class TConfig, class TDynamicConfig = void> class TProgramConfigMixin + : public virtual TProgramMixinBase { protected: explicit TProgramConfigMixin( @@ -34,6 +37,7 @@ protected: } else { opt.Optional(); } + opts .AddLongOption( Format("%v-schema", argumentName), @@ -58,21 +62,21 @@ protected: UnrecognizedStrategy_ = ParseEnum<NYTree::EUnrecognizedStrategy>(value); }); - if constexpr (std::is_same_v<TDynamicConfig, void>) { - return; + if constexpr (!std::is_same_v<TDynamicConfig, void>) { + opts + .AddLongOption( + Format("dynamic-%v-schema", argumentName), + Format("print %v schema and exit", argumentName)) + .OptionalValue(YsonSchemaFormat_, "FORMAT") + .StoreResult(&DynamicConfigSchema_); + opts + .AddLongOption( + Format("dynamic-%v-template", argumentName), + Format("print dynamic %v template and exit", argumentName)) + .SetFlag(&DynamicConfigTemplate_); } - opts - .AddLongOption( - Format("dynamic-%v-schema", argumentName), - Format("print %v schema and exit", argumentName)) - .OptionalValue(YsonSchemaFormat_, "FORMAT") - .StoreResult(&DynamicConfigSchema_); - opts - .AddLongOption( - Format("dynamic-%v-template", argumentName), - Format("print dynamic %v template and exit", argumentName)) - .SetFlag(&DynamicConfigTemplate_); + RegisterMixinCallback([&] { Handle(); }); } TIntrusivePtr<TConfig> GetConfig(bool returnNullIfNotSupplied = false) @@ -99,51 +103,22 @@ protected: return ConfigNode_; } - bool HandleConfigOptions() - { - auto print = [] (const auto& config) { - using namespace NYson; - TYsonWriter writer(&Cout, EYsonFormat::Pretty); - config->Save(&writer); - Cout << Flush; - }; - auto printSchema = [] (const auto& config, TString format) { - if (format == YsonSchemaFormat_) { - using namespace NYson; - TYsonWriter writer(&Cout, EYsonFormat::Pretty); - config->WriteSchema(&writer); - Cout << Endl; - } else { - THROW_ERROR_EXCEPTION("Unknown schema format %Qv", format); - } - }; - if (!ConfigSchema_.empty()) { - printSchema(New<TConfig>(), ConfigSchema_); - return true; - } - if (ConfigTemplate_) { - print(New<TConfig>()); - return true; - } - if (ConfigActual_) { - print(GetConfig()); - return true; - } +private: + const TString ArgumentName_; - if constexpr (!std::is_same_v<TDynamicConfig, void>) { - if (!DynamicConfigSchema_.empty()) { - printSchema(New<TDynamicConfig>(), DynamicConfigSchema_); - return true; - } - if (DynamicConfigTemplate_) { - print(New<TDynamicConfig>()); - return true; - } - } - return false; - } + TString ConfigPath_; + TString ConfigSchema_; + bool ConfigTemplate_; + bool ConfigActual_; + TString DynamicConfigSchema_; + bool DynamicConfigTemplate_ = false; + NYTree::EUnrecognizedStrategy UnrecognizedStrategy_ = NYTree::EUnrecognizedStrategy::KeepRecursive; + + static constexpr auto YsonSchemaFormat_ = "yson-schema"; + + TIntrusivePtr<TConfig> Config_; + NYTree::INodePtr ConfigNode_; -private: void LoadConfigNode() { using namespace NYTree; @@ -181,20 +156,53 @@ private: } } - const TString ArgumentName_; + void Handle() + { + auto print = [] (const auto& config) { + using namespace NYson; + TYsonWriter writer(&Cout, EYsonFormat::Pretty); + config->Save(&writer); + Cout << Flush; + }; - TString ConfigPath_; - TString ConfigSchema_; - bool ConfigTemplate_; - bool ConfigActual_; - TString DynamicConfigSchema_; - bool DynamicConfigTemplate_ = false; - NYTree::EUnrecognizedStrategy UnrecognizedStrategy_ = NYTree::EUnrecognizedStrategy::KeepRecursive; + auto printSchema = [] (const auto& config, TString format) { + if (format == YsonSchemaFormat_) { + using namespace NYson; + TYsonWriter writer(&Cout, EYsonFormat::Pretty); + config->WriteSchema(&writer); + Cout << Endl; + } else { + THROW_ERROR_EXCEPTION("Unknown schema format %Qv", format); + } + }; - static constexpr auto YsonSchemaFormat_ = "yson-schema"; + if (!ConfigSchema_.empty()) { + printSchema(New<TConfig>(), ConfigSchema_); + Exit(EProcessExitCode::OK); + } - TIntrusivePtr<TConfig> Config_; - NYTree::INodePtr ConfigNode_; + if (ConfigTemplate_) { + print(New<TConfig>()); + Exit(EProcessExitCode::OK); + } + + if (ConfigActual_) { + print(GetConfig()); + Exit(EProcessExitCode::OK); + } + + if constexpr (!std::is_same_v<TDynamicConfig, void>) { + if (!DynamicConfigSchema_.empty()) { + printSchema(New<TDynamicConfig>(), DynamicConfigSchema_); + Exit(EProcessExitCode::OK); + } + + if (DynamicConfigTemplate_) { + print(New<TDynamicConfig>()); + Exit(EProcessExitCode::OK); + } + } + } }; //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/library/program/program_mixin.cpp b/yt/yt/library/program/program_mixin.cpp new file mode 100644 index 0000000000..b9c2c5bd1a --- /dev/null +++ b/yt/yt/library/program/program_mixin.cpp @@ -0,0 +1,21 @@ +#include "program_mixin.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +void TProgramMixinBase::RegisterMixinCallback(std::function<void()> callback) +{ + MixinCallbacks_.push_back(std::move(callback)); +} + +void TProgramMixinBase::RunMixinCallbacks() +{ + for (const auto& callback : MixinCallbacks_) { + callback(); + } +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/yt/yt/library/program/program_mixin.h b/yt/yt/library/program/program_mixin.h new file mode 100644 index 0000000000..3104d98946 --- /dev/null +++ b/yt/yt/library/program/program_mixin.h @@ -0,0 +1,22 @@ +#pragma once + +#include "program.h" + +namespace NYT { + +//////////////////////////////////////////////////////////////////////////////// + +class TProgramMixinBase + : public virtual TProgram +{ +protected: + void RegisterMixinCallback(std::function<void()> callback); + void RunMixinCallbacks(); + +private: + std::vector<std::function<void()>> MixinCallbacks_; +}; + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT diff --git a/yt/yt/library/program/program_pdeathsig_mixin.cpp b/yt/yt/library/program/program_pdeathsig_mixin.cpp index 34f1f3b9a8..f78623121c 100644 --- a/yt/yt/library/program/program_pdeathsig_mixin.cpp +++ b/yt/yt/library/program/program_pdeathsig_mixin.cpp @@ -13,9 +13,11 @@ TProgramPdeathsigMixin::TProgramPdeathsigMixin(NLastGetopt::TOpts& opts) opts.AddLongOption("pdeathsig", "parent death signal") .StoreResult(&ParentDeathSignal_) .RequiredArgument("PDEATHSIG"); + + RegisterMixinCallback([&] { Handle(); }); } -bool TProgramPdeathsigMixin::HandlePdeathsigOptions() +void TProgramPdeathsigMixin::Handle() { if (ParentDeathSignal_ > 0) { #ifdef _linux_ @@ -28,7 +30,6 @@ bool TProgramPdeathsigMixin::HandlePdeathsigOptions() YT_VERIFY(prctl(PR_SET_PDEATHSIG, ParentDeathSignal_) == 0); #endif } - return false; } //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/library/program/program_pdeathsig_mixin.h b/yt/yt/library/program/program_pdeathsig_mixin.h index 3e4bcfd4a6..5c26bdf3a0 100644 --- a/yt/yt/library/program/program_pdeathsig_mixin.h +++ b/yt/yt/library/program/program_pdeathsig_mixin.h @@ -1,20 +1,21 @@ #pragma once -#include "program.h" +#include "program_mixin.h" namespace NYT { //////////////////////////////////////////////////////////////////////////////// class TProgramPdeathsigMixin + : public virtual TProgramMixinBase { protected: explicit TProgramPdeathsigMixin(NLastGetopt::TOpts& opts); - bool HandlePdeathsigOptions(); - private: int ParentDeathSignal_ = -1; + + void Handle(); }; //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/library/program/program_setsid_mixin.cpp b/yt/yt/library/program/program_setsid_mixin.cpp index a745fcd3a2..19a0504ab6 100644 --- a/yt/yt/library/program/program_setsid_mixin.cpp +++ b/yt/yt/library/program/program_setsid_mixin.cpp @@ -13,16 +13,17 @@ TProgramSetsidMixin::TProgramSetsidMixin(NLastGetopt::TOpts& opts) opts.AddLongOption("setsid", "create a new session") .StoreTrue(&Setsid_) .Optional(); + + RegisterMixinCallback([&] { Handle(); }); } -bool TProgramSetsidMixin::HandleSetsidOptions() +void TProgramSetsidMixin::Handle() { if (Setsid_) { #ifdef _linux_ setsid(); #endif } - return false; } //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/library/program/program_setsid_mixin.h b/yt/yt/library/program/program_setsid_mixin.h index 00b3dff50e..38465e97d7 100644 --- a/yt/yt/library/program/program_setsid_mixin.h +++ b/yt/yt/library/program/program_setsid_mixin.h @@ -1,20 +1,21 @@ #pragma once -#include "program.h" +#include "program_mixin.h" namespace NYT { //////////////////////////////////////////////////////////////////////////////// class TProgramSetsidMixin + : public virtual TProgramMixinBase { protected: explicit TProgramSetsidMixin(NLastGetopt::TOpts& opts); - bool HandleSetsidOptions(); - private: bool Setsid_ = false; + + void Handle(); }; //////////////////////////////////////////////////////////////////////////////// diff --git a/yt/yt/library/program/ya.make b/yt/yt/library/program/ya.make index 9eead99671..cecd2a7fb0 100644 --- a/yt/yt/library/program/ya.make +++ b/yt/yt/library/program/ya.make @@ -7,6 +7,7 @@ SRCS( config.cpp helpers.cpp program.cpp + program_mixin.cpp program_config_mixin.cpp program_pdeathsig_mixin.cpp program_setsid_mixin.cpp |