提交 64c8de86 编写于 作者: A antirez

Sentinel: simplify and refactor slave reconfig code.

上级 782f9cac
...@@ -1433,6 +1433,19 @@ void sentinelReconnectInstance(sentinelRedisInstance *ri) { ...@@ -1433,6 +1433,19 @@ void sentinelReconnectInstance(sentinelRedisInstance *ri) {
/* ======================== Redis instances pinging ======================== */ /* ======================== Redis instances pinging ======================== */
/* Return true if master looks "sane", that is:
* 1) It is actually a master in the current configuration.
* 2) It reports itself as a master.
* 3) It is not SDOWN or ODOWN.
* 4) We obtained last INFO no more than two times the INFO period of time ago. */
int sentinelMasterLooksSane(sentinelRedisInstance *master) {
return
master->flags & SRI_MASTER &&
master->role_reported == SRI_MASTER &&
(master->flags & (SRI_S_DOWN|SRI_O_DOWN)) == 0 &&
(mstime() - master->info_refresh) < SENTINEL_INFO_PERIOD*2;
}
/* Process the INFO output from masters. */ /* Process the INFO output from masters. */
void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) { void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) {
sds *lines; sds *lines;
...@@ -1596,22 +1609,13 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) { ...@@ -1596,22 +1609,13 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) {
"start",ri->master->addr,ri->addr); "start",ri->master->addr,ri->addr);
} else if (!sentinel.tilt) { } else if (!sentinel.tilt) {
/* A slave turned into a master. We want to force our view and /* A slave turned into a master. We want to force our view and
* reconfigure as slave, but make sure to wait some time before * reconfigure as slave. Wait some time after the change before
* doing this in order to make sure to receive an updated * going forward, to receive new configs if any. */
* configuration via Pub/Sub if any. */
mstime_t wait_time = SENTINEL_PUBLISH_PERIOD*4; mstime_t wait_time = SENTINEL_PUBLISH_PERIOD*4;
if (!sentinelRedisInstanceNoDownFor(ri,wait_time) || if (sentinelMasterLooksSane(ri->master) &&
mstime() - ri->role_reported_time < wait_time || sentinelRedisInstanceNoDownFor(ri,wait_time) &&
mstime() - sentinel.tilt_start_time < wait_time) mstime() - ri->role_reported_time > wait_time)
return;
/* Make sure the master is sane before reconfiguring this instance
* into a slave. */
if (ri->master->flags & SRI_MASTER &&
ri->master->role_reported == SRI_MASTER &&
(ri->master->flags & (SRI_S_DOWN|SRI_O_DOWN)) == 0 &&
(mstime() - ri->master->info_refresh) < SENTINEL_INFO_PERIOD*2)
{ {
int retval = sentinelSendSlaveOf(ri, int retval = sentinelSendSlaveOf(ri,
ri->master->addr->ip, ri->master->addr->ip,
...@@ -1629,16 +1633,11 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) { ...@@ -1629,16 +1633,11 @@ void sentinelRefreshInstanceInfo(sentinelRedisInstance *ri, const char *info) {
{ {
mstime_t wait_time = SENTINEL_PUBLISH_PERIOD*4; mstime_t wait_time = SENTINEL_PUBLISH_PERIOD*4;
if (!sentinelRedisInstanceNoDownFor(ri,wait_time) ||
mstime() - ri->slave_conf_change_time < wait_time)
return;
/* Make sure the master is sane before reconfiguring this instance /* Make sure the master is sane before reconfiguring this instance
* into a slave. */ * into a slave. */
if (ri->master->flags & SRI_MASTER && if (sentinelMasterLooksSane(ri->master) &&
ri->master->role_reported == SRI_MASTER && sentinelRedisInstanceNoDownFor(ri,wait_time) &&
(ri->master->flags & (SRI_S_DOWN|SRI_O_DOWN)) == 0 && mstime() - ri->slave_conf_change_time > wait_time)
(mstime() - ri->master->info_refresh) < SENTINEL_INFO_PERIOD*2)
{ {
int retval = sentinelSendSlaveOf(ri, int retval = sentinelSendSlaveOf(ri,
ri->master->addr->ip, ri->master->addr->ip,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册