Skip to content

Instantly share code, notes, and snippets.

@zaa
Forked from ambroff/patch-keep-slave-data.diff
Created December 22, 2011 09:45
Show Gist options
  • Save zaa/1509723 to your computer and use it in GitHub Desktop.
Save zaa/1509723 to your computer and use it in GitHub Desktop.
Allow setting up replication without purging data in the slave first.
diff --git a/redis.conf b/redis.conf
index 44fb536..505cb6e 100644
--- a/redis.conf
+++ b/redis.conf
@@ -150,6 +150,13 @@ slave-serve-stale-data yes
#
# repl-timeout 60
+# When setting up replication, the slave will purge the current data set before
+# loading the data sync from the master. Setting this will prevent that from
+# happening. This is not recommended. Only use this if you know what you are
+# doing.
+#
+keep-slave-data no
+
################################## SECURITY ###################################
# Require clients to issue AUTH <PASSWORD> before processing any other
diff --git a/src/config.c b/src/config.c
index 1be88f5..0f7c8a1 100644
--- a/src/config.c
+++ b/src/config.c
@@ -213,6 +213,10 @@ void loadServerConfig(char *filename) {
if ((server.repl_serve_stale_data = yesnotoi(argv[1])) == -1) {
err = "argument must be 'yes' or 'no'"; goto loaderr;
}
+ } else if (!strcasecmp(argv[0],"keep-slave-data") && argc == 2) {
+ if ((server.keep_slave_data = yesnotoi(argv[1])) == -1) {
+ err = "argument must be 'yes' or 'no'"; goto loaderr;
+ }
} else if (!strcasecmp(argv[0],"glueoutputbuf")) {
redisLog(REDIS_WARNING, "Deprecated configuration directive: \"%s\"", argv[0]);
} else if (!strcasecmp(argv[0],"rdbcompression") && argc == 2) {
diff --git a/src/rdb.c b/src/rdb.c
index 7d11a11..61b431e 100644
--- a/src/rdb.c
+++ b/src/rdb.c
@@ -1000,7 +1000,11 @@ int rdbLoad(char *filename) {
continue;
}
/* Add the new object in the hash table */
- dbAdd(db,key,val);
+ if (lookupKeyWrite(db,key) == NULL) {
+ dbAdd(db,key,val);
+ } else {
+ dbOverwrite(db,key,val);
+ }
/* Set the expire time if needed */
if (expiretime != -1) setExpire(db,key,expiretime);
diff --git a/src/redis.c b/src/redis.c
index 8dda301..1d6a571 100644
--- a/src/redis.c
+++ b/src/redis.c
@@ -868,6 +868,7 @@ void initServerConfig() {
server.repl_syncio_timeout = REDIS_REPL_SYNCIO_TIMEOUT;
server.repl_serve_stale_data = 1;
server.repl_down_since = -1;
+ server.keep_slave_data = 0;
/* Double constants initialization */
R_Zero = 0.0;
diff --git a/src/redis.h b/src/redis.h
index 6090860..95e73d8 100644
--- a/src/redis.h
+++ b/src/redis.h
@@ -470,6 +470,7 @@ struct redisServer {
time_t repl_transfer_lastio; /* unix time of the latest read, for timeout */
int repl_serve_stale_data; /* Serve stale data when link is down? */
time_t repl_down_since; /* unix time at which link with master went down */
+ int keep_slave_data; /* Don't trash db when setting up replication. */
/* Limits */
unsigned int maxclients;
unsigned long long maxmemory;
diff --git a/src/replication.c b/src/replication.c
index afe86d1..44e4c00 100644
--- a/src/replication.c
+++ b/src/replication.c
@@ -339,7 +339,13 @@ void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
return;
}
redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory");
- emptyDb();
+
+ /* Only purge data in the slave if we haven't specified that we want to
+ * keep it. */
+ if (!server.keep_slave_data) {
+ emptyDb();
+ }
+
/* Before loading the DB into memory we need to delete the readable
* handler, otherwise it will get called recursively since
* rdbLoad() will call the event loop to process events from time to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment