acl/lib_acl_cpp/samples/redis/redis_cluster/redis_cluster.cpp

203 lines
4.9 KiB
C++
Raw Normal View History

2015-03-08 21:39:35 +08:00
#include "stdafx.h"
static void print_slaves_slots(const acl::redis_slot* slot)
2015-03-08 21:39:35 +08:00
{
const std::vector<acl::redis_slot*>& slaves = slot->get_slaves();
std::vector<acl::redis_slot*>::const_iterator cit;
for (cit = slaves.begin(); cit != slaves.end(); ++cit)
2015-03-08 21:39:35 +08:00
{
printf("slave: ip: %s, port: %d, slot_min: %d, slot_max: %d\r\n",
(*cit)->get_ip(), (*cit)->get_port(),
(int) (*cit)->get_slot_min(),
(int) (*cit)->get_slot_max());
2015-03-08 21:39:35 +08:00
}
}
static bool test_slots(acl::redis_cluster& redis)
2015-03-08 21:39:35 +08:00
{
const std::vector<acl::redis_slot*>* slots = redis.cluster_slots();
if (slots == NULL)
2015-03-08 21:39:35 +08:00
return false;
std::vector<acl::redis_slot*>::const_iterator cit;
2015-03-08 21:39:35 +08:00
for (cit = slots->begin(); cit != slots->end(); ++cit)
2015-03-08 21:39:35 +08:00
{
printf("=========================================\r\n");
printf("master: ip: %s, port: %d, slot_min: %d, slot_max: %d\r\n",
(*cit)->get_ip(), (*cit)->get_port(),
(int) (*cit)->get_slot_min(),
(int) (*cit)->get_slot_max());
print_slaves_slots(*cit);
2015-03-08 21:39:35 +08:00
}
return true;
}
static void print_master_slots(const acl::redis_node* master)
{
const std::vector<std::pair<size_t, size_t> >& slots = master->get_slots();
std::vector<std::pair<size_t, size_t> >::const_iterator cit;
for (cit = slots.begin(); cit != slots.end(); ++cit)
printf("slots range: %d-%d\r\n",
(int) (*cit).first, (int) (*cit).second);
}
static void print_slave_nodes(const std::vector<acl::redis_node*>& slaves)
{
std::vector<acl::redis_node*>::const_iterator cit;
for (cit = slaves.begin(); cit != slaves.end(); ++cit)
{
printf("slave, id: %s, addr: %s, master_id: %s\r\n",
(*cit)->get_id(), (*cit)->get_addr(),
(*cit)->get_master_id());
}
}
static bool test_nodes(acl::redis_cluster& redis)
{
const std::map<acl::string, acl::redis_node*>* masters = redis.cluster_nodes();
if (masters == NULL)
return false;
std::map<acl::string, acl::redis_node*>::const_iterator cit;
for (cit = masters->begin(); cit != masters->end(); ++cit)
{
printf("==========================================\r\n");
printf("master, id: %s, addr: %s\r\n",
cit->first.c_str(), cit->second->get_addr());
print_master_slots(cit->second);
const std::vector<acl::redis_node*>* slaves = cit->second->get_slaves();
print_slave_nodes(*slaves);
}
return true;
}
static bool test_slaves(acl::redis_cluster& redis, const char* node)
{
const std::vector<acl::redis_node*>* slaves = redis.cluster_slaves(node);
if (slaves == NULL)
return false;
print_slave_nodes(*slaves);
return true;
}
static bool test_info(acl::redis_cluster& redis)
{
std::map<acl::string, acl::string> result;
if (redis.cluster_info(result) == false)
return false;
std::map<acl::string, acl::string>::const_iterator cit;
for (cit = result.begin(); cit != result.end(); ++cit)
printf("%s=%s\r\n", cit->first.c_str(), cit->second.c_str());
return true;
}
static bool preset_all(const char* addr)
2015-03-08 21:39:35 +08:00
{
int max_slot = 16384;
acl::redis_client_cluster cluster(max_slot);
cluster.set_all_slot(addr, 100);
2015-03-08 21:39:35 +08:00
for (int i = 0; i < max_slot; i++)
2015-03-08 21:39:35 +08:00
{
acl::redis_client_pool* pool = cluster.peek_slot(i);
if (pool == NULL)
2015-03-08 21:39:35 +08:00
{
printf("null slot: %d\r\n", i);
return false;
2015-03-08 21:39:35 +08:00
}
}
printf("preset all slots ok\r\n");
return true;
}
2015-03-08 21:39:35 +08:00
static void usage(const char* procname)
{
printf("usage: %s -h[help]\r\n"
"-s redis_addr[127.0.0.1:6379]\r\n"
2015-03-08 21:39:35 +08:00
"-C connect_timeout[default: 10]\r\n"
"-T rw_timeout[default: 10]\r\n"
"-i node_id[for action: slaves]\r\n"
"-a cmd[slots|nodes|slaves|info|preset]\r\n",
2015-03-08 21:39:35 +08:00
procname);
}
int main(int argc, char* argv[])
{
int ch, conn_timeout = 10, rw_timeout = 10;
acl::string addr("127.0.0.1:6379"), cmd, node;
2015-03-08 21:39:35 +08:00
while ((ch = getopt(argc, argv, "hs:n:C:T:a:i:")) > 0)
2015-03-08 21:39:35 +08:00
{
switch (ch)
{
case 'h':
usage(argv[0]);
return 0;
case 's':
addr = optarg;
2015-03-08 21:39:35 +08:00
break;
case 'C':
conn_timeout = atoi(optarg);
break;
case 'T':
2015-03-08 21:39:35 +08:00
rw_timeout = atoi(optarg);
break;
case 'a':
cmd = optarg;
break;
case 'i':
node = optarg;
break;
2015-03-08 21:39:35 +08:00
default:
break;
}
}
acl::acl_cpp_init();
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
acl::redis_cluster redis(&client);
2015-03-08 21:39:35 +08:00
bool ret;
2015-03-08 21:39:35 +08:00
if (cmd == "slots")
ret = test_slots(redis);
else if (cmd == "preset")
ret = preset_all(addr.c_str());
else if (cmd == "nodes")
ret = test_nodes(redis);
else if (cmd == "slaves")
{
if (node.empty())
{
printf("usage: %s -a slaves -i node\r\n", argv[0]);
return 1;
}
ret = test_slaves(redis, node.c_str());
}
else if (cmd == "info")
ret = test_info(redis);
else
2015-03-08 21:39:35 +08:00
{
ret = false;
printf("unknown cmd: %s\r\n", cmd.c_str());
2015-03-08 21:39:35 +08:00
}
if (ret == true)
printf("test OK!\r\n");
else
printf("test failed!\r\n");
2015-03-08 21:39:35 +08:00
#ifdef WIN32
printf("enter any key to exit\r\n");
getchar();
#endif
return 0;
}