userver: userver/storages/redis/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/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/entry.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;
34} // namespace impl
35} // namespace storages::redis
36
37namespace components {
38
39// clang-format off
40
41/// @ingroup userver_components
42///
43/// @brief Valkey and Redis client component
44///
45/// Provides access to a valkey or redis cluster.
46///
47/// ## Dynamic options:
48/// * @ref REDIS_COMMANDS_BUFFERING_SETTINGS
49/// * @ref REDIS_DEFAULT_COMMAND_CONTROL
50/// * @ref REDIS_METRICS_SETTINGS
51/// * @ref REDIS_PUBSUB_METRICS_SETTINGS
52/// * @ref REDIS_RETRY_BUDGET_SETTINGS
53/// * @ref REDIS_REPLICA_MONITORING_SETTINGS
54/// * @ref REDIS_SUBSCRIBER_DEFAULT_COMMAND_CONTROL
55/// * @ref REDIS_SUBSCRIPTIONS_REBALANCE_MIN_INTERVAL_SECONDS
56/// * @ref REDIS_WAIT_CONNECTED
57///
58/// ## Static options:
59/// Name | Description | Default value
60/// ---- | ----------- | -------------
61/// thread_pools.redis_thread_pool_size | thread count to serve Redis requests | -
62/// thread_pools.sentinel_thread_pool_size | thread count to serve sentinel requests. | -
63/// groups | array of redis clusters to work with excluding subscribers | -
64/// groups.[].config_name | key name in secdist with options for this cluster | -
65/// groups.[].db | name to refer to the cluster in components::Redis::GetClient() | -
66/// groups.[].sharding_strategy | one of RedisCluster, RedisStandalone, KeyShardCrc32, KeyShardTaximeterCrc32 or KeyShardGpsStorageDriver | "KeyShardTaximeterCrc32"
67/// groups.[].allow_reads_from_master | allows read requests from master instance | false
68/// subscribe_groups | array of redis clusters to work with in subscribe mode | -
69/// subscribe_groups.[].config_name | key name in secdist with options for this cluster | -
70/// subscribe_groups.[].db | name to refer to the cluster in components::Redis::GetSubscribeClient() | -
71/// subscribe_groups.[].sharding_strategy | either RedisCluster or KeyShardTaximeterCrc32 | "KeyShardTaximeterCrc32"
72///
73/// ## Static configuration example:
74///
75/// ```
76/// # yaml
77/// redis:
78/// groups:
79/// - config_name: cats
80/// db: hello_service_cats_catalogue
81/// sharding_strategy: RedisCluster
82/// - config_name: dogs
83/// db: hello_service_dogs_catalogue
84/// subscribe_groups:
85/// - config_name: food
86/// db: hello_service_pet_food_orders
87/// thread_pools:
88/// redis_thread_pool_size: 8
89/// sentinel_thread_pool_size: 1
90/// ```
91///
92/// ## Secdist format
93///
94/// If a `config_name` option is provided, for example
95/// `groups.some.config_name: some_name_of_your_database`, then the Secdist
96/// entry for that alias should look like following:
97/// @code{.json}
98/// {
99/// "redis_settings": {
100/// "some_name_of_your_database": {
101/// "password": "the_password_of_your_database",
102/// "sentinels": [
103/// {"host": "the_host1_of_your_database", "port": 11564}
104/// ],
105/// "shards": [
106/// {"name": "test_master0"}
107/// ]
108/// }
109/// }
110/// }
111/// @endcode
112///
113/// For an example of Secdist setup for standalone, sentinel and cluster testsuite configurations see the snippet:
114///
115/// @snippet redis/functional_tests/integration_tests/tests/conftest.py Sample pytest redis configuration
116///
117/// ## Cluster Valkey or Cluster Redis setup
118///
119/// Valkey/Redis cluster is the new recommended way of setting up key-value datastores with improved stability.
120///
121/// To start, set `sharding_strategy: RedisCluster` in the static config
122/// as shown above.
123///
124/// Secdist configuration is simplified:
125///
126/// 1. `"shards"` field is ignored, you can specify an empty array there;
127/// 2. `"sentinels"` field should contain some of the cluster nodes. They are
128/// only used for topology discovery; it is not necessary to list all nodes.
129
130// clang-format on
131class Redis : public ComponentBase {
132public:
133 Redis(const ComponentConfig& config, const ComponentContext& component_context);
134
135 ~Redis() override;
136
137 /// @ingroup userver_component_names
138 /// @brief The default name of components::Redis
139 static constexpr std::string_view kName = "redis";
140
141 std::shared_ptr<storages::redis::Client>
142 GetClient(const std::string& name, storages::redis::RedisWaitConnected wait_connected = {}) const;
143 [[deprecated("use GetClient()")]] std::shared_ptr<storages::redis::impl::Sentinel> Client(const std::string& name
144 ) const;
145 std::shared_ptr<storages::redis::SubscribeClient>
146 GetSubscribeClient(const std::string& name, storages::redis::RedisWaitConnected wait_connected = {}) const;
147
148 static yaml_config::Schema GetStaticConfigSchema();
149
150private:
151 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
152 void OnSecdistUpdate(const storages::secdist::SecdistConfig& cfg);
153
154 void Connect(
155 const ComponentConfig& config,
156 const ComponentContext& component_context,
157 const testsuite::RedisControl& testsuite_redis_control
158 );
159
160 void WriteStatistics(utils::statistics::Writer& writer);
161 void WriteStatisticsPubsub(utils::statistics::Writer& writer);
162
163 std::shared_ptr<storages::redis::impl::ThreadPools> thread_pools_;
164 std::unordered_map<std::string, std::shared_ptr<storages::redis::impl::Sentinel>> sentinels_;
165 std::unordered_map<std::string, std::shared_ptr<storages::redis::Client>> clients_;
166 std::unordered_map<std::string, std::shared_ptr<storages::redis::SubscribeClientImpl>> subscribe_clients_;
167
168 dynamic_config::Source config_;
169 concurrent::AsyncEventSubscriberScope config_subscription_;
170 concurrent::AsyncEventSubscriberScope secdist_subscription_;
171
172 utils::statistics::Entry statistics_holder_;
173 utils::statistics::Entry subscribe_statistics_holder_;
174
175 storages::redis::MetricsSettings::StaticSettings static_metrics_settings_;
176 rcu::Variable<storages::redis::MetricsSettings> metrics_settings_;
177 rcu::Variable<storages::redis::PubsubMetricsSettings> pubsub_metrics_settings_;
178};
179
180template <>
181inline constexpr bool kHasValidate<Redis> = true;
182
183} // namespace components
184
185USERVER_NAMESPACE_END