userver: userver/storages/postgres/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/postgres/component.hpp
4/// @brief @copybrief components::Postgres
5
6#include <chrono>
7
8#include <userver/components/component_base.hpp>
9#include <userver/concurrent/async_event_source.hpp>
10#include <userver/dynamic_config/snapshot.hpp>
11#include <userver/engine/mutex.hpp>
12#include <userver/storages/postgres/database.hpp>
13#include <userver/storages/secdist/secdist.hpp>
14#include <userver/utils/statistics/fwd.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace components {
19
20/// @ingroup userver_components
21///
22/// @brief PosgreSQL client component
23///
24/// Provides access to a PostgreSQL cluster.
25///
26/// ## Dynamic options:
27/// * @ref POSTGRES_DEFAULT_COMMAND_CONTROL
28/// * @ref POSTGRES_HANDLERS_COMMAND_CONTROL
29/// * @ref POSTGRES_QUERIES_COMMAND_CONTROL
30/// * @ref POSTGRES_CONNECTION_POOL_SETTINGS
31/// * @ref POSTGRES_TOPOLOGY_SETTINGS
32/// * @ref POSTGRES_CONNECTION_SETTINGS
33/// * @ref POSTGRES_STATEMENT_METRICS_SETTINGS
34/// * @ref POSTGRES_CONNLIMIT_MODE_AUTO_ENABLED
35///
36/// ## Static configuration example:
37///
38/// ```
39/// # yaml
40/// postgres-taxi:
41/// dbalias: taxi
42/// blocking_task_processor: task-processor-name
43/// max_replication_lag: 60s
44/// min_pool_size: 4
45/// max_pool_size: 15
46/// max_queue_size: 200
47/// max_statement_metrics: 50
48/// ```
49/// You must specify either `dbalias` or `dbconnection`.
50/// If the component is configured with an alias, it will lookup connection data
51/// in Secdist.
52///
53/// It is a common practice to provide a database connection string via
54/// environment variables. To retrieve a value from the environment use
55/// `dbconnection#env: THE_ENV_VARIABLE_WITH_CONNECTION_STRING` as described
56/// in yaml_config::YamlConfig.
57///
58/// You must specify `blocking_task_processor` as well.
59///
60/// `max_replication_lag` can be used to tune replication lag limit for replicas.
61/// Once the replica lag exceeds this value it will be automatically disabled.
62/// Note, however, that client-size lag detection is not precise in nature
63/// and can only provide the precision of couple seconds.
64///
65/// ## Secdist format
66///
67/// A PosgreSQL alias in secdist is described as a JSON array of objects
68/// containing a single cluster description. There are two formats of describing
69/// a cluster, the first one assigns predefined roles to DSNs, the second one
70/// is just a list of DSNs and the Postgres component takes care of discovering
71/// the cluster's topology itself.
72///
73/// Note that if the `dbalias` option is provided and components::Secdist component has `update-period` other
74/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
75///
76/// ### Predefined roles
77///
78/// In predefined roles format the component requires single-host connection
79/// strings.
80///
81/// ```json
82/// {
83/// "shard_number" : 0,
84/// "master": "host=localhost dbname=example",
85/// "sync_slave": "host=localhost dbname=example",
86/// "slaves": [
87/// "host=localhost dbname=example"
88/// ]
89/// }
90/// ```
91///
92/// The predefined roles format is deprecated and the support will be removed
93/// soon.
94///
95/// ### Automatic discovery
96///
97/// In automatic discovery format the connection strings are any valid
98/// PostgreSQL connection strings including multi-host ones with the exception
99/// of `target_session_attrs` which will be ignored.
100///
101/// ```json
102/// {
103/// "shard_number" : 0,
104/// "hosts": [
105/// "host=host1,host2,host3 dbname=example",
106/// "postgresql://host1:5432,host2:6432,host3:12000/example"
107/// ]
108/// }
109/// ```
110///
111/// The `shard_number` parameter is required in both formats and must match the
112/// index of cluster description object in the alias array.
113///
114/// Please see [PostgreSQL documentation](https://www.postgresql.org/docs/12/libpq-connect.html#LIBPQ-CONNSTRING)
115/// on connection strings.
116///
117/// ## Static options of components::Postgres :
118/// @include{doc} scripts/docs/en/components_schema/postgresql/src/storages/postgres/component.md
119///
120/// Options inherited from @ref components::ComponentBase :
121/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
122class Postgres : public ComponentBase {
123public:
124 /// Default shard number
125 static constexpr size_t kDefaultShardNumber = 0;
126 /// Default command control
127 static constexpr storages::postgres::CommandControl kDefaultCommandControl{
128 std::chrono::milliseconds{500}, // network timeout
129 std::chrono::milliseconds{250} // statement timeout
130 };
131
132 /// Component constructor
133 Postgres(const ComponentConfig&, const ComponentContext&);
134 /// Component destructor
135 ~Postgres() override;
136
137 /// Cluster accessor for default shard number
138 storages::postgres::ClusterPtr GetCluster() const;
139
140 /// Cluster accessor for specific shard number
141 storages::postgres::ClusterPtr GetClusterForShard(size_t shard) const;
142
143 /// Get total shard count
144 size_t GetShardCount() const;
145
146 /// Get database object
147 storages::postgres::DatabasePtr GetDatabase() const { return database_; }
148
149 /// Reports statistics for PostgreSQL driver
150 void ExtendStatistics(utils::statistics::Writer& writer);
151
152 static yaml_config::Schema GetStaticConfigSchema();
153
154private:
155 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
156
157 void OnSecdistUpdate(const storages::secdist::SecdistConfig& secdist);
158
159 std::string name_;
160 std::string db_name_;
161 std::string dbalias_;
162 storages::postgres::ClusterSettings initial_settings_;
163 storages::postgres::DatabasePtr database_;
164
165 // Subscriptions must be the last fields, because the fields above are used
166 // from callbacks.
167 concurrent::AsyncEventSubscriberScope config_subscription_;
168 concurrent::AsyncEventSubscriberScope secdist_subscription_;
169 dynamic_config::Source config_source_;
170};
171
172template <>
173inline constexpr bool kHasValidate<Postgres> = true;
174
175} // namespace components
176
177USERVER_NAMESPACE_END