Redis一条命令的执行 – AOF write 源码分析

本文详细解读了Redis AOF日志的实现原理,涉及CALL()方法、propagate()函数及feedAppendOnlyFile()函数的工作流程,展示了如何将命令转化为AOF文件格式,并介绍了特殊字符处理机制。
摘要由CSDN通过智能技术生成

Redis一条命令的执行 – AOF源码分析

(本文章基于redis 5.0)
aof的配置比较简单,熟练的DBA都知道通过如下方式配置

appendonly no  # aof开关,默认关闭
appendfilename "appendonly.aof"  # 保存的文件名,默认appendonly.aof
# 有三种刷数据的策略
appendfsync always  # always是只要有数据改动,就把数据刷到磁盘里,最安全但性能也最差
appendfsync everysec  # 每隔一秒钟刷一次数据,数据安全性和性能折中,这也是redis默认和推荐的配置。 
appendfsync no # 不主动刷,什么时候数据刷到磁盘里取决于操作系统,在大多数Linux系统中每30秒提交一次,性能最好,但数据安全性最差。

AOF日志的又是怎么实现的呢?如下是aof文件的代码分析

AOF 代码分析

redis命令的执行,依赖于server.c文件的CALL()方法.
server.c中的void call(client *c, int flags)是redis接受到client请求后处理请求的入口,其中会检测Redis中的数据有没有发生变化。如果有变化就会执行propagate()函数。
关于CALL()方法的描述,在代码的备注中有如下描述:

* Call() is the core of Redis execution of a command.
* 在执行call方法时,通过如下过程调用执行command
{

...

   /* Call the command. */
    dirty = server.dirty;
    updateCachedTime(0);
    start = server.ustime;
    c->cmd->proc(c);           // 这里执行命令
    duration = ustime()-start;
    dirty = server.dirty-dirty;
    if (dirty < 0) dirty = 0;

...

}

传递到propagate()函数后,做了什么事情呢?
这个函数在代码的备注中,意思是说在指定的数据库ID的背景下,传播指定的命令到AOF和Slave。也就是它负责把命令写到aof中,同时去做主从复制。

void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
               int flags)
{
    if (server.aof_state != AOF_OFF && flags & PROPAGATE_AOF)
        feedAppendOnlyFile(cmd,dbid,argv,argc);
    if (flags & PROPAGATE_REPL)
        replicationFeedSlaves(server.slaves,dbid,argv,argc);
}

这个文章就只看第一个函数: feedAppendOnlyFile(cmd,dbid,argv,argc);

AOF日志的生成


void feedAppendOnlyFile(int dictid, robj **argv, int argc) {
    sds buf = sdsempty();

    serverAssert(dictid >= 0 && dictid < server.dbnum);

    /* Feed timestamp if needed */
    if (server.aof_timestamp_enabled) {
        sds ts = genAofTimestampAnnotationIfNeeded(0);
        if (ts != NULL) {
            buf = sdscatsds(buf, ts);
            sdsfree(ts);
        }
    }

    /* The DB this command was targeting is not the same as the last command
     * we appended. To issue a SELECT command is needed. */
    /*如果你执行的命令跟当前的db是不一致的,要写select 命令*/ 
    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;
        serverLog(LL_NOTICE,"此处为代码测试_serverlog,aof select命令 *2\r\n$6\r\nSELECT\r\n$%lu\r\n%s\r\n",
            (unsigned long)strlen(seldb),seldb);
            // 固定格式 *2 作为第一行,$6作为第二行,SELECT,$长度,$值
    }

    /* All commands should be propagated the same way in AOF as in replication.
     * No need for AOF-specific translation. */
    /* 所有命令在AOF中的传播方式应与复制中的传播方式相同。不需要针对AOF的翻译。 */
    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.child_type == CHILD_TYPE_AOF)
    {
        server.aof_buf = sdscatlen(server.aof_buf, buf, sdslen(buf));
    }

    sdsfree(buf);
}

sds catAppendOnlyGenericCommand(sds dst, int argc, robj **argv) {
    // sds是redis自己针对string做的修改
    // 是一个string api
    // 参考大佬的文章 https://blog.csdn.net/weixin_39993322/article/details/112115086
    char buf[32];
    int len, j;
    robj *o;

    buf[0] = '*';       // 每个命令以* 开头,*后面的数字表示此次命令一共会有几个字段,
                        // $表示要写入的数据(要以string写入到文件中),$后面的数字表示当前的这个string的长度
                        // 比如 set a 1 表示为*3  set为$3 a为$1 1为$1
                        // *3
                        // $3
                        // set
                        // $1
                        // a
                        // $1
                        // 1
                        // SET key-with-expire-time "hello" EX 1008612 表示为
                        // *5
                        // $3
                        // SET
                        // $20
                        // key-with-expire-time
                        // $5
                        // hello
                        // $4
                        // PXAT
                        // $13
                        // 1651130525053

    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]);  // 这里解析当前函数接收到的内容 o是一个 robj结构(redis自己实现的 RedisObject ),
                                        // 可以保证命令执行前验证是否正确,不正确解析的时候会报错err
        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);
    }
    /* 上方代码实现的作用: */
    /* dst为要写入aof的文件的内容 */
    /*  */
    // serverLog(LOG_NOTICE,"dst这里返回的是:%s",dst);
    return dst;
}

AOF文件规则总结

每个命令以* 开头,*后面的数字表示此次命令一共会有几个字段,
$表示要写入的数据(要以string写入到文件中),$后面的数字表示当前的这个string的长度
比如 

1、set a 1 表示为*3  set为$3 a为$1 1为$1
*3
$3
set
$1
a
$1
1

2、SET key-with-expire-time "hello" EX 1008612 表示为
*5
$3
SET
$20
key-with-expire-time
$5
hello
$4
PXAT
$13
1651130525053

深度思考

redis是如何实现特殊的字符处理的呢?
如 set testkey "a\\r\n"
答案在robj结构体里。我现在也没搞懂

参考和感谢

参考大佬分析笔记的一小部分
https://blog.csdn.net/xindoo/article/details/115447240
感谢聪明机智善于分享的大佬,自己研究后不忘分享给其他小弟,让他人快速学习知识。大佬666。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值