2
0

hooks.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* This module is used to test the server events hooks API.
  2. *
  3. * -----------------------------------------------------------------------------
  4. *
  5. * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail 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. #include "redismodule.h"
  33. #include <stdio.h>
  34. #include <string.h>
  35. /* We need to store events to be able to test and see what we got, and we can't
  36. * store them in the key-space since that would mess up rdb loading (duplicates)
  37. * and be lost of flushdb. */
  38. RedisModuleDict *event_log = NULL;
  39. typedef struct EventElement {
  40. long count;
  41. RedisModuleString *last_val_string;
  42. long last_val_int;
  43. } EventElement;
  44. void LogStringEvent(RedisModuleCtx *ctx, const char* keyname, const char* data) {
  45. EventElement *event = RedisModule_DictGetC(event_log, (void*)keyname, strlen(keyname), NULL);
  46. if (!event) {
  47. event = RedisModule_Alloc(sizeof(EventElement));
  48. memset(event, 0, sizeof(EventElement));
  49. RedisModule_DictSetC(event_log, (void*)keyname, strlen(keyname), event);
  50. }
  51. if (event->last_val_string) RedisModule_FreeString(ctx, event->last_val_string);
  52. event->last_val_string = RedisModule_CreateString(ctx, data, strlen(data));
  53. event->count++;
  54. }
  55. void LogNumericEvent(RedisModuleCtx *ctx, const char* keyname, long data) {
  56. REDISMODULE_NOT_USED(ctx);
  57. EventElement *event = RedisModule_DictGetC(event_log, (void*)keyname, strlen(keyname), NULL);
  58. if (!event) {
  59. event = RedisModule_Alloc(sizeof(EventElement));
  60. memset(event, 0, sizeof(EventElement));
  61. RedisModule_DictSetC(event_log, (void*)keyname, strlen(keyname), event);
  62. }
  63. event->last_val_int = data;
  64. event->count++;
  65. }
  66. void FreeEvent(RedisModuleCtx *ctx, EventElement *event) {
  67. if (event->last_val_string)
  68. RedisModule_FreeString(ctx, event->last_val_string);
  69. RedisModule_Free(event);
  70. }
  71. int cmdEventCount(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  72. {
  73. if (argc != 2){
  74. RedisModule_WrongArity(ctx);
  75. return REDISMODULE_OK;
  76. }
  77. EventElement *event = RedisModule_DictGet(event_log, argv[1], NULL);
  78. RedisModule_ReplyWithLongLong(ctx, event? event->count: 0);
  79. return REDISMODULE_OK;
  80. }
  81. int cmdEventLast(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  82. {
  83. if (argc != 2){
  84. RedisModule_WrongArity(ctx);
  85. return REDISMODULE_OK;
  86. }
  87. EventElement *event = RedisModule_DictGet(event_log, argv[1], NULL);
  88. if (event && event->last_val_string)
  89. RedisModule_ReplyWithString(ctx, event->last_val_string);
  90. else if (event)
  91. RedisModule_ReplyWithLongLong(ctx, event->last_val_int);
  92. else
  93. RedisModule_ReplyWithNull(ctx);
  94. return REDISMODULE_OK;
  95. }
  96. void clearEvents(RedisModuleCtx *ctx)
  97. {
  98. RedisModuleString *key;
  99. EventElement *event;
  100. RedisModuleDictIter *iter = RedisModule_DictIteratorStart(event_log, "^", NULL);
  101. while((key = RedisModule_DictNext(ctx, iter, (void**)&event)) != NULL) {
  102. event->count = 0;
  103. event->last_val_int = 0;
  104. if (event->last_val_string) RedisModule_FreeString(ctx, event->last_val_string);
  105. event->last_val_string = NULL;
  106. RedisModule_DictDel(event_log, key, NULL);
  107. RedisModule_Free(event);
  108. }
  109. RedisModule_DictIteratorStop(iter);
  110. }
  111. int cmdEventsClear(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  112. {
  113. REDISMODULE_NOT_USED(argc);
  114. REDISMODULE_NOT_USED(argv);
  115. clearEvents(ctx);
  116. return REDISMODULE_OK;
  117. }
  118. /* Client state change callback. */
  119. void clientChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  120. {
  121. REDISMODULE_NOT_USED(e);
  122. RedisModuleClientInfo *ci = data;
  123. char *keyname = (sub == REDISMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED) ?
  124. "client-connected" : "client-disconnected";
  125. LogNumericEvent(ctx, keyname, ci->id);
  126. }
  127. void flushdbCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  128. {
  129. REDISMODULE_NOT_USED(e);
  130. RedisModuleFlushInfo *fi = data;
  131. char *keyname = (sub == REDISMODULE_SUBEVENT_FLUSHDB_START) ?
  132. "flush-start" : "flush-end";
  133. LogNumericEvent(ctx, keyname, fi->dbnum);
  134. }
  135. void roleChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  136. {
  137. REDISMODULE_NOT_USED(e);
  138. REDISMODULE_NOT_USED(data);
  139. RedisModuleReplicationInfo *ri = data;
  140. char *keyname = (sub == REDISMODULE_EVENT_REPLROLECHANGED_NOW_MASTER) ?
  141. "role-master" : "role-replica";
  142. LogStringEvent(ctx, keyname, ri->masterhost);
  143. }
  144. void replicationChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  145. {
  146. REDISMODULE_NOT_USED(e);
  147. REDISMODULE_NOT_USED(data);
  148. char *keyname = (sub == REDISMODULE_SUBEVENT_REPLICA_CHANGE_ONLINE) ?
  149. "replica-online" : "replica-offline";
  150. LogNumericEvent(ctx, keyname, 0);
  151. }
  152. void rasterLinkChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  153. {
  154. REDISMODULE_NOT_USED(e);
  155. REDISMODULE_NOT_USED(data);
  156. char *keyname = (sub == REDISMODULE_SUBEVENT_MASTER_LINK_UP) ?
  157. "masterlink-up" : "masterlink-down";
  158. LogNumericEvent(ctx, keyname, 0);
  159. }
  160. void persistenceCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  161. {
  162. REDISMODULE_NOT_USED(e);
  163. REDISMODULE_NOT_USED(data);
  164. char *keyname = NULL;
  165. switch (sub) {
  166. case REDISMODULE_SUBEVENT_PERSISTENCE_RDB_START: keyname = "persistence-rdb-start"; break;
  167. case REDISMODULE_SUBEVENT_PERSISTENCE_AOF_START: keyname = "persistence-aof-start"; break;
  168. case REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START: keyname = "persistence-syncrdb-start"; break;
  169. case REDISMODULE_SUBEVENT_PERSISTENCE_ENDED: keyname = "persistence-end"; break;
  170. case REDISMODULE_SUBEVENT_PERSISTENCE_FAILED: keyname = "persistence-failed"; break;
  171. }
  172. /* modifying the keyspace from the fork child is not an option, using log instead */
  173. RedisModule_Log(ctx, "warning", "module-event-%s", keyname);
  174. if (sub == REDISMODULE_SUBEVENT_PERSISTENCE_SYNC_RDB_START)
  175. LogNumericEvent(ctx, keyname, 0);
  176. }
  177. void loadingCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  178. {
  179. REDISMODULE_NOT_USED(e);
  180. REDISMODULE_NOT_USED(data);
  181. char *keyname = NULL;
  182. switch (sub) {
  183. case REDISMODULE_SUBEVENT_LOADING_RDB_START: keyname = "loading-rdb-start"; break;
  184. case REDISMODULE_SUBEVENT_LOADING_AOF_START: keyname = "loading-aof-start"; break;
  185. case REDISMODULE_SUBEVENT_LOADING_REPL_START: keyname = "loading-repl-start"; break;
  186. case REDISMODULE_SUBEVENT_LOADING_ENDED: keyname = "loading-end"; break;
  187. case REDISMODULE_SUBEVENT_LOADING_FAILED: keyname = "loading-failed"; break;
  188. }
  189. LogNumericEvent(ctx, keyname, 0);
  190. }
  191. void loadingProgressCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  192. {
  193. REDISMODULE_NOT_USED(e);
  194. RedisModuleLoadingProgress *ei = data;
  195. char *keyname = (sub == REDISMODULE_SUBEVENT_LOADING_PROGRESS_RDB) ?
  196. "loading-progress-rdb" : "loading-progress-aof";
  197. LogNumericEvent(ctx, keyname, ei->progress);
  198. }
  199. void shutdownCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  200. {
  201. REDISMODULE_NOT_USED(e);
  202. REDISMODULE_NOT_USED(data);
  203. REDISMODULE_NOT_USED(sub);
  204. RedisModule_Log(ctx, "warning", "module-event-%s", "shutdown");
  205. }
  206. void cronLoopCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  207. {
  208. REDISMODULE_NOT_USED(e);
  209. REDISMODULE_NOT_USED(sub);
  210. RedisModuleCronLoop *ei = data;
  211. LogNumericEvent(ctx, "cron-loop", ei->hz);
  212. }
  213. void moduleChangeCallback(RedisModuleCtx *ctx, RedisModuleEvent e, uint64_t sub, void *data)
  214. {
  215. REDISMODULE_NOT_USED(e);
  216. RedisModuleModuleChange *ei = data;
  217. char *keyname = (sub == REDISMODULE_SUBEVENT_MODULE_LOADED) ?
  218. "module-loaded" : "module-unloaded";
  219. LogStringEvent(ctx, keyname, ei->module_name);
  220. }
  221. /* This function must be present on each Redis module. It is used in order to
  222. * register the commands into the Redis server. */
  223. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  224. REDISMODULE_NOT_USED(argv);
  225. REDISMODULE_NOT_USED(argc);
  226. if (RedisModule_Init(ctx,"testhook",1,REDISMODULE_APIVER_1)
  227. == REDISMODULE_ERR) return REDISMODULE_ERR;
  228. /* replication related hooks */
  229. RedisModule_SubscribeToServerEvent(ctx,
  230. RedisModuleEvent_ReplicationRoleChanged, roleChangeCallback);
  231. RedisModule_SubscribeToServerEvent(ctx,
  232. RedisModuleEvent_ReplicaChange, replicationChangeCallback);
  233. RedisModule_SubscribeToServerEvent(ctx,
  234. RedisModuleEvent_MasterLinkChange, rasterLinkChangeCallback);
  235. /* persistence related hooks */
  236. RedisModule_SubscribeToServerEvent(ctx,
  237. RedisModuleEvent_Persistence, persistenceCallback);
  238. RedisModule_SubscribeToServerEvent(ctx,
  239. RedisModuleEvent_Loading, loadingCallback);
  240. RedisModule_SubscribeToServerEvent(ctx,
  241. RedisModuleEvent_LoadingProgress, loadingProgressCallback);
  242. /* other hooks */
  243. RedisModule_SubscribeToServerEvent(ctx,
  244. RedisModuleEvent_ClientChange, clientChangeCallback);
  245. RedisModule_SubscribeToServerEvent(ctx,
  246. RedisModuleEvent_FlushDB, flushdbCallback);
  247. RedisModule_SubscribeToServerEvent(ctx,
  248. RedisModuleEvent_Shutdown, shutdownCallback);
  249. RedisModule_SubscribeToServerEvent(ctx,
  250. RedisModuleEvent_CronLoop, cronLoopCallback);
  251. RedisModule_SubscribeToServerEvent(ctx,
  252. RedisModuleEvent_ModuleChange, moduleChangeCallback);
  253. event_log = RedisModule_CreateDict(ctx);
  254. if (RedisModule_CreateCommand(ctx,"hooks.event_count", cmdEventCount,"",0,0,0) == REDISMODULE_ERR)
  255. return REDISMODULE_ERR;
  256. if (RedisModule_CreateCommand(ctx,"hooks.event_last", cmdEventLast,"",0,0,0) == REDISMODULE_ERR)
  257. return REDISMODULE_ERR;
  258. if (RedisModule_CreateCommand(ctx,"hooks.clear", cmdEventsClear,"",0,0,0) == REDISMODULE_ERR)
  259. return REDISMODULE_ERR;
  260. return REDISMODULE_OK;
  261. }
  262. int RedisModule_OnUnload(RedisModuleCtx *ctx) {
  263. clearEvents(ctx);
  264. RedisModule_FreeDict(ctx, event_log);
  265. event_log = NULL;
  266. return REDISMODULE_OK;
  267. }