From 0413fbc7d015d2ad9a4f57cc50a68e581f0e7744 Mon Sep 17 00:00:00 2001 From: Wen Hui Date: Mon, 19 Apr 2021 02:34:21 -0400 Subject: [PATCH] fix invalid master_link_down_since_seconds in info repication (#8785) When replica never successfully connect to master, server.repl_down_since will be initialized to 0, therefore, the info master_link_down_since_seconds was showing the current unix timestamp, which does not make much sense. This commit fixes the issue by showing master_link_down_since_seconds to -1. means the replica never connect to master before. This commit also resets this variable back to 0 when a replica is turned into a master, so that it'll behave the same if the master is later turned into a replica again. The implication of this change is that if some app is checking if the value is > 60 do something, like conclude the replica is stale, this could case harm (changing a big positive number with a small one). --- src/replication.c | 3 +++ src/server.c | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/replication.c b/src/replication.c index 86e138ca4..8177eb073 100644 --- a/src/replication.c +++ b/src/replication.c @@ -2703,6 +2703,9 @@ void replicationUnsetMaster(void) { * starting from now. Otherwise the backlog will be freed after a * failover if slaves do not connect immediately. */ server.repl_no_slaves_since = server.unixtime; + + /* Reset down time so it'll be ready for when we turn into replica again. */ + server.repl_down_since = 0; /* Fire the role change modules event. */ moduleFireServerEvent(REDISMODULE_EVENT_REPLICATION_ROLE_CHANGED, diff --git a/src/server.c b/src/server.c index cec63fff0..993260619 100644 --- a/src/server.c +++ b/src/server.c @@ -5120,7 +5120,8 @@ sds genRedisInfoString(const char *section) { if (server.repl_state != REPL_STATE_CONNECTED) { info = sdscatprintf(info, "master_link_down_since_seconds:%jd\r\n", - (intmax_t)(server.unixtime-server.repl_down_since)); + server.repl_down_since ? + (intmax_t)(server.unixtime-server.repl_down_since) : -1); } info = sdscatprintf(info, "slave_priority:%d\r\n"