2
0

propagate.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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","thread");
  62. RedisModule_ThreadSafeContextUnlock(ctx);
  63. }
  64. RedisModule_FreeThreadSafeContext(ctx);
  65. return NULL;
  66. }
  67. int propagateTestCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc)
  68. {
  69. REDISMODULE_NOT_USED(argv);
  70. REDISMODULE_NOT_USED(argc);
  71. RedisModuleTimerID timer_id =
  72. RedisModule_CreateTimer(ctx,100,timerHandler,NULL);
  73. REDISMODULE_NOT_USED(timer_id);
  74. pthread_t tid;
  75. if (pthread_create(&tid,NULL,threadMain,NULL) != 0)
  76. return RedisModule_ReplyWithError(ctx,"-ERR Can't start thread");
  77. REDISMODULE_NOT_USED(tid);
  78. RedisModule_ReplyWithSimpleString(ctx,"OK");
  79. return REDISMODULE_OK;
  80. }
  81. int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
  82. REDISMODULE_NOT_USED(argv);
  83. REDISMODULE_NOT_USED(argc);
  84. if (RedisModule_Init(ctx,"propagate-test",1,REDISMODULE_APIVER_1)
  85. == REDISMODULE_ERR) return REDISMODULE_ERR;
  86. if (RedisModule_CreateCommand(ctx,"propagate-test",
  87. propagateTestCommand,
  88. "",1,1,1) == REDISMODULE_ERR)
  89. return REDISMODULE_ERR;
  90. return REDISMODULE_OK;
  91. }