Exemplo n.º 1
0
void redis_command::hash_slot(const char* key, size_t len)
{
	// 只有集群模式才需要计算哈希槽值
	if (cluster_ == NULL)
		return;

	int max_slot = cluster_->get_max_slot();
	if (max_slot <= 0)
		return;

	// 如果缓存了哈希槽值,则不必重新计算
	if (slot_ >= 0 && slot_ < max_slot)
		return;

	unsigned short n = acl_hash_crc16(key, len);
	slot_ = (int) (n % max_slot);
}
Exemplo n.º 2
0
Arquivo: main.cpp Projeto: leemayi/acl
int main(int argc, char* argv[])
{
	// initiation the acl library
	acl::acl_cpp_init();
	acl::log::stdout_open(true);

	int  ch;
	size_t replicas = 0;
	bool add_slave = false, just_display = false;
	acl::string addr, cmd, conf, new_addr, node_id, key, passwd;

	while ((ch = getopt(argc, argv, "hs:a:f:N:SI:r:dk:p:")) > 0)
	{
		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;
		case 'r':
			replicas = (size_t) atoi(optarg);
			break;
		case 'd':
			just_display = true;
			break;
		case 'k':
			key = optarg;
			break;
		case 'p':
			passwd = optarg;
			break;
		default:
			break;
		}
	}

	int conn_timeout = 10, rw_timeout = 120;

	if (cmd == "hash_slot")
	{
		if (key.empty())
			printf("usage: %s -a hash_slot -k key\r\n", argv[0]);
		else
		{
			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())
			printf("usage: %s -s ip:port -a nodes\r\n", argv[0]);
		else {
			acl::redis_client client(addr, conn_timeout, rw_timeout);
			client.set_password(passwd);
			acl::redis redis(&client);
			redis_status status(addr, conn_timeout, rw_timeout, passwd);
			status.show_nodes(redis);
		}
	}
	else if (cmd == "slots")
	{
		if (addr.empty())
			printf("usage: %s -a ip:port -a slots\r\n", argv[0]);
		else
		{
			acl::redis_client client(addr, conn_timeout, rw_timeout);
			client.set_password(passwd);
			acl::redis redis(&client);
			redis_status status(addr, conn_timeout, rw_timeout, passwd);
			status.show_slots(redis);
		}
	}
	else if (cmd == "create")
	{
		if (conf.empty())
			printf("usage: %s -a create -f cluster.xml\r\n", argv[0]);
		else
		{
			redis_builder builder(passwd);
			builder.build(conf.c_str(), replicas, just_display);
		}
	}
	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]);
		else
		{
			redis_builder builder(passwd);
			builder.add_node(addr, new_addr, add_slave);
		}
	}
	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]);
		else
		{
			redis_builder builder(passwd);
			builder.del_node(addr, node_id);
		}
	}
	else if (cmd == "node_id")
	{
		if (addr.empty())
			printf("usage: %s -s ip:port -a node_id\r\n", argv[0]);
		else
		{
			node_id.clear();
			if (redis_util::get_node_id(addr, node_id, passwd) == false)
				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());
		}
	}
	else if (cmd == "reshard")
	{
		if (addr.empty())
			printf("usage: %s -s ip:port -a reshard\r\n", argv[0]);
		else
		{
			redis_reshard reshard(addr, passwd);
			reshard.run();
		}
	}
	else
		printf("unknown cmd: %s\r\n", cmd.c_str());

#ifdef WIN32
	printf("enter any key to exit\r\n");
	getchar();
#endif
	return 0;
}