2
0

propagate.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 < 10)
  50. RedisModule_CreateTimer(ctx,100,timerHandler,NULL);
  51. else
  52. times = 0;
  53. }
  54. /* The thread entry point. */
  55. void *threadMain(void *arg) {
  56. REDISMODULE_NOT_USED(arg);
  57. RedisModuleCtx *ctx = RedisModule_GetThreadSafeContext(NULL);
  58. RedisModule_SelectDb(ctx,9); /* Tests ran in database number 9. */
  59. for (int i = 0; i < 10; i++) {
  60. RedisModule_ThreadSafeContextLock(ctx);
  61. RedisModule_Replicate(ctx,"INCR","c","a-from-thread");
  62. RedisModule_Replicate(ctx,"INCR","c","b-from-thread");
  63. RedisModule_ThreadSafeContextUnlock(ctx);
  64. }
  65. RedisModule_FreeThreadSafeContext(ctx);
  66. return NULL;
  67. }
  68. int propagateTestCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  69. {
  70. REDISMODULE_NOT_USED(argv);
  71. REDISMODULE_NOT_USED(argc);
  72. RedisModuleTimerID timer_id =
  73. RedisModule_CreateTimer(ctx,100,timerHandler,NULL);
  74. REDISMODULE_NOT_USED(timer_id);
  75. pthread_t tid;
  76. if (pthread_create(&tid,NULL,threadMain,NULL) != 0)
  77. return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
  78. REDISMODULE_NOT_USED(tid);
  79. RedisModule_ReplyWithSimpleString(ctx,"OK");
  80. return REDISMODULE_OK;
  81. }
  82. int propagateTest2Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  83. {
  84. REDISMODULE_NOT_USED(argv);
  85. REDISMODULE_NOT_USED(argc);
  86. /* Replicate two commands to test MULTI/EXEC wrapping. */
  87. RedisModule_Replicate(ctx,"INCR","c","counter-1");
  88. RedisModule_Replicate(ctx,"INCR","c","counter-2");
  89. RedisModule_ReplyWithSimpleString(ctx,"OK");
  90. return REDISMODULE_OK;
  91. }
  92. int propagateTest3Command(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  93. {
  94. REDISMODULE_NOT_USED(argv);
  95. REDISMODULE_NOT_USED(argc);
  96. RedisModuleCallReply *reply;
  97. /* This test mixes multiple propagation systems. */
  98. reply = RedisModule_Call(ctx, "INCR", "c!", "using-call");
  99. RedisModule_FreeCallReply(reply);
  100. RedisModule_Replicate(ctx,"INCR","c","counter-1");
  101. RedisModule_Replicate(ctx,"INCR","c","counter-2");
  102. reply = RedisModule_Call(ctx, "INCR", "c!", "after-call");
  103. RedisModule_FreeCallReply(reply);
  104. RedisModule_ReplyWithSimpleString(ctx,"OK");
  105. return REDISMODULE_OK;
  106. }
  107. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  108. REDISMODULE_NOT_USED(argv);
  109. REDISMODULE_NOT_USED(argc);
  110. if (RedisModule_Init(ctx,"propagate-test",1,REDISMODULE_APIVER_1)
  111. == REDISMODULE_ERR) return REDISMODULE_ERR;
  112. if (RedisModule_CreateCommand(ctx,"propagate-test",
  113. propagateTestCommand,
  114. "",1,1,1) == REDISMODULE_ERR)
  115. return REDISMODULE_ERR;
  116. if (RedisModule_CreateCommand(ctx,"propagate-test-2",
  117. propagateTest2Command,
  118. "",1,1,1) == REDISMODULE_ERR)
  119. return REDISMODULE_ERR;
  120. if (RedisModule_CreateCommand(ctx,"propagate-test-3",
  121. propagateTest3Command,
  122. "",1,1,1) == REDISMODULE_ERR)
  123. return REDISMODULE_ERR;
  124. return REDISMODULE_OK;
  125. }