2019-10-23 16:38:30 +00:00
|
|
|
/* This module is used to test the server events hooks API.
|
|
|
|
*
|
|
|
|
* -----------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
#include "valkeymodule.h"
|
2019-10-29 15:59:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2023-11-27 15:26:33 +00:00
|
|
|
#include <strings.h>
|
2022-11-30 09:56:36 +00:00
|
|
|
#include <assert.h>
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
/* We need to store events to be able to test and see what we got, and we can't
|
|
|
|
* store them in the key-space since that would mess up rdb loading (duplicates)
|
|
|
|
* and be lost of flushdb. */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDict *event_log = NULL;
|
2022-11-30 09:56:36 +00:00
|
|
|
/* stores all the keys on which we got 'removed' event */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDict *removed_event_log = NULL;
|
2022-11-30 09:56:36 +00:00
|
|
|
/* stores all the subevent on which we got 'removed' event */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDict *removed_subevent_type = NULL;
|
2022-11-30 09:56:36 +00:00
|
|
|
/* stores all the keys on which we got 'removed' event with expiry information */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDict *removed_expiry_log = NULL;
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
typedef struct EventElement {
|
|
|
|
long count;
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *last_val_string;
|
2019-10-29 15:59:09 +00:00
|
|
|
long last_val_int;
|
|
|
|
} EventElement;
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void LogStringEvent(ValkeyModuleCtx *ctx, const char* keyname, const char* data) {
|
|
|
|
EventElement *event = ValkeyModule_DictGetC(event_log, (void*)keyname, strlen(keyname), NULL);
|
2019-10-29 15:59:09 +00:00
|
|
|
if (!event) {
|
2024-04-23 15:55:44 +00:00
|
|
|
event = ValkeyModule_Alloc(sizeof(EventElement));
|
2019-10-29 15:59:09 +00:00
|
|
|
memset(event, 0, sizeof(EventElement));
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictSetC(event_log, (void*)keyname, strlen(keyname), event);
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
if (event->last_val_string) ValkeyModule_FreeString(ctx, event->last_val_string);
|
|
|
|
event->last_val_string = ValkeyModule_CreateString(ctx, data, strlen(data));
|
2019-10-29 15:59:09 +00:00
|
|
|
event->count++;
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void LogNumericEvent(ValkeyModuleCtx *ctx, const char* keyname, long data) {
|
|
|
|
VALKEYMODULE_NOT_USED(ctx);
|
|
|
|
EventElement *event = ValkeyModule_DictGetC(event_log, (void*)keyname, strlen(keyname), NULL);
|
2019-10-29 15:59:09 +00:00
|
|
|
if (!event) {
|
2024-04-23 15:55:44 +00:00
|
|
|
event = ValkeyModule_Alloc(sizeof(EventElement));
|
2019-10-29 15:59:09 +00:00
|
|
|
memset(event, 0, sizeof(EventElement));
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictSetC(event_log, (void*)keyname, strlen(keyname), event);
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
event->last_val_int = data;
|
|
|
|
event->count++;
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void FreeEvent(ValkeyModuleCtx *ctx, EventElement *event) {
|
2019-10-29 15:59:09 +00:00
|
|
|
if (event->last_val_string)
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeString(ctx, event->last_val_string);
|
|
|
|
ValkeyModule_Free(event);
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int cmdEventCount(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
|
|
|
if (argc != 2){
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_WrongArity(ctx);
|
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
EventElement *event = ValkeyModule_DictGet(event_log, argv[1], NULL);
|
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, event? event->count: 0);
|
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int cmdEventLast(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
|
|
|
if (argc != 2){
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_WrongArity(ctx);
|
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
EventElement *event = ValkeyModule_DictGet(event_log, argv[1], NULL);
|
2019-10-29 15:59:09 +00:00
|
|
|
if (event && event->last_val_string)
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithString(ctx, event->last_val_string);
|
2019-10-29 15:59:09 +00:00
|
|
|
else if (event)
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithLongLong(ctx, event->last_val_int);
|
2019-10-29 15:59:09 +00:00
|
|
|
else
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithNull(ctx);
|
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void clearEvents(ValkeyModuleCtx *ctx)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *key;
|
2019-10-29 15:59:09 +00:00
|
|
|
EventElement *event;
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDictIter *iter = ValkeyModule_DictIteratorStart(event_log, "^", NULL);
|
|
|
|
while((key = ValkeyModule_DictNext(ctx, iter, (void**)&event)) != NULL) {
|
2019-10-29 15:59:09 +00:00
|
|
|
event->count = 0;
|
|
|
|
event->last_val_int = 0;
|
2024-04-23 15:55:44 +00:00
|
|
|
if (event->last_val_string) ValkeyModule_FreeString(ctx, event->last_val_string);
|
2019-10-29 15:59:09 +00:00
|
|
|
event->last_val_string = NULL;
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictDel(event_log, key, NULL);
|
|
|
|
ValkeyModule_Free(event);
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictIteratorStop(iter);
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int cmdEventsClear(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(argc);
|
|
|
|
VALKEYMODULE_NOT_USED(argv);
|
2019-10-29 15:59:09 +00:00
|
|
|
clearEvents(ctx);
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
2019-10-23 16:38:30 +00:00
|
|
|
|
|
|
|
/* Client state change callback. */
|
2024-04-23 15:55:44 +00:00
|
|
|
void clientChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-23 16:38:30 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
2019-10-23 16:38:30 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleClientInfo *ci = data;
|
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"client-connected" : "client-disconnected";
|
|
|
|
LogNumericEvent(ctx, keyname, ci->id);
|
2019-10-23 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void flushdbCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-23 16:38:30 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
2019-10-23 16:38:30 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleFlushInfo *fi = data;
|
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_FLUSHDB_START) ?
|
2019-10-23 16:38:30 +00:00
|
|
|
"flush-start" : "flush-end";
|
2019-10-29 15:59:09 +00:00
|
|
|
LogNumericEvent(ctx, keyname, fi->dbnum);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void roleChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleReplicationInfo *ri = data;
|
|
|
|
char *keyname = (sub == VALKEYMODULE_EVENT_REPLROLECHANGED_NOW_PRIMARY) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"role-master" : "role-replica";
|
|
|
|
LogStringEvent(ctx, keyname, ri->masterhost);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void replicationChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"replica-online" : "replica-offline";
|
|
|
|
LogNumericEvent(ctx, keyname, 0);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void rasterLinkChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_PRIMARY_LINK_UP) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"masterlink-up" : "masterlink-down";
|
|
|
|
LogNumericEvent(ctx, keyname, 0);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void persistenceCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
char *keyname = NULL;
|
|
|
|
switch (sub) {
|
2024-04-23 15:55:44 +00:00
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_RDB_START: keyname = "persistence-rdb-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_AOF_START: keyname = "persistence-aof-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_SYNC_AOF_START: keyname = "persistence-syncaof-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START: keyname = "persistence-syncrdb-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_ENDED: keyname = "persistence-end"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_PERSISTENCE_FAILED: keyname = "persistence-failed"; break;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
/* modifying the keyspace from the fork child is not an option, using log instead */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_Log(ctx, "warning", "module-event-%s", keyname);
|
|
|
|
if (sub == VALKEYMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START ||
|
|
|
|
sub == VALKEYMODULE_SUBEVENT_PERSISTENCE_SYNC_AOF_START)
|
Always create base AOF file when redis start from empty. (#10102)
Force create a BASE file (use a foreground `rewriteAppendOnlyFile`) when redis starts from an
empty data set and `appendonly` is yes.
The reasoning is that normally, after redis is running for some time, and the AOF has gone though
a few rewrites, there's always a base rdb file. and the scenario where the base file is missing, is
kinda rare (happens only at empty startup), so this change normalizes it.
But more importantly, there are or could be some complex modules that are started with some
configuration, when they create persistence they write that configuration to RDB AUX fields, so
that can can always know with which configuration the persistence file they're loading was
created (could be critical). there is (was) one scenario in which they could load their persisted data,
and that configuration was missing, and this change fixes it.
Add a new module event: REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_AOF_START, similar to
REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START which is async.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-13 06:49:26 +00:00
|
|
|
{
|
2019-10-29 15:59:09 +00:00
|
|
|
LogNumericEvent(ctx, keyname, 0);
|
Always create base AOF file when redis start from empty. (#10102)
Force create a BASE file (use a foreground `rewriteAppendOnlyFile`) when redis starts from an
empty data set and `appendonly` is yes.
The reasoning is that normally, after redis is running for some time, and the AOF has gone though
a few rewrites, there's always a base rdb file. and the scenario where the base file is missing, is
kinda rare (happens only at empty startup), so this change normalizes it.
But more importantly, there are or could be some complex modules that are started with some
configuration, when they create persistence they write that configuration to RDB AUX fields, so
that can can always know with which configuration the persistence file they're loading was
created (could be critical). there is (was) one scenario in which they could load their persisted data,
and that configuration was missing, and this change fixes it.
Add a new module event: REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_AOF_START, similar to
REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START which is async.
Co-authored-by: Oran Agra <oran@redislabs.com>
2022-01-13 06:49:26 +00:00
|
|
|
}
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void loadingCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
char *keyname = NULL;
|
|
|
|
switch (sub) {
|
2024-04-23 15:55:44 +00:00
|
|
|
case VALKEYMODULE_SUBEVENT_LOADING_RDB_START: keyname = "loading-rdb-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_LOADING_AOF_START: keyname = "loading-aof-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_LOADING_REPL_START: keyname = "loading-repl-start"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_LOADING_ENDED: keyname = "loading-end"; break;
|
|
|
|
case VALKEYMODULE_SUBEVENT_LOADING_FAILED: keyname = "loading-failed"; break;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
LogNumericEvent(ctx, keyname, 0);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void loadingProgressCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleLoadingProgress *ei = data;
|
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_LOADING_PROGRESS_RDB) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"loading-progress-rdb" : "loading-progress-aof";
|
|
|
|
LogNumericEvent(ctx, keyname, ei->progress);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void shutdownCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(data);
|
|
|
|
VALKEYMODULE_NOT_USED(sub);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_Log(ctx, "warning", "module-event-%s", "shutdown");
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void cronLoopCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(sub);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleCronLoop *ei = data;
|
2019-10-29 15:59:09 +00:00
|
|
|
LogNumericEvent(ctx, "cron-loop", ei->hz);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void moduleChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2019-10-29 15:59:09 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleModuleChange *ei = data;
|
|
|
|
char *keyname = (sub == VALKEYMODULE_SUBEVENT_MODULE_LOADED) ?
|
2019-10-29 15:59:09 +00:00
|
|
|
"module-loaded" : "module-unloaded";
|
|
|
|
LogStringEvent(ctx, keyname, ei->module_name);
|
2019-10-23 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void swapDbCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2020-09-20 10:36:20 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
VALKEYMODULE_NOT_USED(sub);
|
2020-09-20 10:36:20 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleSwapDbInfo *ei = data;
|
2020-09-20 10:36:20 +00:00
|
|
|
LogNumericEvent(ctx, "swapdb-first", ei->dbnum_first);
|
|
|
|
LogNumericEvent(ctx, "swapdb-second", ei->dbnum_second);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void configChangeCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2022-03-07 15:37:57 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
|
|
|
if (sub != VALKEYMODULE_SUBEVENT_CONFIG_CHANGE) {
|
2022-03-07 15:37:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleConfigChangeV1 *ei = data;
|
2022-03-07 15:37:57 +00:00
|
|
|
LogNumericEvent(ctx, "config-change-count", ei->num_changes);
|
|
|
|
LogStringEvent(ctx, "config-change-first", ei->config_names[0]);
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
void keyInfoCallback(ValkeyModuleCtx *ctx, ValkeyModuleEvent e, uint64_t sub, void *data)
|
2022-11-30 09:56:36 +00:00
|
|
|
{
|
2024-04-23 15:55:44 +00:00
|
|
|
VALKEYMODULE_NOT_USED(e);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleKeyInfoV1 *ei = data;
|
|
|
|
ValkeyModuleKey *kp = ei->key;
|
|
|
|
ValkeyModuleString *key = (ValkeyModuleString *) ValkeyModule_GetKeyNameFromModuleKey(kp);
|
|
|
|
const char *keyname = ValkeyModule_StringPtrLen(key, NULL);
|
|
|
|
ValkeyModuleString *event_keyname = ValkeyModule_CreateStringPrintf(ctx, "key-info-%s", keyname);
|
|
|
|
LogStringEvent(ctx, ValkeyModule_StringPtrLen(event_keyname, NULL), keyname);
|
|
|
|
ValkeyModule_FreeString(ctx, event_keyname);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
|
|
|
/* Despite getting a key object from the callback, we also try to re-open it
|
|
|
|
* to make sure the callback is called before it is actually removed from the keyspace. */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleKey *kp_open = ValkeyModule_OpenKey(ctx, key, VALKEYMODULE_READ);
|
|
|
|
assert(ValkeyModule_ValueLength(kp) == ValkeyModule_ValueLength(kp_open));
|
|
|
|
ValkeyModule_CloseKey(kp_open);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
|
|
|
/* We also try to RM_Call a command that accesses that key, also to make sure it's still in the keyspace. */
|
|
|
|
char *size_command = NULL;
|
2024-04-23 15:55:44 +00:00
|
|
|
int key_type = ValkeyModule_KeyType(kp);
|
|
|
|
if (key_type == VALKEYMODULE_KEYTYPE_STRING) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "STRLEN";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (key_type == VALKEYMODULE_KEYTYPE_LIST) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "LLEN";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (key_type == VALKEYMODULE_KEYTYPE_HASH) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "HLEN";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (key_type == VALKEYMODULE_KEYTYPE_SET) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "SCARD";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (key_type == VALKEYMODULE_KEYTYPE_ZSET) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "ZCARD";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (key_type == VALKEYMODULE_KEYTYPE_STREAM) {
|
2022-11-30 09:56:36 +00:00
|
|
|
size_command = "XLEN";
|
|
|
|
}
|
|
|
|
if (size_command != NULL) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleCallReply *reply = ValkeyModule_Call(ctx, size_command, "s", key);
|
2022-11-30 09:56:36 +00:00
|
|
|
assert(reply != NULL);
|
2024-04-23 15:55:44 +00:00
|
|
|
assert(ValkeyModule_ValueLength(kp) == (size_t) ValkeyModule_CallReplyInteger(reply));
|
|
|
|
ValkeyModule_FreeCallReply(reply);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now use the key object we got from the callback for various validations. */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *prev = ValkeyModule_DictGetC(removed_event_log, (void*)keyname, strlen(keyname), NULL);
|
2022-11-30 09:56:36 +00:00
|
|
|
/* We keep object length */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *v = ValkeyModule_CreateStringPrintf(ctx, "%zd", ValkeyModule_ValueLength(kp));
|
2022-11-30 09:56:36 +00:00
|
|
|
/* For string type, we keep value instead of length */
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_KeyType(kp) == VALKEYMODULE_KEYTYPE_STRING) {
|
|
|
|
ValkeyModule_FreeString(ctx, v);
|
2022-11-30 09:56:36 +00:00
|
|
|
size_t len;
|
2024-04-23 15:55:44 +00:00
|
|
|
/* We need to access the string value with ValkeyModule_StringDMA.
|
|
|
|
* ValkeyModule_StringDMA may call dbUnshareStringValue to free the origin object,
|
2022-11-30 09:56:36 +00:00
|
|
|
* so we also can test it. */
|
2024-04-23 15:55:44 +00:00
|
|
|
char *s = ValkeyModule_StringDMA(kp, &len, VALKEYMODULE_READ);
|
|
|
|
v = ValkeyModule_CreateString(ctx, s, len);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictReplaceC(removed_event_log, (void*)keyname, strlen(keyname), v);
|
2022-11-30 09:56:36 +00:00
|
|
|
if (prev != NULL) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeString(ctx, prev);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *subevent = "deleted";
|
2024-04-23 15:55:44 +00:00
|
|
|
if (sub == VALKEYMODULE_SUBEVENT_KEY_EXPIRED) {
|
2022-11-30 09:56:36 +00:00
|
|
|
subevent = "expired";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (sub == VALKEYMODULE_SUBEVENT_KEY_EVICTED) {
|
2022-11-30 09:56:36 +00:00
|
|
|
subevent = "evicted";
|
2024-04-23 15:55:44 +00:00
|
|
|
} else if (sub == VALKEYMODULE_SUBEVENT_KEY_OVERWRITTEN) {
|
2022-11-30 09:56:36 +00:00
|
|
|
subevent = "overwritten";
|
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_DictReplaceC(removed_subevent_type, (void*)keyname, strlen(keyname), (void *)subevent);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *prevexpire = ValkeyModule_DictGetC(removed_expiry_log, (void*)keyname, strlen(keyname), NULL);
|
|
|
|
ValkeyModuleString *expire = ValkeyModule_CreateStringPrintf(ctx, "%lld", ValkeyModule_GetAbsExpire(kp));
|
|
|
|
ValkeyModule_DictReplaceC(removed_expiry_log, (void*)keyname, strlen(keyname), (void *)expire);
|
2022-11-30 09:56:36 +00:00
|
|
|
if (prevexpire != NULL) {
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeString(ctx, prevexpire);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
static int cmdIsKeyRemoved(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc){
|
2022-11-30 09:56:36 +00:00
|
|
|
if(argc != 2){
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
const char *key = ValkeyModule_StringPtrLen(argv[1], NULL);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString *value = ValkeyModule_DictGetC(removed_event_log, (void*)key, strlen(key), NULL);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
|
|
|
if (value == NULL) {
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithError(ctx, "ERR Key was not removed");
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
const char *subevent = ValkeyModule_DictGetC(removed_subevent_type, (void*)key, strlen(key), NULL);
|
|
|
|
ValkeyModule_ReplyWithArray(ctx, 2);
|
|
|
|
ValkeyModule_ReplyWithString(ctx, value);
|
|
|
|
ValkeyModule_ReplyWithSimpleString(ctx, subevent);
|
2022-11-30 09:56:36 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
static int cmdKeyExpiry(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc){
|
2022-11-30 09:56:36 +00:00
|
|
|
if(argc != 2){
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_WrongArity(ctx);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
const char* key = ValkeyModule_StringPtrLen(argv[1], NULL);
|
|
|
|
ValkeyModuleString *expire = ValkeyModule_DictGetC(removed_expiry_log, (void*)key, strlen(key), NULL);
|
2022-11-30 09:56:36 +00:00
|
|
|
if (expire == NULL) {
|
2024-04-23 15:55:44 +00:00
|
|
|
return ValkeyModule_ReplyWithError(ctx, "ERR Key was not removed");
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_ReplyWithString(ctx, expire);
|
|
|
|
return VALKEYMODULE_OK;
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
|
|
|
|
2024-04-09 08:24:03 +00:00
|
|
|
/* This function must be present on each module. It is used in order to
|
|
|
|
* register the commands into the server. */
|
2024-04-23 15:55:44 +00:00
|
|
|
int ValkeyModule_OnLoad(ValkeyModuleCtx *ctx, ValkeyModuleString **argv, int argc) {
|
2020-10-11 14:21:58 +00:00
|
|
|
#define VerifySubEventSupported(e, s) \
|
2024-04-23 15:55:44 +00:00
|
|
|
if (!ValkeyModule_IsSubEventSupported(e, s)) { \
|
|
|
|
return VALKEYMODULE_ERR; \
|
2020-10-11 14:21:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
if (ValkeyModule_Init(ctx,"testhook",1,VALKEYMODULE_APIVER_1)
|
|
|
|
== VALKEYMODULE_ERR) return VALKEYMODULE_ERR;
|
2019-10-23 16:38:30 +00:00
|
|
|
|
2020-10-11 14:21:58 +00:00
|
|
|
/* Example on how to check if a server sub event is supported */
|
2024-04-23 15:55:44 +00:00
|
|
|
if (!ValkeyModule_IsSubEventSupported(ValkeyModuleEvent_ReplicationRoleChanged, VALKEYMODULE_EVENT_REPLROLECHANGED_NOW_PRIMARY)) {
|
|
|
|
return VALKEYMODULE_ERR;
|
2020-10-11 14:21:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-29 15:59:09 +00:00
|
|
|
/* replication related hooks */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_ReplicationRoleChanged, roleChangeCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_ReplicaChange, replicationChangeCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_PrimaryLinkChange, rasterLinkChangeCallback);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
/* persistence related hooks */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_Persistence, persistenceCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_Loading, loadingCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_LoadingProgress, loadingProgressCallback);
|
2019-10-29 15:59:09 +00:00
|
|
|
|
|
|
|
/* other hooks */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_ClientChange, clientChangeCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_FlushDB, flushdbCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_Shutdown, shutdownCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_CronLoop, cronLoopCallback);
|
|
|
|
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_ModuleChange, moduleChangeCallback);
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_SwapDB, swapDbCallback);
|
|
|
|
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_Config, configChangeCallback);
|
|
|
|
|
|
|
|
ValkeyModule_SubscribeToServerEvent(ctx,
|
|
|
|
ValkeyModuleEvent_Key, keyInfoCallback);
|
|
|
|
|
|
|
|
event_log = ValkeyModule_CreateDict(ctx);
|
|
|
|
removed_event_log = ValkeyModule_CreateDict(ctx);
|
|
|
|
removed_subevent_type = ValkeyModule_CreateDict(ctx);
|
|
|
|
removed_expiry_log = ValkeyModule_CreateDict(ctx);
|
|
|
|
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"hooks.event_count", cmdEventCount,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"hooks.event_last", cmdEventLast,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"hooks.clear", cmdEventsClear,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"hooks.is_key_removed", cmdIsKeyRemoved,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
|
|
|
if (ValkeyModule_CreateCommand(ctx,"hooks.pexpireat", cmdKeyExpiry,"",0,0,0) == VALKEYMODULE_ERR)
|
|
|
|
return VALKEYMODULE_ERR;
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2023-11-27 15:26:33 +00:00
|
|
|
if (argc == 1) {
|
2024-04-23 15:55:44 +00:00
|
|
|
const char *ptr = ValkeyModule_StringPtrLen(argv[0], NULL);
|
2023-11-27 15:26:33 +00:00
|
|
|
if (!strcasecmp(ptr, "noload")) {
|
|
|
|
/* This is a hint that we return ERR at the last moment of OnLoad. */
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeDict(ctx, event_log);
|
|
|
|
ValkeyModule_FreeDict(ctx, removed_event_log);
|
|
|
|
ValkeyModule_FreeDict(ctx, removed_subevent_type);
|
|
|
|
ValkeyModule_FreeDict(ctx, removed_expiry_log);
|
|
|
|
return VALKEYMODULE_ERR;
|
2023-11-27 15:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2019-10-23 16:38:30 +00:00
|
|
|
}
|
2019-10-29 15:59:09 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
int ValkeyModule_OnUnload(ValkeyModuleCtx *ctx) {
|
2019-10-29 15:59:09 +00:00
|
|
|
clearEvents(ctx);
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeDict(ctx, event_log);
|
2019-10-29 15:59:09 +00:00
|
|
|
event_log = NULL;
|
2022-11-30 09:56:36 +00:00
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleDictIter *iter = ValkeyModule_DictIteratorStartC(removed_event_log, "^", NULL, 0);
|
2022-11-30 09:56:36 +00:00
|
|
|
char* key;
|
|
|
|
size_t keyLen;
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModuleString* val;
|
|
|
|
while((key = ValkeyModule_DictNextC(iter, &keyLen, (void**)&val))){
|
|
|
|
ValkeyModule_FreeString(ctx, val);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeDict(ctx, removed_event_log);
|
|
|
|
ValkeyModule_DictIteratorStop(iter);
|
2022-11-30 09:56:36 +00:00
|
|
|
removed_event_log = NULL;
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeDict(ctx, removed_subevent_type);
|
2022-11-30 09:56:36 +00:00
|
|
|
removed_subevent_type = NULL;
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
iter = ValkeyModule_DictIteratorStartC(removed_expiry_log, "^", NULL, 0);
|
|
|
|
while((key = ValkeyModule_DictNextC(iter, &keyLen, (void**)&val))){
|
|
|
|
ValkeyModule_FreeString(ctx, val);
|
2022-11-30 09:56:36 +00:00
|
|
|
}
|
2024-04-23 15:55:44 +00:00
|
|
|
ValkeyModule_FreeDict(ctx, removed_expiry_log);
|
|
|
|
ValkeyModule_DictIteratorStop(iter);
|
2022-11-30 09:56:36 +00:00
|
|
|
removed_expiry_log = NULL;
|
|
|
|
|
2024-04-23 15:55:44 +00:00
|
|
|
return VALKEYMODULE_OK;
|
2019-10-29 15:59:09 +00:00
|
|
|
}
|
|
|
|
|