1- #include < iostream>
2-
31#include < userver/storages/scylla/component.hpp>
42
3+ #include < boost/algorithm/string/predicate.hpp>
4+
5+ #include < userver/clients/dns/resolver_utils.hpp>
56#include < userver/components/component.hpp>
6- #include < userver/storages/scylla/exception .hpp>
7+ #include < userver/components/statistics_storage .hpp>
78#include < userver/dynamic_config/storage/component.hpp>
9+ #include < userver/storages/scylla/exception.hpp>
10+ #include < userver/storages/scylla/session.hpp>
11+ #include < userver/storages/scylla/session_config.hpp>
812#include < userver/yaml_config/merge_schemas.hpp>
913
14+ #include < storages/scylla/scylla_secdist.hpp>
15+
16+ #include < userver/storages/secdist/component.hpp>
17+
1018#ifndef ARCADIA_ROOT
1119#include " generated/src/storages/scylla/component.yaml.hpp" // Y_IGNORE
1220#endif
1321
14- #include " userver/storages/secdist/component.hpp"
15-
16- #include < boost/algorithm/string/case_conv.hpp>
17- #include < boost/algorithm/string/predicate.hpp>
18- #include < userver/storages/scylla/session.hpp>
19- #include < userver/storages/scylla/session_config.hpp>
20-
21- #include < userver/clients/dns/resolver_utils.hpp>
22- #include < userver/components/statistics_storage.hpp>
23-
2422USERVER_NAMESPACE_BEGIN
2523
2624namespace components {
@@ -46,10 +44,8 @@ Scylla::Scylla(const ComponentConfig& config, const ComponentContext& context) :
4644 if (!db_alias.empty ()) {
4745 dbalias_ = db_alias;
4846 secdist = &context.FindComponent <Secdist>().GetStorage ();
49-
50- hosts = " ???" ;
47+ hosts = storages::scylla::secdist::GetSecdistHosts (*secdist, dbalias_);
5148 } else {
52- // TODO: rename to hosts
5349 hosts = config[" dbconnection" ].As <std::string>();
5450 }
5551
@@ -63,28 +59,46 @@ Scylla::Scylla(const ComponentConfig& config, const ComponentContext& context) :
6359 const auto dynamic_config = context.FindComponent <DynamicConfig>().GetSource ();
6460 const auto session_config = ParseSessionConfig (config);
6561
66- session_ = std::make_shared<
67- storages::scylla::Session>(config.Name (), hosts, session_config, dynamic_config, dns_resolver);
68-
69- const auto & statistics_storage = context.FindComponent <components::StatisticsStorage>();
62+ session_ = std::make_shared<storages::scylla::Session>(
63+ config.Name (), hosts, session_config, dynamic_config, dns_resolver);
7064
71- auto component_name = config.Name ();
65+ if (!dbalias_.empty ()) {
66+ secdist_subscriber_ = secdist->UpdateAndListen (this , dbalias_, &Scylla::OnSecdistUpdate);
67+ }
7268
73- const bool has_name_after_prefix = component_name.size () > kStandardScyllaPrefix .size ();
74- const bool has_scylla_prefix = boost::algorithm::starts_with (component_name, kStandardScyllaPrefix );
69+ auto & statistics_storage = context.FindComponent <components::StatisticsStorage>();
7570
76- if (has_scylla_prefix && has_name_after_prefix) {
77- component_name = component_name.substr (kStandardScyllaPrefix .size ());
71+ auto section_name = config.Name ();
72+ if (boost::algorithm::starts_with (section_name, kStandardScyllaPrefix ) &&
73+ section_name.size () != kStandardScyllaPrefix .size ()) {
74+ section_name = section_name.substr (kStandardScyllaPrefix .size ());
7875 }
79- };
76+
77+ statistics_entry_ = statistics_storage.GetStorage ().RegisterWriter (
78+ " scylla" ,
79+ [this ](utils::statistics::Writer& writer) {
80+ UASSERT (session_);
81+ DumpMetric (writer, *session_);
82+ },
83+ {{" scylla_database" , std::move (section_name)}}
84+ );
85+ }
8086
8187storages::scylla::SessionPtr Scylla::GetSession () const { return session_; }
8288
8389yaml_config::Schema Scylla::GetStaticConfigSchema () {
8490 return yaml_config::MergeSchemasFromResource<ComponentBase>(" src/storages/scylla/component.yaml" );
8591}
8692
87- Scylla::~Scylla () = default ;
93+ void Scylla::OnSecdistUpdate (const storages::secdist::SecdistConfig& config) {
94+ auto hosts = storages::scylla::secdist::GetSecdistHosts (config, dbalias_);
95+ session_->SetContactPoints (hosts);
96+ }
97+
98+ Scylla::~Scylla () {
99+ statistics_entry_.Unregister ();
100+ secdist_subscriber_.Unsubscribe ();
101+ }
88102} // namespace components
89103
90104USERVER_NAMESPACE_END
0 commit comments