-
Notifications
You must be signed in to change notification settings - Fork 76
/
ConnectionPool.cpp
711 lines (619 loc) · 19.3 KB
/
ConnectionPool.cpp
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#include <sys/socket.h>
#include <poll.h>
#include <errno.h>
#include <stdlib.h>
#include <list>
#include <vector>
#include <algorithm>
#include "ConnectionPool.h"
#include "Utility.h"
#include "Keywords.h"
#include "Parser.h"
using std::vector;
using douban::mc::keywords::kCRLF;
using douban::mc::keywords::kSPACE;
using douban::mc::keywords::k_NOREPLY;
using douban::mc::hashkit::KetamaSelector;
using douban::mc::types::RetrievalResult;
namespace douban {
namespace mc {
ConnectionPool::ConnectionPool()
: m_nActiveConn(0), m_nInvalidKey(0), m_conns(NULL), m_nConns(0),
m_pollTimeout(MC_DEFAULT_POLL_TIMEOUT) {
}
ConnectionPool::~ConnectionPool() {
delete[] m_conns;
}
void ConnectionPool::setHashFunction(hash_function_options_t fn_opt) {
switch (fn_opt) {
case OPT_HASH_MD5:
m_connSelector.setHashFunction(&douban::mc::hashkit::hash_md5);
break;
case OPT_HASH_FNV1_32:
m_connSelector.setHashFunction(&douban::mc::hashkit::hash_fnv1_32);
break;
case OPT_HASH_FNV1A_32:
m_connSelector.setHashFunction(&douban::mc::hashkit::hash_fnv1a_32);
break;
case OPT_HASH_CRC_32:
m_connSelector.setHashFunction(&douban::mc::hashkit::hash_crc_32);
break;
default:
NOT_REACHED();
break;
}
}
int ConnectionPool::init(const char* const * hosts, const uint32_t* ports, const size_t n,
const char* const * aliases) {
delete[] m_conns;
m_connSelector.reset();
int rv = 0;
m_nConns = n;
m_conns = new Connection[m_nConns];
for (size_t i = 0; i < m_nConns; i++) {
rv += m_conns[i].init(hosts[i], ports[i], aliases == NULL ? NULL : aliases[i]);
}
m_connSelector.addServers(m_conns, m_nConns);
return rv;
}
int ConnectionPool::updateServers(const char* const* hosts, const uint32_t* ports, const size_t n,
const char* const* aliases) {
int rv = 0;
if (m_nConns != n) {
return 1;
}
if (aliases != NULL) {
for (size_t i = 0; i < m_nConns; i++) {
if (m_conns[i].hasAlias() && (strcmp(m_conns[i].name(), aliases[i]) != 0)) {
return 2;
}
}
}
for (size_t i = 0; i < m_nConns; i++) {
if ((strcmp(m_conns[i].host(), hosts[i]) == 0) && (m_conns[i].port() == ports[i])) {
--rv;
} else {
rv += m_conns[i].init(hosts[i], ports[i], aliases == NULL ? NULL : aliases[i]);
m_conns[i].markDead(keywords::kUPDATE_SERVER, 0);
m_conns[i].reset();
}
}
return rv;
}
const char* ConnectionPool::getServerAddressByKey(const char *key, const size_t keyLen) {
bool check_alive = false;
Connection* conn = m_connSelector.getConn(key, keyLen, check_alive);
if (conn == NULL) {
return NULL;
}
return conn->name();
}
const char* ConnectionPool::getRealtimeServerAddressByKey(const char *key, const size_t keyLen) {
bool check_alive = true;
Connection* conn = m_connSelector.getConn(key, keyLen, check_alive);
if (conn == NULL) {
return NULL;
}
return conn->name();
}
void ConnectionPool::enableConsistentFailover() {
m_connSelector.enableFailover();
}
void ConnectionPool::disableConsistentFailover() {
m_connSelector.disableFailover();
}
void ConnectionPool::dispatchStorage(op_code_t op,
const char* const* keys, const size_t* keyLens,
const flags_t* flags, const exptime_t exptime,
const cas_unique_t* cas_uniques, const bool noreply,
const char* const* vals, const size_t* valLens,
size_t nItems) {
size_t i = 0, idx = 0;
for (; i < nItems; ++i) {
if (!utility::isValidKey(keys[i], keyLens[i])) {
++m_nInvalidKey;
continue;
}
Connection* conn = m_connSelector.getConn(keys[i], keyLens[i]);
if (conn == NULL) {
continue;
}
switch (op) {
case SET_OP:
conn->takeBuffer(keywords::kSET_, 4);
break;
case ADD_OP:
conn->takeBuffer(keywords::kADD_, 4);
break;
case REPLACE_OP:
conn->takeBuffer(keywords::kREPLACE_, 8);
break;
case APPEND_OP:
conn->takeBuffer(keywords::kAPPEND_, 7);
break;
case PREPEND_OP:
conn->takeBuffer(keywords::kPREPEND_, 8);
break;
case CAS_OP:
conn->takeBuffer(keywords::kCAS_, 4);
break;
default:
NOT_REACHED();
break;
}
conn->takeBuffer(keys[i], keyLens[i]);
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(flags[i]);
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(exptime);
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(valLens[i]);
if (op == CAS_OP) {
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(cas_uniques[i]);
}
if (noreply) {
conn->takeBuffer(k_NOREPLY, 8);
} else {
conn->addRequestKey(keys[i], keyLens[i]);
}
++conn->m_counter;
conn->takeBuffer(kCRLF, 2);
conn->takeBuffer(vals[i], valLens[i]);
conn->takeBuffer(kCRLF, 2);
}
for (idx = 0; idx < m_nConns; idx++) {
Connection* conn = m_conns + idx;
if (conn->m_counter > 0) {
conn->setParserMode(MODE_COUNTING);
++m_nActiveConn;
m_activeConns.push_back(conn);
}
// for ignore noreply
conn->m_counter = conn->requestKeyCount();
if (conn->m_counter > 0) {
conn->getMessageResults()->reserve(conn->m_counter);
}
}
}
void ConnectionPool::dispatchRetrieval(op_code_t op, const char* const* keys,
const size_t* keyLens, size_t nKeys) {
size_t i = 0, idx = 0;
for (; i < nKeys; ++i) {
const char* key = keys[i];
const size_t len = keyLens[i];
if (!utility::isValidKey(key, len)) {
++m_nInvalidKey;
continue;
}
Connection* conn = m_connSelector.getConn(key, len);
if (conn == NULL) {
continue;
}
if (++conn->m_counter == 1) {
switch (op) {
case GET_OP:
conn->takeBuffer(keywords::kGET, 3);
break;
case GETS_OP:
conn->takeBuffer(keywords::kGETS, 4);
break;
default:
NOT_REACHED();
break;
}
}
conn->takeBuffer(kSPACE, 1);
conn->takeBuffer(key, len);
}
for (idx = 0; idx < m_nConns; idx++) {
Connection* conn = m_conns + idx;
if (conn->m_counter > 0) {
conn->takeBuffer(kCRLF, 2);
conn->setParserMode(MODE_END_STATE);
++m_nActiveConn;
m_activeConns.push_back(conn);
conn->getRetrievalResults()->reserve(conn->m_counter);
}
}
// log_debug("after dispatchRetrieval: m_nActiveConn: %d", this->m_nActiveConn);
}
void ConnectionPool::dispatchDeletion(const char* const* keys, const size_t* keyLens,
const bool noreply, size_t nItems) {
size_t i = 0, idx = 0;
for (; i < nItems; ++i) {
if (!utility::isValidKey(keys[i], keyLens[i])) {
++m_nInvalidKey;
continue;
}
Connection* conn = m_connSelector.getConn(keys[i], keyLens[i]);
if (conn == NULL) {
continue;
}
conn->takeBuffer(keywords::kDELETE_, 7);
conn->takeBuffer(keys[i], keyLens[i]);
if (noreply) {
conn->takeBuffer(k_NOREPLY, 8);
} else {
conn->addRequestKey(keys[i], keyLens[i]);
}
++conn->m_counter;
conn->takeBuffer(kCRLF, 2);
}
for (idx = 0; idx < m_nConns; idx++) {
Connection* conn = m_conns + idx;
if (conn->m_counter > 0) {
conn->setParserMode(MODE_COUNTING);
++m_nActiveConn;
m_activeConns.push_back(conn);
}
// for ignore noreply
conn->m_counter = conn->requestKeyCount();
if (conn->m_counter > 0) {
conn->getMessageResults()->reserve(conn->m_counter);
}
}
}
void ConnectionPool::dispatchTouch(
const char* const* keys, const size_t* keyLens,
const exptime_t exptime, const bool noreply, size_t nItems) {
size_t i = 0, idx = 0;
for (; i < nItems; ++i) {
if (!utility::isValidKey(keys[i], keyLens[i])) {
++m_nInvalidKey;
continue;
}
Connection* conn = m_connSelector.getConn(keys[i], keyLens[i]);
if (conn == NULL) {
continue;
}
conn->takeBuffer(keywords::kTOUCH_, 6);
conn->takeBuffer(keys[i], keyLens[i]);
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(exptime);
if (noreply) {
conn->takeBuffer(k_NOREPLY, 8);
} else {
conn->addRequestKey(keys[i], keyLens[i]);
}
++conn->m_counter;
conn->takeBuffer(kCRLF, 2);
}
for (idx = 0; idx < m_nConns; idx++) {
Connection* conn = m_conns + idx;
if (conn->m_counter > 0) {
conn->setParserMode(MODE_COUNTING);
++m_nActiveConn;
m_activeConns.push_back(conn);
}
// for ignore noreply
conn->m_counter = conn->requestKeyCount();
if (conn->m_counter > 0) {
conn->getMessageResults()->reserve(conn->m_counter);
}
}
}
void ConnectionPool::dispatchIncrDecr(op_code_t op, const char* key, const size_t keyLen,
const uint64_t delta, const bool noreply) {
if (!utility::isValidKey(key, keyLen)) {
++m_nInvalidKey;
return;
}
Connection* conn = m_connSelector.getConn(key, keyLen);
if (conn == NULL) {
return;
}
switch (op) {
case INCR_OP:
conn->takeBuffer(keywords::kINCR_, 5);
break;
case DECR_OP:
conn->takeBuffer(keywords::kDECR_, 5);
break;
default:
NOT_REACHED();
break;
}
conn->takeBuffer(key, keyLen);
conn->takeBuffer(kSPACE, 1);
conn->takeNumber(delta);
if (noreply) {
conn->takeBuffer(k_NOREPLY, 8);
} else {
conn->addRequestKey(key, keyLen);
}
++conn->m_counter;
conn->takeBuffer(kCRLF, 2);
conn->setParserMode(MODE_COUNTING);
++m_nActiveConn;
m_activeConns.push_back(conn);
// for ignore noreply
// before the below line, conn->m_counter is a counter regarding how many packet to *send*
conn->m_counter = conn->requestKeyCount();
// after the upper line, conn->m_counter is a counter regarding how many packet to *recv*
}
void ConnectionPool::broadcastCommand(const char * const cmd, const size_t cmdLen, const bool noreply) {
for (size_t idx = 0; idx < m_nConns; ++idx) {
Connection* conn = m_conns + idx;
if (!conn->alive()) {
if (!conn->tryReconnect(false)) {
continue;
}
}
conn->takeBuffer(cmd, cmdLen);
if (!noreply) {
++conn->m_counter;
}
conn->takeBuffer(kCRLF, 2);
conn->setParserMode(MODE_END_STATE);
++m_nActiveConn;
m_activeConns.push_back(conn);
}
}
err_code_t ConnectionPool::waitPoll() {
if (m_nActiveConn == 0) {
if (m_nInvalidKey > 0) {
return RET_INVALID_KEY_ERR;
} else {
// hard server error
return RET_MC_SERVER_ERR;
}
}
nfds_t n_fds = m_nActiveConn;
pollfd_t pollfds[n_fds];
Connection* fd2conn[n_fds];
pollfd_t* pollfd_ptr = NULL;
nfds_t fd_idx = 0;
for (std::vector<Connection*>::iterator it = m_activeConns.begin(); it != m_activeConns.end();
++it, ++fd_idx) {
Connection* conn = *it;
pollfd_ptr = &pollfds[fd_idx];
pollfd_ptr->fd = conn->socketFd();
pollfd_ptr->events = POLLOUT | POLLIN;
fd2conn[fd_idx] = conn;
}
err_code_t ret_code = RET_OK;
while (m_nActiveConn) {
int rv = poll(pollfds, n_fds, m_pollTimeout);
if (rv == -1) {
markDeadAll(pollfds, keywords::kPOLL_ERROR);
ret_code = RET_POLL_ERR;
break;
} else if (rv == 0) {
log_warn("poll timeout. (m_nActiveConn: %d)", m_nActiveConn);
// NOTE: MUST reset all active TCP connections after timeout.
markDeadAll(pollfds, keywords::kPOLL_TIMEOUT_ERROR);
ret_code = RET_POLL_TIMEOUT_ERR;
break;
} else {
err_code_t err;
for (fd_idx = 0; fd_idx < n_fds; fd_idx++) {
pollfd_ptr = &pollfds[fd_idx];
Connection* conn = fd2conn[fd_idx];
if (pollfd_ptr->revents & (POLLERR | POLLHUP | POLLNVAL)) {
markDeadConn(conn, keywords::kCONN_POLL_ERROR, pollfd_ptr);
if (conn->tryReconnect()) {
rewindConn(conn, pollfd_ptr);
} else {
ret_code = RET_CONN_POLL_ERR;
--m_nActiveConn;
}
goto next_fd;
}
// first recv before send
if (pollfd_ptr->revents & POLLIN && !conn->isSent()) {
ssize_t nRecv = conn->recv(true);
if (nRecv == -1 || nRecv == 0) {
markDeadConn(conn, keywords::kRECV_ERROR, pollfd_ptr);
if (conn->tryReconnect(false)) {
pollfd_ptr->fd = conn->socketFd();
pollfd_ptr->events = POLLOUT;
} else {
ret_code = RET_RECV_ERR;
--m_nActiveConn;
}
goto next_fd;
} else {
// if we recv some data before we sent (normally impossible)
pollfd_ptr->events &= ~POLLIN;
}
}
// send
if (pollfd_ptr->revents & POLLOUT) {
// POLLOUT send
ssize_t nToSend = conn->send();
if (nToSend == -1) {
markDeadConn(conn, keywords::kSEND_ERROR, pollfd_ptr);
if (conn->tryReconnect()) {
rewindConn(conn, pollfd_ptr);
} else {
ret_code = RET_SEND_ERR;
--m_nActiveConn;
}
goto next_fd;
} else {
// start to recv if any data is sent
pollfd_ptr->events |= POLLIN;
if (nToSend == 0) {
// log_debug("[%d] all sent", pollfd_ptr->fd);
pollfd_ptr->events &= ~POLLOUT;
if (conn->m_counter == 0) {
// just send, no recv for noreply
--m_nActiveConn;
}
}
}
}
// recv
if (pollfd_ptr->revents & POLLIN) {
// POLLIN recv
ssize_t nRecv = conn->recv();
if (nRecv == -1 || nRecv == 0) {
markDeadConn(conn, keywords::kRECV_ERROR, pollfd_ptr);
if (conn->tryReconnect()) {
rewindConn(conn, pollfd_ptr);
} else {
ret_code = RET_RECV_ERR;
--m_nActiveConn;
}
goto next_fd;
}
conn->process(err);
switch (err) {
case RET_OK:
pollfd_ptr->events &= ~POLLIN;
--m_nActiveConn;
break;
case RET_INCOMPLETE_BUFFER_ERR:
break;
case RET_PROGRAMMING_ERR:
markDeadConn(conn, keywords::kPROGRAMMING_ERROR, pollfd_ptr);
ret_code = RET_PROGRAMMING_ERR;
--m_nActiveConn;
goto next_fd;
break;
case RET_MC_SERVER_ERR:
// soft server error
markDeadConn(conn, keywords::kSERVER_ERROR, pollfd_ptr);
ret_code = RET_MC_SERVER_ERR;
--m_nActiveConn;
goto next_fd;
break;
default:
NOT_REACHED();
break;
}
}
next_fd: {}
} // end for
}
}
return ret_code;
}
void ConnectionPool::collectRetrievalResult(std::vector<retrieval_result_t*>& results) {
for (std::vector<Connection*>::iterator it = m_activeConns.begin();
it != m_activeConns.end(); ++it) {
types::RetrievalResultList* rst = (*it)->getRetrievalResults();
for (types::RetrievalResultList::iterator it2 = rst->begin(); it2 != rst->end(); ++it2) {
RetrievalResult& r1 = *it2;
if (r1.bytesRemain > 0) {
// This may be triggered on get_multi when data_block
// of one retrieval result is not complete yet.
continue;
}
results.push_back(r1.inner());
}
}
}
void ConnectionPool::collectMessageResult(std::vector<message_result_t*>& results) {
for (std::vector<Connection*>::iterator it = m_activeConns.begin();
it != m_activeConns.end(); ++it) {
types::MessageResultList* rst = (*it)->getMessageResults();
for (types::MessageResultList::iterator it2 = rst->begin(); it2 != rst->end(); ++it2) {
results.push_back(&(*it2));
}
}
}
void ConnectionPool::collectBroadcastResult(std::vector<broadcast_result_t>& results, bool isFlushAll) {
results.resize(m_nConns);
for (size_t i = 0; i < m_nConns; ++i) {
Connection* conn = m_conns + i;
broadcast_result_t* conn_result = &results[i];
conn_result->host = const_cast<char*>(conn->name());
conn_result->lines = NULL;
conn_result->line_lens = NULL;
conn_result->len = 0;
conn_result->msg_type = MSG_LIBMC_INVALID;
if (isFlushAll) {
types::MessageResultList* rst = conn->getMessageResults();
if (rst->size() == 1) {
conn_result->msg_type = (rst->front()).type_;
} else {
conn_result->msg_type = MSG_LIBMC_INVALID;
}
} else {
types::LineResultList* rst = conn->getLineResults();
conn_result->len = rst->size();
if (conn_result->len == 0) {
continue;
}
conn_result->lines = new char*[conn_result->len];
conn_result->line_lens = new size_t[conn_result->len];
int j = 0;
for (types::LineResultList::iterator it2 = rst->begin(); it2 != rst->end(); ++it2, ++j) {
types::LineResult* r1 = &(*it2);
conn_result->lines[j] = r1->inner(conn_result->line_lens[j]);
}
}
}
}
void ConnectionPool::collectUnsignedResult(std::vector<unsigned_result_t*>& results) {
if (m_activeConns.size() == 1) {
types::UnsignedResultList* numericRst = m_activeConns.front()->getUnsignedResults();
types::MessageResultList* msgRst = m_activeConns.front()->getMessageResults();
if (numericRst->size() == 1) {
results.push_back(&numericRst->front());
} else if (msgRst->size() == 1) {
ASSERT(msgRst->front().type_ == MSG_NOT_FOUND);
results.push_back(NULL);
}
}
}
void ConnectionPool::reset() {
for (std::vector<Connection*>::iterator it = m_activeConns.begin();
it != m_activeConns.end(); ++it) {
(*it)->reset();
}
m_nActiveConn = 0;
m_nInvalidKey = 0;
m_activeConns.clear();
}
void ConnectionPool::setPollTimeout(int timeout) {
m_pollTimeout = timeout;
}
void ConnectionPool::setConnectTimeout(int timeout) {
for (size_t idx = 0; idx < m_nConns; ++idx) {
Connection* conn = m_conns + idx;
conn->setConnectTimeout(timeout);
}
}
void ConnectionPool::setRetryTimeout(int timeout) {
for (size_t idx = 0; idx < m_nConns; ++idx) {
Connection* conn = m_conns + idx;
conn->setRetryTimeout(timeout);
}
}
void ConnectionPool::setMaxRetries(int max_retries) {
for (size_t idx = 0; idx < m_nConns; ++idx) {
Connection* conn = m_conns + idx;
conn->setMaxRetries(max_retries);
}
}
void ConnectionPool::markDeadAll(pollfd_t* pollfds, const char* reason) {
nfds_t fd_idx = 0;
for (std::vector<Connection*>::iterator it = m_activeConns.begin();
it != m_activeConns.end();
++it, ++fd_idx) {
Connection* conn = *it;
if (pollfds == NULL) {
conn->markDead(reason);
} else {
pollfd_t* pollfd_ptr = &pollfds[fd_idx];
if (pollfd_ptr->events & (POLLOUT | POLLIN)) {
conn->markDead(reason);
}
}
}
}
void ConnectionPool::markDeadConn(Connection* conn, const char* reason, pollfd_t* fd_ptr) {
conn->markDead(reason);
fd_ptr->events &= ~POLLOUT & ~POLLIN;
fd_ptr->fd = conn->socketFd();
}
void ConnectionPool::rewindConn(Connection* conn, pollfd_t* fd_ptr) {
conn->rewind();
fd_ptr->fd = conn->socketFd();
fd_ptr->events = POLLOUT;
}
} // namespace mc
} // namespace douban