userver: userver/storages/redis/command_control.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 {
28 public:
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
53 private:
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 {
62 return std::hash<std::size_t>{}(server_id.GetId());
63 }
64};
65
67
68/// Can be used as an additional parameter in some commands to force retries to
69/// master if slave returned a nil reply.
71
72/// Redis command execution options
74 enum class Strategy {
75 /// Same as kEveryDc
77
78 /// Send ~1/N requests to an instance with ping N ms
80
81 /// Send requests to Redis instances located in local DC (by Conductor info)
83
84 /// Send requests to 'best_dc_count' Redis instances with the min ping
86 };
87
88 /// Timeout for a single attempt to execute command
89 std::optional<std::chrono::milliseconds> timeout_single;
90
91 /// Command execution timeout, including retries
92 std::optional<std::chrono::milliseconds> timeout_all;
93
94 /// The maximum number of retries while executing command
95 std::optional<std::size_t> max_retries;
96
97 /// Server instance selection strategy
98 std::optional<Strategy> strategy;
99
100 /// How many nearest DCs to use
101 std::optional<std::size_t> best_dc_count;
102
103 /// Force execution on master node
104 std::optional<bool> force_request_to_master;
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
134
135 CommandControl() = default;
136 CommandControl(const std::optional<std::chrono::milliseconds>& timeout_single,
137 const std::optional<std::chrono::milliseconds>& timeout_all,
138 const std::optional<size_t>& max_retries);
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