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 {
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 CommandControl() = default;
131 CommandControl(const std::optional<std::chrono::milliseconds>& timeout_single,
132 const std::optional<std::chrono::milliseconds>& timeout_all,
133 const std::optional<size_t>& max_retries);
134
135 CommandControl MergeWith(const CommandControl& b) const;
136 CommandControl MergeWith(const testsuite::RedisControl&) const;
137 CommandControl MergeWith(RetryNilFromMaster) const;
138
139 std::string ToString() const;
140};
141
142/// Returns CommandControl::Strategy from string
144
145} // namespace redis
146
147USERVER_NAMESPACE_END