Introduce redis module ctx flag 'server startup' & 'sentinel'

A module may be loaded only during initial stage, a typical case is
connection type shared library.

Introduce REDISMODULE_CTX_FLAGS_SERVER_STARTUP context flag
to tell the module the stage of Redis. Then the module gets the flag
by RedisModule_GetContextFlags(ctx), tests flags and returns error in
onload handler.

Also introduce 'REDISMODULE_CTX_FLAGS_SENTINEL' context flag to tell
the module the sentinel mode or not.

Suggested-by: Oran Agra <oran@redislabs.com>
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
This commit is contained in:
zhenwei pi 2022-08-03 09:48:01 +08:00
parent 0c4d2fcc8e
commit 8a59c19310
2 changed files with 16 additions and 1 deletions

View file

@ -3529,6 +3529,10 @@ int RM_GetSelectedDb(RedisModuleCtx *ctx) {
*
* * REDISMODULE_CTX_FLAGS_RESP3: Indicate the that client attached to this
* context is using RESP3.
*
* * REDISMODULE_CTX_FLAGS_SERVER_STARTUP: The Redis instance is starting
*
* * REDISMODULE_CTX_FLAGS_SENTINEL: The Redis instance is in sentinel mode
*/
int RM_GetContextFlags(RedisModuleCtx *ctx) {
int flags = 0;
@ -3614,6 +3618,13 @@ int RM_GetContextFlags(RedisModuleCtx *ctx) {
if (hasActiveChildProcess()) flags |= REDISMODULE_CTX_FLAGS_ACTIVE_CHILD;
if (server.in_fork_child) flags |= REDISMODULE_CTX_FLAGS_IS_CHILD;
/* Non-empty server.loadmodule_queue means that Redis is starting. */
if (listLength(server.loadmodule_queue) > 0)
flags |= REDISMODULE_CTX_FLAGS_SERVER_STARTUP;
if (server.sentinel_mode)
flags |= REDISMODULE_CTX_FLAGS_SENTINEL;
return flags;
}

View file

@ -162,11 +162,15 @@ typedef struct RedisModuleStreamID {
#define REDISMODULE_CTX_FLAGS_RESP3 (1<<22)
/* Redis is currently async loading database for diskless replication. */
#define REDISMODULE_CTX_FLAGS_ASYNC_LOADING (1<<23)
/* Redis is starting. */
#define REDISMODULE_CTX_FLAGS_SERVER_STARTUP (1<<24)
/* The instance is running in sentinel mode */
#define REDISMODULE_CTX_FLAGS_SENTINEL (1<<25)
/* Next context flag, must be updated when adding new flags above!
This flag should not be used directly by the module.
* Use RedisModule_GetContextFlagsAll instead. */
#define _REDISMODULE_CTX_FLAGS_NEXT (1<<24)
#define _REDISMODULE_CTX_FLAGS_NEXT (1<<26)
/* Keyspace changes notification classes. Every class is associated with a
* character for configuration purposes.