-
Notifications
You must be signed in to change notification settings - Fork 40
/
r3c.cpp
executable file
·6210 lines (5656 loc) · 204 KB
/
r3c.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
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
// Writed by yijian ([email protected])
// R3C is a C++ client for redis based on hiredis (https://github.com/redis/hiredis)
#include "r3c.h"
#include "utils.h"
#include <assert.h>
#define R3C_ASSERT assert
#define THROW_REDIS_EXCEPTION(errinfo) \
throw CRedisException(errinfo, __FILE__, __LINE__)
#define THROW_REDIS_EXCEPTION_WITH_NODE(errinfo, node_ip, node_port) \
throw CRedisException(errinfo, __FILE__, __LINE__, node_ip, node_port)
#define THROW_REDIS_EXCEPTION_WITH_NODE_AND_COMMAND(errinfo, node_ip, node_port, command, key) \
throw CRedisException(errinfo, __FILE__, __LINE__, node_ip, node_port, command, key)
namespace r3c {
int NUM_RETRIES = 15; // The default number of retries is 15 (CLUSTERDOWN cost more than 6s)
int CONNECT_TIMEOUT_MILLISECONDS = 2000; // Connection timeout in milliseconds
int READWRITE_TIMEOUT_MILLISECONDS = 2000; // Receive and send timeout in milliseconds
#if R3C_TEST // for test
static LOG_WRITE g_error_log = r3c_log_write;
static LOG_WRITE g_info_log = r3c_log_write;
static LOG_WRITE g_debug_log = r3c_log_write;
#else
static LOG_WRITE g_error_log = null_log_write;
static LOG_WRITE g_info_log = null_log_write;
static LOG_WRITE g_debug_log = null_log_write;
#endif // R3C_TEST
void set_error_log_write(LOG_WRITE info_log)
{
g_error_log = info_log;
if (NULL == g_error_log)
g_error_log = null_log_write;
}
void set_info_log_write(LOG_WRITE info_log)
{
g_info_log = info_log;
if (NULL == g_info_log)
g_info_log = null_log_write;
}
void set_debug_log_write(LOG_WRITE debug_log)
{
g_debug_log = debug_log;
if (NULL == g_debug_log)
g_debug_log = null_log_write;
}
static int get_retry_sleep_milliseconds(int loop_counter)
{
static const int sleep_milliseconds_table[] = { 10, 100, 200, 500, 1000 };
if (loop_counter<0 || loop_counter>=static_cast<int>(sizeof(sleep_milliseconds_table)/sleep_milliseconds_table[0]-1))
return 1000;
else
return sleep_milliseconds_table[loop_counter];
}
// Calculate the time elapsed to execute the redis command in microseconds.
static int64_t calc_elapsed_time(const struct timeval& start_tv, const struct timeval& stop_tv)
{
return static_cast<int64_t>((stop_tv.tv_sec - start_tv.tv_sec) * (__UINT64_C(1000000)) + (stop_tv.tv_usec - start_tv.tv_usec));
}
enum
{
CLUSTER_SLOTS = 16384 // number of slots, defined in cluster.h
};
std::string zaddflag2str(ZADDFLAG zaddflag)
{
std::string zaddflag_str;
switch (zaddflag)
{
case Z_XX:
zaddflag_str = "XX";
break;
case Z_NX:
zaddflag_str = "NX";
break;
case Z_CH:
zaddflag_str = "CH";
break;
default:
break;
}
return zaddflag_str;
}
size_t NodeHasher::operator ()(const Node& node) const
{
const unsigned char* s = reinterpret_cast<const unsigned char*>(node.first.c_str());
const uint64_t l = static_cast<uint64_t>(node.first.size());
uint64_t crc = 0;
return static_cast<size_t>(crc64(crc, s, l));
}
std::string& node2string(const Node& node, std::string* str)
{
*str = node.first + std::string(":") + int2string(node.second);
return *str;
}
std::string node2string(const Node& node)
{
std::string nodestr;
return node2string(node, &nodestr);
}
std::string NodeInfo::str() const
{
return format_string("nodeinfo://%s/%s:%d/%s", id.c_str(), node.first.c_str(), node.second, flags.c_str());
}
bool NodeInfo::is_master() const
{
const std::string::size_type pos = flags.find("master");
return (pos != std::string::npos);
}
bool NodeInfo::is_replica() const
{
const std::string::size_type pos = flags.find("slave");
return (pos != std::string::npos);
}
bool NodeInfo::is_fail() const
{
const std::string::size_type pos = flags.find("fail");
return (pos != std::string::npos);
}
////////////////////////////////////////////////////////////////////////////////
// CCommandArgs
CommandArgs::CommandArgs()
: _argc(0), _argv(NULL), _argvlen(NULL)
{
}
CommandArgs::~CommandArgs()
{
delete []_argvlen;
for (int i=0; i<_argc; ++i)
delete []_argv[i];
delete []_argv;
}
void CommandArgs::set_key(const std::string& key)
{
_key = key;
}
void CommandArgs::set_command(const std::string& command)
{
_command = command;
}
void CommandArgs::add_arg(const std::string& arg)
{
_args.push_back(arg);
}
void CommandArgs::add_arg(char arg)
{
const std::string str(&arg, 1);
add_arg(str);
}
void CommandArgs::add_arg(int32_t arg)
{
_args.push_back(int2string(arg));
}
void CommandArgs::add_arg(uint32_t arg)
{
_args.push_back(int2string(arg));
}
void CommandArgs::add_arg(int64_t arg)
{
_args.push_back(int2string(arg));
}
void CommandArgs::add_args(const std::vector<std::string>& args)
{
for (std::vector<std::string>::size_type i=0; i<args.size(); ++i)
{
const std::string& arg = args[i];
add_arg(arg);
}
}
void CommandArgs::add_args(const std::vector<std::pair<std::string, std::string> >& values)
{
for (std::vector<std::string>::size_type i=0; i<values.size(); ++i)
{
const std::string& field = values[i].first;
const std::string& value = values[i].second;
add_arg(field);
add_arg(value);
}
}
void CommandArgs::add_args(const std::map<std::string, std::string>& map)
{
for (std::map<std::string, std::string>::const_iterator iter=map.begin(); iter!=map.end(); ++iter)
{
add_arg(iter->first);
add_arg(iter->second);
}
}
void CommandArgs::add_args(const std::map<std::string, int64_t>& map, bool reverse)
{
for (std::map<std::string, int64_t>::const_iterator iter=map.begin(); iter!=map.end(); ++iter)
{
if (!reverse)
{
add_arg(iter->first);
add_arg(iter->second);
}
else
{
add_arg(iter->second);
add_arg(iter->first);
}
}
}
void CommandArgs::add_args(const std::vector<FVPair>& fvpairs)
{
for (std::vector<FVPair>::size_type i=0; i<fvpairs.size(); ++i)
{
const FVPair& fvpair = fvpairs[i];
add_arg(fvpair.field);
add_arg(fvpair.value);
}
}
void CommandArgs::final()
{
_argc = static_cast<int>(_args.size());
_argv = new char*[_argc];
_argvlen = new size_t[_argc];
for (int i=0; i<_argc; ++i)
{
_argvlen[i] = _args[i].size();
_argv[i] = new char[_argvlen[i]+1];
memcpy(_argv[i], _args[i].c_str(), _argvlen[i]); // Support binary key&value.
_argv[i][_argvlen[i]] = '\0';
}
}
int CommandArgs::get_argc() const
{
return _argc;
}
const char** CommandArgs::get_argv() const
{
return (const char**)_argv;
}
const size_t* CommandArgs::get_argvlen() const
{
return _argvlen;
}
const std::string& CommandArgs::get_command() const
{
return _command;
}
const std::string& CommandArgs::get_key() const
{
return _key;
}
////////////////////////////////////////////////////////////////////////////////
// CRedisNode
// CRedisMasterNode
// CRedisReplicaNode
class CRedisNode
{
public:
CRedisNode(const NodeId& nodeid, const Node& node, redisContext* redis_context)
: _nodeid(nodeid),
_node(node),
_redis_context(redis_context),
_conn_errors(0)
{
}
~CRedisNode()
{
close();
}
const NodeId& get_nodeid() const
{
return _nodeid;
}
const Node& get_node() const
{
return _node;
}
redisContext* get_redis_context() const
{
return _redis_context;
}
void set_redis_context(redisContext* redis_context)
{
_redis_context = redis_context;
if (NULL == _redis_context)
{
++_conn_errors;
//(*g_debug_log)("%s\n", str().c_str());
}
}
void close()
{
if (_redis_context != NULL)
{
redisFree(_redis_context);
_redis_context = NULL;
}
}
std::string str() const
{
return format_string("node://(connerrors:%u)%s:%d", _conn_errors, _node.first.c_str(), _node.second);
}
unsigned int get_conn_errors() const
{
return _conn_errors;
}
void inc_conn_errors()
{
++_conn_errors;
}
void reset_conn_errors()
{
_conn_errors = 0;
}
void set_conn_errors(unsigned int conn_errors)
{
_conn_errors = conn_errors;
}
bool need_refresh_master() const
{
return ((_conn_errors>3 && 0==_conn_errors%3) || (_conn_errors>2018));
}
protected:
NodeId _nodeid;
Node _node;
redisContext* _redis_context;
unsigned int _conn_errors; // 连续连接失败数
};
class CRedisMasterNode;
class CRedisReplicaNode: public CRedisNode
{
public:
CRedisReplicaNode(const NodeId& node_id, const Node& node, redisContext* redis_context)
: CRedisNode(node_id, node, redis_context),
_redis_master_node(NULL)
{
}
private:
CRedisMasterNode* _redis_master_node;
};
class CRedisMasterNode: public CRedisNode
{
public:
CRedisMasterNode(const NodeId& node_id, const Node& node, redisContext* redis_context)
: CRedisNode(node_id, node, redis_context),
_index(0)
{
}
~CRedisMasterNode()
{
clear();
}
void clear()
{
for (RedisReplicaNodeTable::iterator iter=_redis_replica_nodes.begin(); iter!=_redis_replica_nodes.end(); ++iter)
{
CRedisReplicaNode* replica_node = iter->second;
delete replica_node;
}
_redis_replica_nodes.clear();
}
void add_replica_node(CRedisReplicaNode* redis_replica_node)
{
const Node& node = redis_replica_node->get_node();
const std::pair<RedisReplicaNodeTable::iterator, bool> ret = _redis_replica_nodes.insert(std::make_pair(node, redis_replica_node));
if (!ret.second)
{
CRedisReplicaNode* old_replica_node = ret.first->second;
delete old_replica_node;
ret.first->second = redis_replica_node;
}
}
CRedisNode* choose_node(ReadPolicy read_policy)
{
const unsigned int num_redis_replica_nodes = static_cast<unsigned int>(_redis_replica_nodes.size());
CRedisNode* redis_node = NULL;
if (0 == num_redis_replica_nodes)
{
redis_node = this;
}
else
{
unsigned int K = _index++ % (num_redis_replica_nodes+1); // Included master
if (RP_READ_REPLICA==read_policy && K==num_redis_replica_nodes)
{
redis_node = this;
}
else
{
RedisReplicaNodeTable::iterator iter = _redis_replica_nodes.begin();
for (unsigned int i=0; i<K; ++i)
{
if (++iter == _redis_replica_nodes.end())
iter = _redis_replica_nodes.begin();
}
if (iter != _redis_replica_nodes.end())
{
redis_node = iter->second;
}
if (NULL == redis_node)
{
redis_node = this;
}
}
}
return redis_node;
}
private:
#if __cplusplus < 201103L
typedef std::tr1::unordered_map<Node, CRedisReplicaNode*, NodeHasher> RedisReplicaNodeTable;
#else
typedef std::unordered_map<Node, CRedisReplicaNode*, NodeHasher> RedisReplicaNodeTable;
#endif // __cplusplus < 201103L
RedisReplicaNodeTable _redis_replica_nodes;
unsigned int _index;
};
////////////////////////////////////////////////////////////////////////////////
// RedisReplyHelper
RedisReplyHelper::RedisReplyHelper()
: _redis_reply(NULL)
{
}
RedisReplyHelper::RedisReplyHelper(const redisReply* redis_reply)
: _redis_reply(redis_reply)
{
}
RedisReplyHelper::RedisReplyHelper(const RedisReplyHelper& other)
{
_redis_reply = other.detach();
}
RedisReplyHelper::~RedisReplyHelper()
{
free();
}
RedisReplyHelper::operator bool() const
{
return _redis_reply != NULL;
}
void RedisReplyHelper::free()
{
if (_redis_reply != NULL)
{
freeReplyObject((void*)_redis_reply);
_redis_reply = NULL;
}
}
const redisReply* RedisReplyHelper::get() const
{
return _redis_reply;
}
const redisReply* RedisReplyHelper::detach() const
{
const redisReply* redis_reply = _redis_reply;
_redis_reply = NULL;
return redis_reply;
}
RedisReplyHelper& RedisReplyHelper::operator =(const redisReply* redis_reply)
{
free();
_redis_reply = redis_reply;
return *this;
}
RedisReplyHelper& RedisReplyHelper::operator =(const RedisReplyHelper& other)
{
free();
_redis_reply = other.detach();
return *this;
}
const redisReply* RedisReplyHelper::operator ->() const
{
return _redis_reply;
}
std::ostream& RedisReplyHelper::operator <<(std::ostream& os)
{
os << *_redis_reply;
return os;
}
////////////////////////////////////////////////////////////////////////////////
// ErrorInfo
ErrorInfo::ErrorInfo()
: errcode(0)
{
}
ErrorInfo::ErrorInfo(const std::string& raw_errmsg_, const std::string& errmsg_, const std::string& errtype_, int errcode_)
: raw_errmsg(raw_errmsg_), errmsg(errmsg_), errtype(errtype_), errcode(errcode_)
{
}
void ErrorInfo::clear()
{
errcode = 0;
errtype.clear();
errmsg.clear();
raw_errmsg.clear();
}
////////////////////////////////////////////////////////////////////////////////
// CRedisException
CRedisException::CRedisException(
const struct ErrorInfo& errinfo,
const char* file, int line,
const std::string& node_ip, uint16_t node_port,
const std::string& command, const std::string& key) throw ()
: _errinfo(errinfo),
_line(line),
_node_ip(node_ip), _node_port(node_port),
_command(command), _key(key)
{
const char* slash_position = strrchr(file, '/');
const std::string* file_cp = &_file;
std::string* file_p = const_cast<std::string*>(file_cp);
if (NULL == slash_position)
{
*file_p = file;
}
else
{
*file_p = std::string(slash_position + 1);
}
}
const char* CRedisException::what() const throw()
{
return _errinfo.errmsg.c_str();
}
std::string CRedisException::str() const throw ()
{
const std::string& errmsg = format_string("redis_exception://%s:%d/CMD:%s/%s/(%d)%s@%s:%d/(KEY:%.*s)",
_node_ip.c_str(), _node_port,
_command.c_str(),
_errinfo.errtype.c_str(), _errinfo.errcode, _errinfo.errmsg.c_str(),
_file.c_str(), _line,
static_cast<int>(_key.size()), _key.c_str());
#if __cplusplus < 201103L
return errmsg;
#else
return std::move(errmsg);
#endif // __cplusplus < 201103L
}
bool is_general_error(const std::string& errtype)
{
return (errtype.size() == sizeof("ERR")-1) && (errtype == "ERR");
}
bool is_ask_error(const std::string& errtype)
{
return (errtype.size() == sizeof("ASK")-1) && (errtype == "ASK");
}
bool is_clusterdown_error(const std::string& errtype)
{
// CLUSTERDOWN The cluster is down
// 如果在从未成为主之前,将主和从同时干掉,则即使从正常了,从也无法成为主,
// 这个时候必须将原来的主恢复,否则该部分slots就不能再服务
return (errtype.size() == sizeof("CLUSTERDOWN")-1) && (errtype == "CLUSTERDOWN");
}
bool is_moved_error(const std::string& errtype)
{
return (errtype.size() == sizeof("MOVED")-1) && (errtype == "MOVED");
}
bool is_noauth_error(const std::string& errtype)
{
// NOAUTH Authentication required.
return (errtype.size() == sizeof("NOAUTH")-1) && (errtype == "NOAUTH");
}
bool is_noscript_error(const std::string& errtype)
{
// NOSCRIPT No matching script. Please use EVAL.
return (errtype.size() == sizeof("NOSCRIPT")-1) && (errtype == "NOSCRIPT");
}
bool is_wrongtype_error(const std::string& errtype)
{
return (errtype.size() == sizeof("WRONGTYPE")-1) && (errtype == "WRONGTYPE");
}
bool is_busygroup_error(const std::string& errtype)
{
return (errtype.size() == sizeof("BUSYGROUP")-1) && (errtype == "BUSYGROUP");
}
bool is_nogroup_error(const std::string& errtype)
{
return (errtype.size() == sizeof("NOGROUP")-1) && (errtype == "NOGROUP");
}
bool is_crossslot_error(const std::string& errtype)
{
return (errtype.size() == sizeof("CROSSSLOT")-1) && (errtype == "CROSSSLOT");
}
////////////////////////////////////////////////////////////////////////////////
// CRedisClient
CRedisClient::CRedisClient(
const std::string& raw_nodes_string,
int connect_timeout_milliseconds,
int readwrite_timeout_milliseconds,
const std::string& password,
ReadPolicy read_policy
)
: _command_monitor(NULL),
_raw_nodes_string(raw_nodes_string),
_connect_timeout_milliseconds(connect_timeout_milliseconds),
_readwrite_timeout_milliseconds(readwrite_timeout_milliseconds),
_password(password),
_read_policy(read_policy)
{
init();
}
CRedisClient::CRedisClient(
const std::string& raw_nodes_string,
const std::string& password,
int connect_timeout_milliseconds,
int readwrite_timeout_milliseconds,
ReadPolicy read_policy)
: _command_monitor(NULL),
_raw_nodes_string(raw_nodes_string),
_connect_timeout_milliseconds(connect_timeout_milliseconds),
_readwrite_timeout_milliseconds(readwrite_timeout_milliseconds),
_password(password),
_read_policy(read_policy)
{
init();
}
CRedisClient::CRedisClient(
const std::string& raw_nodes_string,
ReadPolicy read_policy,
const std::string& password,
int connect_timeout_milliseconds,
int readwrite_timeout_milliseconds)
: _command_monitor(NULL),
_raw_nodes_string(raw_nodes_string),
_connect_timeout_milliseconds(connect_timeout_milliseconds),
_readwrite_timeout_milliseconds(readwrite_timeout_milliseconds),
_password(password),
_read_policy(read_policy)
{
init();
}
CRedisClient::~CRedisClient()
{
fini();
}
const std::string& CRedisClient::get_raw_nodes_string() const
{
return _raw_nodes_string;
}
const std::string& CRedisClient::get_nodes_string() const
{
return _nodes_string;
}
std::string CRedisClient::str() const
{
if (cluster_mode())
return std::string("rediscluster://") + _raw_nodes_string;
else
return std::string("redisstandalone://") + _raw_nodes_string;
}
bool CRedisClient::cluster_mode() const
{
return _nodes.size() > 1;
}
const char* CRedisClient::get_mode_str() const
{
return cluster_mode()? "CLUSTER": "STANDALONE";
}
void CRedisClient::enable_debug_log()
{
_enable_debug_log = true;
}
void CRedisClient::disable_debug_log()
{
_enable_debug_log = false;
}
void CRedisClient::enable_info_log()
{
_enable_info_log = true;
}
void CRedisClient::disable_info_log()
{
_enable_info_log = false;
}
void CRedisClient::enable_error_log()
{
_enable_error_log = true;
}
void CRedisClient::disable_error_log()
{
_enable_error_log = false;
}
int CRedisClient::list_nodes(std::vector<struct NodeInfo>* nodes_info)
{
struct ErrorInfo errinfo;
for (RedisMasterNodeTable::iterator iter=_redis_master_nodes.begin(); iter!=_redis_master_nodes.end(); ++iter)
{
const Node& node = iter->first;
struct CRedisNode* redis_node = iter->second;
redisContext* redis_context = redis_node->get_redis_context();
if (redis_context != NULL)
{
if (list_cluster_nodes(nodes_info, &errinfo, redis_context, node))
break;
}
}
if (nodes_info->empty())
{
THROW_REDIS_EXCEPTION(errinfo);
}
return static_cast<int>(nodes_info->size());
}
void CRedisClient::flushall()
{
const int num_retries = NUM_RETRIES;
const std::string key;
CommandArgs cmd_args;
cmd_args.set_command("FLUSHALL");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.final();
// Simple string reply (REDIS_REPLY_STATUS)
redis_command(true, num_retries, key, cmd_args, NULL);
}
void CRedisClient::multi(const std::string& key, Node* which)
{
if (cluster_mode())
{
struct ErrorInfo errinfo;
errinfo.errcode = ERROR_NOT_SUPPORT;
errinfo.errmsg = "MULTI not supported in cluster mode";
THROW_REDIS_EXCEPTION(errinfo);
}
else
{
const int num_retries = 0;
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("MULTI");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.final();
// Simple string reply (REDIS_REPLY_STATUS):
// always OK.
redis_command(false, num_retries, key, cmd_args, which);
}
}
const RedisReplyHelper CRedisClient::exec(const std::string& key, Node* which)
{
if (cluster_mode())
{
struct ErrorInfo errinfo;
errinfo.errcode = ERROR_NOT_SUPPORT;
errinfo.errmsg = "EXEC not supported in cluster mode";
THROW_REDIS_EXCEPTION(errinfo);
}
else
{
const int num_retries = 0;
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("EXEC");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.final();
// Array reply:
// each element being the reply to each of the commands in the atomic transaction.
return redis_command(false, num_retries, key, cmd_args, which);
}
}
//
// KEY/VALUE
//
// Time complexity: O(1)
// EXPIRE key seconds
bool CRedisClient::expire(
const std::string& key,
uint32_t seconds,
Node* which,
int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("EXPIRE");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.add_arg(key);
cmd_args.add_arg(seconds);
cmd_args.final();
// Integer reply, specifically:
// 1 if the timeout was set.
// 0 if key does not exist.
const RedisReplyHelper redis_reply = redis_command(false, num_retries, key, cmd_args, which);
if (REDIS_REPLY_INTEGER == redis_reply->type)
return 1 == redis_reply->integer;
return true;
}
bool CRedisClient::expireat(const std::string& key, int64_t timestamp, Node* which, int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("EXPIREAT");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.add_arg(key);
cmd_args.add_arg(timestamp);
cmd_args.final();
// Integer reply, specifically:
// 1 if the timeout was set.
// 0 if key does not exist.
const RedisReplyHelper redis_reply = redis_command(false, num_retries, key, cmd_args, which);
if (REDIS_REPLY_INTEGER == redis_reply->type)
return 1 == redis_reply->integer;
return true;
}
// Time complexity: O(1)
// EXISTS key [key ...]
bool CRedisClient::exists(const std::string& key, Node* which, int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("EXISTS");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.add_arg(key);
cmd_args.final();
// Integer reply, specifically:
// 1 if the key exists.
// 0 if the key does not exist.
const RedisReplyHelper redis_reply = redis_command(true, num_retries, key, cmd_args, which);
if (REDIS_REPLY_INTEGER == redis_reply->type)
return 1 == redis_reply->integer;
return true;
}
// Time complexity:
// O(N) where N is the number of keys that will be removed.
// When a key to remove holds a value other than a string,
// the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash.
// Removing a single key that holds a string value is O(1).
bool CRedisClient::del(const std::string& key, Node* which, int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("DEL");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.add_arg(key);
cmd_args.final();
// Integer reply:
// The number of keys that were removed.
const RedisReplyHelper redis_reply = redis_command(false, num_retries, key, cmd_args, which);
if (REDIS_REPLY_INTEGER == redis_reply->type)
return 1 == redis_reply->integer;
return true;
}
// GET key
// Time complexity: O(1)
bool CRedisClient::get(
const std::string& key,
std::string* value,
Node* which,
int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);
cmd_args.set_command("GET");
cmd_args.add_arg(cmd_args.get_command());
cmd_args.add_arg(key);
cmd_args.final();
// Bulk string reply:
// the value of key, or nil when key does not exist.
RedisReplyHelper redis_reply = redis_command(true, num_retries, key, cmd_args, which);
if (REDIS_REPLY_NIL == redis_reply->type)
return false;
if (REDIS_REPLY_STRING == redis_reply->type)
return get_value(redis_reply.get(), value);
return true;
}
// SET key value [EX seconds] [PX milliseconds] [NX|XX]
// Time complexity: O(1)
void CRedisClient::set(
const std::string& key,
const std::string& value,
Node* which,
int num_retries)
{
CommandArgs cmd_args;
cmd_args.set_key(key);