-
Notifications
You must be signed in to change notification settings - Fork 43
/
hircluster.h
380 lines (326 loc) · 16.3 KB
/
hircluster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Copyright (c) 2015-2017, Ieshen Zheng <ieshen.zheng at 163 dot com>
* Copyright (c) 2020, Nick <heronr1 at gmail dot com>
* Copyright (c) 2020-2021, Bjorn Svensson <bjorn.a.svensson at est dot tech>
* Copyright (c) 2021, Red Hat
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Redis nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __HIRCLUSTER_H
#define __HIRCLUSTER_H
#include "dict.h"
#include <hiredis/async.h>
#include <hiredis/hiredis.h>
#define UNUSED(x) (void)(x)
#define HIREDIS_CLUSTER_MAJOR 0
#define HIREDIS_CLUSTER_MINOR 14
#define HIREDIS_CLUSTER_PATCH 0
#define HIREDIS_CLUSTER_SONAME 0.14
#define REDIS_CLUSTER_SLOTS 16384
#define REDIS_ROLE_NULL 0
#define REDIS_ROLE_MASTER 1
#define REDIS_ROLE_SLAVE 2
/* Configuration flags */
#define HIRCLUSTER_FLAG_NULL 0x0
/* Flag to enable parsing of slave nodes. Currently not used, but the
information is added to its master node structure. */
#define HIRCLUSTER_FLAG_ADD_SLAVE 0x1000
/* Flag to enable parsing of importing/migrating slots for master nodes.
* Only applicable when 'cluster nodes' command is used for route updates. */
#define HIRCLUSTER_FLAG_ADD_OPENSLOT 0x2000
/* Flag to enable routing table updates using the command 'cluster slots'.
* Default is the 'cluster nodes' command. */
#define HIRCLUSTER_FLAG_ROUTE_USE_SLOTS 0x4000
/* Flag specific to the async API which means that the user requested a
* client shutdown by a disconnect or free. */
#define HIRCLUSTER_FLAG_SHUTDOWN 0x8000
/* Events, for redisClusterSetEventCallback() */
#define HIRCLUSTER_EVENT_SLOTMAP_UPDATED 1
#define HIRCLUSTER_EVENT_READY 2
#define HIRCLUSTER_EVENT_FREE_CONTEXT 3
/* The non-const connect callback API is not available when:
* - using hiredis prior v.1.1.0; or
* - built on Windows since hiredis_cluster.def can't have conditional definitions. */
#if !(HIREDIS_MAJOR >= 1 && HIREDIS_MINOR >= 1) || _WIN32
#define HIRCLUSTER_NO_NONCONST_CONNECT_CB
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct dict;
struct hilist;
struct redisClusterAsyncContext;
typedef int(adapterAttachFn)(redisAsyncContext *, void *);
typedef int(sslInitFn)(redisContext *, void *);
typedef void(redisClusterCallbackFn)(struct redisClusterAsyncContext *, void *,
void *);
typedef struct redisClusterNode {
sds name;
sds addr;
sds host;
uint16_t port;
uint8_t role;
uint8_t pad;
int failure_count; /* consecutive failing attempts in async */
redisContext *con;
redisAsyncContext *acon;
int64_t lastConnectionAttempt; /* Timestamp */
struct hilist *slots;
struct hilist *slaves;
struct hiarray *migrating; /* copen_slot[] */
struct hiarray *importing; /* copen_slot[] */
} redisClusterNode;
typedef struct cluster_slot {
uint32_t start;
uint32_t end;
redisClusterNode *node; /* master that this slot region belong to */
} cluster_slot;
typedef struct copen_slot {
uint32_t slot_num; /* slot number */
int migrate; /* migrating or importing? */
sds remote_name; /* name of node this slot migrating to/importing from */
redisClusterNode *node; /* master that this slot belong to */
} copen_slot;
/* Context for accessing a Redis Cluster */
typedef struct redisClusterContext {
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
/* Configurations */
int flags; /* Configuration flags */
struct timeval *connect_timeout; /* TCP connect timeout */
struct timeval *command_timeout; /* Receive and send timeout */
int max_retry_count; /* Allowed retry attempts */
char *username; /* Authenticate using user */
char *password; /* Authentication password */
struct dict *nodes; /* Known redisClusterNode's */
uint64_t route_version; /* Increased when the node lookup table changes */
redisClusterNode **table; /* redisClusterNode lookup table */
struct hilist *requests; /* Outstanding commands (Pipelining) */
int retry_count; /* Current number of failing attempts */
int need_update_route; /* Indicator for redisClusterReset() (Pipel.) */
void *ssl; /* Pointer to a redisSSLContext when using SSL/TLS. */
sslInitFn *ssl_init_fn; /* Func ptr for SSL context initiation */
void (*on_connect)(const struct redisContext *c, int status);
void (*event_callback)(const struct redisClusterContext *cc, int event,
void *privdata);
void *event_privdata;
} redisClusterContext;
/* Context for accessing a Redis Cluster asynchronously */
typedef struct redisClusterAsyncContext {
redisClusterContext *cc;
int err; /* Error flags, 0 when there is no error */
char errstr[128]; /* String representation of error when applicable */
int64_t lastSlotmapUpdateAttempt; /* Timestamp */
void *adapter; /* Adapter to the async event library */
adapterAttachFn *attach_fn; /* Func ptr for attaching the async library */
/* Called when either the connection is terminated due to an error or per
* user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
redisDisconnectCallback *onDisconnect;
/* Called when the first write event was received. */
redisConnectCallback *onConnect;
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
redisConnectCallbackNC *onConnectNC;
#endif
} redisClusterAsyncContext;
typedef struct redisClusterNodeIterator {
redisClusterContext *cc;
uint64_t route_version;
int retries_left;
dictIterator di;
} redisClusterNodeIterator;
/*
* Synchronous API
*/
redisClusterContext *redisClusterConnect(const char *addrs, int flags);
redisClusterContext *redisClusterConnectWithTimeout(const char *addrs,
const struct timeval tv,
int flags);
int redisClusterConnect2(redisClusterContext *cc);
redisClusterContext *redisClusterContextInit(void);
void redisClusterFree(redisClusterContext *cc);
/* Configuration options */
int redisClusterSetOptionAddNode(redisClusterContext *cc, const char *addr);
int redisClusterSetOptionAddNodes(redisClusterContext *cc, const char *addrs);
/* Deprecated function, option has no effect. */
int redisClusterSetOptionConnectBlock(redisClusterContext *cc);
/* Deprecated function, option has no effect. */
int redisClusterSetOptionConnectNonBlock(redisClusterContext *cc);
int redisClusterSetOptionUsername(redisClusterContext *cc,
const char *username);
int redisClusterSetOptionPassword(redisClusterContext *cc,
const char *password);
int redisClusterSetOptionParseSlaves(redisClusterContext *cc);
int redisClusterSetOptionParseOpenSlots(redisClusterContext *cc);
int redisClusterSetOptionRouteUseSlots(redisClusterContext *cc);
int redisClusterSetOptionConnectTimeout(redisClusterContext *cc,
const struct timeval tv);
int redisClusterSetOptionTimeout(redisClusterContext *cc,
const struct timeval tv);
int redisClusterSetOptionMaxRetry(redisClusterContext *cc, int max_retry_count);
/* Deprecated function, replaced with redisClusterSetOptionMaxRetry() */
void redisClusterSetMaxRedirect(redisClusterContext *cc,
int max_redirect_count);
/* A hook for connect and reconnect attempts, e.g. for applying additional
* socket options. This is called just after connect, before TLS handshake and
* Redis authentication.
*
* On successful connection, `status` is set to `REDIS_OK` and the file
* descriptor can be accessed as `c->fd` to apply socket options.
*
* On failed connection attempt, this callback is called with `status` set to
* `REDIS_ERR`. The `err` field in the `redisContext` can be used to find out
* the cause of the error. */
int redisClusterSetConnectCallback(redisClusterContext *cc,
void(fn)(const redisContext *c, int status));
/* A hook for events. */
int redisClusterSetEventCallback(redisClusterContext *cc,
void(fn)(const redisClusterContext *cc,
int event, void *privdata),
void *privdata);
/* Blocking
* The following functions will block for a reply, or return NULL if there was
* an error in performing the command.
*/
/* Variadic commands (like printf) */
void *redisClusterCommand(redisClusterContext *cc, const char *format, ...);
void *redisClusterCommandToNode(redisClusterContext *cc, redisClusterNode *node,
const char *format, ...);
/* Variadic using va_list */
void *redisClustervCommand(redisClusterContext *cc, const char *format,
va_list ap);
void *redisClustervCommandToNode(redisClusterContext *cc,
redisClusterNode *node, const char *format,
va_list ap);
/* Using argc and argv */
void *redisClusterCommandArgv(redisClusterContext *cc, int argc,
const char **argv, const size_t *argvlen);
/* Send a Redis protocol encoded string */
void *redisClusterFormattedCommand(redisClusterContext *cc, char *cmd, int len);
/* Pipelining
* The following functions will write a command to the output buffer.
* A call to `redisClusterGetReply()` will flush all commands in the output
* buffer and read until it has a reply from the first command in the buffer.
*/
/* Variadic commands (like printf) */
int redisClusterAppendCommand(redisClusterContext *cc, const char *format, ...);
int redisClusterAppendCommandToNode(redisClusterContext *cc,
redisClusterNode *node, const char *format,
...);
/* Variadic using va_list */
int redisClustervAppendCommand(redisClusterContext *cc, const char *format,
va_list ap);
int redisClustervAppendCommandToNode(redisClusterContext *cc,
redisClusterNode *node, const char *format,
va_list ap);
/* Using argc and argv */
int redisClusterAppendCommandArgv(redisClusterContext *cc, int argc,
const char **argv, const size_t *argvlen);
/* Use a Redis protocol encoded string as command */
int redisClusterAppendFormattedCommand(redisClusterContext *cc, char *cmd,
int len);
/* Flush output buffer and return first reply */
int redisClusterGetReply(redisClusterContext *cc, void **reply);
/* Reset context after a performed pipelining */
void redisClusterReset(redisClusterContext *cc);
/* Update the slotmap by querying any node. */
int redisClusterUpdateSlotmap(redisClusterContext *cc);
/* Internal functions */
redisContext *ctx_get_by_node(redisClusterContext *cc, redisClusterNode *node);
struct dict *parse_cluster_nodes(redisClusterContext *cc, char *str,
int str_len, int flags);
struct dict *parse_cluster_slots(redisClusterContext *cc, redisReply *reply,
int flags);
/*
* Asynchronous API
*/
redisClusterAsyncContext *redisClusterAsyncContextInit(void);
void redisClusterAsyncFree(redisClusterAsyncContext *acc);
int redisClusterAsyncSetConnectCallback(redisClusterAsyncContext *acc,
redisConnectCallback *fn);
#ifndef HIRCLUSTER_NO_NONCONST_CONNECT_CB
int redisClusterAsyncSetConnectCallbackNC(redisClusterAsyncContext *acc,
redisConnectCallbackNC *fn);
#endif
int redisClusterAsyncSetDisconnectCallback(redisClusterAsyncContext *acc,
redisDisconnectCallback *fn);
/* Connect and update slotmap, will block until complete. */
redisClusterAsyncContext *redisClusterAsyncConnect(const char *addrs,
int flags);
/* Connect and update slotmap asynchronously using configured event engine. */
int redisClusterAsyncConnect2(redisClusterAsyncContext *acc);
void redisClusterAsyncDisconnect(redisClusterAsyncContext *acc);
/* Commands */
int redisClusterAsyncCommand(redisClusterAsyncContext *acc,
redisClusterCallbackFn *fn, void *privdata,
const char *format, ...);
int redisClusterAsyncCommandToNode(redisClusterAsyncContext *acc,
redisClusterNode *node,
redisClusterCallbackFn *fn, void *privdata,
const char *format, ...);
int redisClustervAsyncCommand(redisClusterAsyncContext *acc,
redisClusterCallbackFn *fn, void *privdata,
const char *format, va_list ap);
int redisClusterAsyncCommandArgv(redisClusterAsyncContext *acc,
redisClusterCallbackFn *fn, void *privdata,
int argc, const char **argv,
const size_t *argvlen);
int redisClusterAsyncCommandArgvToNode(redisClusterAsyncContext *acc,
redisClusterNode *node,
redisClusterCallbackFn *fn,
void *privdata, int argc,
const char **argv,
const size_t *argvlen);
/* Use a Redis protocol encoded string as command */
int redisClusterAsyncFormattedCommand(redisClusterAsyncContext *acc,
redisClusterCallbackFn *fn,
void *privdata, char *cmd, int len);
int redisClusterAsyncFormattedCommandToNode(redisClusterAsyncContext *acc,
redisClusterNode *node,
redisClusterCallbackFn *fn,
void *privdata, char *cmd, int len);
/* Internal functions */
redisAsyncContext *actx_get_by_node(redisClusterAsyncContext *acc,
redisClusterNode *node);
/* Cluster node iterator functions */
void redisClusterInitNodeIterator(redisClusterNodeIterator *iter,
redisClusterContext *cc);
redisClusterNode *redisClusterNodeNext(redisClusterNodeIterator *iter);
/* Helper functions */
unsigned int redisClusterGetSlotByKey(char *key);
redisClusterNode *redisClusterGetNodeByKey(redisClusterContext *cc, char *key);
/* Old names of renamed functions and types, kept for backward compatibility. */
#ifndef HIRCLUSTER_NO_OLD_NAMES
#define cluster_update_route redisClusterUpdateSlotmap
#define initNodeIterator redisClusterInitNodeIterator
#define nodeNext redisClusterNodeNext
#define redisClusterConnectNonBlock redisClusterConnect
typedef struct redisClusterNode cluster_node;
typedef struct redisClusterNodeIterator nodeIterator;
#endif
#ifdef __cplusplus
}
#endif
#endif