summaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/modules/keyspace_events.c
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/tests/modules/keyspace_events.c')
-rw-r--r--examples/redis-unstable/tests/modules/keyspace_events.c479
1 files changed, 479 insertions, 0 deletions
diff --git a/examples/redis-unstable/tests/modules/keyspace_events.c b/examples/redis-unstable/tests/modules/keyspace_events.c
new file mode 100644
index 0000000..146261f
--- /dev/null
+++ b/examples/redis-unstable/tests/modules/keyspace_events.c
@@ -0,0 +1,479 @@
1/* This module is used to test the server keyspace events API.
2 *
3 * -----------------------------------------------------------------------------
4 *
5 * Copyright (c) 2020-Present, Redis Ltd.
6 * All rights reserved.
7 *
8 * Licensed under your choice of (a) the Redis Source Available License 2.0
9 * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
10 * GNU Affero General Public License v3 (AGPLv3).
11 */
12
13#define _BSD_SOURCE
14#define _DEFAULT_SOURCE /* For usleep */
15
16#include "redismodule.h"
17#include <stdio.h>
18#include <string.h>
19#include <strings.h>
20#include <unistd.h>
21
22ustime_t cached_time = 0;
23
24/** stores all the keys on which we got 'loaded' keyspace notification **/
25RedisModuleDict *loaded_event_log = NULL;
26/** stores all the keys on which we got 'module' keyspace notification **/
27RedisModuleDict *module_event_log = NULL;
28
29/** Counts how many deleted KSN we got on keys with a prefix of "count_dels_" **/
30static size_t dels = 0;
31
32static int KeySpace_NotificationLoaded(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key){
33 REDISMODULE_NOT_USED(ctx);
34 REDISMODULE_NOT_USED(type);
35
36 if(strcmp(event, "loaded") == 0){
37 const char* keyName = RedisModule_StringPtrLen(key, NULL);
38 int nokey;
39 RedisModule_DictGetC(loaded_event_log, (void*)keyName, strlen(keyName), &nokey);
40 if(nokey){
41 RedisModule_DictSetC(loaded_event_log, (void*)keyName, strlen(keyName), RedisModule_HoldString(ctx, key));
42 }
43 }
44
45 return REDISMODULE_OK;
46}
47
48static long long callback_call_count = 0;
49static int KeySpace_NotificationGeneric(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
50 REDISMODULE_NOT_USED(type);
51 callback_call_count++;
52 const char *key_str = RedisModule_StringPtrLen(key, NULL);
53 if (strncmp(key_str, "count_dels_", 11) == 0 && strcmp(event, "del") == 0) {
54 if (RedisModule_GetContextFlags(ctx) & REDISMODULE_CTX_FLAGS_MASTER) {
55 dels++;
56 RedisModule_Replicate(ctx, "keyspace.incr_dels", "");
57 }
58 return REDISMODULE_OK;
59 }
60 if (cached_time) {
61 RedisModule_Assert(cached_time == RedisModule_CachedMicroseconds());
62 usleep(1);
63 RedisModule_Assert(cached_time != RedisModule_Microseconds());
64 }
65
66 if (strcmp(event, "del") == 0) {
67 RedisModuleString *copykey = RedisModule_CreateStringPrintf(ctx, "%s_copy", RedisModule_StringPtrLen(key, NULL));
68 RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", copykey);
69 RedisModule_FreeString(ctx, copykey);
70 RedisModule_FreeCallReply(rep);
71
72 int ctx_flags = RedisModule_GetContextFlags(ctx);
73 if (ctx_flags & REDISMODULE_CTX_FLAGS_LUA) {
74 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "c", "lua");
75 RedisModule_FreeCallReply(rep);
76 }
77 if (ctx_flags & REDISMODULE_CTX_FLAGS_MULTI) {
78 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "c", "multi");
79 RedisModule_FreeCallReply(rep);
80 }
81 }
82
83 return REDISMODULE_OK;
84}
85
86static int KeySpace_NotificationExpired(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
87 REDISMODULE_NOT_USED(type);
88 REDISMODULE_NOT_USED(event);
89 REDISMODULE_NOT_USED(key);
90
91 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "c!", "testkeyspace:expired");
92 RedisModule_FreeCallReply(rep);
93
94 return REDISMODULE_OK;
95}
96
97/* This key miss notification handler is performing a write command inside the notification callback.
98 * Notice, it is discourage and currently wrong to perform a write command inside key miss event.
99 * It can cause read commands to be replicated to the replica/aof. This test is here temporary (for coverage and
100 * verification that it's not crashing). */
101static int KeySpace_NotificationModuleKeyMiss(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
102 REDISMODULE_NOT_USED(type);
103 REDISMODULE_NOT_USED(event);
104 REDISMODULE_NOT_USED(key);
105
106 int flags = RedisModule_GetContextFlags(ctx);
107 if (!(flags & REDISMODULE_CTX_FLAGS_MASTER)) {
108 return REDISMODULE_OK; // ignore the event on replica
109 }
110
111 RedisModuleCallReply* rep = RedisModule_Call(ctx, "incr", "!c", "missed");
112 RedisModule_FreeCallReply(rep);
113
114 return REDISMODULE_OK;
115}
116
117static int KeySpace_NotificationModuleString(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
118 REDISMODULE_NOT_USED(type);
119 REDISMODULE_NOT_USED(event);
120 RedisModuleKey *redis_key = RedisModule_OpenKey(ctx, key, REDISMODULE_READ);
121
122 size_t len = 0;
123 /* RedisModule_StringDMA could change the data format and cause the old robj to be freed.
124 * This code verifies that such format change will not cause any crashes.*/
125 char *data = RedisModule_StringDMA(redis_key, &len, REDISMODULE_READ);
126 int res = strncmp(data, "dummy", 5);
127 REDISMODULE_NOT_USED(res);
128
129 RedisModule_CloseKey(redis_key);
130
131 return REDISMODULE_OK;
132}
133
134static void KeySpace_PostNotificationStringFreePD(void *pd) {
135 RedisModule_FreeString(NULL, pd);
136}
137
138static void KeySpace_PostNotificationString(RedisModuleCtx *ctx, void *pd) {
139 REDISMODULE_NOT_USED(ctx);
140 RedisModuleCallReply* rep = RedisModule_Call(ctx, "incr", "!s", pd);
141 RedisModule_FreeCallReply(rep);
142}
143
144static int KeySpace_NotificationModuleStringPostNotificationJob(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
145 REDISMODULE_NOT_USED(ctx);
146 REDISMODULE_NOT_USED(type);
147 REDISMODULE_NOT_USED(event);
148
149 const char *key_str = RedisModule_StringPtrLen(key, NULL);
150
151 if (strncmp(key_str, "string1_", 8) != 0) {
152 return REDISMODULE_OK;
153 }
154
155 RedisModuleString *new_key = RedisModule_CreateStringPrintf(NULL, "string_changed{%s}", key_str);
156 RedisModule_AddPostNotificationJob(ctx, KeySpace_PostNotificationString, new_key, KeySpace_PostNotificationStringFreePD);
157 return REDISMODULE_OK;
158}
159
160static int KeySpace_NotificationModule(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
161 REDISMODULE_NOT_USED(ctx);
162 REDISMODULE_NOT_USED(type);
163 REDISMODULE_NOT_USED(event);
164
165 const char* keyName = RedisModule_StringPtrLen(key, NULL);
166 int nokey;
167 RedisModule_DictGetC(module_event_log, (void*)keyName, strlen(keyName), &nokey);
168 if(nokey){
169 RedisModule_DictSetC(module_event_log, (void*)keyName, strlen(keyName), RedisModule_HoldString(ctx, key));
170 }
171 return REDISMODULE_OK;
172}
173
174static int cmdNotify(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
175 if(argc != 2){
176 return RedisModule_WrongArity(ctx);
177 }
178
179 RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_MODULE, "notify", argv[1]);
180 RedisModule_ReplyWithNull(ctx);
181 return REDISMODULE_OK;
182}
183
184static int cmdIsModuleKeyNotified(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
185 if(argc != 2){
186 return RedisModule_WrongArity(ctx);
187 }
188
189 const char* key = RedisModule_StringPtrLen(argv[1], NULL);
190
191 int nokey;
192 RedisModuleString* keyStr = RedisModule_DictGetC(module_event_log, (void*)key, strlen(key), &nokey);
193
194 RedisModule_ReplyWithArray(ctx, 2);
195 RedisModule_ReplyWithLongLong(ctx, !nokey);
196 if(nokey){
197 RedisModule_ReplyWithNull(ctx);
198 }else{
199 RedisModule_ReplyWithString(ctx, keyStr);
200 }
201 return REDISMODULE_OK;
202}
203
204static int cmdIsKeyLoaded(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
205 if(argc != 2){
206 return RedisModule_WrongArity(ctx);
207 }
208
209 const char* key = RedisModule_StringPtrLen(argv[1], NULL);
210
211 int nokey;
212 RedisModuleString* keyStr = RedisModule_DictGetC(loaded_event_log, (void*)key, strlen(key), &nokey);
213
214 RedisModule_ReplyWithArray(ctx, 2);
215 RedisModule_ReplyWithLongLong(ctx, !nokey);
216 if(nokey){
217 RedisModule_ReplyWithNull(ctx);
218 }else{
219 RedisModule_ReplyWithString(ctx, keyStr);
220 }
221 return REDISMODULE_OK;
222}
223
224static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
225 if (argc != 2)
226 return RedisModule_WrongArity(ctx);
227
228 cached_time = RedisModule_CachedMicroseconds();
229
230 RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", argv[1]);
231 if (!rep) {
232 RedisModule_ReplyWithError(ctx, "NULL reply returned");
233 } else {
234 RedisModule_ReplyWithCallReply(ctx, rep);
235 RedisModule_FreeCallReply(rep);
236 }
237 cached_time = 0;
238 return REDISMODULE_OK;
239}
240
241/* Call INCR and propagate using RM_Call with `!`. */
242static int cmdIncrCase1(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
243 if (argc != 2)
244 return RedisModule_WrongArity(ctx);
245
246 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s!", argv[1]);
247 if (!rep) {
248 RedisModule_ReplyWithError(ctx, "NULL reply returned");
249 } else {
250 RedisModule_ReplyWithCallReply(ctx, rep);
251 RedisModule_FreeCallReply(rep);
252 }
253 return REDISMODULE_OK;
254}
255
256/* Call INCR and propagate using RM_Replicate. */
257static int cmdIncrCase2(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
258 if (argc != 2)
259 return RedisModule_WrongArity(ctx);
260
261 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s", argv[1]);
262 if (!rep) {
263 RedisModule_ReplyWithError(ctx, "NULL reply returned");
264 } else {
265 RedisModule_ReplyWithCallReply(ctx, rep);
266 RedisModule_FreeCallReply(rep);
267 }
268 RedisModule_Replicate(ctx, "INCR", "s", argv[1]);
269 return REDISMODULE_OK;
270}
271
272/* Call INCR and propagate using RM_ReplicateVerbatim. */
273static int cmdIncrCase3(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
274 if (argc != 2)
275 return RedisModule_WrongArity(ctx);
276
277 RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s", argv[1]);
278 if (!rep) {
279 RedisModule_ReplyWithError(ctx, "NULL reply returned");
280 } else {
281 RedisModule_ReplyWithCallReply(ctx, rep);
282 RedisModule_FreeCallReply(rep);
283 }
284 RedisModule_ReplicateVerbatim(ctx);
285 return REDISMODULE_OK;
286}
287
288static int cmdIncrDels(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
289 REDISMODULE_NOT_USED(argv);
290 REDISMODULE_NOT_USED(argc);
291 dels++;
292 return RedisModule_ReplyWithSimpleString(ctx, "OK");
293}
294
295static int cmdGetDels(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
296 REDISMODULE_NOT_USED(argv);
297 REDISMODULE_NOT_USED(argc);
298 return RedisModule_ReplyWithLongLong(ctx, dels);
299}
300
301static RedisModuleNotificationFunc get_callback_for_event(int event_mask) {
302 switch(event_mask) {
303 case REDISMODULE_NOTIFY_LOADED:
304 return KeySpace_NotificationLoaded;
305 case REDISMODULE_NOTIFY_GENERIC:
306 return KeySpace_NotificationGeneric;
307 case REDISMODULE_NOTIFY_EXPIRED:
308 return KeySpace_NotificationExpired;
309 case REDISMODULE_NOTIFY_MODULE:
310 return KeySpace_NotificationModule;
311 case REDISMODULE_NOTIFY_KEY_MISS:
312 return KeySpace_NotificationModuleKeyMiss;
313 case REDISMODULE_NOTIFY_STRING:
314 // We have two callbacks for STRING events in your OnLoad,
315 // For simplicity, pick the first:
316 return KeySpace_NotificationModuleString;
317 default:
318 return NULL;
319 }
320}
321
322int GetCallbackCountCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
323 REDISMODULE_NOT_USED(argv);
324 REDISMODULE_NOT_USED(argc);
325 RedisModule_ReplyWithLongLong(ctx, callback_call_count);
326 return REDISMODULE_OK;
327}
328
329static int CmdUnsub(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
330 if (argc != 2) {
331 return RedisModule_WrongArity(ctx);
332 }
333
334 long long event_mask;
335 if (RedisModule_StringToLongLong(argv[1], &event_mask) != REDISMODULE_OK) {
336 return RedisModule_ReplyWithError(ctx, "ERR invalid event mask");
337 }
338
339 RedisModuleNotificationFunc cb = get_callback_for_event((int)event_mask);
340 if (cb == NULL) {
341 return RedisModule_ReplyWithError(ctx, "ERR unknown event mask");
342 }
343
344 if (RedisModule_UnsubscribeFromKeyspaceEvents(ctx, (int)event_mask, cb) != REDISMODULE_OK) {
345 return RedisModule_ReplyWithError(ctx, "ERR unsubscribe failed");
346 }
347
348 return RedisModule_ReplyWithSimpleString(ctx, "OK");
349}
350/* This function must be present on each Redis module. It is used in order to
351 * register the commands into the Redis server. */
352int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
353 if (RedisModule_Init(ctx,"testkeyspace",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR){
354 return REDISMODULE_ERR;
355 }
356
357 loaded_event_log = RedisModule_CreateDict(ctx);
358 module_event_log = RedisModule_CreateDict(ctx);
359
360 int keySpaceAll = RedisModule_GetKeyspaceNotificationFlagsAll();
361
362 if (!(keySpaceAll & REDISMODULE_NOTIFY_LOADED)) {
363 // REDISMODULE_NOTIFY_LOADED event are not supported we can not start
364 return REDISMODULE_ERR;
365 }
366
367 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_LOADED, KeySpace_NotificationLoaded) != REDISMODULE_OK){
368 return REDISMODULE_ERR;
369 }
370
371 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_GENERIC, KeySpace_NotificationGeneric) != REDISMODULE_OK){
372 return REDISMODULE_ERR;
373 }
374
375 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_EXPIRED, KeySpace_NotificationExpired) != REDISMODULE_OK){
376 return REDISMODULE_ERR;
377 }
378
379 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_MODULE, KeySpace_NotificationModule) != REDISMODULE_OK){
380 return REDISMODULE_ERR;
381 }
382
383 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_KEY_MISS, KeySpace_NotificationModuleKeyMiss) != REDISMODULE_OK){
384 return REDISMODULE_ERR;
385 }
386
387 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_STRING, KeySpace_NotificationModuleString) != REDISMODULE_OK){
388 return REDISMODULE_ERR;
389 }
390
391 if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_STRING, KeySpace_NotificationModuleStringPostNotificationJob) != REDISMODULE_OK){
392 return REDISMODULE_ERR;
393 }
394
395 if (RedisModule_CreateCommand(ctx,"keyspace.notify", cmdNotify,"",0,0,0) == REDISMODULE_ERR){
396 return REDISMODULE_ERR;
397 }
398
399 if (RedisModule_CreateCommand(ctx,"keyspace.is_module_key_notified", cmdIsModuleKeyNotified,"",0,0,0) == REDISMODULE_ERR){
400 return REDISMODULE_ERR;
401 }
402
403 if (RedisModule_CreateCommand(ctx,"keyspace.is_key_loaded", cmdIsKeyLoaded,"",0,0,0) == REDISMODULE_ERR){
404 return REDISMODULE_ERR;
405 }
406
407 if (RedisModule_CreateCommand(ctx, "keyspace.del_key_copy", cmdDelKeyCopy,
408 "write", 0, 0, 0) == REDISMODULE_ERR){
409 return REDISMODULE_ERR;
410 }
411
412 if (RedisModule_CreateCommand(ctx, "keyspace.incr_case1", cmdIncrCase1,
413 "write", 0, 0, 0) == REDISMODULE_ERR){
414 return REDISMODULE_ERR;
415 }
416
417 if (RedisModule_CreateCommand(ctx, "keyspace.incr_case2", cmdIncrCase2,
418 "write", 0, 0, 0) == REDISMODULE_ERR){
419 return REDISMODULE_ERR;
420 }
421
422 if (RedisModule_CreateCommand(ctx, "keyspace.incr_case3", cmdIncrCase3,
423 "write", 0, 0, 0) == REDISMODULE_ERR){
424 return REDISMODULE_ERR;
425 }
426
427 if (RedisModule_CreateCommand(ctx, "keyspace.incr_dels", cmdIncrDels,
428 "write", 0, 0, 0) == REDISMODULE_ERR){
429 return REDISMODULE_ERR;
430 }
431
432 if (RedisModule_CreateCommand(ctx, "keyspace.get_dels", cmdGetDels,
433 "readonly", 0, 0, 0) == REDISMODULE_ERR){
434 return REDISMODULE_ERR;
435 }
436
437 if (RedisModule_CreateCommand(ctx, "keyspace.unsubscribe", CmdUnsub, "write", 0, 0, 0) == REDISMODULE_ERR){
438 return REDISMODULE_ERR;
439 }
440
441 if (RedisModule_CreateCommand(ctx, "keyspace.callback_count", GetCallbackCountCommand, "", 0, 0, 0)== REDISMODULE_ERR){
442 return REDISMODULE_ERR;
443 }
444
445 if (argc == 1) {
446 const char *ptr = RedisModule_StringPtrLen(argv[0], NULL);
447 if (!strcasecmp(ptr, "noload")) {
448 /* This is a hint that we return ERR at the last moment of OnLoad. */
449 RedisModule_FreeDict(ctx, loaded_event_log);
450 RedisModule_FreeDict(ctx, module_event_log);
451 return REDISMODULE_ERR;
452 }
453 }
454
455 return REDISMODULE_OK;
456}
457
458int RedisModule_OnUnload(RedisModuleCtx *ctx) {
459 RedisModuleDictIter *iter = RedisModule_DictIteratorStartC(loaded_event_log, "^", NULL, 0);
460 char* key;
461 size_t keyLen;
462 RedisModuleString* val;
463 while((key = RedisModule_DictNextC(iter, &keyLen, (void**)&val))){
464 RedisModule_FreeString(ctx, val);
465 }
466 RedisModule_FreeDict(ctx, loaded_event_log);
467 RedisModule_DictIteratorStop(iter);
468 loaded_event_log = NULL;
469
470 iter = RedisModule_DictIteratorStartC(module_event_log, "^", NULL, 0);
471 while((key = RedisModule_DictNextC(iter, &keyLen, (void**)&val))){
472 RedisModule_FreeString(ctx, val);
473 }
474 RedisModule_FreeDict(ctx, module_event_log);
475 RedisModule_DictIteratorStop(iter);
476 module_event_log = NULL;
477
478 return REDISMODULE_OK;
479}