userver: userver/storages/mongo/component.hpp Source File
Loading...
Searching...
No Matches
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/// Note that if the `dbalias` option is provided and the components::Secdist component has `update-period` other
81/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
82///
83/// ## Secdist format
84///
85/// If a `dbalias` option is provided, for example
86/// `dbalias: some_name_of_your_database`, then the Secdist entry for that alias
87/// should look like following:
88/// @code{.json}
89/// {
90/// "mongo_settings": {
91/// "some_name_of_your_database": {
92/// "uri": "mongodb://user:password@host:port/database_name"
93/// }
94/// }
95/// }
96/// @endcode
97
98// clang-format on
99
100class Mongo : public ComponentBase {
101public:
102 /// Component constructor
103 Mongo(const ComponentConfig&, const ComponentContext&);
104
105 /// Component destructor
106 ~Mongo() override;
107
108 /// Client pool accessor
109 storages::mongo::PoolPtr GetPool() const;
110
111 static yaml_config::Schema GetStaticConfigSchema();
112
113private:
114 void OnSecdistUpdate(const storages::secdist::SecdistConfig& config);
115
116 std::string dbalias_;
117 storages::mongo::PoolPtr pool_;
118
119 // Subscriptions must be the last fields.
120 concurrent::AsyncEventSubscriberScope secdist_subscriber_;
121 utils::statistics::Entry statistics_holder_;
122};
123
124template <>
125inline constexpr bool kHasValidate<Mongo> = true;
126
127// clang-format off
128
129/// @ingroup userver_components
130///
131/// @brief Dynamically configurable MongoDB client component
132///
133/// Provides access to a dynamically reconfigurable set of MongoDB databases.
134///
135/// ## Dynamic options:
136/// * @ref MONGO_CONGESTION_CONTROL_DATABASES_SETTINGS
137/// * @ref MONGO_CONGESTION_CONTROL_ENABLED
138/// * @ref MONGO_CONGESTION_CONTROL_SETTINGS
139/// * @ref MONGO_CONNECTION_POOL_SETTINGS
140/// * @ref MONGO_DEFAULT_MAX_TIME_MS
141///
142/// ## Static configuration example:
143///
144/// ```
145/// multi-mongo:
146/// appname: userver-sample
147/// conn_timeout: 2s
148/// so_timeout: 10s
149/// queue_timeout: 1s
150/// initial_size: 16
151/// max_size: 128
152/// idle_limit: 64
153/// connecting_limit: 8
154/// local_threshold: 15ms
155/// stats_verbosity: terse
156/// ```
157///
158/// ## Static options:
159/// Name | Description | Default value
160/// ---- | ----------- | -------------
161/// appname | application name for the DB server | userver
162/// conn_timeout | connection timeout | 2s
163/// so_timeout | socket timeout | 10s
164/// queue_timeout | max connection queue wait time | 1s
165/// initial_size | number of connections created initially (per database) | 16
166/// max_size | limit for total connections number (per database) | 128
167/// idle_limit | limit for idle connections number (per database) | 64
168/// connecting_limit | limit for establishing connections number (per database) | 8
169/// local_threshold | latency window for instance selection | mongodb default
170/// max_replication_lag | replication lag limit for usable secondaries, min. 90s | -
171/// stats_verbosity | changes the granularity of reported metrics | 'terse'
172/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
173///
174/// `stats_verbosity` accepts one of the following values:
175/// Value | Description
176/// ----- | -----------
177/// terse | Default value, report only cumulative stats and read/write totals
178/// full | Separate metrics for each operation, divided by read preference or write concern
179///
180/// Note that if the components::Secdist component has `update-period` other
181/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
182
183// clang-format on
184
185class MultiMongo : public ComponentBase {
186public:
187 /// @ingroup userver_component_names
188 /// @brief The default name of components::MultiMongo
189 static constexpr std::string_view kName = "multi-mongo";
190
191 /// Component constructor
192 MultiMongo(const ComponentConfig&, const ComponentContext&);
193
194 /// Component destructor
195 ~MultiMongo() override;
196
197 /// @brief Client pool accessor
198 /// @param dbalias name previously passed to `AddPool`
199 /// @throws PoolNotFound if no such database is enabled
200 storages::mongo::PoolPtr GetPool(const std::string& dbalias) const;
201
202 /// @brief Adds a database to the working set by its name.
203 /// Equivalent to
204 /// `NewPoolSet()`-`AddExistingPools()`-`AddPool(dbalias)`-`Activate()`
205 /// @param dbalias name of the database in secdist config
206 void AddPool(std::string dbalias);
207
208 /// @brief Removes the database with the specified name from the working set.
209 /// Equivalent to
210 /// `NewPoolSet()`-`AddExistingPools()`-`RemovePool(dbalias)`-`Activate()`
211 /// @param dbalias name of the database passed to AddPool
212 /// @returns whether the database was in the working set
213 bool RemovePool(const std::string& dbalias);
214
215 /// Creates an empty database set bound to the component
216 storages::mongo::MultiMongo::PoolSet NewPoolSet();
217
218 using PoolSet = storages::mongo::MultiMongo::PoolSet;
219
220 static yaml_config::Schema GetStaticConfigSchema();
221
222private:
223 storages::mongo::MultiMongo multi_mongo_;
224
225 // Subscriptions must be the last fields.
226 utils::statistics::Entry statistics_holder_;
227};
228
229template <>
230inline constexpr bool kHasValidate<MultiMongo> = true;
231
232} // namespace components
233
234USERVER_NAMESPACE_END