关键词搜索

源码搜索 ×
×

漫话Redis源码之三十二

发布2021-12-12浏览653次

详情内容

关于report相关的函数,非核心逻辑。我一般是直接忽略,不纠结。这样对理解整个代码也有好处,该忽略就忽略,该屏蔽就屏蔽:

  1. #ifdef __GNUC__
  2. void rdbReportError(int corruption_error, int linenum, char *reason, ...) __attribute__ ((format (printf, 3, 4)));
  3. #endif
  4. void rdbReportError(int corruption_error, int linenum, char *reason, ...) {
  5. va_list ap;
  6. char msg[1024];
  7. int len;
  8. len = snprintf(msg,sizeof(msg),
  9. "Internal error in RDB reading offset %llu, function at rdb.c:%d -> ",
  10. (unsigned long long)server.loading_loaded_bytes, linenum);
  11. va_start(ap,reason);
  12. vsnprintf(msg+len,sizeof(msg)-len,reason,ap);
  13. va_end(ap);
  14. if (!server.loading) {
  15. /* If we're in the context of a RESTORE command, just propagate the error. */
  16. /* log in VERBOSE, and return (don't exit). */
  17. serverLog(LL_VERBOSE, "%s", msg);
  18. return;
  19. } else if (rdbCheckMode) {
  20. /* If we're inside the rdb checker, let it handle the error. */
  21. rdbCheckError("%s",msg);
  22. } else if (rdbFileBeingLoaded) {
  23. /* If we're loading an rdb file form disk, run rdb check (and exit) */
  24. serverLog(LL_WARNING, "%s", msg);
  25. char *argv[2] = {"",rdbFileBeingLoaded};
  26. redis_check_rdb_main(2,argv,NULL);
  27. } else if (corruption_error) {
  28. /* In diskless loading, in case of corrupt file, log and exit. */
  29. serverLog(LL_WARNING, "%s. Failure loading rdb format", msg);
  30. } else {
  31. /* In diskless loading, in case of a short read (not a corrupt
  32. * file), log and proceed (don't exit). */
  33. serverLog(LL_WARNING, "%s. Failure loading rdb format from socket, assuming connection error, resuming operation.", msg);
  34. return;
  35. }
  36. serverLog(LL_WARNING, "Terminating server after rdb file reading failure.");
  37. exit(1);
  38. }
  39. static ssize_t rdbWriteRaw(rio *rdb, void *p, size_t len) {
  40. if (rdb && rioWrite(rdb,p,len) == 0)
  41. return -1;
  42. return len;
  43. }
  44. int rdbSaveType(rio *rdb, unsigned char type) {
  45. return rdbWriteRaw(rdb,&type,1);
  46. }
  47. /* Load a "type" in RDB format, that is a one byte unsigned integer.
  48. * This function is not only used to load object types, but also special
  49. * "types" like the end-of-file type, the EXPIRE type, and so forth. */
  50. int rdbLoadType(rio *rdb) {
  51. unsigned char type;
  52. if (rioRead(rdb,&type,1) == 0) return -1;
  53. return type;
  54. }
  55. /* This is only used to load old databases stored with the RDB_OPCODE_EXPIRETIME
  56. * opcode. New versions of Redis store using the RDB_OPCODE_EXPIRETIME_MS
  57. * opcode. On error -1 is returned, however this could be a valid time, so
  58. * to check for loading errors the caller should call rioGetReadError() after
  59. * calling this function. */
  60. time_t rdbLoadTime(rio *rdb) {
  61. int32_t t32;
  62. if (rioRead(rdb,&t32,4) == 0) return -1;
  63. return (time_t)t32;
  64. }
  65. int rdbSaveMillisecondTime(rio *rdb, long long t) {
  66. int64_t t64 = (int64_t) t;
  67. memrev64ifbe(&t64); /* Store in little endian. */
  68. return rdbWriteRaw(rdb,&t64,8);
  69. }
  70. /* This function loads a time from the RDB file. It gets the version of the
  71. * RDB because, unfortunately, before Redis 5 (RDB version 9), the function
  72. * failed to convert data to/from little endian, so RDB files with keys having
  73. * expires could not be shared between big endian and little endian systems
  74. * (because the expire time will be totally wrong). The fix for this is just
  75. * to call memrev64ifbe(), however if we fix this for all the RDB versions,
  76. * this call will introduce an incompatibility for big endian systems:
  77. * after upgrading to Redis version 5 they will no longer be able to load their
  78. * own old RDB files. Because of that, we instead fix the function only for new
  79. * RDB versions, and load older RDB versions as we used to do in the past,
  80. * allowing big endian systems to load their own old RDB files.
  81. *
  82. * On I/O error the function returns LLONG_MAX, however if this is also a
  83. * valid stored value, the caller should use rioGetReadError() to check for
  84. * errors after calling this function. */
  85. long long rdbLoadMillisecondTime(rio *rdb, int rdbver) {
  86. int64_t t64;
  87. if (rioRead(rdb,&t64,8) == 0) return LLONG_MAX;
  88. if (rdbver >= 9) /* Check the top comment of this function. */
  89. memrev64ifbe(&t64); /* Convert in big endian if the system is BE. */
  90. return (long long)t64;
  91. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载