2015-04-19 22:15:30 +08:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "redis_status.h"
|
2016-04-12 20:11:15 +08:00
|
|
|
#include "redis_monitor.h"
|
2015-04-26 00:11:10 +08:00
|
|
|
#include "redis_util.h"
|
2015-04-19 22:15:30 +08:00
|
|
|
#include "redis_builder.h"
|
2015-04-26 00:11:10 +08:00
|
|
|
#include "redis_reshard.h"
|
2016-04-01 09:36:18 +08:00
|
|
|
#include "redis_commands.h"
|
2016-04-12 20:11:15 +08:00
|
|
|
#include "redis_cmdump.h"
|
2015-04-19 22:15:30 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-02 11:55:00 +08:00
|
|
|
#ifdef HAS_READLINE
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
static void test_readline(void)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
char* ptr = readline("> ");
|
|
|
|
if (ptr == NULL || *ptr == 0)
|
|
|
|
break;
|
|
|
|
if (strcasecmp(ptr, "q") == 0)
|
|
|
|
{
|
|
|
|
printf("Bye!\r\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s", ptr);
|
|
|
|
add_history(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-04-19 22:15:30 +08:00
|
|
|
static void usage(const char* procname)
|
|
|
|
{
|
|
|
|
printf("usage: %s -h[help]\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" -s redis_addr[ip:port]\r\n"
|
2016-04-12 20:11:15 +08:00
|
|
|
" -a cmd[nodes|slots|create|add_node|del_node|node_id|reshard|hash_slot|status|dump|run]\r\n"
|
2016-03-13 20:01:41 +08:00
|
|
|
" -p passwd\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" -N new_node[ip:port]\r\n"
|
|
|
|
" -S [add node as slave]\r\n"
|
|
|
|
" -r replicas[default 0]\r\n"
|
|
|
|
" -d [if just display the result for create command]\r\n"
|
|
|
|
" -k key\r\n"
|
2016-04-12 20:11:15 +08:00
|
|
|
" -T dump_to_file\r\n"
|
|
|
|
" -A [dump_all_masters_cmds, default: no]\r\n"
|
|
|
|
" -M [prefer using masters]\r\n"
|
2016-04-15 14:51:45 +08:00
|
|
|
" -f configure_file\r\n"
|
|
|
|
" -F redis_commands_file\r\n",
|
2015-04-19 22:15:30 +08:00
|
|
|
procname);
|
|
|
|
|
2015-05-06 17:16:10 +08:00
|
|
|
printf("\r\nabout command:\r\n"
|
|
|
|
" nodes: display information about nodes of the cluster\r\n"
|
|
|
|
" slots: display information about slots of the cluster\r\n"
|
|
|
|
" create: build a cluster after all nodes started\r\n"
|
|
|
|
" add_node: add a node specified by -N to another by -s\r\n"
|
|
|
|
" del_node: remove a node specified by -N from anothrer by -s\r\n"
|
|
|
|
" node_id: get the address of one node specified by -s\r\n"
|
|
|
|
" reshard: resharding the slots in all the nodes of the cluster\r\n");
|
|
|
|
|
2015-04-19 22:15:30 +08:00
|
|
|
printf("\r\nfor samples:\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a create -f cluster.xml\r\n"
|
2016-03-13 20:01:41 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a create -f cluster.xml p 123456\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a create -f nodes4.xml -r 2\r\n"
|
|
|
|
" %s -s 127.0.0.1:6379 -a nodes\r\n"
|
2016-03-13 20:01:41 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a nodes -p 123456\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a slots\r\n"
|
|
|
|
" %s -s 127.0.0.1:6379 -a del_node -I node_id\r\n"
|
|
|
|
" %s -s 127.0.0.1:6379 -a node_id\r\n"
|
|
|
|
" %s -s 127.0.0.1:6379 -a reshard\r\n"
|
|
|
|
" %s -a hash_slot -k key\r\n"
|
2016-04-12 20:11:15 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a status\r\n"
|
|
|
|
" %s -s 127.0.0.1:6379 -a dump -f dump.txt -A all -M\r\n"
|
2015-05-06 17:16:10 +08:00
|
|
|
" %s -s 127.0.0.1:6379 -a add_node -N 127.0.0.1:6380 -S\r\n",
|
|
|
|
procname, procname, procname, procname, procname, procname,
|
2016-04-12 20:11:15 +08:00
|
|
|
procname, procname, procname, procname, procname, procname,
|
|
|
|
procname);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
// initiation the acl library
|
|
|
|
acl::acl_cpp_init();
|
|
|
|
acl::log::stdout_open(true);
|
|
|
|
|
|
|
|
int ch;
|
2015-04-26 00:11:10 +08:00
|
|
|
size_t replicas = 0;
|
2015-05-06 17:16:10 +08:00
|
|
|
bool add_slave = false, just_display = false;
|
2016-03-13 20:01:41 +08:00
|
|
|
acl::string addr, cmd, conf, new_addr, node_id, key, passwd;
|
2016-04-12 20:11:15 +08:00
|
|
|
bool dump_all = false, prefer_master = false;
|
2016-04-15 14:51:45 +08:00
|
|
|
acl::string filepath("dump.txt"), cmds_file;
|
2015-04-19 22:15:30 +08:00
|
|
|
|
2016-04-15 14:51:45 +08:00
|
|
|
while ((ch = getopt(argc, argv, "hs:a:f:N:SI:r:dk:p:T:AMF:")) > 0)
|
2015-04-19 22:15:30 +08:00
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 'h':
|
|
|
|
usage(argv[0]);
|
|
|
|
return 0;
|
|
|
|
case 's':
|
|
|
|
addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
cmd = optarg;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
conf = optarg;
|
|
|
|
break;
|
|
|
|
case 'N':
|
|
|
|
new_addr = optarg;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
add_slave = true;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
node_id = optarg;
|
|
|
|
break;
|
2015-04-26 00:11:10 +08:00
|
|
|
case 'r':
|
|
|
|
replicas = (size_t) atoi(optarg);
|
|
|
|
break;
|
2015-05-06 17:16:10 +08:00
|
|
|
case 'd':
|
|
|
|
just_display = true;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
key = optarg;
|
|
|
|
break;
|
2016-03-13 20:01:41 +08:00
|
|
|
case 'p':
|
|
|
|
passwd = optarg;
|
|
|
|
break;
|
2016-04-12 20:11:15 +08:00
|
|
|
case 'T':
|
|
|
|
filepath = optarg;
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
dump_all = true;
|
|
|
|
break;
|
|
|
|
case 'M':
|
|
|
|
prefer_master = true;
|
|
|
|
break;
|
2016-04-15 14:51:45 +08:00
|
|
|
case 'F':
|
|
|
|
cmds_file = optarg;
|
|
|
|
break;
|
2015-04-19 22:15:30 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int conn_timeout = 10, rw_timeout = 120;
|
|
|
|
|
2015-05-12 20:59:51 +08:00
|
|
|
if (cmd == "hash_slot")
|
2015-04-19 22:15:30 +08:00
|
|
|
{
|
2015-05-12 20:59:51 +08:00
|
|
|
if (key.empty())
|
|
|
|
printf("usage: %s -a hash_slot -k key\r\n", argv[0]);
|
|
|
|
else
|
2015-04-19 22:15:30 +08:00
|
|
|
{
|
2015-05-12 20:59:51 +08:00
|
|
|
size_t max_slot = 16384;
|
|
|
|
unsigned short n = acl_hash_crc16(key.c_str(), key.length());
|
|
|
|
unsigned short slot = n % max_slot;
|
|
|
|
printf("key: %s, slot: %d\r\n", key.c_str(), (int) slot);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "nodes")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
2015-04-19 22:15:30 +08:00
|
|
|
printf("usage: %s -s ip:port -a nodes\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else {
|
|
|
|
acl::redis_client client(addr, conn_timeout, rw_timeout);
|
2016-03-13 20:01:41 +08:00
|
|
|
client.set_password(passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
acl::redis redis(&client);
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_status status(addr, conn_timeout, rw_timeout, passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
status.show_nodes(redis);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "slots")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
|
|
|
printf("usage: %s -a ip:port -a slots\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
acl::redis_client client(addr, conn_timeout, rw_timeout);
|
2016-03-13 20:01:41 +08:00
|
|
|
client.set_password(passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
acl::redis redis(&client);
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_status status(addr, conn_timeout, rw_timeout, passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
status.show_slots(redis);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "create")
|
|
|
|
{
|
|
|
|
if (conf.empty())
|
|
|
|
printf("usage: %s -a create -f cluster.xml\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else
|
|
|
|
{
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_builder builder(passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
builder.build(conf.c_str(), replicas, just_display);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "add_node")
|
|
|
|
{
|
|
|
|
if (addr.empty() || new_addr.empty())
|
|
|
|
printf("usage: %s -s ip:port -a add_node -N ip:port -S\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else
|
|
|
|
{
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_builder builder(passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
builder.add_node(addr, new_addr, add_slave);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "del_node")
|
|
|
|
{
|
|
|
|
if (addr.empty() || node_id.empty())
|
|
|
|
printf("usage: %s -s ip:port -a del_node -I nod_id\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else
|
|
|
|
{
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_builder builder(passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
builder.del_node(addr, node_id);
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "node_id")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
|
|
|
printf("usage: %s -s ip:port -a node_id\r\n", argv[0]);
|
|
|
|
else
|
2015-05-12 20:59:51 +08:00
|
|
|
{
|
|
|
|
node_id.clear();
|
2016-03-13 20:01:41 +08:00
|
|
|
if (redis_util::get_node_id(addr, node_id, passwd) == false)
|
2015-05-12 20:59:51 +08:00
|
|
|
printf("can't get node id, addr: %s\r\n",
|
|
|
|
addr.c_str());
|
|
|
|
else
|
|
|
|
printf("addr: %s, node_id: %s\r\n",
|
|
|
|
addr.c_str(), node_id.c_str());
|
|
|
|
}
|
2015-04-19 22:15:30 +08:00
|
|
|
}
|
2015-04-26 00:11:10 +08:00
|
|
|
else if (cmd == "reshard")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
|
|
|
printf("usage: %s -s ip:port -a reshard\r\n", argv[0]);
|
2015-05-12 20:59:51 +08:00
|
|
|
else
|
2015-05-06 17:16:10 +08:00
|
|
|
{
|
2016-03-13 20:01:41 +08:00
|
|
|
redis_reshard reshard(addr, passwd);
|
2015-05-12 20:59:51 +08:00
|
|
|
reshard.run();
|
2015-05-06 17:16:10 +08:00
|
|
|
}
|
|
|
|
}
|
2016-04-12 20:11:15 +08:00
|
|
|
else if (cmd == "status")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
|
|
|
printf("usage: %s -s ip:port -a status\r\n", argv[0]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
redis_monitor monitor(addr, conn_timeout, rw_timeout,
|
|
|
|
passwd, prefer_master);
|
|
|
|
monitor.status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (cmd == "dump")
|
|
|
|
{
|
|
|
|
if (addr.empty())
|
|
|
|
printf("usage: %s -s ip:port -a monitor -f dump.txt\r\n", argv[0]);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
redis_cmdump dump(addr, conn_timeout, rw_timeout,
|
|
|
|
passwd, prefer_master);
|
|
|
|
dump.saveto(filepath, dump_all);
|
|
|
|
}
|
|
|
|
}
|
2016-04-02 11:55:00 +08:00
|
|
|
else if (cmd == "run" || cmd.empty())
|
2016-04-01 09:36:18 +08:00
|
|
|
{
|
2016-04-12 20:11:15 +08:00
|
|
|
redis_commands cmds(addr, passwd, conn_timeout,
|
2016-04-15 14:51:45 +08:00
|
|
|
rw_timeout, prefer_master, cmds_file);
|
2016-04-02 11:55:00 +08:00
|
|
|
cmds.run();
|
2016-04-01 09:36:18 +08:00
|
|
|
}
|
2016-04-02 11:55:00 +08:00
|
|
|
#ifdef HAS_READLINE
|
|
|
|
else if (cmd == "readline")
|
|
|
|
test_readline();
|
|
|
|
#endif
|
2015-04-19 22:15:30 +08:00
|
|
|
else
|
|
|
|
printf("unknown cmd: %s\r\n", cmd.c_str());
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
printf("enter any key to exit\r\n");
|
|
|
|
getchar();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|