2019-07-27 22:44:32 +08:00
|
|
|
|
#pragma once
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../acl_cpp_define.hpp"
|
2015-04-02 22:19:57 +08:00
|
|
|
|
#include <vector>
|
2015-04-07 23:31:31 +08:00
|
|
|
|
#include <utility>
|
2017-06-02 14:47:24 +08:00
|
|
|
|
#include "../stdlib/string.hpp"
|
2019-05-09 13:57:51 +08:00
|
|
|
|
#include "../stdlib/noncopyable.hpp"
|
2015-04-02 22:19:57 +08:00
|
|
|
|
|
2019-05-19 16:54:20 +08:00
|
|
|
|
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
|
2019-05-18 21:19:21 +08:00
|
|
|
|
|
2015-04-02 22:19:57 +08:00
|
|
|
|
namespace acl
|
|
|
|
|
{
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 该类主要用于 redis_cluster 命令类获取有关集群 redis 结点信息
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* this class is mainly used for redis_cluster command class to
|
|
|
|
|
* get some information about the nodes in redis cluster
|
|
|
|
|
*/
|
2019-05-09 13:57:51 +08:00
|
|
|
|
class ACL_CPP_API redis_node : public noncopyable
|
2015-04-02 22:19:57 +08:00
|
|
|
|
{
|
|
|
|
|
public:
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当使用此构造函数实例化类对象时,需要调用 set_id 和 set_addr 方法设置
|
|
|
|
|
* 该 redis 结点的唯一标识符及服务监听地址,同时还可调用其它的 set_xxx 设置方法
|
2015-04-19 22:15:30 +08:00
|
|
|
|
*/
|
2016-01-17 13:35:12 +08:00
|
|
|
|
redis_node(void);
|
|
|
|
|
~redis_node(void);
|
2015-04-02 22:19:57 +08:00
|
|
|
|
|
2015-04-19 22:15:30 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 除了在构造函数中的参数中传入该结点的 ID 标识符外,还可以通过此函数设置
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set the node's ID
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param id {const char*} 集群中 redis 结点的唯一标识符
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* the unique ID for one redis node in the reids cluster
|
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_id(const char* id);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 除了在构造函数中的参数中传入该结点的地址外,还可以通过此函数设置
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set the node's listening addr
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param addr {const char*} 集群中 redis 结点的服务地址,格式:ip:port
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* the listening addr of one redis node in the reids cluster
|
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_addr(const char* addr);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置当前结点的类型
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set the current node's type
|
|
|
|
|
* @param type {const char*}
|
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_type(const char* type);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置当前结点是否为当前的连接对象
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set if the current node is belonging to the current connection
|
|
|
|
|
* @param yesno {bool}
|
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_myself(bool yesno);
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为从结点时,设置当前结点的主结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* setting current slave node's master node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param master {const redis_node*} 主结点对象
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the redis master node of the current slave in cluster
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_master(const redis_node* master);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置当前结点正处于握手阶段
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set the current node being in handshaking status
|
|
|
|
|
* @param yesno {bool}
|
|
|
|
|
* @return {redis_node&}
|
2015-04-08 23:17:16 +08:00
|
|
|
|
*/
|
2015-04-19 22:15:30 +08:00
|
|
|
|
redis_node& set_handshaking(bool yesno);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 设置当前结点处于连线状态
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* set the node been connected in the cluster
|
|
|
|
|
* @param yesno {bool}
|
|
|
|
|
* @return {redis_node&}
|
|
|
|
|
*/
|
|
|
|
|
redis_node& set_connected(bool yesno);
|
2015-04-08 23:17:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为从结点时,设置当前结点的主结点标识符
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* setting current node's master node when the node is slave node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param id {const char*} 主结点唯一标识符
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the unique ID of the master node
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* @return {redis_node&}
|
2015-04-08 23:17:16 +08:00
|
|
|
|
*/
|
2015-04-19 22:15:30 +08:00
|
|
|
|
redis_node& set_master_id(const char* id);
|
2015-04-08 23:17:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,添加一个从结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* add one slave node to the current node if it's one master node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @return {bool} 添加是否成功,当从结点已经存在于当前主结点时则返回 false
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* false will be returned when the slave to be added is already
|
|
|
|
|
* existing in the current master node
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
bool add_slave(redis_node* slave);
|
2015-04-08 23:17:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,根据结点唯一标识符删除一个从结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* when the current node is a master node, this function will
|
|
|
|
|
* remove one slave node by the unique ID
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param id {const char*} redis 结点唯一标识符
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the unique ID of the redis node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @return {const redis_node*} 返回被删除的从结点,如果不存在则返回 NULL
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the slave node according to the ID will be returned, and if
|
|
|
|
|
* not exists NULL will be returned
|
|
|
|
|
*/
|
|
|
|
|
redis_node* remove_slave(const char* id);
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,清空本结点的所有从结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* clear all the slave nodes in the current master node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param free_all {bool} 是否需要同时释放这些从结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* if freeing the all slave nodes memory meanwhile
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
void clear_slaves(bool free_all = false);
|
2015-04-02 22:19:57 +08:00
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,添加哈希槽范围
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* add hash-slots range to the master node
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param min {size_t} 哈希槽范围的起始值
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the begin hash-slot of the slots range
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* @param max {size_t} 哈希槽范围的结束值
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* the end hash-slot of the slots range
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
void add_slot_range(size_t min, size_t max);
|
2015-04-02 22:19:57 +08:00
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,则获得主结点的哈希槽范围,当为从结点时则获得其对应的
|
|
|
|
|
* 主结点的哈希槽范围
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* @return {const std::vector<std::pair<size_t, size_t> >&}
|
|
|
|
|
*/
|
|
|
|
|
const std::vector<std::pair<size_t, size_t> >& get_slots() const;
|
|
|
|
|
|
2015-04-19 22:15:30 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得当前结点的类型
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* get the node's type
|
|
|
|
|
* @return {const char*}
|
|
|
|
|
*/
|
|
|
|
|
const char* get_type() const
|
|
|
|
|
{
|
|
|
|
|
return type_.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断当前结点是否为当前的连接对象结点
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* check if the node belongs to the current connection
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool is_myself() const
|
|
|
|
|
{
|
|
|
|
|
return myself_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断当前结点是否正处于握手阶段
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* check if the node is in handshaking status
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool is_handshaking() const
|
|
|
|
|
{
|
|
|
|
|
return handshaking_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断当前结点是否已经处于连线状态
|
2015-04-19 22:15:30 +08:00
|
|
|
|
* check if the node is connected in the cluster
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
|
|
|
|
bool is_connected() const
|
|
|
|
|
{
|
|
|
|
|
return connected_;
|
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为从结点时,获得该从结点的主结点对象
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* get the current slave's master node
|
|
|
|
|
* @return {const redis_node*}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const redis_node* get_master() const
|
2015-04-02 22:19:57 +08:00
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
|
return master_;
|
2015-04-02 22:19:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为从结点时,获得该从结点对应的主结点的 ID 标识
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* when the current node is slave, getting its master's ID
|
|
|
|
|
* @return {const char*}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const char* get_master_id() const
|
2015-04-02 22:19:57 +08:00
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
|
return master_id_.c_str();
|
2015-04-02 22:19:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 当本结点为主结点时,获得该主结点的所有从结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* getting all the slaves of the master
|
|
|
|
|
* @return {const std::vector<redis_node*>*}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const std::vector<redis_node*>* get_slaves() const
|
2015-04-02 22:19:57 +08:00
|
|
|
|
{
|
2015-04-19 22:15:30 +08:00
|
|
|
|
return &slaves_;
|
2015-04-02 22:19:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 判断当前结点是否为集群中的一个主结点
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* check if the current node is a master in the redis cluster
|
|
|
|
|
* @return {bool}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
bool is_master() const
|
2015-04-02 22:19:57 +08:00
|
|
|
|
{
|
2015-04-07 23:31:31 +08:00
|
|
|
|
return master_ == this;
|
2015-04-02 22:19:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得当前结点的 ID 标识
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* get the unique ID of the current node, set in constructor
|
|
|
|
|
* @return {const char*}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const char* get_id() const
|
|
|
|
|
{
|
|
|
|
|
return id_.c_str();
|
|
|
|
|
}
|
2015-04-02 22:19:57 +08:00
|
|
|
|
|
2015-04-08 23:17:16 +08:00
|
|
|
|
/**
|
2019-07-27 22:44:32 +08:00
|
|
|
|
* 获得当前结点的监听地址
|
2015-04-08 23:17:16 +08:00
|
|
|
|
* get the listening addr of the current node, set in constructor
|
|
|
|
|
* @reutrn {const char*}
|
|
|
|
|
*/
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const char* get_addr() const
|
|
|
|
|
{
|
|
|
|
|
return addr_.c_str();
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-01 10:48:41 +08:00
|
|
|
|
/**
|
2018-01-01 11:05:05 +08:00
|
|
|
|
* result of CLUSTER NODES for redis.4.x.x:
|
2018-01-01 10:48:41 +08:00
|
|
|
|
* d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 127.0.0.1:16385@116385 master - 0 1428410625374 73 connected 5461-10922
|
2018-01-01 11:05:05 +08:00
|
|
|
|
* @return return 127.0.0.1:16385@116385 for redis.4.x.x
|
2018-01-01 10:48:41 +08:00
|
|
|
|
*/
|
2018-01-01 11:05:05 +08:00
|
|
|
|
const char* get_addr_info() const
|
2018-01-01 10:48:41 +08:00
|
|
|
|
{
|
2018-01-01 11:05:05 +08:00
|
|
|
|
return addr_info_.c_str();
|
2018-01-01 10:48:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-04-07 23:31:31 +08:00
|
|
|
|
private:
|
|
|
|
|
string id_;
|
|
|
|
|
string addr_;
|
2018-01-01 11:05:05 +08:00
|
|
|
|
string addr_info_;
|
2015-04-19 22:15:30 +08:00
|
|
|
|
string type_;
|
|
|
|
|
bool myself_;
|
|
|
|
|
bool handshaking_;
|
|
|
|
|
bool connected_;
|
2015-04-07 23:31:31 +08:00
|
|
|
|
const redis_node* master_;
|
|
|
|
|
string master_id_;
|
2015-04-02 22:19:57 +08:00
|
|
|
|
std::vector<redis_node*> slaves_;
|
2015-04-07 23:31:31 +08:00
|
|
|
|
std::vector<std::pair<size_t, size_t> > slots_;
|
2015-04-02 22:19:57 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace acl
|
2019-05-18 21:19:21 +08:00
|
|
|
|
|
2019-05-19 16:54:20 +08:00
|
|
|
|
#endif // !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
|