hircluster.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #ifndef __HIRCLUSTER_H
  2. #define __HIRCLUSTER_H
  3. #include "hiredis.h"
  4. #include "async.h"
  5. #define HIREDIS_VIP_MAJOR 0
  6. #define HIREDIS_VIP_MINOR 3
  7. #define HIREDIS_VIP_PATCH 0
  8. #define REDIS_CLUSTER_SLOTS 16384
  9. #define REDIS_ROLE_NULL 0
  10. #define REDIS_ROLE_MASTER 1
  11. #define REDIS_ROLE_SLAVE 2
  12. #define HIRCLUSTER_FLAG_NULL 0x0
  13. /* The flag to decide whether add slave node in
  14. * redisClusterContext->nodes. This is set in the
  15. * least significant bit of the flags field in
  16. * redisClusterContext. (1000000000000) */
  17. #define HIRCLUSTER_FLAG_ADD_SLAVE 0x1000
  18. /* The flag to decide whether add open slot
  19. * for master node. (10000000000000) */
  20. #define HIRCLUSTER_FLAG_ADD_OPENSLOT 0x2000
  21. /* The flag to decide whether get the route
  22. * table by 'cluster slots' command. Default
  23. * is 'cluster nodes' command.*/
  24. #define HIRCLUSTER_FLAG_ROUTE_USE_SLOTS 0x4000
  25. struct dict;
  26. struct hilist;
  27. typedef struct cluster_node
  28. {
  29. sds name;
  30. sds addr;
  31. sds host;
  32. int port;
  33. uint8_t role;
  34. uint8_t myself; /* myself ? */
  35. redisContext *con;
  36. redisAsyncContext *acon;
  37. struct hilist *slots;
  38. struct hilist *slaves;
  39. int failure_count;
  40. void *data; /* Not used by hiredis */
  41. struct hiarray *migrating; /* copen_slot[] */
  42. struct hiarray *importing; /* copen_slot[] */
  43. }cluster_node;
  44. typedef struct cluster_slot
  45. {
  46. uint32_t start;
  47. uint32_t end;
  48. cluster_node *node; /* master that this slot region belong to */
  49. }cluster_slot;
  50. typedef struct copen_slot
  51. {
  52. uint32_t slot_num; /* slot number */
  53. int migrate; /* migrating or importing? */
  54. sds remote_name; /* name for the node that this slot migrating to/importing from */
  55. cluster_node *node; /* master that this slot belong to */
  56. }copen_slot;
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. /* Context for a connection to Redis cluster */
  61. typedef struct redisClusterContext {
  62. int err; /* Error flags, 0 when there is no error */
  63. char errstr[128]; /* String representation of error when applicable */
  64. sds ip;
  65. int port;
  66. int flags;
  67. enum redisConnectionType connection_type;
  68. struct timeval *timeout;
  69. struct hiarray *slots;
  70. struct dict *nodes;
  71. cluster_node *table[REDIS_CLUSTER_SLOTS];
  72. uint64_t route_version;
  73. int max_redirect_count;
  74. int retry_count;
  75. struct hilist *requests;
  76. int need_update_route;
  77. int64_t update_route_time;
  78. } redisClusterContext;
  79. redisClusterContext *redisClusterConnect(const char *addrs, int flags);
  80. redisClusterContext *redisClusterConnectWithTimeout(const char *addrs,
  81. const struct timeval tv, int flags);
  82. redisClusterContext *redisClusterConnectNonBlock(const char *addrs, int flags);
  83. void redisClusterFree(redisClusterContext *cc);
  84. void redisClusterSetMaxRedirect(redisClusterContext *cc, int max_redirect_count);
  85. void *redisClusterFormattedCommand(redisClusterContext *cc, char *cmd, int len);
  86. void *redisClustervCommand(redisClusterContext *cc, const char *format, va_list ap);
  87. void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);
  88. void *redisClusterCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);
  89. redisContext *ctx_get_by_node(struct cluster_node *node, const struct timeval *timeout, int flags);
  90. int redisClusterAppendFormattedCommand(redisClusterContext *cc, char *cmd, int len);
  91. int redisClustervAppendCommand(redisClusterContext *cc, const char *format, va_list ap);
  92. int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);
  93. int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc, const char **argv, const size_t *argvlen);
  94. int redisClusterGetReply(redisClusterContext *cc, void **reply);
  95. void redisClusterReset(redisClusterContext *cc);
  96. int cluster_update_route(redisClusterContext *cc);
  97. int test_cluster_update_route(redisClusterContext *cc);
  98. struct dict *parse_cluster_nodes(redisClusterContext *cc, char *str, int str_len, int flags);
  99. struct dict *parse_cluster_slots(redisClusterContext *cc, redisReply *reply, int flags);
  100. /*############redis cluster async############*/
  101. struct redisClusterAsyncContext;
  102. typedef int (adapterAttachFn)(redisAsyncContext*, void*);
  103. typedef void (redisClusterCallbackFn)(struct redisClusterAsyncContext*, void*, void*);
  104. /* Context for an async connection to Redis */
  105. typedef struct redisClusterAsyncContext {
  106. redisClusterContext *cc;
  107. /* Setup error flags so they can be used directly. */
  108. int err;
  109. char errstr[128]; /* String representation of error when applicable */
  110. /* Not used by hiredis */
  111. void *data;
  112. void *adapter;
  113. adapterAttachFn *attach_fn;
  114. /* Called when either the connection is terminated due to an error or per
  115. * user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
  116. redisDisconnectCallback *onDisconnect;
  117. /* Called when the first write event was received. */
  118. redisConnectCallback *onConnect;
  119. } redisClusterAsyncContext;
  120. redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs, int flags);
  121. int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc, redisConnectCallback *fn);
  122. int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc, redisDisconnectCallback *fn);
  123. int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, char *cmd, int len);
  124. int redisClustervAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, va_list ap);
  125. int redisClusterAsyncCommand(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, const char *format, ...);
  126. int redisClusterAsyncCommandArgv(redisClusterAsyncContext *acc, redisClusterCallbackFn *fn, void *privdata, int argc, const char **argv, const size_t *argvlen);
  127. void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc);
  128. void redisClusterAsyncFree(redisClusterAsyncContext *acc);
  129. redisAsyncContext *actx_get_by_node(redisClusterAsyncContext *acc, cluster_node *node);
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif