同步命令sync主要實現(xiàn)函數(shù)
syncCommand
{
日志:Slave %s asks for synchronization
/ * So the slave knows the new replid and offset to try a PSYNC later
* if the connection with the master is lost. */
函數(shù):masterTryPartialResynchronization {
if (!server.repl_backlog || psync_offset < server.repl_backlog_off ||
psync_offset > (server.repl_backlog_off + server.repl_backlog_histlen))
{
日志:"Unable to partial resync with slave %s for lack of backlog (Slave request was: %lld)."
if (psync_offset > server.master_repl_offset) {
日志:"Warning: slave %s tried to PSYNC with an offset that is greater than the master replication offset."
}
走向全同步
}
/* If we reached this point, we are able to perform a partial resync:
* 1) Set client state to make it a slave.
* 2) Inform the client we can continue with +CONTINUE
* 3) Send the backlog data (from the offset to the end) to the slave. */
}
/ **********************************************************
開始全同步
**********************************************************/
/* Full resynchronization. */
/* Setup the slave as one waiting for BGSAVE to start. The following code
* paths will change the state if we handle the slave differently. */
如果是第一個連過來的slave,則創(chuàng)建repl_backlog
/* Create the replication backlog if needed. */
有BGSAVE進行的情形
/* CASE 1: BGSAVE is in progress, with disk target. */
/* CASE 2: BGSAVE is in progress, with socket target. */
/* CASE 3: There is no BGSAVE is progress. */
非 repl_diskless_sync情形下,沒有AOF rewrite操作呢岗,進行Bgsave操作
startBgsaveForReplication(c->slave_capa) {
serverLog(LL_NOTICE,"Starting BGSAVE for SYNC with target: %s", socket_target ? "slaves sockets" :"disk");
rdbSaveBackground()
replicationSetupSlaveForFullResync(slave, getPsyncInitialOffset());
}
/ **********************************************************
在syncCommand函數(shù)中全同步結(jié)束本昏,此時由主進程handle 子進程處理rdbsave的信號,
進一步創(chuàng)建事件痢缎,傳輸save的rdb數(shù)據(jù)
********************************************************** /
}
redis定時任務(wù)函數(shù)
int serverCron
{
/* Check if a background saving or AOF rewrite in progress terminated. */
if (server.rdb_child_pid != -1 || server.aof_child_pid != -1 || ldbPendingChildren())
{
backgroundSaveDoneHandler(exitcode,bysignal);
backgroundSaveDoneHandlerDisk(exitcode,bysignal);
日志:serverLog(LL_NOTICE, "Background saving terminated with success");
updateSlavesWaitingBgsave((!bysignal && exitcode == 0)
aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);
if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) ==
AE_ERR)
{
freeClient(slave);
}
backgroundRewriteDoneHandler(exitcode,bysignal);
}
}
redis主從復(fù)制過程
先不解釋replication buffer和replication backlog胁勺,而先看看redis主從復(fù)制的過程。
redis的主從復(fù)制分為兩個階段:
- 1)同步(sync rdb snapshot):slave復(fù)制master的某時間點(t)的全量數(shù)據(jù)独旷,t為master接收到slave的sync命令后執(zhí)行rdb bgsave的時間點署穗。2.8增加psync寥裂,支持full resync和partial resync命令。master發(fā)送rdb文件到slave案疲,slave讀取rdb把數(shù)據(jù)加載到內(nèi)存抚恒。
- 2)命令傳播(commands propagation):同步時間點t后master上的數(shù)據(jù)更新到slave上, 發(fā)送的數(shù)據(jù)是redis的命令络拌。
replication buffer的作用
redis的slave buffer(replication buffer俭驮,master端上)存放的數(shù)據(jù)是下面三個時間內(nèi)所有的master數(shù)據(jù)更新操作。
- 1)master執(zhí)行rdb bgsave產(chǎn)生snapshot的時間
- 2)master發(fā)送rdb到slave網(wǎng)絡(luò)傳輸時間
- 3)slave load rdb文件把數(shù)據(jù)恢復(fù)到內(nèi)存的時間
replication buffer太小會引發(fā)的問題:
replication buffer由client-output-buffer-limit slave設(shè)置春贸,當這個值太小會導(dǎo)致主從復(fù)制鏈接斷開混萝。
- 1)當master-slave復(fù)制連接斷開,server端會釋放連接相關(guān)的數(shù)據(jù)結(jié)構(gòu)萍恕。replication buffer中的數(shù)據(jù)也就丟失了逸嘀,此時主從之間重新開始復(fù)制過程。
- 2)還有個更嚴重的問題允粤,主從復(fù)制連接斷開崭倘,導(dǎo)致主從上出現(xiàn)rdb bgsave和rdb重傳操作無限循環(huán)。