modify redis_cluster sample

This commit is contained in:
zsx 2015-03-02 09:56:01 +08:00
parent 9b50d1dc50
commit 358a73d07e
4 changed files with 29 additions and 12 deletions

View File

@ -67,17 +67,17 @@ public:
}
/**
* >= 2 () 1
* >= 2 () 100
* redis 线
* ( redis.conf cluster-node-timeout )
*
* @param n {int} () 1
* @param n {int} () 100
*/
void set_redirect_sleep(int n);
/**
* set_redirect_sleep
* @return {int}
* @return {int}
*/
int get_redirect_sleep() const
{

View File

@ -225,6 +225,8 @@ static void usage(const char* procname)
"-C connect_timeout[default: 10]\r\n"
"-I rw_timeout[default: 10]\r\n"
"-c max_threads[default: 10]\r\n"
"-w wait_for_cluster_resume[default: 500 ms]\r\n"
"-r retry_for_cluster_resnum[default: 10]\r\n"
"-a cmd[set|get|expire|ttl|exists|type|del]\r\n",
procname);
}
@ -232,10 +234,10 @@ static void usage(const char* procname)
int main(int argc, char* argv[])
{
int ch, n = 1, conn_timeout = 10, rw_timeout = 10;
int max_threads = 10;
int max_threads = 10, nsleep = 500, nretry = 10;
acl::string addrs("127.0.0.1:6379"), cmd;
while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:")) > 0)
while ((ch = getopt(argc, argv, "hs:n:C:I:c:a:w:r:")) > 0)
{
switch (ch)
{
@ -260,6 +262,12 @@ int main(int argc, char* argv[])
case 'a':
cmd = optarg;
break;
case 'w':
nsleep = atoi(optarg);
break;
case 'r':
nretry = atoi(optarg);
break;
default:
break;
}
@ -275,10 +283,10 @@ int main(int argc, char* argv[])
cluster.set_retry_inter(0);
// 设置重定向的最大阀值,若重定向次数超过此阀值则报错
cluster.set_redirect_max(20);
cluster.set_redirect_max(nretry);
// 当重定向次数 >= 2 时每次再重定向此函数设置休息的时间(秒)
cluster.set_redirect_sleep(1);
// 当重定向次数 >= 2 时每次再重定向此函数设置休息的时间(秒)
cluster.set_redirect_sleep(nsleep);
cluster.init(NULL, addrs.c_str(), max_threads);

View File

@ -11,7 +11,7 @@ redis_cluster::redis_cluster(int conn_timeout, int rw_timeout,
, rw_timeout_(rw_timeout)
, max_slot_(max_slot)
, redirect_max_(15)
, redirect_sleep_(1)
, redirect_sleep_(100)
{
slot_addrs_ = (const char**) acl_mycalloc(max_slot_, sizeof(char*));
}

View File

@ -22,7 +22,7 @@ redis_command::redis_command()
, used_(0)
, slot_(-1)
, redirect_max_(15)
, redirect_sleep_(1)
, redirect_sleep_(100)
, slice_req_(false)
, request_buf_(NULL)
, request_obj_(NULL)
@ -449,7 +449,7 @@ const redis_result* redis_command::run(redis_cluster* cluster, size_t nchild)
{
logger("redirect %d, curr %s, waiting %s ...",
n, ptr, addr);
sleep(redirect_sleep_);
acl_doze(redirect_sleep_);
}
last_moved = true;
@ -473,6 +473,15 @@ const redis_result* redis_command::run(redis_cluster* cluster, size_t nchild)
return result_;
}
ptr = conn->get_pool()->get_addr();
if (n >= 2 && redirect_sleep_ > 0
&& strcmp(ptr, addr) != 0)
{
logger("redirect %d, curr %s, waiting %s ...",
n, ptr, addr);
acl_doze(redirect_sleep_);
}
last_moved = false;
reset(true);
}
@ -486,7 +495,7 @@ const redis_result* redis_command::run(redis_cluster* cluster, size_t nchild)
{
logger("redirect %d, slot %d, waiting %s ...",
n, slot_, ptr);
sleep(redirect_sleep_);
acl_doze(redirect_sleep_);
}
conn = peek_conn(cluster, -1);