mirror of https://github.com/redis/redis.git
parent
192799539f
commit
294492dbf2
|
@ -1695,8 +1695,8 @@ static int RedisModule_Init(RedisModuleCtx *ctx, const char *name, int ver, int
|
|||
REDISMODULE_GET_API(RegisterNumericConfig);
|
||||
REDISMODULE_GET_API(RegisterStringConfig);
|
||||
REDISMODULE_GET_API(RegisterEnumConfig);
|
||||
REDISMODULE_GET_API(LoadDefaultConfigs);
|
||||
REDISMODULE_GET_API(LoadConfigs);
|
||||
REDISMODULE_GET_API(LoadDefaultConfigs);
|
||||
REDISMODULE_GET_API(RdbStreamCreateFromFile);
|
||||
REDISMODULE_GET_API(RdbStreamFree);
|
||||
REDISMODULE_GET_API(RdbLoad);
|
||||
|
|
|
@ -169,16 +169,22 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
|||
REDISMODULE_NOT_USED(argv);
|
||||
REDISMODULE_NOT_USED(argc);
|
||||
|
||||
if (RedisModule_Init(ctx, "moduleconfigs", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) return REDISMODULE_ERR;
|
||||
if (RedisModule_Init(ctx, "moduleconfigs", 1, REDISMODULE_APIVER_1) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to init module");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
|
||||
if (RedisModule_RegisterBoolConfig(ctx, "mutable_bool", 1, REDISMODULE_CONFIG_DEFAULT, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &mutable_bool_val) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register mutable_bool");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
/* Immutable config here. */
|
||||
if (RedisModule_RegisterBoolConfig(ctx, "immutable_bool", 0, REDISMODULE_CONFIG_IMMUTABLE, getBoolConfigCommand, setBoolConfigCommand, boolApplyFunc, &immutable_bool_val) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register immutable_bool");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterStringConfig(ctx, "string", "secret password", REDISMODULE_CONFIG_DEFAULT, getStringConfigCommand, setStringConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register string");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
|
||||
|
@ -187,59 +193,82 @@ int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
|
|||
const int int_vals[] = {0, 5, 1, 2, 4};
|
||||
|
||||
if (RedisModule_RegisterEnumConfig(ctx, "enum", 1, REDISMODULE_CONFIG_DEFAULT, enum_vals, int_vals, 5, getEnumConfigCommand, setEnumConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register enum");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterEnumConfig(ctx, "flags", 3, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_BITFLAGS, enum_vals, int_vals, 5, getFlagsConfigCommand, setFlagsConfigCommand, NULL, NULL) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register flags");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
/* Memory config here. */
|
||||
if (RedisModule_RegisterNumericConfig(ctx, "memory_numeric", 1024, REDISMODULE_CONFIG_DEFAULT | REDISMODULE_CONFIG_MEMORY, 0, 3000000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &memval) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register memory_numeric");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterNumericConfig(ctx, "numeric", -1, REDISMODULE_CONFIG_DEFAULT, -5, 2000, getNumericConfigCommand, setNumericConfigCommand, longlongApplyFunc, &longval) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register numeric");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
|
||||
/*** unprefixed and aliased configuration ***/
|
||||
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-bool|unprefix-bool-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register unprefix-bool");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterBoolConfig(ctx, "unprefix-noalias-bool", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||
getBoolConfigCommand, setBoolConfigCommand, NULL, &no_prefix_bool2) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register unprefix-noalias-bool");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterNumericConfig(ctx, "unprefix.numeric|unprefix.numeric-alias", -1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||
-5, 2000, getNumericConfigCommand, setNumericConfigCommand, NULL, &no_prefix_longval) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register unprefix.numeric");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterStringConfig(ctx, "unprefix-string|unprefix.string-alias", "secret unprefix", REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||
getStringConfigUnprefix, setStringConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register unprefix-string");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
if (RedisModule_RegisterEnumConfig(ctx, "unprefix-enum|unprefix-enum-alias", 1, REDISMODULE_CONFIG_DEFAULT|REDISMODULE_CONFIG_UNPREFIXED,
|
||||
enum_vals, int_vals, 5, getEnumConfigUnprefix, setEnumConfigUnprefix, NULL, NULL) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register unprefix-enum");
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
|
||||
|
||||
RedisModule_Log(ctx, "debug", "Registered configuration");
|
||||
size_t len;
|
||||
if (argc && !strcasecmp(RedisModule_StringPtrLen(argv[0], &len), "noload")) {
|
||||
return REDISMODULE_OK;
|
||||
} else if (RedisModule_LoadDefaultConfigs(ctx) == REDISMODULE_ERR) {
|
||||
return REDISMODULE_ERR;
|
||||
} else if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
|
||||
if (strval) {
|
||||
RedisModule_FreeString(ctx, strval);
|
||||
strval = NULL;
|
||||
}
|
||||
return REDISMODULE_ERR;
|
||||
RedisModule_Log(ctx, "warning", "Failed to load default configuration");
|
||||
goto err;
|
||||
} else if (argc && !strcasecmp(RedisModule_StringPtrLen(argv[0], &len), "override")) {
|
||||
// simulate configuration values being overwritten by the command line
|
||||
RedisModule_Log(ctx, "debug", "Overriding configuration values");
|
||||
if (strval) RedisModule_FreeString(ctx, strval);
|
||||
strval = RedisModule_CreateString(ctx, "foo", 3);
|
||||
longval = memval = 123;
|
||||
}
|
||||
RedisModule_Log(ctx, "debug", "Loading configuration");
|
||||
if (RedisModule_LoadConfigs(ctx) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to load configuration");
|
||||
goto err;
|
||||
}
|
||||
/* Creates a command which registers configs outside OnLoad() function. */
|
||||
if (RedisModule_CreateCommand(ctx,"block.register.configs.outside.onload", registerBlockCheck, "write", 0, 0, 0) == REDISMODULE_ERR)
|
||||
return REDISMODULE_ERR;
|
||||
if (RedisModule_CreateCommand(ctx,"block.register.configs.outside.onload", registerBlockCheck, "write", 0, 0, 0) == REDISMODULE_ERR) {
|
||||
RedisModule_Log(ctx, "warning", "Failed to register command");
|
||||
goto err;
|
||||
}
|
||||
|
||||
return REDISMODULE_OK;
|
||||
err:
|
||||
if (strval) {
|
||||
RedisModule_FreeString(ctx, strval);
|
||||
strval = NULL;
|
||||
}
|
||||
return REDISMODULE_ERR;
|
||||
}
|
||||
|
||||
int RedisModule_OnUnload(RedisModuleCtx *ctx) {
|
||||
|
|
|
@ -346,13 +346,12 @@ start_server {tags {"modules"}} {
|
|||
test {loadmodule CONFIG values take precedence over loadmoduleex ARGS values} {
|
||||
# Load module with conflicting CONFIG and ARGS values
|
||||
r module loadex $testmodule \
|
||||
CONFIG moduleconfigs.mutable_bool yes \
|
||||
CONFIG moduleconfigs.string foo \
|
||||
CONFIG moduleconfigs.memory_numeric 2mb \
|
||||
ARGS moduleconfigs.mutable_bool no \
|
||||
ARGS moduleconfigs.memory_numeric 4mb
|
||||
ARGS override
|
||||
|
||||
# Verify CONFIG values took precedence
|
||||
assert_equal [r config get moduleconfigs.mutable_bool] "moduleconfigs.mutable_bool yes"
|
||||
# Verify CONFIG values took precedence over the pseudo values that
|
||||
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string foo"
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 2097152"
|
||||
|
||||
r module unload moduleconfigs
|
||||
|
@ -361,29 +360,26 @@ start_server {tags {"modules"}} {
|
|||
# Test: Ensure that modified configuration values from ARGS are correctly written to the config file
|
||||
test {Modified ARGS values are persisted after config rewrite when set through CONFIG commands} {
|
||||
# Load module with non-default ARGS values
|
||||
r module loadex $testmodule \
|
||||
ARGS moduleconfigs.memory_numeric 4mb \
|
||||
ARGS moduleconfigs.string_setting "custom_value"
|
||||
r module loadex $testmodule ARGS override
|
||||
|
||||
# Verify the initial ARGS values
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 4194304"
|
||||
assert_equal [r config get moduleconfigs.string_setting] "moduleconfigs.string_setting custom_value"
|
||||
# Verify the initial values were overwritten
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 123"
|
||||
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string foo"
|
||||
|
||||
# Set new values to simulate user configuration changes
|
||||
r config set moduleconfigs.memory_numeric 8mb
|
||||
r config set moduleconfigs.string_setting "modified_value"
|
||||
r config set moduleconfigs.memory_numeric 1mb
|
||||
r config set moduleconfigs.string "modified_value"
|
||||
|
||||
# Verify that the changes took effect
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 8388608"
|
||||
assert_equal [r config get moduleconfigs.string_setting] "moduleconfigs.string_setting modified_value"
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1048576"
|
||||
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string modified_value"
|
||||
|
||||
# Perform a config rewrite
|
||||
r config rewrite
|
||||
|
||||
# Read and verify config file contents to check for persistence
|
||||
set config_contents [file read [config_path]]
|
||||
assert_contains "moduleconfigs.memory_numeric 8388608" $config_contents
|
||||
assert_contains "moduleconfigs.string_setting modified_value" $config_contents
|
||||
restart_server 0 true false
|
||||
assert_equal [r config get moduleconfigs.memory_numeric] "moduleconfigs.memory_numeric 1048576"
|
||||
assert_equal [r config get moduleconfigs.string] "moduleconfigs.string modified_value"
|
||||
r module unload moduleconfigs
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue