redis源码分析(8)——replication

来源:互联网 发布:linux视频教程百度网盘 编辑:程序博客网 时间:2024/06/05 16:06

redis的主从同步用于实现高可用,并且可以实现读写分离,用于抗大量读请求。通过两种方式指定一个slave从master复制:

- 在配置文件中配置:slaveof <masterip> <masterport>

- 或者在客户端中执行命令:slaveof <master> <masterport>

这两种方式对应的操作很简单,都是将server.repl_state设置为REDIS_REPL_CONNECT,即将slave设置成同步的初始状态,并设置server的masterhost和masterport字段为对应master的ip和端口。

在主从同步过程中,实际上master端和slave端都是一个状态机,整个过程不是在一个函数中执行的,被事件循环打断成多次的函数调用,在函数中会根据当前状态执行相应的操作。

master端的状态有:

/* Slave replication state - from the point of view of the master. * In SEND_BULK and ONLINE state the slave receives new updates * in its output queue. In the WAIT_BGSAVE state instead the server is waiting * to start the next background saving in order to send updates to it. */#define REDIS_REPL_WAIT_BGSAVE_START 6 /* We need to produce a new RDB file. */#define REDIS_REPL_WAIT_BGSAVE_END 7 /* Waiting RDB file creation to finish. */#define REDIS_REPL_SEND_BULK 8 /* Sending RDB file to slave. */#define REDIS_REPL_ONLINE 9 /* RDB file transmitted, sending just updates. */

slave端的状态:

/* Slave replication state - from the point of view of the slave. */#define REDIS_REPL_NONE 0 /* No active replication */#define REDIS_REPL_CONNECT 1 /* Must connect to master */#define REDIS_REPL_CONNECTING 2 /* Connecting to master */#define REDIS_REPL_RECEIVE_PONG 3 /* Wait for PING reply */#define REDIS_REPL_TRANSFER 4 /* Receiving .rdb from master */#define REDIS_REPL_CONNECTED 5 /* Connected to master */

在serverCron(默认10ms执行一次)中,会执行replicationCron函数用于维护同步的状态。

slave与master交互的时序图如下,由于交互的状态很复杂,这里只描述第一次同步成功的过程:


整个同步过程可以分为两个步骤:一是生成并传输快照(RDB),二是传输增量backlog。下面就看一下主从同步相关的代码。

    /* Replication cron function -- used to reconnect to master and     * to detect transfer failures. */    run_with_period(1000) replicationCron();
在serverCron函数中,会近似以大于1秒的时间间隔执行replicationCron函数,用来维护同步关系。主从同步很复杂,这里我们先看一下同步过程,然后再介绍维护同步过程的其他逻辑。

1. slave发起连接

在replicationCron函数中:

    /* Check if we should connect to a MASTER */    if (server.repl_state == REDIS_REPL_CONNECT) {        redisLog(REDIS_NOTICE,"Connecting to MASTER %s:%d",            server.masterhost, server.masterport);        if (connectWithMaster() == REDIS_OK) {            redisLog(REDIS_NOTICE,"MASTER <-> SLAVE sync started");        }    }

如果slave的同步状态是REDIS_REPL_CONNECT,会调用connectWithMaster()函数发起对master的连接。

int connectWithMaster(void) {    int fd;    fd = anetTcpNonBlockConnect(NULL,server.masterhost,server.masterport);    if (fd == -1) {        redisLog(REDIS_WARNING,"Unable to connect to MASTER: %s",            strerror(errno));        return REDIS_ERR;    }    if (aeCreateFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE,syncWithMaster,NULL) ==            AE_ERR)    {        close(fd);        redisLog(REDIS_WARNING,"Can't create readable event for SYNC");        return REDIS_ERR;    }    server.repl_transfer_lastio = server.unixtime;    server.repl_transfer_s = fd;    server.repl_state = REDIS_REPL_CONNECTING;    return REDIS_OK;}
这个函数主要完成以下几件事:

- 非阻塞的调用connect,连接master

- 对该连接socket注册读、写事件的处理函数syncWithMaster

- 更新server.repl_transfer_lastio,避免超时,被干掉

- server.repl_transfer_s表示连接的socket

- 更新同步状态为REDIS_REPL_CONNECTING

当连接建立成功后,会回调syncWithMaster,接下来看一下这个函数。

    char tmpfile[256], *err;    int dfd, maxtries = 5;    int sockerr = 0, psync_result;    socklen_t errlen = sizeof(sockerr);    REDIS_NOTUSED(el);    REDIS_NOTUSED(privdata);    REDIS_NOTUSED(mask);    /* If this event fired after the user turned the instance into a master     * with SLAVEOF NO ONE we must just return ASAP. */    if (server.repl_state == REDIS_REPL_NONE) {        close(fd);        return;    }
声明各种变量,并检查当前的同步状态。

    /* Check for errors in the socket. */    if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1)        sockerr = errno;    if (sockerr) {        aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);        redisLog(REDIS_WARNING,"Error condition on socket for SYNC: %s",            strerror(sockerr));        goto error;    }

检查socket错误

    /* If we were connecting, it's time to send a non blocking PING, we want to     * make sure the master is able to reply before going into the actual     * replication process where we have long timeouts in the order of     * seconds (in the meantime the slave would block). */    if (server.repl_state == REDIS_REPL_CONNECTING) {        redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event.");        /* Delete the writable event so that the readable event remains         * registered and we can wait for the PONG reply. */        aeDeleteFileEvent(server.el,fd,AE_WRITABLE);        server.repl_state = REDIS_REPL_RECEIVE_PONG;        /* Send the PING, don't check for errors at all, we have the timeout         * that will take care about this. */        syncWrite(fd,"PING\r\n",6,100);        return;    }

这个函数在建立连接的过程中,会被回调多次,根据当前的同步状态进行一些处理。如果处于REDIS_REPL_CONNECTING状态,即刚发起连接。这里会将连接socket的写事件处理函数删除掉,同时更新同步状态为REDIS_REPL_RECEIVE_PONG,然后同步的发送PING,并返回。

PING master主要是为了double check一下,保证接下来执行一些阻塞操作时master是活跃的。

    /* Receive the PONG command. */    if (server.repl_state == REDIS_REPL_RECEIVE_PONG) {        char buf[1024];        /* Delete the readable event, we no longer need it now that there is         * the PING reply to read. */        aeDeleteFileEvent(server.el,fd,AE_READABLE);        /* Read the reply with explicit timeout. */        buf[0] = '\0';        if (syncReadLine(fd,buf,sizeof(buf),            server.repl_syncio_timeout*1000) == -1)        {            redisLog(REDIS_WARNING,                "I/O error reading PING reply from master: %s",                strerror(errno));            goto error;        }        /* We accept only two replies as valid, a positive +PONG reply         * (we just check for "+") or an authentication error.         * Note that older versions of Redis replied with "operation not         * permitted" instead of using a proper error code, so we test         * both. */        if (buf[0] != '+' &&            strncmp(buf,"-NOAUTH",7) != 0 &&            strncmp(buf,"-ERR operation not permitted",28) != 0)        {            redisLog(REDIS_WARNING,"Error reply to PING from master: '%s'",buf);            goto error;        } else {            redisLog(REDIS_NOTICE,                "Master replied to PING, replication can continue...");        }    }


如果同步状态为REDIS_REPL_RECEIVE_PONG,即刚发送PING命令,准备接受PONG。首先删除连接socket的读事件处理函数,同步读取响应。可能读到的正确响应是:+PONG或验证失败错误。如果读到了正确的响应,就继续进行操作。

    /* AUTH with the master if required. */    if(server.masterauth) {        err = sendSynchronousCommand(fd,"AUTH",server.masterauth,NULL);        if (err[0] == '-') {            redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",err);            sdsfree(err);            goto error;        }        sdsfree(err);    }
如果配置了server.masterauth,则同步发送验证命令,并等待响应,如果验证失败,则退出。

    /* Set the slave port, so that Master's INFO command can list the     * slave listening port correctly. */    {        sds port = sdsfromlonglong(server.port);        err = sendSynchronousCommand(fd,"REPLCONF","listening-port",port,                                         NULL);        sdsfree(port);        /* Ignore the error if any, not all the Redis versions support         * REPLCONF listening-port. */        if (err[0] == '-') {            redisLog(REDIS_NOTICE,"(Non critical) Master does not understand REPLCONF listening-port: %s", err);        }        sdsfree(err);    }
同步发送REPLCONF命令,将slave的端口号发送给master,用于设置master端INFO命令显示slave的信息。

2. slave开始同步

上面就是slave发起同步过程前的准备工作,接下来进入正式的发起同步请求操作。

在redis2.8中,引入了半同步(partial resynchonization),支持在主从同步断开,重新连接后不需要进行全同步,可以避免master做RDB、主从传输RDB、slave加载RDB等重量级操作。在master端会有一个buffer存储back log,buffer的大小可以配置,slave在发起同步请求时,会附带masterid和back log offset,master在接收到请求后,会根据其runid做校验,并检查对应的offset是否在buffer内。如果两个检验都通过的话,就可以执行半同步,直接从offset开始发送back log。如果没有满足上述两个条件,就需要执行一次全同步。下面看一下具体代码。

    /* Try a partial resynchonization. If we don't have a cached master     * slaveTryPartialResynchronization() will at least try to use PSYNC     * to start a full resynchronization so that we get the master run id     * and the global offset, to try a partial resync at the next     * reconnection attempt. */    psync_result = slaveTryPartialResynchronization(fd);

调用slaveTryPartialResynchronization()函数尝试进行半同步。

    char *psync_runid;    char psync_offset[32];    sds reply;    /* Initially set repl_master_initial_offset to -1 to mark the current     * master run_id and offset as not valid. Later if we'll be able to do     * a FULL resync using the PSYNC command we'll set the offset at the     * right value, so that this information will be propagated to the     * client structure representing the master into server.master. */    server.repl_master_initial_offset = -1;

具体见注释

    if (server.cached_master) {        psync_runid = server.cached_master->replrunid;        snprintf(psync_offset,sizeof(psync_offset),"%lld", server.cached_master->reploff+1);        redisLog(REDIS_NOTICE,"Trying a partial resynchronization (request %s:%s).", psync_runid, psync_offset);    } else {        redisLog(REDIS_NOTICE,"Partial resynchronization not possible (no cached master)");        psync_runid = "?";        memcpy(psync_offset,"-1",3);    }

这段代码用来获取runid和offset的。如果server.cached_master不为空,则从cached_master中获取;否则,直接使用?和-1作为runid和offset。后文会介绍cached_master是何时设置的。

    /* Issue the PSYNC command */    reply = sendSynchronousCommand(fd,"PSYNC",psync_runid,psync_offset,NULL);
同步发送PSYNC命令。master会有以下几种响应:

- +FULLRESYNC:全量同步

- +CONTINUE:半同步

- -ERR:master不支持PSYNC命令

根据不同的响应做处理。

    if (!strncmp(reply,"+FULLRESYNC",11)) {        char *runid = NULL, *offset = NULL;        /* FULL RESYNC, parse the reply in order to extract the run id         * and the replication offset. */        runid = strchr(reply,' ');        if (runid) {            runid++;            offset = strchr(runid,' ');            if (offset) offset++;        }        if (!runid || !offset || (offset-runid-1) != REDIS_RUN_ID_SIZE) {            redisLog(REDIS_WARNING,                "Master replied with wrong +FULLRESYNC syntax.");            /* This is an unexpected condition, actually the +FULLRESYNC             * reply means that the master supports PSYNC, but the reply             * format seems wrong. To stay safe we blank the master             * runid to make sure next PSYNCs will fail. */            memset(server.repl_master_runid,0,REDIS_RUN_ID_SIZE+1);        } else {            memcpy(server.repl_master_runid, runid, offset-runid-1);            server.repl_master_runid[REDIS_RUN_ID_SIZE] = '\0';            server.repl_master_initial_offset = strtoll(offset,NULL,10);            redisLog(REDIS_NOTICE,"Full resync from master: %s:%lld",                server.repl_master_runid,                server.repl_master_initial_offset);        }        /* We are going to full resync, discard the cached master structure. */        replicationDiscardCachedMaster();        sdsfree(reply);        return PSYNC_FULLRESYNC;    }

全同步的情况,master会返回+FULLRESYNC runid offset。这段代码完成的主要功能是:

- 从响应中提取runid和offset,并复制给server.repl_master_runid和server.repl_master_initial_offset

- 因为全同步,已有的cached_master失效,会将其情况

- 返回PSYNC_FULLRESYNC

    if (!strncmp(reply,"+CONTINUE",9)) {        /* Partial resync was accepted, set the replication state accordingly */        redisLog(REDIS_NOTICE,            "Successful partial resynchronization with master.");        sdsfree(reply);        replicationResurrectCachedMaster(fd);        return PSYNC_CONTINUE;    }
半同步的情况。需要调用replicationResurrectCachedMaster函数根据cached_master,重建与master的链接。先看一下这个函数:

/* Turn the cached master into the current master, using the file descriptor * passed as argument as the socket for the new master. * * This function is called when successfully setup a partial resynchronization * so the stream of data that we'll receive will start from were this * master left. */void replicationResurrectCachedMaster(int newfd) {    server.master = server.cached_master;    server.cached_master = NULL;    server.master->fd = newfd;    server.master->flags &= ~(REDIS_CLOSE_AFTER_REPLY|REDIS_CLOSE_ASAP);    server.master->authenticated = 1;    server.master->lastinteraction = server.unixtime;    server.repl_state = REDIS_REPL_CONNECTED;    /* Re-add to the list of clients. */    listAddNodeTail(server.clients,server.master);    if (aeCreateFileEvent(server.el, newfd, AE_READABLE,                          readQueryFromClient, server.master)) {        redisLog(REDIS_WARNING,"Error resurrecting the cached master, impossible to add the readable handler: %s", strerror(errno));        freeClientAsync(server.master); /* Close ASAP. */    }    /* We may also need to install the write handler as well if there is     * pending data in the write buffers. */    if (server.master->bufpos || listLength(server.master->reply)) {        if (aeCreateFileEvent(server.el, newfd, AE_WRITABLE,                          sendReplyToClient, server.master)) {            redisLog(REDIS_WARNING,"Error resurrecting the cached master, impossible to add the writable handler: %s", strerror(errno));            freeClientAsync(server.master); /* Close ASAP. */        }    }}
这个函数的几个关键点是:从cached_master拷贝一些属性,重新设置server.repl_state为REDIS_REPL_CONNECTED,即直接变为最终状态,读取master实时发送的back log。注册读、写事件处理函数。通过以上步骤就将一个因网络抖动链接断开的主从关系重新恢复过来,而不需要全量同步。下面返回slaveTryPartialResynchronization函数。

    /* If we reach this point we receied either an error since the master does     * not understand PSYNC, or an unexpected reply from the master.     * Return PSYNC_NOT_SUPPORTED to the caller in both cases. */    if (strncmp(reply,"-ERR",4)) {        /* If it's not an error, log the unexpected event. */        redisLog(REDIS_WARNING,            "Unexpected reply to PSYNC from master: %s", reply);    } else {        redisLog(REDIS_NOTICE,            "Master does not support PSYNC or is in "            "error state (reply: %s)", reply);    }    sdsfree(reply);    replicationDiscardCachedMaster();    return PSYNC_NOT_SUPPORTED;
ERR及其他情况。清空cached_master,并返回PSYNC_NOT_SUPPORTED。

上面就是slaveTryPartialResynchronization函数,下面回来继续看syncWithMaster函数,首先就是根据slaveTryPartialResynchronization返回值做不同处理。

    if (psync_result == PSYNC_CONTINUE) {        redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.");        return;    }
如果可以半同步,打印日志,直接返回即可。所有操作在函数replicationResurrectCachedMaster以及完成。

    /* Fall back to SYNC if needed. Otherwise psync_result == PSYNC_FULLRESYNC     * and the server.repl_master_runid and repl_master_initial_offset are     * already populated. */    if (psync_result == PSYNC_NOT_SUPPORTED) {        redisLog(REDIS_NOTICE,"Retrying with SYNC...");        if (syncWrite(fd,"SYNC\r\n",6,server.repl_syncio_timeout*1000) == -1) {            redisLog(REDIS_WARNING,"I/O error writing to MASTER: %s",                strerror(errno));            goto error;        }    }
如果master不支持PSYNC,退回到SYNC。这里向master发送SYNC命令。

    /* Prepare a suitable temp file for bulk transfer */    while(maxtries--) {        snprintf(tmpfile,256,            "temp-%d.%ld.rdb",(int)server.unixtime,(long int)getpid());        dfd = open(tmpfile,O_CREAT|O_WRONLY|O_EXCL,0644);        if (dfd != -1) break;        sleep(1);    }    if (dfd == -1) {        redisLog(REDIS_WARNING,"Opening the temp file needed for MASTER <-> SLAVE synchronization: %s",strerror(errno));        goto error;    }

尝试创建存储RDB的临时文件。这里会尝试5次确保创建成功。

    /* Setup the non blocking download of the bulk file. */    if (aeCreateFileEvent(server.el,fd, AE_READABLE,readSyncBulkPayload,NULL)            == AE_ERR)    {        redisLog(REDIS_WARNING,            "Can't create readable event for SYNC: %s (fd=%d)",            strerror(errno),fd);        goto error;    }

注册读事件处理函数,用于从master接收RDB文件。

    server.repl_state = REDIS_REPL_TRANSFER;    server.repl_transfer_size = -1;    server.repl_transfer_read = 0;    server.repl_transfer_last_fsync_off = 0;    server.repl_transfer_fd = dfd;    server.repl_transfer_lastio = server.unixtime;    server.repl_transfer_tmpfile = zstrdup(tmpfile);    return;

函数最后进行一些属性设置。将server.repl_state设置为REDIS_REPL_TRANSFER,即传输RDB的状态。

error:    close(fd);    server.repl_transfer_s = -1;    server.repl_state = REDIS_REPL_CONNECT;    return;

最后是错误处理部分,将sever.repl_state设置为REDIS_REPL_CONNECT,即同步的初始状态。之后的replicationCron会尝试重新同步。

3. Master处理同步请求

slave向master发送PSYNC命令请求同步,这里就从PSYNC的命令处理函数syncCommand开始

    /* ignore SYNC if already slave or in monitor mode */    if (c->flags & REDIS_SLAVE) return;

判断请求client的类型,如果已经是slave或是monitor,拒绝请求。

    /* Refuse SYNC requests if we are a slave but the link with our master     * is not ok... */    if (server.masterhost && server.repl_state != REDIS_REPL_CONNECTED) {        addReplyError(c,"Can't SYNC while not connected with my master");        return;    }

如果master也是一个slave,并且还在同步过程中,则同样拒绝请求。

    /* SYNC can't be issued when the server has pending data to send to     * the client about already issued commands. We need a fresh reply     * buffer registering the differences between the BGSAVE and the current     * dataset, so that we can copy to other slaves if needed. */    if (listLength(c->reply) != 0 || c->bufpos != 0) {        addReplyError(c,"SYNC and PSYNC are invalid with pending output");        return;    }

如果该client有待输出的响应,这里也会拒绝请求。需要保证client的输出缓冲为空,用来存放back log。

    redisLog(REDIS_NOTICE,"Slave %s asks for synchronization",        replicationGetSlaveName(c));    /* Try a partial resynchronization if this is a PSYNC command.     * If it fails, we continue with usual full resynchronization, however     * when this happens masterTryPartialResynchronization() already     * replied with:     *     * +FULLRESYNC <runid> <offset>     *     * So the slave knows the new runid and offset to try a PSYNC later     * if the connection with the master is lost. */    if (!strcasecmp(c->argv[0]->ptr,"psync")) {        if (masterTryPartialResynchronization(c) == REDIS_OK) {            server.stat_sync_partial_ok++;            return; /* No full resync needed, return. */        } else {            char *master_runid = c->argv[1]->ptr;            /* Increment stats for failed PSYNCs, but only if the             * runid is not "?", as this is used by slaves to force a full             * resync on purpose when they are not albe to partially             * resync. */            if (master_runid[0] != '?') server.stat_sync_partial_err++;        }    } else {        /* If a slave uses SYNC, we are dealing with an old implementation         * of the replication protocol (like redis-cli --slave). Flag the client         * so that we don't expect to receive REPLCONF ACK feedbacks. */        c->flags |= REDIS_PRE_PSYNC;    }
如果上述校验通过,则可以接受同步请求,打印日志。如果请求的命令名为psync,则master会尝试进行半同步。否则,标记该client为REDIS_PRE_PSYNC,即旧版本,不支持PSYNC。下面看一下masterTryPartialResynchronization函数,接收半同步请求。

    long long psync_offset, psync_len;    char *master_runid = c->argv[1]->ptr;    char buf[128];    int buflen;    /* Is the runid of this master the same advertised by the wannabe slave     * via PSYNC? If runid changed this master is a different instance and     * there is no way to continue. */    if (strcasecmp(master_runid, server.runid)) {        /* Run id "?" is used by slaves that want to force a full resync. */        if (master_runid[0] != '?') {            redisLog(REDIS_NOTICE,"Partial resynchronization not accepted: "                "Runid mismatch (Client asked for '%s', I'm '%s')",                master_runid, server.runid);        } else {            redisLog(REDIS_NOTICE,"Full resync requested by slave %s",                replicationGetSlaveName(c));        }        goto need_full_resync;    }

首先声明变量,然后比对请求的runid,如果不匹配,则跳到need_full_resync分支,进行全同步。

    /* We still have the data our slave is asking for? */    if (getLongLongFromObjectOrReply(c,c->argv[2],&psync_offset,NULL) !=       REDIS_OK) goto need_full_resync;    if (!server.repl_backlog ||        psync_offset < server.repl_backlog_off ||        psync_offset > (server.repl_backlog_off + server.repl_backlog_histlen))    {        redisLog(REDIS_NOTICE,            "Unable to partial resync with slave %s for lack of backlog (Slave request was: %lld).", replicationGetSlaveName(c), psync_offset);        if (psync_offset > server.master_repl_offset) {            redisLog(REDIS_WARNING,                "Warning: slave %s tried to PSYNC with an offset that is greater than the master replication offset.", replicationGetSlaveName(c));        }        goto need_full_resync;    }

获取并校验offset,如果不在当前back log buffer内,不能完成半同步,就跳到need_full_resync分支,进行全同步。具体back log buffer相关的内容,后文会介绍。

    /* 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. */    c->flags |= REDIS_SLAVE;    c->replstate = REDIS_REPL_ONLINE;    c->repl_ack_time = server.unixtime;    c->repl_put_online_on_ack = 0;    listAddNodeTail(server.slaves,c);    /* We can't use the connection buffers since they are used to accumulate     * new commands at this stage. But we are sure the socket send buffer is     * empty so this write will never fail actually. */    buflen = snprintf(buf,sizeof(buf),"+CONTINUE\r\n");    if (write(c->fd,buf,buflen) != buflen) {        freeClientAsync(c);        return REDIS_OK;    }
经过runid和offset的校验,则符合半同步的条件。这段代码完成以下几件事:

- 标记client为slave

- 更新client的同步状态为REDIS_REPL_ONLINE

- 更新repl_ack_time,防止timeout

- 更新repl_put_online_on_ack,用于标记是否在slave向master发送ack时,在将slave put online。此处为0,则之后代码直接将其put online

- 将client添加到slaves链表

- 发送响应内容+CONTINUE\r\n

    psync_len = addReplyReplicationBacklog(c,psync_offset);

调用addRepliyReplicationBacklog函数,将从offset指定的back log发送给slave。这个函数很简单,不详述。

    redisLog(REDIS_NOTICE,        "Partial resynchronization request from %s accepted. Sending %lld bytes of backlog starting from offset %lld.",            replicationGetSlaveName(c),            psync_len, psync_offset);    /* Note that we don't need to set the selected DB at server.slaveseldb     * to -1 to force the master to emit SELECT, since the slave already     * has this state from the previous connection with the master. */    refreshGoodSlavesCount();    return REDIS_OK; /* The caller can return, no full resync needed. */

打印日志,更新活跃的slave的数目,最后返回REDIS_OK。

need_full_resync:    /* We need a full resync for some reason... notify the client. */    psync_offset = server.master_repl_offset;    /* Add 1 to psync_offset if it the replication backlog does not exists     * as when it will be created later we'll increment the offset by one. */    if (server.repl_backlog == NULL) psync_offset++;    /* Again, we can't use the connection buffers (see above). */    buflen = snprintf(buf,sizeof(buf),"+FULLRESYNC %s %lld\r\n",                      server.runid,psync_offset);    if (write(c->fd,buf,buflen) != buflen) {        freeClientAsync(c);        return REDIS_OK;    }    return REDIS_ERR;
看一下need_full_resync分支,发出响应内容+FULLRESYNC和,runid以及back log offset,然后返回REDIS_ERR。下面继续看syncCommand函数:

    /* Try a partial resynchronization if this is a PSYNC command.     * If it fails, we continue with usual full resynchronization, however     * when this happens masterTryPartialResynchronization() already     * replied with:     *     * +FULLRESYNC <runid> <offset>     *     * So the slave knows the new runid and offset to try a PSYNC later     * if the connection with the master is lost. */    if (!strcasecmp(c->argv[0]->ptr,"psync")) {        if (masterTryPartialResynchronization(c) == REDIS_OK) {            server.stat_sync_partial_ok++;            return; /* No full resync needed, return. */        } else {            char *master_runid = c->argv[1]->ptr;            /* Increment stats for failed PSYNCs, but only if the             * runid is not "?", as this is used by slaves to force a full             * resync on purpose when they are not albe to partially             * resync. */            if (master_runid[0] != '?') server.stat_sync_partial_err++;        }    } else {        /* If a slave uses SYNC, we are dealing with an old implementation         * of the replication protocol (like redis-cli --slave). Flag the client         * so that we don't expect to receive REPLCONF ACK feedbacks. */        c->flags |= REDIS_PRE_PSYNC;    }
还看这段代码,调用masterTryPartialResynchronization函数,如果返回REDIS_OK,即可以半同步,则更新统计,然后返回。
    /* Full resynchronization. */    server.stat_sync_full++;

更新统计,全同步的数量。接下来就要进行RDB相关事情,会有多个分支:

1)正在进行RDB,并且RDB进程使用磁盘

    /* Here we need to check if there is a background saving operation     * in progress, or if it is required to start one */    if (server.rdb_child_pid != -1 &&        server.rdb_child_type == REDIS_RDB_CHILD_TYPE_DISK)    {        /* Ok a background save is in progress. Let's check if it is a good         * one for replication, i.e. if there is another slave that is         * registering differences since the server forked to save. */        redisClient *slave;        listNode *ln;        listIter li;        listRewind(server.slaves,&li);        while((ln = listNext(&li))) {            slave = ln->value;            if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) break;        }        if (ln) {            /* Perfect, the server is already registering differences for             * another slave. Set the right state, and copy the buffer. */            copyClientOutputBuffer(c,slave);            c->replstate = REDIS_REPL_WAIT_BGSAVE_END;            redisLog(REDIS_NOTICE,"Waiting for end of BGSAVE for SYNC");        } else {            /* No way, we need to wait for the next BGSAVE in order to             * register differences. */            c->replstate = REDIS_REPL_WAIT_BGSAVE_START;            redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");        }
这种情况下,会有一个优化,在同时有多个slave同步时,只需要执行一次RDB。检查是否此次RDB dump是由于之前的slave触发的。如果是就可以避免再次重新dump RDB,将触发RDB的slave的output buffer拷贝到当前的slave,所以前面介绍需要保证slave的output buffer是空的。否则此处不能直接拷贝。具体看一下代码:

- 遍历slave链表,检查是否有处于REDIS_REPL_WAIT_BGSAVE_END状态的slave。因为redis保证同时只会有一个RDB进程,所以只要有处于这个状态的slave,就说明此次RDB是有该slave触发的

- 如果存在该状态的slave,则直接拷贝其output buffer到当前的slave,并更改状态为REDIS_REPL_WAIT_BGSAVE_END

- 如果不存在这样的slave,则将当前slave的状态设置为REDIS_REPL_WAIT_BGSAVE_START,会等待此次RDB结束后,再次触发一次RDB dump

2)正在进行RDB,并且RDB直接使用socket

    } else if (server.rdb_child_pid != -1 &&               server.rdb_child_type == REDIS_RDB_CHILD_TYPE_SOCKET)    {        /* There is an RDB child process but it is writing directly to         * children sockets. We need to wait for the next BGSAVE         * in order to synchronize. */        c->replstate = REDIS_REPL_WAIT_BGSAVE_START;        redisLog(REDIS_NOTICE,"Waiting for next BGSAVE for SYNC");

此种情况,多个slave不能共享状态,必须等待其完成,才能进行下一次RDB。所有将当前slave状态设置为REDIS_REPL_WAIT_BGSAVE_START,等待下一次RDB。

3)没有RDB正在进行

    } else {        if (server.repl_diskless_sync) {            /* Diskless replication RDB child is created inside             * replicationCron() since we want to delay its start a             * few seconds to wait for more slaves to arrive. */            c->replstate = REDIS_REPL_WAIT_BGSAVE_START;            if (server.repl_diskless_sync_delay)                redisLog(REDIS_NOTICE,"Delay next BGSAVE for SYNC");        } else {            /* Ok we don't have a BGSAVE in progress, let's start one. */            if (startBgsaveForReplication() != REDIS_OK) {                redisLog(REDIS_NOTICE,"Replication failed, can't BGSAVE");                addReplyError(c,"Unable to perform background save");                return;            }            c->replstate = REDIS_REPL_WAIT_BGSAVE_END;        }
如果使用server.repl_diskless_sync,即不使用磁盘,只会设置同步状态为REDIS_REPL_WAIT_BGSAVE_START,在replicationCron中延迟触发,为了更多的slave可以共享。

如果使用磁盘,会调用startBgsaveForRelication开始后台RDB dump。并把slave的状态设置为REDIS_REPL_WAIT_BGSAVE_END。

/* Start a BGSAVE for replication goals, which is, selecting the disk or * socket target depending on the configuration, and making sure that * the script cache is flushed before to start. * * Returns REDIS_OK on success or REDIS_ERR otherwise. */int startBgsaveForReplication(void) {    int retval;    redisLog(REDIS_NOTICE,"Starting BGSAVE for SYNC with target: %s",        server.repl_diskless_sync ? "slaves sockets" : "disk");    if (server.repl_diskless_sync)        retval = rdbSaveToSlavesSockets();    else        retval = rdbSaveBackground(server.rdb_filename);    /* Flush the script cache, since we need that slave differences are     * accumulated without requiring slaves to match our cached scripts. */    if (retval == REDIS_OK) replicationScriptCacheFlush();    return retval;}
函数startBgsaveForReplication函数在这种情况,只会调用rdbSaveBackground函数,执行后台RDB dump。接下来就是等待RDB完成,然后传输给slave。

4.Master发送RDB

之前在介绍RDB时,提到过在serverCron中,会wait子进程,当RDB进程成功退出后,会调用backgroundSaveDoneHandler函数,其中会调用updateSlavesWaitingBgsave函数用于处理同步相关的事情,下面看一下这个函数。

    listNode *ln;    int startbgsave = 0;    listIter li;    listRewind(server.slaves,&li);

声明变量,并重置slave链表,为遍历准备。

    while((ln = listNext(&li))) {        redisClient *slave = ln->value;        if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START) {            startbgsave = 1;            slave->replstate = REDIS_REPL_WAIT_BGSAVE_END;        } else if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_END) {            struct redis_stat buf;            /* If this was an RDB on disk save, we have to prepare to send             * the RDB from disk to the slave socket. Otherwise if this was             * already an RDB -> Slaves socket transfer, used in the case of             * diskless replication, our work is trivial, we can just put             * the slave online. */            if (type == REDIS_RDB_CHILD_TYPE_SOCKET) {                redisLog(REDIS_NOTICE,                    "Streamed RDB transfer with slave %s succeeded (socket). Waiting for REPLCONF ACK from slave to enable streaming",                        replicationGetSlaveName(slave));                /* Note: we wait for a REPLCONF ACK message from slave in                 * order to really put it online (install the write handler                 * so that the accumulated data can be transfered). However                 * we change the replication state ASAP, since our slave                 * is technically online now. */                slave->replstate = REDIS_REPL_ONLINE;                slave->repl_put_online_on_ack = 1;            } else {                if (bgsaveerr != REDIS_OK) {                    freeClient(slave);                    redisLog(REDIS_WARNING,"SYNC failed. BGSAVE child returned an error");                    continue;                }                if ((slave->repldbfd = open(server.rdb_filename,O_RDONLY)) == -1 ||                    redis_fstat(slave->repldbfd,&buf) == -1) {                    freeClient(slave);                    redisLog(REDIS_WARNING,"SYNC failed. Can't open/stat DB after BGSAVE: %s", strerror(errno));                    continue;                }                slave->repldboff = 0;                slave->repldbsize = buf.st_size;                slave->replstate = REDIS_REPL_SEND_BULK;                slave->replpreamble = sdscatprintf(sdsempty(),"$%lld\r\n",                    (unsigned long long) slave->repldbsize);                aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);                if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, sendBulkToSlave, slave) == AE_ERR) {                    freeClient(slave);                    continue;                }            }        }    }
接下来遍历所有的slave。如果slave的同步状态是REDIS_REPL_WAIT_BGSAVE_START,也就是说刚完成的RDB不是有slave触发的。此处,会设置startbgsave为1,并设置slave的同步状态为REDIS_REPL_WAIT_BGSAVE_END,后面会开始一次新的RDB dump过程。

如果slave的状态是REDIS_REPL_WAIT_BGSAVE_END,即刚完成的RDB是由slave触发的。在这种情况下,只看disk方式。主要完成以下几件事:

- 检查RDB的成功状态,如果失败,会释放slave,让其超时后,进行同步重试

- 为每个slave打开RDB文件,如果有5个slave,则会打开该文件5次。不能共享的原因是,不同slave的状态不同,比如文件指针。

- 设置同步的属性,包括:设置同步状态为REDIS_REPL_SEND_BULK,即发生RDB的过程。

- 删除旧的写事件处理函数,注册新的写事件处理函数sendBulkToSlave,用于发送RDB。

    if (startbgsave) {        if (startBgsaveForReplication() != REDIS_OK) {            listIter li;            listRewind(server.slaves,&li);            redisLog(REDIS_WARNING,"SYNC failed. BGSAVE failed");            while((ln = listNext(&li))) {                redisClient *slave = ln->value;                if (slave->replstate == REDIS_REPL_WAIT_BGSAVE_START)                    freeClient(slave);            }        }    }

如果设置了startbgsave,就调用startBgsaveForReplication函数开始新的RDB dump。

master在发送RDB文件的格式:RDB文件长度+RDB文件内容。slave会首先接收长度,然后接收到对应长度的内容后就认为RDB文件接收完毕。下面看下具体函数

    redisClient *slave = privdata;    REDIS_NOTUSED(el);    REDIS_NOTUSED(mask);    char buf[REDIS_IOBUF_LEN];    ssize_t nwritten, buflen;    /* Before sending the RDB file, we send the preamble as configured by the     * replication process. Currently the preamble is just the bulk count of     * the file in the form "$<length>\r\n". */    if (slave->replpreamble) {        nwritten = write(fd,slave->replpreamble,sdslen(slave->replpreamble));        if (nwritten == -1) {            redisLog(REDIS_VERBOSE,"Write error sending RDB preamble to slave: %s",                strerror(errno));            freeClient(slave);            return;        }        server.stat_net_output_bytes += nwritten;        sdsrange(slave->replpreamble,nwritten,-1);        if (sdslen(slave->replpreamble) == 0) {            sdsfree(slave->replpreamble);            slave->replpreamble = NULL;            /* fall through sending data. */        } else {            return;        }    }
发送函数也类似状态机,如果当前有报头(preamble),则发送报头。当前报头就是在updateSlavesWaitingBgsave函数中设置的,就是RDB长度。

- 调用write输出

- 更新server.stat_net_output_bytes

- 同时调整slave->replpreamble buffer,截去已发送部分

- 如果slave->replpreamble为空,则释放其空间并置为NULL,然后继续发送RDB

接下来看发送RDB文件部分。

    /* If the preamble was already transfered, send the RDB bulk data. */    lseek(slave->repldbfd,slave->repldboff,SEEK_SET);    buflen = read(slave->repldbfd,buf,REDIS_IOBUF_LEN);    if (buflen <= 0) {        redisLog(REDIS_WARNING,"Read error sending DB to slave: %s",            (buflen == 0) ? "premature EOF" : strerror(errno));        freeClient(slave);        return;    }

slave->repldboff记录了当前RDB文件发送的偏移量。slave->repldbfd对应RDB文件打开的文件描述符。这里,首先定位到slave->repldboff指定的位置,然后调用read系统调用读取到buffer。指定的读取大小为REDIS_IOBUF_LEN,16KB,也就是说会以16KB的块去发送RDB文件。

如果read失败会导致此次发送失败,直接释放slave对应的客户端,会导致slave超时,然后重新发起同步过程。

    if ((nwritten = write(fd,buf,buflen)) == -1) {        if (errno != EAGAIN) {            redisLog(REDIS_WARNING,"Write error sending DB to slave: %s",                strerror(errno));            freeClient(slave);        }        return;    }

调用write将读到buffer发送给slave,同样出错后会导致slave重连。

    slave->repldboff += nwritten;    server.stat_net_output_bytes += nwritten;

以最终发送成功的数据长度去更新slave->repldboff,下一次事件循环会从该位置继续发送。更新统计信息server.stat_net_output_bytes。

    if (slave->repldboff == slave->repldbsize) {        close(slave->repldbfd);        slave->repldbfd = -1;        aeDeleteFileEvent(server.el,slave->fd,AE_WRITABLE);        putSlaveOnline(slave);    }

如果发送数据总长度等于slave->repldbsize,即表示RDB文件发送完毕。

- 关闭RDB文件对应的描述符

- 将slave->repldbfd置为-1

- 删除同步连接的写事件处理函数

- 调用putSlaveOnline函数,进行后续处理。

/* This function puts a slave in the online state, and should be called just * after a slave received the RDB file for the initial synchronization, and * we are finally ready to send the incremental stream of commands. * * It does a few things: * * 1) Put the slave in ONLINE state. * 2) Make sure the writable event is re-installed, since calling the SYNC *    command disables it, so that we can accumulate output buffer without *    sending it to the slave. * 3) Update the count of good slaves. */void putSlaveOnline(redisClient *slave) {    slave->replstate = REDIS_REPL_ONLINE;    slave->repl_put_online_on_ack = 0;    slave->repl_ack_time = server.unixtime;    if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,        sendReplyToClient, slave) == AE_ERR) {        redisLog(REDIS_WARNING,"Unable to register writable event for slave bulk transfer: %s", strerror(errno));        freeClient(slave);        return;    }    refreshGoodSlavesCount();    redisLog(REDIS_NOTICE,"Synchronization with slave %s succeeded",        replicationGetSlaveName(slave));}
这个函数主要做一下几件事:

- 更新slave的同步状态REDIS_REPL_ONLINE

- 将repl_put_online_on_ack置为0,只有以socket方式同步才需要置为1

- 更新repl_ack_time,防止slave超时,被干掉

- 注册连接写事件处理函数。这个处理函数与响应客户端请求的一致,因为master会将修改请求的序列化buffer原封不动拷贝到slave的output buffer。

- 重新计算活跃的slave的个数

完成这个函数之后,master端就已经建立好同步过程,之后master会源源不断地将其接受的写请求发送给slave。

5. Slave接收RDB文件

在上文介绍slave发起同步请求的最后,slave会注册socket写事件处理函数readSyncBulkPayload,用于接收master发送的RDB文件,下面就看一下这个函数的具体实现

    char buf[4096];    ssize_t nread, readlen;    off_t left;    REDIS_NOTUSED(el);    REDIS_NOTUSED(privdata);    REDIS_NOTUSED(mask);    /* Static vars used to hold the EOF mark, and the last bytes received     * form the server: when they match, we reached the end of the transfer. */    static char eofmark[REDIS_RUN_ID_SIZE];    static char lastbytes[REDIS_RUN_ID_SIZE];    static int usemark = 0;
声明各种变量。

    /* If repl_transfer_size == -1 we still have to read the bulk length     * from the master reply. */    if (server.repl_transfer_size == -1) {        if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) {            redisLog(REDIS_WARNING,                "I/O error reading bulk count from MASTER: %s",                strerror(errno));            goto error;        }        if (buf[0] == '-') {            redisLog(REDIS_WARNING,                "MASTER aborted replication with an error: %s",                buf+1);            goto error;        } else if (buf[0] == '\0') {            /* At this stage just a newline works as a PING in order to take             * the connection live. So we refresh our last interaction             * timestamp. */            server.repl_transfer_lastio = server.unixtime;            return;        } else if (buf[0] != '$') {            redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$' (we received '%s'), are you sure the host and port are right?", buf);            goto error;        }        /* There are two possible forms for the bulk payload. One is the         * usual $<count> bulk format. The other is used for diskless transfers         * when the master does not know beforehand the size of the file to         * transfer. In the latter case, the following format is used:         *         * $EOF:<40 bytes delimiter>         *         * At the end of the file the announced delimiter is transmitted. The         * delimiter is long and random enough that the probability of a         * collision with the actual file content can be ignored. */        if (strncmp(buf+1,"EOF:",4) == 0 && strlen(buf+5) >= REDIS_RUN_ID_SIZE) {            usemark = 1;            memcpy(eofmark,buf+5,REDIS_RUN_ID_SIZE);            memset(lastbytes,0,REDIS_RUN_ID_SIZE);            /* Set any repl_transfer_size to avoid entering this code path             * at the next call. */            server.repl_transfer_size = 0;            redisLog(REDIS_NOTICE,                "MASTER <-> SLAVE sync: receiving streamed RDB from master");        } else {            usemark = 0;            server.repl_transfer_size = strtol(buf+1,NULL,10);            redisLog(REDIS_NOTICE,                "MASTER <-> SLAVE sync: receiving %lld bytes from master",                (long long) server.repl_transfer_size);        }        return;    }

接收RDB文件的过程也是一个状态机,master发送的格式:RDB文件长度+RDB数据。server.repl_transfer_size记录需要从master接收的RDB文件的长度。在注册readSyncBulkPayload函数时,将其置为-1。

上段代码就是会判断server.repl_transfer_size是否为-1。如果是,则读取RDB文件长度,具体过程:

- 调用syncReadLine,同步阻塞的读取一行数据

- 对读取到的buffer判断是否有错。正确的响应应该是以$为前缀

- 将读取的数据转化为整型,并赋值给server.repl_transfer_size

    /* Read bulk data */    if (usemark) {        readlen = sizeof(buf);    } else {        left = server.repl_transfer_size - server.repl_transfer_read;        readlen = (left < (signed)sizeof(buf)) ? left : (signed)sizeof(buf);    }

计算此次读取的数据长度。server.repl_transfer_read记录以读取的数据长度。会以4KB的块进行读取

    nread = read(fd,buf,readlen);    if (nread <= 0) {        redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",            (nread == -1) ? strerror(errno) : "connection lost");        replicationAbortSyncTransfer();        return;    }    server.stat_net_input_bytes += nread;

调用read函数读取,成功后更新统计信息server.stat_net_input_bytes。如果失败,会调用replicationAbortSyncTransfer函数,终止此次同步过程,会将同步状态重置为REDIS_REPL_CONNECT,重新尝试同步。

    /* When a mark is used, we want to detect EOF asap in order to avoid     * writing the EOF mark into the file... */    int eof_reached = 0;    if (usemark) {        /* Update the last bytes array, and check if it matches our delimiter.*/        if (nread >= REDIS_RUN_ID_SIZE) {            memcpy(lastbytes,buf+nread-REDIS_RUN_ID_SIZE,REDIS_RUN_ID_SIZE);        } else {            int rem = REDIS_RUN_ID_SIZE-nread;            memmove(lastbytes,lastbytes+nread,rem);            memcpy(lastbytes+rem,buf,nread);        }        if (memcmp(lastbytes,eofmark,REDIS_RUN_ID_SIZE) == 0) eof_reached = 1;    }    server.repl_transfer_lastio = server.unixtime;    if (write(server.repl_transfer_fd,buf,nread) != nread) {        redisLog(REDIS_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchronization: %s", strerror(errno));        goto error;    }    server.repl_transfer_read += nread;

开始部分是使用socket模式,暂时略过。

- 更新repl_transfer_lastio,防止被超时

- 调用write写入临时RDB文件

- 更新repl_transfer_read统计信息

    /* Delete the last 40 bytes from the file if we reached EOF. */    if (usemark && eof_reached) {        if (ftruncate(server.repl_transfer_fd,            server.repl_transfer_read - REDIS_RUN_ID_SIZE) == -1)        {            redisLog(REDIS_WARNING,"Error truncating the RDB file received from the master for SYNC: %s", strerror(errno));            goto error;        }    }    /* Sync data on disk from time to time, otherwise at the end of the transfer     * we may suffer a big delay as the memory buffers are copied into the     * actual disk. */    if (server.repl_transfer_read >=        server.repl_transfer_last_fsync_off + REPL_MAX_WRITTEN_BEFORE_FSYNC)    {        off_t sync_size = server.repl_transfer_read -                          server.repl_transfer_last_fsync_off;        rdb_fsync_range(server.repl_transfer_fd,            server.repl_transfer_last_fsync_off, sync_size);        server.repl_transfer_last_fsync_off += sync_size;    }

将已写到page cache中的数据分块sync到磁盘,块大小为8MB

    /* Check if the transfer is now complete */    if (!usemark) {        if (server.repl_transfer_read == server.repl_transfer_size)            eof_reached = 1;    }

判断是否读完整个RDB文件。就是用已读数据的大小repl_transfer_read与RDB文件大小做比对。如果读完,将eof_reached置为1.

下面是读取完整个RDB文件之后的操作,分步骤进行介绍。

    if (eof_reached) {        if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) {            redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));            replicationAbortSyncTransfer();            return;        }        redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Flushing old data");

将临时文件重命名为对应的RDB文件名。

        signalFlushedDb(-1);        emptyDb(replicationEmptyDbCallback);
清空所有db。

        /* Before loading the DB into memory we need to delete the readable         * handler, otherwise it will get called recursively since         * rdbLoad() will call the event loop to process events from time to         * time for non blocking loading. */        aeDeleteFileEvent(server.el,server.repl_transfer_s,AE_READABLE);        redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Loading DB in memory");
删除同步连接的写事件处理函数

        if (rdbLoad(server.rdb_filename) != REDIS_OK) {            redisLog(REDIS_WARNING,"Failed trying to load the MASTER synchronization DB from disk");            replicationAbortSyncTransfer();            return;        }
调用rdbLoad加载RDB文件

        /* Final setup of the connected slave <- master link */        zfree(server.repl_transfer_tmpfile);        close(server.repl_transfer_fd);

清理资源,是否临时文件名占用的内存,关闭其文件描述符。

        server.master = createClient(server.repl_transfer_s);        server.master->flags |= REDIS_MASTER;        server.master->authenticated = 1;        server.repl_state = REDIS_REPL_CONNECTED;        server.master->reploff = server.repl_master_initial_offset;        memcpy(server.master->replrunid, server.repl_master_runid,            sizeof(server.repl_master_runid));

创建master对应的client数据结构,同时会注册读事件处理函数,接收master发送过来的backlog。设置已写属性:

- 将client的flags标记为REDIS_MASTER

- 设置为已经验证

- 同步状态为REDIS_REPL_CONNECTED

- 设置同步backlog的偏移量

- 设置master的runid

        /* If master offset is set to -1, this master is old and is not         * PSYNC capable, so we flag it accordingly. */        if (server.master->reploff == -1)            server.master->flags |= REDIS_PRE_PSYNC;        redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Finished with success");

按条件设置client的flags为REDIS_PRE_PSYNC。

        /* Restart the AOF subsystem now that we finished the sync. This         * will trigger an AOF rewrite, and when done will start appending         * to the new file. */        if (server.aof_state != REDIS_AOF_OFF) {            int retry = 10;            stopAppendOnly();            while (retry-- && startAppendOnly() == REDIS_ERR) {                redisLog(REDIS_WARNING,"Failed enabling the AOF after successful master synchronization! Trying it again in one second.");                sleep(1);            }            if (!retry) {                redisLog(REDIS_WARNING,"FATAL: this slave instance finished the synchronization with its master, but the AOF can't be turned on. Exiting now.");                exit(1);            }        }    }    return;

完成AOF相关的任务。

error:    replicationAbortSyncTransfer();    return;

失败处理分支。执行replicationAbortTransfer函数停止此次同步。

6. Master端backlog处理

当主从同步搭建之后,master需要将实时更新同步到slave,同时为了支持半同步,master需要维护一个back log用于缓存部分更新。在介绍请求处理时,提到过在每个命令都会由call函数处理,其中会调用propagate处理与aof和同步相关的逻辑:

/* Propagate the specified command (in the context of the specified database id) * to AOF and Slaves. * * flags are an xor between: * + REDIS_PROPAGATE_NONE (no propagation of command at all) * + REDIS_PROPAGATE_AOF (propagate into the AOF file if is enabled) * + REDIS_PROPAGATE_REPL (propagate into the replication link) */void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,               int flags){    if (server.aof_state != REDIS_AOF_OFF && flags & REDIS_PROPAGATE_AOF)        feedAppendOnlyFile(cmd,dbid,argv,argc);    if (flags & REDIS_PROPAGATE_REPL)        replicationFeedSlaves(server.slaves,dbid,argv,argc);}

调用replicationFeedSlaves函数将更新传递给slave。这个函数完成两件事,一是填充back log,二是给slave发送实时更新的命令。

    listNode *ln;    listIter li;    int j, len;    char llstr[REDIS_LONGSTR_SIZE];    /* If there aren't slaves, and there is no backlog buffer to populate,     * we can return ASAP. */    if (server.repl_backlog == NULL && listLength(slaves) == 0) return;    /* We can't have slaves attached and no backlog. */    redisAssert(!(listLength(slaves) != 0 && server.repl_backlog == NULL));

声明变量,检查是否需要继续操作。如果没有slave并且back log为NULL就不需要继续执行。

这一篇已经够长了,维护同步过程的其他逻辑将在下一篇介绍。。。。。


0 0
原创粉丝点击