2012-11-08 18:25:23 +01:00
|
|
|
/*
|
2024-08-14 17:20:36 +01:00
|
|
|
* Copyright (c) 2016, Redis Ltd.
|
2012-11-08 18:25:23 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-12-23 05:52:07 -08:00
|
|
|
#include "mt19937-64.h"
|
2015-07-26 15:14:57 +02:00
|
|
|
#include "server.h"
|
2014-05-12 12:12:48 -04:00
|
|
|
#include "rdb.h"
|
2025-01-02 17:35:10 +00:00
|
|
|
#include "module.h"
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
#include <stdarg.h>
|
2020-12-23 05:52:07 -08:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
Implement Multi Part AOF mechanism to avoid AOFRW overheads. (#9788)
Implement Multi-Part AOF mechanism to avoid overheads during AOFRW.
Introducing a folder with multiple AOF files tracked by a manifest file.
The main issues with the the original AOFRW mechanism are:
* buffering of commands that are processed during rewrite (consuming a lot of RAM)
* freezes of the main process when the AOFRW completes to drain the remaining part of the buffer and fsync it.
* double disk IO for the data that arrives during AOFRW (had to be written to both the old and new AOF files)
The main modifications of this PR:
1. Remove the AOF rewrite buffer and related code.
2. Divide the AOF into multiple files, they are classified as two types, one is the the `BASE` type,
it represents the full amount of data (Maybe AOF or RDB format) after each AOFRW, there is only
one `BASE` file at most. The second is `INCR` type, may have more than one. They represent the
incremental commands since the last AOFRW.
3. Use a AOF manifest file to record and manage these AOF files mentioned above.
4. The original configuration of `appendfilename` will be the base part of the new file name, for example:
`appendonly.aof.1.base.rdb` and `appendonly.aof.2.incr.aof`
5. Add manifest-related TCL tests, and modified some existing tests that depend on the `appendfilename`
6. Remove the `aof_rewrite_buffer_length` field in info.
7. Add `aof-disable-auto-gc` configuration. By default we're automatically deleting HISTORY type AOFs.
It also gives users the opportunity to preserve the history AOFs. just for testing use now.
8. Add AOFRW limiting measure. When the AOFRW failures reaches the threshold (3 times now),
we will delay the execution of the next AOFRW by 1 minute. If the next AOFRW also fails, it will be
delayed by 2 minutes. The next is 4, 8, 16, the maximum delay is 60 minutes (1 hour). During the limit
period, we can still use the 'bgrewriteaof' command to execute AOFRW immediately.
9. Support upgrade (load) data from old version redis.
10. Add `appenddirname` configuration, as the directory name of the append only files. All AOF files and
manifest file will be placed in this directory.
11. Only the last AOF file (BASE or INCR) can be truncated. Otherwise redis will exit even if
`aof-load-truncated` is enabled.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-04 01:14:13 +08:00
|
|
|
#include <sys/stat.h>
|
2016-06-30 23:44:44 +02:00
|
|
|
|
2016-07-01 09:36:52 +02:00
|
|
|
void createSharedObjects(void);
|
2016-06-30 23:44:44 +02:00
|
|
|
void rdbLoadProgressCallback(rio *r, const void *buf, size_t len);
|
2016-07-01 09:36:52 +02:00
|
|
|
int rdbCheckMode = 0;
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
struct {
|
|
|
|
rio *rio;
|
|
|
|
robj *key; /* Current key we are reading. */
|
2016-07-01 11:40:40 +02:00
|
|
|
int key_type; /* Current key type if != -1. */
|
2016-06-30 23:44:44 +02:00
|
|
|
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. */
|
2016-07-01 11:59:25 +02:00
|
|
|
int error_set; /* True if error is populated. */
|
|
|
|
char error[1024];
|
2016-06-30 23:44:44 +02:00
|
|
|
} rdbstate;
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-07-01 09:36:52 +02:00
|
|
|
/* 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. */
|
2016-06-30 23:44:44 +02:00
|
|
|
#define RDB_CHECK_DOING_START 0
|
2016-07-01 09:36:52 +02:00
|
|
|
#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
|
2020-09-22 10:18:07 +03:00
|
|
|
#define RDB_CHECK_DOING_READ_MODULE_AUX 8
|
2022-04-05 10:27:24 +03:00
|
|
|
#define RDB_CHECK_DOING_READ_FUNCTIONS 9
|
2016-07-01 09:36:52 +02:00
|
|
|
|
|
|
|
char *rdb_check_doing_string[] = {
|
|
|
|
"start",
|
|
|
|
"read-type",
|
|
|
|
"read-expire",
|
|
|
|
"read-key",
|
|
|
|
"read-object-value",
|
2022-04-05 10:27:24 +03:00
|
|
|
"check-sum",
|
|
|
|
"read-len",
|
|
|
|
"read-aux",
|
|
|
|
"read-module-aux",
|
|
|
|
"read-functions",
|
|
|
|
};
|
2016-07-01 09:36:52 +02:00
|
|
|
|
2016-07-01 11:40:40 +02:00
|
|
|
char *rdb_type_string[] = {
|
|
|
|
"string",
|
|
|
|
"list-linked",
|
|
|
|
"set-hashtable",
|
|
|
|
"zset-v1",
|
|
|
|
"hash-hashtable",
|
|
|
|
"zset-v2",
|
2022-08-15 21:41:44 +03:00
|
|
|
"module-pre-release",
|
2016-07-01 11:40:40 +02:00
|
|
|
"module-value",
|
2022-08-15 21:41:44 +03:00
|
|
|
"",
|
2016-07-01 11:40:40 +02:00
|
|
|
"hash-zipmap",
|
|
|
|
"list-ziplist",
|
|
|
|
"set-intset",
|
|
|
|
"zset-ziplist",
|
|
|
|
"hash-ziplist",
|
2018-02-21 11:17:46 +01:00
|
|
|
"quicklist",
|
2021-08-10 14:18:49 +08:00
|
|
|
"stream",
|
2021-09-22 15:40:31 +08:00
|
|
|
"hash-listpack",
|
2021-11-03 20:47:18 +02:00
|
|
|
"zset-listpack",
|
2022-11-09 18:50:07 +01:00
|
|
|
"quicklist-v2",
|
2024-01-23 11:48:02 +02:00
|
|
|
"stream-v2",
|
2022-11-09 18:50:07 +01:00
|
|
|
"set-listpack",
|
2024-01-23 11:48:02 +02:00
|
|
|
"stream-v3",
|
2016-07-01 11:40:40 +02:00
|
|
|
};
|
|
|
|
|
2016-07-01 09:36:52 +02:00
|
|
|
/* 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);
|
|
|
|
}
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
/* 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;
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-07-01 09:36:52 +02:00
|
|
|
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);
|
2016-07-01 11:40:40 +02:00
|
|
|
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");
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbShowGenericInfo();
|
2010-03-13 15:55:42 +01:00
|
|
|
}
|
|
|
|
|
2021-06-10 20:39:33 +08:00
|
|
|
/* Print information during RDB checking. */
|
2016-06-30 23:44:44 +02:00
|
|
|
void rdbCheckInfo(const char *fmt, ...) {
|
|
|
|
char msg[1024];
|
|
|
|
va_list ap;
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
printf("[offset %llu] %s\n", (unsigned long long)(rdbstate.rio ? rdbstate.rio->processed_bytes : 0), msg);
|
2012-10-22 11:44:20 +02:00
|
|
|
}
|
|
|
|
|
2016-07-01 11:59:25 +02:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
/* 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);
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
rdbCheckError("Server crash checking the specified RDB file!");
|
|
|
|
exit(1);
|
2010-03-13 15:55:42 +01:00
|
|
|
}
|
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
void rdbCheckSetupSignals(void) {
|
|
|
|
struct sigaction act;
|
2010-03-13 15:55:42 +01:00
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
sigemptyset(&act.sa_mask);
|
|
|
|
act.sa_flags = SA_NODEFER | SA_RESETHAND | SA_SIGINFO;
|
2016-07-01 11:40:40 +02:00
|
|
|
act.sa_sigaction = rdbCheckHandleCrash;
|
2016-06-30 23:44:44 +02:00
|
|
|
sigaction(SIGSEGV, &act, NULL);
|
|
|
|
sigaction(SIGBUS, &act, NULL);
|
|
|
|
sigaction(SIGFPE, &act, NULL);
|
|
|
|
sigaction(SIGILL, &act, NULL);
|
2020-11-03 14:59:21 +02:00
|
|
|
sigaction(SIGABRT, &act, NULL);
|
2010-03-13 15:55:42 +01:00
|
|
|
}
|
|
|
|
|
2016-07-06 19:00:43 +02:00
|
|
|
/* Check the specified RDB file. Return 0 if the RDB looks sane, otherwise
|
2017-07-10 13:38:23 +02:00
|
|
|
* 1 is returned.
|
2023-04-17 14:05:36 -04:00
|
|
|
* The file is specified as a filename in 'rdbfilename' if 'fp' is NULL,
|
2017-07-10 13:38:23 +02:00
|
|
|
* otherwise the already open file 'fp' is checked. */
|
|
|
|
int redis_check_rdb(char *rdbfilename, FILE *fp) {
|
2016-06-30 23:44:44 +02:00
|
|
|
uint64_t dbid;
|
2021-06-16 14:45:49 +08:00
|
|
|
int selected_dbid = -1;
|
2016-06-30 23:44:44 +02:00
|
|
|
int type, rdbver;
|
|
|
|
char buf[1024];
|
|
|
|
long long expiretime, now = mstime();
|
2016-07-06 19:12:24 +02:00
|
|
|
static rio rdb; /* Pointed by global struct riostate. */
|
Implement Multi Part AOF mechanism to avoid AOFRW overheads. (#9788)
Implement Multi-Part AOF mechanism to avoid overheads during AOFRW.
Introducing a folder with multiple AOF files tracked by a manifest file.
The main issues with the the original AOFRW mechanism are:
* buffering of commands that are processed during rewrite (consuming a lot of RAM)
* freezes of the main process when the AOFRW completes to drain the remaining part of the buffer and fsync it.
* double disk IO for the data that arrives during AOFRW (had to be written to both the old and new AOF files)
The main modifications of this PR:
1. Remove the AOF rewrite buffer and related code.
2. Divide the AOF into multiple files, they are classified as two types, one is the the `BASE` type,
it represents the full amount of data (Maybe AOF or RDB format) after each AOFRW, there is only
one `BASE` file at most. The second is `INCR` type, may have more than one. They represent the
incremental commands since the last AOFRW.
3. Use a AOF manifest file to record and manage these AOF files mentioned above.
4. The original configuration of `appendfilename` will be the base part of the new file name, for example:
`appendonly.aof.1.base.rdb` and `appendonly.aof.2.incr.aof`
5. Add manifest-related TCL tests, and modified some existing tests that depend on the `appendfilename`
6. Remove the `aof_rewrite_buffer_length` field in info.
7. Add `aof-disable-auto-gc` configuration. By default we're automatically deleting HISTORY type AOFs.
It also gives users the opportunity to preserve the history AOFs. just for testing use now.
8. Add AOFRW limiting measure. When the AOFRW failures reaches the threshold (3 times now),
we will delay the execution of the next AOFRW by 1 minute. If the next AOFRW also fails, it will be
delayed by 2 minutes. The next is 4, 8, 16, the maximum delay is 60 minutes (1 hour). During the limit
period, we can still use the 'bgrewriteaof' command to execute AOFRW immediately.
9. Support upgrade (load) data from old version redis.
10. Add `appenddirname` configuration, as the directory name of the append only files. All AOF files and
manifest file will be placed in this directory.
11. Only the last AOF file (BASE or INCR) can be truncated. Otherwise redis will exit even if
`aof-load-truncated` is enabled.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-04 01:14:13 +08:00
|
|
|
struct stat sb;
|
2016-06-30 23:44:44 +02:00
|
|
|
|
2017-07-10 13:38:23 +02:00
|
|
|
int closefile = (fp == NULL);
|
|
|
|
if (fp == NULL && (fp = fopen(rdbfilename, "r")) == NULL) return 1;
|
2016-06-30 23:44:44 +02:00
|
|
|
|
Implement Multi Part AOF mechanism to avoid AOFRW overheads. (#9788)
Implement Multi-Part AOF mechanism to avoid overheads during AOFRW.
Introducing a folder with multiple AOF files tracked by a manifest file.
The main issues with the the original AOFRW mechanism are:
* buffering of commands that are processed during rewrite (consuming a lot of RAM)
* freezes of the main process when the AOFRW completes to drain the remaining part of the buffer and fsync it.
* double disk IO for the data that arrives during AOFRW (had to be written to both the old and new AOF files)
The main modifications of this PR:
1. Remove the AOF rewrite buffer and related code.
2. Divide the AOF into multiple files, they are classified as two types, one is the the `BASE` type,
it represents the full amount of data (Maybe AOF or RDB format) after each AOFRW, there is only
one `BASE` file at most. The second is `INCR` type, may have more than one. They represent the
incremental commands since the last AOFRW.
3. Use a AOF manifest file to record and manage these AOF files mentioned above.
4. The original configuration of `appendfilename` will be the base part of the new file name, for example:
`appendonly.aof.1.base.rdb` and `appendonly.aof.2.incr.aof`
5. Add manifest-related TCL tests, and modified some existing tests that depend on the `appendfilename`
6. Remove the `aof_rewrite_buffer_length` field in info.
7. Add `aof-disable-auto-gc` configuration. By default we're automatically deleting HISTORY type AOFs.
It also gives users the opportunity to preserve the history AOFs. just for testing use now.
8. Add AOFRW limiting measure. When the AOFRW failures reaches the threshold (3 times now),
we will delay the execution of the next AOFRW by 1 minute. If the next AOFRW also fails, it will be
delayed by 2 minutes. The next is 4, 8, 16, the maximum delay is 60 minutes (1 hour). During the limit
period, we can still use the 'bgrewriteaof' command to execute AOFRW immediately.
9. Support upgrade (load) data from old version redis.
10. Add `appenddirname` configuration, as the directory name of the append only files. All AOF files and
manifest file will be placed in this directory.
11. Only the last AOF file (BASE or INCR) can be truncated. Otherwise redis will exit even if
`aof-load-truncated` is enabled.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-04 01:14:13 +08:00
|
|
|
if (fstat(fileno(fp), &sb) == -1) sb.st_size = 0;
|
|
|
|
|
|
|
|
startLoadingFile(sb.st_size, rdbfilename, RDBFLAGS_NONE);
|
2016-06-30 23:44:44 +02:00
|
|
|
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");
|
2017-10-13 13:20:45 -04:00
|
|
|
goto err;
|
2010-03-13 15:55:42 +01:00
|
|
|
}
|
2016-06-30 23:44:44 +02:00
|
|
|
rdbver = atoi(buf + 5);
|
|
|
|
if (rdbver < 1 || rdbver > RDB_VERSION) {
|
|
|
|
rdbCheckError("Can't handle RDB format version %d", rdbver);
|
2017-10-13 13:20:45 -04:00
|
|
|
goto err;
|
2010-03-13 15:55:42 +01:00
|
|
|
}
|
|
|
|
|
2018-03-15 16:39:33 +01:00
|
|
|
expiretime = -1;
|
2016-06-30 23:44:44 +02:00
|
|
|
while (1) {
|
|
|
|
robj *key, *val;
|
|
|
|
|
|
|
|
/* Read type. */
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_TYPE;
|
2016-06-30 23:44:44 +02:00
|
|
|
if ((type = rdbLoadType(&rdb)) == -1) goto eoferr;
|
|
|
|
|
|
|
|
/* Handle special types. */
|
|
|
|
if (type == RDB_OPCODE_EXPIRETIME) {
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;
|
2016-06-30 23:44:44 +02:00
|
|
|
/* 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. */
|
2019-07-17 17:30:02 +02:00
|
|
|
expiretime = rdbLoadTime(&rdb);
|
|
|
|
expiretime *= 1000;
|
|
|
|
if (rioGetReadError(&rdb)) goto eoferr;
|
2018-03-15 16:39:33 +01:00
|
|
|
continue; /* Read next opcode. */
|
2016-06-30 23:44:44 +02:00
|
|
|
} else if (type == RDB_OPCODE_EXPIRETIME_MS) {
|
|
|
|
/* EXPIRETIME_MS: milliseconds precision expire times introduced
|
|
|
|
* with RDB v3. Like EXPIRETIME but no with more precision. */
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_EXPIRE;
|
2019-07-17 17:30:02 +02:00
|
|
|
expiretime = rdbLoadMillisecondTime(&rdb, rdbver);
|
|
|
|
if (rioGetReadError(&rdb)) goto eoferr;
|
2018-03-15 16:39:33 +01:00
|
|
|
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. */
|
2016-06-30 23:44:44 +02:00
|
|
|
} 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. */
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_LEN;
|
2016-06-30 23:44:44 +02:00
|
|
|
if ((dbid = rdbLoadLen(&rdb, NULL)) == RDB_LENERR) goto eoferr;
|
2021-05-03 08:33:21 +03:00
|
|
|
rdbCheckInfo("Selecting DB ID %llu", (unsigned long long)dbid);
|
2021-06-16 14:45:49 +08:00
|
|
|
selected_dbid = dbid;
|
2016-06-30 23:44:44 +02:00
|
|
|
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;
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_LEN;
|
2016-06-30 23:44:44 +02:00
|
|
|
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
|
2021-06-10 20:39:33 +08:00
|
|
|
* are required to skip AUX fields they don't understand.
|
2016-06-30 23:44:44 +02:00
|
|
|
*
|
|
|
|
* An AUX field is composed of two strings: key and value. */
|
|
|
|
robj *auxkey, *auxval;
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_AUX;
|
2016-06-30 23:44:44 +02:00
|
|
|
if ((auxkey = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
|
2022-06-13 23:23:53 +08:00
|
|
|
if ((auxval = rdbLoadStringObject(&rdb)) == NULL) {
|
|
|
|
decrRefCount(auxkey);
|
|
|
|
goto eoferr;
|
|
|
|
}
|
2016-06-30 23:44:44 +02:00
|
|
|
|
2016-07-01 09:52:35 +02:00
|
|
|
rdbCheckInfo("AUX FIELD %s = '%s'", (char *)auxkey->ptr, (char *)auxval->ptr);
|
2016-06-30 23:44:44 +02:00
|
|
|
decrRefCount(auxkey);
|
|
|
|
decrRefCount(auxval);
|
|
|
|
continue; /* Read type again. */
|
2020-09-22 10:18:07 +03:00
|
|
|
} 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;
|
2022-06-13 23:23:53 +08:00
|
|
|
if (when_opcode != RDB_MODULE_OPCODE_UINT) {
|
|
|
|
rdbCheckError("bad when_opcode");
|
|
|
|
goto err;
|
|
|
|
}
|
2020-09-22 10:18:07 +03:00
|
|
|
|
|
|
|
char name[10];
|
|
|
|
moduleTypeNameByID(name, moduleid);
|
|
|
|
rdbCheckInfo("MODULE AUX for: %s", name);
|
|
|
|
|
|
|
|
robj *o = rdbLoadCheckModuleValue(&rdb, name);
|
|
|
|
decrRefCount(o);
|
|
|
|
continue; /* Read type again. */
|
2022-08-15 21:41:44 +03:00
|
|
|
} else if (type == RDB_OPCODE_FUNCTION_PRE_GA) {
|
|
|
|
rdbCheckError("Pre-release function format not supported %d", rdbver);
|
|
|
|
goto err;
|
|
|
|
} else if (type == RDB_OPCODE_FUNCTION2) {
|
2022-01-20 11:10:33 +02:00
|
|
|
sds err = NULL;
|
2022-04-05 10:27:24 +03:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_FUNCTIONS;
|
2022-08-15 21:41:44 +03:00
|
|
|
if (rdbFunctionLoad(&rdb, rdbver, NULL, 0, &err) != C_OK) {
|
2022-01-20 11:10:33 +02:00
|
|
|
rdbCheckError("Failed loading library, %s", err);
|
|
|
|
sdsfree(err);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
continue;
|
2016-07-01 09:36:52 +02:00
|
|
|
} else {
|
|
|
|
if (!rdbIsObjectType(type)) {
|
|
|
|
rdbCheckError("Invalid object type: %d", type);
|
2017-10-13 13:20:45 -04:00
|
|
|
goto err;
|
2016-07-01 09:36:52 +02:00
|
|
|
}
|
2016-07-01 11:40:40 +02:00
|
|
|
rdbstate.key_type = type;
|
2016-06-30 23:44:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read key */
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_KEY;
|
2016-06-30 23:44:44 +02:00
|
|
|
if ((key = rdbLoadStringObject(&rdb)) == NULL) goto eoferr;
|
|
|
|
rdbstate.key = key;
|
|
|
|
rdbstate.keys++;
|
|
|
|
/* Read value */
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_READ_OBJECT_VALUE;
|
2021-08-06 03:42:20 +08:00
|
|
|
if ((val = rdbLoadObject(type, &rdb, key->ptr, selected_dbid, NULL)) == NULL) goto eoferr;
|
2018-07-12 21:21:37 +08:00
|
|
|
/* Check if the key already expired. */
|
|
|
|
if (expiretime != -1 && expiretime < now) rdbstate.already_expired++;
|
2016-06-30 23:44:44 +02:00
|
|
|
if (expiretime != -1) rdbstate.expires++;
|
|
|
|
rdbstate.key = NULL;
|
|
|
|
decrRefCount(key);
|
|
|
|
decrRefCount(val);
|
2016-07-01 11:40:40 +02:00
|
|
|
rdbstate.key_type = -1;
|
2018-03-15 16:39:33 +01:00
|
|
|
expiretime = -1;
|
2016-06-30 23:44:44 +02:00
|
|
|
}
|
|
|
|
/* Verify the checksum if RDB version is >= 5 */
|
|
|
|
if (rdbver >= 5 && server.rdb_checksum) {
|
|
|
|
uint64_t cksum, expected = rdb.cksum;
|
|
|
|
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbstate.doing = RDB_CHECK_DOING_CHECK_SUM;
|
2016-06-30 23:44:44 +02:00
|
|
|
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");
|
2017-10-13 13:20:45 -04:00
|
|
|
goto err;
|
2016-07-01 09:52:35 +02:00
|
|
|
} else {
|
|
|
|
rdbCheckInfo("Checksum OK");
|
2016-06-30 23:44:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 13:38:23 +02:00
|
|
|
if (closefile) fclose(fp);
|
2019-10-29 17:59:09 +02:00
|
|
|
stopLoading(1);
|
2010-03-13 15:55:42 +01:00
|
|
|
return 0;
|
|
|
|
|
2016-06-30 23:44:44 +02:00
|
|
|
eoferr: /* unexpected end of file is handled here with a fatal exit */
|
2016-07-01 11:59:25 +02:00
|
|
|
if (rdbstate.error_set) {
|
|
|
|
rdbCheckError(rdbstate.error);
|
|
|
|
} else {
|
|
|
|
rdbCheckError("Unexpected EOF reading RDB file");
|
|
|
|
}
|
2017-10-13 13:20:45 -04:00
|
|
|
err:
|
|
|
|
if (closefile) fclose(fp);
|
2019-10-29 17:59:09 +02:00
|
|
|
stopLoading(0);
|
2010-03-13 15:55:42 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-04-09 01:24:03 -07:00
|
|
|
/* RDB check main: called form server.c when the server is executed with the
|
|
|
|
* valkey-check-rdb alias, on during RDB loading errors.
|
2016-06-30 23:44:44 +02:00
|
|
|
*
|
2017-07-10 13:38:23 +02:00
|
|
|
* The function works in two ways: can be called with argc/argv as a
|
|
|
|
* standalone executable, or called with a non NULL 'fp' argument if we
|
|
|
|
* already have an open file to check. This happens when the function
|
|
|
|
* is used to check an RDB preamble inside an AOF file.
|
|
|
|
*
|
|
|
|
* When called with fp = NULL, the function never returns, but exits with the
|
|
|
|
* status code according to success (RDB is sane) or error (RDB is corrupted).
|
|
|
|
* Otherwise if called with a non NULL fp, the function returns C_OK or
|
|
|
|
* C_ERR depending on the success or failure. */
|
|
|
|
int redis_check_rdb_main(int argc, char **argv, FILE *fp) {
|
2020-12-23 05:52:07 -08:00
|
|
|
struct timeval tv;
|
|
|
|
|
2017-07-10 13:38:23 +02:00
|
|
|
if (argc != 2 && fp == NULL) {
|
2015-02-03 10:25:01 +01:00
|
|
|
fprintf(stderr, "Usage: %s <rdb-file-name>\n", argv[0]);
|
|
|
|
exit(1);
|
2021-12-21 20:11:18 +08:00
|
|
|
} else if (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version")) {
|
2023-12-21 19:51:46 +08:00
|
|
|
sds version = getVersion();
|
2024-04-04 16:46:17 +08:00
|
|
|
printf("valkey-check-rdb %s\n", version);
|
2021-12-21 20:11:18 +08:00
|
|
|
sdsfree(version);
|
|
|
|
exit(0);
|
2015-02-03 10:25:01 +01:00
|
|
|
}
|
2020-12-23 05:52:07 -08:00
|
|
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
init_genrand64(((long long)tv.tv_sec * 1000000 + tv.tv_usec) ^ getpid());
|
|
|
|
|
2016-07-06 19:05:44 +02:00
|
|
|
/* In order to call the loading functions we need to create the shared
|
|
|
|
* integer objects, however since this function may be called from
|
2024-04-09 01:24:03 -07:00
|
|
|
* an already initialized server instance, check if we really need to. */
|
2016-07-06 19:05:44 +02:00
|
|
|
if (shared.integers[0] == NULL) createSharedObjects();
|
2016-06-30 23:44:44 +02:00
|
|
|
server.loading_process_events_interval_bytes = 0;
|
Sanitize dump payload: ziplist, listpack, zipmap, intset, stream
When loading an encoded payload we will at least do a shallow validation to
check that the size that's encoded in the payload matches the size of the
allocation.
This let's us later use this encoded size to make sure the various offsets
inside encoded payload don't reach outside the allocation, if they do, we'll
assert/panic, but at least we won't segfault or smear memory.
We can also do 'deep' validation which runs on all the records of the encoded
payload and validates that they don't contain invalid offsets. This lets us
detect corruptions early and reject a RESTORE command rather than accepting
it and asserting (crashing) later when accessing that payload via some command.
configuration:
- adding ACL flag skip-sanitize-payload
- adding config sanitize-dump-payload [yes/no/clients]
For now, we don't have a good way to ensure MIGRATE in cluster resharding isn't
being slowed down by these sanitation, so i'm setting the default value to `no`,
but later on it should be set to `clients` by default.
changes:
- changing rdbReportError not to `exit` in RESTORE command
- adding a new stat to be able to later check if cluster MIGRATE isn't being
slowed down by sanitation.
2020-08-13 16:41:05 +03:00
|
|
|
server.sanitize_dump_payload = SANITIZE_DUMP_YES;
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbCheckMode = 1;
|
2016-06-30 23:44:44 +02:00
|
|
|
rdbCheckInfo("Checking RDB file %s", argv[1]);
|
|
|
|
rdbCheckSetupSignals();
|
2017-07-10 13:38:23 +02:00
|
|
|
int retval = redis_check_rdb(argv[1], fp);
|
2016-06-30 23:44:44 +02:00
|
|
|
if (retval == 0) {
|
|
|
|
rdbCheckInfo("\\o/ RDB looks OK! \\o/");
|
2016-07-01 09:36:52 +02:00
|
|
|
rdbShowGenericInfo();
|
2016-06-30 23:44:44 +02:00
|
|
|
}
|
2017-07-10 13:38:23 +02:00
|
|
|
if (fp) return (retval == 0) ? C_OK : C_ERR;
|
2016-06-30 23:44:44 +02:00
|
|
|
exit(retval);
|
2015-02-03 10:25:01 +01:00
|
|
|
}
|