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