Skip to content

Commit 2458e54

Browse files
authored
RM_GetContextFlags provides indication that we're in a fork child (#7783)
1 parent dfe9714 commit 2458e54

8 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/aof.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,15 +1603,15 @@ int rewriteAppendOnlyFileBackground(void) {
16031603
if (hasActiveChildProcess()) return C_ERR;
16041604
if (aofCreatePipes() != C_OK) return C_ERR;
16051605
openChildInfoPipe();
1606-
if ((childpid = redisFork()) == 0) {
1606+
if ((childpid = redisFork(CHILD_TYPE_AOF)) == 0) {
16071607
char tmpfile[256];
16081608

16091609
/* Child */
16101610
redisSetProcTitle("redis-aof-rewrite");
16111611
redisSetCpuAffinity(server.aof_rewrite_cpulist);
16121612
snprintf(tmpfile,256,"temp-rewriteaof-bg-%d.aof", (int) getpid());
16131613
if (rewriteAppendOnlyFile(tmpfile) == C_OK) {
1614-
sendChildCOWInfo(CHILD_INFO_TYPE_AOF, "AOF rewrite");
1614+
sendChildCOWInfo(CHILD_TYPE_AOF, "AOF rewrite");
16151615
exitFromChild(0);
16161616
} else {
16171617
exitFromChild(1);

src/childinfo.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ void receiveChildInfo(void) {
7676
if (read(server.child_info_pipe[0],&server.child_info_data,wlen) == wlen &&
7777
server.child_info_data.magic == CHILD_INFO_MAGIC)
7878
{
79-
if (server.child_info_data.process_type == CHILD_INFO_TYPE_RDB) {
79+
if (server.child_info_data.process_type == CHILD_TYPE_RDB) {
8080
server.stat_rdb_cow_bytes = server.child_info_data.cow_size;
81-
} else if (server.child_info_data.process_type == CHILD_INFO_TYPE_AOF) {
81+
} else if (server.child_info_data.process_type == CHILD_TYPE_AOF) {
8282
server.stat_aof_cow_bytes = server.child_info_data.cow_size;
83-
} else if (server.child_info_data.process_type == CHILD_INFO_TYPE_MODULE) {
83+
} else if (server.child_info_data.process_type == CHILD_TYPE_MODULE) {
8484
server.stat_module_cow_bytes = server.child_info_data.cow_size;
8585
}
8686
}

src/module.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,7 @@ int RM_GetContextFlags(RedisModuleCtx *ctx) {
19911991

19921992
/* Presence of children processes. */
19931993
if (hasActiveChildProcess()) flags |= REDISMODULE_CTX_FLAGS_ACTIVE_CHILD;
1994+
if (server.in_fork_child) flags |= REDISMODULE_CTX_FLAGS_IS_CHILD;
19941995

19951996
return flags;
19961997
}
@@ -6899,7 +6900,7 @@ int RM_Fork(RedisModuleForkDoneHandler cb, void *user_data) {
68996900
}
69006901

69016902
openChildInfoPipe();
6902-
if ((childpid = redisFork()) == 0) {
6903+
if ((childpid = redisFork(CHILD_TYPE_MODULE)) == 0) {
69036904
/* Child */
69046905
redisSetProcTitle("redis-module-fork");
69056906
} else if (childpid == -1) {
@@ -6919,7 +6920,7 @@ int RM_Fork(RedisModuleForkDoneHandler cb, void *user_data) {
69196920
* retcode will be provided to the done handler executed on the parent process.
69206921
*/
69216922
int RM_ExitFromChild(int retcode) {
6922-
sendChildCOWInfo(CHILD_INFO_TYPE_MODULE, "Module fork");
6923+
sendChildCOWInfo(CHILD_TYPE_MODULE, "Module fork");
69236924
exitFromChild(retcode);
69246925
return REDISMODULE_OK;
69256926
}

src/rdb.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,15 +1385,15 @@ int rdbSaveBackground(char *filename, rdbSaveInfo *rsi) {
13851385
server.lastbgsave_try = time(NULL);
13861386
openChildInfoPipe();
13871387

1388-
if ((childpid = redisFork()) == 0) {
1388+
if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {
13891389
int retval;
13901390

13911391
/* Child */
13921392
redisSetProcTitle("redis-rdb-bgsave");
13931393
redisSetCpuAffinity(server.bgsave_cpulist);
13941394
retval = rdbSave(filename,rsi);
13951395
if (retval == C_OK) {
1396-
sendChildCOWInfo(CHILD_INFO_TYPE_RDB, "RDB");
1396+
sendChildCOWInfo(CHILD_TYPE_RDB, "RDB");
13971397
}
13981398
exitFromChild((retval == C_OK) ? 0 : 1);
13991399
} else {
@@ -2561,7 +2561,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {
25612561

25622562
/* Create the child process. */
25632563
openChildInfoPipe();
2564-
if ((childpid = redisFork()) == 0) {
2564+
if ((childpid = redisFork(CHILD_TYPE_RDB)) == 0) {
25652565
/* Child */
25662566
int retval, dummy;
25672567
rio rdb;
@@ -2576,7 +2576,7 @@ int rdbSaveToSlavesSockets(rdbSaveInfo *rsi) {
25762576
retval = C_ERR;
25772577

25782578
if (retval == C_OK) {
2579-
sendChildCOWInfo(CHILD_INFO_TYPE_RDB, "RDB");
2579+
sendChildCOWInfo(CHILD_TYPE_RDB, "RDB");
25802580
}
25812581

25822582
rioFreeFd(&rdb);

src/redismodule.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112
#define REDISMODULE_CTX_FLAGS_ACTIVE_CHILD (1<<18)
113113
/* The next EXEC will fail due to dirty CAS (touched keys). */
114114
#define REDISMODULE_CTX_FLAGS_MULTI_DIRTY (1<<19)
115+
/* Redis is currently running inside background child process. */
116+
#define REDISMODULE_CTX_FLAGS_IS_CHILD (1<<20)
115117

116118
/* Keyspace changes notification classes. Every class is associated with a
117119
* character for configuration purposes.

src/scripting.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1858,7 +1858,7 @@ void ldbSendLogs(void) {
18581858
int ldbStartSession(client *c) {
18591859
ldb.forked = (c->flags & CLIENT_LUA_DEBUG_SYNC) == 0;
18601860
if (ldb.forked) {
1861-
pid_t cp = redisFork();
1861+
pid_t cp = redisFork(CHILD_TYPE_LDB);
18621862
if (cp == -1) {
18631863
addReplyError(c,"Fork() failed: can't run EVAL in debugging mode.");
18641864
return 0;

src/server.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,6 +2886,7 @@ void initServer(void) {
28862886
server.aof_state = server.aof_enabled ? AOF_ON : AOF_OFF;
28872887
server.hz = server.config_hz;
28882888
server.pid = getpid();
2889+
server.in_fork_child = CHILD_TYPE_NONE;
28892890
server.main_thread_id = pthread_self();
28902891
server.current_client = NULL;
28912892
server.fixed_time_expire = 0;
@@ -4989,7 +4990,8 @@ void removeSignalHandlers(void) {
49894990
* accepting writes because of a write error condition. */
49904991
static void sigKillChildHandler(int sig) {
49914992
UNUSED(sig);
4992-
serverLogFromHandler(LL_WARNING, "Received SIGUSR1 in child, exiting now.");
4993+
int level = server.in_fork_child == CHILD_TYPE_MODULE? LL_VERBOSE: LL_WARNING;
4994+
serverLogFromHandler(level, "Received SIGUSR1 in child, exiting now.");
49934995
exitFromChild(SERVER_CHILD_NOERROR_RETVAL);
49944996
}
49954997

@@ -5015,11 +5017,13 @@ void closeClildUnusedResourceAfterFork() {
50155017
close(server.cluster_config_file_lock_fd); /* don't care if this fails */
50165018
}
50175019

5018-
int redisFork() {
5020+
/* purpose is one of CHILD_TYPE_ types */
5021+
int redisFork(int purpose) {
50195022
int childpid;
50205023
long long start = ustime();
50215024
if ((childpid = fork()) == 0) {
50225025
/* Child */
5026+
server.in_fork_child = purpose;
50235027
setOOMScoreAdj(CONFIG_OOM_BGCHILD);
50245028
setupChildSignalHandlers();
50255029
closeClildUnusedResourceAfterFork();

src/server.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,9 +1046,11 @@ struct clusterState;
10461046
#endif
10471047

10481048
#define CHILD_INFO_MAGIC 0xC17DDA7A12345678LL
1049-
#define CHILD_INFO_TYPE_RDB 0
1050-
#define CHILD_INFO_TYPE_AOF 1
1051-
#define CHILD_INFO_TYPE_MODULE 3
1049+
#define CHILD_TYPE_NONE 0
1050+
#define CHILD_TYPE_RDB 1
1051+
#define CHILD_TYPE_AOF 2
1052+
#define CHILD_TYPE_LDB 3
1053+
#define CHILD_TYPE_MODULE 4
10521054

10531055
struct redisServer {
10541056
/* General */
@@ -1062,6 +1064,7 @@ struct redisServer {
10621064
the actual 'hz' field value if dynamic-hz
10631065
is enabled. */
10641066
int hz; /* serverCron() calls frequency in hertz */
1067+
int in_fork_child; /* indication that this is a fork child */
10651068
redisDb *db;
10661069
dict *commands; /* Command table */
10671070
dict *orig_commands; /* Command table before command renaming. */
@@ -1904,7 +1907,7 @@ void sendChildInfo(int process_type);
19041907
void receiveChildInfo(void);
19051908

19061909
/* Fork helpers */
1907-
int redisFork();
1910+
int redisFork(int type);
19081911
int hasActiveChildProcess();
19091912
void sendChildCOWInfo(int ptype, char *pname);
19101913

0 commit comments

Comments
 (0)