userver: userver/storages/redis/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/redis/component.hpp
4/// @brief @copybrief components::Redis
5
6#include <memory>
7#include <string>
8#include <unordered_map>
9
10#include <userver/components/component_base.hpp>
11#include <userver/components/component_fwd.hpp>
12#include <userver/dynamic_config/source.hpp>
13#include <userver/rcu/rcu.hpp>
14#include <userver/storages/redis/base.hpp>
15#include <userver/storages/redis/fwd.hpp>
16#include <userver/storages/redis/wait_connected_mode.hpp>
17#include <userver/storages/secdist/secdist.hpp>
18#include <userver/testsuite/redis_control.hpp>
19#include <userver/utils/statistics/fwd.hpp>
20
21USERVER_NAMESPACE_BEGIN
22
23/// Components, clients and helpers for different databases and storages
24namespace storages {}
25
26/// Valkey and Redis client and helpers
27namespace storages::redis {
28
29class SubscribeClientImpl;
30
31namespace impl {
32class Sentinel;
33class ThreadPools;
34class HealthCheckManager;
35} // namespace impl
36} // namespace storages::redis
37
38namespace components {
39
40/// @ingroup userver_components
41///
42/// @brief Valkey and Redis client component
43///
44/// Provides access to a valkey or redis cluster.
45/// Set `enable_subscriptions: true` on a `groups` entry to use both
46/// @ref components::Redis::GetClient and @ref components::Redis::GetSubscribeClient
47/// with the same database name and topology. Commands and subscriptions use
48/// separate connections to the Redis nodes.
49/// Alternatively, entries in `groups` and `subscribe_groups` with the same `db`
50/// and `config_name` share topology automatically when their `sharding_strategy`,
51/// `topology_update_method` and `max_disconnect_time` match. For Redis Cluster,
52/// both entries must also agree on whether `required` is `no_wait`.
53/// Subscription settings come from `subscribe_groups`, including
54/// `allow_reads_from_master`, `per_channel_statistics` and health checks.
55///
56/// ## Dynamic options:
57/// * @ref REDIS_COMMANDS_BUFFERING_SETTINGS
58/// * @ref REDIS_DEFAULT_COMMAND_CONTROL
59/// * @ref REDIS_METRICS_SETTINGS
60/// * @ref REDIS_PUBSUB_METRICS_SETTINGS
61/// * @ref REDIS_RETRY_BUDGET_SETTINGS
62/// * @ref REDIS_REPLICA_MONITORING_SETTINGS
63/// * @ref REDIS_SUBSCRIBER_DEFAULT_COMMAND_CONTROL
64/// * @ref REDIS_SUBSCRIPTIONS_REBALANCE_MIN_INTERVAL_SECONDS
65/// * @ref REDIS_WAIT_CONNECTED
66/// * @ref REDIS_IGNORE_HEALTH_CHECK
67///
68/// ## Static options of components::Redis :
69/// @include{doc} scripts/docs/en/components_schema/redis/src/storages/redis/component.md
70///
71/// Options inherited from @ref components::ComponentBase :
72/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
73///
74/// ## Static configuration example:
75///
76/// ```
77/// # yaml
78/// redis:
79/// groups:
80/// - config_name: cats
81/// db: hello_service_cats_catalogue
82/// sharding_strategy: RedisCluster
83/// - config_name: dogs
84/// db: hello_service_dogs_catalogue
85/// subscribe_groups:
86/// - config_name: food
87/// db: hello_service_pet_food_orders
88/// thread_pools:
89/// redis_thread_pool_size: 8
90/// sentinel_thread_pool_size: 1
91/// ```
92///
93/// ## Secdist format
94///
95/// If a `config_name` option is provided, for example
96/// `groups.some.config_name: some_name_of_your_database`, then the Secdist
97/// entry for that alias should look like following:
98/// @code{.json}
99/// {
100/// "redis_settings": {
101/// "some_name_of_your_database": {
102/// "password": "the_password_of_your_database",
103/// "sentinel_password": "the_password_for_sentinels_if_any",
104/// "secure_connection": false,
105/// "sentinels": [
106/// {"host": "the_host1_of_your_database", "port": 11564}
107/// ],
108/// "shards": [
109/// {"name": "test_master0"}
110/// ]
111/// }
112/// }
113/// }
114/// @endcode
115///
116/// For an example of Secdist setup for standalone, sentinel and cluster testsuite configurations see the snippet:
117///
118/// @snippet redis/functional_tests/integration_tests/tests/conftest.py Sample pytest redis configuration
119///
120/// Note that if the components::Secdist component has `update-period` other
121/// than 0, then new connections are created or gracefully closed as the secdist configuration change to new value.
122///
123/// ## Cluster Valkey or Cluster Redis setup
124///
125/// Valkey/Redis cluster is the new recommended way of setting up key-value datastores with improved stability.
126///
127/// To start, set `sharding_strategy: RedisCluster` in the static config
128/// as shown above.
129///
130/// Secdist configuration is simplified:
131///
132/// 1. `"shards"` field is ignored, you can specify an empty array there;
133/// 2. `"sentinels"` field should contain some of the cluster nodes. They are
134/// only used for topology discovery; it is not necessary to list all nodes.
135class Redis : public ComponentBase {
136public:
137 Redis(const ComponentConfig& config, const ComponentContext& component_context);
138
139 ~Redis() override;
140
141 /// @ingroup userver_component_names
142 /// @brief The default name of components::Redis
143 static constexpr std::string_view kName = "redis";
144
145 std::shared_ptr<storages::redis::Client> GetClient(
146 const std::string& name,
147 storages::redis::RedisWaitConnected wait_connected = {}
148 ) const;
149 [[deprecated("use GetClient()")]] std::shared_ptr<storages::redis::impl::Sentinel> Client(const std::string& name
150 ) const;
151 std::shared_ptr<storages::redis::SubscribeClient> GetSubscribeClient(
152 const std::string& name,
153 storages::redis::RedisWaitConnected wait_connected = {}
154 ) const;
155
157
158 static yaml_config::Schema GetStaticConfigSchema();
159
160private:
161 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
162 void OnSecdistUpdate(const storages::secdist::SecdistConfig& cfg);
163
164 void Connect(
165 const ComponentConfig& config,
166 const ComponentContext& component_context,
167 const testsuite::RedisControl& testsuite_redis_control
168 );
169
170 void WriteStatistics(utils::statistics::Writer& writer);
171 void WriteStatisticsPubsub(utils::statistics::Writer& writer);
172
173 std::shared_ptr<storages::redis::impl::ThreadPools> thread_pools_;
174 std::unordered_map<std::string, std::shared_ptr<storages::redis::impl::Sentinel>> sentinels_;
175 std::unordered_map<std::string, std::shared_ptr<storages::redis::Client>> clients_;
176 std::unordered_map<std::string, std::shared_ptr<storages::redis::SubscribeClientImpl>> subscribe_clients_;
177
178 std::unique_ptr<storages::redis::impl::HealthCheckManager> health_check_manager_;
179
180 dynamic_config::Source config_;
181
182 rcu::Variable<storages::redis::MetricsSettings> metrics_settings_;
183 rcu::Variable<storages::redis::PubsubMetricsSettings> pubsub_metrics_settings_;
184};
185
186template <>
187inline constexpr bool kHasValidate<Redis> = true;
188
189} // namespace components
190
191USERVER_NAMESPACE_END