From e9e007555e03af906544c0870faaaba1e1d2d0a2 Mon Sep 17 00:00:00 2001 From: antirez Date: Thu, 16 Oct 2014 09:03:52 +0200 Subject: [PATCH] Diskless replication: trigger diskless RDB transfer if needed. --- src/redis.h | 2 +- src/replication.c | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/redis.h b/src/redis.h index e5581436..fd491b45 100644 --- a/src/redis.h +++ b/src/redis.h @@ -115,7 +115,7 @@ typedef long long mstime_t; /* millisecond time type. */ #define REDIS_DEFAULT_RDB_CHECKSUM 1 #define REDIS_DEFAULT_RDB_FILENAME "dump.rdb" #define REDIS_DEFAULT_RDB_DISKLESS 0 -#define REIDS_DEFAULT_RDB_DISKLESS_DELAY 5 +#define REDIS_DEFAULT_RDB_DISKLESS_DELAY 5 #define REDIS_DEFAULT_SLAVE_SERVE_STALE_DATA 1 #define REDIS_DEFAULT_SLAVE_READ_ONLY 1 #define REDIS_DEFAULT_REPL_DISABLE_TCP_NODELAY 0 diff --git a/src/replication.c b/src/replication.c index 02d4d50b..11ed3a91 100644 --- a/src/replication.c +++ b/src/replication.c @@ -212,7 +212,7 @@ void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc) { } /* Write the command to every slave. */ - listRewind(slaves,&li); + listRewind(server.slaves,&li); while((ln = listNext(&li))) { redisClient *slave = ln->value; @@ -1941,6 +1941,41 @@ void replicationCron(void) { replicationScriptCacheFlush(); } + /* If we are using diskless replication and there are slaves waiting + * in WAIT_BGSAVE_START state, check if enough seconds elapsed and + * start one. */ + if (server.repl_diskless && server.rdb_child_pid == -1 && + server.aof_child_pid == -1) + { + time_t idle, max_idle = 0; + int slaves_waiting = 0; + listNode *ln; + listIter li; + + listRewind(server.slaves,&li); + while((ln = listNext(&li))) { + redisClient *slave = ln->value; + if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) { + idle = server.unixtime - slave->lastinteraction; + if (idle > max_idle) max_idle = idle; + slaves_waiting++; + } + } + + if (slaves_waiting && max_idle > REDIS_DEFAULT_RDB_DISKLESS_DELAY) { + /* Let's start a BGSAVE with disk target. */ + if (startBgsaveForReplication() == REDIS_OK) { + /* It started! We need to change the state of slaves + * from WAIT_BGSAVE_START to WAIT_BGSAVE_END. */ + while((ln = listNext(&li))) { + redisClient *slave = ln->value; + if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) + slave->replstate = REDIS_REPL_WAIT_BGSAVE_END; + } + } + } + } + /* Refresh the number of slaves with lag <= min-slaves-max-lag. */ refreshGoodSlavesCount(); }