Quantcast
Channel: 趁着年轻 »分布式
Viewing all articles
Browse latest Browse all 3

Redis 2.8版部分同步功能源码浅析-Replication Partial Resynchronization

$
0
0

前面的2篇文章分别介绍了Redis主从同步源码浅析-Master端 以及 Redis主从同步源码浅析-Slave端 相关的代码实现,从中我们可以看出redis主从同步的一个最大的缺点,也是阻碍大数据应用的地方便是其每次连接端开都需要重连master进行全量数据的重新同步,这个代价是可想而知的。

长连接断开在线上环境中出现得很频繁,如果需要重新同步所有RDB文件,几十G的文件,从建立RDB快照,发送文件内容到slave,然后slave执行命令一一加载进内存中,这个时间开销估计也得好几个小时,更别说树形结构的master->slave->slave, 对网卡的压力,对服务器的压力都是很恐怖的。从这方面来说,动辄几个小时甚至一天的修复时间,没人敢用Redis主从同步在生产环境中使用。

但是福音来了:即将(2013年第三季度)发布的2.8版本会解决这个问题,通过:Replication partial resynchronization 的方式,也就是部分重新同步,这里就说部分同步吧,注意不是常规情况下的新写入指令同步。

具体的增量同步功能请看作者在刚开始的想法(Designing Redis replication partial resync) 和 中间的(Partial resyncs and synchronous replication.) 以及最后的想法(PSYNC),从这里可以知道redis的部分同步功能很详细的解说。所以就不多说了,只是下面简单总结一下方便后面分析代码。

注意本文列出的代码是目前的最新代码,不是2.8版本的代码·https://github.com/antirez/redis

零、Partial Resynchronization介绍

为了避免每次重连都需要重新全量同步RDB文件,redis采用类似mysql的backlog的方式,允许slave在一定的时间内进行部分同步,只同步自己需要的部分回去,已经有的不需要同步了。注意如果重启了,那还是得重新同步,这个其实也有点悲剧,不知道后续会不会加入这个功能,实现也不太难的。

简单来讲,用口语就是:

  1. 对于slave :master兄,我刚刚走神了断了连接,得重新找你同步一下。如果你还是昔日的那个replrunid, 我刚才同步到的位置是这里reploff,如果还有机会请把我落下的数据马上发给我一下;否则请给我全部RDB文件;
  2. 对于master: slave们,如果你断了连接,请最好给我你刚才记着的runid和你算的同步到的位置发送给我,我看看是不是可以只让你同步你落下的部分;否则你得全量同步RDB文件。

根据这个设计,可想而知,master必须记住一定数目的backlog,也就是记住一段时间内的发送给slave们的命令列表,以及其起始,结束为止。slave必须在连接端开的时候记着自己同步到了什么位置,重连的时候用这位置去问master,自己是否还有机会赶上来。

一、SLAVE发起部分同步请求

大部分跟之前的2.6版本同步差不多:

  1. 标记server.repl_state为 REDIS_REPL_CONNECT状态;
  2. replicationCron定时任务检测到调用connectWithMaster函数连接master;
  3. slave连接成功调用syncWithMaster,发送PING指令;
  4. slave发送SYNC指令通知master做RDB快照;
  5. 接收master的RDB快照文件;
  6. 加载新数据;

在2.8版本部分同步的时候,将上面的第4步修改了,加入了发送PSYNC指令尝试部分同步的功能。调用slaveTryPartialResynchronization函数尝试部分同步,如果发现master不认识这个指令,那就没办法了,再次发送SYNC进行全量同步。

void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
//·····
    /* 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);
    if (psync_result == PSYNC_CONTINUE) {
        redisLog(REDIS_NOTICE, "MASTER <-> SLAVE sync: Master accepted a Partial Resynchronization.");
        return;
    }

    /* 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;
        }
    }

slaveTryPartialResynchronization是像master发送PSYNC指令的地方。PSYNC指令的语法为:PSYNC runid psync_offset 。下面解释一下2个参数的含义。

runid就是master告诉slave的一串字符串,用来记录master的实例,避免master重启后,同步错误的情况,这个值是master在slave第一次同步的时候告诉他的,且一直不变直到master重启;

psync_offset这个参数就是slave当前同步到的数据位置,实际上是同步了多少数据,以字节为单位。master根据这个来决定是否可以增量同步以及发送哪些数据给slave。第一次同步的时候master会告诉他的。以后slave每次收到从master过来的连接后,都会增加读取的数据长度 到这个值,保存在c->reploff上面。
下面是发送PSYNC指令的代码。

int slaveTryPartialResynchronization(int fd) {
    char *psync_runid;
    char psync_offset[32];
    sds reply;

    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);
    }

    /* Issue the PSYNC command */
    reply = sendSynchronousCommand(fd,"PSYNC",psync_runid,psync_offset,NULL);

收到PSYNC指令后,master如果觉得可以进行增量同步,则会返回”+CONTINUE”, 如果必须进行全量同步,会返回”+FULLRESYNC”, 否则ERROR,这里具体待会介绍master的时候介绍。

1.只能进行全量同步

来看看如果必须进行全量同步的情况,这种情况下master会返回”+FULLRESYNC  runid offset” 给slave。虽然得全量,但是还会告诉slave runid是多少,以及当前master的backlog   offset位置,这样让slave下回来同步的时候能够进行部分同步。也算是互相沟通一下状态。

slave收到”+FULLRESYNC”结果后,会将runid保存到server.repl_master_runid上面,backlog offset位置放在server.repl_master_initial_offset里面。以便后面使用部分同步功能。读取完RDB文件后会设置到server.master->reploff上的。

注意PSYNC如果只能进行全量同步,master自己会做RDB快照的,不需要再次发送SYNC。看下面的代码:

int slaveTryPartialResynchronization(int fd) {
//`````
    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;
    }
//····
}

void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
//````
        server.master->reploff = server.repl_master_initial_offset;
        memcpy(server.master->replrunid, server.repl_master_runid,
            sizeof(server.repl_master_runid));
//·····
}

2.可以进行部分同步

如果master返回”+CONTINUE”,那就可以进行部分同步。这个比较简单,继续接收后面的数据 就行了。

3.发生错误

这个时候可能是master是老版本,不认识PSYNC,或者发生其他错误了,那就重新发送SYNC指令进行全量同步就行。

到这里还剩下几个问题,第一个是如果连接断开了,slave怎么记住master的runid和reploff位置的呢?

这个可以参考replicationCacheMaster,freeClient在断开一个连接的时候,会判断这个是不是master的连接,如果是,会调用replicationCacheMaster,将当前的状态cache住,并且断开跟本slave的下一级slave的连接。

void replicationCacheMaster(redisClient *c) {
//····
    /* Save the master. Server.master will be set to null later by
     * replicationHandleMasterDisconnection(). */
    server.cached_master = server.master;
//···
    replicationHandleMasterDisconnection();
}

下一个问题是slave的c->reploff如何保持跟master同步,因为他们必须绝对一致才行。

这个是通过在2端完成,双方只要是发送给对方的指令,都会讲指令的总长度加在offset上面,slave在readQueryFromClient读取连接数据的时候增加这个值。master在replicationFeedSlaves函数里面会调用feedReplicationBacklogWithObject,后者最终调用feedReplicationBacklog,进而调整offset和backlog,这个待会介绍。

void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) {
//····
    if (nread) {
        sdsIncrLen(c->querybuf,nread);
        c->lastinteraction = server.unixtime;
        if (c->flags & REDIS_MASTER) c->reploff += nread;
    }
//····
}

到这里slave部分介绍完毕。下一部分master端。

二、MASTER接收处理PSYNC指令

在master端,SYNC和PSYNC的处理函数都是syncCommand。只是增量了一段代码检测PSYNC指令,如果是,就会调用masterTryPartialResynchronization尝试部分同步,如果不能进行部分同步,那就按照SYNC的方式处理,也就是进行全量同步,这个请参考“Redis主从同步源码浅析-Slave端”。

void syncCommand(redisClient *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_SLAVE;
    }
//````
}

masterTryPartialResynchronization函数处理部分同步的检查。

首先检查runid是否匹配,如果不匹配那说明master重启过了,必须全量,调转到goto need_full_resync;

如果psync_offset 介于server.repl_backlog_off 和server.repl_backlog_off + server.repl_backlog_size 之间的话,那说明slave已经同步到的位置正好在我么的backlog之间,那说明他落下的东西master是记录在backlog里面的!good,可以进行增量同步。

int masterTryPartialResynchronization(redisClient *c) {
    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.");
        }
        goto 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_size))
//上面这一行的计算我看有点问题,应该用将repl_backlog_size替换为repl_backlog_histlen,因为后者才是代表实际数据长度。
 {
 redisLog(REDIS_NOTICE,
 "Unable to partial resync with the slave for lack of backlog (Slave request was: %lld).", psync_offset);
 goto need_full_resync;
 }

下面进行增量同步的工作包括:将这个连接加到server.slaves里面,然后给slave发送”+CONTINUE\r\n”告诉他“没事,你还可以赶得上”,然后使用addReplyReplicationBacklog把他落下的部分数据放到他的发送缓冲区中。

    /* 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;
    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
     * emtpy 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;
    }
    psync_len = addReplyReplicationBacklog(c,psync_offset);

这样slave收到”+CONTINUE\r\n”后就会像正常情况一样接收master发送过来的数据,并且移动其c->reploff指针,部分同步开始。其实部分同步就是将落下的部分放到发送缓冲区发送给slave的事情。

关于addReplyReplicationBacklog函数就不多介绍了,里面是关于循环的backlog的处理,找出slave落下的数据,用addReplySds放到其缓冲区中准备发送。

如果不能进行部分同步,只能全部同步的话,master会附带将当前master的状态发送给slave。如下代码,用”+FULLRESYNC %s %lld\r\n”指令发送过去。

int masterTryPartialResynchronization(redisClient *c) {
//·····
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;
}

到这里基本结束了,关于addReplyReplicationBacklog函数,其工作是将slave落下的backlog缓冲数据发送给slave。代码跟feedReplicationBacklog类似,后者的功能是往backlog填入数据,这里指介绍feedReplicationBacklog作为例子,介绍一下backlog。

redis的backlog是个循环的buffer,跟mysql不一样。其数据保存在server.repl_backlog 指针里面。下面分别介绍几个关键变量:

struct redisServer {
//····
//master_repl_offset用来记录当前master发送给slave的所有数据的位置,字节为单位,其实就是发送一个命令增加相应字节,不断移动。
    long long master_repl_offset;   /* Global replication offset */
//backlog 数据保存在这个数组里面,可以由repl-backlog-size配置项配置。
    char *repl_backlog;             /* Replication backlog for partial syncs */
//也就是配置的repl-backlog-size,表示backlog缓冲区大小。默认为REDIS_DEFAULT_REPL_BACKLOG_SIZE = 1M,好小啊。
    long long repl_backlog_size;    /* Backlog circular buffer size */
//backlog缓冲区中的有效数据大小。开始的时候小于repl_backlog_size,但缓冲区满后就一直等于repl_backlog_size了。
    long long repl_backlog_histlen; /* Backlog actual data length */
//这个是说backlog指针指向的数组中,有效数据的起始位置,从0开始。
    long long repl_backlog_idx;     /* Backlog circular buffer current offset */
//这个表示backlog数据中,起始位置也就是上面的idx变量所指的位置的offset,一般等于master_repl_offset减去repl_backlog_histlen,
//slave发送过来的offset只要大于这个值,说明slave落下的数据在backlog中,否则说明slave连接端开的太久了,已经没法找到历史记录了。
    long long repl_backlog_off;     /* Replication offset of first byte in the
}

通过上面的字段介绍,应该基本猜出feedReplicationBacklog往backlog填充最新要发给slave的数据的代码了。

/* Add data to the replication backlog.
 * This function also increments the global replication offset stored at
 * server.master_repl_offset, because there is no case where we want to feed
 * the backlog without incrementing the buffer. */
void feedReplicationBacklog(void *ptr, size_t len) {
    unsigned char *p = ptr;

    server.master_repl_offset += len;

    /* This is a circular buffer, so write as much data we can at every
     * iteration and rewind the "idx" index if we reach the limit. */
    while(len) {
        size_t thislen = server.repl_backlog_size - server.repl_backlog_idx;
        if (thislen > len) thislen = len;
        memcpy(server.repl_backlog+server.repl_backlog_idx,p,thislen);
        server.repl_backlog_idx += thislen;
        if (server.repl_backlog_idx == server.repl_backlog_size)
            server.repl_backlog_idx = 0;
        len -= thislen;
        p += thislen;
        server.repl_backlog_histlen += thislen;
    }
    if (server.repl_backlog_histlen > server.repl_backlog_size)
        server.repl_backlog_histlen = server.repl_backlog_size;
    /* Set the offset of the first byte we have in the backlog. */
    server.repl_backlog_off = server.master_repl_offset -
                              server.repl_backlog_histlen + 1;
}

注意函数开头对server.master_repl_offset的赋值,以及对server.repl_backlog_off的设置,这2个值的差就是backlog中的有效数据长度。并且master所有发给slave的指令,除了同步的基本指令外,都会增加这个计数。

同样对于slave,其每次从master收到的数据,也都会相应的在readQueryFromClient里面增加c->reploff的计数。这样master-slave对于offset就能保持一致,这就是其使用backlog通信的保证。

三、总结

很完美,master-slave能够部分同步了,这样避免了每次连接断开都需要进行全量同步的弊端。

不过redis2.8版本代码还没有发布,所以这里只是提前预告一下其功能,估计在这个季度就能发布了,目前已经比较稳定了。

不过还有一个个人觉得可能很有用的功能,那就是支持slave重启后部分同步的功能。目前重启后必须重新同步的。实现的话可以考虑在RDB文件和AOF文件写入的时候,同时增加一个文件记录对应的时刻slave上的runrid和c->reploff 值。启动的时候读取这个值就可以了。

同样的道理,支持master重启后不用重新同步所有数据,实现应该不难,类似的保存runid和offset等数据就行了。不然的话,生产环境中重启这种事情在所难免的。

如果能有这个功能就完美了,实现不难,不过很可能得泡汤了···

改天看看redis正在开发的功能:Redis Cluster

Related Posts

Share


Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images