mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-02 20:08:21 +08:00
modify redis_cluster sample
This commit is contained in:
parent
9b50d1dc50
commit
358a73d07e
@ -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
|
||||
{
|
||||
|
@ -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);
|
||||
|
||||
|
@ -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*));
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user