Skip to content
Permalink
Browse files

增加线上加载rdb功能

  • Loading branch information...
arloor committed Sep 4, 2019
1 parent 31f984c commit a8c58feb26861106a38b8293180906df9b4a9797
Showing with 61 additions and 2 deletions.
  1. +13 −0 README.md
  2. BIN dump
  3. +10 −1 src/help.h
  4. +22 −0 src/rdb.c
  5. +2 −1 src/server.c
  6. +2 −0 src/server.h
  7. +12 −0 test.sh
@@ -1,3 +1,16 @@
这是修改过的redis源码。增加了一个`LOADRDB <filename>`的 命令,用于在运行时动态加载rdb文件。

为什么可以?在阅读了replicate.c后,我发现在从节点收到rdb文件后,直接调用`rdbload`函数加载该文件,这个过程也是在运行时进行的,这就是依据。

test.sh 进行了简单的测试。

下一步需要做的:如果loadrdb指定的filename与redis.conf中指定的`dbfilename`相同,则不进行导入。因为可能在一边写他,一遍读他,后果就是 redis崩溃

参考博客[线上加载rdb、aof]](https://blog.csdn.net/laowxl/article/details/68924510)


============================================================

This README is just a fast *quick start* document. You can find more detailed documentation at http://redis.io.

What is Redis?
BIN +170 Bytes dump
Binary file not shown.
@@ -1011,7 +1011,16 @@ struct commandHelp {
"destination numkeys key [key ...] [WEIGHTS weight] [AGGREGATE SUM|MIN|MAX]",
"Add multiple sorted sets and store the resulting sorted set in a new key",
4,
"2.0.0" }
"2.0.0" },
{ "LOADRDB",

"rdb filename",

"load rdb file online,which can not repeat !!!",

9,

"3.0.7"}
};

#endif
@@ -2019,3 +2019,25 @@ void bgsaveCommand(client *c) {
}


void loadrdbCommand(client *c) {
//从别的博客抄的,但是阅读源码之后觉得没有必要
// if (server.rdb_child_pid != -1) {
// addReplyError(c,"Background save already in progress");
// } else if (server.aof_child_pid != -1) {
// addReplyError(c,"Can't BGSAVE while AOF log rewriting is in progress");
// } else if (c->argc != 2){
if (c->argc != 2){ //实际不需要,在上层有做参数个数校验
addReplyStatus(c,"USAGE: LOADRDB <rdbfilename>");
return;
} else if (rdbLoad(c->argv[1]->ptr,NULL) == C_OK) {
serverLog(LL_WARNING,server.rdb_filename);
//todo:如果rdbload的参数就是server导出的rdb文件名,则不进行导入,因为会崩溃
addReplyStatus(c,"online loadrdb DONE!");
} else {
addReplyError(c,"online load rdb FAILED! PLEASE check the filename");

}


}

@@ -304,7 +304,8 @@ struct redisCommand redisCommandTable[] = {
{"pfdebug",pfdebugCommand,-3,"w",0,NULL,0,0,0,0,0},
{"post",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0},
{"host:",securityWarningCommand,-1,"lt",0,NULL,0,0,0,0,0},
{"latency",latencyCommand,-2,"aslt",0,NULL,0,0,0,0,0}
{"latency",latencyCommand,-2,"aslt",0,NULL,0,0,0,0,0},
{"loadrdb",loadrdbCommand,2,"ar",0,NULL,0,0,0,0,0},
};

/*============================ Utility functions ============================ */
@@ -2020,3 +2020,5 @@ void xorDigest(unsigned char *digest, void *ptr, size_t len);
printf("-- MARK %s:%d --\n", __FILE__, __LINE__)

#endif

void loadrdbCommand(client *c);
12 test.sh
@@ -0,0 +1,12 @@
#! /bin/bash

src/redis-cli set a test
src/redis-cli BGSAVE
src/redis-cli get a
sleep 10
mv -f dump.rdb dump
src/redis-cli flushall
src/redis-cli get a
src/redis-cli loadrdb dump
# 等同于 (printf "loadrdb dump\r\n";sleep 1)|nc localhost 6379
src/redis-cli get a

0 comments on commit a8c58fe

Please sign in to comment.
You can’t perform that action at this time.