userver: userver/ydb/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ydb/component.hpp
4/// @brief @copybrief ydb::YdbComponent
5
6#include <memory>
7#include <string>
8#include <unordered_map>
9
10#include <userver/components/component_base.hpp>
11#include <userver/dynamic_config/source.hpp>
12#include <userver/formats/json.hpp>
13#include <userver/utils/statistics/fwd.hpp>
14#include <userver/utils/statistics/storage.hpp>
15
16#include <userver/ydb/fwd.hpp>
17
18namespace NYdb {
19class TDriver;
20}
21
22USERVER_NAMESPACE_BEGIN
23
24namespace ydb {
25
26namespace impl {
27class Driver;
28} // namespace impl
29
30// clang-format off
31
32/// @ingroup userver_components
33///
34/// @brief YDB client component
35///
36/// Provides access to ydb::TableClient, ydb::TopicClient, ydb::CoordinationClient.
37///
38/// ## Static options:
39/// Name | Description | Default value
40/// ---- | ----------- | -------------
41/// credentials-provider | name of credentials provider component | -
42/// operation-settings.retries | default retries count for an operation | 3
43/// operation-settings.operation-timeout | default operation timeout in utils::StringToDuration() format | 1s
44/// operation-settings.cancel-after | cancel operation after specified string in utils::StringToDuration() format | 1s
45/// operation-settings.client-timeout | default client timeout in utils::StringToDuration() format | 1s
46/// operation-settings.get-session-timeout | default session timeout in milliseconds | 5s
47/// databases.<dbname>.endpoint | gRPC endpoint URL, e.g. grpc://localhost:1234 | -
48/// databases.<dbname>.database | full database path, e.g. /ru/service/production/database | -
49/// databases.<dbname>.credentials | credentials config passed to credentials provider component | -
50/// databases.<dbname>.min_pool_size | minimum pool size for database with name <dbname> | 10
51/// databases.<dbname>.max_pool_size | maximum pool size for database with name <dbname> | 50
52/// databases.<dbname>.get_session_retry_limit | retries count to get session, every attempt with a get-session-timeout | 5
53/// databases.<dbname>.keep-in-query-cache | whether to use query cache | true
54/// databases.<dbname>.prefer_local_dc | prefer making requests to local DataCenter | false
55/// databases.<dbname>.aliases | list of alias names for this database | []
56/// databases.<dbname>.sync_start | fail on boot time if YDB is not accessible | true
57/// databases.<dbname>.by-database-timings-buckets-ms | histogram bounds for by-database timing metrics | 40 buckets with +20% increment per step
58/// databases.<dbname>.by-query-timings-buckets-ms | histogram bounds for by-query timing metrics | 15 buckets with +100% increment per step
59
60// clang-format on
61
62class YdbComponent final : public components::ComponentBase {
63public:
64 /// @ingroup userver_component_names
65 /// @brief The default name of ydb::YdbComponent component
66 static constexpr std::string_view kName = "ydb";
67
68 YdbComponent(const components::ComponentConfig&, const components::ComponentContext&);
69
70 ~YdbComponent();
71
72 /// Get table client
73 /// @param dbname database name from static config key
74 std::shared_ptr<TableClient> GetTableClient(const std::string& dbname) const;
75
76 /// Get topic client
77 /// @param dbname database name from static config key
78 std::shared_ptr<TopicClient> GetTopicClient(const std::string& dbname) const;
79
80 /// Get coordination client
81 /// @param dbname database name from static config key
82 std::shared_ptr<CoordinationClient> GetCoordinationClient(const std::string& dbname) const;
83
84 /// Get native driver
85 /// @param dbname database name from static config key
86 /// @warning Use with care! Facilities from
87 /// `<core/include/userver/drivers/subscribable_futures.hpp>` can help with
88 /// non-blocking wait operations.
89 const NYdb::TDriver& GetNativeDriver(const std::string& dbname) const;
90
91 /// Get database path
92 /// @param dbname database name from static config key
93 const std::string& GetDatabasePath(const std::string& dbname) const;
94
95 static yaml_config::Schema GetStaticConfigSchema();
96
97private:
98 struct DatabaseUtils;
99
100 struct Database {
101 std::shared_ptr<impl::Driver> driver;
102 std::shared_ptr<TableClient> table_client;
103 std::shared_ptr<TopicClient> topic_client;
104 std::shared_ptr<CoordinationClient> coordination_client;
105 };
106
107 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
108 void WriteStatistics(utils::statistics::Writer& writer) const;
109 const Database& FindDatabase(const std::string& dbname) const;
110
111 std::unordered_map<std::string, Database> databases_;
112
113 dynamic_config::Source config_;
114
115 // These fields must be the last ones
116 concurrent::AsyncEventSubscriberScope config_subscription_;
117 utils::statistics::Entry statistic_holder_;
118};
119
120} // namespace ydb
121
122template <>
123inline constexpr bool components::kHasValidate<ydb::YdbComponent> = true;
124
125USERVER_NAMESPACE_END