redis源代码分析18–持久化之aof
Redis的aof功能的目的是在性能和持久化粒度上对持久化机制提供更好的支持。 快照方式持久化的粒度有时间(秒)和改变的key数两种,如果持久化的粒度较小,对性能会有较大的影响,因为每次都是dump整个db;如果持久化的粒度较大,则在指定时间内指定数目的数
Redis的aof功能的目的是在性能和持久化粒度上对持久化机制提供更好的支持。
快照方式持久化的粒度有时间(秒)和改变的key数两种,如果持久化的粒度较小,对性能会有较大的影响,因为每次都是dump整个db;如果持久化的粒度较大,则在指定时间内指定数目的数据的持久化无法保证。而aof持久化的粒度是每次会修改db数据的命令,因此粒度是最小的了,跟日志方式有点类似,由于仅记录一条命令,性能也最好。另外,跟日志类似,aof文件会越来越大,则可以通过执行BGREWRITEAOF命令在后台重建该文件。
我们先来看看redis如何记录命令的。
call函数是命令执行的函数(前面命令处理章节已详细介绍过该函数)。如果命令执行前后数据有修改,则server.dirty的取值会有变化。在启用了aof机制的情况下,call函数会调用feedAppendOnlyFile保存命令及其相关参数。
static void call(redisClient *c, struct redisCommand *cmd){ long long dirty; dirty = server.dirty; cmd->proc(c); dirty = server.dirty-dirty; if(server.appendonly && dirty) feedAppendOnlyFile(cmd,c->db->id,c->argv,c->argc); --- }
feedAppendOnlyFile会首先检查当前命令所处的db是否跟前一条命令执行所处db一致。若不一致,则需要发布一条选择db的select命令,然后做些命令的转换工作(代码略去)。
紧接着,将命令参数所对应的buf保存到server.aofbuf中,该参数保存了一段时间内redis执行的命令及其参数,redis会在适当的时机将其刷到磁盘上的aof文件中;然后如果有后台重建aof文件,则也将该缓冲区保存到server.bgrewritebuf中,该缓冲区保存了重建aof文件的后台进程运行时redis所执行的命令及其参数,后台进程退出时需要将这些命令保存到重建文件中。
static void feedAppendOnlyFile(struct redisCommand *cmd, int dictid, robj **argv, int argc){ --- server.aofbuf = sdscatlen(server.aofbuf,buf,sdslen(buf)); --- if(server.bgrewritechildpid != -1) server.bgrewritebuf = sdscatlen(server.bgrewritebuf,buf,sdslen(buf)); sdsfree(buf); }
我们来看看server.aofbuf会在什么时机被刷新到磁盘aof文件中。
刷新采用的是flushAppendOnlyFile函数。该函数在beforeSleep中会被调用(事件处理章节已介绍过该函数),而该函数是在处理client事件之前执行执行的(事件循环函数aeMain是先执行beforesleep,然后执行aeProcessEvents),因此,server.aofbuf中的值会在向client发送响应之前刷新到磁盘上。
flushAppendOnlyFile调用write一次性写全部server.aofbuf缓冲区中的数据,并根据配置的同步策略,调用aof_fsync(对系统同步函数fsync的保证)进行同步,这样新的命令及其参数就被附加到aof文件当中了。
static void flushAppendOnlyFile(void){ time_t now; ssize_t nwritten; --- nwritten = write(server.appendfd,server.aofbuf,sdslen(server.aofbuf)); --- sdsfree(server.aofbuf); server.aofbuf = sdsempty(); /* Fsync if needed */ now = time(NULL); if(server.appendfsync == APPENDFSYNC_ALWAYS|| (server.appendfsync == APPENDFSYNC_EVERYSEC && now-server.lastfsync > 1)) { /* aof_fsync is defined as fdatasync() for Linux in order to avoid * flushing metadata. */ aof_fsync(server.appendfd);/* Let's try to get this data on the disk */ server.lastfsync = now; } }
接下来我们看看后台如何重建aof文件。
aof重建靠调用rewriteAppendOnlyFileBackground函数完成。查看该函数的调用关系就可以知道,该函数会在收到bgrewriteaof命令后执行,也会在收到config命令并且从不使用aof机制到开启aof机制时被调用,也会在运行redis的系统作为slave时,跟master建立连接后并在serverCron函数中执行syncWithMaster时调用。
rewriteAppendOnlyFileBackground重建aof的主要逻辑如下(代码略去):
1)使用fork创建一个子进程
2)子进程调用rewriteAppendOnlyFile在一个临时文件里写能够反映当前db状态的数据和命令,
此时父进程会把这段时间内执行的能够改变当前db数据的命令放到server.bgrewritebuf中(参看前面对feedAppendOnlyFile的解释)
3)当子进程退出时,父进程收到信号,将上面的内存缓冲区中的数据flush到临时文件中,然后将临时文件rename成新的aof文件(backgroundRewriteDoneHandler)。
父进程会在serverCron函数中等待执行aof重写或者快照保存的子进程,代码如下:
/* Check if a background saving or AOF rewrite in progress terminated */ if(server.bgsavechildpid != -1||server.bgrewritechildpid != -1){ int statloc; pid_t pid; if((pid = wait3(&statloc,WNOHANG,NULL))!= 0){ if(pid == server.bgsavechildpid){ backgroundSaveDoneHandler(statloc); } else { backgroundRewriteDoneHandler(statloc); } updateDictResizePolicy(); } }
rewriteAppendOnlyFile将反映当前db状态的命令和参数写到一个临时文件中。该函数遍历db中的每条数据,redis中的db其实是一个大的hash表,每一条数据都用(key,val)来表示。从key可以知道val的类型(redis支持REDIS_STRING、REDIS_LIST、REDIS_SET、REDIS_ZSET、REDIS_HASH五种数据类型),然后解码val中的数据。写入时,按照客户端执行命令的形式写入。比如对于REDIS_STRING类型,则先写入”*3\r\n$3\r \nSET\r\n”,然后写入set的key,然后写入val;对于REDIS_LIST类型,将val强制转换为list类型后,先写入”*3\r \n$5\r\nRPUSH\r\n”,然后写入要操作的list的名字,然后写入list的第一个数据,循环前面3个步骤直到list遍历完;对于REDIS_SET类型,则对于每条数据先写入”*3\r\n$4\r\nSADD\r\n”;对于REDIS_ZSET类型,则对于每条数据先写入”*4\r\n$4\r\nZADD\r\n”;对于REDIS_HASH类型,则对于每条数据先写入”*4\r\n$4\r\nHSET\r\n”(代码简单但较琐碎,略去)。
最后我们介绍下redis启动时使用aof重建db的步骤。
启动时重建的关键是构建一个fake client,然后使用这个client向server发送从aof文件中读入的命令。
int loadAppendOnlyFile(char *filename){ --- fakeClient = createFakeClient(); while(1){ --- if(fgets(buf,sizeof(buf),fp)== NULL){ --- } // 解析buf为对应的命令及参数 // 查找命令 cmd = lookupCommand(argv[0]->ptr); --- // 执行命令 cmd->proc(fakeClient); --- } --- }
原文地址:redis源代码分析18–持久化之aof, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Redis cluster mode deploys Redis instances to multiple servers through sharding, improving scalability and availability. The construction steps are as follows: Create odd Redis instances with different ports; Create 3 sentinel instances, monitor Redis instances and failover; configure sentinel configuration files, add monitoring Redis instance information and failover settings; configure Redis instance configuration files, enable cluster mode and specify the cluster information file path; create nodes.conf file, containing information of each Redis instance; start the cluster, execute the create command to create a cluster and specify the number of replicas; log in to the cluster to execute the CLUSTER INFO command to verify the cluster status; make

How to clear Redis data: Use the FLUSHALL command to clear all key values. Use the FLUSHDB command to clear the key value of the currently selected database. Use SELECT to switch databases, and then use FLUSHDB to clear multiple databases. Use the DEL command to delete a specific key. Use the redis-cli tool to clear the data.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

On CentOS systems, you can limit the execution time of Lua scripts by modifying Redis configuration files or using Redis commands to prevent malicious scripts from consuming too much resources. Method 1: Modify the Redis configuration file and locate the Redis configuration file: The Redis configuration file is usually located in /etc/redis/redis.conf. Edit configuration file: Open the configuration file using a text editor (such as vi or nano): sudovi/etc/redis/redis.conf Set the Lua script execution time limit: Add or modify the following lines in the configuration file to set the maximum execution time of the Lua script (unit: milliseconds)

Use the Redis command line tool (redis-cli) to manage and operate Redis through the following steps: Connect to the server, specify the address and port. Send commands to the server using the command name and parameters. Use the HELP command to view help information for a specific command. Use the QUIT command to exit the command line tool.

Redis counter is a mechanism that uses Redis key-value pair storage to implement counting operations, including the following steps: creating counter keys, increasing counts, decreasing counts, resetting counts, and obtaining counts. The advantages of Redis counters include fast speed, high concurrency, durability and simplicity and ease of use. It can be used in scenarios such as user access counting, real-time metric tracking, game scores and rankings, and order processing counting.

There are two types of Redis data expiration strategies: periodic deletion: periodic scan to delete the expired key, which can be set through expired-time-cap-remove-count and expired-time-cap-remove-delay parameters. Lazy Deletion: Check for deletion expired keys only when keys are read or written. They can be set through lazyfree-lazy-eviction, lazyfree-lazy-expire, lazyfree-lazy-user-del parameters.

In Debian systems, readdir system calls are used to read directory contents. If its performance is not good, try the following optimization strategy: Simplify the number of directory files: Split large directories into multiple small directories as much as possible, reducing the number of items processed per readdir call. Enable directory content caching: build a cache mechanism, update the cache regularly or when directory content changes, and reduce frequent calls to readdir. Memory caches (such as Memcached or Redis) or local caches (such as files or databases) can be considered. Adopt efficient data structure: If you implement directory traversal by yourself, select more efficient data structures (such as hash tables instead of linear search) to store and access directory information
