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/loggable_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.
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>.keep-in-query-cache | whether to use query cache | true
53/// databases.<dbname>.prefer_local_dc | prefer making requests to local DataCenter | false
54/// databases.<dbname>.aliases | list of alias names for this database | []
55/// databases.<dbname>.sync_start | fail on boot time if YDB is not accessible | true
56/// databases.<dbname>.by-database-timings-buckets-ms | histogram bounds for by-database timing metrics | 40 buckets with +20% increment per step
57/// databases.<dbname>.by-query-timings-buckets-ms | histogram bounds for by-query timing metrics | 15 buckets with +100% increment per step
58
59// clang-format on
60
61class YdbComponent final : public components::LoggableComponentBase {
62 public:
63 /// @ingroup userver_component_names
64 /// @brief The default name of ydb::YdbComponent component
65 static constexpr std::string_view kName = "ydb";
66
67 YdbComponent(const components::ComponentConfig&,
68 const components::ComponentContext&);
69
70 ~YdbComponent();
71
72 std::shared_ptr<TableClient> GetTableClient(const std::string& dbname) const;
73
74 std::shared_ptr<TopicClient> GetTopicClient(const std::string& dbname) const;
75
76 std::shared_ptr<CoordinationClient> GetCoordinationClient(
77 const std::string& dbname) const;
78
79 const NYdb::TDriver& GetNativeDriver(const std::string& dbname) const;
80
81 const std::string& GetDatabasePath(const std::string& dbname) const;
82
83 static yaml_config::Schema GetStaticConfigSchema();
84
85 private:
86 struct DatabaseUtils;
87
88 struct Database {
89 std::shared_ptr<impl::Driver> driver;
90 std::shared_ptr<TableClient> table_client;
91 std::shared_ptr<TopicClient> topic_client;
92 std::shared_ptr<CoordinationClient> coordination_client;
93 };
94
95 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
96 void WriteStatistics(utils::statistics::Writer& writer) const;
97 const Database& FindDatabase(const std::string& dbname) const;
98
99 std::unordered_map<std::string, Database> databases_;
100
101 dynamic_config::Source config_;
102
103 // These fields must be the last ones
104 concurrent::AsyncEventSubscriberScope config_subscription_;
105 utils::statistics::Entry statistic_holder_;
106};
107
108} // namespace ydb
109
110template <>
111inline constexpr bool components::kHasValidate<ydb::YdbComponent> = true;
112
113USERVER_NAMESPACE_END