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_fwd.hpp>
11#include <userver/components/loggable_component_base.hpp>
12#include <userver/dynamic_config/source.hpp>
13#include <userver/rcu/rcu.hpp>
14#include <userver/storages/redis/impl/base.hpp>
15#include <userver/storages/redis/impl/wait_connected_mode.hpp>
16#include <userver/testsuite/redis_control.hpp>
17#include <userver/utils/statistics/entry.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace redis {
22class Sentinel;
23class ThreadPools;
24} // namespace redis
25
26/// Components, clients and helpers for different databases and storages
27namespace storages {}
28
29/// Redis client
30namespace storages::redis {
31class Client;
32class SubscribeClient;
33class SubscribeClientImpl;
34} // namespace storages::redis
35
36namespace components {
37
38// clang-format off
39
40/// @ingroup userver_components
41///
42/// @brief Redis client component
43///
44/// Provides access to a redis cluster.
45///
46/// ## Dynamic options:
47/// * @ref REDIS_DEFAULT_COMMAND_CONTROL
48/// * @ref REDIS_SUBSCRIBER_DEFAULT_COMMAND_CONTROL
49/// * @ref REDIS_SUBSCRIPTIONS_REBALANCE_MIN_INTERVAL_SECONDS
50/// * @ref REDIS_WAIT_CONNECTED
51/// * @ref REDIS_COMMANDS_BUFFERING_SETTINGS
52/// * @ref REDIS_METRICS_SETTINGS
53/// * @ref REDIS_PUBSUB_METRICS_SETTINGS
54///
55/// ## Static options:
56/// Name | Description | Default value
57/// ---- | ----------- | -------------
58/// thread_pools.redis_thread_pool_size | thread count to serve Redis requests | -
59/// thread_pools.sentinel_thread_pool_size | thread count to serve sentinel requests. | -
60/// groups | array of redis clusters to work with excluding subscribers | -
61/// groups.[].config_name | key name in secdist with options for this cluster | -
62/// groups.[].db | name to refer to the cluster in components::Redis::GetClient() | -
63/// groups.[].sharding_strategy | one of RedisCluster, KeyShardCrc32, KeyShardTaximeterCrc32 or KeyShardGpsStorageDriver | "KeyShardTaximeterCrc32"
64/// groups.[].allow_reads_from_master | allows read requests from master instance | false
65/// subscribe_groups | array of redis clusters to work with in subscribe mode | -
66/// subscribe_groups.[].config_name | key name in secdist with options for this cluster | -
67/// subscribe_groups.[].db | name to refer to the cluster in components::Redis::GetSubscribeClient() | -
68/// subscribe_groups.[].sharding_strategy | either RedisCluster or KeyShardTaximeterCrc32 | "KeyShardTaximeterCrc32"
69///
70/// ## Static configuration example:
71///
72/// ```
73/// redis:
74/// groups:
75/// - config_name: taxi-tmp
76/// db: taxi-tmp
77/// sharding_strategy: "RedisCluster"
78/// - config_name: taxi-tmp-pubsub
79/// db: taxi-tmp-pubsub
80/// subscribe_groups:
81/// - config_name: taxi-tmp-pubsub
82/// db: taxi-tmp-pubsub
83/// thread_pools:
84/// redis_thread_pool_size: 8
85/// sentinel_thread_pool_size: 1
86/// ```
87
88// clang-format on
90 public:
91 Redis(const ComponentConfig& config,
92 const ComponentContext& component_context);
93
94 ~Redis() override;
95
96 /// @ingroup userver_component_names
97 /// @brief The default name of components::Redis
98 static constexpr std::string_view kName = "redis";
99
100 std::shared_ptr<storages::redis::Client> GetClient(
101 const std::string& name,
102 USERVER_NAMESPACE::redis::RedisWaitConnected wait_connected = {}) const;
103 [[deprecated("use GetClient()")]] std::shared_ptr<redis::Sentinel> Client(
104 const std::string& name) const;
105 std::shared_ptr<storages::redis::SubscribeClient> GetSubscribeClient(
106 const std::string& name,
107 USERVER_NAMESPACE::redis::RedisWaitConnected wait_connected = {}) const;
108
109 static yaml_config::Schema GetStaticConfigSchema();
110
111 private:
112 void OnConfigUpdate(const dynamic_config::Snapshot& cfg);
113
114 void Connect(const ComponentConfig& config,
115 const ComponentContext& component_context,
116 const testsuite::RedisControl& testsuite_redis_control);
117
118 void WriteStatistics(utils::statistics::Writer& writer);
119 void WriteStatisticsPubsub(utils::statistics::Writer& writer);
120
121 std::shared_ptr<redis::ThreadPools> thread_pools_;
122 std::unordered_map<std::string, std::shared_ptr<redis::Sentinel>> sentinels_;
123 std::unordered_map<std::string, std::shared_ptr<storages::redis::Client>>
124 clients_;
125 std::unordered_map<std::string,
126 std::shared_ptr<storages::redis::SubscribeClientImpl>>
127 subscribe_clients_;
128
129 dynamic_config::Source config_;
130 concurrent::AsyncEventSubscriberScope config_subscription_;
131
132 utils::statistics::Entry statistics_holder_;
133 utils::statistics::Entry subscribe_statistics_holder_;
134
135 rcu::Variable<redis::MetricsSettings> metrics_settings_;
136 rcu::Variable<redis::PubsubMetricsSettings> pubsub_metrics_settings_;
137};
138
139template <>
140inline constexpr bool kHasValidate<Redis> = true;
141
142} // namespace components
143
144USERVER_NAMESPACE_END