forked from redis-rs/redis-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_routing.rs
1350 lines (1217 loc) · 44.9 KB
/
cluster_routing.rs
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
use std::cmp::min;
use std::collections::HashMap;
use crate::cluster_topology::get_slot;
use crate::cmd::{Arg, Cmd};
use crate::types::Value;
use crate::{ErrorKind, RedisResult};
use std::iter::Once;
#[derive(Clone)]
pub(crate) enum Redirect {
Moved(String),
Ask(String),
}
/// Logical bitwise aggregating operators.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum LogicalAggregateOp {
/// Aggregate by bitwise &&
And,
// Or, omitted due to dead code warnings. ATM this value isn't constructed anywhere
}
/// Numerical aggreagting operators.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum AggregateOp {
/// Choose minimal value
Min,
/// Sum all values
Sum,
// Max, omitted due to dead code warnings. ATM this value isn't constructed anywhere
}
/// Policy defining how to combine multiple responses into one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ResponsePolicy {
/// Wait for one request to succeed and return its results. Return error if all requests fail.
OneSucceeded,
/// Returns the first succeeded non-empty result; if all results are empty, returns `Nil`; otherwise, returns the last received error.
FirstSucceededNonEmptyOrAllEmpty,
/// Waits for all requests to succeed, and the returns one of the successes. Returns the error on the first received error.
AllSucceeded,
/// Aggregate success results according to a logical bitwise operator. Return error on any failed request or on a response that doesn't conform to 0 or 1.
AggregateLogical(LogicalAggregateOp),
/// Aggregate success results according to a numeric operator. Return error on any failed request or on a response that isn't an integer.
Aggregate(AggregateOp),
/// Aggregate array responses into a single array. Return error on any failed request or on a response that isn't an array.
CombineArrays,
/// Handling is not defined by the Redis standard. Will receive a special case
Special,
/// Combines multiple map responses into a single map.
CombineMaps,
}
/// Defines whether a request should be routed to a single node, or multiple ones.
#[derive(Debug, Clone, PartialEq)]
pub enum RoutingInfo {
/// Route to single node
SingleNode(SingleNodeRoutingInfo),
/// Route to multiple nodes
MultiNode((MultipleNodeRoutingInfo, Option<ResponsePolicy>)),
}
/// Defines which single node should receive a request.
#[derive(Debug, Clone, PartialEq)]
pub enum SingleNodeRoutingInfo {
/// Route to any node at random
Random,
/// Route to the node that matches the [Route]
SpecificNode(Route),
/// Route to the node with the given address.
ByAddress {
/// DNS hostname of the node
host: String,
/// port of the node
port: u16,
},
}
impl From<Option<Route>> for SingleNodeRoutingInfo {
fn from(value: Option<Route>) -> Self {
value
.map(SingleNodeRoutingInfo::SpecificNode)
.unwrap_or(SingleNodeRoutingInfo::Random)
}
}
/// Defines which collection of nodes should receive a request
#[derive(Debug, Clone, PartialEq)]
pub enum MultipleNodeRoutingInfo {
/// Route to all nodes in the clusters
AllNodes,
/// Route to all primaries in the cluster
AllMasters,
/// Instructions for how to split a multi-slot command (e.g. MGET, MSET) into sub-commands. Each tuple is the route for each subcommand, and the indices of the arguments from the original command that should be copied to the subcommand.
MultiSlot(Vec<(Route, Vec<usize>)>),
}
/// Takes a routable and an iterator of indices, which is assued to be created from`MultipleNodeRoutingInfo::MultiSlot`,
/// and returns a command with the arguments matching the indices.
pub fn command_for_multi_slot_indices<'a, 'b>(
original_cmd: &'a impl Routable,
indices: impl Iterator<Item = &'b usize> + 'a,
) -> Cmd
where
'b: 'a,
{
let mut new_cmd = Cmd::new();
let command_length = 1; // TODO - the +1 should change if we have multi-slot commands with 2 command words.
new_cmd.arg(original_cmd.arg_idx(0));
for index in indices {
new_cmd.arg(original_cmd.arg_idx(index + command_length));
}
new_cmd
}
/// Aggreagte numeric responses.
pub fn aggregate(values: Vec<Value>, op: AggregateOp) -> RedisResult<Value> {
let initial_value = match op {
AggregateOp::Min => i64::MAX,
AggregateOp::Sum => 0,
};
let result = values.into_iter().try_fold(initial_value, |acc, curr| {
let int = match curr {
Value::Int(int) => int,
_ => {
return RedisResult::Err(
(
ErrorKind::TypeError,
"expected array of integers as response",
)
.into(),
);
}
};
let acc = match op {
AggregateOp::Min => min(acc, int),
AggregateOp::Sum => acc + int,
};
Ok(acc)
})?;
Ok(Value::Int(result))
}
/// Aggreagte numeric responses by a boolean operator.
pub fn logical_aggregate(values: Vec<Value>, op: LogicalAggregateOp) -> RedisResult<Value> {
let initial_value = match op {
LogicalAggregateOp::And => true,
};
let results = values.into_iter().try_fold(Vec::new(), |acc, curr| {
let values = match curr {
Value::Array(values) => values,
_ => {
return RedisResult::Err(
(
ErrorKind::TypeError,
"expected array of integers as response",
)
.into(),
);
}
};
let mut acc = if acc.is_empty() {
vec![initial_value; values.len()]
} else {
acc
};
for (index, value) in values.into_iter().enumerate() {
let int = match value {
Value::Int(int) => int,
_ => {
return Err((
ErrorKind::TypeError,
"expected array of integers as response",
)
.into());
}
};
acc[index] = match op {
LogicalAggregateOp::And => acc[index] && (int > 0),
};
}
Ok(acc)
})?;
Ok(Value::Array(
results
.into_iter()
.map(|result| Value::Int(result as i64))
.collect(),
))
}
/// Aggregate array responses into a single map.
pub fn combine_map_results(values: Vec<Value>) -> RedisResult<Value> {
let mut map: HashMap<Vec<u8>, i64> = HashMap::new();
for value in values {
match value {
Value::Array(elements) => {
let mut iter = elements.into_iter();
while let Some(key) = iter.next() {
if let Value::BulkString(key_bytes) = key {
if let Some(Value::Int(value)) = iter.next() {
*map.entry(key_bytes).or_insert(0) += value;
} else {
return Err((ErrorKind::TypeError, "expected integer value").into());
}
} else {
return Err((ErrorKind::TypeError, "expected string key").into());
}
}
}
_ => {
return Err((ErrorKind::TypeError, "expected array of values as response").into());
}
}
}
let result_vec: Vec<(Value, Value)> = map
.into_iter()
.map(|(k, v)| (Value::BulkString(k), Value::Int(v)))
.collect();
Ok(Value::Map(result_vec))
}
/// Aggregate array responses into a single array.
pub fn combine_array_results(values: Vec<Value>) -> RedisResult<Value> {
let mut results = Vec::new();
for value in values {
match value {
Value::Array(values) => results.extend(values),
_ => {
return Err((ErrorKind::TypeError, "expected array of values as response").into());
}
}
}
Ok(Value::Array(results))
}
/// Combines multiple call results in the `values` field, each assume to be an array of results,
/// into a single array. `sorting_order` defines the order of the results in the returned array -
/// for each array of results, `sorting_order` should contain a matching array with the indices of
/// the results in the final array.
pub(crate) fn combine_and_sort_array_results<'a>(
values: Vec<Value>,
sorting_order: impl ExactSizeIterator<Item = &'a Vec<usize>>,
) -> RedisResult<Value> {
let mut results = Vec::new();
results.resize(
values.iter().fold(0, |acc, value| match value {
Value::Array(values) => values.len() + acc,
_ => 0,
}),
Value::Nil,
);
assert_eq!(values.len(), sorting_order.len());
for (key_indices, value) in sorting_order.into_iter().zip(values) {
match value {
Value::Array(values) => {
assert_eq!(values.len(), key_indices.len());
for (index, value) in key_indices.iter().zip(values) {
results[*index] = value;
}
}
_ => {
return Err((ErrorKind::TypeError, "expected array of values as response").into());
}
}
}
Ok(Value::Array(results))
}
fn get_route(is_readonly: bool, key: &[u8]) -> Route {
let slot = get_slot(key);
if is_readonly {
Route::new(slot, SlotAddr::ReplicaOptional)
} else {
Route::new(slot, SlotAddr::Master)
}
}
/// Takes the given `routable` and creates a multi-slot routing info.
/// This is used for commands like MSET & MGET, where if the command's keys
/// are hashed to multiple slots, the command should be split into sub-commands,
/// each targetting a single slot. The results of these sub-commands are then
/// usually reassembled using `combine_and_sort_array_results`. In order to do this,
/// `MultipleNodeRoutingInfo::MultiSlot` contains the routes for each sub-command, and
/// the indices in the final combined result for each result from the sub-command.
///
/// If all keys are routed to the same slot, there's no need to split the command,
/// so a single node routing info will be returned.
fn multi_shard<R>(
routable: &R,
cmd: &[u8],
first_key_index: usize,
has_values: bool,
) -> Option<RoutingInfo>
where
R: Routable + ?Sized,
{
let is_readonly = is_readonly_cmd(cmd);
let mut routes = HashMap::new();
let mut key_index = 0;
while let Some(key) = routable.arg_idx(first_key_index + key_index) {
let route = get_route(is_readonly, key);
let entry = routes.entry(route);
let keys = entry.or_insert(Vec::new());
keys.push(key_index);
if has_values {
key_index += 1;
routable.arg_idx(first_key_index + key_index)?; // check that there's a value for the key
keys.push(key_index);
}
key_index += 1;
}
let mut routes: Vec<(Route, Vec<usize>)> = routes.into_iter().collect();
Some(if routes.len() == 1 {
RoutingInfo::SingleNode(SingleNodeRoutingInfo::SpecificNode(routes.pop().unwrap().0))
} else {
RoutingInfo::MultiNode((
MultipleNodeRoutingInfo::MultiSlot(routes),
ResponsePolicy::for_command(cmd),
))
})
}
impl ResponsePolicy {
/// Parse the command for the matching response policy.
pub fn for_command(cmd: &[u8]) -> Option<ResponsePolicy> {
match cmd {
b"SCRIPT EXISTS" => Some(ResponsePolicy::AggregateLogical(LogicalAggregateOp::And)),
b"DBSIZE" | b"DEL" | b"EXISTS" | b"SLOWLOG LEN" | b"TOUCH" | b"UNLINK"
| b"LATENCY RESET" | b"PUBSUB NUMPAT" => {
Some(ResponsePolicy::Aggregate(AggregateOp::Sum))
}
b"WAIT" => Some(ResponsePolicy::Aggregate(AggregateOp::Min)),
b"ACL SETUSER" | b"ACL DELUSER" | b"ACL SAVE" | b"CLIENT SETNAME"
| b"CLIENT SETINFO" | b"CONFIG SET" | b"CONFIG RESETSTAT" | b"CONFIG REWRITE"
| b"FLUSHALL" | b"FLUSHDB" | b"FUNCTION DELETE" | b"FUNCTION FLUSH"
| b"FUNCTION LOAD" | b"FUNCTION RESTORE" | b"MEMORY PURGE" | b"MSET" | b"PING"
| b"SCRIPT FLUSH" | b"SCRIPT LOAD" | b"SLOWLOG RESET" | b"UNWATCH" | b"WATCH" => {
Some(ResponsePolicy::AllSucceeded)
}
b"KEYS" | b"MGET" | b"SLOWLOG GET" | b"PUBSUB CHANNELS" | b"PUBSUB SHARDCHANNELS" => {
Some(ResponsePolicy::CombineArrays)
}
b"PUBSUB NUMSUB" | b"PUBSUB SHARDNUMSUB" => Some(ResponsePolicy::CombineMaps),
b"FUNCTION KILL" | b"SCRIPT KILL" => Some(ResponsePolicy::OneSucceeded),
// This isn't based on response_tips, but on the discussion here - https://github.com/redis/redis/issues/12410
b"RANDOMKEY" => Some(ResponsePolicy::FirstSucceededNonEmptyOrAllEmpty),
b"LATENCY GRAPH" | b"LATENCY HISTOGRAM" | b"LATENCY HISTORY" | b"LATENCY DOCTOR"
| b"LATENCY LATEST" => Some(ResponsePolicy::Special),
b"FUNCTION STATS" => Some(ResponsePolicy::Special),
b"MEMORY MALLOC-STATS" | b"MEMORY DOCTOR" | b"MEMORY STATS" => {
Some(ResponsePolicy::Special)
}
b"INFO" => Some(ResponsePolicy::Special),
_ => None,
}
}
}
enum RouteBy {
AllNodes,
AllPrimaries,
FirstKey,
MultiShardNoValues,
MultiShardWithValues,
Random,
SecondArg,
SecondArgAfterKeyCount,
SecondArgSlot,
StreamsIndex,
ThirdArgAfterKeyCount,
Undefined,
}
fn base_routing(cmd: &[u8]) -> RouteBy {
match cmd {
b"ACL SETUSER"
| b"ACL DELUSER"
| b"ACL SAVE"
| b"CLIENT SETNAME"
| b"CLIENT SETINFO"
| b"SLOWLOG GET"
| b"SLOWLOG LEN"
| b"SLOWLOG RESET"
| b"CONFIG SET"
| b"CONFIG RESETSTAT"
| b"CONFIG REWRITE"
| b"SCRIPT FLUSH"
| b"SCRIPT LOAD"
| b"LATENCY RESET"
| b"LATENCY GRAPH"
| b"LATENCY HISTOGRAM"
| b"LATENCY HISTORY"
| b"LATENCY DOCTOR"
| b"LATENCY LATEST"
| b"PUBSUB NUMPAT"
| b"PUBSUB CHANNELS"
| b"PUBSUB NUMSUB"
| b"PUBSUB SHARDCHANNELS"
| b"PUBSUB SHARDNUMSUB" => RouteBy::AllNodes,
b"DBSIZE"
| b"FLUSHALL"
| b"FLUSHDB"
| b"FUNCTION DELETE"
| b"FUNCTION FLUSH"
| b"FUNCTION KILL"
| b"FUNCTION LOAD"
| b"FUNCTION RESTORE"
| b"FUNCTION STATS"
| b"INFO"
| b"KEYS"
| b"MEMORY DOCTOR"
| b"MEMORY MALLOC-STATS"
| b"MEMORY PURGE"
| b"MEMORY STATS"
| b"PING"
| b"SCRIPT EXISTS"
| b"SCRIPT KILL"
| b"UNWATCH"
| b"WAIT"
| b"RANDOMKEY"
| b"WAITAOF" => RouteBy::AllPrimaries,
b"MGET" | b"DEL" | b"EXISTS" | b"UNLINK" | b"TOUCH" | b"WATCH" => {
RouteBy::MultiShardNoValues
}
b"MSET" => RouteBy::MultiShardWithValues,
// TODO - special handling - b"SCAN"
b"SCAN" | b"SHUTDOWN" | b"SLAVEOF" | b"REPLICAOF" => RouteBy::Undefined,
b"BLMPOP" | b"BZMPOP" | b"EVAL" | b"EVALSHA" | b"EVALSHA_RO" | b"EVAL_RO" | b"FCALL"
| b"FCALL_RO" => RouteBy::ThirdArgAfterKeyCount,
b"BITOP"
| b"MEMORY USAGE"
| b"PFDEBUG"
| b"XGROUP CREATE"
| b"XGROUP CREATECONSUMER"
| b"XGROUP DELCONSUMER"
| b"XGROUP DESTROY"
| b"XGROUP SETID"
| b"XINFO CONSUMERS"
| b"XINFO GROUPS"
| b"XINFO STREAM"
| b"OBJECT ENCODING"
| b"OBJECT FREQ"
| b"OBJECT IDLETIME"
| b"OBJECT REFCOUNT" => RouteBy::SecondArg,
b"LMPOP" | b"SINTERCARD" | b"ZDIFF" | b"ZINTER" | b"ZINTERCARD" | b"ZMPOP" | b"ZUNION" => {
RouteBy::SecondArgAfterKeyCount
}
b"XREAD" | b"XREADGROUP" => RouteBy::StreamsIndex,
// keyless commands with more arguments, whose arguments might be wrongly taken to be keys.
// TODO - double check these, in order to find better ways to route some of them.
b"ACL DRYRUN"
| b"ACL GENPASS"
| b"ACL GETUSER"
| b"ACL HELP"
| b"ACL LIST"
| b"ACL LOG"
| b"ACL USERS"
| b"ACL WHOAMI"
| b"AUTH"
| b"BGSAVE"
| b"CLIENT GETNAME"
| b"CLIENT GETREDIR"
| b"CLIENT ID"
| b"CLIENT INFO"
| b"CLIENT KILL"
| b"CLIENT PAUSE"
| b"CLIENT REPLY"
| b"CLIENT TRACKINGINFO"
| b"CLIENT UNBLOCK"
| b"CLIENT UNPAUSE"
| b"CLUSTER COUNT-FAILURE-REPORTS"
| b"CLUSTER INFO"
| b"CLUSTER KEYSLOT"
| b"CLUSTER MEET"
| b"CLUSTER MYSHARDID"
| b"CLUSTER NODES"
| b"CLUSTER REPLICAS"
| b"CLUSTER RESET"
| b"CLUSTER SET-CONFIG-EPOCH"
| b"CLUSTER SHARDS"
| b"CLUSTER SLOTS"
| b"COMMAND COUNT"
| b"COMMAND GETKEYS"
| b"COMMAND LIST"
| b"COMMAND"
| b"CONFIG GET"
| b"DEBUG"
| b"ECHO"
| b"FUNCTION LIST"
| b"LASTSAVE"
| b"LOLWUT"
| b"MODULE LIST"
| b"MODULE LOAD"
| b"MODULE LOADEX"
| b"MODULE UNLOAD"
| b"READONLY"
| b"READWRITE"
| b"SAVE"
| b"TFCALL"
| b"TFCALLASYNC"
| b"TFUNCTION DELETE"
| b"TFUNCTION LIST"
| b"TFUNCTION LOAD"
| b"TIME" => RouteBy::Random,
b"CLUSTER ADDSLOTS"
| b"CLUSTER COUNTKEYSINSLOT"
| b"CLUSTER DELSLOTS"
| b"CLUSTER DELSLOTSRANGE"
| b"CLUSTER GETKEYSINSLOT"
| b"CLUSTER SETSLOT" => RouteBy::SecondArgSlot,
_ => RouteBy::FirstKey,
}
}
impl RoutingInfo {
/// Returns true if the `cmd` should be routed to all nodes.
pub fn is_all_nodes(cmd: &[u8]) -> bool {
matches!(base_routing(cmd), RouteBy::AllNodes)
}
/// Returns true if the `cmd` is a key-based command that triggers MOVED errors.
/// A key-based command is one that will be accepted only by the slot owner,
/// while other nodes will respond with a MOVED error redirecting to the relevant primary owner.
pub fn is_key_routing_command(cmd: &[u8]) -> bool {
match base_routing(cmd) {
RouteBy::FirstKey
| RouteBy::SecondArg
| RouteBy::SecondArgAfterKeyCount
| RouteBy::ThirdArgAfterKeyCount
| RouteBy::SecondArgSlot
| RouteBy::StreamsIndex
| RouteBy::MultiShardNoValues
| RouteBy::MultiShardWithValues => {
if matches!(cmd, b"SPUBLISH") {
// SPUBLISH does not return MOVED errors within the slot's shard. This means that even if READONLY wasn't sent to a replica,
// executing SPUBLISH FOO BAR on that replica will succeed. This behavior differs from true key-based commands,
// such as SET FOO BAR, where a non-readonly replica would return a MOVED error if READONLY is off.
// Consequently, SPUBLISH does not meet the requirement of being a command that triggers MOVED errors.
// TODO: remove this when PRIMARY_PREFERRED route for SPUBLISH is added
false
} else {
true
}
}
RouteBy::AllNodes | RouteBy::AllPrimaries | RouteBy::Random | RouteBy::Undefined => {
false
}
}
}
/// Returns the routing info for `r`.
pub fn for_routable<R>(r: &R) -> Option<RoutingInfo>
where
R: Routable + ?Sized,
{
let cmd = &r.command()?[..];
match base_routing(cmd) {
RouteBy::AllNodes => Some(RoutingInfo::MultiNode((
MultipleNodeRoutingInfo::AllNodes,
ResponsePolicy::for_command(cmd),
))),
RouteBy::AllPrimaries => Some(RoutingInfo::MultiNode((
MultipleNodeRoutingInfo::AllMasters,
ResponsePolicy::for_command(cmd),
))),
RouteBy::MultiShardWithValues => multi_shard(r, cmd, 1, true),
RouteBy::MultiShardNoValues => multi_shard(r, cmd, 1, false),
RouteBy::Random => Some(RoutingInfo::SingleNode(SingleNodeRoutingInfo::Random)),
RouteBy::ThirdArgAfterKeyCount => {
let key_count = r
.arg_idx(2)
.and_then(|x| std::str::from_utf8(x).ok())
.and_then(|x| x.parse::<u64>().ok())?;
if key_count == 0 {
Some(RoutingInfo::SingleNode(SingleNodeRoutingInfo::Random))
} else {
r.arg_idx(3).map(|key| RoutingInfo::for_key(cmd, key))
}
}
RouteBy::SecondArg => r.arg_idx(2).map(|key| RoutingInfo::for_key(cmd, key)),
RouteBy::SecondArgAfterKeyCount => {
let key_count = r
.arg_idx(1)
.and_then(|x| std::str::from_utf8(x).ok())
.and_then(|x| x.parse::<u64>().ok())?;
if key_count == 0 {
Some(RoutingInfo::SingleNode(SingleNodeRoutingInfo::Random))
} else {
r.arg_idx(2).map(|key| RoutingInfo::for_key(cmd, key))
}
}
RouteBy::StreamsIndex => {
let streams_position = r.position(b"STREAMS")?;
r.arg_idx(streams_position + 1)
.map(|key| RoutingInfo::for_key(cmd, key))
}
RouteBy::SecondArgSlot => r
.arg_idx(2)
.and_then(|arg| std::str::from_utf8(arg).ok())
.and_then(|slot| slot.parse::<u16>().ok())
.map(|slot| {
RoutingInfo::SingleNode(SingleNodeRoutingInfo::SpecificNode(Route::new(
slot,
SlotAddr::Master,
)))
}),
RouteBy::FirstKey => match r.arg_idx(1) {
Some(key) => Some(RoutingInfo::for_key(cmd, key)),
None => Some(RoutingInfo::SingleNode(SingleNodeRoutingInfo::Random)),
},
RouteBy::Undefined => None,
}
}
fn for_key(cmd: &[u8], key: &[u8]) -> RoutingInfo {
RoutingInfo::SingleNode(SingleNodeRoutingInfo::SpecificNode(get_route(
is_readonly_cmd(cmd),
key,
)))
}
}
/// Returns true if the given `routable` represents a readonly command.
pub fn is_readonly(routable: &impl Routable) -> bool {
match routable.command() {
Some(cmd) => is_readonly_cmd(cmd.as_slice()),
None => false,
}
}
/// Returns `true` if the given `cmd` is a readonly command.
pub fn is_readonly_cmd(cmd: &[u8]) -> bool {
matches!(
cmd,
b"BITCOUNT"
| b"BITFIELD_RO"
| b"BITPOS"
| b"DBSIZE"
| b"DUMP"
| b"EVAL_RO"
| b"EVALSHA_RO"
| b"EXISTS"
| b"EXPIRETIME"
| b"FCALL_RO"
| b"FUNCTION DUMP"
| b"FUNCTION KILL"
| b"FUNCTION LIST"
| b"FUNCTION STATS"
| b"GEODIST"
| b"GEOHASH"
| b"GEOPOS"
| b"GEORADIUSBYMEMBER_RO"
| b"GEORADIUS_RO"
| b"GEOSEARCH"
| b"GET"
| b"GETBIT"
| b"GETRANGE"
| b"HEXISTS"
| b"HGET"
| b"HGETALL"
| b"HKEYS"
| b"HLEN"
| b"HMGET"
| b"HRANDFIELD"
| b"HSCAN"
| b"HSTRLEN"
| b"HVALS"
| b"KEYS"
| b"LCS"
| b"LINDEX"
| b"LLEN"
| b"LOLWUT"
| b"LPOS"
| b"LRANGE"
| b"MEMORY USAGE"
| b"MGET"
| b"OBJECT ENCODING"
| b"OBJECT FREQ"
| b"OBJECT IDLETIME"
| b"OBJECT REFCOUNT"
| b"PEXPIRETIME"
| b"PFCOUNT"
| b"PTTL"
| b"RANDOMKEY"
| b"SCAN"
| b"SCARD"
| b"SCRIPT DEBUG"
| b"SCRIPT EXISTS"
| b"SCRIPT FLUSH"
| b"SCRIPT KILL"
| b"SCRIPT LOAD"
| b"SDIFF"
| b"SINTER"
| b"SINTERCARD"
| b"SISMEMBER"
| b"SMEMBERS"
| b"SMISMEMBER"
| b"SORT_RO"
| b"SRANDMEMBER"
| b"SSCAN"
| b"STRLEN"
| b"SUBSTR"
| b"SUNION"
| b"TOUCH"
| b"TTL"
| b"TYPE"
| b"XINFO CONSUMERS"
| b"XINFO GROUPS"
| b"XINFO STREAM"
| b"XLEN"
| b"XPENDING"
| b"XRANGE"
| b"XREAD"
| b"XREVRANGE"
| b"ZCARD"
| b"ZCOUNT"
| b"ZDIFF"
| b"ZINTER"
| b"ZINTERCARD"
| b"ZLEXCOUNT"
| b"ZMSCORE"
| b"ZRANDMEMBER"
| b"ZRANGE"
| b"ZRANGEBYLEX"
| b"ZRANGEBYSCORE"
| b"ZRANK"
| b"ZREVRANGE"
| b"ZREVRANGEBYLEX"
| b"ZREVRANGEBYSCORE"
| b"ZREVRANK"
| b"ZSCAN"
| b"ZSCORE"
| b"ZUNION"
)
}
/// Objects that implement this trait define a request that can be routed by a cluster client to different nodes in the cluster.
pub trait Routable {
/// Convenience function to return ascii uppercase version of the
/// the first argument (i.e., the command).
fn command(&self) -> Option<Vec<u8>> {
let primary_command = self.arg_idx(0).map(|x| x.to_ascii_uppercase())?;
let mut primary_command = match primary_command.as_slice() {
b"XGROUP" | b"OBJECT" | b"SLOWLOG" | b"FUNCTION" | b"MODULE" | b"COMMAND"
| b"PUBSUB" | b"CONFIG" | b"MEMORY" | b"XINFO" | b"CLIENT" | b"ACL" | b"SCRIPT"
| b"CLUSTER" | b"LATENCY" => primary_command,
_ => {
return Some(primary_command);
}
};
Some(match self.arg_idx(1) {
Some(secondary_command) => {
let previous_len = primary_command.len();
primary_command.reserve(secondary_command.len() + 1);
primary_command.extend(b" ");
primary_command.extend(secondary_command);
let current_len = primary_command.len();
primary_command[previous_len + 1..current_len].make_ascii_uppercase();
primary_command
}
None => primary_command,
})
}
/// Returns a reference to the data for the argument at `idx`.
fn arg_idx(&self, idx: usize) -> Option<&[u8]>;
/// Returns index of argument that matches `candidate`, if it exists
fn position(&self, candidate: &[u8]) -> Option<usize>;
}
impl Routable for Cmd {
fn arg_idx(&self, idx: usize) -> Option<&[u8]> {
self.arg_idx(idx)
}
fn position(&self, candidate: &[u8]) -> Option<usize> {
self.args_iter().position(|a| match a {
Arg::Simple(d) => d.eq_ignore_ascii_case(candidate),
_ => false,
})
}
}
impl Routable for Value {
fn arg_idx(&self, idx: usize) -> Option<&[u8]> {
match self {
Value::Array(args) => match args.get(idx) {
Some(Value::BulkString(ref data)) => Some(&data[..]),
_ => None,
},
_ => None,
}
}
fn position(&self, candidate: &[u8]) -> Option<usize> {
match self {
Value::Array(args) => args.iter().position(|a| match a {
Value::BulkString(d) => d.eq_ignore_ascii_case(candidate),
_ => false,
}),
_ => None,
}
}
}
#[derive(Debug, Hash)]
pub(crate) struct Slot {
pub(crate) start: u16,
pub(crate) end: u16,
pub(crate) master: String,
pub(crate) replicas: Vec<String>,
}
impl Slot {
pub fn new(s: u16, e: u16, m: String, r: Vec<String>) -> Self {
Self {
start: s,
end: e,
master: m,
replicas: r,
}
}
pub fn start(&self) -> u16 {
self.start
}
pub fn end(&self) -> u16 {
self.end
}
#[allow(dead_code)] // used in tests
pub(crate) fn master(&self) -> &str {
self.master.as_str()
}
#[allow(dead_code)] // used in tests
pub fn replicas(&self) -> Vec<String> {
self.replicas.clone()
}
}
/// What type of node should a request be routed to, assuming read from replica is enabled.
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
pub enum SlotAddr {
/// The request must be routed to primary node
Master,
/// The request may be routed to a replica node.
/// For example, a GET command can be routed either to replica or primary.
ReplicaOptional,
/// The request must be routed to replica node, if one exists.
/// For example, by user requested routing.
ReplicaRequired,
}
/// This is just a simplified version of [`Slot`],
/// which stores only the master and [optional] replica
/// to avoid the need to choose a replica each time
/// a command is executed
#[derive(Debug, Eq, PartialEq)]
pub(crate) struct SlotAddrs {
pub(crate) primary: String,
pub(crate) replicas: Vec<String>,
}
impl SlotAddrs {
pub(crate) fn new(primary: String, replicas: Vec<String>) -> Self {
Self { primary, replicas }
}
pub(crate) fn from_slot(slot: Slot) -> Self {
SlotAddrs::new(slot.master, slot.replicas)
}
}
impl<'a> IntoIterator for &'a SlotAddrs {
type Item = &'a String;
type IntoIter = std::iter::Chain<Once<&'a String>, std::slice::Iter<'a, String>>;
fn into_iter(self) -> Self::IntoIter {
std::iter::once(&self.primary).chain(self.replicas.iter())
}
}
/// Defines the slot and the [`SlotAddr`] to which
/// a command should be sent
#[derive(Eq, PartialEq, Clone, Copy, Debug, Hash)]
pub struct Route(u16, SlotAddr);
impl Route {
/// Returns a new Route.
pub fn new(slot: u16, slot_addr: SlotAddr) -> Self {
Self(slot, slot_addr)
}
/// Returns the slot number of the route.
pub fn slot(&self) -> u16 {
self.0
}
/// Returns the slot address of the route.
pub fn slot_addr(&self) -> SlotAddr {
self.1
}
}
#[cfg(test)]
mod tests {
use super::{
command_for_multi_slot_indices, AggregateOp, MultipleNodeRoutingInfo, ResponsePolicy,
Route, RoutingInfo, SingleNodeRoutingInfo, SlotAddr,
};
use crate::{cluster_topology::slot, cmd, parser::parse_redis_value, Value};
use core::panic;
#[test]
fn test_routing_info_mixed_capatalization() {
let mut upper = cmd("XREAD");
upper.arg("STREAMS").arg("foo").arg(0);
let mut lower = cmd("xread");
lower.arg("streams").arg("foo").arg(0);
assert_eq!(
RoutingInfo::for_routable(&upper).unwrap(),
RoutingInfo::for_routable(&lower).unwrap()
);
let mut mixed = cmd("xReAd");
mixed.arg("StReAmS").arg("foo").arg(0);
assert_eq!(
RoutingInfo::for_routable(&lower).unwrap(),
RoutingInfo::for_routable(&mixed).unwrap()
);
}
#[test]
fn test_routing_info() {
let mut test_cmds = vec![];
// RoutingInfo::AllMasters
let mut test_cmd = cmd("FLUSHALL");
test_cmd.arg("");
test_cmds.push(test_cmd);
// RoutingInfo::AllNodes
test_cmd = cmd("ECHO");
test_cmd.arg("");
test_cmds.push(test_cmd);
// Routing key is 2nd arg ("42")
test_cmd = cmd("SET");
test_cmd.arg("42");
test_cmds.push(test_cmd);