From dfe9714c867ba69f9ace30c9a9270ae95a111556 Mon Sep 17 00:00:00 2001 From: Wen Hui Date: Sun, 20 Sep 2020 06:36:20 -0400 Subject: [PATCH] Add Swapdb Module Event (#7804) --- src/db.c | 2 ++ src/module.c | 16 ++++++++++++++++ src/redismodule.h | 16 ++++++++++++++++ tests/modules/hooks.c | 12 ++++++++++++ tests/unit/moduleapi/hooks.tcl | 6 ++++++ 5 files changed, 52 insertions(+) diff --git a/src/db.c b/src/db.c index 2405cc2ab..668a1193a 100644 --- a/src/db.c +++ b/src/db.c @@ -1157,6 +1157,8 @@ void swapdbCommand(client *c) { addReplyError(c,"DB index is out of range"); return; } else { + RedisModuleSwapDbInfo si = {REDISMODULE_SWAPDBINFO_VERSION,id1,id2}; + moduleFireServerEvent(REDISMODULE_EVENT_SWAPDB,0,&si); server.dirty++; addReply(c,shared.ok); } diff --git a/src/module.c b/src/module.c index a501a4830..c6ea0cbbb 100644 --- a/src/module.c +++ b/src/module.c @@ -7174,6 +7174,20 @@ void ModuleForkDoneHandler(int exitcode, int bysignal) { * int32_t progress; // Approximate progress between 0 and 1024, * or -1 if unknown. * + * RedisModuleEvent_SwapDB + * + * This event is called when a swap db command has been successfully + * Executed. + * For this event call currently there is no subevents available. + * + * The data pointer can be casted to a RedisModuleSwapDbInfo + * structure with the following fields: + * + * int32_t dbnum_first; // Swap Db first dbnum + * int32_t dbnum_second; // Swap Db second dbnum + * + * + * * The function returns REDISMODULE_OK if the module was successfully subscribed * for the specified event. If the API is called from a wrong context then * REDISMODULE_ERR is returned. */ @@ -7278,6 +7292,8 @@ void moduleFireServerEvent(uint64_t eid, int subid, void *data) { moduledata = data; } else if (eid == REDISMODULE_EVENT_CRON_LOOP) { moduledata = data; + } else if (eid == REDISMODULE_EVENT_SWAPDB) { + moduledata = data; } ModulesInHooks++; diff --git a/src/redismodule.h b/src/redismodule.h index 4a0e5bf15..56011fae0 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -190,6 +190,7 @@ typedef uint64_t RedisModuleTimerID; #define REDISMODULE_EVENT_CRON_LOOP 8 #define REDISMODULE_EVENT_MODULE_CHANGE 9 #define REDISMODULE_EVENT_LOADING_PROGRESS 10 +#define REDISMODULE_EVENT_SWAPDB 11 typedef struct RedisModuleEvent { uint64_t id; /* REDISMODULE_EVENT_... defines. */ @@ -243,6 +244,10 @@ static const RedisModuleEvent RedisModuleEvent_LoadingProgress = { REDISMODULE_EVENT_LOADING_PROGRESS, 1 + }, + RedisModuleEvent_SwapDB = { + REDISMODULE_EVENT_SWAPDB, + 1 }; /* Those are values that are used for the 'subevent' callback argument. */ @@ -374,6 +379,17 @@ typedef struct RedisModuleLoadingProgressInfo { #define RedisModuleLoadingProgress RedisModuleLoadingProgressV1 +#define REDISMODULE_SWAPDBINFO_VERSION 1 +typedef struct RedisModuleSwapDbInfo { + uint64_t version; /* Not used since this structure is never passed + from the module to the core right now. Here + for future compatibility. */ + int32_t dbnum_first; /* Swap Db first dbnum */ + int32_t dbnum_second; /* Swap Db second dbnum */ +} RedisModuleSwapDbInfoV1; + +#define RedisModuleSwapDbInfo RedisModuleSwapDbInfoV1 + /* ------------------------- End of common defines ------------------------ */ #ifndef REDISMODULE_CORE diff --git a/tests/modules/hooks.c b/tests/modules/hooks.c index 665a20481..54f84aa23 100644 --- a/tests/modules/hooks.c +++ b/tests/modules/hooks.c @@ -253,6 +253,16 @@ void moduleChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, LogStringEvent(ctx, keyname, ei->module_name); } +void swapDbCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data) +{ + REDISMODULE_NOT_USED(e); + REDISMODULE_NOT_USED(sub); + + RedisModuleSwapDbInfo *ei = data; + LogNumericEvent(ctx, "swapdb-first", ei->dbnum_first); + LogNumericEvent(ctx, "swapdb-second", ei->dbnum_second); +} + /* This function must be present on each Redis module. It is used in order to * register the commands into the Redis server. */ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { @@ -289,6 +299,8 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) RedisModuleEvent_CronLoop, cronLoopCallback); RedisModule_SubscribeToServerEvent(ctx, RedisModuleEvent_ModuleChange, moduleChangeCallback); + RedisModule_SubscribeToServerEvent(ctx, + RedisModuleEvent_SwapDB, swapDbCallback); event_log = RedisModule_CreateDict(ctx); diff --git a/tests/unit/moduleapi/hooks.tcl b/tests/unit/moduleapi/hooks.tcl index da0307ce6..c4af59bd2 100644 --- a/tests/unit/moduleapi/hooks.tcl +++ b/tests/unit/moduleapi/hooks.tcl @@ -147,6 +147,12 @@ tags "modules" { set replica_stdout [srv 0 stdout] } + test {Test swapdb hooks} { + r swapdb 0 10 + assert_equal [r hooks.event_last swapdb-first] 0 + assert_equal [r hooks.event_last swapdb-second] 10 + + } # look into the log file of the server that just exited test {Test shutdown hook} {