From f278c6ef5dc2f0a981a8b5381c5e498fe4d666be Mon Sep 17 00:00:00 2001 From: hwware Date: Tue, 17 Sep 2024 17:23:43 +0000 Subject: [PATCH 1/6] Implement module update function Signed-off-by: hwware --- src/commands.def | 24 ++++++++++++++++++ src/commands/module-set-argument.json | 32 +++++++++++++++++++++++ src/module.c | 35 ++++++++++++++++++++++++++ tests/modules/Makefile | 1 + tests/modules/moduleparameter.c | 24 ++++++++++++++++++ tests/unit/moduleapi/moduleconfigs.tcl | 18 +++++++++++++ 6 files changed, 134 insertions(+) create mode 100644 src/commands/module-set-argument.json create mode 100644 tests/modules/moduleparameter.c diff --git a/src/commands.def b/src/commands.def index 791b30d540..8fcd3ea90b 100644 --- a/src/commands.def +++ b/src/commands.def @@ -7404,6 +7404,29 @@ struct COMMAND_ARG MODULE_LOADEX_Args[] = { {MAKE_ARG("args",ARG_TYPE_STRING,-1,"ARGS",NULL,NULL,CMD_ARG_OPTIONAL|CMD_ARG_MULTIPLE,0,NULL)}, }; +/********** MODULE SET_ARGUMENT ********************/ + +#ifndef SKIP_CMD_HISTORY_TABLE +/* MODULE SET_ARGUMENT history */ +#define MODULE_SET_ARGUMENT_History NULL +#endif + +#ifndef SKIP_CMD_TIPS_TABLE +/* MODULE SET_ARGUMENT tips */ +#define MODULE_SET_ARGUMENT_Tips NULL +#endif + +#ifndef SKIP_CMD_KEY_SPECS_TABLE +/* MODULE SET_ARGUMENT key specs */ +#define MODULE_SET_ARGUMENT_Keyspecs NULL +#endif + +/* MODULE SET_ARGUMENT argument table */ +struct COMMAND_ARG MODULE_SET_ARGUMENT_Args[] = { +{MAKE_ARG("name",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_NONE,0,NULL)}, +{MAKE_ARG("arg",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_OPTIONAL|CMD_ARG_MULTIPLE,0,NULL)}, +}; + /********** MODULE UNLOAD ********************/ #ifndef SKIP_CMD_HISTORY_TABLE @@ -7432,6 +7455,7 @@ struct COMMAND_STRUCT MODULE_Subcommands[] = { {MAKE_CMD("list","Returns all loaded modules.","O(N) where N is the number of loaded modules.","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LIST_History,0,MODULE_LIST_Tips,1,moduleCommand,2,CMD_ADMIN|CMD_NOSCRIPT,0,MODULE_LIST_Keyspecs,0,NULL,0)}, {MAKE_CMD("load","Loads a module.","O(1)","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LOAD_History,0,MODULE_LOAD_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_LOAD_Keyspecs,0,NULL,2),.args=MODULE_LOAD_Args}, {MAKE_CMD("loadex","Loads a module using extended parameters.","O(1)","7.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_LOADEX_History,0,MODULE_LOADEX_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_LOADEX_Keyspecs,0,NULL,3),.args=MODULE_LOADEX_Args}, +{MAKE_CMD("set-argument","Sets module arguments to new values during runtime.","O(1)","8.2.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_SET_ARGUMENT_History,0,MODULE_SET_ARGUMENT_Tips,0,moduleCommand,-3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_SET_ARGUMENT_Keyspecs,0,NULL,2),.args=MODULE_SET_ARGUMENT_Args}, {MAKE_CMD("unload","Unloads a module.","O(1)","4.0.0",CMD_DOC_NONE,NULL,NULL,"server",COMMAND_GROUP_SERVER,MODULE_UNLOAD_History,0,MODULE_UNLOAD_Tips,0,moduleCommand,3,CMD_NO_ASYNC_LOADING|CMD_ADMIN|CMD_NOSCRIPT|CMD_PROTECTED,0,MODULE_UNLOAD_Keyspecs,0,NULL,1),.args=MODULE_UNLOAD_Args}, {0} }; diff --git a/src/commands/module-set-argument.json b/src/commands/module-set-argument.json new file mode 100644 index 0000000000..b60484e878 --- /dev/null +++ b/src/commands/module-set-argument.json @@ -0,0 +1,32 @@ +{ + "SET-ARGUMENT": { + "summary": "Sets module arguments to new values during runtime.", + "complexity": "O(1)", + "group": "server", + "since": "8.2.0", + "arity": -3, + "container": "MODULE", + "function": "moduleCommand", + "command_flags": [ + "NO_ASYNC_LOADING", + "ADMIN", + "NOSCRIPT", + "PROTECTED" + ], + "reply_schema": { + "const": "OK" + }, + "arguments": [ + { + "name": "name", + "type": "string" + }, + { + "name": "arg", + "type": "string", + "optional": true, + "multiple": true + } + ] + } +} diff --git a/src/module.c b/src/module.c index 2884239200..bf26c68bcb 100644 --- a/src/module.c +++ b/src/module.c @@ -13032,11 +13032,22 @@ int VM_RdbSave(ValkeyModuleCtx *ctx, ValkeyModuleRdbStream *stream, int flags) { return VALKEYMODULE_OK; } + +void updateModuleRunTimeArgument(struct ValkeyModule *module, void **argv, int argc) { + module->loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; + module->loadmod->argc = argc; + for (int i = 0; i < argc; i++) { + module->loadmod->argv[i] = argv[i]; + incrRefCount(module->loadmod->argv[i]); + } +} + /* MODULE command. * * MODULE LIST * MODULE LOAD [args...] * MODULE LOADEX [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...] + * MODULE SET-ARGUMENT [args...] * MODULE UNLOAD */ void moduleCommand(client *c) { @@ -13050,6 +13061,8 @@ void moduleCommand(client *c) { " Load a module library from , passing to it any optional arguments.", "LOADEX [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...]", " Load a module library from , while passing it module configurations and optional arguments.", + "SET-ARGUMENT [ ...]", + " Set module arguments to new values during runtime.", "UNLOAD ", " Unload a module.", NULL}; @@ -13096,6 +13109,28 @@ void moduleCommand(client *c) { } } else if (!strcasecmp(subcmd, "list") && c->argc == 2) { addReplyLoadedModules(c); + } else if (!strcasecmp(subcmd, "set-argument") && c->argc >= 3) { + struct ValkeyModule *module = dictFetchValue(modules, c->argv[2]->ptr); + if (module != NULL) { + for (int i = 0; i < module->loadmod->argc; i++) { + decrRefCount(module->loadmod->argv[i]); + } + zfree(module->loadmod->argv); + robj **argv = NULL; + int argc = 0; + + if (c->argc > 3) { + argc = c->argc - 3; + argv = &c->argv[3]; + } + updateModuleRunTimeArgument(module, (void **)argv, argc); + + addReply(c, shared.ok); + } else { + addReplyError(c, "Error set arguments for module: no such module with that name "); + serverLog(LL_WARNING, "Error set arguments for module %s: no such module with that name", + (sds)c->argv[2]->ptr); + } } else { addReplySubcommandSyntaxError(c); return; diff --git a/tests/modules/Makefile b/tests/modules/Makefile index 1690b9b627..82813bb6f7 100644 --- a/tests/modules/Makefile +++ b/tests/modules/Makefile @@ -58,6 +58,7 @@ TEST_MODULES = \ eventloop.so \ moduleconfigs.so \ moduleconfigstwo.so \ + moduleparameter.so \ publish.so \ usercall.so \ postnotifications.so \ diff --git a/tests/modules/moduleparameter.c b/tests/modules/moduleparameter.c new file mode 100644 index 0000000000..30678f037a --- /dev/null +++ b/tests/modules/moduleparameter.c @@ -0,0 +1,24 @@ +#include "valkeymodule.h" +#include +#include +#include +#include + +int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { + return ValkeyModule_ReplyWithSimpleString(ctx, "This is update module parameter test module"); +} + +int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { + VALKEYMODULE_NOT_USED(argv); + VALKEYMODULE_NOT_USED(argc); + + if (ValkeyModule_Init(ctx,"myhello",1,VALKEYMODULE_APIVER_1) + == VALKEYMODULE_ERR) return VALKEYMODULE_ERR; + + + if (ValkeyModule_CreateCommand(ctx,"hello.hi", + GET_HELLO,"fast",0,0,0) == VALKEYMODULE_ERR) + return VALKEYMODULE_ERR; + + return VALKEYMODULE_OK; +} diff --git a/tests/unit/moduleapi/moduleconfigs.tcl b/tests/unit/moduleapi/moduleconfigs.tcl index 44f994d2d0..0ea5d94c1f 100644 --- a/tests/unit/moduleapi/moduleconfigs.tcl +++ b/tests/unit/moduleapi/moduleconfigs.tcl @@ -1,5 +1,7 @@ set testmodule [file normalize tests/modules/moduleconfigs.so] set testmoduletwo [file normalize tests/modules/moduleconfigstwo.so] +set testmoduleparameter [file normalize tests/modules/moduleparameter.so] + start_server {tags {"modules"}} { r module load $testmodule @@ -243,5 +245,21 @@ start_server {tags {"modules"}} { assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1024" } } + + test {Module Set-Argument command work} { + r module load $testmoduleparameter + + set t [r module list] + set modulename [lmap x [r module list] {dict get $x name}] + assert_not_equal [lsearch $modulename myhello] -1 + string match "" [lmap x [r module list] {dict get $x args}] + r module set-argument myhello 1 2 3 + r config rewrite + restart_server 0 true false + string match "1 2 3" [lmap x [r module list] {dict get $x args}] + } + } + + From 605086d444be9c8a0738804fc1e6b2dd91ea08a9 Mon Sep 17 00:00:00 2001 From: hwware Date: Tue, 17 Sep 2024 18:08:22 +0000 Subject: [PATCH 2/6] adjust format and remove test warning Signed-off-by: hwware --- src/module.c | 6 +++--- tests/modules/moduleparameter.c | 3 +++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/module.c b/src/module.c index bf26c68bcb..5b9b0dcb0c 100644 --- a/src/module.c +++ b/src/module.c @@ -13061,7 +13061,7 @@ void moduleCommand(client *c) { " Load a module library from , passing to it any optional arguments.", "LOADEX [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...]", " Load a module library from , while passing it module configurations and optional arguments.", - "SET-ARGUMENT [ ...]", + "SET-ARGUMENT [ ...]", " Set module arguments to new values during runtime.", "UNLOAD ", " Unload a module.", @@ -13112,11 +13112,11 @@ void moduleCommand(client *c) { } else if (!strcasecmp(subcmd, "set-argument") && c->argc >= 3) { struct ValkeyModule *module = dictFetchValue(modules, c->argv[2]->ptr); if (module != NULL) { - for (int i = 0; i < module->loadmod->argc; i++) { + for (int i = 0; i < module->loadmod->argc; i++) { decrRefCount(module->loadmod->argv[i]); } zfree(module->loadmod->argv); - robj **argv = NULL; + robj **argv = NULL; int argc = 0; if (c->argc > 3) { diff --git a/tests/modules/moduleparameter.c b/tests/modules/moduleparameter.c index 30678f037a..62d7c0cafc 100644 --- a/tests/modules/moduleparameter.c +++ b/tests/modules/moduleparameter.c @@ -5,6 +5,9 @@ #include int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { + VALKEYMODULE_NOT_USED(argv); + VALKEYMODULE_NOT_USED(argc); + return ValkeyModule_ReplyWithSimpleString(ctx, "This is update module parameter test module"); } From d24590029e97ef87c711720dbd2393792f28063a Mon Sep 17 00:00:00 2001 From: hwware Date: Mon, 14 Oct 2024 13:18:50 +0000 Subject: [PATCH 3/6] Add API for accessing Runtime args Signed-off-by: hwware --- src/module.c | 15 +++++++++++++++ src/redismodule.h | 2 ++ src/valkeymodule.h | 7 +++++++ tests/modules/moduleparameter.c | 7 ++++++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/module.c b/src/module.c index 5b9b0dcb0c..864c584165 100644 --- a/src/module.c +++ b/src/module.c @@ -3042,6 +3042,20 @@ client *moduleGetReplyClient(ValkeyModuleCtx *ctx) { } } +ValkeyModuleRunTimeArgs *VM_GetRunTimeArgs(ValkeyModuleCtx *ctx) { + client *c = moduleGetReplyClient(ctx); + if (c == NULL) return NULL; + + ValkeyModuleRunTimeArgs *args = zmalloc(sizeof(struct ValkeyModuleRunTimeArgs)); + int argc = ctx->module->loadmod->argc; + args->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; + args->argc = argc; + for (int i = 0; i < argc; i++) { + args->argv[i] = (char *)ctx->module->loadmod->argv[i]->ptr; + } + return args; +} + /* Send an integer reply to the client, with the specified `long long` value. * The function always returns VALKEYMODULE_OK. */ int VM_ReplyWithLongLong(ValkeyModuleCtx *ctx, long long ll) { @@ -13595,6 +13609,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(SetModuleAttribs); REGISTER_API(IsModuleNameBusy); REGISTER_API(WrongArity); + REGISTER_API(GetRunTimeArgs); REGISTER_API(ReplyWithLongLong); REGISTER_API(ReplyWithError); REGISTER_API(ReplyWithErrorFormat); diff --git a/src/redismodule.h b/src/redismodule.h index d692104d52..77b927ea46 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -334,6 +334,7 @@ #define RedisModuleCommandInfo ValkeyModuleCommandInfo #define RedisModuleCommandKeySpec ValkeyModuleCommandKeySpec #define RedisModuleCommandHistoryEntry ValkeyModuleCommandHistoryEntry +#define RedisModuleRunTimeArgs ValkeyModuleRunTimeArgs /* RedisModule APIs */ #define RedisModule_OnLoad ValkeyModule_OnLoad @@ -357,6 +358,7 @@ #define RedisModule_SetModuleAttribs ValkeyModule_SetModuleAttribs #define RedisModule_IsModuleNameBusy ValkeyModule_IsModuleNameBusy #define RedisModule_WrongArity ValkeyModule_WrongArity +#define RedisModule_GetRunTimeArgs ValkeyModule_GetRunTimeArgs #define RedisModule_ReplyWithLongLong ValkeyModule_ReplyWithLongLong #define RedisModule_GetSelectedDb ValkeyModule_GetSelectedDb #define RedisModule_SelectDb ValkeyModule_SelectDb diff --git a/src/valkeymodule.h b/src/valkeymodule.h index c2cdb2f0e7..b76d99f10b 100644 --- a/src/valkeymodule.h +++ b/src/valkeymodule.h @@ -147,6 +147,11 @@ typedef long long ustime_t; #define VALKEYMODULE_CONFIG_MEMORY (1ULL << 7) /* Indicates if this value can be set as a memory value */ #define VALKEYMODULE_CONFIG_BITFLAGS (1ULL << 8) /* Indicates if this value can be set as a multiple enum values */ +typedef struct ValkeyModuleRunTimeArgs { + int argc; + char **argv; +} ValkeyModuleRunTimeArgs; + /* StreamID type. */ typedef struct ValkeyModuleStreamID { uint64_t ms; @@ -967,6 +972,7 @@ VALKEYMODULE_API void (*ValkeyModule_SetModuleAttribs)(ValkeyModuleCtx *ctx, con VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_IsModuleNameBusy)(const char *name) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_WrongArity)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; +VALKEYMODULE_API struct ValkeyModuleRunTimeArgs *(*ValkeyModule_GetRunTimeArgs)(ValkeyModuleCtx *ctx)VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_ReplyWithLongLong)(ValkeyModuleCtx *ctx, long long ll) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_GetSelectedDb)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_SelectDb)(ValkeyModuleCtx *ctx, int newid) VALKEYMODULE_ATTR; @@ -1673,6 +1679,7 @@ static int ValkeyModule_Init(ValkeyModuleCtx *ctx, const char *name, int ver, in VALKEYMODULE_GET_API(SetModuleAttribs); VALKEYMODULE_GET_API(IsModuleNameBusy); VALKEYMODULE_GET_API(WrongArity); + VALKEYMODULE_GET_API(GetRunTimeArgs); VALKEYMODULE_GET_API(ReplyWithLongLong); VALKEYMODULE_GET_API(ReplyWithError); VALKEYMODULE_GET_API(ReplyWithErrorFormat); diff --git a/tests/modules/moduleparameter.c b/tests/modules/moduleparameter.c index 62d7c0cafc..0bd5ce3556 100644 --- a/tests/modules/moduleparameter.c +++ b/tests/modules/moduleparameter.c @@ -8,7 +8,12 @@ int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { VALKEYMODULE_NOT_USED(argv); VALKEYMODULE_NOT_USED(argc); - return ValkeyModule_ReplyWithSimpleString(ctx, "This is update module parameter test module"); + ValkeyModuleRunTimeArgs *ret = ValkeyModule_GetRunTimeArgs(ctx); + ValkeyModule_Log(ctx, "warning", "dbsize command arg number is %d", + ret->argc); + ValkeyModule_Log(ctx, "warning", "dbsize command arg 0 is %s", + ret->argv[0]); + return ValkeyModule_ReplyWithSimpleString(ctx, "Module runtime args test"); } int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { From 18b532c8fb5ce7a41a70b5073572d894e7548915 Mon Sep 17 00:00:00 2001 From: hwware Date: Wed, 16 Oct 2024 07:38:45 +0000 Subject: [PATCH 4/6] remove new command and relative functions --- src/commands/module-set-argument.json | 32 ----------------------- src/module.c | 35 -------------------------- tests/unit/moduleapi/moduleconfigs.tcl | 13 ---------- 3 files changed, 80 deletions(-) delete mode 100644 src/commands/module-set-argument.json diff --git a/src/commands/module-set-argument.json b/src/commands/module-set-argument.json deleted file mode 100644 index b60484e878..0000000000 --- a/src/commands/module-set-argument.json +++ /dev/null @@ -1,32 +0,0 @@ -{ - "SET-ARGUMENT": { - "summary": "Sets module arguments to new values during runtime.", - "complexity": "O(1)", - "group": "server", - "since": "8.2.0", - "arity": -3, - "container": "MODULE", - "function": "moduleCommand", - "command_flags": [ - "NO_ASYNC_LOADING", - "ADMIN", - "NOSCRIPT", - "PROTECTED" - ], - "reply_schema": { - "const": "OK" - }, - "arguments": [ - { - "name": "name", - "type": "string" - }, - { - "name": "arg", - "type": "string", - "optional": true, - "multiple": true - } - ] - } -} diff --git a/src/module.c b/src/module.c index 864c584165..f4a712d672 100644 --- a/src/module.c +++ b/src/module.c @@ -13046,22 +13046,11 @@ int VM_RdbSave(ValkeyModuleCtx *ctx, ValkeyModuleRdbStream *stream, int flags) { return VALKEYMODULE_OK; } - -void updateModuleRunTimeArgument(struct ValkeyModule *module, void **argv, int argc) { - module->loadmod->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; - module->loadmod->argc = argc; - for (int i = 0; i < argc; i++) { - module->loadmod->argv[i] = argv[i]; - incrRefCount(module->loadmod->argv[i]); - } -} - /* MODULE command. * * MODULE LIST * MODULE LOAD [args...] * MODULE LOADEX [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...] - * MODULE SET-ARGUMENT [args...] * MODULE UNLOAD */ void moduleCommand(client *c) { @@ -13075,8 +13064,6 @@ void moduleCommand(client *c) { " Load a module library from , passing to it any optional arguments.", "LOADEX [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...]", " Load a module library from , while passing it module configurations and optional arguments.", - "SET-ARGUMENT [ ...]", - " Set module arguments to new values during runtime.", "UNLOAD ", " Unload a module.", NULL}; @@ -13123,28 +13110,6 @@ void moduleCommand(client *c) { } } else if (!strcasecmp(subcmd, "list") && c->argc == 2) { addReplyLoadedModules(c); - } else if (!strcasecmp(subcmd, "set-argument") && c->argc >= 3) { - struct ValkeyModule *module = dictFetchValue(modules, c->argv[2]->ptr); - if (module != NULL) { - for (int i = 0; i < module->loadmod->argc; i++) { - decrRefCount(module->loadmod->argv[i]); - } - zfree(module->loadmod->argv); - robj **argv = NULL; - int argc = 0; - - if (c->argc > 3) { - argc = c->argc - 3; - argv = &c->argv[3]; - } - updateModuleRunTimeArgument(module, (void **)argv, argc); - - addReply(c, shared.ok); - } else { - addReplyError(c, "Error set arguments for module: no such module with that name "); - serverLog(LL_WARNING, "Error set arguments for module %s: no such module with that name", - (sds)c->argv[2]->ptr); - } } else { addReplySubcommandSyntaxError(c); return; diff --git a/tests/unit/moduleapi/moduleconfigs.tcl b/tests/unit/moduleapi/moduleconfigs.tcl index 0ea5d94c1f..6098f91c5f 100644 --- a/tests/unit/moduleapi/moduleconfigs.tcl +++ b/tests/unit/moduleapi/moduleconfigs.tcl @@ -246,19 +246,6 @@ start_server {tags {"modules"}} { } } - test {Module Set-Argument command work} { - r module load $testmoduleparameter - - set t [r module list] - set modulename [lmap x [r module list] {dict get $x name}] - assert_not_equal [lsearch $modulename myhello] -1 - string match "" [lmap x [r module list] {dict get $x args}] - r module set-argument myhello 1 2 3 - r config rewrite - restart_server 0 true false - string match "1 2 3" [lmap x [r module list] {dict get $x args}] - } - } From cbfd1d58f59338addbe06f85a96994f18adb2cb2 Mon Sep 17 00:00:00 2001 From: hwware Date: Thu, 17 Oct 2024 01:56:45 +0000 Subject: [PATCH 5/6] Add API UpdateRunTimeArgs --- src/module.c | 17 ++++++----------- src/redismodule.h | 3 +-- src/valkeymodule.h | 9 ++------- 3 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/module.c b/src/module.c index f4a712d672..d9af6f9d5d 100644 --- a/src/module.c +++ b/src/module.c @@ -3042,18 +3042,13 @@ client *moduleGetReplyClient(ValkeyModuleCtx *ctx) { } } -ValkeyModuleRunTimeArgs *VM_GetRunTimeArgs(ValkeyModuleCtx *ctx) { +int VM_UpdateRunTimeArgs(ValkeyModuleCtx *ctx, int index, char *value) { client *c = moduleGetReplyClient(ctx); - if (c == NULL) return NULL; + if (c == NULL) return VALKEYMODULE_OK; - ValkeyModuleRunTimeArgs *args = zmalloc(sizeof(struct ValkeyModuleRunTimeArgs)); - int argc = ctx->module->loadmod->argc; - args->argv = argc ? zmalloc(sizeof(robj *) * argc) : NULL; - args->argc = argc; - for (int i = 0; i < argc; i++) { - args->argv[i] = (char *)ctx->module->loadmod->argv[i]->ptr; - } - return args; + ValkeyModuleString *o = createStringObject(value, strlen(value)); + ctx->module->loadmod->argv[index] = o; + return VALKEYMODULE_OK; } /* Send an integer reply to the client, with the specified `long long` value. @@ -13574,7 +13569,7 @@ void moduleRegisterCoreAPI(void) { REGISTER_API(SetModuleAttribs); REGISTER_API(IsModuleNameBusy); REGISTER_API(WrongArity); - REGISTER_API(GetRunTimeArgs); + REGISTER_API(UpdateRunTimeArgs); REGISTER_API(ReplyWithLongLong); REGISTER_API(ReplyWithError); REGISTER_API(ReplyWithErrorFormat); diff --git a/src/redismodule.h b/src/redismodule.h index 77b927ea46..43a295aa9a 100644 --- a/src/redismodule.h +++ b/src/redismodule.h @@ -334,7 +334,6 @@ #define RedisModuleCommandInfo ValkeyModuleCommandInfo #define RedisModuleCommandKeySpec ValkeyModuleCommandKeySpec #define RedisModuleCommandHistoryEntry ValkeyModuleCommandHistoryEntry -#define RedisModuleRunTimeArgs ValkeyModuleRunTimeArgs /* RedisModule APIs */ #define RedisModule_OnLoad ValkeyModule_OnLoad @@ -358,7 +357,7 @@ #define RedisModule_SetModuleAttribs ValkeyModule_SetModuleAttribs #define RedisModule_IsModuleNameBusy ValkeyModule_IsModuleNameBusy #define RedisModule_WrongArity ValkeyModule_WrongArity -#define RedisModule_GetRunTimeArgs ValkeyModule_GetRunTimeArgs +#define RedisModule_UpdateRunTimeArgs ValkeyModule_UpdateRunTimeArgs #define RedisModule_ReplyWithLongLong ValkeyModule_ReplyWithLongLong #define RedisModule_GetSelectedDb ValkeyModule_GetSelectedDb #define RedisModule_SelectDb ValkeyModule_SelectDb diff --git a/src/valkeymodule.h b/src/valkeymodule.h index b76d99f10b..1ccc59ddef 100644 --- a/src/valkeymodule.h +++ b/src/valkeymodule.h @@ -147,11 +147,6 @@ typedef long long ustime_t; #define VALKEYMODULE_CONFIG_MEMORY (1ULL << 7) /* Indicates if this value can be set as a memory value */ #define VALKEYMODULE_CONFIG_BITFLAGS (1ULL << 8) /* Indicates if this value can be set as a multiple enum values */ -typedef struct ValkeyModuleRunTimeArgs { - int argc; - char **argv; -} ValkeyModuleRunTimeArgs; - /* StreamID type. */ typedef struct ValkeyModuleStreamID { uint64_t ms; @@ -972,7 +967,7 @@ VALKEYMODULE_API void (*ValkeyModule_SetModuleAttribs)(ValkeyModuleCtx *ctx, con VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_IsModuleNameBusy)(const char *name) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_WrongArity)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; -VALKEYMODULE_API struct ValkeyModuleRunTimeArgs *(*ValkeyModule_GetRunTimeArgs)(ValkeyModuleCtx *ctx)VALKEYMODULE_ATTR; +VALKEYMODULE_API int (*ValkeyModule_UpdateRunTimeArgs)(ValkeyModuleCtx *ctx, int index, char *value)VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_ReplyWithLongLong)(ValkeyModuleCtx *ctx, long long ll) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_GetSelectedDb)(ValkeyModuleCtx *ctx) VALKEYMODULE_ATTR; VALKEYMODULE_API int (*ValkeyModule_SelectDb)(ValkeyModuleCtx *ctx, int newid) VALKEYMODULE_ATTR; @@ -1679,7 +1674,7 @@ static int ValkeyModule_Init(ValkeyModuleCtx *ctx, const char *name, int ver, in VALKEYMODULE_GET_API(SetModuleAttribs); VALKEYMODULE_GET_API(IsModuleNameBusy); VALKEYMODULE_GET_API(WrongArity); - VALKEYMODULE_GET_API(GetRunTimeArgs); + VALKEYMODULE_GET_API(UpdateRunTimeArgs); VALKEYMODULE_GET_API(ReplyWithLongLong); VALKEYMODULE_GET_API(ReplyWithError); VALKEYMODULE_GET_API(ReplyWithErrorFormat); From 1d412726104daf751be11c78ec0f5d4f8bee7e26 Mon Sep 17 00:00:00 2001 From: hwware Date: Thu, 17 Oct 2024 02:29:15 +0000 Subject: [PATCH 6/6] Update test case --- tests/modules/moduleparameter.c | 6 +----- tests/unit/moduleapi/moduleconfigs.tcl | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/modules/moduleparameter.c b/tests/modules/moduleparameter.c index 0bd5ce3556..a60753c659 100644 --- a/tests/modules/moduleparameter.c +++ b/tests/modules/moduleparameter.c @@ -8,11 +8,7 @@ int GET_HELLO(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) { VALKEYMODULE_NOT_USED(argv); VALKEYMODULE_NOT_USED(argc); - ValkeyModuleRunTimeArgs *ret = ValkeyModule_GetRunTimeArgs(ctx); - ValkeyModule_Log(ctx, "warning", "dbsize command arg number is %d", - ret->argc); - ValkeyModule_Log(ctx, "warning", "dbsize command arg 0 is %s", - ret->argv[0]); + ValkeyModule_UpdateRunTimeArgs(ctx, 0, "99"); return ValkeyModule_ReplyWithSimpleString(ctx, "Module runtime args test"); } diff --git a/tests/unit/moduleapi/moduleconfigs.tcl b/tests/unit/moduleapi/moduleconfigs.tcl index 6098f91c5f..7ffe2c0677 100644 --- a/tests/unit/moduleapi/moduleconfigs.tcl +++ b/tests/unit/moduleapi/moduleconfigs.tcl @@ -246,6 +246,17 @@ start_server {tags {"modules"}} { } } + test {Module Update Args } { + r module load $testmoduleparameter 10 20 30 + + set t [r module list] + set modulename [lmap x [r module list] {dict get $x name}] + assert_not_equal [lsearch $modulename myhello] -1 + string match "10 20 30" [lmap x [r module list] {dict get $x args}] + r hello.hi + string match "99 20 30" [lmap x [r module list] {dict get $x args}] + } + }