Skip to content

Commit fa3301a

Browse files
author
Nitai Caro
committed
Contribute threadsave
1 parent 1cd73bc commit fa3301a

20 files changed

Lines changed: 2233 additions & 300 deletions

src/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ ENGINE_SERVER_OBJ = \
561561
vset.o \
562562
ziplist.o \
563563
zipmap.o \
564-
zmalloc.o
564+
zmalloc.o \
565+
threadsave.o
565566
ENGINE_SERVER_OBJ+=$(ENGINE_TRACE_OBJ)
566567
ENGINE_CLI_NAME=$(ENGINE_NAME)-cli$(PROG_SUFFIX)
567568
ENGINE_CLI_OBJ = \

src/bgiteration.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ static bool isDeleteCmd(struct serverCommand *cmd) {
5454
}
5555

5656

57-
static bool onValkeyMainThread(void) {
58-
return (pthread_equal(server.main_thread_id, pthread_self()) != 0);
59-
}
60-
6157
/* Parse a parameters robj, extracting a valid DBID.
6258
* Returns FALSE if DBID isn't valid.
6359
*/

src/commands.def

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7170,6 +7170,7 @@ struct COMMAND_STRUCT ACL_Subcommands[] = {
71707170
commandHistory BGSAVE_History[] = {
71717171
{"3.2.2","Added the `SCHEDULE` option."},
71727172
{"8.1.0","Added the `CANCEL` option."},
7173+
{"10.0.0","Added the `FORK` and `THREAD` options. `SCHEDULE` can be combined with `FORK` or `THREAD`."},
71737174
};
71747175
#endif
71757176

@@ -7183,15 +7184,17 @@ commandHistory BGSAVE_History[] = {
71837184
#define BGSAVE_Keyspecs NULL
71847185
#endif
71857186

7186-
/* BGSAVE operation argument table */
7187-
struct COMMAND_ARG BGSAVE_operation_Subargs[] = {
7188-
{MAKE_ARG("schedule",ARG_TYPE_PURE_TOKEN,-1,"SCHEDULE",NULL,"3.2.2",CMD_ARG_NONE,0,NULL)},
7189-
{MAKE_ARG("cancel",ARG_TYPE_PURE_TOKEN,-1,"CANCEL",NULL,"8.1.0",CMD_ARG_NONE,0,NULL)},
7187+
/* BGSAVE save_type argument table */
7188+
struct COMMAND_ARG BGSAVE_save_type_Subargs[] = {
7189+
{MAKE_ARG("fork",ARG_TYPE_PURE_TOKEN,-1,"FORK",NULL,NULL,CMD_ARG_NONE,0,NULL)},
7190+
{MAKE_ARG("thread",ARG_TYPE_PURE_TOKEN,-1,"THREAD",NULL,NULL,CMD_ARG_NONE,0,NULL)},
71907191
};
71917192

71927193
/* BGSAVE argument table */
71937194
struct COMMAND_ARG BGSAVE_Args[] = {
7194-
{MAKE_ARG("operation",ARG_TYPE_ONEOF,-1,NULL,NULL,NULL,CMD_ARG_OPTIONAL,2,NULL),.subargs=BGSAVE_operation_Subargs},
7195+
{MAKE_ARG("schedule",ARG_TYPE_PURE_TOKEN,-1,"SCHEDULE",NULL,"3.2.2",CMD_ARG_OPTIONAL,0,NULL)},
7196+
{MAKE_ARG("save-type",ARG_TYPE_ONEOF,-1,NULL,NULL,"10.0.0",CMD_ARG_OPTIONAL,2,NULL),.subargs=BGSAVE_save_type_Subargs},
7197+
{MAKE_ARG("cancel",ARG_TYPE_PURE_TOKEN,-1,"CANCEL",NULL,"8.1.0",CMD_ARG_OPTIONAL,0,NULL)},
71957198
};
71967199

71977200
/********** COMMAND COUNT ********************/
@@ -11992,7 +11995,7 @@ struct COMMAND_STRUCT serverCommandTable[] = {
1199211995
/* server */
1199311996
{MAKE_CMD("acl","A container for Access List Control commands.","Depends on subcommand.","6.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,ACL_History,0,ACL_Tips,0,NULL,-2,CMD_SENTINEL,ACL_CATEGORY_SLOW,NULL,ACL_Keyspecs,0,NULL,0),.subcommands=ACL_Subcommands},
1199411997
{MAKE_CMD("bgrewriteaof","Asynchronously rewrites the append-only file to disk.","O(1)","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,BGREWRITEAOF_History,0,BGREWRITEAOF_Tips,0,bgrewriteaofCommand,1,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,BGREWRITEAOF_Keyspecs,0,NULL,0)},
11995-
{MAKE_CMD("bgsave","Asynchronously saves the database(s) to disk.","O(1)","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,BGSAVE_History,2,BGSAVE_Tips,0,bgsaveCommand,-1,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,BGSAVE_Keyspecs,0,NULL,1),.args=BGSAVE_Args},
11998+
{MAKE_CMD("bgsave","Asynchronously saves the database(s) to disk.","O(1)","1.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,BGSAVE_History,3,BGSAVE_Tips,0,bgsaveCommand,-1,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT,ACL_CATEGORY_ADMIN|ACL_CATEGORY_DANGEROUS|ACL_CATEGORY_SLOW,NULL,BGSAVE_Keyspecs,0,NULL,3),.args=BGSAVE_Args},
1199611999
{MAKE_CMD("command","Returns detailed information about all commands.","O(N) where N is the total number of commands","2.8.13",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,COMMAND_History,0,COMMAND_Tips,1,commandCommand,-1,CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_CONNECTION|ACL_CATEGORY_SLOW,NULL,COMMAND_Keyspecs,0,NULL,0),.subcommands=COMMAND_Subcommands},
1199712000
{MAKE_CMD("commandlog","A container for command log commands.","Depends on subcommand.","8.1.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,COMMANDLOG_History,0,COMMANDLOG_Tips,0,NULL,-2,0,ACL_CATEGORY_SLOW,NULL,COMMANDLOG_Keyspecs,0,NULL,0),.subcommands=COMMANDLOG_Subcommands},
1199812001
{MAKE_CMD("config","A container for server configuration commands.","Depends on subcommand.","2.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,CONFIG_History,0,CONFIG_Tips,0,NULL,-2,0,ACL_CATEGORY_SLOW,NULL,CONFIG_Keyspecs,0,NULL,0),.subcommands=CONFIG_Subcommands},

src/commands/bgsave.json

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
[
1515
"8.1.0",
1616
"Added the `CANCEL` option."
17+
],
18+
[
19+
"10.0.0",
20+
"Added the `FORK` and `THREAD` options. `SCHEDULE` can be combined with `FORK` or `THREAD`."
1721
]
1822
],
1923
"command_flags": [
@@ -23,29 +27,45 @@
2327
],
2428
"arguments": [
2529
{
26-
"name": "operation",
30+
"name": "schedule",
31+
"token": "SCHEDULE",
32+
"type": "pure-token",
33+
"optional": true,
34+
"since": "3.2.2"
35+
},
36+
{
37+
"name": "save-type",
2738
"type": "oneof",
2839
"optional": true,
40+
"since": "10.0.0",
2941
"arguments": [
3042
{
31-
"name": "schedule",
32-
"token": "SCHEDULE",
33-
"type": "pure-token",
34-
"since": "3.2.2"
43+
"name": "fork",
44+
"token": "FORK",
45+
"type": "pure-token"
3546
},
3647
{
37-
"name": "cancel",
38-
"token": "CANCEL",
39-
"type": "pure-token",
40-
"since": "8.1.0"
48+
"name": "thread",
49+
"token": "THREAD",
50+
"type": "pure-token"
4151
}
4252
]
53+
},
54+
{
55+
"name": "cancel",
56+
"token": "CANCEL",
57+
"type": "pure-token",
58+
"optional": true,
59+
"since": "8.1.0"
4360
}
4461
],
4562
"reply_schema": {
4663
"oneOf": [
4764
{
48-
"const": "Background saving started"
65+
"const": "Background saving (fork) started"
66+
},
67+
{
68+
"const": "Background saving (thread) started"
4969
},
5070
{
5171
"const": "Background saving scheduled"

src/config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,6 +3256,7 @@ standardConfig static_configs[] = {
32563256
createBoolConfig("rdb-del-sync-files", NULL, MODIFIABLE_CONFIG, server.rdb_del_sync_files, 0, NULL, NULL),
32573257
createBoolConfig("activerehashing", NULL, MODIFIABLE_CONFIG, server.activerehashing, 1, NULL, NULL),
32583258
createBoolConfig("stop-writes-on-bgsave-error", NULL, MODIFIABLE_CONFIG, server.stop_writes_on_bgsave_err, 1, NULL, NULL),
3259+
createBoolConfig("threadsave-enabled-for-backup", NULL, MODIFIABLE_CONFIG, server.threadsave_enabled_for_backup, 0, NULL, NULL),
32593260
createBoolConfig("set-proc-title", NULL, IMMUTABLE_CONFIG, server.set_proc_title, 1, NULL, NULL), /* Should setproctitle be used? */
32603261
createBoolConfig("lazyfree-lazy-eviction", NULL, DEBUG_CONFIG | MODIFIABLE_CONFIG, server.lazyfree_lazy_eviction, 1, NULL, NULL),
32613262
createBoolConfig("lazyfree-lazy-expire", NULL, DEBUG_CONFIG | MODIFIABLE_CONFIG, server.lazyfree_lazy_expire, 1, NULL, NULL),

src/db.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "vector.h"
3939
#include "expire.h"
4040
#include "bgiteration.h"
41+
#include "threadsave.h"
4142

4243
/*-----------------------------------------------------------------------------
4344
* C-level DB API
@@ -823,7 +824,8 @@ int getFlushCommandFlags(client *c, int *flags) {
823824
/* Flushes the whole server data set. */
824825
void flushAllDataAndResetRDB(int flags) {
825826
server.dirty += emptyData(-1, flags, NULL);
826-
if (server.child_type == CHILD_TYPE_RDB) killRDBChild();
827+
if (isForkBgsaveInProgress()) killRDBChild();
828+
if (isThreadBgsaveInProgress()) threadsaveCancel();
827829
if (server.child_type == CHILD_TYPE_SLOT_MIGRATION) killSlotMigrationChild();
828830
if (server.saveparamslen > 0) {
829831
rdbSaveInfo rsi, *rsiptr;

src/module.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
#include "scripting_engine.h"
7272
#include "cluster_migrateslots.h"
7373
#include "bgiteration.h"
74+
#include "threadsave.h"
7475
#include <dlfcn.h>
7576
#include <sys/stat.h>
7677
#include <sys/wait.h>
@@ -13746,7 +13747,8 @@ int VM_RdbLoad(ValkeyModuleCtx *ctx, ValkeyModuleRdbStream *stream, int flags) {
1374613747

1374713748
/* Kill existing RDB fork as it is saving outdated data. Also killing it
1374813749
* will prevent COW memory issue. */
13749-
if (server.child_type == CHILD_TYPE_RDB) killRDBChild();
13750+
if (isForkBgsaveInProgress()) killRDBChild();
13751+
if (isThreadBgsaveInProgress()) threadsaveCancel();
1375013752

1375113753
/* Kill existing slot migration fork as it is saving outdated data. Also killing it
1375213754
* will prevent COW memory issue. */

0 commit comments

Comments
 (0)