@@ -354,6 +354,7 @@ int checkSingleAof(char *aof_filename, char *aof_filepath, int last_file, int fi
354
354
* 1. The file is an old style RDB-preamble AOF
355
355
* 2. The file is a BASE AOF in Multi Part AOF
356
356
* */
357
+ /* 判断是否是混合持久化 AOF 类型或者是 MP-AOF 的 base 文件,根据前 5 个字符是否为 REDIS */
357
358
int fileIsRDB (char * filepath ) {
358
359
FILE * fp = fopen (filepath , "r" );
359
360
if (fp == NULL ) {
@@ -389,6 +390,7 @@ int fileIsRDB(char *filepath) {
389
390
390
391
/* Used to determine whether the file is a manifest file. */
391
392
#define MANIFEST_MAX_LINE 1024
393
+ /* 判断文件是否是清单文件 */
392
394
int fileIsManifest (char * filepath ) {
393
395
int is_manifest = 0 ;
394
396
FILE * fp = fopen (filepath , "r" );
@@ -440,6 +442,7 @@ int fileIsManifest(char *filepath) {
440
442
* redis-check-aof tool will automatically perform different
441
443
* verification logic according to different file formats.
442
444
* */
445
+ /* 获取 AOF 文件的类型,在三种中选择一种返回 */
443
446
input_file_type getInputFileType (char * filepath ) {
444
447
if (fileIsManifest (filepath )) {
445
448
return AOF_MULTI_PART ;
@@ -461,16 +464,23 @@ input_file_type getInputFileType(char *filepath) {
461
464
*
462
465
* Note that in Multi Part AOF, we only allow truncation for the last AOF file.
463
466
* */
467
+ /* MP-AOF 检查,检查成功后:
468
+ * 1. 清单文件格式有效
469
+ * 2. BASE AOF 和 INCR AOFs 格式都有效
470
+ * 3. 没有 BASE 或 INCR AOFs 文件丢失
471
+ * 4. 在截取 AOF 文件时,此场景只允许截断最后一个 INCR AOF 文件 */
464
472
void checkMultiPartAof (char * dirpath , char * manifest_filepath , int fix ) {
465
473
int total_num = 0 , aof_num = 0 , last_file ;
466
474
int ret ;
467
475
468
476
printf ("Start checking Multi Part AOF\n" );
477
+ /* 加载 AOF 清单文件 */
469
478
aofManifest * am = aofLoadManifestFromFile (manifest_filepath );
470
479
471
480
if (am -> base_aof_info ) total_num ++ ;
472
481
if (am -> incr_aof_list ) total_num += listLength (am -> incr_aof_list );
473
482
483
+ /* 检查 BASE AOF */
474
484
if (am -> base_aof_info ) {
475
485
sds aof_filename = am -> base_aof_info -> file_name ;
476
486
sds aof_filepath = makePath (dirpath , aof_filename );
@@ -492,6 +502,7 @@ void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) {
492
502
sdsfree (aof_filepath );
493
503
}
494
504
505
+ /* 检查 INCR AOFs */
495
506
if (listLength (am -> incr_aof_list )) {
496
507
listNode * ln ;
497
508
listIter li ;
@@ -525,6 +536,7 @@ void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) {
525
536
/* Check if old style AOF is valid. Internally, it will identify whether
526
537
* the AOF is in RDB-preamble format, and will eventually call `checkSingleAof`
527
538
* to do the check. */
539
+ /* 在旧的 AOF 模式(RESP 和混合持久化)下调用此处理函数 */
528
540
void checkOldStyleAof (char * filepath , int fix , int preamble ) {
529
541
printf ("Start checking Old-Style AOF\n" );
530
542
int ret = checkSingleAof (filepath , filepath , 1 , fix , preamble );
@@ -540,12 +552,17 @@ void checkOldStyleAof(char *filepath, int fix, int preamble) {
540
552
}
541
553
}
542
554
555
+ /* redis-check-aof 工具的主入口函数,用来检查 AOF 文件的正确性,并能对错误文件进行一定程度的修复
556
+ * 此工具的使用场景:
557
+ * 1)独立的可执行文件来检查某个 AOF 文件
558
+ * 2)被 server 调用去检查已经打开的文件 */
543
559
int redis_check_aof_main (int argc , char * * argv ) {
544
560
char * filepath ;
545
561
char temp_filepath [PATH_MAX + 1 ];
546
562
char * dirpath ;
547
563
int fix = 0 ;
548
564
565
+ /* 根据入参数量,做相应分支处理 */
549
566
if (argc < 2 ) {
550
567
goto invalid_args ;
551
568
} else if (argc == 2 ) {
@@ -579,6 +596,8 @@ int redis_check_aof_main(int argc, char **argv) {
579
596
dirpath = dirname (temp_filepath );
580
597
581
598
/* Select the corresponding verification method according to the input file type. */
599
+ /* 获取 AOF 的类型,然后根据类型进入不同的分支处理函数
600
+ * 类型有 增量 AOF、RESP 和混合持久化 */
582
601
input_file_type type = getInputFileType (filepath );
583
602
switch (type ) {
584
603
case AOF_MULTI_PART :
@@ -594,6 +613,7 @@ int redis_check_aof_main(int argc, char **argv) {
594
613
595
614
exit (0 );
596
615
616
+ /* 在参数输入错误时,打印帮助信息 */
597
617
invalid_args :
598
618
printf ("Usage: %s [--fix|--truncate-to-timestamp $timestamp] <file.manifest|file.aof>\n" ,
599
619
argv [0 ]);
0 commit comments