acl  3.5.3.0
redis_cluster.hpp
浏览该文件的文档.
1 #pragma once
2 #include "../acl_cpp_define.hpp"
3 #include <vector>
4 #include <list>
5 #include <map>
6 #include "../stdlib/string.hpp"
7 #include "redis_command.hpp"
8 
9 #if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
10 
11 namespace acl
12 {
13 
14 class redis_result;
15 class redis_node;
16 class redis_slot;
17 
18 class ACL_CPP_API redis_cluster : virtual public redis_command
19 {
20 public:
21  /**
22  * see redis_command::redis_command()
23  */
24  redis_cluster(void);
25 
26  /**
27  * see redis_command::redis_command(redis_client*)
28  */
30 
31  /**
32  * see redis_command::redis_command(redis_client_cluster*)
33  */
35 
37  redis_cluster(redis_client_cluster* cluster, size_t max_conns);
38 
40 
41  virtual ~redis_cluster(void);
42 
43  /**
44  * 批量添加可用的哈希槽,最后必须以小于 0 的哈希槽值表示结束
45  * add some hash-slots, the last slot value must be < 0 indicating
46  * the end of the slots array
47  * @param first {int} 第一个哈希槽,该值必须 >= 0 才有效
48  * the first hash-slot which must be >= 0
49  * param slot_list {const int[]} 要添加的哈希槽的列表
50  * the hash-slots array to be added
51  * param n {size_t} the count of the hash-slots list
52  * param slot_list {const std::vector<init>&} 要添加的哈希槽的列表
53  * the hash-slots list to be added
54  * @return {bool} 是否成功
55  * return true if successful
56  */
57  bool cluster_addslots(int first, ...);
58  bool cluster_addslots(const int slot_list[], size_t n);
59  bool cluster_addslots(const std::vector<int>& slot_list);
60 
61  /**
62  * 批量删除哈希槽,最后必须以小于 0 的哈希槽表示结束
63  * remove some hash-slots, the last slot value must be < 0 indicating
64  * the end of the slots array
65  * @param first {int} 第一个哈希槽,该值必须 >= 0 才有效
66  * the first hash-slot which must be >= 0
67  * param slot_list {const int[]} 要删除的哈希槽的列表
68  * the hash-slots array to be removed
69  * param n {size_t} the count of the hash-slots list
70  * param slot_list {const std::vector<init>&} 要删除的哈希槽的列表
71  * the hash-slots array to be removed
72  * @return {bool} 是否成功
73  * return true if successful
74  */
75  bool cluster_delslots(int first, ...);
76  bool cluster_delslots(const int slot_list[], size_t n);
77  bool cluster_delslots(const std::vector<int>& slot_list);
78 
79  /**
80  * 获得某个哈希槽当前所存储对象的键名集合
81  * get keys array stored in one specified hash-slot
82  * @param slot {size_t} 哈希槽值
83  * the specified hash-slot
84  * @param max {size_t} 限制的结果集数量
85  * limit the max results count
86  * @param result {std::list<acl::string>&} 存储结果集
87  * stored the results
88  * @return {int} 查询结果集的个数,-1 表示出错
89  * >= 0 if OK, -1 if error
90  */
91  int cluster_getkeysinslot(size_t slot, size_t max, std::list<string>& result);
92 
93  /**
94  * 在建立 redis 集群时,可以使用此命令让一个 redis 结点是连接别的结点
95  * let one redis node to link to the other redis node
96  * when buiding the redis cluster
97  * @param ip {const char*} 被连接的其它一个 redis 结点的 IP 地址
98  * the other redis node's ip to be linked
99  * @param port {int} 被连接的其它一个 redis 结点的 port 端口
100  * the other redis node's port to be linked
101  * @return {bool} 连接是否成功
102  * if the linking is successful
103  */
104  bool cluster_meet(const char* ip, int port);
105 
106  /**
107  * 重置一个 redis 结点的状态,使之从集群结点中脱离,清除哈希槽-结点的对应关系,
108  * 该方法等同于 reset_soft
109  * reset one redis node's status, escaping from the other nodes
110  * of the redis cluster, and clearing slot-to-nodes mapping;
111  * same as reset_soft
112  * @return {bool} 操作是否成功
113  * if the operation is successful
114  */
115  bool cluster_reset();
116  bool cluster_reset_hard();
117  bool cluster_reset_soft();
118 
119  /**
120  * 设定某个哈希槽在当前 redis 结点上正处于导入状态
121  * set the hash-slot in importing status from the other node
122  * to the current node
123  * @param slot {size_t} 哈希槽值
124  * the hash-slot value
125  * @param src_node {const char*} 该哈希槽的 redis 源结点
126  * the source redis-node of the hash-slot importing from
127  * @return {boo} 设置状态是否成功
128  * if success for setting the slot's status
129  */
130  bool cluster_setslot_importing(size_t slot, const char* src_node);
131 
132  /**
133  * 设定某个哈希槽在当前 redis 结点上正处于迁移状态
134  * set the hash-slot in migrating status to the other node
135  * from the current node
136  * @param slot {size_t} 哈希槽值
137  * the hash-slot value
138  * @param dst_node {const char*} 该哈希槽的 redis 迁移目标结点
139  * the target redis-node of the hash-slot migrating to
140  * @return {boo} 设置状态是否成功
141  * if success for setting the slot's status
142  */
143  bool cluster_setslot_migrating(size_t slot, const char* dst_node);
144 
145  /**
146  * 当导入/迁移哈希槽完成后使用此操作指定该哈希槽为稳定状态
147  * set the hash-slot stable after importing or migrating
148  * @param slot {size_t} 哈希槽值
149  * the hash-slot value
150  * @return {bool} 设置状态是否成功
151  * if success for setting the slot's status
152  */
153  bool cluster_setslot_stable(size_t slot);
154 
155  /**
156  * 设置指定的哈希槽至指定的某个 redis 结点,该指令有较为复杂的行为特征,具体
157  * 请参见官方在线文档
158  * set one hash-slot to one redis node, for more help see online doc
159  * @param slot {size_t} 哈希槽值
160  * the hash-slot to be set
161  * @param node {const char*} 接收该哈希槽的 redis 结点
162  * the redis node to be holding the hash-slot
163  * @return {bool} 操作是否成功
164  * if the operation is successful
165  */
166  bool cluster_setslot_node(size_t slot, const char* node);
167 
168  /**
169  * 获得某个指定 redis 结点报错的数量
170  * get the count of the failure resports by one redis node
171  * @param node {const char*} 指定的某个 redis 结点
172  * @return {int} 结点报错的数量,正常情况下 >= 0,如果返回值 -1 表明操作出错
173  * return the failure count reporting by the specified redis node,
174  * return value >= 0 if successful, or -1 for error happened
175  */
176  int cluster_count_failure_reports(const char* node);
177 
178  /**
179  * 该命令操作只能发送给一个从结点,用来对主结点进行故障转移,使当前的从结点
180  * 做为主结点,需要与它的主结点进行协商,同时需要获得其它主结点的认可
181  * this command can only be sent to one slave node for failover
182  * of the master node, make the current slave to be the master
183  * @return {bool} 操作是否成功
184  * if the operation is successful
185  */
186  bool cluster_failover();
187 
188  /**
189  * 强制性将一个从结点变为主结点,该操作不必与原来的主结点进行协商,但仍需得到
190  * 集群中大多数主结点的同意
191  * force a slave to be the master, not handshake with it's master,
192  * but still need get agreement by majority of the masters in cluster
193  * @return {bool} 操作是否成功
194  * if the operation is successful
195  */
196  bool cluster_failover_force();
197 
198  /**
199  * 强制性将一个从结点变为主结点,该操作不必与原来的主结点和集群中的其它主结点
200  * 进行协商
201  * force a slave to be the master, not handshake with it's master,
202  * and also no need get agreement by the other masters in cluster
203  * @return {bool} 操作是否成功
204  * if the operation is successful
205  */
206  bool cluster_failover_takeover();
207 
208  /**
209  * 获得当前集群的一些概述信息
210  * get running informantion about the redis cluster
211  * @param result {std::map<acl::string, acl::string>&} 存储结果
212  * store the result of this operation
213  * @return {bool} 操作是否成功
214  * if this operation is successful
215  */
216  bool cluster_info(std::map<string, string>& result);
217 
218  /**
219  * 让当前 redis 结点将配置信息保存至磁盘的 nodes.conf 中
220  * let the current node to save the cluster information in nodes.conf
221  * @return {bool} 操作是否成功
222  * if this operation is successful
223  */
224  bool cluster_saveconfig();
225 
226  /**
227  * 获得某个哈希槽中的对象总数量
228  * get all the keys's count in one hash-slot
229  * @param slot {size_t} 指定哈希槽
230  * the specified hash-slot
231  * @return {int} 返回哈希槽中的对象数量,-1 表示出错
232  * return the keys's count in the hash-slot, return -1 if error
233  */
234  int cluster_countkeysinslot(size_t slot);
235 
236  /**
237  * 将指定结点从当前的结点中移除
238  * remove the specified node from the current node
239  * @param node {const char*} 指定的要被移除的结点
240  * the speicied node to be removed
241  * @return {bool} 操作是否成功
242  * if this operation is successful
243  */
244  bool cluster_forget(const char* node);
245 
246  /**
247  * 获得某个键所属的哈希槽
248  * get the hash-slot wich the key belongs to
249  * @param key {const char*} 键值
250  * the key string
251  * @return {int} 哈希槽值,>= 0 表示成功,-1 表示操作失败
252  * return the key's hash-slot, >= 0 if successful, -1 on error
253  */
254  int cluster_keyslot(const char* key);
255 
256  /**
257  * 将指定结点设置为从结点,如果该结点原来为从结点,则也会返回成功
258  * set the specified node to be a slave node
259  * @param node {const char*} 指定结点标识符
260  * the specified node
261  * @return {bool} 操作是否成功
262  * if this operation is successful
263  */
264  bool cluster_replicate(const char* node);
265 
266  bool cluster_set_config_epoch(const char* epoch);
267 
268  /**
269  * 获得所有哈希槽在集群中各个 redis 结点的分布情况
270  * get all nodes with all slots
271  * @return {const std::vector<redis_slot*>*} 返回存储哈希槽信息
272  * 的所有主结点集合,返回 NULL 表示出错
273  * return all the master nodes with all hash-slots in them,
274  * and NULL will be returned if error happened, and the return
275  * value needn't be freed because it can be freed internal
276  */
277  const std::vector<redis_slot*>* cluster_slots();
278 
279  /**
280  * 获得当前集群中所有结点的主结点,主结点的所有从结点可以通过
281  * redis_node::get_slaves 获得
282  * get all the masters of the cluster, and master's slave nodes
283  * can be got by redis_node::get_slaves
284  * @return {const std::map<string, redis_node*>*} 返回 NULL 表示出错
285  * return NULL if error happened, the return value needn't be
286  * freed because it can be freed internal
287  */
288  const std::map<string, redis_node*>* cluster_nodes();
289 
290  /**
291  * 获得指定主结点的所有从结点
292  * get all slave nodes of the specified master node
293  * @return node {const char*} 主结点标识符,返回 NULL 表示出错,该
294  * 返回结果不需要释放,内部自动维护
295  * one of the master node, NULL if error, and the return value
296  * needn't be freed because it can be freed internal
297  */
298  const std::vector<redis_node*>* cluster_slaves(const char* node);
299 
300 private:
301  std::vector<redis_slot*> slots_;
302 
303  redis_slot* get_slot_master(const redis_result* rr);
304  redis_slot* get_slot(const redis_result* rr,
305  size_t slot_max, size_t slot_min);
306  void free_slots();
307 
308 private:
309  std::map<string, redis_node*> masters_;
310 
311  redis_node* get_node(string& line);
312  void add_slot_range(redis_node* node, char* slots);
313  void free_masters();
314  redis_node* get_slave(const std::vector<string>& tokens);
315 
316 private:
317  std::vector<redis_node*> slaves_;
318  void free_slaves();
319 };
320 
321 } // namespace acl
322 
323 #endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
#define ACL_CPP_DEPRECATED
Definition: atomic.hpp:86
#define ACL_CPP_API