summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorantirez <antirez@gmail.com>2013-12-22 11:43:25 +0100
committerantirez <antirez@gmail.com>2013-12-22 11:54:10 +0100
commit4456ee11734872fcab676ff439784ce94070d5a5 (patch)
tree09f10f76e1161896c7376f763786b3539f0efb36
parent5fa937d956786fc43c223adac505aacb93476117 (diff)
downloadredis-4456ee11734872fcab676ff439784ce94070d5a5.tar.gz
Make new masters inherit replication offsets.
Currently replication offsets could be used into a limited way in order to understand, out of a set of slaves, what is the one with the most updated data. For example this comparison is possible of N slaves were replicating all with the same master. However the replication offset was not transferred from master to slaves (that are later promoted as masters) in any way, so for instance if there were three instances A, B, C, with A master and B and C replication from A, the following could happen: C disconnects from A. B is turned into master. A is switched to master of B. B receives some write. In this context there was no way to compare the offset of A and C, because B would use its own local master replication offset as replication offset to initialize the replication with A. With this commit what happens is that when B is turned into master it inherits the replication offset from A, making A and C comparable. In the above case assuming no inconsistencies are created during the disconnection and failover process, A will show to have a replication offset greater than C. Note that this does not mean offsets are always comparable to understand what is, in a set of instances, since in more complex examples the replica with the higher replication offset could be partitioned away when picking the instance to elect as new master. However this in general improves the ability of a system to try to pick a good replica to promote to master.
-rw-r--r--src/replication.c13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/replication.c b/src/replication.c
index b4bd9fe69..b1d08c774 100644
--- a/src/replication.c
+++ b/src/replication.c
@@ -1242,6 +1242,7 @@ void replicationSetMaster(char *ip, int port) {
freeReplicationBacklog(); /* Don't allow our chained slaves to PSYNC. */
cancelReplicationHandshake();
server.repl_state = REDIS_REPL_CONNECT;
+ server.master_repl_offset = 0;
}
/* Cancel replication, setting the instance as a master itself. */
@@ -1249,7 +1250,17 @@ void replicationUnsetMaster(void) {
if (server.masterhost == NULL) return; /* Nothing to do. */
sdsfree(server.masterhost);
server.masterhost = NULL;
- if (server.master) freeClient(server.master);
+ if (server.master) {
+ if (listLength(server.slaves) == 0) {
+ /* If this instance is turned into a master and there are no
+ * slaves, it inherits the replication offset from the master.
+ * Under certain conditions this makes replicas comparable by
+ * replication offset to understand what is the most updated. */
+ server.master_repl_offset = server.master->reploff;
+ freeReplicationBacklog();
+ }
+ freeClient(server.master);
+ }
replicationDiscardCachedMaster();
cancelReplicationHandshake();
server.repl_state = REDIS_REPL_NONE;