forked from phpredis/phpredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis_cluster.c
3055 lines (2594 loc) · 95.6 KB
/
redis_cluster.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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2009 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Michael Grunder <michael.grunder@gmail.com> |
| Maintainer: Nicolas Favre-Felix <n.favre-felix@owlient.eu> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "common.h"
#include "php_redis.h"
#include "ext/standard/info.h"
#include "crc16.h"
#include "redis_cluster.h"
#include "redis_commands.h"
#include <zend_exceptions.h>
#include "library.h"
#include <php_variables.h>
#include <SAPI.h>
zend_class_entry *redis_cluster_ce;
/* Exception handler */
zend_class_entry *redis_cluster_exception_ce;
static zend_class_entry *spl_rte_ce = NULL;
/* Handlers for RedisCluster */
zend_object_handlers RedisCluster_handlers;
/* Argument info for HSCAN, SSCAN, HSCAN */
ZEND_BEGIN_ARG_INFO_EX(arginfo_kscan_cl, 0, 0, 2)
ZEND_ARG_INFO(0, str_key)
ZEND_ARG_INFO(1, i_iterator)
ZEND_ARG_INFO(0, str_pattern)
ZEND_ARG_INFO(0, i_count)
ZEND_END_ARG_INFO();
/* Argument infor for SCAN */
ZEND_BEGIN_ARG_INFO_EX(arginfo_scan_cl, 0, 0, 2)
ZEND_ARG_INFO(1, i_iterator)
ZEND_ARG_INFO(0, str_node)
ZEND_ARG_INFO(0, str_pattern)
ZEND_ARG_INFO(0, i_count)
ZEND_END_ARG_INFO();
/* Function table */
zend_function_entry redis_cluster_functions[] = {
PHP_ME(RedisCluster, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, close, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, set, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, mget, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, mset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, msetnx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, del, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, setex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, psetex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, setnx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, exists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, keys, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, type, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lpop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rpop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, spop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lpush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rpush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, blpop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, brpop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rpushx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lpushx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, linsert, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lindex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lrem, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, brpoplpush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rpoplpush, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, llen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, scard, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, smembers, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sismember, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sadd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, saddarray, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, srem, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sunion, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sunionstore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sinter, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sinterstore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sdiff, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sdiffstore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, srandmember, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, strlen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, persist, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, ttl, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pttl, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zcard, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zcount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zremrangebyscore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zscore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zadd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zincrby, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hlen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hkeys, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hvals, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hget, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hgetall, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hexists, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hincrby, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hsetnx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hmget, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hmset, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hdel, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hincrbyfloat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hstrlen, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, dump, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrevrank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, incr, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, decr, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, incrby, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, decrby, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, incrbyfloat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, expire, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pexpire, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, expireat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pexpireat, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, append, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getbit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, setbit, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, bitop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, bitpos, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, bitcount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lget, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getrange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, ltrim, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lrange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zremrangebyrank, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, publish, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rename, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, renamenx, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pfcount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pfadd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pfmerge, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, setrange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, restore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, smove, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrevrange, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrangebyscore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrevrangebyscore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrangebylex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrevrangebylex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zlexcount, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zremrangebylex, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zunionstore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zinterstore, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zrem, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sort, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, object, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, subscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, psubscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, unsubscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, punsubscribe, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, eval, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, evalsha, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, scan, arginfo_scan_cl, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, sscan, arginfo_kscan_cl, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, zscan, arginfo_kscan_cl, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, hscan, arginfo_kscan_cl, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getmode, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getlasterror, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, clearlasterror, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, getoption, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, setoption, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, _prefix, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, _serialize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, _unserialize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, _masters, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, _redir, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, multi, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, exec, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, discard, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, watch, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, unwatch, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, save, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, bgsave, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, flushdb, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, flushall, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, dbsize, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, bgrewriteaof, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, lastsave, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, info, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, role, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, time, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, randomkey, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, ping, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, echo, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, command, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, rawcommand, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, cluster, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, client, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, config, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, pubsub, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, script, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, slowlog, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, geoadd, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, geohash, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, geopos, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, geodist, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, georadius, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RedisCluster, georadiusbymember, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
/* Our context seeds will be a hash table with RedisSock* pointers */
#if (PHP_MAJOR_VERSION < 7)
static void ht_free_seed(void *data)
#else
static void ht_free_seed(zval *data)
#endif
{
RedisSock *redis_sock = *(RedisSock**)data;
if(redis_sock) redis_free_socket(redis_sock);
}
/* Free redisClusterNode objects we've stored */
#if (PHP_MAJOR_VERSION < 7)
static void ht_free_node(void *data)
#else
static void ht_free_node(zval *data)
#endif
{
redisClusterNode *node = *(redisClusterNode**)data;
cluster_free_node(node);
}
/* Initialize/Register our RedisCluster exceptions */
PHPAPI zend_class_entry *rediscluster_get_exception_base(int root TSRMLS_DC) {
#if HAVE_SPL
if(!root) {
if(!spl_rte_ce) {
zend_class_entry *pce;
if ((pce = zend_hash_str_find_ptr(CG(class_table), "runtimeexception", sizeof("runtimeexception") - 1))) {
spl_rte_ce = pce;
return pce;
}
} else {
return spl_rte_ce;
}
}
#endif
#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 2)
return zend_exception_get_default();
#else
return zend_exception_get_default(TSRMLS_C);
#endif
}
/* Create redisCluster context */
#if (PHP_MAJOR_VERSION < 7)
zend_object_value
create_cluster_context(zend_class_entry *class_type TSRMLS_DC) {
redisCluster *cluster;
// Allocate our actual struct
cluster = ecalloc(1, sizeof(redisCluster));
#else
zend_object *
create_cluster_context(zend_class_entry *class_type TSRMLS_DC) {
redisCluster *cluster;
// Allocate our actual struct
cluster = ecalloc(1, sizeof(redisCluster) + sizeof(zval) * (class_type->default_properties_count - 1));
#endif
// We're not currently subscribed anywhere
cluster->subscribed_slot = -1;
// Allocate our RedisSock we'll use to store prefix/serialization flags
cluster->flags = ecalloc(1, sizeof(RedisSock));
// Allocate our hash table for seeds
ALLOC_HASHTABLE(cluster->seeds);
zend_hash_init(cluster->seeds, 0, NULL, ht_free_seed, 0);
// Allocate our hash table for connected Redis objects
ALLOC_HASHTABLE(cluster->nodes);
zend_hash_init(cluster->nodes, 0, NULL, ht_free_node, 0);
// Initialize it
zend_object_std_init(&cluster->std, class_type TSRMLS_CC);
#if (PHP_MAJOR_VERSION < 7)
zend_object_value retval;
#if PHP_VERSION_ID < 50399
zval *tmp;
zend_hash_copy(cluster->std.properties, &class_type->default_properties,
(copy_ctor_func_t)zval_add_ref, (void*)&tmp, sizeof(zval*));
#endif
retval.handle = zend_objects_store_put(cluster,
(zend_objects_store_dtor_t)zend_objects_destroy_object,
free_cluster_context, NULL TSRMLS_CC);
retval.handlers = zend_get_std_object_handlers();
return retval;
#else
object_properties_init(&cluster->std, class_type);
memcpy(&RedisCluster_handlers, zend_get_std_object_handlers(), sizeof(RedisCluster_handlers));
RedisCluster_handlers.offset = XtOffsetOf(redisCluster, std);
RedisCluster_handlers.free_obj = free_cluster_context;
cluster->std.handlers = &RedisCluster_handlers;
return &cluster->std;
#endif
}
/* Free redisCluster context */
void
#if (PHP_MAJOR_VERSION < 7)
free_cluster_context(void *object TSRMLS_DC) {
redisCluster *cluster = (redisCluster*)object;
#else
free_cluster_context(zend_object *object) {
redisCluster *cluster = (redisCluster*)((char*)(object) - XtOffsetOf(redisCluster, std));
#endif
// Free any allocated prefix, as well as the struct
if(cluster->flags->prefix) efree(cluster->flags->prefix);
efree(cluster->flags);
// Free seeds HashTable itself
zend_hash_destroy(cluster->seeds);
efree(cluster->seeds);
// Destroy all Redis objects and free our nodes HashTable
zend_hash_destroy(cluster->nodes);
efree(cluster->nodes);
if(cluster->err) efree(cluster->err);
zend_object_std_dtor(&cluster->std TSRMLS_CC);
#if (PHP_MAJOR_VERSION < 7)
efree(cluster);
#endif
}
/* Attempt to connect to a Redis cluster provided seeds and timeout options */
void redis_cluster_init(redisCluster *c, HashTable *ht_seeds, double timeout,
double read_timeout, int persistent TSRMLS_DC)
{
// Validate timeout
if(timeout < 0L || timeout > INT_MAX) {
zend_throw_exception(redis_cluster_exception_ce,
"Invalid timeout", 0 TSRMLS_CC);
}
// Validate our read timeout
if(read_timeout < 0L || read_timeout > INT_MAX) {
zend_throw_exception(redis_cluster_exception_ce,
"Invalid read timeout", 0 TSRMLS_CC);
}
/* Make sure there are some seeds */
if(zend_hash_num_elements(ht_seeds)==0) {
zend_throw_exception(redis_cluster_exception_ce,
"Must pass seeds", 0 TSRMLS_CC);
}
/* Set our timeout and read_timeout which we'll pass through to the
* socket type operations */
c->timeout = timeout;
c->read_timeout = read_timeout;
/* Set our option to use or not use persistent connections */
c->persistent = persistent;
/* Calculate the number of miliseconds we will wait when bouncing around,
* (e.g. a node goes down), which is not the same as a standard timeout. */
c->waitms = (long)(timeout * 1000);
// Initialize our RedisSock "seed" objects
cluster_init_seeds(c, ht_seeds);
// Create and map our key space
cluster_map_keyspace(c TSRMLS_CC);
}
/* Attempt to load a named cluster configured in php.ini */
void redis_cluster_load(redisCluster *c, char *name, int name_len TSRMLS_DC) {
zval z_seeds, z_timeout, z_read_timeout, z_persistent, *z_value;
char *iptr;
double timeout=0, read_timeout=0;
int persistent = 0;
HashTable *ht_seeds = NULL;
/* Seeds */
array_init(&z_seeds);
if ((iptr = INI_STR("redis.clusters.seeds")) != NULL) {
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_seeds TSRMLS_CC);
}
if ((z_value = zend_hash_str_find(Z_ARRVAL(z_seeds), name, name_len)) != NULL) {
ht_seeds = Z_ARRVAL_P(z_value);
} else {
zval_dtor(&z_seeds);
zend_throw_exception(redis_cluster_exception_ce, "Couldn't find seeds for cluster", 0 TSRMLS_CC);
return;
}
/* Connection timeout */
array_init(&z_timeout);
if ((iptr = INI_STR("redis.clusters.timeout")) != NULL) {
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_timeout TSRMLS_CC);
}
if ((z_value = zend_hash_str_find(Z_ARRVAL(z_timeout), name, name_len)) != NULL) {
if (Z_TYPE_P(z_value) == IS_STRING) {
timeout = atof(Z_STRVAL_P(z_value));
} else if (Z_TYPE_P(z_value) == IS_DOUBLE) {
timeout = Z_DVAL_P(z_value);
}
}
/* Read timeout */
array_init(&z_read_timeout);
if ((iptr = INI_STR("redis.clusters.read_timeout")) != NULL) {
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_read_timeout TSRMLS_CC);
}
if ((z_value = zend_hash_str_find(Z_ARRVAL(z_read_timeout), name, name_len)) != NULL) {
if (Z_TYPE_P(z_value) == IS_STRING) {
read_timeout = atof(Z_STRVAL_P(z_value));
} else if (Z_TYPE_P(z_value) == IS_DOUBLE) {
read_timeout = Z_DVAL_P(z_value);
}
}
/* Persistent connections */
array_init(&z_persistent);
if ((iptr = INI_STR("redis.clusters.persistent")) != NULL) {
sapi_module.treat_data(PARSE_STRING, estrdup(iptr), &z_persistent TSRMLS_CC);
}
if ((z_value = zend_hash_str_find(Z_ARRVAL(z_persistent), name, name_len)) != NULL) {
if (Z_TYPE_P(z_value) == IS_STRING) {
persistent = atoi(Z_STRVAL_P(z_value));
} else if (Z_TYPE_P(z_value) == IS_LONG) {
persistent = Z_LVAL_P(z_value);
}
}
/* Attempt to create/connect to the cluster */
redis_cluster_init(c, ht_seeds, timeout, read_timeout, persistent TSRMLS_CC);
/* Clean up our arrays */
zval_dtor(&z_seeds);
zval_dtor(&z_timeout);
zval_dtor(&z_read_timeout);
zval_dtor(&z_persistent);
}
/*
* PHP Methods
*/
/* Create a RedisCluster Object */
PHP_METHOD(RedisCluster, __construct) {
zval *object, *z_seeds=NULL;
char *name;
strlen_t name_len;
double timeout = 0.0, read_timeout = 0.0;
zend_bool persistent = 0;
redisCluster *context = GET_CONTEXT();
// Parse arguments
if(zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(),
"Os|addb", &object, redis_cluster_ce, &name,
&name_len, &z_seeds, &timeout,
&read_timeout, &persistent)==FAILURE)
{
RETURN_FALSE;
}
// Require a name
if(name_len == 0 && ZEND_NUM_ARGS() < 2) {
zend_throw_exception(redis_cluster_exception_ce,
"You must specify a name or pass seeds!",
0 TSRMLS_CC);
}
/* If we've been passed only one argument, the user is attempting to connect
* to a named cluster, stored in php.ini, otherwise we'll need manual seeds */
if (ZEND_NUM_ARGS() > 1) {
redis_cluster_init(context, Z_ARRVAL_P(z_seeds), timeout, read_timeout,
persistent TSRMLS_CC);
} else {
redis_cluster_load(context, name, name_len TSRMLS_CC);
}
}
/*
* RedisCluster method implementation
*/
/* {{{ proto bool RedisCluster::close() */
PHP_METHOD(RedisCluster, close) {
cluster_disconnect(GET_CONTEXT() TSRMLS_CC);
RETURN_TRUE;
}
/* {{{ proto string RedisCluster::get(string key) */
PHP_METHOD(RedisCluster, get) {
CLUSTER_PROCESS_KW_CMD("GET", redis_key_cmd, cluster_bulk_resp, 1);
}
/* }}} */
/* {{{ proto bool RedisCluster::set(string key, string value) */
PHP_METHOD(RedisCluster, set) {
CLUSTER_PROCESS_CMD(set, cluster_bool_resp, 0);
}
/* }}} */
/* Generic handler for MGET/MSET/MSETNX */
static int
distcmd_resp_handler(INTERNAL_FUNCTION_PARAMETERS, redisCluster *c, short slot,
clusterMultiCmd *mc, zval *z_ret, int last, cluster_cb cb)
{
clusterMultiCtx *ctx;
// Finalize multi command
cluster_multi_fini(mc);
// Spin up multi context
ctx = emalloc(sizeof(clusterMultiCtx));
ctx->z_multi = z_ret;
ctx->count = mc->argc;
ctx->last = last;
// Attempt to send the command
if(cluster_send_command(c,slot,mc->cmd.c,mc->cmd.len TSRMLS_CC)<0 ||
c->err!=NULL)
{
cluster_multi_free(mc);
zval_dtor(z_ret);
efree(ctx);
return -1;
}
if(CLUSTER_IS_ATOMIC(c)) {
// Process response now
cb(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, (void*)ctx);
} else {
CLUSTER_ENQUEUE_RESPONSE(c, slot, cb, ctx);
}
// Clear out our command but retain allocated memory
CLUSTER_MULTI_CLEAR(mc);
return 0;
}
/* Container struct for a key/value pair pulled from an array */
typedef struct clusterKeyValHT {
char kbuf[22];
char *key;
strlen_t key_len;
int key_free;
short slot;
char *val;
strlen_t val_len;
int val_free;
} clusterKeyValHT;
/* Helper to pull a key/value pair from a HashTable */
static int get_key_val_ht(redisCluster *c, HashTable *ht, HashPosition *ptr,
clusterKeyValHT *kv TSRMLS_DC)
{
zval *z_val;
zend_ulong idx;
// Grab the key, convert it to a string using provided kbuf buffer if it's
// a LONG style key
#if (PHP_MAJOR_VERSION < 7)
uint key_len;
switch(zend_hash_get_current_key_ex(ht, &(kv->key), &key_len, &idx, 0, ptr)) {
case HASH_KEY_IS_STRING:
kv->key_len = (int)(key_len-1);
#else
zend_string *zkey;
switch (zend_hash_get_current_key_ex(ht, &zkey, &idx, ptr)) {
case HASH_KEY_IS_STRING:
kv->key_len = zkey->len;
kv->key = zkey->val;
#endif
break;
case HASH_KEY_IS_LONG:
kv->key_len = snprintf(kv->kbuf,sizeof(kv->kbuf),"%ld",(long)idx);
kv->key = kv->kbuf;
break;
default:
zend_throw_exception(redis_cluster_exception_ce,
"Internal Zend HashTable error", 0 TSRMLS_CC);
return -1;
}
// Prefix our key if we need to, set the slot
kv->key_free = redis_key_prefix(c->flags, &(kv->key), &(kv->key_len));
kv->slot = cluster_hash_key(kv->key, kv->key_len);
// Now grab our value
if ((z_val = zend_hash_get_current_data_ex(ht, ptr)) == NULL) {
zend_throw_exception(redis_cluster_exception_ce,
"Internal Zend HashTable error", 0 TSRMLS_CC);
return -1;
}
// Serialize our value if required
kv->val_free = redis_serialize(c->flags,z_val,&(kv->val),&(kv->val_len)
TSRMLS_CC);
// Success
return 0;
}
/* Helper to pull, prefix, and hash a key from a HashTable value */
static int get_key_ht(redisCluster *c, HashTable *ht, HashPosition *ptr,
clusterKeyValHT *kv TSRMLS_DC)
{
zval *z_key;
if ((z_key = zend_hash_get_current_data_ex(ht, ptr)) == NULL) {
// Shouldn't happen, but check anyway
zend_throw_exception(redis_cluster_exception_ce,
"Internal Zend HashTable error", 0 TSRMLS_CC);
return -1;
}
// Always want to work with strings
convert_to_string(z_key);
kv->key = Z_STRVAL_P(z_key);
kv->key_len = Z_STRLEN_P(z_key);
kv->key_free = redis_key_prefix(c->flags, &(kv->key), &(kv->key_len));
// Hash our key
kv->slot = cluster_hash_key(kv->key, kv->key_len);
// Success
return 0;
}
/* Turn variable arguments into a HashTable for processing */
static HashTable *method_args_to_ht(zval *z_args, int argc) {
HashTable *ht_ret;
int i;
/* Allocate our hash table */
ALLOC_HASHTABLE(ht_ret);
zend_hash_init(ht_ret, argc, NULL, NULL, 0);
/* Populate our return hash table with our arguments */
for (i = 0; i < argc; i++) {
zend_hash_next_index_insert(ht_ret, &z_args[i]);
}
/* Return our hash table */
return ht_ret;
}
/* Handler for both MGET and DEL */
static int cluster_mkey_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
zval *z_ret, cluster_cb cb)
{
redisCluster *c = GET_CONTEXT();
clusterMultiCmd mc = {0};
clusterKeyValHT kv;
zval *z_args;
HashTable *ht_arr;
HashPosition ptr;
int i=1, argc = ZEND_NUM_ARGS(), ht_free=0;
short slot;
/* If we don't have any arguments we're invalid */
if (!argc) return -1;
/* Extract our arguments into an array */
z_args = ecalloc(argc, sizeof(zval));
if (zend_get_parameters_array(ht, argc, z_args) == FAILURE) {
efree(z_args);
return -1;
}
/* Determine if we're working with a single array or variadic args */
if (argc == 1 && Z_TYPE(z_args[0]) == IS_ARRAY) {
ht_arr = Z_ARRVAL(z_args[0]);
argc = zend_hash_num_elements(ht_arr);
if (!argc) {
efree(z_args);
return -1;
}
} else {
ht_arr = method_args_to_ht(z_args, argc);
ht_free = 1;
}
/* MGET is readonly, DEL is not */
c->readonly = kw_len == 4 && CLUSTER_IS_ATOMIC(c);
// Initialize our "multi" command handler with command/len
CLUSTER_MULTI_INIT(mc, kw, kw_len);
// Process the first key outside of our loop, so we don't have to check if
// it's the first iteration every time, needlessly
zend_hash_internal_pointer_reset_ex(ht_arr, &ptr);
if(get_key_ht(c, ht_arr, &ptr, &kv TSRMLS_CC)<0) {
efree(z_args);
return -1;
}
// Process our key and add it to the command
cluster_multi_add(&mc, kv.key, kv.key_len);
// Free key if we prefixed
if(kv.key_free) efree(kv.key);
// Move to the next key
zend_hash_move_forward_ex(ht_arr, &ptr);
// Iterate over keys 2...N
slot = kv.slot;
while(zend_hash_has_more_elements_ex(ht_arr, &ptr)==SUCCESS) {
if(get_key_ht(c, ht_arr, &ptr, &kv TSRMLS_CC)<0) {
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
efree(z_args);
return -1;
}
// If the slots have changed, kick off the keys we've aggregated
if(slot != kv.slot) {
// Process this batch of MGET keys
if(distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, slot,
&mc, z_ret, i==argc, cb)<0)
{
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
efree(z_args);
return -1;
}
}
// Add this key to the command
cluster_multi_add(&mc, kv.key, kv.key_len);
// Free key if we prefixed
if(kv.key_free) efree(kv.key);
// Update the last slot we encountered, and the key we're on
slot = kv.slot;
i++;
zend_hash_move_forward_ex(ht_arr, &ptr);
}
efree(z_args);
// If we've got straggler(s) process them
if(mc.argc > 0) {
if(distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, slot,
&mc, z_ret, 1, cb)<0)
{
cluster_multi_free(&mc);
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
return -1;
}
}
// Free our command
cluster_multi_free(&mc);
/* Clean up our hash table if we constructed it from variadic args */
if (ht_free) {
zend_hash_destroy(ht_arr);
efree(ht_arr);
}
/* Return our object if we're in MULTI mode */
if (!CLUSTER_IS_ATOMIC(c))
RETVAL_ZVAL(getThis(), 1, 0);
// Success
return 0;
}
/* Handler for both MSET and MSETNX */
static int cluster_mset_cmd(INTERNAL_FUNCTION_PARAMETERS, char *kw, int kw_len,
zval *z_ret, cluster_cb cb)
{
redisCluster *c = GET_CONTEXT();
clusterKeyValHT kv;
clusterMultiCmd mc = {0};
zval *z_arr;
HashTable *ht_arr;
HashPosition ptr;
int i=1, argc;
short slot;
// Parse our arguments
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &z_arr)==FAILURE) {
return -1;
}
// No reason to send zero args
ht_arr = Z_ARRVAL_P(z_arr);
if((argc = zend_hash_num_elements(ht_arr))==0) {
return -1;
}
/* This is a write command */
c->readonly = 0;
// Set up our multi command handler
CLUSTER_MULTI_INIT(mc, kw, kw_len);
// Process the first key/value pair outside of our loop
zend_hash_internal_pointer_reset_ex(ht_arr, &ptr);
if(get_key_val_ht(c, ht_arr, &ptr, &kv TSRMLS_CC)==-1) return -1;
zend_hash_move_forward_ex(ht_arr, &ptr);
// Add this to our multi cmd, set slot, free key if we prefixed
cluster_multi_add(&mc, kv.key, kv.key_len);
cluster_multi_add(&mc, kv.val, kv.val_len);
if(kv.key_free) efree(kv.key);
if(kv.val_free) efree(kv.val);
// While we've got more keys to set
slot = kv.slot;
while(zend_hash_has_more_elements_ex(ht_arr, &ptr)==SUCCESS) {
// Pull the next key/value pair
if(get_key_val_ht(c, ht_arr, &ptr, &kv TSRMLS_CC)==-1) {
return -1;
}
// If the slots have changed, process responses
if(slot != kv.slot) {
if(distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c,
slot, &mc, z_ret, i==argc, cb)<0)
{
return -1;
}
}
// Add this key and value to our command
cluster_multi_add(&mc, kv.key, kv.key_len);
cluster_multi_add(&mc, kv.val, kv.val_len);
// Free our key and value if we need to
if(kv.key_free) efree(kv.key);
if(kv.val_free) efree(kv.val);
// Update our slot, increment position
slot = kv.slot;
i++;
// Move on
zend_hash_move_forward_ex(ht_arr, &ptr);
}
// If we've got stragglers, process them too
if(mc.argc > 0) {
if(distcmd_resp_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, c, slot, &mc,
z_ret, 1, cb)<0)
{
return -1;
}
}
// Free our command
cluster_multi_free(&mc);
/* Return our object if we're in MULTI mode */
if (!CLUSTER_IS_ATOMIC(c))
RETVAL_ZVAL(getThis(), 1, 0);
// Success
return 0;
}
/* {{{ proto array RedisCluster::del(string key1, string key2, ... keyN) */
PHP_METHOD(RedisCluster, del) {
zval *z_ret;
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_ret);
#else
z_ret = emalloc(sizeof(zval));
#endif
// Initialize a LONG value to zero for our return
ZVAL_LONG(z_ret, 0);
// Parse args, process
if(cluster_mkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "DEL",
sizeof("DEL")-1, z_ret, cluster_del_resp)<0)
{
efree(z_ret);
RETURN_FALSE;
}
}
/* {{{ proto array RedisCluster::mget(array keys) */
PHP_METHOD(RedisCluster, mget) {
zval *z_ret;
// Array response
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_ret);
#else
z_ret = emalloc(sizeof(zval));
#endif
array_init(z_ret);
// Parse args, process
if(cluster_mkey_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MGET",
sizeof("MGET")-1, z_ret, cluster_mbulk_mget_resp)<0)
{
zval_dtor(z_ret);
efree(z_ret);
RETURN_FALSE;
}
}
/* {{{ proto bool RedisCluster::mset(array keyvalues) */
PHP_METHOD(RedisCluster, mset) {
zval *z_ret;
// Response, defaults to TRUE
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_ret);
#else
z_ret = emalloc(sizeof(zval));
#endif
ZVAL_TRUE(z_ret);
// Parse args and process. If we get a failure, free zval and return FALSE.
if(cluster_mset_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSET",
sizeof("MSET")-1, z_ret, cluster_mset_resp)==-1)
{
efree(z_ret);
RETURN_FALSE;
}
}
/* {{{ proto array RedisCluster::msetnx(array keyvalues) */
PHP_METHOD(RedisCluster, msetnx) {
zval *z_ret;
// Array response
#if (PHP_MAJOR_VERSION < 7)
MAKE_STD_ZVAL(z_ret);
#else
z_ret = emalloc(sizeof(zval));
#endif
array_init(z_ret);
// Parse args and process. If we get a failure, free mem and return FALSE
if(cluster_mset_cmd(INTERNAL_FUNCTION_PARAM_PASSTHRU, "MSETNX",
sizeof("MSETNX")-1, z_ret, cluster_msetnx_resp)==-1)
{
zval_dtor(z_ret);
efree(z_ret);
RETURN_FALSE;