From 01cbf17ba2c0cd8ef7cbb232c0173deaf346788f Mon Sep 17 00:00:00 2001 From: guybe7 Date: Thu, 28 Jan 2021 15:38:49 +0100 Subject: [PATCH] Modules: Add event for fork child birth and termination (#8289) Useful to avoid doing background jobs that can cause excessive COW --- src/module.c | 10 ++++++++++ src/redismodule.h | 13 ++++++++++--- src/server.c | 6 ++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/module.c b/src/module.c index cabccfb7a..f3191bbc7 100644 --- a/src/module.c +++ b/src/module.c @@ -7899,6 +7899,14 @@ void ModuleForkDoneHandler(int exitcode, int bysignal) { * * `REDISMODULE_SUBEVENT_REPL_BACKUP_RESTORE` * * `REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD` * + * * RedisModuleEvent_ForkChild + * + * Called when a fork child (AOFRW, RDBSAVE, module fork...) is born/dies + * The following sub events are available: + * + * * `REDISMODULE_SUBEVENT_FORK_CHILD_BORN` + * * `REDISMODULE_SUBEVENT_FORK_CHILD_DIED` + * * The function returns REDISMODULE_OK if the module was successfully subscribed * for the specified event. If the API is called from a wrong context or unsupported event * is given then REDISMODULE_ERR is returned. */ @@ -7971,6 +7979,8 @@ int RM_IsSubEventSupported(RedisModuleEvent event, int64_t subevent) { return subevent < _REDISMODULE_SUBEVENT_SWAPDB_NEXT; case REDISMODULE_EVENT_REPL_BACKUP: return subevent < _REDISMODULE_SUBEVENT_REPL_BACKUP_NEXT; + case REDISMODULE_EVENT_FORK_CHILD: + return subevent < _REDISMODULE_SUBEVENT_FORK_CHILD_NEXT; default: break; } diff --git a/src/redismodule.h b/src/redismodule.h index 1b1304172..5d46b22f9 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -230,9 +230,8 @@ typedef uint64_t RedisModuleTimerID; #define REDISMODULE_EVENT_LOADING_PROGRESS 10 #define REDISMODULE_EVENT_SWAPDB 11 #define REDISMODULE_EVENT_REPL_BACKUP 12 - -/* Next event flag, should be updated if a new event added. */ -#define _REDISMODULE_EVENT_NEXT 13 +#define REDISMODULE_EVENT_FORK_CHILD 13 +#define _REDISMODULE_EVENT_NEXT 14 /* Next event flag, should be updated if a new event added. */ typedef struct RedisModuleEvent { uint64_t id; /* REDISMODULE_EVENT_... defines. */ @@ -295,6 +294,10 @@ static const RedisModuleEvent RedisModuleEvent_ReplBackup = { REDISMODULE_EVENT_REPL_BACKUP, 1 + }, + RedisModuleEvent_ForkChild = { + REDISMODULE_EVENT_FORK_CHILD, + 1 }; /* Those are values that are used for the 'subevent' callback argument. */ @@ -345,6 +348,10 @@ static const RedisModuleEvent #define REDISMODULE_SUBEVENT_REPL_BACKUP_DISCARD 2 #define _REDISMODULE_SUBEVENT_REPL_BACKUP_NEXT 3 +#define REDISMODULE_SUBEVENT_FORK_CHILD_BORN 0 +#define REDISMODULE_SUBEVENT_FORK_CHILD_DIED 1 +#define _REDISMODULE_SUBEVENT_FORK_CHILD_NEXT 2 + #define _REDISMODULE_SUBEVENT_SHUTDOWN_NEXT 0 #define _REDISMODULE_SUBEVENT_CRON_LOOP_NEXT 0 #define _REDISMODULE_SUBEVENT_SWAPDB_NEXT 0 diff --git a/src/server.c b/src/server.c index ce34af3df..9135c0867 100644 --- a/src/server.c +++ b/src/server.c @@ -1600,6 +1600,9 @@ void resetChildState() { server.stat_current_cow_bytes = 0; updateDictResizePolicy(); closeChildInfoPipe(); + moduleFireServerEvent(REDISMODULE_EVENT_FORK_CHILD, + REDISMODULE_SUBEVENT_FORK_CHILD_DIED, + NULL); } /* Return if child type is mutual exclusive with other fork children */ @@ -5567,6 +5570,9 @@ int redisFork(int purpose) { } updateDictResizePolicy(); + moduleFireServerEvent(REDISMODULE_EVENT_FORK_CHILD, + REDISMODULE_SUBEVENT_FORK_CHILD_BORN, + NULL); } return childpid; }