Skip to content

Commit 387074f

Browse files
增加 redis-check-aof 部分注释(#90)
Co-authored-by: Binbin <[email protected]>
1 parent 2086628 commit 387074f

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ redis 仓库链接:https://github.com/redis/redis<br>
4949
| [ae.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/ae.c) | redis 事件循环器功能 | 完成 |
5050
| [multi.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/multi.c) | redis 事务实现 | 完成 |
5151
| [redis-check-rdb.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/redis-check-rdb.c) | Redis RDB 检查工具 | 完成 |
52+
| [redis-check-aof.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/redis-check-aof.c) | Redis AOF 检查工具 | 低于一半 |
5253
| [evict.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/evict.c) | 四种 redis 内存淘汰算法 | 完成 |
5354
| [aof.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/aof.c) | redis AOF 功能 | 过半 |
5455
| [replication.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/replication.c) | Redis 主从复制 | 低于一半 |
@@ -57,7 +58,7 @@ redis 仓库链接:https://github.com/redis/redis<br>
5758
| [networking.c](https://github.com/CN-annotation-team/redis7.0-chinese-annotated/blob/7.0-cn-annotated/src/networking.c) | Redis 客户端、主、副本 I/O 相关 | 低于一半 |
5859
</p>
5960
尚未有中文注释的文件不会出现在表格中。<br>
60-
更新日期:2022/11/5
61+
更新日期:2022/11/7
6162

6263

6364
## 关于提交 PR 的方法:

src/redis-check-aof.c

+20
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ int checkSingleAof(char *aof_filename, char *aof_filepath, int last_file, int fi
354354
* 1. The file is an old style RDB-preamble AOF
355355
* 2. The file is a BASE AOF in Multi Part AOF
356356
* */
357+
/* 判断是否是混合持久化 AOF 类型或者是 MP-AOF 的 base 文件,根据前 5 个字符是否为 REDIS */
357358
int fileIsRDB(char *filepath) {
358359
FILE *fp = fopen(filepath, "r");
359360
if (fp == NULL) {
@@ -389,6 +390,7 @@ int fileIsRDB(char *filepath) {
389390

390391
/* Used to determine whether the file is a manifest file. */
391392
#define MANIFEST_MAX_LINE 1024
393+
/* 判断文件是否是清单文件 */
392394
int fileIsManifest(char *filepath) {
393395
int is_manifest = 0;
394396
FILE *fp = fopen(filepath, "r");
@@ -440,6 +442,7 @@ int fileIsManifest(char *filepath) {
440442
* redis-check-aof tool will automatically perform different
441443
* verification logic according to different file formats.
442444
* */
445+
/* 获取 AOF 文件的类型,在三种中选择一种返回 */
443446
input_file_type getInputFileType(char *filepath) {
444447
if (fileIsManifest(filepath)) {
445448
return AOF_MULTI_PART;
@@ -461,16 +464,23 @@ input_file_type getInputFileType(char *filepath) {
461464
*
462465
* Note that in Multi Part AOF, we only allow truncation for the last AOF file.
463466
* */
467+
/* MP-AOF 检查,检查成功后:
468+
* 1. 清单文件格式有效
469+
* 2. BASE AOF 和 INCR AOFs 格式都有效
470+
* 3. 没有 BASE 或 INCR AOFs 文件丢失
471+
* 4. 在截取 AOF 文件时,此场景只允许截断最后一个 INCR AOF 文件 */
464472
void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) {
465473
int total_num = 0, aof_num = 0, last_file;
466474
int ret;
467475

468476
printf("Start checking Multi Part AOF\n");
477+
/* 加载 AOF 清单文件 */
469478
aofManifest *am = aofLoadManifestFromFile(manifest_filepath);
470479

471480
if (am->base_aof_info) total_num++;
472481
if (am->incr_aof_list) total_num += listLength(am->incr_aof_list);
473482

483+
/* 检查 BASE AOF */
474484
if (am->base_aof_info) {
475485
sds aof_filename = am->base_aof_info->file_name;
476486
sds aof_filepath = makePath(dirpath, aof_filename);
@@ -492,6 +502,7 @@ void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) {
492502
sdsfree(aof_filepath);
493503
}
494504

505+
/* 检查 INCR AOFs */
495506
if (listLength(am->incr_aof_list)) {
496507
listNode *ln;
497508
listIter li;
@@ -525,6 +536,7 @@ void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) {
525536
/* Check if old style AOF is valid. Internally, it will identify whether
526537
* the AOF is in RDB-preamble format, and will eventually call `checkSingleAof`
527538
* to do the check. */
539+
/* 在旧的 AOF 模式(RESP 和混合持久化)下调用此处理函数 */
528540
void checkOldStyleAof(char *filepath, int fix, int preamble) {
529541
printf("Start checking Old-Style AOF\n");
530542
int ret = checkSingleAof(filepath, filepath, 1, fix, preamble);
@@ -540,12 +552,17 @@ void checkOldStyleAof(char *filepath, int fix, int preamble) {
540552
}
541553
}
542554

555+
/* redis-check-aof 工具的主入口函数,用来检查 AOF 文件的正确性,并能对错误文件进行一定程度的修复
556+
* 此工具的使用场景:
557+
* 1)独立的可执行文件来检查某个 AOF 文件
558+
* 2)被 server 调用去检查已经打开的文件 */
543559
int redis_check_aof_main(int argc, char **argv) {
544560
char *filepath;
545561
char temp_filepath[PATH_MAX + 1];
546562
char *dirpath;
547563
int fix = 0;
548564

565+
/* 根据入参数量,做相应分支处理 */
549566
if (argc < 2) {
550567
goto invalid_args;
551568
} else if (argc == 2) {
@@ -579,6 +596,8 @@ int redis_check_aof_main(int argc, char **argv) {
579596
dirpath = dirname(temp_filepath);
580597

581598
/* Select the corresponding verification method according to the input file type. */
599+
/* 获取 AOF 的类型,然后根据类型进入不同的分支处理函数
600+
* 类型有 增量 AOF、RESP 和混合持久化 */
582601
input_file_type type = getInputFileType(filepath);
583602
switch (type) {
584603
case AOF_MULTI_PART:
@@ -594,6 +613,7 @@ int redis_check_aof_main(int argc, char **argv) {
594613

595614
exit(0);
596615

616+
/* 在参数输入错误时,打印帮助信息 */
597617
invalid_args:
598618
printf("Usage: %s [--fix|--truncate-to-timestamp $timestamp] <file.manifest|file.aof>\n",
599619
argv[0]);

src/redis-check-rdb.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ int redis_check_rdb(char *rdbfilename, FILE *fp) {
210210
int selected_dbid = -1;
211211
int type, rdbver;
212212
char buf[1024];
213-
/* now 毫秒时间戳 */
213+
/* now 毫秒时间戳 */
214214
long long expiretime, now = mstime();
215215
static rio rdb; /* Pointed by global struct riostate. */
216216
struct stat sb;
@@ -230,9 +230,9 @@ int redis_check_rdb(char *rdbfilename, FILE *fp) {
230230
rdbstate.rio = &rdb;
231231
rdb.update_cksum = rdbLoadProgressCallback;
232232

233-
/* 对 RDB 文件前 9 个字符做判断:
234-
* 1)前 5 个字符必须为 REDIS
235-
* 2)第 6-9 个字符为版本号,版本号必须大于等于 1,并且小于 RDB_VERSION */
233+
/* 对 RDB 文件前 9 个字符做判断:
234+
* 1)前 5 个字符必须为 REDIS
235+
* 2)第 6-9 个字符为版本号,版本号必须大于等于 1,并且小于 RDB_VERSION */
236236
if (rioRead(&rdb,buf,9) == 0) goto eoferr;
237237
buf[9] = '\0';
238238
if (memcmp(buf,"REDIS",5) != 0) {
@@ -452,7 +452,7 @@ static sds checkRdbVersion(void) {
452452
int redis_check_rdb_main(int argc, char **argv, FILE *fp) {
453453
struct timeval tv;
454454

455-
/* 判断入参数量是否等于2,并且 fp 指针不为空,否则打印使用帮助并退出程序 */
455+
/* 判断入参数量是否等于 2,并且 fp 指针不为空,否则打印使用帮助并退出程序 */
456456
if (argc != 2 && fp == NULL) {
457457
fprintf(stderr, "Usage: %s <rdb-file-name>\n", argv[0]);
458458
exit(1);

0 commit comments

Comments
 (0)