propagate.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* This module is used to test the propagation (replication + AOF) of
  2. * commands, via the RedisModule_Replicate() interface, in asynchronous
  3. * contexts, such as callbacks not implementing commands, and thread safe
  4. * contexts.
  5. *
  6. * We create a timer callback and a threads using a thread safe context.
  7. * Using both we try to propagate counters increments, and later we check
  8. * if the replica contains the changes as expected.
  9. *
  10. * -----------------------------------------------------------------------------
  11. *
  12. * Copyright (c) 2019, Salvatore Sanfilippo <antirez at gmail dot com>
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * * Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * * Neither the name of Redis nor the names of its contributors may be used
  24. * to endorse or promote products derived from this software without
  25. * specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  30. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  34. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  35. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  36. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  37. * POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #define REDISMODULE_EXPERIMENTAL_API
  40. #include "redismodule.h"
  41. #include <pthread.h>
  42. /* Timer callback. */
  43. void timerHandler(RedisModuleCtx *ctx, void *data) {
  44. REDISMODULE_NOT_USED(ctx);
  45. REDISMODULE_NOT_USED(data);
  46. static int times = 0;
  47. RedisModule_Replicate(ctx,"INCR","c","timer");
  48. times++;
  49. if (times < 3)
  50. RedisModule_CreateTimer(ctx,100,timerHandler,NULL);
  51. else
  52. times = 0;
  53. }
  54. int propagateTestTimerCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  55. {
  56. REDISMODULE_NOT_USED(argv);
  57. REDISMODULE_NOT_USED(argc);
  58. RedisModuleTimerID timer_id =
  59. RedisModule_CreateTimer(ctx,100,timerHandler,NULL);
  60. REDISMODULE_NOT_USED(timer_id);
  61. RedisModule_ReplyWithSimpleString(ctx,"OK");
  62. return REDISMODULE_OK;
  63. }
  64. /* Timer callback. */
  65. void timerNestedHandler(RedisModuleCtx *ctx, void *data) {
  66. int repl = (long long)data;
  67. /* The goal is the trigger a module command that calls RM_Replicate
  68. * in order to test MULTI/EXEC structure */
  69. RedisModule_Replicate(ctx,"INCRBY","cc","timer-nested-start","1");
  70. RedisModuleCallReply *reply = RedisModule_Call(ctx,"propagate-test.nested", repl? "!" : "");
  71. RedisModule_FreeCallReply(reply);
  72. RedisModule_Replicate(ctx,"INCRBY","cc","timer-nested-end","1");
  73. }
  74. int propagateTestTimerNestedCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  75. {
  76. REDISMODULE_NOT_USED(argv);
  77. REDISMODULE_NOT_USED(argc);
  78. RedisModuleTimerID timer_id =
  79. RedisModule_CreateTimer(ctx,100,timerNestedHandler,(void*)0);
  80. REDISMODULE_NOT_USED(timer_id);
  81. RedisModule_ReplyWithSimpleString(ctx,"OK");
  82. return REDISMODULE_OK;
  83. }
  84. int propagateTestTimerNestedReplCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  85. {
  86. REDISMODULE_NOT_USED(argv);
  87. REDISMODULE_NOT_USED(argc);
  88. RedisModuleTimerID timer_id =
  89. RedisModule_CreateTimer(ctx,100,timerNestedHandler,(void*)1);
  90. REDISMODULE_NOT_USED(timer_id);
  91. RedisModule_ReplyWithSimpleString(ctx,"OK");
  92. return REDISMODULE_OK;
  93. }
  94. /* The thread entry point. */
  95. void *threadMain(void *arg) {
  96. REDISMODULE_NOT_USED(arg);
  97. RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
  98. RedisModule_SelectDb(ctx,9); /* Tests ran in database number 9. */
  99. for (int i = 0; i < 3; i++) {
  100. RedisModule_ThreadSafeContextLock(ctx);
  101. RedisModule_Replicate(ctx,"INCR","c","a-from-thread");
  102. RedisModule_Replicate(ctx,"INCR","c","b-from-thread");
  103. RedisModule_ThreadSafeContextUnlock(ctx);
  104. }
  105. RedisModule_FreeThreadSafeContext(ctx);
  106. return NULL;
  107. }
  108. int propagateTestThreadCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  109. {
  110. REDISMODULE_NOT_USED(argv);
  111. REDISMODULE_NOT_USED(argc);
  112. pthread_t tid;
  113. if (pthread_create(&tid,NULL,threadMain,NULL) != 0)
  114. return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
  115. REDISMODULE_NOT_USED(tid);
  116. RedisModule_ReplyWithSimpleString(ctx,"OK");
  117. return REDISMODULE_OK;
  118. }
  119. int propagateTestSimpleCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  120. {
  121. REDISMODULE_NOT_USED(argv);
  122. REDISMODULE_NOT_USED(argc);
  123. /* Replicate two commands to test MULTI/EXEC wrapping. */
  124. RedisModule_Replicate(ctx,"INCR","c","counter-1");
  125. RedisModule_Replicate(ctx,"INCR","c","counter-2");
  126. RedisModule_ReplyWithSimpleString(ctx,"OK");
  127. return REDISMODULE_OK;
  128. }
  129. int propagateTestMixedCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  130. {
  131. REDISMODULE_NOT_USED(argv);
  132. REDISMODULE_NOT_USED(argc);
  133. RedisModuleCallReply *reply;
  134. /* This test mixes multiple propagation systems. */
  135. reply = RedisModule_Call(ctx, "INCR", "c!", "using-call");
  136. RedisModule_FreeCallReply(reply);
  137. RedisModule_Replicate(ctx,"INCR","c","counter-1");
  138. RedisModule_Replicate(ctx,"INCR","c","counter-2");
  139. reply = RedisModule_Call(ctx, "INCR", "c!", "after-call");
  140. RedisModule_FreeCallReply(reply);
  141. RedisModule_ReplyWithSimpleString(ctx,"OK");
  142. return REDISMODULE_OK;
  143. }
  144. int propagateTestNestedCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  145. {
  146. REDISMODULE_NOT_USED(argv);
  147. REDISMODULE_NOT_USED(argc);
  148. RedisModuleCallReply *reply;
  149. /* This test mixes multiple propagation systems. */
  150. reply = RedisModule_Call(ctx, "INCR", "c!", "using-call");
  151. RedisModule_FreeCallReply(reply);
  152. reply = RedisModule_Call(ctx,"propagate-test.simple", "!");
  153. RedisModule_FreeCallReply(reply);
  154. RedisModule_Replicate(ctx,"INCR","c","counter-3");
  155. RedisModule_Replicate(ctx,"INCR","c","counter-4");
  156. reply = RedisModule_Call(ctx, "INCR", "c!", "after-call");
  157. RedisModule_FreeCallReply(reply);
  158. RedisModule_ReplyWithSimpleString(ctx,"OK");
  159. return REDISMODULE_OK;
  160. }
  161. int propagateTestIncr(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  162. {
  163. REDISMODULE_NOT_USED(argc);
  164. RedisModuleCallReply *reply;
  165. /* This test propagates the module command, not the INCR it executes. */
  166. reply = RedisModule_Call(ctx, "INCR", "s", argv[1]);
  167. RedisModule_ReplyWithCallReply(ctx,reply);
  168. RedisModule_FreeCallReply(reply);
  169. RedisModule_ReplicateVerbatim(ctx);
  170. return REDISMODULE_OK;
  171. }
  172. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  173. REDISMODULE_NOT_USED(argv);
  174. REDISMODULE_NOT_USED(argc);
  175. if (RedisModule_Init(ctx,"propagate-test",1,REDISMODULE_APIVER_1)
  176. == REDISMODULE_ERR) return REDISMODULE_ERR;
  177. if (RedisModule_CreateCommand(ctx,"propagate-test.timer",
  178. propagateTestTimerCommand,
  179. "",1,1,1) == REDISMODULE_ERR)
  180. return REDISMODULE_ERR;
  181. if (RedisModule_CreateCommand(ctx,"propagate-test.timer-nested",
  182. propagateTestTimerNestedCommand,
  183. "",1,1,1) == REDISMODULE_ERR)
  184. return REDISMODULE_ERR;
  185. if (RedisModule_CreateCommand(ctx,"propagate-test.timer-nested-repl",
  186. propagateTestTimerNestedReplCommand,
  187. "",1,1,1) == REDISMODULE_ERR)
  188. return REDISMODULE_ERR;
  189. if (RedisModule_CreateCommand(ctx,"propagate-test.thread",
  190. propagateTestThreadCommand,
  191. "",1,1,1) == REDISMODULE_ERR)
  192. return REDISMODULE_ERR;
  193. if (RedisModule_CreateCommand(ctx,"propagate-test.simple",
  194. propagateTestSimpleCommand,
  195. "",1,1,1) == REDISMODULE_ERR)
  196. return REDISMODULE_ERR;
  197. if (RedisModule_CreateCommand(ctx,"propagate-test.mixed",
  198. propagateTestMixedCommand,
  199. "",1,1,1) == REDISMODULE_ERR)
  200. return REDISMODULE_ERR;
  201. if (RedisModule_CreateCommand(ctx,"propagate-test.nested",
  202. propagateTestNestedCommand,
  203. "",1,1,1) == REDISMODULE_ERR)
  204. return REDISMODULE_ERR;
  205. if (RedisModule_CreateCommand(ctx,"propagate-test.incr",
  206. propagateTestIncr,
  207. "",1,1,1) == REDISMODULE_ERR)
  208. return REDISMODULE_ERR;
  209. return REDISMODULE_OK;
  210. }