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

123 lines
2.6 KiB
C++
Raw Normal View History

2015-03-08 21:39:35 +08:00
#include "stdafx.h"
static void print_slaves(const acl::redis_node* master)
2015-03-08 21:39:35 +08:00
{
const std::vector<acl::redis_node*>& slaves = master->get_slaves();
std::vector<acl::redis_node*>::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_range_from(),
(int) (*cit)->get_slot_range_to());
2015-03-08 21:39:35 +08:00
}
}
static bool test_slots(acl::redis_cluster& option)
2015-03-08 21:39:35 +08:00
{
const std::vector<acl::redis_node*>* nodes = option.slots();
if (nodes == NULL)
2015-03-08 21:39:35 +08:00
return false;
std::vector<acl::redis_node*>::const_iterator cit;
2015-03-08 21:39:35 +08:00
for (cit = nodes->begin(); cit != nodes->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_range_from(),
(int) (*cit)->get_slot_range_to());
print_slaves(*cit);
2015-03-08 21:39:35 +08:00
}
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(10, 10, 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"
"-a cmd[slots|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;
2015-03-08 21:39:35 +08:00
while ((ch = getopt(argc, argv, "hs:n:C:T:a:")) > 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;
default:
break;
}
}
acl::acl_cpp_init();
acl::redis_client client(addr.c_str(), conn_timeout, rw_timeout);
acl::redis_cluster option(&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(option);
else if (cmd == "preset")
ret = preset_all(addr.c_str());
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;
}