Skip to content

Commit

Permalink
Update codes to API UpdateRunTimeArgs
Browse files Browse the repository at this point in the history
  • Loading branch information
hwware committed Oct 31, 2024
1 parent 6b4efbc commit 0caa3bd
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 125 deletions.
24 changes: 0 additions & 24 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -7404,29 +7404,6 @@ 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 @@ -7455,7 +7432,6 @@ 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: 0 additions & 32 deletions src/commands/module-set-argument.json

This file was deleted.

52 changes: 6 additions & 46 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -13046,22 +13041,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 <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 @@ -13075,8 +13059,6 @@ 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 @@ -13123,28 +13105,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;
Expand Down Expand Up @@ -13609,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);
Expand Down
3 changes: 1 addition & 2 deletions src/redismodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@
#define RedisModuleCommandInfo ValkeyModuleCommandInfo
#define RedisModuleCommandKeySpec ValkeyModuleCommandKeySpec
#define RedisModuleCommandHistoryEntry ValkeyModuleCommandHistoryEntry
#define RedisModuleRunTimeArgs ValkeyModuleRunTimeArgs

/* RedisModule APIs */
#define RedisModule_OnLoad ValkeyModule_OnLoad
Expand All @@ -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
Expand Down
9 changes: 2 additions & 7 deletions src/valkeymodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 1 addition & 5 deletions tests/modules/moduleparameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
14 changes: 5 additions & 9 deletions tests/unit/moduleapi/moduleconfigs.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,16 @@ 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
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 "" [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}]
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}]
}

}


Expand Down

0 comments on commit 0caa3bd

Please sign in to comment.