summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2015-08-05 13:34:46 +0200
committerantirez <antirez@gmail.com>2015-08-05 13:57:38 +0200
commit27c0ab238b19b42066930f72f7b127399d6a5f85 (patch)
tree0e01ec75a4818b45f28931edc6b76667cb45ad56
parent547ccc4b5e24682d2bb8c6fe0a1316172d7847bf (diff)
downloadredis-27c0ab238b19b42066930f72f7b127399d6a5f85.tar.gz
Make sure we re-emit SELECT after each new slave full sync setup.
In previous commits we moved the FULLRESYNC to the moment we start the BGSAVE, so that the offset we provide is the right one. However this also means that we need to re-emit the SELECT statement every time a new slave starts to accumulate the changes. To obtian this effect in a more clean way, the function that sends the FULLRESYNC reply was overloaded with a more important role of also doing this and chanigng the slave state. So it was renamed to replicationSetupSlaveForFullResync() to better reflect what it does now.
-rw-r--r--src/rdb.c2
-rw-r--r--src/redis.h2
-rw-r--r--src/replication.c39
3 files changed, 26 insertions, 17 deletions
diff --git a/src/rdb.c b/src/rdb.c
index c050664fe..4903bca91 100644
--- a/src/rdb.c
+++ b/src/rdb.c
@@ -1421,7 +1421,7 @@ int rdbSaveToSlavesSockets(void) {
clientids[numfds] = slave->id;
fds[numfds++] = slave->fd;
slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
- replicationSendFullresyncReply(slave,getPsyncInitialOffset());
+ replicationSetupSlaveForFullResync(slave,getPsyncInitialOffset());
/* Put the socket in non-blocking mode to simplify RDB transfer.
* We'll restore it when the children returns (since duped socket
* will share the O_NONBLOCK attribute with the parent). */
diff --git a/src/redis.h b/src/redis.h
index 1dd8c9ec4..e12684407 100644
--- a/src/redis.h
+++ b/src/redis.h
@@ -1175,7 +1175,7 @@ void replicationSendNewlineToMaster(void);
long long replicationGetSlaveOffset(void);
char *replicationGetSlaveName(redisClient *c);
long long getPsyncInitialOffset(void);
-int replicationSendFullresyncReply(redisClient *slave, long long offset);
+int replicationSetupSlaveForFullResync(redisClient *slave, long long offset);
/* Generic persistence functions */
void startLoading(FILE *fp);
diff --git a/src/replication.c b/src/replication.c
index e01447dfb..0a695155a 100644
--- a/src/replication.c
+++ b/src/replication.c
@@ -362,16 +362,30 @@ long long getPsyncInitialOffset(void) {
return psync_offset;
}
-/* Send a PSYNC reply in the specific case of a full resynchronization.
- * As a side effect, set into the slave client structure the offset
- * we sent here, so that if new slaves will later attach to the same
- * background RDB saving process (by duplicating this client output
- * buffer), we can get the right offset from this slave. */
-int replicationSendFullresyncReply(redisClient *slave, long long offset) {
+/* Send a FULLRESYNC reply in the specific case of a full resynchronization,
+ * as a side effect setup the slave for a full sync in different ways:
+ *
+ * 1) Remember, into the slave client structure, the offset we sent
+ * here, so that if new slaves will later attach to the same
+ * background RDB saving process (by duplicating this client output
+ * buffer), we can get the right offset from this slave.
+ * 2) Set the replication state of the slave to WAIT_BGSAVE_END so that
+ * we start accumulating differences from this point.
+ * 3) Force the replication stream to re-emit a SELECT statement so
+ * the new slave incremental differences will start selecting the
+ * right database number.
+ */
+int replicationSetupSlaveForFullResync(redisClient *slave, long long offset) {
char buf[128];
int buflen;
slave->psync_initial_offset = offset;
+ slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
+ /* We are going to accumulate the incremental changes for this
+ * slave as well. Set slaveseldb to -1 in order to force to re-emit
+ * a SLEECT statement in the replication stream. */
+ server.slaveseldb = -1;
+
/* Don't send this reply to slaves that approached us with
* the old SYNC command. */
if (!(slave->flags & REDIS_PRE_PSYNC)) {
@@ -566,8 +580,7 @@ void syncCommand(redisClient *c) {
/* Perfect, the server is already registering differences for
* another slave. Set the right state, and copy the buffer. */
copyClientOutputBuffer(c,slave);
- c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
- replicationSendFullresyncReply(c,slave->psync_initial_offset);
+ replicationSetupSlaveForFullResync(c,slave->psync_initial_offset);
redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");
} else {
/* No way, we need to wait for the next BGSAVE in order to
@@ -603,8 +616,7 @@ void syncCommand(redisClient *c) {
addReplyError(c,"Unable to perform background save");
return;
}
- c->replstate = REDIS_REPL_WAIT_BGSAVE_END;
- replicationSendFullresyncReply(c,getPsyncInitialOffset());
+ replicationSetupSlaveForFullResync(c,getPsyncInitialOffset());
}
}
@@ -612,7 +624,6 @@ void syncCommand(redisClient *c) {
anetDisableTcpNoDelay(NULL, c->fd); /* Non critical if it fails. */
c->repldbfd = -1;
c->flags |= REDIS_SLAVE;
- server.slaveseldb = -1; /* Force to re-emit the SELECT command. */
listAddNodeTail(server.slaves,c);
if (listLength(server.slaves) == 1 && server.repl_backlog == NULL)
createReplicationBacklog();
@@ -791,8 +802,7 @@ void updateSlavesWaitingBgsave(int bgsaveerr, int type) {
if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
startbgsave = 1;
- slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
- replicationSendFullresyncReply(slave,getPsyncInitialOffset());
+ replicationSetupSlaveForFullResync(slave,getPsyncInitialOffset());
} else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {
struct redis_stat buf;
@@ -2131,8 +2141,7 @@ void replicationCron(void) {
while((ln = listNext(&li))) {
redisClient *slave = ln->value;
if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {
- slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;
- replicationSendFullresyncReply(slave,
+ replicationSetupSlaveForFullResync(slave,
getPsyncInitialOffset());
}
}