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/entry.hpp>
20
21USERVER_NAMESPACE_BEGIN
22
23/// Components, clients and helpers for different databases and storages
24namespace storages {}
25
26/// 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 Redis client component
44///
45/// Provides access to a 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, 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/// ## Cluster Redis setup
114///
115/// Redis cluster is the new recommended way of setting up Redis servers
116/// with improved stability.
117///
118/// To start, set `sharding_strategy: RedisCluster` in the static config
119/// as shown above.
120///
121/// Secdist configuration is simplified:
122///
123/// 1. `"shards"` field is ignored, you can specify an empty array there;
124/// 2. `"sentinels"` field should contain some of the cluster nodes. They are
125/// only used for topology discovery; it is not necessary to list all nodes.
126
127// clang-format on
128class Redis : public ComponentBase {
129public:
130 Redis(const ComponentConfig& config, const ComponentContext& component_context);
131
132 ~Redis() override;
133
134 /// @ingroup userver_component_names
135 /// @brief The default name of components::Redis
136 static constexpr std::string_view kName = "redis";
137
138 std::shared_ptr<storages::redis::Client>
139 GetClient(const std::string& name, storages::redis::RedisWaitConnected wait_connected = {}) const;
140 [[deprecated("use GetClient()")]] std::shared_ptr<storages::redis::impl::Sentinel> Client(const std::string& name
141 ) const;
142 std::shared_ptr<storages::redis::SubscribeClient>
143 GetSubscribeClient(const std::string& name, storages::redis::RedisWaitConnected wait_connected = {}) const;
144
145 static yaml_config::Schema GetStaticConfigSchema();
146
147private:
148 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
149 void OnSecdistUpdate(const storages::secdist::SecdistConfig& cfg);
150
151 void Connect(
152 const ComponentConfig& config,
153 const ComponentContext& component_context,
154 const testsuite::RedisControl& testsuite_redis_control
155 );
156
157 void WriteStatistics(utils::statistics::Writer& writer);
158 void WriteStatisticsPubsub(utils::statistics::Writer& writer);
159
160 std::shared_ptr<storages::redis::impl::ThreadPools> thread_pools_;
161 std::unordered_map<std::string, std::shared_ptr<storages::redis::impl::Sentinel>> sentinels_;
162 std::unordered_map<std::string, std::shared_ptr<storages::redis::Client>> clients_;
163 std::unordered_map<std::string, std::shared_ptr<storages::redis::SubscribeClientImpl>> subscribe_clients_;
164
165 dynamic_config::Source config_;
166 concurrent::AsyncEventSubscriberScope config_subscription_;
167 concurrent::AsyncEventSubscriberScope secdist_subscription_;
168
169 utils::statistics::Entry statistics_holder_;
170 utils::statistics::Entry subscribe_statistics_holder_;
171
172 storages::redis::MetricsSettings::StaticSettings static_metrics_settings_;
173 rcu::Variable<storages::redis::MetricsSettings> metrics_settings_;
174 rcu::Variable<storages::redis::PubsubMetricsSettings> pubsub_metrics_settings_;
175};
176
177template <>
178inline constexpr bool kHasValidate<Redis> = true;
179
180} // namespace components
181
182USERVER_NAMESPACE_END