-
Notifications
You must be signed in to change notification settings - Fork 940
/
redis_cluster.cpp
860 lines (670 loc) · 18.4 KB
/
redis_cluster.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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
#include "acl_stdafx.hpp"
#ifndef ACL_PREPARE_COMPILE
#include "acl_cpp/stdlib/snprintf.hpp"
#include "acl_cpp/stdlib/log.hpp"
#include "acl_cpp/redis/redis_node.hpp"
#include "acl_cpp/redis/redis_slot.hpp"
#include "acl_cpp/redis/redis_cluster.hpp"
#endif
#if !defined(ACL_CLIENT_ONLY) && !defined(ACL_REDIS_DISABLE)
namespace acl {
#define INT_LEN 11
#define LONG_LEN 21
redis_cluster::redis_cluster()
{
}
redis_cluster::redis_cluster(redis_client* conn)
: redis_command(conn)
{
}
redis_cluster::redis_cluster(redis_client_cluster* cluster)
: redis_command(cluster)
{
}
redis_cluster::redis_cluster(redis_client_pipeline* pipeline)
: redis_command(pipeline)
{
}
redis_cluster::redis_cluster(redis_client_cluster* cluster, size_t)
: redis_command(cluster)
{
}
redis_cluster::~redis_cluster()
{
free_slots();
free_masters();
free_slaves();
}
bool redis_cluster::cluster_addslots(int first, ...)
{
std::vector<int> slot_list;
va_list ap;
va_start(ap, first);
int n;
while ((n = va_arg(ap, int)) >= 0) {
slot_list.push_back(n);
}
va_end(ap);
if (slot_list.empty()) {
return true;
}
return cluster_addslots(slot_list);
}
bool redis_cluster::cluster_addslots(const int slot_list[], size_t n)
{
build("CLUSTER", "ADDSLOTS", slot_list, n);
return check_status();
}
bool redis_cluster::cluster_addslots(const std::vector<int>& slot_list)
{
build("CLUSTER", "ADDSLOTS", slot_list);
return check_status();
}
bool redis_cluster::cluster_delslots(int first, ...)
{
std::vector<int> slot_list;
va_list ap;
va_start(ap, first);
int n;
while ((n = va_arg(ap, int)) >= 0) {
slot_list.push_back(n);
}
va_end(ap);
if (slot_list.empty()) {
return true;
}
return cluster_delslots(slot_list);
}
bool redis_cluster::cluster_delslots(const int slot_list[], size_t n)
{
build("CLUSTER", "DELSLOTS", slot_list, n);
return check_status();
}
bool redis_cluster::cluster_delslots(const std::vector<int>& slot_list)
{
build("CLUSTER", "DELSLOTS", slot_list);
return check_status();
}
int redis_cluster::cluster_getkeysinslot(size_t slot, size_t max,
std::list<string>& result)
{
const char* argv[4];
size_t lens[4];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "GETKEYSINSLOT";
lens[1] = sizeof("GETKEYSINSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
char* max_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(max_s, LONG_LEN, "%lu", (unsigned long) max);
argv[3] = max_s;
lens[3] = strlen(max_s);
build_request(4, argv, lens);
return get_strings(result);
}
bool redis_cluster::cluster_meet(const char* ip, int port)
{
const char* argv[4];
size_t lens[4];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "MEET";
lens[1] = sizeof("MEET") - 1;
argv[2] = ip;
lens[2] = strlen(ip);
char* port_s = (char*) dbuf_->dbuf_alloc(INT_LEN);
safe_snprintf(port_s, INT_LEN, "%d", port);
argv[3] = port_s;
lens[3] = strlen(port_s);
build_request(4, argv, lens);
return check_status();
}
bool redis_cluster::cluster_reset()
{
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "RESET";
lens[1] = sizeof("RESET") - 1;
build_request(2, argv, lens);
return check_status();
}
bool redis_cluster::cluster_reset_hard()
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "RESET";
lens[1] = sizeof("RESET") - 1;
argv[2] = "HARD";
lens[2] = sizeof("HARD") - 1;
build_request(3, argv, lens);
return check_status();
}
bool redis_cluster::cluster_reset_soft()
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "RESET";
lens[1] = sizeof("RESET") - 1;
argv[2] = "SOFT";
lens[2] = sizeof("SOFT") - 1;
build_request(3, argv, lens);
return check_status();
}
bool redis_cluster::cluster_setslot_importing(size_t slot, const char* src_node)
{
const char* argv[5];
size_t lens[5];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SETSLOT";
lens[1] = sizeof("SETSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
argv[3] = "IMPORTING";
lens[3] = sizeof("IMPORTING") - 1;
argv[4] = src_node;
lens[4] = strlen(src_node);
build_request(5, argv, lens);
return check_status();
}
bool redis_cluster::cluster_setslot_migrating(size_t slot, const char* dst_node)
{
const char* argv[5];
size_t lens[5];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SETSLOT";
lens[1] = sizeof("SETSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
argv[3] = "MIGRATING";
lens[3] = sizeof("MIGRATING") - 1;
argv[4] = dst_node;
lens[4] = strlen(dst_node);
build_request(5, argv, lens);
return check_status();
}
bool redis_cluster::cluster_setslot_stable(size_t slot)
{
const char* argv[4];
size_t lens[4];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SETSLOT";
lens[1] = sizeof("SETSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
argv[3] = "STABLE";
lens[3] = sizeof("STABLE") - 1;
build_request(4, argv, lens);
return check_status();
}
bool redis_cluster::cluster_setslot_node(size_t slot, const char* node)
{
const char* argv[5];
size_t lens[5];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SETSLOT";
lens[1] = sizeof("SETSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
argv[3] = "NODE";
lens[3] = sizeof("NODE") - 1;
argv[4] = node;
lens[4] = strlen(node);
build_request(5, argv, lens);
return check_status();
}
int redis_cluster::cluster_count_failure_reports(const char* node)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "COUNT-FAILURE-REPORTS";
lens[1] = sizeof("COUNT-FAILURE-REPORTS") - 1;
argv[2] = node;
lens[2] = strlen(node);
build_request(3, argv, lens);
return get_number();
}
bool redis_cluster::cluster_failover()
{
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "FAILOVER";
lens[1] = sizeof("FAILOVER") - 1;
build_request(2, argv, lens);
return check_status();
}
bool redis_cluster::cluster_failover_force()
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "FAILOVER";
lens[1] = sizeof("FAILOVER") - 1;
argv[2] = "FORCE";
lens[2] = sizeof("FORCE") - 1;
build_request(3, argv, lens);
return check_status();
}
bool redis_cluster::cluster_failover_takeover()
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "FAILOVER";
lens[1] = sizeof("FAILOVER") - 1;
argv[2] = "TAKEOVER";
lens[2] = sizeof("TAKEOVER") - 1;
build_request(3, argv, lens);
return check_status();
}
bool redis_cluster::cluster_info(std::map<string, string>& result)
{
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "INFO";
lens[1] = sizeof("INFO") - 1;
build_request(2, argv, lens);
string buf;
if (get_string(buf) <= 0) {
return false;
}
string line;
while (true) {
line.clear();
if (!buf.scan_line(line)) {
break;
}
char* name = line.c_str();
char* value = strchr(name, ':');
if (value == NULL || *(value + 1) == 0) {
continue;
}
*value++ = 0;
result[name] = value;
}
return true;
}
bool redis_cluster::cluster_saveconfig()
{
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SAVECONFIG";
lens[1] = sizeof("SAVECONFIG") - 1;
build_request(2, argv, lens);
return check_status();
}
int redis_cluster::cluster_countkeysinslot(size_t slot)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "COUNTKEYSINSLOT";
lens[1] = sizeof("COUNTKEYSINSLOT") - 1;
char* slot_s = (char*) dbuf_->dbuf_alloc(LONG_LEN);
safe_snprintf(slot_s, LONG_LEN, "%lu", (unsigned long) slot);
argv[2] = slot_s;
lens[2] = strlen(slot_s);
build_request(3, argv, lens);
return get_number();
}
bool redis_cluster::cluster_forget(const char* node)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "FORGET";
lens[1] = sizeof("FORGET") - 1;
argv[2] = node;
lens[2] = strlen(node);
build_request(3, argv, lens);
return check_status();
}
int redis_cluster::cluster_keyslot(const char* key)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "KEYSLOT";
lens[1] = sizeof("KEYSLOT") - 1;
argv[2] = key;
lens[2] = strlen(key);
build_request(3, argv, lens);
return get_number();
}
bool redis_cluster::cluster_replicate(const char* node)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "REPLICATE";
lens[1] = sizeof("REPLICATE") - 1;
argv[2] = node;
lens[2] = strlen(node);
build_request(3, argv, lens);
return check_status();
}
bool redis_cluster::cluster_set_config_epoch(const char* epoch)
{
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSETR";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SET-CONFIG-EPOCH";
lens[1] = sizeof("SET-CONFIG-EPOCH") - 1;
argv[2] = epoch;
lens[2] = strlen(epoch);
build_request(3, argv, lens);
return check_status();
}
//////////////////////////////////////////////////////////////////////////
const std::vector<redis_slot*>* redis_cluster::cluster_slots()
{
free_slots();
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SLOTS";
lens[1] = sizeof("SLOTS") - 1;
build_request(2, argv, lens);
const redis_result* rr = run();
if (rr == NULL || rr->get_type() != REDIS_RESULT_ARRAY) {
return NULL;
}
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL) {
return NULL;
}
for (size_t i = 0; i < size; i++) {
const redis_result* rr2 = children[i];
if (rr2 == NULL || rr2->get_type() != REDIS_RESULT_ARRAY) {
continue;
}
redis_slot* master = get_slot_master(rr2);
if (master != NULL) {
slots_.push_back(master);
}
}
return &slots_;
}
redis_slot* redis_cluster::get_slot_master(const redis_result* rr)
{
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL) {
return NULL;
}
if (size < 3) {
return NULL;
}
int slot_min = children[0]->get_integer();
if (slot_min < 0) {
return NULL;
}
int slot_max = children[1]->get_integer();
if (slot_max < slot_min) {
return NULL;
}
redis_slot* master = get_slot(children[2], slot_max, slot_min);
if (master == NULL) {
return NULL;
}
for (size_t i = 3; i < size; i++) {
redis_slot* slave = get_slot(children[i], slot_max, slot_min);
if (slave != NULL) {
master->add_slave(slave);
}
}
return master;
}
redis_slot* redis_cluster::get_slot(const redis_result* rr,
size_t slot_max, size_t slot_min)
{
if (rr == NULL) {
return NULL;
}
size_t size;
const redis_result** children = rr->get_children(&size);
if (children == NULL || size < 2) {
return NULL;
}
char ip[128];
if (children[0]->argv_to_string(ip, sizeof(ip)) <= 0) {
return NULL;
}
int port = children[1]->get_integer();
if (port <= 0) {
return NULL;
}
redis_slot* slot = NEW redis_slot(slot_min, slot_max, ip, port);
return slot;
}
void redis_cluster::free_slots()
{
std::vector<redis_slot*>::iterator it;
for (it = slots_.begin(); it != slots_.end(); ++it) {
delete *it;
}
slots_.clear();
}
//////////////////////////////////////////////////////////////////////////
const std::vector<redis_node*>* redis_cluster::cluster_slaves(const char* node)
{
free_slaves();
const char* argv[3];
size_t lens[3];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "SLAVES";
lens[1] = sizeof("SLAVES") - 1;
argv[2] = node;
lens[2] = strlen(node);
build_request(3, argv, lens);
std::vector<string> lines;
if (get_strings(lines) < 0) {
return NULL;
}
std::vector<string>::iterator it = lines.begin();
for (; it != lines.end(); ++it) {
std::vector<string>& tokens = (*it).split2(" ");
if (tokens.size() < 3) {
continue;
}
char* node_type = tokens[2].c_str();
char* ptr = strchr(node_type, ',');
if (ptr != NULL && *(ptr + 1)) {
node_type = ptr + 1;
}
if (strcasecmp(node_type, "slave") != 0) {
continue;
}
redis_node* slave = get_slave(tokens);
if (slave != NULL) {
slaves_.push_back(slave);
}
}
return &slaves_;
}
redis_node* redis_cluster::get_slave(const std::vector<string>& tokens)
{
if (tokens.size() < 8) {
return NULL;
}
redis_node* node = NEW redis_node;
node->set_id(tokens[0].c_str());
node->set_addr(tokens[1].c_str());
node->set_myself(false);
node->set_connected(strcasecmp(tokens[7].c_str(), "connected") == 0);
node->set_master_id(tokens[3].c_str());
node->set_type("slave");
return node;
}
void redis_cluster::free_slaves()
{
std::vector<redis_node*>::iterator it = slaves_.begin();
for (; it != slaves_.end(); ++it) {
delete *it;
}
slaves_.clear();
}
const std::map<string, redis_node*>* redis_cluster::cluster_nodes()
{
free_masters();
const char* argv[2];
size_t lens[2];
argv[0] = "CLUSTER";
lens[0] = sizeof("CLUSTER") - 1;
argv[1] = "NODES";
lens[1] = sizeof("NODES") - 1;
build_request(2, argv, lens);
string buf;
if (get_string(buf) <= 0) {
return NULL;
}
std::vector<redis_node*> slaves;
acl::string line;
while (true) {
if (!buf.scan_line(line)) {
break;
}
redis_node* node = get_node(line);
if (node != NULL && !node->is_master()) {
slaves.push_back(node);
}
line.clear();
}
for (std::vector<redis_node*>::iterator it = slaves.begin();
it != slaves.end(); ++it) {
const char* id = (*it)->get_master_id();
std::map<string, redis_node*>::iterator it2 = masters_.find(id);
if (it2 != masters_.end()) {
it2->second->add_slave(*it);
} else {
logger_warn("delete orphan slave: %s", id);
delete *it;
}
}
return &masters_;
}
// for redis.3.x.x
// d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 127.0.0.1:16385 master - 0 1428410625374 73 connected 5461-10922
// 94e5d32cbcc9539cc1539078ca372094c14f9f49 127.0.0.1:16380 myself,master - 0 0 1 connected 0-9 11-5460
// e7b21f65e8d0d6e82dee026de29e499bb518db36 127.0.0.1:16381 slave d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 0 1428410625373 73 connected
// 6a78b47b2e150693fc2bed8578a7ca88b8f1e04c 127.0.0.1:16383 myself,slave 94e5d32cbcc9539cc1539078ca372094c14f9f49 0 0 4 connected
// 70a2cd8936a3d28d94b4915afd94ea69a596376a :16381 myself,master - 0 0 0 connected
// for redis.4.x.x
// d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 127.0.0.1:16385@116385 master - 0 1428410625374 73 connected 5461-10922
// 94e5d32cbcc9539cc1539078ca372094c14f9f49 127.0.0.1:16380@116380 myself,master - 0 0 1 connected 0-9 11-5460
// e7b21f65e8d0d6e82dee026de29e499bb518db36 127.0.0.1:16381@116381 slave d52ea3cb4cdf7294ac1fb61c696ae6483377bcfc 0 1428410625373 73 connected
// 6a78b47b2e150693fc2bed8578a7ca88b8f1e04c 127.0.0.1:16383@116383 myself,slave 94e5d32cbcc9539cc1539078ca372094c14f9f49 0 0 4 connected
// 70a2cd8936a3d28d94b4915afd94ea69a596376a :16381 myself,master - 0 0 0 connected
redis_node* redis_cluster::get_node(string& line)
{
std::vector<string>& tokens = line.split2(" ");
if (tokens.size() < 8) {
logger_warn("invalid tokens's size: %d < 8", (int) tokens.size());
return NULL;
}
bool myself = false;
char* node_type = tokens[2].c_str();
char* ptr = strchr(node_type, ',');
if (ptr != NULL && *(ptr + 1) != 0) {
*ptr++ = 0;
if (strcasecmp(node_type, "myself") == 0) {
myself = true;
}
node_type = ptr;
}
redis_node* node = NEW redis_node;
node->set_id(tokens[0].c_str());
node->set_addr(tokens[1].c_str());
node->set_myself(myself);
node->set_connected(strcasecmp(tokens[7].c_str(), "connected") == 0);
node->set_master_id(tokens[3].c_str());
if (strcasecmp(node_type, "master") == 0) {
node->set_master(node);
node->set_type("master");
masters_[tokens[0]] = node;
size_t n = tokens.size();
for (size_t i = 8; i < n; i++) {
add_slot_range(node, tokens[i].c_str());
}
} else if (strcasecmp(node_type, "slave") == 0) {
node->set_type("slave");
} else if (strcasecmp(node_type, "handshake") == 0) {
node->set_master(node);
node->set_type("handshake");
node->set_handshaking(true);
masters_[tokens[0]] = node;
size_t n = tokens.size();
for (size_t i = 8; i < n; i++) {
add_slot_range(node, tokens[i].c_str());
}
} else {
logger_warn("unknown node type: %s", node_type);
}
return node;
}
void redis_cluster::add_slot_range(redis_node* node, char* slots)
{
size_t slot_min, slot_max;
char* ptr = strchr(slots, '-');
if (ptr != NULL && *(ptr + 1) != 0) {
*ptr++ = 0;
slot_min = (size_t) atol(slots);
slot_max = (size_t) atol(ptr);
// xxx
if (slot_max < slot_min)
slot_max = slot_min;
} else {
slot_min = (size_t) atol(slots);
slot_max = slot_min;
}
node->add_slot_range(slot_min, slot_max);
}
void redis_cluster::free_masters()
{
std::map<string, redis_node*>::iterator it = masters_.begin();
for (; it != masters_.end(); ++it) {
it->second->clear_slaves(true);
delete it->second;
}
masters_.clear();
}
} // namespace acl
#endif // ACL_CLIENT_ONLY