-
Notifications
You must be signed in to change notification settings - Fork 386
Expand file tree
/
Copy pathasync_shards_pool.cpp
More file actions
466 lines (370 loc) · 12.7 KB
/
Copy pathasync_shards_pool.cpp
File metadata and controls
466 lines (370 loc) · 12.7 KB
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
/**************************************************************************
Copyright (c) 2021 sewenew
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************/
#include "sw/redis++/async_shards_pool.h"
#include <cassert>
#include <chrono>
#include <thread>
#include "sw/redis++/errors.h"
namespace sw {
namespace redis {
const std::size_t AsyncShardsPool::SHARDS;
AsyncShardsPool::AsyncShardsPool(const EventLoopSPtr &loop,
const ConnectionPoolOptions &pool_opts,
const ConnectionOptions &connection_opts,
Role role,
const ClusterOptions &cluster_opts) :
_pool_opts(pool_opts),
_connection_opts(connection_opts),
_role(role),
_cluster_opts(cluster_opts),
_loop(loop) {
assert(loop);
if (_connection_opts.type != ConnectionType::TCP) {
throw Error("Only support TCP connection for Redis Cluster");
}
// Initialize local node-slot mapping with all slots to the given node.
// We'll update it later.
auto node = Node{_connection_opts.host, _connection_opts.port};
_shards.emplace(SlotRange{0U, SHARDS}, node);
_pools.emplace(node,
std::make_shared<AsyncConnectionPool>(_loop, _pool_opts, _connection_opts));
_worker = std::thread([this]() { this->_run(); });
// Update node-slot mapping asynchrounously.
update();
}
AsyncShardsPool::~AsyncShardsPool() {
update({}, nullptr);
if (_worker.joinable()) {
_worker.join();
}
}
AsyncConnectionPoolSPtr AsyncShardsPool::fetch(const StringView &key) {
auto slot = _slot(key);
return _fetch(slot);
}
AsyncConnectionPoolSPtr AsyncShardsPool::fetch() {
auto slot = _slot();
return _fetch(slot);
}
AsyncConnectionPoolSPtr AsyncShardsPool::fetch(const Node &node) {
std::lock_guard<std::mutex> lock(_mutex);
auto iter = _pools.find(node);
if (iter == _pools.end()) {
// Node doesn't exist, and it should be a newly created node.
// So add a new connection pool.
iter = _add_node(node);
}
assert(iter != _pools.end());
return iter->second;
}
void AsyncShardsPool::update(const std::string &key, AsyncEventUPtr event) {
{
std::lock_guard<std::mutex> lock(_mutex);
_events.push(RedeliverEvent{key, std::move(event)});
}
_cv.notify_one();
}
void AsyncShardsPool::update() {
update({}, AsyncEventUPtr(new UpdateShardsEvent));
}
ConnectionOptions AsyncShardsPool::connection_options(const StringView &key) {
auto slot = _slot(key);
return _connection_options(slot);
}
ConnectionOptions AsyncShardsPool::connection_options() {
auto slot = _slot();
return _connection_options(slot);
}
ConnectionOptions AsyncShardsPool::_connection_options(Slot slot) {
std::lock_guard<std::mutex> lock(_mutex);
auto &pool = _get_pool(slot);
assert(pool);
return pool->connection_options();
}
Slot AsyncShardsPool::_slot(const StringView &key) const {
// The following code is copied from: https://redis.io/topics/cluster-spec
// And I did some minor changes.
const auto *k = key.data();
auto keylen = static_cast<int>(key.size());
// start-end indexes of { and }.
int s = 0;
int e = 0;
// Search the first occurrence of '{'.
for (s = 0; s < keylen; s++)
if (k[s] == '{') break;
// No '{' ? Hash the whole key. This is the base case.
if (s == keylen) return crc16(k, keylen) & SHARDS;
// '{' found? Check if we have the corresponding '}'.
for (e = s + 1; e < keylen; e++)
if (k[e] == '}') break;
// No '}' or nothing between {} ? Hash the whole key.
if (e == keylen || e == s + 1) return crc16(k, keylen) & SHARDS;
// If we are here there is both a { and a } on its right. Hash
// what is in the middle between { and }.
return crc16(k + s + 1, e - s - 1) & SHARDS;
}
Slot AsyncShardsPool::_slot() const {
return _random(0, SHARDS);
}
AsyncConnectionPoolSPtr AsyncShardsPool::_fetch(Slot slot) {
std::lock_guard<std::mutex> lock(_mutex);
return _get_pool(slot);
}
AsyncConnectionPoolSPtr& AsyncShardsPool::_get_pool(Slot slot) {
const auto &node = _get_node(slot);
auto node_iter = _pools.find(node);
if (node_iter == _pools.end()) {
throw SlotUncoveredError(slot);
}
return node_iter->second;
}
void AsyncShardsPool::_run() {
while (true) {
auto events = _fetch_events();
assert(!events.empty());
try {
// TODO: when we try to stop the worker thread,
// we don't need to call `_update_shards`
_update_shards();
// if _redeliver_events or _fail_events returns true if there's a null event,
// and we exit the thread loop.
if (_redeliver_events(events)) {
break;
}
} catch (...) {
if (_fail_events(events, std::current_exception())) {
break;
}
// Failed to update shards, retry later.
std::this_thread::sleep_for(_pool_opts.solt_node_error_recover_time);
update();
}
}
}
auto AsyncShardsPool::_fetch_events() -> std::queue<RedeliverEvent> {
std::queue<RedeliverEvent> events;
std::unique_lock<std::mutex> lock(_mutex);
if (!_cv.wait_for(lock,
_cluster_opts.slot_map_refresh_interval,
[this]() { return !(this->_events).empty(); })) {
// Reach timeout, but there's still no event, put an update event.
_events.push(RedeliverEvent{{}, AsyncEventUPtr(new UpdateShardsEvent)});
}
events.swap(_events);
return events;
}
void AsyncShardsPool::update_conn_opt(std::map<std::string, std::string> _new_opts)
{
std::lock_guard<std::mutex> lock(_mutex);
//Auth related
if (_new_opts.count("passwd") != 0)
{
_connection_opts.password = _new_opts["passwd"];
}
if (_new_opts.count("user") != 0)
{
_connection_opts.user = _new_opts["user"];
}
//TLS related
if (_new_opts.count("enable_tls") != 0)
{
if (_new_opts["enable_tls"] == "True") {
_connection_opts.tls.enabled = true;
} else {
_connection_opts.tls.enabled = false;
}
}
if (_new_opts.count("trust_ca_file") != 0)
{
_connection_opts.tls.cacert = _new_opts["trust_ca_file"];
}
if (_new_opts.count("trust_ca_path") != 0)
{
_connection_opts.tls.cacertdir = _new_opts["trust_ca_path"];
}
if (_new_opts.count("client_cert_file") != 0)
{
_connection_opts.tls.cert = _new_opts["client_cert_file"];
}
if (_new_opts.count("client_cert_key") != 0)
{
_connection_opts.tls.key = _new_opts["client_cert_key"];
}
if (_new_opts.count("server_name") != 0)
{
_connection_opts.tls.sni = _new_opts["server_name"];
}
if (_new_opts.count("tls_protocol") != 0)
{
_connection_opts.tls.tls_protocol = _new_opts["tls_protocol"];
}
if (_new_opts.count("tls_ciphers") != 0)
{
_connection_opts.tls.ciphers = _new_opts["tls_ciphers"];
}
//Timers
int interval;
if (_new_opts.count("conn_timeout") != 0)
{
try {
interval = std::stoi(_new_opts["conn_timeout"]);
_connection_opts.connect_timeout = std::chrono::milliseconds(interval);
} catch (std::exception &e) {
//ingore
}
}
if (_new_opts.count("command_timeout") != 0)
{
try {
interval = std::stoi(_new_opts["command_timeout"]);
_connection_opts.socket_timeout = std::chrono::milliseconds(interval);
} catch (std::exception &e) {
//ingore
}
}
if (_new_opts.count("slot_error_retry") != 0)
{
try {
interval = std::stoi(_new_opts["slot_error_retry"]);
_connection_opts.socket_timeout = std::chrono::seconds(interval);
} catch (std::exception &e) {
// ingore
}
}
//Above new prop update, only apply into new connections.
for (const auto &entry : _pools)
{
const AsyncConnectionPoolSPtr &pool = entry.second;
if (pool != nullptr)
{
pool->update_conn_opt(_connection_opts);
}
}
}
std::size_t AsyncShardsPool::_random(std::size_t min, std::size_t max) const {
static thread_local std::default_random_engine engine;
std::uniform_int_distribution<std::size_t> uniform_dist(min, max);
return uniform_dist(engine);
}
const Node& AsyncShardsPool::_get_node(Slot slot) const {
auto shards_iter = _shards.lower_bound(SlotRange{slot, slot});
if (shards_iter == _shards.end() || slot < shards_iter->first.min) {
throw SlotUncoveredError(slot);
}
return shards_iter->second;
}
Shards AsyncShardsPool::_get_shards(const std::string &host, int port) {
auto opts = _connection_opts;
opts.host = host;
opts.port = port;
ShardsPool pool(_pool_opts, opts, _role, _cluster_opts);
return pool.shards();
}
void AsyncShardsPool::_update_shards() {
for (int idx = 0; idx < 4; ++idx) {
try {
Shards shards;
if (idx < 3) {
Node node;
{
std::lock_guard<std::mutex> lock(_mutex);
// Randomly pick a node.
node = _get_node(_slot());
}
shards = _get_shards(node.host, node.port);
} else {
shards = _get_shards(_connection_opts.host, _connection_opts.port);
}
std::unordered_set<Node, NodeHash> nodes;
for (const auto &shard : shards) {
nodes.insert(shard.second);
}
std::lock_guard<std::mutex> lock(_mutex);
_shards = std::move(shards);
// Remove non-existent nodes.
for (auto iter = _pools.begin(); iter != _pools.end(); ) {
if (nodes.find(iter->first) == nodes.end()) {
// Node has been removed.
_pools.erase(iter++);
} else {
++iter;
}
}
// Add connection pool for new nodes.
// In fact, connections will be created lazily.
for (const auto &node : nodes) {
if (_pools.find(node) == _pools.end()) {
_add_node(node);
}
}
if (!_slot_ready) {
_slot_ready = true; //inital ready, mark it
}
// Update successfully.
return;
} catch (const Error &) {
// continue;
}
}
throw Error("Failed to update shards info");
}
auto AsyncShardsPool::_add_node(const Node &node) -> NodeMap::iterator {
auto opts = _connection_opts;
opts.host = node.host;
opts.port = node.port;
// TODO: Better set readonly an attribute of `Node`.
if (_role == Role::SLAVE) {
opts.readonly = true;
}
return _pools.emplace(node,
std::make_shared<AsyncConnectionPool>(_loop, _pool_opts, opts)).first;
}
bool AsyncShardsPool::_redeliver_events(std::queue<RedeliverEvent> &events) {
bool should_stop_worker = false;
while (!events.empty()) {
auto &event = events.front();
try {
auto &async_event = event.event;
if (!async_event) {
should_stop_worker = true;
events.pop();
continue;
}
auto pool = fetch(event.key);
assert(pool);
GuardedAsyncConnection connection(pool);
connection.connection().send(std::move(async_event));
} catch (...) {
event.event->set_exception(std::current_exception());
}
events.pop();
}
return should_stop_worker;
}
bool AsyncShardsPool::_fail_events(std::queue<RedeliverEvent> &events,
std::exception_ptr err) {
bool should_stop_worker = false;
while (!events.empty()) {
auto &event = events.front();
auto &async_event = event.event;
if (!async_event) {
should_stop_worker = true;
} else {
async_event->set_exception(err);
}
events.pop();
}
return should_stop_worker;
}
}
}