2019-07-28 10:31:56 +08:00
|
|
|
#pragma once
|
2015-04-19 22:15:30 +08:00
|
|
|
#include <vector>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
class redis_builder
|
|
|
|
{
|
|
|
|
public:
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_builder(const char* passwd = NULL, int meet_wait = 100);
|
2015-04-19 22:15:30 +08:00
|
|
|
~redis_builder(void);
|
|
|
|
|
2015-05-06 17:16:10 +08:00
|
|
|
bool build(const char* conf, size_t replicas, bool just_display);
|
2015-04-19 22:15:30 +08:00
|
|
|
|
|
|
|
bool add_node(const char* addr, const char* new_node_addr, bool slave);
|
|
|
|
bool del_node(const char* addr, const char* node_id);
|
|
|
|
|
|
|
|
private:
|
2016-03-13 20:01:41 +08:00
|
|
|
acl::string passwd_;
|
2015-04-19 22:15:30 +08:00
|
|
|
int meet_wait_;
|
|
|
|
std::vector<acl::redis_node*> masters_;
|
|
|
|
time_t last_check_;
|
|
|
|
|
|
|
|
// load the cluster.xml configure and create redis nodes for creating
|
2015-04-26 00:11:10 +08:00
|
|
|
bool load(const char* conf, size_t replicas);
|
|
|
|
|
|
|
|
// parse xml and create cluster nodes before connecting them
|
|
|
|
bool create_cluster(acl::xml& xml);
|
|
|
|
|
|
|
|
// parse xml and create cluster nodes before connecting them,
|
|
|
|
// don't use the master/slave relation from xml configure,
|
|
|
|
// use the replicas param to build the cluster automatic
|
|
|
|
bool create_cluster(acl::xml& xml, size_t replicas);
|
|
|
|
|
|
|
|
// peek one master node and remove it from nodes
|
|
|
|
acl::redis_node* peek_master(std::vector<acl::redis_node*>& nodes,
|
|
|
|
std::map<acl::string, size_t>& addrs);
|
|
|
|
|
|
|
|
// peek one slave node and remove it from nodes
|
|
|
|
acl::redis_node* peek_slave(const char* master_addr,
|
|
|
|
std::vector<acl::redis_node*>& nodes,
|
|
|
|
std::map<acl::string, size_t>& addrs);
|
2015-04-19 22:15:30 +08:00
|
|
|
|
|
|
|
// create one master node according to one xml node of configure
|
|
|
|
acl::redis_node* create_master(acl::xml_node& node);
|
|
|
|
|
2015-04-26 00:11:10 +08:00
|
|
|
// create one node
|
|
|
|
acl::redis_node* create_node(acl::xml_node& node);
|
2015-04-19 22:15:30 +08:00
|
|
|
|
|
|
|
// begin build the redis cluster, connect all redis nodes
|
2019-07-08 11:49:19 +08:00
|
|
|
bool build_cluster(void);
|
2015-04-19 22:15:30 +08:00
|
|
|
|
|
|
|
// allocate slots for every master, and let its slaves connect
|
|
|
|
// to their master node
|
|
|
|
bool build_master(acl::redis_node& master);
|
|
|
|
|
|
|
|
// let one node meet to another one
|
|
|
|
bool cluster_meet(acl::redis& redis, const acl::redis_node& node);
|
|
|
|
|
|
|
|
// check the MEET status between the current node and the other one
|
|
|
|
bool cluster_meeting(acl::redis& redis, const char* addr);
|
|
|
|
|
|
|
|
// add slots to one master node
|
|
|
|
bool master_set_slots(acl::redis& redis, acl::redis_node& master);
|
|
|
|
|
|
|
|
// add one slave to its master node, let the slave MEET its master,
|
|
|
|
// and make the slave REPLICATE its master.
|
|
|
|
bool add_slave(const acl::redis_node& master,
|
|
|
|
const acl::redis_node& slave);
|
|
|
|
|
|
|
|
// check if the given addr was in the node's slave
|
|
|
|
acl::redis_node* find_slave(const acl::redis_node* node,
|
|
|
|
const char* addr, size_t& nslaves);
|
|
|
|
};
|