编辑
2023-11-15
Redis源码阅读
00
请注意,本文编写于 542 天前,最后修改于 542 天前,其中某些信息可能已经过时。

文章讲得很清楚 看一下兼容老版本的逻辑

c
/* Select the corresponding verification method according to the input file type. */ input_file_type type = getInputFileType(filepath); switch (type) { case AOF_MULTI_PART: checkMultiPartAof(dirpath, filepath, fix); //新版本 break; case AOF_RESP: checkOldStyleAof(filepath, fix, 0); //老版本 break; case AOF_RDB_PREAMBLE: checkOldStyleAof(filepath, fix, 1)// 老版本 break; } //**************************************************************************************** /* Check if Multi Part AOF is valid. It will check the BASE file and INCR files * at once according to the manifest instructions (this is somewhat similar to * redis' AOF loading). * * When the verification is successful, we can guarantee: * 1. The manifest file format is valid * 2. Both BASE AOF and INCR AOFs format are valid * 3. No BASE or INCR AOFs files are missing * * Note that in Multi Part AOF, we only allow truncation for the last AOF file. * */ void checkMultiPartAof(char *dirpath, char *manifest_filepath, int fix) { int total_num = 0, aof_num = 0, last_file; int ret; printf("Start checking Multi Part AOF\n"); aofManifest *am = aofLoadManifestFromFile(manifest_filepath); if (am->base_aof_info) total_num++; if (am->incr_aof_list) total_num += listLength(am->incr_aof_list); if (am->base_aof_info) { sds aof_filename = am->base_aof_info->file_name; sds aof_filepath = makePath(dirpath, aof_filename); last_file = ++aof_num == total_num; int aof_preable = fileIsRDB(aof_filepath); printf("Start to check BASE AOF (%s format).\n", aof_preable ? "RDB":"RESP"); ret = checkSingleAof(aof_filename, aof_filepath, last_file, fix, aof_preable); printAofStyle(ret, aof_filename, (char *)"BASE AOF"); sdsfree(aof_filepath); } if (listLength(am->incr_aof_list)) { listNode *ln; listIter li; printf("Start to check INCR files.\n"); listRewind(am->incr_aof_list, &li); while ((ln = listNext(&li)) != NULL) { aofInfo *ai = (aofInfo*)ln->value; sds aof_filename = (char*)ai->file_name; sds aof_filepath = makePath(dirpath, aof_filename); last_file = ++aof_num == total_num; ret = checkSingleAof(aof_filename, aof_filepath, last_file, fix, 0); printAofStyle(ret, aof_filename, (char *)"INCR AOF"); sdsfree(aof_filepath); } } aofManifestFree(am); printf("All AOF files and manifest are valid\n"); } /* Check if old style AOF is valid. Internally, it will identify whether * the AOF is in RDB-preamble format, and will eventually call `checkSingleAof` * to do the check. */ void checkOldStyleAof(char *filepath, int fix, int preamble) { printf("Start checking Old-Style AOF\n"); int ret = checkSingleAof(filepath, filepath, 1, fix, preamble); printAofStyle(ret, filepath, (char *)"AOF"); }

以上都是在校验AOF文件及其完整性

本文作者:yowayimono

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!