redis/tests/unit/moduleapi
Mincho Paskalev 15706f2e82
Module set/get config API (#14051)
# Problem

Some redis modules need to call `CONFIG GET/SET` commands. Server may be
ran with `rename-command CONFIG ""`(or something similar) which leads to
the module being unable to access the config.

# Solution

Added new API functions for use by modules
```
RedisModuleConfigIterator* RedisModule_GetConfigIterator(RedisModuleCtx *ctx, const char *pattern);
void RedisModule_ReleaseConfigIterator(RedisModuleCtx *ctx, RedisModuleConfigIterator *iter);
const char *RedisModule_ConfigIteratorNext(RedisModuleConfigIterator *iter);
int RedisModule_GetConfigType(const char *name, RedisModuleConfigType *res);
int RedisModule_GetBoolConfig(RedisModuleCtx *ctx, const char *name, int *res);
int RedisModule_GetConfig(RedisModuleCtx *ctx, const char *name, RedisModuleString **res);
int RedisModule_GetEnumConfig(RedisModuleCtx *ctx, const char *name, RedisModuleString **res);
int RedisModule_GetNumericConfig(RedisModuleCtx *ctx, const char *name, long long *res);
int RedisModule_SetBoolConfig(RedisModuleCtx *ctx, const char *name, int value, RedisModuleString **err);
int RedisModule_SetConfig(RedisModuleCtx *ctx, const char *name, RedisModuleString *value, RedisModuleString **err);
int RedisModule_SetEnumConfig(RedisModuleCtx *ctx, const char *name, RedisModuleString *value, RedisModuleString **err);
int RedisModule_SetNumericConfig(RedisModuleCtx *ctx, const char *name, long long value, RedisModuleString **err);
```

## Implementation

The work is mostly done inside `config.c` as I didn't want to expose the
config dict outside of it. That means each of these module functions has
a corresponding method in `config.c` that actually does the job. F.e
`RedisModule_SetEnumConfig` calls `moduleSetEnumConfig` which is
implemented in `config.c`

## Notes

Also, refactored `configSetCommand` and `restoreBackupConfig` functions
for the following reasons:
- code and logic is now way more clear in `configSetCommand`. Only
caveat here is removal of an optimization that skipped running apply
functions that already have ran in favour of code clarity.
- Both functions needlessly separated logic for module configs and
normal configs whereas no such separation is needed. This also had the
side effect of removing some allocations.
- `restoreBackupConfig` now has clearer interface and can be reused with
ease. One of the places I reused it is for the individual
`moduleSet*Config` functions, each of which needs the restoration
functionality but for a single config only.

## Future

Additionally, a couple considerations were made for potentially
extending the API in the future
- if need be an API for atomically setting multiple config values can be
added - `RedisModule_SetConfigsTranscationStart/End` or similar that can
be put around `RedisModule_Set*Config` calls.
- if performance is an issue an API
`RedisModule_GetConfigIteratorNextWithTypehint` or similar may be added
in order not to incur the additional cost of calling
`RedisModule_GetConfigType`.

---------

Co-authored-by: Oran Agra <oran@redislabs.com>
2025-07-03 13:46:33 +03:00
..
aclcheck.tcl modules API: Add test for ACL check of empty prefix (#13678) 2024-12-10 09:16:30 +02:00
async_rm_call.tcl Before evicted and before expired server events are not executed inside an execution unit. (#12733) 2023-11-08 09:28:22 +02:00
auth.tcl Fix module redact test for valgrind (#10432) 2022-03-16 08:53:57 +02:00
basics.tcl Add flag for ability of a module context to execute debug commands (#13774) 2025-02-03 09:52:41 +02:00
blockedclient.tcl Async IO Threads (#13695) 2024-12-23 14:16:40 +08:00
blockonbackground.tcl Fix external test hang in redis-cli test when run in a certain order (#13423) 2024-07-17 15:42:28 +03:00
blockonkeys.tcl Modules: Unblock from within a timer coverage (#12337) 2023-06-22 23:15:16 +03:00
cluster.tcl Fix delKeysInSlot server events are not executed inside an execution unit (#12745) 2023-12-11 20:15:19 +02:00
cmdintrospection.tcl Add reply_schema to command json files (internal for now) (#10273) 2023-03-11 10:14:16 +02:00
commandfilter.tcl Un-register notification and server event when RedisModule_OnLoad fails (#12809) 2023-11-27 17:26:33 +02:00
configaccess.tcl Module set/get config API (#14051) 2025-07-03 13:46:33 +03:00
crash.tcl Add thread sanitizer run to daily CI (#13964) 2025-06-02 10:13:23 +03:00
datatype.tcl Fixed passing incorrect endtime value for module context (#13822) 2025-02-23 12:58:48 +08:00
datatype2.tcl Tests: don't rely on the response of MEMORY USAGE when mem_allocator is not jemalloc (#10010) 2021-12-27 21:37:21 +02:00
defrag.tcl Add thread sanitizer run to daily CI (#13964) 2025-06-02 10:13:23 +03:00
eventloop.tcl sub-command support for ACL CAT and COMMAND LIST. redisCommand always stores fullname (#10127) 2022-01-23 10:05:06 +02:00
fork.tcl Fix async safety in signal handlers (#12658) 2023-11-23 13:22:20 +02:00
getchannels.tcl Include missing getchannels.tcl in moduleapi tests and fix incorrect assertions (#14037) 2025-05-14 08:57:01 +08:00
getkeys.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
hash.tcl Fix various KEYSIZES enumeration issues (#13923) 2025-05-08 10:59:12 +03:00
hooks.tcl Un-register notification and server event when RedisModule_OnLoad fails (#12809) 2023-11-27 17:26:33 +02:00
infotest.tcl Fix missing sections for INFO ALL with module (#11291) 2022-09-21 08:10:03 +03:00
infra.tcl Build TLS as a loadable module 2022-08-23 12:37:56 +03:00
internalsecret.tcl Add internal connection and command mechanism (#13740) 2025-02-05 11:48:08 +02:00
keyspace_events.tcl avoid possible use-after-free with module KSN changes (#13875) 2025-03-24 12:24:52 +02:00
keyspecs.tcl Unify ACL failure error messaging. (#11160) 2022-10-16 09:01:37 +03:00
list.tcl Fix various KEYSIZES enumeration issues (#13923) 2025-05-08 10:59:12 +03:00
mallocsize.tcl Add RM_MallocSizeString, RM_MallocSizeDict (#10542) 2022-04-17 08:31:57 +03:00
misc.tcl Cluster compatibility check (#13846) 2025-03-20 10:35:53 +08:00
moduleauth.tcl Custom authentication for Modules (#11659) 2023-03-15 15:18:42 -07:00
moduleconfigs.tcl * fix comments 2025-02-06 13:16:33 +02:00
postnotifications.tcl Before evicted and before expired server events are not executed inside an execution unit. (#12733) 2023-11-08 09:28:22 +02:00
propagate.tcl Don't keep global replication buffer reference for replicas marked CLIENT_CLOSE_ASAP (#13363) 2024-06-26 08:26:23 +08:00
publish.tcl Fix broken protocol when PUBLISH emits local push inside MULTI (#12326) 2023-06-20 20:41:41 +03:00
rdbloadsave.tcl Fix RM_RdbLoad() to enable AOF after loading is completed (#13510) 2024-09-04 11:11:04 +03:00
reply.tcl Align RM_ReplyWithErrorFormat with RM_ReplyWithError (#12321) 2023-06-20 20:44:43 +03:00
scan.tcl Fix some test failures caused by key being deleted due to premature expiration (#13453) 2024-07-31 08:15:39 +08:00
stream.tcl Delete empty key if fails after moduleCreateEmptyKey() in module (#12129) 2023-05-07 10:13:19 +03:00
subcommands.tcl Fix regression not aborting transaction on error, and re-edit some error responses (#10612) 2022-04-25 13:08:13 +03:00
test_lazyfree.tcl Sort out mess around propagation and MULTI/EXEC (#9890) 2021-12-23 00:03:48 +02:00
testrdb.tcl Fix short read issue that causes exit() on replica (#14085) 2025-05-28 12:43:59 +03:00
timer.tcl forbid module to unload when it holds ongoing timer (#10187) 2022-02-01 14:54:11 +02:00
usercall.tcl Fix assertion in updateClientMemUsageAndBucket (#14152) 2025-07-02 11:55:57 +03:00
zset.tcl Fix various KEYSIZES enumeration issues (#13923) 2025-05-08 10:59:12 +03:00