MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / replicationCron

Function replicationCron

src/replication.cpp:4757–4996  ·  view source on GitHub ↗

Replication cron function, called 1 time per second. */

Source from the content-addressed store, hash-verified

4755
4756/* Replication cron function, called 1 time per second. */
4757void replicationCron(void) {
4758 static long long replication_cron_loops = 0;
4759 serverAssert(GlobalLocksAcquired());
4760
4761 /* Check failover status first, to see if we need to start
4762 * handling the failover. */
4763 updateFailoverStatus();
4764
4765 listIter liMaster;
4766 listNode *lnMaster;
4767 listRewind(g_pserver->masters, &liMaster);
4768
4769 bool fInMasterConnection = false;
4770 while ((lnMaster = listNext(&liMaster)) && !fInMasterConnection)
4771 {
4772 redisMaster *mi = (redisMaster*)listNodeValue(lnMaster);
4773 if (mi->repl_state != REPL_STATE_NONE && mi->repl_state != REPL_STATE_CONNECTED && mi->repl_state != REPL_STATE_CONNECT) {
4774 fInMasterConnection = true;
4775 }
4776 }
4777
4778 bool fConnectionStarted = false;
4779 listRewind(g_pserver->masters, &liMaster);
4780 while ((lnMaster = listNext(&liMaster)))
4781 {
4782 redisMaster *mi = (redisMaster*)listNodeValue(lnMaster);
4783
4784 std::unique_lock<decltype(mi->master->lock)> ulock;
4785 if (mi->master != nullptr)
4786 ulock = decltype(ulock)(mi->master->lock);
4787
4788 /* Non blocking connection timeout? */
4789 if (mi->masterhost &&
4790 (mi->repl_state == REPL_STATE_CONNECTING ||
4791 slaveIsInHandshakeState(mi)) &&
4792 (time(NULL)-mi->repl_transfer_lastio) > g_pserver->repl_timeout)
4793 {
4794 serverLog(LL_WARNING,"Timeout connecting to the MASTER...");
4795 cancelReplicationHandshake(mi,true);
4796 }
4797
4798 /* Bulk transfer I/O timeout? */
4799 if (mi->masterhost && mi->repl_state == REPL_STATE_TRANSFER &&
4800 (time(NULL)-mi->repl_transfer_lastio) > g_pserver->repl_timeout)
4801 {
4802 serverLog(LL_WARNING,"Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in keydb.conf to a larger value.");
4803 cancelReplicationHandshake(mi,true);
4804 }
4805
4806 /* Timed out master when we are an already connected replica? */
4807 if (mi->masterhost && mi->master && mi->repl_state == REPL_STATE_CONNECTED &&
4808 (time(NULL)-mi->master->lastinteraction) > g_pserver->repl_timeout)
4809 {
4810 serverLog(LL_WARNING,"MASTER timeout: no data nor PING received...");
4811 disconnectMaster(mi);
4812 }
4813
4814 /* Check if we should connect to a MASTER */

Callers 1

serverCronFunction · 0.85

Calls 15

GlobalLocksAcquiredFunction · 0.85
updateFailoverStatusFunction · 0.85
listRewindFunction · 0.85
listNextFunction · 0.85
slaveIsInHandshakeStateFunction · 0.85
serverLogFunction · 0.85
disconnectMasterFunction · 0.85
connectWithMasterFunction · 0.85
replicationSendAckFunction · 0.85
listRotateHeadToTailFunction · 0.85

Tested by

no test coverage detected