漫话Redis源码之二十九

2024-02-06 09:48
文章标签 源码 redis 二十九 漫话

本文主要是介绍漫话Redis源码之二十九,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这里主要讲rdb的一些check信息,不是咱们缕清逻辑的重点,不想看的话,可以跳过:

struct {rio *rio;robj *key;                      /* Current key we are reading. */int key_type;                   /* Current key type if != -1. */unsigned long keys;             /* Number of keys processed. */unsigned long expires;          /* Number of keys with an expire. */unsigned long already_expired;  /* Number of keys already expired. */int doing;                      /* The state while reading the RDB. */int error_set;                  /* True if error is populated. */char error[1024];
} rdbstate;/* At every loading step try to remember what we were about to do, so that* we can log this information when an error is encountered. */
#define RDB_CHECK_DOING_START 0
#define RDB_CHECK_DOING_READ_TYPE 1
#define RDB_CHECK_DOING_READ_EXPIRE 2
#define RDB_CHECK_DOING_READ_KEY 3
#define RDB_CHECK_DOING_READ_OBJECT_VALUE 4
#define RDB_CHECK_DOING_CHECK_SUM 5
#define RDB_CHECK_DOING_READ_LEN 6
#define RDB_CHECK_DOING_READ_AUX 7
#define RDB_CHECK_DOING_READ_MODULE_AUX 8char *rdb_check_doing_string[] = {"start","read-type","read-expire","read-key","read-object-value","check-sum","read-len","read-aux","read-module-aux"
};char *rdb_type_string[] = {"string","list-linked","set-hashtable","zset-v1","hash-hashtable","zset-v2","module-value","","","hash-zipmap","list-ziplist","set-intset","zset-ziplist","hash-ziplist","quicklist","stream"
};/* Show a few stats collected into 'rdbstate' */
void rdbShowGenericInfo(void) {printf("[info] %lu keys read\n", rdbstate.keys);printf("[info] %lu expires\n", rdbstate.expires);printf("[info] %lu already expired\n", rdbstate.already_expired);
}/* Called on RDB errors. Provides details about the RDB and the offset* we were when the error was detected. */
void rdbCheckError(const char *fmt, ...) {char msg[1024];va_list ap;va_start(ap, fmt);vsnprintf(msg, sizeof(msg), fmt, ap);va_end(ap);printf("--- RDB ERROR DETECTED ---\n");printf("[offset %llu] %s\n",(unsigned long long) (rdbstate.rio ?rdbstate.rio->processed_bytes : 0), msg);printf("[additional info] While doing: %s\n",rdb_check_doing_string[rdbstate.doing]);if (rdbstate.key)printf("[additional info] Reading key '%s'\n",(char*)rdbstate.key->ptr);if (rdbstate.key_type != -1)printf("[additional info] Reading type %d (%s)\n",rdbstate.key_type,((unsigned)rdbstate.key_type <sizeof(rdb_type_string)/sizeof(char*)) ?rdb_type_string[rdbstate.key_type] : "unknown");rdbShowGenericInfo();
}/* Print informations during RDB checking. */
void rdbCheckInfo(const char *fmt, ...) {char msg[1024];va_list ap;va_start(ap, fmt);vsnprintf(msg, sizeof(msg), fmt, ap);va_end(ap);printf("[offset %llu] %s\n",(unsigned long long) (rdbstate.rio ?rdbstate.rio->processed_bytes : 0), msg);
}/* Used inside rdb.c in order to log specific errors happening inside* the RDB loading internals. */
void rdbCheckSetError(const char *fmt, ...) {va_list ap;va_start(ap, fmt);vsnprintf(rdbstate.error, sizeof(rdbstate.error), fmt, ap);va_end(ap);rdbstate.error_set = 1;
}/* During RDB check we setup a special signal handler for memory violations* and similar conditions, so that we can log the offending part of the RDB* if the crash is due to broken content. */
void rdbCheckHandleCrash(int sig, siginfo_t *info, void *secret) {UNUSED(sig);UNUSED(info);UNUSED(secret);rdbCheckError("Server crash checking the specified RDB file!");exit(1);
}void rdbCheckSetupSignals(void) {struct sigaction act;sigemptyset(&act.sa_mask);act.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;act.sa_sigaction = rdbCheckHandleCrash;sigaction(SIGSEGV, &act, NULL);sigaction(SIGBUS, &act, NULL);sigaction(SIGFPE, &act, NULL);sigaction(SIGILL, &act, NULL);sigaction(SIGABRT, &act, NULL);
}/* Check the specified RDB file. Return 0 if the RDB looks sane, otherwise* 1 is returned.* The file is specified as a filename in 'rdbfilename' if 'fp' is not NULL,* otherwise the already open file 'fp' is checked. */
int redis_check_rdb(char *rdbfilename, FILE *fp) {uint64_t dbid;int type, rdbver;char buf[1024];long long expiretime, now = mstime();static rio rdb; /* Pointed by global struct riostate. */int closefile = (fp == NULL);if (fp == NULL && (fp = fopen(rdbfilename,"r")) == NULL) return 1;startLoadingFile(fp, rdbfilename, RDBFLAGS_NONE);rioInitWithFile(&rdb,fp);rdbstate.rio = &rdb;rdb.update_cksum = rdbLoadProgressCallback;if (rioRead(&rdb,buf,9) == 0) goto eoferr;buf[9] = '\0';if (memcmp(buf,"REDIS",5) != 0) {rdbCheckError("Wrong signature trying to load DB from file");goto err;}rdbver = atoi(buf+5);if (rdbver < 1 || rdbver > RDB_VERSION) {rdbCheckError("Can't handle RDB format version %d",rdbver);goto err;}expiretime = -1;while(1) {robj *key, *val;/* Read type. */rdbstate.doing = RDB_CHECK_DOING_READ_TYPE;if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;/* Handle special types. */if (type == RDB_OPCODE_EXPIRETIME) {rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;/* EXPIRETIME: load an expire associated with the next key* to load. Note that after loading an expire we need to* load the actual type, and continue. */expiretime = rdbLoadTime(&rdb);expiretime *= 1000;if (rioGetReadError(&rdb)) goto eoferr;continue; /* Read next opcode. */} else if (type == RDB_OPCODE_EXPIRETIME_MS) {/* EXPIRETIME_MS: milliseconds precision expire times introduced* with RDB v3. Like EXPIRETIME but no with more precision. */rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;expiretime = rdbLoadMillisecondTime(&rdb, rdbver);if (rioGetReadError(&rdb)) goto eoferr;continue; /* Read next opcode. */} else if (type == RDB_OPCODE_FREQ) {/* FREQ: LFU frequency. */uint8_t byte;if (rioRead(&rdb,&byte,1) == 0) goto eoferr;continue; /* Read next opcode. */} else if (type == RDB_OPCODE_IDLE) {/* IDLE: LRU idle time. */if (rdbLoadLen(&rdb,NULL) == RDB_LENERR) goto eoferr;continue; /* Read next opcode. */} else if (type == RDB_OPCODE_EOF) {/* EOF: End of file, exit the main loop. */break;} else if (type == RDB_OPCODE_SELECTDB) {/* SELECTDB: Select the specified database. */rdbstate.doing = RDB_CHECK_DOING_READ_LEN;if ((dbid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)goto eoferr;rdbCheckInfo("Selecting DB ID %llu", (unsigned long long)dbid);continue; /* Read type again. */} else if (type == RDB_OPCODE_RESIZEDB) {/* RESIZEDB: Hint about the size of the keys in the currently* selected data base, in order to avoid useless rehashing. */uint64_t db_size, expires_size;rdbstate.doing = RDB_CHECK_DOING_READ_LEN;if ((db_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)goto eoferr;if ((expires_size = rdbLoadLen(&rdb,NULL)) == RDB_LENERR)goto eoferr;continue; /* Read type again. */} else if (type == RDB_OPCODE_AUX) {/* AUX: generic string-string fields. Use to add state to RDB* which is backward compatible. Implementations of RDB loading* are requierd to skip AUX fields they don't understand.** An AUX field is composed of two strings: key and value. */robj *auxkey, *auxval;rdbstate.doing = RDB_CHECK_DOING_READ_AUX;if ((auxkey = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;if ((auxval = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;rdbCheckInfo("AUX FIELD %s = '%s'",(char*)auxkey->ptr, (char*)auxval->ptr);decrRefCount(auxkey);decrRefCount(auxval);continue; /* Read type again. */} else if (type == RDB_OPCODE_MODULE_AUX) {/* AUX: Auxiliary data for modules. */uint64_t moduleid, when_opcode, when;rdbstate.doing = RDB_CHECK_DOING_READ_MODULE_AUX;if ((moduleid = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;if ((when_opcode = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;if ((when = rdbLoadLen(&rdb,NULL)) == RDB_LENERR) goto eoferr;char name[10];moduleTypeNameByID(name,moduleid);rdbCheckInfo("MODULE AUX for: %s", name);robj *o = rdbLoadCheckModuleValue(&rdb,name);decrRefCount(o);continue; /* Read type again. */} else {if (!rdbIsObjectType(type)) {rdbCheckError("Invalid object type: %d", type);goto err;}rdbstate.key_type = type;}/* Read key */rdbstate.doing = RDB_CHECK_DOING_READ_KEY;if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;rdbstate.key = key;rdbstate.keys++;/* Read value */rdbstate.doing = RDB_CHECK_DOING_READ_OBJECT_VALUE;if ((val = rdbLoadObject(type,&rdb,key->ptr,NULL)) == NULL) goto eoferr;/* Check if the key already expired. */if (expiretime != -1 && expiretime < now)rdbstate.already_expired++;if (expiretime != -1) rdbstate.expires++;rdbstate.key = NULL;decrRefCount(key);decrRefCount(val);rdbstate.key_type = -1;expiretime = -1;}/* Verify the checksum if RDB version is >= 5 */if (rdbver >= 5 && server.rdb_checksum) {uint64_t cksum, expected = rdb.cksum;rdbstate.doing = RDB_CHECK_DOING_CHECK_SUM;if (rioRead(&rdb,&cksum,8) == 0) goto eoferr;memrev64ifbe(&cksum);if (cksum == 0) {rdbCheckInfo("RDB file was saved with checksum disabled: no check performed.");} else if (cksum != expected) {rdbCheckError("RDB CRC error");goto err;} else {rdbCheckInfo("Checksum OK");}}if (closefile) fclose(fp);stopLoading(1);return 0;eoferr: /* unexpected end of file is handled here with a fatal exit */if (rdbstate.error_set) {rdbCheckError(rdbstate.error);} else {rdbCheckError("Unexpected EOF reading RDB file");}
err:if (closefile) fclose(fp);stopLoading(0);return 1;
}

这篇关于漫话Redis源码之二十九的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/683906

相关文章

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Redis主从复制实现原理分析

《Redis主从复制实现原理分析》Redis主从复制通过Sync和CommandPropagate阶段实现数据同步,2.8版本后引入Psync指令,根据复制偏移量进行全量或部分同步,优化了数据传输效率... 目录Redis主DodMIK从复制实现原理实现原理Psync: 2.8版本后总结Redis主从复制实

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

Redis分布式锁使用及说明

《Redis分布式锁使用及说明》本文总结了Redis和Zookeeper在高可用性和高一致性场景下的应用,并详细介绍了Redis的分布式锁实现方式,包括使用Lua脚本和续期机制,最后,提到了RedLo... 目录Redis分布式锁加锁方式怎么会解错锁?举个小案例吧解锁方式续期总结Redis分布式锁如果追求

Redis的Hash类型及相关命令小结

《Redis的Hash类型及相关命令小结》edisHash是一种数据结构,用于存储字段和值的映射关系,本文就来介绍一下Redis的Hash类型及相关命令小结,具有一定的参考价值,感兴趣的可以了解一下... 目录HSETHGETHEXISTSHDELHKEYSHVALSHGETALLHMGETHLENHSET

如何提高Redis服务器的最大打开文件数限制

《如何提高Redis服务器的最大打开文件数限制》文章讨论了如何提高Redis服务器的最大打开文件数限制,以支持高并发服务,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录如何提高Redis服务器的最大打开文件数限制问题诊断解决步骤1. 修改系统级别的限制2. 为Redis进程特别设置限制

Redis KEYS查询大批量数据替代方案

《RedisKEYS查询大批量数据替代方案》在使用Redis时,KEYS命令虽然简单直接,但其全表扫描的特性在处理大规模数据时会导致性能问题,甚至可能阻塞Redis服务,本文将介绍SCAN命令、有序... 目录前言KEYS命令问题背景替代方案1.使用 SCAN 命令2. 使用有序集合(Sorted Set)

基于Redis有序集合实现滑动窗口限流的步骤

《基于Redis有序集合实现滑动窗口限流的步骤》滑动窗口算法是一种基于时间窗口的限流算法,通过动态地滑动窗口,可以动态调整限流的速率,Redis有序集合可以用来实现滑动窗口限流,本文介绍基于Redis... 滑动窗口算法是一种基于时间窗口的限流算法,它将时间划分为若干个固定大小的窗口,每个窗口内记录了该时间

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]