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}
19
20namespace storages::redis {
21class Client;
22} // namespace storages::redis
23
24namespace redis {
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::chrono::milliseconds timeout_single = std::chrono::milliseconds{500};
90
91 /// Command execution timeout, including retries
92 std::chrono::milliseconds timeout_all = std::chrono::milliseconds{2000};
93
94 /// The maximum number of retries while executing command
96
97 /// Server instance selection strategy
99
100 /// How many nearest DCs to use, 0 for no limit
102
103 /// Server latency limit
104 std::chrono::milliseconds max_ping_latency = std::chrono::milliseconds(0);
105
106 /// Force execution on master node
108
109 /// Allow execution of readonly commands on master node along with replica
110 /// nodes to facilitate load distribution.
112
113 /// Controls if the command execution accounted 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::size_t chunk_size = 0;
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.
126
127 /// If set, command retries are directed to the master instance
129
130 CommandControl() = default;
131 CommandControl(std::chrono::milliseconds timeout_single,
132 std::chrono::milliseconds timeout_all, std::size_t max_retries,
133 Strategy strategy = Strategy::kDefault, int best_dc_count = 0,
134 std::chrono::milliseconds max_ping_latency =
135 std::chrono::milliseconds(0));
136
137 CommandControl MergeWith(const CommandControl& b) const;
138 CommandControl MergeWith(const testsuite::RedisControl&) const;
139 CommandControl MergeWith(RetryNilFromMaster) const;
140
141 std::string ToString() const;
142
143 friend class Sentinel;
144 friend class storages::redis::Client;
145};
146
147/// Returns CommandControl::Strategy from string
149
150} // namespace redis
151
152USERVER_NAMESPACE_END