1
0

keyspace_events.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /* This module is used to test the server keyspace events API.
  2. *
  3. * -----------------------------------------------------------------------------
  4. *
  5. * Copyright (c) 2020, Meir Shpilraien <meir at redislabs dot com>
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * * Neither the name of Redis nor the names of its contributors may be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. * POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #define REDISMODULE_EXPERIMENTAL_API
  33. #include "redismodule.h"
  34. #include <stdio.h>
  35. #include <string.h>
  36. /** stores all the keys on which we got 'loaded' keyspace notification **/
  37. RedisModuleDict *loaded_event_log = NULL;
  38. /** stores all the keys on which we got 'module' keyspace notification **/
  39. RedisModuleDict *module_event_log = NULL;
  40. static int KeySpace_NotificationLoaded(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key){
  41. REDISMODULE_NOT_USED(ctx);
  42. REDISMODULE_NOT_USED(type);
  43. if(strcmp(event, "loaded") == 0){
  44. const char* keyName = RedisModule_StringPtrLen(key, NULL);
  45. int nokey;
  46. RedisModule_DictGetC(loaded_event_log, (void*)keyName, strlen(keyName), &nokey);
  47. if(nokey){
  48. RedisModule_DictSetC(loaded_event_log, (void*)keyName, strlen(keyName), RedisModule_HoldString(ctx, key));
  49. }
  50. }
  51. return REDISMODULE_OK;
  52. }
  53. static int KeySpace_NotificationGeneric(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
  54. REDISMODULE_NOT_USED(type);
  55. if (strcmp(event, "del") == 0) {
  56. RedisModuleString *copykey = RedisModule_CreateStringPrintf(ctx, "%s_copy", RedisModule_StringPtrLen(key, NULL));
  57. RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", copykey);
  58. RedisModule_FreeString(ctx, copykey);
  59. RedisModule_FreeCallReply(rep);
  60. int ctx_flags = RedisModule_GetContextFlags(ctx);
  61. if (ctx_flags & REDISMODULE_CTX_FLAGS_LUA) {
  62. RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "c", "lua");
  63. RedisModule_FreeCallReply(rep);
  64. }
  65. if (ctx_flags & REDISMODULE_CTX_FLAGS_MULTI) {
  66. RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "c", "multi");
  67. RedisModule_FreeCallReply(rep);
  68. }
  69. }
  70. return REDISMODULE_OK;
  71. }
  72. static int KeySpace_NotificationModule(RedisModuleCtx *ctx, int type, const char *event, RedisModuleString *key) {
  73. REDISMODULE_NOT_USED(ctx);
  74. REDISMODULE_NOT_USED(type);
  75. REDISMODULE_NOT_USED(event);
  76. const char* keyName = RedisModule_StringPtrLen(key, NULL);
  77. int nokey;
  78. RedisModule_DictGetC(module_event_log, (void*)keyName, strlen(keyName), &nokey);
  79. if(nokey){
  80. RedisModule_DictSetC(module_event_log, (void*)keyName, strlen(keyName), RedisModule_HoldString(ctx, key));
  81. }
  82. return REDISMODULE_OK;
  83. }
  84. static int cmdNotify(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
  85. if(argc != 2){
  86. return RedisModule_WrongArity(ctx);
  87. }
  88. RedisModule_NotifyKeyspaceEvent(ctx, REDISMODULE_NOTIFY_MODULE, "notify", argv[1]);
  89. RedisModule_ReplyWithNull(ctx);
  90. return REDISMODULE_OK;
  91. }
  92. static int cmdIsModuleKeyNotified(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
  93. if(argc != 2){
  94. return RedisModule_WrongArity(ctx);
  95. }
  96. const char* key = RedisModule_StringPtrLen(argv[1], NULL);
  97. int nokey;
  98. RedisModuleString* keyStr = RedisModule_DictGetC(module_event_log, (void*)key, strlen(key), &nokey);
  99. RedisModule_ReplyWithArray(ctx, 2);
  100. RedisModule_ReplyWithLongLong(ctx, !nokey);
  101. if(nokey){
  102. RedisModule_ReplyWithNull(ctx);
  103. }else{
  104. RedisModule_ReplyWithString(ctx, keyStr);
  105. }
  106. return REDISMODULE_OK;
  107. }
  108. static int cmdIsKeyLoaded(RedisModuleCtx *ctx, RedisModuleString **argv, int argc){
  109. if(argc != 2){
  110. return RedisModule_WrongArity(ctx);
  111. }
  112. const char* key = RedisModule_StringPtrLen(argv[1], NULL);
  113. int nokey;
  114. RedisModuleString* keyStr = RedisModule_DictGetC(loaded_event_log, (void*)key, strlen(key), &nokey);
  115. RedisModule_ReplyWithArray(ctx, 2);
  116. RedisModule_ReplyWithLongLong(ctx, !nokey);
  117. if(nokey){
  118. RedisModule_ReplyWithNull(ctx);
  119. }else{
  120. RedisModule_ReplyWithString(ctx, keyStr);
  121. }
  122. return REDISMODULE_OK;
  123. }
  124. static int cmdDelKeyCopy(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  125. if (argc != 2)
  126. return RedisModule_WrongArity(ctx);
  127. RedisModuleCallReply* rep = RedisModule_Call(ctx, "DEL", "s!", argv[1]);
  128. if (!rep) {
  129. RedisModule_ReplyWithError(ctx, "NULL reply returned");
  130. } else {
  131. RedisModule_ReplyWithCallReply(ctx, rep);
  132. RedisModule_FreeCallReply(rep);
  133. }
  134. return REDISMODULE_OK;
  135. }
  136. /* Call INCR and propagate using RM_Call with `!`. */
  137. static int cmdIncrCase1(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  138. if (argc != 2)
  139. return RedisModule_WrongArity(ctx);
  140. RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s!", argv[1]);
  141. if (!rep) {
  142. RedisModule_ReplyWithError(ctx, "NULL reply returned");
  143. } else {
  144. RedisModule_ReplyWithCallReply(ctx, rep);
  145. RedisModule_FreeCallReply(rep);
  146. }
  147. return REDISMODULE_OK;
  148. }
  149. /* Call INCR and propagate using RM_Replicate. */
  150. static int cmdIncrCase2(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  151. if (argc != 2)
  152. return RedisModule_WrongArity(ctx);
  153. RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s", argv[1]);
  154. if (!rep) {
  155. RedisModule_ReplyWithError(ctx, "NULL reply returned");
  156. } else {
  157. RedisModule_ReplyWithCallReply(ctx, rep);
  158. RedisModule_FreeCallReply(rep);
  159. }
  160. RedisModule_Replicate(ctx, "INCR", "s", argv[1]);
  161. return REDISMODULE_OK;
  162. }
  163. /* Call INCR and propagate using RM_ReplicateVerbatim. */
  164. static int cmdIncrCase3(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  165. if (argc != 2)
  166. return RedisModule_WrongArity(ctx);
  167. RedisModuleCallReply* rep = RedisModule_Call(ctx, "INCR", "s", argv[1]);
  168. if (!rep) {
  169. RedisModule_ReplyWithError(ctx, "NULL reply returned");
  170. } else {
  171. RedisModule_ReplyWithCallReply(ctx, rep);
  172. RedisModule_FreeCallReply(rep);
  173. }
  174. RedisModule_ReplicateVerbatim(ctx);
  175. return REDISMODULE_OK;
  176. }
  177. /* This function must be present on each Redis module. It is used in order to
  178. * register the commands into the Redis server. */
  179. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  180. REDISMODULE_NOT_USED(argv);
  181. REDISMODULE_NOT_USED(argc);
  182. if (RedisModule_Init(ctx,"testkeyspace",1,REDISMODULE_APIVER_1) == REDISMODULE_ERR){
  183. return REDISMODULE_ERR;
  184. }
  185. loaded_event_log = RedisModule_CreateDict(ctx);
  186. module_event_log = RedisModule_CreateDict(ctx);
  187. int keySpaceAll = RedisModule_GetKeyspaceNotificationFlagsAll();
  188. if (!(keySpaceAll & REDISMODULE_NOTIFY_LOADED)) {
  189. // REDISMODULE_NOTIFY_LOADED event are not supported we can not start
  190. return REDISMODULE_ERR;
  191. }
  192. if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_LOADED, KeySpace_NotificationLoaded) != REDISMODULE_OK){
  193. return REDISMODULE_ERR;
  194. }
  195. if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_GENERIC, KeySpace_NotificationGeneric) != REDISMODULE_OK){
  196. return REDISMODULE_ERR;
  197. }
  198. if(RedisModule_SubscribeToKeyspaceEvents(ctx, REDISMODULE_NOTIFY_MODULE, KeySpace_NotificationModule) != REDISMODULE_OK){
  199. return REDISMODULE_ERR;
  200. }
  201. if (RedisModule_CreateCommand(ctx,"keyspace.notify", cmdNotify,"",0,0,0) == REDISMODULE_ERR){
  202. return REDISMODULE_ERR;
  203. }
  204. if (RedisModule_CreateCommand(ctx,"keyspace.is_module_key_notified", cmdIsModuleKeyNotified,"",0,0,0) == REDISMODULE_ERR){
  205. return REDISMODULE_ERR;
  206. }
  207. if (RedisModule_CreateCommand(ctx,"keyspace.is_key_loaded", cmdIsKeyLoaded,"",0,0,0) == REDISMODULE_ERR){
  208. return REDISMODULE_ERR;
  209. }
  210. if (RedisModule_CreateCommand(ctx,"keyspace.del_key_copy", cmdDelKeyCopy,"",0,0,0) == REDISMODULE_ERR){
  211. return REDISMODULE_ERR;
  212. }
  213. if (RedisModule_CreateCommand(ctx,"keyspace.incr_case1", cmdIncrCase1,"",0,0,0) == REDISMODULE_ERR){
  214. return REDISMODULE_ERR;
  215. }
  216. if (RedisModule_CreateCommand(ctx,"keyspace.incr_case2", cmdIncrCase2,"",0,0,0) == REDISMODULE_ERR){
  217. return REDISMODULE_ERR;
  218. }
  219. if (RedisModule_CreateCommand(ctx,"keyspace.incr_case3", cmdIncrCase3,"",0,0,0) == REDISMODULE_ERR){
  220. return REDISMODULE_ERR;
  221. }
  222. return REDISMODULE_OK;
  223. }
  224. int RedisModule_OnUnload(RedisModuleCtx *ctx) {
  225. RedisModuleDictIter *iter = RedisModule_DictIteratorStartC(loaded_event_log, "^", NULL, 0);
  226. char* key;
  227. size_t keyLen;
  228. RedisModuleString* val;
  229. while((key = RedisModule_DictNextC(iter, &keyLen, (void**)&val))){
  230. RedisModule_FreeString(ctx, val);
  231. }
  232. RedisModule_FreeDict(ctx, loaded_event_log);
  233. RedisModule_DictIteratorStop(iter);
  234. loaded_event_log = NULL;
  235. iter = RedisModule_DictIteratorStartC(module_event_log, "^", NULL, 0);
  236. while((key = RedisModule_DictNextC(iter, &keyLen, (void**)&val))){
  237. RedisModule_FreeString(ctx, val);
  238. }
  239. RedisModule_FreeDict(ctx, module_event_log);
  240. RedisModule_DictIteratorStop(iter);
  241. module_event_log = NULL;
  242. return REDISMODULE_OK;
  243. }