diff options
author | innokentii <innokentii@yandex-team.com> | 2023-03-27 10:13:52 +0300 |
---|---|---|
committer | innokentii <innokentii@yandex-team.com> | 2023-03-27 10:13:52 +0300 |
commit | 89d1fd3393f6e44b65e34a0adb6575dc3140d604 (patch) | |
tree | 5f8fe83e6d72288a3da7dd88180507dc40981936 | |
parent | 4febd9ea8a1bd50118483f55b98bf9655f479ab8 (diff) | |
download | ydb-89d1fd3393f6e44b65e34a0adb6575dc3140d604.tar.gz |
Add yaml config utils:
add yaml config utils
-rw-r--r-- | ydb/core/cms/console/yaml_config/yaml_config.cpp | 63 | ||||
-rw-r--r-- | ydb/core/cms/console/yaml_config/yaml_config.h | 12 | ||||
-rw-r--r-- | ydb/core/cms/console/yaml_config/yaml_config_ut.cpp | 10 |
3 files changed, 73 insertions, 12 deletions
diff --git a/ydb/core/cms/console/yaml_config/yaml_config.cpp b/ydb/core/cms/console/yaml_config/yaml_config.cpp index ae4a461526c..4f00f349553 100644 --- a/ydb/core/cms/console/yaml_config/yaml_config.cpp +++ b/ydb/core/cms/console/yaml_config/yaml_config.cpp @@ -4,6 +4,27 @@ #include <library/cpp/protobuf/json/json2proto.h> #include <ydb/core/base/appdata.h> +template <> +struct THash<NYamlConfig::TLabel> { + inline size_t operator()(const NYamlConfig::TLabel& value) const { + return CombineHashes(THash<TString>{}(value.Value), (size_t)value.Type); + } +}; + +template <> +struct THash<TVector<TString>> { + inline size_t operator()(const TVector<TString>& value) const { + size_t result = 0; + for (auto& str : value) { + result = CombineHashes(result, THash<TString>{}(str)); + } + return result; + } +}; + +template <> +struct THash<TVector<NYamlConfig::TLabel>> : public TSimpleRangeHash {}; + namespace NYamlConfig { inline const TMap<TString, EYamlConfigLabelTypeClass> ClassMapping{ @@ -22,18 +43,6 @@ TString GetKey(const NFyaml::TNodeRef& node, TString key) { return k; } -TString CalcHash(const NFyaml::TDocument& resolved) { - TStringStream ss; - ss << resolved; - TString s = ss.Str(); - SHA256_CTX sha; - SHA256_Init(&sha); - SHA256_Update(&sha, s.data(), s.size()); - unsigned char hash[SHA256_DIGEST_LENGTH]; - SHA256_Final(hash, &sha); - return TString(reinterpret_cast<char*>(hash), sizeof(hash)); -} - bool Fit(const TSelector& selector, const TSet<TNamedLabel>& labels) { bool result = true; size_t matched = 0; @@ -520,6 +529,27 @@ TResolvedConfig ResolveAll(NFyaml::TDocument& doc) return {labelNames, std::move(configs)}; } +size_t Hash(const NFyaml::TNodeRef& resolved) { + TStringStream ss; + ss << resolved; + TString s = ss.Str(); + return THash<TString>{}(s); +} + +size_t Hash(const TResolvedConfig& config) +{ + size_t configsHash = 0; + for (auto& [labelSet, docConfig] : config.Configs) { + for (auto labels : labelSet) { + auto labelsHash = THash<TVector<TLabel>>{}(labels); + configsHash = CombineHashes(labelsHash, configsHash); + } + configsHash = CombineHashes(Hash(docConfig.second), configsHash); + } + + return CombineHashes(THash<TVector<TString>>{}(config.Labels), configsHash); +} + void ValidateVolatileConfig(NFyaml::TDocument& doc) { auto root = doc.Root(); auto seq = root.Sequence(); @@ -558,6 +588,15 @@ void AppendVolatileConfigs(NFyaml::TDocument& config, NFyaml::TDocument& volatil } } +ui64 GetVersion(const TString& config) { + auto parser = NFyaml::TParser::Create(config); + auto header = parser.NextDocument(); + auto str = header->Root().Map().at("version").Scalar(); + ui64 version = 0; + TryFromString<ui64>(str, version); + return version; +} + } // namespace NYamlConfig template <> diff --git a/ydb/core/cms/console/yaml_config/yaml_config.h b/ydb/core/cms/console/yaml_config/yaml_config.h index 85f80071703..08f0c63fadf 100644 --- a/ydb/core/cms/console/yaml_config/yaml_config.h +++ b/ydb/core/cms/console/yaml_config/yaml_config.h @@ -4,6 +4,7 @@ #include <library/cpp/actors/core/actor.h> #include <ydb/core/protos/config.pb.h> +#include <ydb/core/protos/console_config.pb.h> #include <openssl/sha.h> @@ -151,6 +152,12 @@ struct TResolvedConfig { TResolvedConfig ResolveAll(NFyaml::TDocument& doc); /** + * Calculates hash of resolved config + * Used to ensure that cli resolves config the same as a server + */ +size_t Hash(const TResolvedConfig& config); + +/** * Validates single YAML volatile config schema */ void ValidateVolatileConfig(NFyaml::TDocument& doc); @@ -160,4 +167,9 @@ void ValidateVolatileConfig(NFyaml::TDocument& doc); */ void AppendVolatileConfigs(NFyaml::TDocument& config, NFyaml::TDocument& volatileConfig); +/** + * Parses config version + */ +ui64 GetVersion(const TString& config); + } // namespace NYamlConfig diff --git a/ydb/core/cms/console/yaml_config/yaml_config_ut.cpp b/ydb/core/cms/console/yaml_config/yaml_config_ut.cpp index 1738cb08a23..fd558e60eb6 100644 --- a/ydb/core/cms/console/yaml_config/yaml_config_ut.cpp +++ b/ydb/core/cms/console/yaml_config/yaml_config_ut.cpp @@ -1405,4 +1405,14 @@ Y_UNIT_TEST_SUITE(YamlConfig) { stream << cfg; UNIT_ASSERT_VALUES_EQUAL(stream.Str(), TString(Concatenated)); } + + Y_UNIT_TEST(AppendAndResolve) { + auto cfg = NFyaml::TDocument::Parse(SimpleConfig); + for (int i = 0; i < 4; ++i) { + auto volatilePart = NFyaml::TDocument::Parse(VolatilePart); + NYamlConfig::AppendVolatileConfigs(cfg, volatilePart); + } + TStringStream stream; + stream << cfg; + } } |