userver: userver/storages/mongo/component.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/mongo/component.hpp
4/// @brief @copybrief components::Mongo
5
6#include <userver/components/component_base.hpp>
7#include <userver/storages/mongo/multi_mongo.hpp>
8#include <userver/storages/mongo/pool.hpp>
9#include <userver/storages/secdist/component.hpp>
10#include <userver/utils/statistics/entry.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace components {
15
16// clang-format off
17
18/// @ingroup userver_components
19///
20/// @brief MongoDB client component
21///
22/// Provides access to a MongoDB database.
23///
24/// ## Dynamic options:
25/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
26/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
27/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
28/// * @ref MONGO_CONNECTION_POOL_SETTINGS
29/// * @ref MONGO_DEFAULT_MAX_TIME_MS
30///
31/// ## Static configuration example:
32///
33/// ```
34/// mongo-taxi:
35/// dbalias: taxi
36/// appname: userver-sample
37/// conn_timeout: 2s
38/// so_timeout: 10s
39/// queue_timeout: 1s
40/// initial_size: 16
41/// max_size: 128
42/// idle_limit: 64
43/// connecting_limit: 8
44/// local_threshold: 15ms
45/// maintenance_period: 15s
46/// stats_verbosity: terse
47/// ```
48/// You must specify one of `dbalias` or `dbconnection`.
49///
50/// ## Static options:
51/// Name | Description | Default value
52/// ---- | ----------- | -------------
53/// dbalias | name of the database in secdist config (if available) | --
54/// dbconnection | connection string (used if no dbalias specified) | --
55/// appname | application name for the DB server | userver
56/// conn_timeout | connection timeout | 2s
57/// so_timeout | socket timeout | 10s
58/// queue_timeout | max connection queue wait time | 1s
59/// initial_size | number of connections created initially | 16
60/// max_size | limit for total connections number | 128
61/// idle_limit | limit for idle connections number | 64
62/// connecting_limit | limit for establishing connections number | 8
63/// local_threshold | latency window for instance selection | mongodb default
64/// max_replication_lag | replication lag limit for usable secondaries, min. 90s | -
65/// maintenance_period | pool maintenance period (idle connections pruning etc.) | 15s
66/// stats_verbosity | changes the granularity of reported metrics | 'terse'
67/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
68///
69/// `stats_verbosity` accepts one of the following values:
70/// Value | Description
71/// ----- | -----------
72/// terse | Default value, report only cumulative stats and read/write totals
73/// full | Separate metrics for each operation, divided by read preference or write concern
74///
75/// It is a common practice to provide a database connection string via
76/// environment variables. To retrieve a value from the environment use
77/// `dbconnection#env: THE_ENV_VARIABLE_WITH_CONNECTION_STRING` as described
78/// in yaml_config::YamlConfig.
79///
80/// ## Secdist format
81///
82/// If a `dbalias` option is provided, for example
83/// `dbalias: some_name_of_your_database`, then the Secdist entry for that alias
84/// should look like following:
85/// @code{.json}
86/// {
87/// "mongo_settings": {
88/// "some_name_of_your_database": {
89/// "uri": "mongodb://user:password@host:port/database_name"
90/// }
91/// }
92/// }
93/// @endcode
94
95// clang-format on
96
97class Mongo : public ComponentBase {
98public:
99 /// Component constructor
100 Mongo(const ComponentConfig&, const ComponentContext&);
101
102 /// Component destructor
103 ~Mongo() override;
104
105 /// Client pool accessor
106 storages::mongo::PoolPtr GetPool() const;
107
108 static yaml_config::Schema GetStaticConfigSchema();
109
110private:
111 void OnSecdistUpdate(const storages::secdist::SecdistConfig& config);
112
113 std::string dbalias_;
114 storages::mongo::PoolPtr pool_;
115
116 // Subscriptions must be the last fields.
117 concurrent::AsyncEventSubscriberScope secdist_subscriber_;
118 utils::statistics::Entry statistics_holder_;
119};
120
121template <>
122inline constexpr bool kHasValidate<Mongo> = true;
123
124// clang-format off
125
126/// @ingroup userver_components
127///
128/// @brief Dynamically configurable MongoDB client component
129///
130/// Provides access to a dynamically reconfigurable set of MongoDB databases.
131///
132/// ## Dynamic options:
133/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
134/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
135/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
136/// * @ref MONGO_CONNECTION_POOL_SETTINGS
137/// * @ref MONGO_DEFAULT_MAX_TIME_MS
138///
139/// ## Static configuration example:
140///
141/// ```
142/// multi-mongo:
143/// appname: userver-sample
144/// conn_timeout: 2s
145/// so_timeout: 10s
146/// queue_timeout: 1s
147/// initial_size: 16
148/// max_size: 128
149/// idle_limit: 64
150/// connecting_limit: 8
151/// local_threshold: 15ms
152/// stats_verbosity: terse
153/// ```
154///
155/// ## Static options:
156/// Name | Description | Default value
157/// ---- | ----------- | -------------
158/// appname | application name for the DB server | userver
159/// conn_timeout | connection timeout | 2s
160/// so_timeout | socket timeout | 10s
161/// queue_timeout | max connection queue wait time | 1s
162/// initial_size | number of connections created initially (per database) | 16
163/// max_size | limit for total connections number (per database) | 128
164/// idle_limit | limit for idle connections number (per database) | 64
165/// connecting_limit | limit for establishing connections number (per database) | 8
166/// local_threshold | latency window for instance selection | mongodb default
167/// max_replication_lag | replication lag limit for usable secondaries, min. 90s | -
168/// stats_verbosity | changes the granularity of reported metrics | 'terse'
169/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
170///
171/// `stats_verbosity` accepts one of the following values:
172/// Value | Description
173/// ----- | -----------
174/// terse | Default value, report only cumulative stats and read/write totals
175/// full | Separate metrics for each operation, divided by read preference or write concern
176
177// clang-format on
178
179class MultiMongo : public ComponentBase {
180public:
181 /// @ingroup userver_component_names
182 /// @brief The default name of components::MultiMongo
183 static constexpr std::string_view kName = "multi-mongo";
184
185 /// Component constructor
186 MultiMongo(const ComponentConfig&, const ComponentContext&);
187
188 /// Component destructor
189 ~MultiMongo() override;
190
191 /// @brief Client pool accessor
192 /// @param dbalias name previously passed to `AddPool`
193 /// @throws PoolNotFound if no such database is enabled
194 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
195
196 /// @brief Adds a database to the working set by its name.
197 /// Equivalent to
198 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
199 /// @param dbalias name of the database in secdist config
200 void AddPool(std::string dbalias);
201
202 /// @brief Removes the database with the specified name from the working set.
203 /// Equivalent to
204 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
205 /// @param dbalias name of the database passed to AddPool
206 /// @returns whether the database was in the working set
207 bool RemovePool(const std::string& dbalias);
208
209 /// Creates an empty database set bound to the component
210 storages::mongo::MultiMongo::PoolSet NewPoolSet();
211
212 using PoolSet = storages::mongo::MultiMongo::PoolSet;
213
214 static yaml_config::Schema GetStaticConfigSchema();
215
216private:
217 storages::mongo::MultiMongo multi_mongo_;
218
219 // Subscriptions must be the last fields.
220 utils::statistics::Entry statistics_holder_;
221};
222
223template <>
224inline constexpr bool kHasValidate<MultiMongo> = true;
225
226} // namespace components
227
228USERVER_NAMESPACE_END