userver: userver/storages/redis/command_control.hpp Source File
Loading...
Searching...
No Matches
command_control.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// @brief @copybrief storages::redis::CommandControl
5
6#include <atomic>
7#include <chrono>
8#include <compare>
9#include <cstdint>
10#include <optional>
11#include <string>
12
13#include <userver/storages/redis/fwd.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace testsuite {
18struct RedisControl;
19} // namespace testsuite
20
21namespace storages::redis {
22
23inline constexpr std::chrono::milliseconds kDefaultTimeoutSingle{500};
24inline constexpr std::chrono::milliseconds kDefaultTimeoutAll{2000};
25inline constexpr std::size_t kDefaultMaxRetries{4};
26
27/// Opaque Id of Redis server instance / any server instance.
28class ServerId {
29public:
30 /// Default: any server
31 constexpr ServerId() = default;
32
33 bool IsAny() const { return id_ == -1; }
34
35 auto operator<=>(const ServerId&) const = default;
36
37 static ServerId Generate() {
38 ServerId sid;
39 sid.id_ = next_id++;
40 return sid;
41 }
42
43 static ServerId Invalid() { return invalid; }
44
45 int64_t GetId() const { return id_; }
46
47 void SetDescription(std::string description) const;
48 void RemoveDescription() const;
49 std::string GetDescription() const;
50
51private:
52 static std::atomic<std::int64_t> next_id;
53 static ServerId invalid;
54
55 std::int64_t id_{-1};
56};
57
59 std::size_t operator()(ServerId server_id) const noexcept { return std::hash<std::size_t>{}(server_id.GetId()); }
60};
61
63
64/// Can be used as an additional parameter in some commands to force retries to
65/// master if slave returned a nil reply.
67
68/// Redis command execution options
70 enum class Strategy {
71 /// Same as kEveryDc
73
74 /// Send ~1/N requests to an instance with ping N ms
76
77 /// Send requests to Redis instances located in local DC (by Conductor info)
79
80 /// Send requests to 'best_dc_count' Redis instances with the min ping
82 };
83
84 /// Timeout for a single attempt to execute command
85 std::optional<std::chrono::milliseconds> timeout_single;
86
87 /// Command execution timeout, including retries
88 std::optional<std::chrono::milliseconds> timeout_all;
89
90 /// The maximum number of retries while executing command
91 std::optional<std::size_t> max_retries;
92
93 /// Server instance selection strategy
94 std::optional<Strategy> strategy{};
95
96 /// How many nearest DCs to use
97 std::optional<std::size_t> best_dc_count{};
98
99 /// Force execution on master node
100 std::optional<bool> force_request_to_master{};
101
102 /// Consider ping to nodes in instance selection (true if not specified).
103 /// Setting to false makes the load on the database evenly distributed, but may increase timings
104 std::optional<bool> consider_ping{};
105
106 /// Server latency limit
107 std::optional<std::chrono::milliseconds> max_ping_latency{};
108
109 /// Allow execution of readonly commands on master node along with replica
110 /// nodes to facilitate load distribution
111 std::optional<bool> allow_reads_from_master{};
112
113 /// Controls if the command execution accounted in statistics
114 std::optional<bool> account_in_statistics{};
115
116 /// If set, force execution on specific shard
117 std::optional<std::size_t> force_shard_idx{};
118
119 /// Split execution of multi-key commands (i.e., MGET) to multiple requests
120 std::optional<std::size_t> chunk_size{};
121
122 /// If set, the user wants a specific Redis instance to handle the command.
123 /// Sentinel may not redirect the command to other instances. strategy is
124 /// ignored.
125 std::optional<ServerId> force_server_id{};
126
127 /// If set, command retries are directed to the master instance
129
130 /// Need to be set to if you do manual retries and want retry budget to work.
131 /// If set value other than 0 then request treated as retry.
132 /// 0 - original request, 1 - first retry, 2 - second and so on
133 size_t retry_counter{0};
134
135 constexpr CommandControl() = default;
136 constexpr CommandControl(
137 const std::optional<std::chrono::milliseconds>& timeout_single,
138 const std::optional<std::chrono::milliseconds>& timeout_all,
139 const std::optional<size_t>& max_retries
140 ) noexcept
141 : timeout_single(timeout_single), timeout_all(timeout_all), max_retries(max_retries) {}
142
143 auto operator<=>(const CommandControl&) const = default;
144
145 CommandControl MergeWith(const CommandControl& b) const;
146 CommandControl MergeWith(const testsuite::RedisControl&) const;
147 CommandControl MergeWith(RetryNilFromMaster) const;
148
149 std::string ToString() const;
150};
151
152/// Returns CommandControl::Strategy from string
154
155/// Returns string representation of CommandControl::Strategy
157
158} // namespace storages::redis
159
160USERVER_NAMESPACE_END