redis aof命令缓冲区的写入源码

需要通过aof记录的命令主要为set命令和过期命令,这些命令在执行完毕后需要记录到aof缓冲区中以便写入到aof文件中。

sds aof_buf;      /* AOF buffer, written before entering the event loop */

在结构体redisServer中通过一个sds来作为aof的缓冲区。

 

Redis服务器在执行完相应的命令的时候,如果开启了aof功能,就会在feedAppendOnlyFile()函数中准备将命令添加至aof缓冲区当中。

void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc) {
    sds buf = sdsempty();
    robj *tmpargv[3];

    /* The DB this command was targeting is not the same as the last command
     * we appended. To issue a SELECT command is needed. */
    if (dictid != server.aof_selected_db) {
        char seldb[64];

        snprintf(seldb,sizeof(seldb),"%d",dictid);
        buf = sdscatprintf(buf,"*2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
            (unsigned long)strlen(seldb),seldb);
        server.aof_selected_db = dictid;
    }

    if (cmd->proc == expireCommand || cmd->proc == pexpireCommand ||
        cmd->proc == expireatCommand) {
        /* Translate EXPIRE/PEXPIRE/EXPIREAT into PEXPIREAT */
        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);
    } else if (cmd->proc == setexCommand || cmd->proc == psetexCommand) {
        /* Translate SETEX/PSETEX to SET and PEXPIREAT */
        tmpargv[0] = createStringObject("SET",3);
        tmpargv[1] = argv[1];
        tmpargv[2] = argv[3];
        buf = catAppendOnlyGenericCommand(buf,3,tmpargv);
        decrRefCount(tmpargv[0]);
        buf = catAppendOnlyExpireAtCommand(buf,cmd,argv[1],argv[2]);
    } else if (cmd->proc == setCommand && argc > 3) {
        int i;
        robj *exarg = NULL, *pxarg = NULL;
        /* Translate SET [EX seconds][PX milliseconds] to SET and PEXPIREAT */
        buf = catAppendOnlyGenericCommand(buf,3,argv);
        for (i = 3; i < argc; i ++) {
            if (!strcasecmp(argv[i]->ptr, "ex")) exarg = argv[i+1];
            if (!strcasecmp(argv[i]->ptr, "px")) pxarg = argv[i+1];
        }
        serverAssert(!(exarg && pxarg));
        if (exarg)
            buf = catAppendOnlyExpireAtCommand(buf,server.expireCommand,argv[1],
                                               exarg);
        if (pxarg)
            buf = catAppendOnlyExpireAtCommand(buf,server.pexpireCommand,argv[1],
                                               pxarg);
    } else {
        /* All the other commands don't need translation or need the
         * same translation already operated in the command vector
         * for the replication itself. */
        buf = catAppendOnlyGenericCommand(buf,argc,argv);
    }

    /* Append to the AOF buffer. This will be flushed on disk just before
     * of re-entering the event loop, so before the client will get a
     * positive reply about the operation performed. */
    if (server.aof_state == AOF_ON)
        server.aof_buf = sdscatlen(server.aof_buf,buf,sdslen(buf));

    /* If a background append only file rewriting is in progress we want to
     * accumulate the differences between the child DB and the current one
     * in a buffer, so that when the child process will do its work we
     * can append the differences to the new append only file. */
    if (server.aof_child_pid != -1)
        aofRewriteBufferAppend((unsigned char*)buf,sdslen(buf));

    sdsfree(buf);
}

首先,判断该命令的执行dbid是否是和当前的dbid一致,如果不一致,需要额外写入select命令切换到命令所执行的dbid上。

 

而后,如果该命令属于expire,perpire, expireat这三个纯粹设置过期时间的命令,直接通过catAppendOnlyExpireAtCommand()方法转换成过期命令写到结果sds中。

 

对于setex和psetex两个存放和过期键同时设置的命令,首先根据这两条命令的格式:

SETEX key seconds value取到第一个和第三个参数,通过catAppendOnlyGenericCommand()方法转换成set命令写到aof缓冲区中,再根据第一个参数和第二个参数组成pexpireat命令写到结果sds中,在这里被转换成了两条命令。

 

而如果是set [ex seconds] [px millisecond] 形式的命令,则根据参数数数量来确定。

如果是这样的命令,也是拆分成set部分和pexpireat部分拆分为多段命令。

 

如果只是简单的set命令,就简单通过catAppendOnlyGenericCommand()方法插入结果sds。

 

 

在完成了上述操作后,判断如果开启了aof,那么则将结果sds写入到server的aof缓冲区中。

如果此时redis正有子进程进行重写操作,那么直接将新的sds加入到重写的内容中。

最后清空结果sds以节约空间。

sds catAppendOnlyGenericCommand(sds dst, int argc, robj **argv) {
    char buf[32];
    int len, j;
    robj *o;

    buf[0] = '*';
    len = 1+ll2string(buf+1,sizeof(buf)-1,argc);
    buf[len++] = '\r';
    buf[len++] = '\n';
    dst = sdscatlen(dst,buf,len);

    for (j = 0; j < argc; j++) {
        o = getDecodedObject(argv[j]);
        buf[0] = '$';
        len = 1+ll2string(buf+1,sizeof(buf)-1,sdslen(o->ptr));
        buf[len++] = '\r';
        buf[len++] = '\n';
        dst = sdscatlen(dst,buf,len);
        dst = sdscatlen(dst,o->ptr,sdslen(o->ptr));
        dst = sdscatlen(dst,"\r\n",2);
        decrRefCount(o);
    }
    return dst;
}

catAppendOnlyGenericCommand()方法用来将set命令转换为sds以便写入到aof缓冲区中,每次在写命令的开头,都会计算包括命令和参数的个数总和,加上*号写在命令上一行。同时每次在写具体值之前,都要计算写入的参数的具体长度,使用$加在前一行。作为解析对象的o每次在使用玩一次,都会通过decrRefCount()方法减少一次引用次数便于回收。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Redis AOF是一种持久化机制,用于将Redis服务器的操作日志以追加的方式记录在磁盘上。引用中提到,当AOF文件的体积变得过大时,Redis可以自动进行AOF重写。AOF重写实际上是对当前数据集所需的最小命令集合进行重写,而不是对原有的AOF文件进行写入和读取操作。这个重写过程是在后台进行的,通过fork子进程来执行。重写后的新AOF文件能够恢复当前数据集的状态。 引用中提到,RedisAOF重写程序放在后台子进程中执行的原因是为了避免影响服务器的请求处理能力。通过将AOF重写程序放在后台执行,可以确保服务器能够继续处理请求,而不会被AOF重写过程阻塞。 在Redis服务器中,redisServer结构维护着服务器的状态,而aof_buf域则用来保存等待写入AOF文件的协议文本(RESP)。这些协议文本包含了对键的操作命令,用于记录服务器的操作日志。当需要将操作日志写入AOF文件时,这些协议文本会被写入AOF缓冲区中,然后由后台的AOF子进程负责将缓冲区中的内容写入AOF文件中。这种方式可以提高性能,并且减少了直接写入文件的开销。引用中提到了这一点。 综上所述,RedisAOF机制是通过记录操作日志来实现数据持久化的。当AOF文件体积过大时,Redis会自动进行AOF重写,将当前数据集所需的最小命令集合写入一个新的AOF文件中。为了避免影响服务器的请求处理能力,AOF重写过程会在后台执行。通过维护一个AOF缓冲区Redis可以将待写入的协议文本暂时保存在内存中,然后由后台子进程负责将其写入AOF文件中。这种机制可以提高性能并减少直接写入文件的开销。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值