From 2320e632d9d31385162d74dec2c970f559dd6ab7 Mon Sep 17 00:00:00 2001 From: John Sully Date: Sat, 30 May 2020 15:13:28 -0400 Subject: [PATCH] We only want one master connection in progress at a time, ensure that if the connection fails we try a different master Former-commit-id: 8a0441c14475dc54616337270e092068acaa274a --- src/replication.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/replication.cpp b/src/replication.cpp index 58b080d53..103529eaf 100644 --- a/src/replication.cpp +++ b/src/replication.cpp @@ -3747,6 +3747,18 @@ void replicationCron(void) { listIter liMaster; listNode *lnMaster; listRewind(g_pserver->masters, &liMaster); + + bool fInMasterConnection = false; + while ((lnMaster = listNext(&liMaster)) && !fInMasterConnection) + { + redisMaster *mi = (redisMaster*)listNodeValue(lnMaster); + if (mi->repl_state != REPL_STATE_NONE && mi->repl_state != REPL_STATE_CONNECTED && mi->repl_state != REPL_STATE_CONNECT) { + fInMasterConnection = true; + } + } + + bool fConnectionStarted = false; + listRewind(g_pserver->masters, &liMaster); while ((lnMaster = listNext(&liMaster))) { redisMaster *mi = (redisMaster*)listNodeValue(lnMaster); @@ -3785,12 +3797,14 @@ void replicationCron(void) { } /* Check if we should connect to a MASTER */ - if (mi->repl_state == REPL_STATE_CONNECT) { + if (mi->repl_state == REPL_STATE_CONNECT && !fInMasterConnection) { serverLog(LL_NOTICE,"Connecting to MASTER %s:%d", mi->masterhost, mi->masterport); if (connectWithMaster(mi) == C_OK) { serverLog(LL_NOTICE,"MASTER <-> REPLICA sync started"); } + fInMasterConnection = true; + fConnectionStarted = true; } /* Send ACK to master from time to time. @@ -3801,6 +3815,11 @@ void replicationCron(void) { replicationSendAck(mi); } + if (fConnectionStarted) { + // If we cancel this handshake we want the next attempt to be a different master + listRotateHeadToTail(g_pserver->masters); + } + /* If we have attached slaves, PING them from time to time. * So slaves can implement an explicit timeout to masters, and will * be able to detect a link disconnection even if the TCP connection