主从同步过程

2017-12-16  本文已影响0人  fred290

同步命令sync主要实现函数

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. */
      函数: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,则创建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函数中全同步结束,此时由主进程handle 子进程处理rdbsave的信号,
进一步创建事件,传输save的rdb数据
 ********************************************************** /
}

redis定时任务函数

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主从复制过程

先不解释replication buffer和replication backlog,而先看看redis主从复制的过程。
redis的主从复制分为两个阶段:

replication buffer的作用

redis的slave buffer(replication buffer,master端上)存放的数据是下面三个时间内所有的master数据更新操作。

replication buffer太小会引发的问题:

replication buffer由client-output-buffer-limit slave设置,当这个值太小会导致主从复制链接断开。

参考链接:
http://mdba.cn/2015/03/17/redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6%EF%BC%882%EF%BC%89-replication-buffer%E4%B8%8Ereplication-backlog/

上一篇 下一篇

猜你喜欢

热点阅读