-
Notifications
You must be signed in to change notification settings - Fork 173
/
hircluster.c
4964 lines (4045 loc) · 122 KB
/
hircluster.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
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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "fmacros.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "hircluster.h"
#include "hiutil.h"
#include "adlist.h"
#include "hiarray.h"
#include "command.h"
#include "dict.c"
#define REDIS_COMMAND_CLUSTER_NODES "CLUSTER NODES"
#define REDIS_COMMAND_CLUSTER_SLOTS "CLUSTER SLOTS"
#define REDIS_COMMAND_ASKING "ASKING"
#define REDIS_COMMAND_PING "PING"
#define REDIS_PROTOCOL_ASKING "*1\r\n$6\r\nASKING\r\n"
#define IP_PORT_SEPARATOR ":"
#define CLUSTER_ADDRESS_SEPARATOR ","
#define CLUSTER_DEFAULT_MAX_REDIRECT_COUNT 5
typedef struct cluster_async_data
{
redisClusterAsyncContext *acc;
struct cmd *command;
redisClusterCallbackFn *callback;
int retry_count;
void *privdata;
}cluster_async_data;
typedef enum CLUSTER_ERR_TYPE{
CLUSTER_NOT_ERR = 0,
CLUSTER_ERR_MOVED,
CLUSTER_ERR_ASK,
CLUSTER_ERR_TRYAGAIN,
CLUSTER_ERR_CROSSSLOT,
CLUSTER_ERR_CLUSTERDOWN,
CLUSTER_ERR_SENTINEL
}CLUSTER_ERR_TYPE;
static void cluster_node_deinit(cluster_node *node);
static void cluster_slot_destroy(cluster_slot *slot);
static void cluster_open_slot_destroy(copen_slot *oslot);
void listClusterNodeDestructor(void *val)
{
cluster_node_deinit(val);
hi_free(val);
}
void listClusterSlotDestructor(void *val)
{
cluster_slot_destroy(val);
}
unsigned int dictSdsHash(const void *key) {
return dictGenHashFunction((unsigned char*)key, sdslen((char*)key));
}
int dictSdsKeyCompare(void *privdata, const void *key1,
const void *key2)
{
int l1,l2;
DICT_NOTUSED(privdata);
l1 = sdslen((sds)key1);
l2 = sdslen((sds)key2);
if (l1 != l2) return 0;
return memcmp(key1, key2, l1) == 0;
}
void dictSdsDestructor(void *privdata, void *val)
{
DICT_NOTUSED(privdata);
sdsfree(val);
}
void dictClusterNodeDestructor(void *privdata, void *val)
{
DICT_NOTUSED(privdata);
cluster_node_deinit(val);
hi_free(val);
}
/* Cluster nodes hash table, mapping nodes
* name(437c719f50dc9d0745032f3b280ce7ecc40792ac)
* or addresses(1.2.3.4:6379) to clusterNode structures.
* Those nodes need destroy.
*/
dictType clusterNodesDictType = {
dictSdsHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCompare, /* key compare */
dictSdsDestructor, /* key destructor */
dictClusterNodeDestructor /* val destructor */
};
/* Cluster nodes hash table, mapping nodes
* name(437c719f50dc9d0745032f3b280ce7ecc40792ac)
* or addresses(1.2.3.4:6379) to clusterNode structures.
* Those nodes do not need destroy.
*/
dictType clusterNodesRefDictType = {
dictSdsHash, /* hash function */
NULL, /* key dup */
NULL, /* val dup */
dictSdsKeyCompare, /* key compare */
dictSdsDestructor, /* key destructor */
NULL /* val destructor */
};
void listCommandFree(void *command)
{
struct cmd *cmd = command;
command_destroy(cmd);
}
/* Defined in hiredis.c */
void __redisSetError(redisContext *c, int type, const char *str);
/* Forward declaration of function in hiredis.c */
int __redisAppendCommand(redisContext *c, const char *cmd, size_t len);
/* Helper function for the redisClusterCommand* family of functions.
*
* Write a formatted command to the output buffer. If the given context is
* blocking, immediately read the reply into the "reply" pointer. When the
* context is non-blocking, the "reply" pointer will not be used and the
* command is simply appended to the write buffer.
*
* Returns the reply when a reply was succesfully retrieved. Returns NULL
* otherwise. When NULL is returned in a blocking context, the error field
* in the context will be set.
*/
static void *__redisBlockForReply(redisContext *c) {
void *reply;
if (c->flags & REDIS_BLOCK) {
if (redisGetReply(c,&reply) != REDIS_OK)
return NULL;
return reply;
}
return NULL;
}
/* -----------------------------------------------------------------------------
* Key space handling
* -------------------------------------------------------------------------- */
/* We have 16384 hash slots. The hash slot of a given key is obtained
* as the least significant 14 bits of the crc16 of the key.
*
* However if the key contains the {...} pattern, only the part between
* { and } is hashed. This may be useful in the future to force certain
* keys to be in the same node (assuming no resharding is in progress). */
static unsigned int keyHashSlot(char *key, int keylen) {
int s, e; /* start-end indexes of { and } */
for (s = 0; s < keylen; s++)
if (key[s] == '{') break;
/* No '{' ? Hash the whole key. This is the base case. */
if (s == keylen) return crc16(key,keylen) & 0x3FFF;
/* '{' found? Check if we have the corresponding '}'. */
for (e = s+1; e < keylen; e++)
if (key[e] == '}') break;
/* No '}' or nothing betweeen {} ? Hash the whole key. */
if (e == keylen || e == s+1) return crc16(key,keylen) & 0x3FFF;
/* If we are here there is both a { and a } on its right. Hash
* what is in the middle between { and }. */
return crc16(key+s+1,e-s-1) & 0x3FFF;
}
static void __redisClusterSetError(redisClusterContext *cc, int type, const char *str) {
size_t len;
if(cc == NULL){
return;
}
cc->err = type;
if (str != NULL) {
len = strlen(str);
len = len < (sizeof(cc->errstr)-1) ? len : (sizeof(cc->errstr)-1);
memcpy(cc->errstr,str,len);
cc->errstr[len] = '\0';
} else {
/* Only REDIS_ERR_IO may lack a description! */
assert(type == REDIS_ERR_IO);
__redis_strerror_r(errno, cc->errstr, sizeof(cc->errstr));
}
}
static int cluster_reply_error_type(redisReply *reply)
{
if(reply == NULL)
{
return REDIS_ERR;
}
if(reply->type == REDIS_REPLY_ERROR)
{
if((int)strlen(REDIS_ERROR_MOVED) < reply->len &&
strncmp(reply->str, REDIS_ERROR_MOVED, strlen(REDIS_ERROR_MOVED)) == 0)
{
return CLUSTER_ERR_MOVED;
}
else if((int)strlen(REDIS_ERROR_ASK) < reply->len &&
strncmp(reply->str, REDIS_ERROR_ASK, strlen(REDIS_ERROR_ASK)) == 0)
{
return CLUSTER_ERR_ASK;
}
else if((int)strlen(REDIS_ERROR_TRYAGAIN) < reply->len &&
strncmp(reply->str, REDIS_ERROR_TRYAGAIN, strlen(REDIS_ERROR_TRYAGAIN)) == 0)
{
return CLUSTER_ERR_TRYAGAIN;
}
else if((int)strlen(REDIS_ERROR_CROSSSLOT) < reply->len &&
strncmp(reply->str, REDIS_ERROR_CROSSSLOT, strlen(REDIS_ERROR_CROSSSLOT)) == 0)
{
return CLUSTER_ERR_CROSSSLOT;
}
else if((int)strlen(REDIS_ERROR_CLUSTERDOWN) < reply->len &&
strncmp(reply->str, REDIS_ERROR_CLUSTERDOWN, strlen(REDIS_ERROR_CLUSTERDOWN)) == 0)
{
return CLUSTER_ERR_CLUSTERDOWN;
}
else
{
return CLUSTER_ERR_SENTINEL;
}
}
return CLUSTER_NOT_ERR;
}
static int cluster_node_init(cluster_node *node)
{
if(node == NULL){
return REDIS_ERR;
}
node->name = NULL;
node->addr = NULL;
node->host = NULL;
node->port = 0;
node->role = REDIS_ROLE_NULL;
node->myself = 0;
node->slaves = NULL;
node->con = NULL;
node->acon = NULL;
node->slots = NULL;
node->failure_count = 0;
node->data = NULL;
node->migrating = NULL;
node->importing = NULL;
return REDIS_OK;
}
static void cluster_node_deinit(cluster_node *node)
{
copen_slot **oslot;
if(node == NULL)
{
return;
}
sdsfree(node->name);
sdsfree(node->addr);
sdsfree(node->host);
node->port = 0;
node->role = REDIS_ROLE_NULL;
node->myself = 0;
if(node->con != NULL)
{
redisFree(node->con);
}
if(node->acon != NULL)
{
redisAsyncFree(node->acon);
}
if(node->slots != NULL)
{
listRelease(node->slots);
}
if(node->slaves != NULL)
{
listRelease(node->slaves);
}
if(node->migrating)
{
while(hiarray_n(node->migrating))
{
oslot = hiarray_pop(node->migrating);
cluster_open_slot_destroy(*oslot);
}
hiarray_destroy(node->migrating);
node->migrating = NULL;
}
if(node->importing)
{
while(hiarray_n(node->importing))
{
oslot = hiarray_pop(node->importing);
cluster_open_slot_destroy(*oslot);
}
hiarray_destroy(node->importing);
node->importing = NULL;
}
}
static int cluster_slot_init(cluster_slot *slot, cluster_node *node)
{
slot->start = 0;
slot->end = 0;
slot->node = node;
return REDIS_OK;
}
static cluster_slot *cluster_slot_create(cluster_node *node)
{
cluster_slot *slot;
slot = hi_alloc(sizeof(*slot));
if(slot == NULL){
return NULL;
}
cluster_slot_init(slot, node);
if(node != NULL){
ASSERT(node->role == REDIS_ROLE_MASTER);
if(node->slots == NULL){
node->slots = listCreate();
if(node->slots == NULL)
{
cluster_slot_destroy(slot);
return NULL;
}
node->slots->free = listClusterSlotDestructor;
}
listAddNodeTail(node->slots, slot);
}
return slot;
}
static int cluster_slot_ref_node(cluster_slot * slot, cluster_node *node)
{
if(slot == NULL || node == NULL){
return REDIS_ERR;
}
if(node->role != REDIS_ROLE_MASTER){
return REDIS_ERR;
}
if(node->slots == NULL){
node->slots = listCreate();
if(node->slots == NULL)
{
return REDIS_ERR;
}
node->slots->free = listClusterSlotDestructor;
}
listAddNodeTail(node->slots, slot);
slot->node = node;
return REDIS_OK;
}
static void cluster_slot_destroy(cluster_slot *slot)
{
slot->start = 0;
slot->end = 0;
slot->node = NULL;
hi_free(slot);
}
static copen_slot *cluster_open_slot_create(uint32_t slot_num, int migrate,
sds remote_name, cluster_node *node)
{
copen_slot *oslot;
oslot = hi_alloc(sizeof(*oslot));
if(oslot == NULL){
return NULL;
}
oslot->slot_num = 0;
oslot->migrate = 0;
oslot->node = NULL;
oslot->remote_name = NULL;
oslot->slot_num = slot_num;
oslot->migrate = migrate;
oslot->node = node;
oslot->remote_name = sdsdup(remote_name);
return oslot;
}
static void cluster_open_slot_destroy(copen_slot *oslot)
{
oslot->slot_num = 0;
oslot->migrate = 0;
oslot->node = NULL;
if(oslot->remote_name != NULL){
sdsfree(oslot->remote_name);
oslot->remote_name = NULL;
}
hi_free(oslot);
}
/**
* Return a new node with the "cluster slots" command reply.
*/
static cluster_node *node_get_with_slots(
redisClusterContext *cc, redisReply *host_elem,
redisReply *port_elem, uint8_t role)
{
cluster_node *node = NULL;
if(host_elem == NULL || port_elem == NULL){
return NULL;
}
if(host_elem->type != REDIS_REPLY_STRING ||
host_elem->len <= 0){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"node ip is not string.");
goto error;
}
if(port_elem->type != REDIS_REPLY_INTEGER ||
port_elem->integer <= 0){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"node port is not integer.");
goto error;
}
if(!hi_valid_port((int)port_elem->integer)){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"node port is not valid.");
goto error;
}
node = hi_alloc(sizeof(cluster_node));
if(node == NULL){
__redisClusterSetError(cc,
REDIS_ERR_OOM,"Out of memory");
goto error;
}
cluster_node_init(node);
if(role == REDIS_ROLE_MASTER){
node->slots = listCreate();
if(node->slots == NULL){
hi_free(node);
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"slots for node listCreate error");
goto error;
}
node->slots->free = listClusterSlotDestructor;
}
node->name = NULL;
node->addr = sdsnewlen(host_elem->str, host_elem->len);
node->addr = sdscatfmt(node->addr, ":%i", port_elem->integer);
node->host = sdsnewlen(host_elem->str, host_elem->len);
node->port = (int)port_elem->integer;
node->role = role;
return node;
error:
if(node != NULL){
hi_free(node);
}
return NULL;
}
/**
* Return a new node with the "cluster nodes" command reply.
*/
static cluster_node *node_get_with_nodes(
redisClusterContext *cc,
sds *node_infos, int info_count, uint8_t role)
{
sds *ip_port = NULL;
int count_ip_port = 0;
cluster_node *node;
if(info_count < 8)
{
return NULL;
}
node = hi_alloc(sizeof(cluster_node));
if(node == NULL)
{
__redisClusterSetError(cc,
REDIS_ERR_OOM,"Out of memory");
goto error;
}
cluster_node_init(node);
if(role == REDIS_ROLE_MASTER)
{
node->slots = listCreate();
if(node->slots == NULL)
{
hi_free(node);
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"slots for node listCreate error");
goto error;
}
node->slots->free = listClusterSlotDestructor;
}
node->name = node_infos[0];
node->addr = node_infos[1];
ip_port = sdssplitlen(node_infos[1], sdslen(node_infos[1]),
IP_PORT_SEPARATOR, strlen(IP_PORT_SEPARATOR), &count_ip_port);
if(ip_port == NULL || count_ip_port != 2)
{
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"split ip port error");
goto error;
}
node->host = ip_port[0];
node->port = hi_atoi(ip_port[1], sdslen(ip_port[1]));
node->role = role;
sdsfree(ip_port[1]);
free(ip_port);
node_infos[0] = NULL;
node_infos[1] = NULL;
return node;
error:
if(ip_port != NULL)
{
sdsfreesplitres(ip_port, count_ip_port);
}
if(node != NULL)
{
hi_free(node);
}
return NULL;
}
static void cluster_nodes_swap_ctx(dict *nodes_f, dict *nodes_t)
{
dictIterator *di;
dictEntry *de_f, *de_t;
cluster_node *node_f, *node_t;
redisContext *c;
redisAsyncContext *ac;
if(nodes_f == NULL || nodes_t == NULL){
return;
}
di = dictGetIterator(nodes_t);
while((de_t = dictNext(di)) != NULL){
node_t = dictGetEntryVal(de_t);
if(node_t == NULL){
continue;
}
de_f = dictFind(nodes_f, node_t->addr);
if(de_f == NULL){
continue;
}
node_f = dictGetEntryVal(de_f);
if(node_f->con != NULL){
c = node_f->con;
node_f->con = node_t->con;
node_t->con = c;
}
if(node_f->acon != NULL){
ac = node_f->acon;
node_f->acon = node_t->acon;
node_t->acon = ac;
node_t->acon->data = node_t;
if (node_f->acon)
node_f->acon->data = node_f;
}
}
dictReleaseIterator(di);
}
static int
cluster_slot_start_cmp(const void *t1, const void *t2)
{
const cluster_slot **s1 = t1, **s2 = t2;
return (*s1)->start > (*s2)->start?1:-1;
}
static int
cluster_master_slave_mapping_with_name(redisClusterContext *cc,
dict **nodes, cluster_node *node, sds master_name)
{
int ret;
dictEntry *di;
cluster_node *node_old;
listNode *lnode;
if(node == NULL || master_name == NULL)
{
return REDIS_ERR;
}
if(*nodes == NULL)
{
*nodes = dictCreate(
&clusterNodesRefDictType, NULL);
}
di = dictFind(*nodes, master_name);
if(di == NULL)
{
ret = dictAdd(*nodes,
sdsnewlen(master_name, sdslen(master_name)), node);
if(ret != DICT_OK)
{
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"the address already exists in the nodes");
return REDIS_ERR;
}
}
else
{
node_old = dictGetEntryVal(di);
if(node_old == NULL)
{
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"dict get value null");
return REDIS_ERR;
}
if(node->role == REDIS_ROLE_MASTER &&
node_old->role == REDIS_ROLE_MASTER)
{
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"two masters have the same name");
return REDIS_ERR;
}
else if(node->role == REDIS_ROLE_MASTER
&& node_old->role == REDIS_ROLE_SLAVE)
{
if(node->slaves == NULL)
{
node->slaves = listCreate();
if(node->slaves == NULL)
{
__redisClusterSetError(cc,REDIS_ERR_OOM,
"Out of memory");
return REDIS_ERR;
}
node->slaves->free =
listClusterNodeDestructor;
}
if(node_old->slaves != NULL)
{
node_old->slaves->free = NULL;
while(listLength(node_old->slaves) > 0)
{
lnode = listFirst(node_old->slaves);
listAddNodeHead(node->slaves, lnode->value);
listDelNode(node_old->slaves, lnode);
}
listRelease(node_old->slaves);
node_old->slaves = NULL;
}
listAddNodeHead(node->slaves, node_old);
dictSetHashVal(*nodes, di, node);
}
else if(node->role == REDIS_ROLE_SLAVE)
{
if(node_old->slaves == NULL)
{
node_old->slaves = listCreate();
if(node_old->slaves == NULL)
{
__redisClusterSetError(cc,REDIS_ERR_OOM,
"Out of memory");
return REDIS_ERR;
}
node_old->slaves->free =
listClusterNodeDestructor;
}
listAddNodeTail(node_old->slaves, node);
}
else
{
NOT_REACHED();
}
}
return REDIS_OK;
}
/**
* Parse the "cluster slots" command reply to nodes dict.
*/
dict *
parse_cluster_slots(redisClusterContext *cc,
redisReply *reply, int flags)
{
int ret;
cluster_slot *slot = NULL;
dict *nodes = NULL;
dictEntry *den;
redisReply *elem_slots;
redisReply *elem_slots_begin, *elem_slots_end;
redisReply *elem_nodes;
redisReply *elem_ip, *elem_port;
cluster_node *master = NULL, *slave;
sds address;
uint32_t i, idx;
if(reply == NULL){
return NULL;
}
nodes = dictCreate(&clusterNodesDictType, NULL);
if(nodes == NULL){
__redisClusterSetError(cc,REDIS_ERR_OOM,
"out of memory");
goto error;
}
if(reply->type != REDIS_REPLY_ARRAY || reply->elements <= 0){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"reply is not an array.");
goto error;
}
for(i = 0; i < reply->elements; i ++){
elem_slots = reply->element[i];
if(elem_slots->type != REDIS_REPLY_ARRAY ||
elem_slots->elements < 3){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"first sub_reply is not an array.");
goto error;
}
slot = cluster_slot_create(NULL);
if(slot == NULL){
__redisClusterSetError(cc, REDIS_ERR_OOM,
"Slot create failed: out of memory.");
goto error;
}
//one slots region
for(idx = 0; idx < elem_slots->elements; idx ++){
if(idx == 0){
elem_slots_begin = elem_slots->element[idx];
if(elem_slots_begin->type != REDIS_REPLY_INTEGER){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"slot begin is not an integer.");
goto error;
}
slot->start = (int)(elem_slots_begin->integer);
}else if(idx == 1){
elem_slots_end = elem_slots->element[idx];
if(elem_slots_end->type != REDIS_REPLY_INTEGER){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"slot end is not an integer.");
goto error;
}
slot->end = (int)(elem_slots_end->integer);
if(slot->start > slot->end){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"slot begin is bigger than slot end.");
goto error;
}
}else{
elem_nodes = elem_slots->element[idx];
if(elem_nodes->type != REDIS_REPLY_ARRAY ||
elem_nodes->elements != 3){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"nodes sub_reply is not an correct array.");
goto error;
}
elem_ip = elem_nodes->element[0];
elem_port = elem_nodes->element[1];
if(elem_ip == NULL || elem_port == NULL ||
elem_ip->type != REDIS_REPLY_STRING ||
elem_port->type != REDIS_REPLY_INTEGER){
__redisClusterSetError(cc, REDIS_ERR_OTHER,
"Command(cluster slots) reply error: "
"master ip or port is not correct.");
goto error;
}
//this is master.
if(idx == 2){
address = sdsnewlen(elem_ip->str, elem_ip->len);
address = sdscatfmt(address, ":%i", elem_port->integer);
den = dictFind(nodes, address);
//master already exits, break to the next slots region.
if(den != NULL){
sdsfree(address);
master = dictGetEntryVal(den);
ret = cluster_slot_ref_node(slot, master);
if(ret != REDIS_OK){
__redisClusterSetError(cc, REDIS_ERR_OOM,
"Slot ref node failed: out of memory.");
goto error;
}
slot = NULL;
break;
}
sdsfree(address);
master = node_get_with_slots(cc, elem_ip,
elem_port, REDIS_ROLE_MASTER);
if(master == NULL){
goto error;
}
ret = dictAdd(nodes,
sdsnewlen(master->addr, sdslen(master->addr)), master);
if(ret != DICT_OK){
__redisClusterSetError(cc,REDIS_ERR_OTHER,
"The address already exists in the nodes");
cluster_node_deinit(master);
hi_free(master);
goto error;
}
ret = cluster_slot_ref_node(slot, master);
if(ret != REDIS_OK){
__redisClusterSetError(cc, REDIS_ERR_OOM,
"Slot ref node failed: out of memory.");
goto error;
}
slot = NULL;
}else if(flags & HIRCLUSTER_FLAG_ADD_SLAVE){
slave = node_get_with_slots(cc, elem_ip,
elem_port, REDIS_ROLE_SLAVE);
if(slave == NULL){
goto error;
}
if(master->slaves == NULL){
master->slaves = listCreate();
if(master->slaves == NULL){
__redisClusterSetError(cc,REDIS_ERR_OOM,
"Out of memory");
cluster_node_deinit(slave);
goto error;
}
master->slaves->free =
listClusterNodeDestructor;
}
listAddNodeTail(master->slaves, slave);
}
}
}
}
return nodes;
error:
if(nodes != NULL){
dictRelease(nodes);
}
if(slot != NULL){
cluster_slot_destroy(slot);
}
return NULL;
}
/**
* Parse the "cluster nodes" command reply to nodes dict.
*/
dict *
parse_cluster_nodes(redisClusterContext *cc,
char *str, int str_len, int flags)
{
int ret;
dict *nodes = NULL;
dict *nodes_name = NULL;
cluster_node *master, *slave;
cluster_slot *slot;
char *pos, *start, *end, *line_start, *line_end;
char *role;
int role_len;
uint8_t myself = 0;
int slot_start, slot_end;
sds *part = NULL, *slot_start_end = NULL;
int count_part = 0, count_slot_start_end = 0;
int k;
int len;
nodes = dictCreate(&clusterNodesDictType, NULL);
if(nodes == NULL){
__redisClusterSetError(cc,REDIS_ERR_OOM,
"out of memory");
goto error;
}
start = str;
end = start + str_len;
line_start = start;
for(pos = start; pos < end; pos ++){
if(*pos == '\n'){
line_end = pos - 1;
len = line_end - line_start;