Skip to content

Commit

Permalink
Implement module update function
Browse files Browse the repository at this point in the history
Signed-off-by: paas <paas@localhost.localdomain>
  • Loading branch information
hwware authored and paas committed Sep 17, 2024
1 parent 9f8185f commit c3341b1
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -7397,6 +7397,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
Expand Down Expand Up @@ -7425,6 +7448,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}
};
Expand Down
32 changes: 32 additions & 0 deletions src/commands/module-set-argument.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
}
35 changes: 35 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -13031,11 +13031,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 <path> [args...]
* MODULE LOADEX <path> [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...]
* MODULE SET-ARGUMENT <name> [args...]
* MODULE UNLOAD <name>
*/
void moduleCommand(client *c) {
Expand All @@ -13049,6 +13060,8 @@ void moduleCommand(client *c) {
" Load a module library from <path>, passing to it any optional arguments.",
"LOADEX <path> [[CONFIG NAME VALUE] [CONFIG NAME VALUE]] [ARGS ...]",
" Load a module library from <path>, while passing it module configurations and optional arguments.",
"SET-ARGUMENT <name> [<arg> ...]",
" Set module arguments to new values during runtime.",
"UNLOAD <name>",
" Unload a module.",
NULL};
Expand Down Expand Up @@ -13095,6 +13108,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;
Expand Down
1 change: 1 addition & 0 deletions tests/modules/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ TEST_MODULES = \
eventloop.so \
moduleconfigs.so \
moduleconfigstwo.so \
moduleparameter.so \
publish.so \
usercall.so \
postnotifications.so \
Expand Down
24 changes: 24 additions & 0 deletions tests/modules/moduleparameter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "valkeymodule.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

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;
}
18 changes: 18 additions & 0 deletions tests/unit/moduleapi/moduleconfigs.tcl
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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}]
}

}



0 comments on commit c3341b1

Please sign in to comment.