forked from RedisLabs/redisraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.c
349 lines (281 loc) · 9.11 KB
/
connection.c
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
/*
* This file is part of RedisRaft.
*
* Copyright (c) 2020-2021 Redis Ltd.
*
* RedisRaft is licensed under the Redis Source Available License (RSAL).
*/
#include <time.h>
#include "redisraft.h"
#include "hiredis/adapters/libuv.h"
#include <assert.h>
#define CONN_LOG(level, conn, fmt, ...) \
LOG(level, "{conn:%lu} " fmt, conn ? conn->id : 0, ##__VA_ARGS__)
#define CONN_LOG_ERROR(conn, fmt, ...) CONN_LOG(LOGLEVEL_ERROR, conn, fmt, ##__VA_ARGS__)
#define CONN_LOG_INFO(conn, fmt, ...) CONN_LOG(LOGLEVEL_INFO, conn, fmt, ##__VA_ARGS__)
#define CONN_LOG_VERBOSE(conn, fmt, ...) CONN_LOG(LOGLEVEL_VERBOSE, conn, fmt, ##__VA_ARGS__)
#define CONN_LOG_DEBUG(conn, fmt, ...) CONN_LOG(LOGLEVEL_DEBUG, conn, fmt, ##__VA_ARGS__)
#ifdef ENABLE_TRACE
#define CONN_TRACE(conn, fmt, ...) \
LOG(LOGLEVEL_DEBUG, "%s:%d {conn:%lu} " fmt, \
__FILE__, __LINE__, \
(conn) ? (conn)->id : 0, \
##__VA_ARGS__)
#else
#define CONN_TRACE(conn, fmt, ...) do {} while (0)
#endif
static const char *ConnStateStr[] = {
"disconnected",
"resolving",
"connecting",
"connected",
"connect_error"
};
/* A list of all connections */
static LIST_HEAD(conn_list, Connection) conn_list = LIST_HEAD_INITIALIZER(conn_list);
/* Create a new connection.
*
* The new connection is created in an idle state, so if it has an idle
* callback it should be called shortly after.
*/
Connection *ConnCreate(RedisRaftCtx *rr, void *privdata, ConnectionCallbackFunc idle_cb, ConnectionFreeFunc free_cb)
{
static unsigned long id = 0;
Connection *conn = RedisModule_Calloc(1, sizeof(Connection));
LIST_INSERT_HEAD(&conn_list, conn, entries);
conn->rr = rr;
conn->privdata = privdata;
conn->idle_callback = idle_cb;
conn->free_callback = free_cb;
conn->id = ++id;
conn->timeout.tv_usec = rr->config->connection_timeout;
CONN_TRACE(conn, "Connection created.");
return conn;
}
/* Free a connection object.
*/
static void ConnFree(Connection *conn)
{
if (!conn) {
return;
}
if (conn->free_callback) {
conn->free_callback(conn->privdata);
}
CONN_TRACE(conn, "Connection freed.");
LIST_REMOVE(conn, entries);
RedisModule_Free(conn);
}
/* A callback we register on hiredis to make sure ConnFree gets called when
* the async context is freed. FIXME: Do we need it?
*/
static void connDataCleanupCallback(void *privdata)
{
Connection *conn = (Connection *) privdata;
CONN_TRACE(conn, "connDataCleanupCallback: flags=%d, rc=%p",
conn ? conn->flags : 0,
conn ? conn->rc : NULL);
/* If we got called hiredis is tearing down the context, make sure
* we drop the reference to it.
*/
conn->rc = NULL;
/* If connection was not flagged for async termination, don't clean it up. It
* may get reused or cleaned up at a later stage.
*/
if (!(conn->flags & CONN_TERMINATING)) {
return;
}
ConnFree(conn);
}
/* Request async termination of a connection. The connection will be closed and
* in the next iteration.
*/
void ConnAsyncTerminate(Connection *conn)
{
CONN_TRACE(conn, "ConnAsyncTerminate called.");
conn->flags |= CONN_TERMINATING;
}
/* Connect callback (hiredis).
*
* This callback will always be called as a result of a prior redisAsyncConnect()
* from handleResolved(), with state CONN_CONNECTING.
*
* Depending on status, state transitions to CONN_CONNECTED or CONN_CONNECT_ERROR.
* User callback is called in both cases.
*/
static void handleConnected(const redisAsyncContext *c, int status)
{
Connection *conn = (Connection *) c->data;
CONN_TRACE(conn, "handleConnected: status=%d", status);
if (status == REDIS_OK) {
conn->state = CONN_CONNECTED;
conn->connect_oks++;
conn->last_connected_time = RedisModule_Milliseconds();
} else {
conn->state = CONN_CONNECT_ERROR;
conn->rc = NULL;
conn->connect_errors++;
}
/* If connection was flagged for termination between connection attempt
* and now, we don't call the connect callback.
*/
/* If we're terminating, abort now */
if (conn->flags & CONN_TERMINATING) {
return;
}
/* Call explicit connect callback (even if failed) */
if (conn->connect_callback) {
conn->connect_callback(conn);
}
}
/* Disconnect callback (hiredis).
*
*/
static void handleDisconnected(const redisAsyncContext *c, int status)
{
UNUSED(status);
Connection *conn = (Connection *) c->data;
CONN_TRACE(conn, "handleDisconnected: rc=%p",
conn ? conn->rc : NULL);
if (conn) {
conn->state = CONN_DISCONNECTED;
conn->rc = NULL; /* FIXME: Need this? */
}
}
/* Callback for uv_getaddrinfo.
*/
static void handleResolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
{
Connection *conn = uv_req_get_data((uv_req_t *)resolver);
CONN_TRACE(conn, "handleResolved: flags=%d, state=%s, rc=%p",
conn->flags,
ConnStateStr[conn->state],
conn->rc);
/* If flagged for terminated in the meanwhile, drop now.
*/
if (conn->flags & CONN_TERMINATING) {
conn->state = CONN_DISCONNECTED;
uv_freeaddrinfo(res);
return;
}
if (status < 0) {
CONN_LOG_ERROR(conn, "Failed to resolve '%s': %s", conn->addr.host, uv_strerror(status));
conn->state = CONN_CONNECT_ERROR;
conn->connect_errors++;
uv_freeaddrinfo(res);
return;
}
uv_ip4_name((struct sockaddr_in *) res->ai_addr, conn->ipaddr, sizeof(conn->ipaddr)-1);
uv_freeaddrinfo(res);
/* Initiate connection */
if (conn->rc != NULL) {
redisAsyncFree(conn->rc);
}
/* copy default options setup from redisAsyncConnect with support for timeout */
redisOptions options = {0};
options.connect_timeout = &conn->timeout;
REDIS_OPTIONS_SET_TCP(&options, conn->ipaddr, conn->addr.port);
conn->rc = redisAsyncConnectWithOptions(&options);
if (conn->rc->err) {
conn->state = CONN_CONNECT_ERROR;
conn->connect_errors++;
redisAsyncFree(conn->rc);
conn->rc = NULL;
return;
}
conn->rc->data = conn;
conn->rc->dataCleanup = connDataCleanupCallback;
conn->state = CONN_CONNECTING;
conn->flags &= ~CONN_TERMINATING;
redisLibuvAttach(conn->rc, conn->rr->loop);
redisAsyncSetConnectCallback(conn->rc, handleConnected);
redisAsyncSetDisconnectCallback(conn->rc, handleDisconnected);
}
RRStatus ConnConnect(Connection *conn, const NodeAddr *addr, ConnectionCallbackFunc connect_callback)
{
struct addrinfo hints = {
.ai_family = PF_INET,
.ai_socktype = SOCK_STREAM,
.ai_protocol = IPPROTO_TCP,
.ai_flags = 0
};
CONN_TRACE(conn, "ConnConnect: connecting %s:%u.", addr->host, addr->port);
assert(ConnIsIdle(conn));
conn->addr = *addr;
conn->state = CONN_RESOLVING;
conn->connect_callback = connect_callback;
uv_req_set_data((uv_req_t *)&conn->uv_resolver, conn);
int r = uv_getaddrinfo(conn->rr->loop, &conn->uv_resolver, handleResolved,
conn->addr.host, NULL, &hints);
if (r) {
conn->state = CONN_CONNECT_ERROR;
return RR_ERROR;
}
return RR_OK;
}
void ConnMarkDisconnected(Connection *conn)
{
CONN_TRACE(conn, "ConnMarkDisconnected: rc=%p", conn->rc);
conn->state = CONN_DISCONNECTED;
if (conn->rc) {
redisAsyncFree(conn->rc);
conn->rc = NULL;
}
}
/* An idle state is one that will not transition automatically to another
* state, unless actively mutated.
*/
bool ConnIsIdle(Connection *conn)
{
return (conn->state == CONN_DISCONNECTED || conn->state == CONN_CONNECT_ERROR);
}
bool ConnIsConnected(Connection *conn)
{
return (conn->state == CONN_CONNECTED && !(conn->flags & CONN_TERMINATING));
}
const char *ConnGetStateStr(Connection *conn)
{
return ConnStateStr[conn->state];
}
void *ConnGetPrivateData(Connection *conn)
{
return conn->privdata;
}
RedisRaftCtx *ConnGetRedisRaftCtx(Connection *conn)
{
return conn->rr;
}
redisAsyncContext *ConnGetRedisCtx(Connection *conn)
{
return conn->rc;
}
void HandleIdleConnections(RedisRaftCtx *rr)
{
if (rr->state == REDIS_RAFT_LOADING)
return;
Connection *conn, *tmp;
LIST_FOREACH_SAFE(conn, &conn_list, entries, tmp) {
/* Idle connections are either terminating and should be reaped,
* or waiting for an idle callback which can re-connect them.
*/
if (ConnIsIdle(conn)) {
if (conn->flags & CONN_TERMINATING) {
LIST_REMOVE(conn, entries);
if (conn->rc) {
/* Note: redisAsyncFree will call the connDataCleanupCallback
* callback which will free the Conn structure!
*/
redisAsyncContext *ac = conn->rc;
conn->rc = NULL;
redisAsyncFree(ac);
} else {
ConnFree(conn);
}
} else {
if (conn->idle_callback) {
conn->idle_callback(conn);
}
}
}
}
}