forked from aembke/fred.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.rs
1316 lines (1169 loc) · 41.5 KB
/
responses.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 crate::error::{RedisError, RedisErrorKind};
use crate::modules::inner::RedisClientInner;
use crate::multiplexer::utils;
use crate::multiplexer::{Counters, SentCommand, SentCommands};
use crate::protocol::types::{RedisCommandKind, ResponseKind, ValueScanInner, ValueScanResult};
use crate::protocol::utils as protocol_utils;
use crate::protocol::utils::{frame_to_error, frame_to_single_result};
use crate::trace;
use crate::types::{HScanResult, KeyspaceEvent, RedisKey, RedisValue, SScanResult, ScanResult, ZScanResult};
use crate::utils as client_utils;
use bytes_utils::Str;
use parking_lot::{Mutex, RwLock};
use redis_protocol::resp3::types::Frame as Resp3Frame;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::sync::Arc;
#[cfg(feature = "custom-reconnect-errors")]
use crate::globals::globals;
#[cfg(feature = "metrics")]
use crate::modules::metrics::MovingStats;
#[cfg(feature = "metrics")]
use std::cmp;
#[cfg(feature = "metrics")]
use std::time::Instant;
const LAST_CURSOR: &'static str = "0";
const KEYSPACE_PREFIX: &'static str = "__keyspace@";
const KEYEVENT_PREFIX: &'static str = "__keyevent@";
#[derive(Clone, Debug, Eq, PartialEq)]
enum TransactionEnded {
Exec,
Discard,
}
#[cfg(feature = "metrics")]
fn sample_latency(latency_stats: &RwLock<MovingStats>, sent: Instant) {
let dur = Instant::now().duration_since(sent);
let dur_ms = cmp::max(0, (dur.as_secs() * 1000) + dur.subsec_millis() as u64) as i64;
latency_stats.write().sample(dur_ms);
}
/// Sample overall and network latency values for a command.
#[cfg(feature = "metrics")]
fn sample_command_latencies(inner: &Arc<RedisClientInner>, command: &mut SentCommand) {
if let Some(sent) = command.network_start.take() {
sample_latency(&inner.network_latency_stats, sent);
}
sample_latency(&inner.latency_stats, command.command.sent);
}
#[cfg(not(feature = "metrics"))]
fn sample_command_latencies(_: &Arc<RedisClientInner>, _: &mut SentCommand) {}
/// Merge multiple potentially nested frames into one flat array of frames.
fn merge_multiple_frames(frames: &mut VecDeque<Resp3Frame>) -> Resp3Frame {
let inner_len = frames.iter().fold(0, |count, frame| {
count
+ match frame {
Resp3Frame::Array { ref data, .. } => data.len(),
Resp3Frame::Push { ref data, .. } => data.len(),
_ => 1,
}
});
let mut out = Vec::with_capacity(inner_len);
for frame in frames.drain(..) {
match frame {
Resp3Frame::Array { data, .. } | Resp3Frame::Push { data, .. } => {
for inner_frame in data.into_iter() {
out.push(inner_frame);
}
}
_ => out.push(frame),
};
}
Resp3Frame::Array {
data: out,
attributes: None,
}
}
/// Update the SCAN cursor on a command, changing the internal cursor and the arguments array for the next call to SCAN.
fn update_scan_cursor(inner: &Arc<RedisClientInner>, last_command: &mut SentCommand, cursor: Str) {
if last_command.command.kind.is_scan() {
last_command.command.args[0] = cursor.clone().into();
} else if last_command.command.kind.is_value_scan() {
last_command.command.args[1] = cursor.clone().into();
}
let old_cursor = match last_command.command.kind {
RedisCommandKind::Scan(ref mut inner) => &mut inner.cursor,
RedisCommandKind::Hscan(ref mut inner) => &mut inner.cursor,
RedisCommandKind::Sscan(ref mut inner) => &mut inner.cursor,
RedisCommandKind::Zscan(ref mut inner) => &mut inner.cursor,
_ => {
_warn!(inner, "Failed to update cursor. Invalid command kind.");
return;
}
};
*old_cursor = cursor;
}
/// Parse the output of a command that scans keys.
fn handle_key_scan_result(frame: Resp3Frame) -> Result<(Str, Vec<RedisKey>), RedisError> {
if let Resp3Frame::Array { mut data, .. } = frame {
if data.len() == 2 {
let cursor = match protocol_utils::frame_to_str(&data[0]) {
Some(s) => s,
None => {
return Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected first SCAN result element to be a bulk string.",
))
}
};
if let Some(Resp3Frame::Array { data, .. }) = data.pop() {
let mut keys = Vec::with_capacity(data.len());
for frame in data.into_iter() {
let key = match protocol_utils::frame_to_bytes(&frame) {
Some(s) => s,
None => {
return Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected an array of strings from second SCAN result.",
))
}
};
keys.push(key.into());
}
Ok((cursor, keys))
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected second SCAN result element to be an array.",
))
}
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected two-element bulk string array from SCAN.",
))
}
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected bulk string array from SCAN.",
))
}
}
/// Parse the output of a command that scans values.
fn handle_value_scan_result(frame: Resp3Frame) -> Result<(Str, Vec<RedisValue>), RedisError> {
if let Resp3Frame::Array { mut data, .. } = frame {
if data.len() == 2 {
let cursor = match protocol_utils::frame_to_str(&data[0]) {
Some(s) => s,
None => {
return Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected first result element to be a bulk string.",
))
}
};
if let Some(Resp3Frame::Array { data, .. }) = data.pop() {
let mut values = Vec::with_capacity(data.len());
for frame in data.into_iter() {
values.push(frame_to_single_result(frame)?);
}
Ok((cursor, values))
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected second result element to be an array.",
))
}
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected two-element bulk string array.",
))
}
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Expected bulk string array.",
))
}
}
/// Send the output to the caller of a command that scans keys.
fn send_key_scan_result(
inner: &Arc<RedisClientInner>,
last_command: SentCommand,
result: Vec<RedisKey>,
can_continue: bool,
) -> Result<(), RedisError> {
if let RedisCommandKind::Scan(scan_state) = last_command.command.kind {
let tx = scan_state.tx.clone();
let scan_result = ScanResult {
can_continue,
inner: inner.clone(),
scan_state,
args: last_command.command.args,
results: Some(result),
};
if let Err(_) = tx.send(Ok(scan_result)) {
_warn!(inner, "Failed to send SCAN callback result.");
}
Ok(())
} else {
Err(RedisError::new(
RedisErrorKind::Unknown,
"Invalid redis command. Expected SCAN.",
))
}
}
/// Send an error to the caller of a command that scans keys.
fn send_key_scan_error(
inner: &Arc<RedisClientInner>,
last_command: &SentCommand,
e: RedisError,
) -> Result<(), RedisError> {
if let RedisCommandKind::Scan(ref scan_state) = last_command.command.kind {
if let Err(_) = scan_state.tx.send(Err(e)) {
_warn!(inner, "Failed to send SCAN callback error.");
}
Ok(())
} else {
Err(RedisError::new(
RedisErrorKind::Unknown,
"Invalid redis command. Expected SCAN.",
))
}
}
/// Send the output to the caller of a command that scans values.
fn send_value_scan_result(
inner: &Arc<RedisClientInner>,
last_command: SentCommand,
result: Vec<RedisValue>,
can_continue: bool,
) -> Result<(), RedisError> {
let args = last_command.command.args;
match last_command.command.kind {
RedisCommandKind::Zscan(scan_state) => {
let tx = scan_state.tx.clone();
let results = ValueScanInner::transform_zscan_result(result)?;
let state = ValueScanResult::ZScan(ZScanResult {
can_continue,
inner: inner.clone(),
scan_state,
args,
results: Some(results),
});
if let Err(_) = tx.send(Ok(state)) {
_warn!(inner, "Failed to send ZSCAN result to caller");
}
}
RedisCommandKind::Sscan(scan_state) => {
let tx = scan_state.tx.clone();
let state = ValueScanResult::SScan(SScanResult {
can_continue,
inner: inner.clone(),
scan_state,
args,
results: Some(result),
});
if let Err(_) = tx.send(Ok(state)) {
_warn!(inner, "Failed to send SSCAN result to caller");
}
}
RedisCommandKind::Hscan(scan_state) => {
let tx = scan_state.tx.clone();
let results = ValueScanInner::transform_hscan_result(result)?;
let state = ValueScanResult::HScan(HScanResult {
can_continue,
inner: inner.clone(),
scan_state,
args,
results: Some(results),
});
if let Err(_) = tx.send(Ok(state)) {
_warn!(inner, "Failed to send HSCAN result to caller");
}
}
_ => {
return Err(RedisError::new(
RedisErrorKind::Unknown,
"Invalid redis command. Expected HSCAN, SSCAN, or ZSCAN.",
))
}
};
Ok(())
}
/// Send an error to the caller of a command that scans values.
fn send_value_scan_error(
inner: &Arc<RedisClientInner>,
last_command: &SentCommand,
e: RedisError,
) -> Result<(), RedisError> {
let scan_state = match last_command.command.kind {
RedisCommandKind::Zscan(ref inner) => inner,
RedisCommandKind::Sscan(ref inner) => inner,
RedisCommandKind::Hscan(ref inner) => inner,
_ => {
return Err(RedisError::new(
RedisErrorKind::Unknown,
"Invalid redis command. Expected HSCAN, SSCAN, or ZSCAN.",
))
}
};
if let Err(_) = scan_state.tx.send(Err(e)) {
_warn!(inner, "Failed to respond to caller with value scan result.");
}
Ok(())
}
/// Emit `(channel, message)` tuples on the pubsub interface, closing any senders for whom the receiver has been dropped.
fn emit_pubsub_message(inner: &Arc<RedisClientInner>, channel: String, message: RedisValue) {
let mut to_remove = BTreeSet::new();
// check for closed senders as we emit messages, and drop them at the end
{
for (idx, tx) in inner.message_tx.read().iter().enumerate() {
if let Err(_) = tx.send((channel.clone(), message.clone())) {
to_remove.insert(idx);
}
}
}
if !to_remove.is_empty() {
_trace!(inner, "Removing {} closed pubsub listeners", to_remove.len());
let mut message_tx_guard = inner.message_tx.write();
let message_tx_ref = &mut *message_tx_guard;
let mut new_listeners = VecDeque::with_capacity(message_tx_ref.len() - to_remove.len());
for (idx, tx) in message_tx_ref.drain(..).enumerate() {
if !to_remove.contains(&idx) {
new_listeners.push_back(tx);
}
}
*message_tx_ref = new_listeners;
}
}
/// Emit keyspace events on the keyspace interface, closing any senders for whom the receiver has been dropped.
fn emit_keyspace_event(inner: &Arc<RedisClientInner>, event: KeyspaceEvent) {
let mut to_remove = BTreeSet::new();
// check for closed senders as we emit messages, and drop them at the end
{
for (idx, tx) in inner.keyspace_tx.read().iter().enumerate() {
if let Err(_) = tx.send(event.clone()) {
to_remove.insert(idx);
}
}
}
if !to_remove.is_empty() {
_trace!(inner, "Removing {} closed keyspace listeners", to_remove.len());
let mut message_tx_guard = inner.message_tx.write();
let message_tx_ref = &mut *message_tx_guard;
let mut new_listeners = VecDeque::with_capacity(message_tx_ref.len() - to_remove.len());
for (idx, tx) in message_tx_ref.drain(..).enumerate() {
if !to_remove.contains(&idx) {
new_listeners.push_back(tx);
}
}
*message_tx_ref = new_listeners;
}
}
/// Respond to the caller with the output of the command.
fn respond_to_caller(inner: &Arc<RedisClientInner>, last_command: SentCommand, frame: Resp3Frame) {
_trace!(
inner,
"Responding to caller for {}",
last_command.command.kind.to_str_debug()
);
if let Some(tx) = last_command.command.tx {
if let Err(_) = tx.send(Ok(frame)) {
_warn!(inner, "Failed to respond to caller.");
}
} else {
_debug!(inner, "Skip writing response to caller for command without response.");
}
}
/// Respond to the caller with an error.
fn respond_to_caller_error(inner: &Arc<RedisClientInner>, last_command: SentCommand, error: RedisError) {
_trace!(
inner,
"Responding to caller with error for {}",
last_command.command.kind.to_str_debug()
);
if let Some(tx) = last_command.command.tx {
if let Err(_) = tx.send(Err(error)) {
_warn!(inner, "Failed to respond to caller.");
}
} else {
_debug!(
inner,
"Skip writing response error to caller for command without response."
);
}
}
/// Handle a frame that came from a command that was sent to all nodes in the cluster.
async fn handle_all_nodes_response(
inner: &Arc<RedisClientInner>,
last_command: SentCommand,
frame: Resp3Frame,
) -> Option<SentCommand> {
if let Some(resp) = last_command.command.kind.all_nodes_response() {
if frame.is_error() {
check_command_resp_tx(inner, &last_command).await;
if let Some(tx) = resp.take_tx() {
let err = protocol_utils::frame_to_error(&frame).unwrap_or(RedisError::new_canceled());
if let Err(_) = tx.send(Err(err)) {
_warn!(inner, "Error sending all nodes response.");
}
} else {
_warn!(inner, "Could not send error to all nodes response sender.");
}
} else {
if resp.decr_num_nodes() == 0 {
check_command_resp_tx(inner, &last_command).await;
// if the client sent HELLO to all nodes then wait for the last response to arrive before changing the protocol version
if last_command.command.kind.is_hello() {
update_protocol_version(inner, &last_command, &frame);
}
// take the final response sender off the command and write to that
if let Some(tx) = resp.take_tx() {
_trace!(inner, "Sending all nodes response after recv all responses.");
if let Err(_) = tx.send(Ok(())) {
_warn!(inner, "Error sending all nodes response.");
}
} else {
_warn!(inner, "Could not send result to all nodes response sender.");
}
} else {
_trace!(
inner,
"Waiting on {} more responses to all nodes command",
resp.num_nodes()
);
}
}
} else {
_warn!(inner, "Command with all nodes response has no callback sender.");
}
None
}
/// Handle a response frame from a command that expects multiple top-level response frames, such as PSUBSCRIBE.
async fn handle_multiple_responses(
inner: &Arc<RedisClientInner>,
mut last_command: SentCommand,
frame: Resp3Frame,
) -> Result<Option<SentCommand>, RedisError> {
let frames = match last_command.command.kind.response_kind_mut() {
Some(kind) => {
if let ResponseKind::Multiple {
ref count,
ref mut buffer,
} = kind
{
buffer.push_back(frame);
if buffer.len() < *count {
_trace!(
inner,
"Waiting for {} more frames for request with multiple responses.",
count - buffer.len()
);
None
} else {
_trace!(inner, "Merging {} frames into one response.", buffer.len());
Some(merge_multiple_frames(buffer))
}
} else {
_warn!(inner, "Invalid command response kind. Expected multiple responses.");
return Ok(None);
}
}
None => {
_warn!(
inner,
"Failed to read multiple response kind. Dropping response frame..."
);
return Ok(None);
}
};
if let Some(frames) = frames {
check_command_resp_tx(inner, &last_command).await;
respond_to_caller(inner, last_command, frames);
Ok(None)
} else {
// more responses are expected so return the last command to be put back in the queue
Ok(Some(last_command))
}
}
/// Update the client's protocol version codec version after receiving a non-error response to HELLO.
fn update_protocol_version(inner: &Arc<RedisClientInner>, last_command: &SentCommand, frame: &Resp3Frame) {
if !frame.is_error() {
let version = match last_command.command.kind {
RedisCommandKind::Hello(ref version) => version,
RedisCommandKind::_HelloAllCluster((_, ref version)) => version,
_ => return,
};
// HELLO cannot be pipelined so this is safe
inner.switch_protocol_versions(version.clone());
}
}
/// Process the frame in the context of the last (oldest) command sent.
///
/// If the last command has more expected responses it will be returned so it can be put back on the front of the response queue.
async fn process_response(
inner: &Arc<RedisClientInner>,
server: &Arc<String>,
counters: &Counters,
mut last_command: SentCommand,
frame: Resp3Frame,
) -> Result<Option<SentCommand>, RedisError> {
_trace!(
inner,
"Processing response from {} to {} with frame kind {:?}",
server,
last_command.command.kind.to_str_debug(),
frame.kind()
);
if last_command.command.kind.has_multiple_response_kind() {
// one assumption this makes, which might not be true, is that in cases where multiple responses are sent in separate top-level response frames,
// such as PSUBSCRIBE, that those frames will all arrive without any other command responses interleaved in the middle. i _think_ this is the case,
// but there's a chance it's not when the client is pipelined. if this is not true then i'm not sure what to do here other than to make these
// types of commands non-pipelined in all cases, since there's no mechanism in the protocol to associate out-of-order responses.
return handle_multiple_responses(inner, last_command, frame).await;
} else if last_command.command.kind.is_scan() {
client_utils::decr_atomic(&counters.in_flight);
let (next_cursor, keys) = match handle_key_scan_result(frame) {
Ok(result) => result,
Err(e) => {
let _ = send_key_scan_error(inner, &last_command, e);
check_command_resp_tx(inner, &last_command).await;
return Ok(None);
}
};
let should_stop = next_cursor == LAST_CURSOR;
update_scan_cursor(inner, &mut last_command, next_cursor);
check_command_resp_tx(inner, &last_command).await;
_trace!(inner, "Sending key scan result with {} keys", keys.len());
if let Err(_) = send_key_scan_result(inner, last_command, keys, !should_stop) {
_warn!(inner, "Failed to send key scan result");
}
} else if last_command.command.kind.is_value_scan() {
client_utils::decr_atomic(&counters.in_flight);
let (next_cursor, values) = match handle_value_scan_result(frame) {
Ok(result) => result,
Err(e) => {
let _ = send_value_scan_error(inner, &last_command, e);
check_command_resp_tx(inner, &last_command).await;
return Ok(None);
}
};
let should_stop = next_cursor == LAST_CURSOR;
update_scan_cursor(inner, &mut last_command, next_cursor);
check_command_resp_tx(inner, &last_command).await;
_trace!(inner, "Sending value scan result with {} values", values.len());
if let Err(_) = send_value_scan_result(inner, last_command, values, !should_stop) {
_warn!(inner, "Failed to send value scan result");
}
} else if last_command.command.kind.is_all_cluster_nodes() {
return Ok(handle_all_nodes_response(inner, last_command, frame).await);
} else {
client_utils::decr_atomic(&counters.in_flight);
sample_command_latencies(inner, &mut last_command);
// update the protocol version after a non-error response is received from HELLO
if last_command.command.kind.is_hello() {
update_protocol_version(inner, &last_command, &frame);
}
check_command_resp_tx(inner, &last_command).await;
respond_to_caller(inner, last_command, frame);
}
Ok(None)
}
fn parse_keyspace_notification(channel: String, message: RedisValue) -> Result<KeyspaceEvent, (String, RedisValue)> {
if channel.starts_with(KEYEVENT_PREFIX) {
let parts: Vec<&str> = channel.split("@").collect();
if parts.len() != 2 {
return Err((channel, message));
}
let suffix: Vec<&str> = parts[1].split(":").collect();
if suffix.len() != 2 {
return Err((channel, message));
}
let db = match suffix[0].replace("__", "").parse::<u8>() {
Ok(db) => db,
Err(_) => return Err((channel, message)),
};
let operation = suffix[1].to_owned();
let key = match message.as_string() {
Some(k) => k,
None => return Err((channel, message)),
};
Ok(KeyspaceEvent { db, key, operation })
} else if channel.starts_with(KEYSPACE_PREFIX) {
let parts: Vec<&str> = channel.split("@").collect();
if parts.len() != 2 {
return Err((channel, message));
}
let suffix: Vec<&str> = parts[1].split(":").collect();
if suffix.len() != 2 {
return Err((channel, message));
}
let db = match suffix[0].replace("__", "").parse::<u8>() {
Ok(db) => db,
Err(_) => return Err((channel, message)),
};
let key = suffix[1].to_owned();
let operation = match message.as_string() {
Some(k) => k,
None => return Err((channel, message)),
};
Ok(KeyspaceEvent { db, key, operation })
} else {
Err((channel, message))
}
}
/// Check for the various pubsub formats for both RESP2 and RESP3.
fn check_pubsub_formats(frame: &Resp3Frame) -> (bool, bool) {
if frame.is_pubsub_message() {
return (true, false);
}
// otherwise check for RESP2 formats automatically converted to RESP3 by the codec
let data = match frame {
Resp3Frame::Array { ref data, .. } => data,
Resp3Frame::Push { ref data, .. } => data,
_ => return (false, false),
};
// RESP2 and RESP3 differ in that RESP3 contains an additional "pubsub" string frame at the start
// so here we check the frame contents according to the RESP2 pubsub rules
(
false,
(data.len() == 3 || data.len() == 4)
&& data[0]
.as_str()
.map(|s| s == "message" || s == "pmessage")
.unwrap_or(false),
)
}
/// Try to parse the frame in either RESP2 or RESP3 pubsub formats.
fn parse_pubsub_message(
frame: Resp3Frame,
is_resp3: bool,
is_resp2: bool,
) -> Result<(String, RedisValue), RedisError> {
if is_resp3 {
protocol_utils::frame_to_pubsub(frame)
} else if is_resp2 {
// this is safe to do in limited circumstances like this since RESP2 and RESP3 pubsub arrays are similar enough
protocol_utils::parse_as_resp2_pubsub(frame)
} else {
Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Invalid pubsub message.",
))
}
}
/// Check if the frame is part of a pubsub message, and if so route it to any listeners.
///
/// If not then return it to the caller for further processing.
fn check_pubsub_message(inner: &Arc<RedisClientInner>, frame: Resp3Frame) -> Option<Resp3Frame> {
// in this case using resp3 frames can cause issues, since resp3 push commands are represented
// differently than resp2 array frames. to fix this we convert back to resp2 here if needed.
let (is_resp3_pubsub, is_resp2_pubsub) = check_pubsub_formats(&frame);
if !is_resp3_pubsub && !is_resp2_pubsub {
return Some(frame);
}
let span = if inner.should_trace() {
let span = trace::create_pubsub_span(inner, &frame);
Some(span)
} else {
None
};
_trace!(inner, "Processing pubsub message.");
let parsed_frame = if let Some(ref span) = span {
let _enter = span.enter();
parse_pubsub_message(frame, is_resp3_pubsub, is_resp2_pubsub)
} else {
parse_pubsub_message(frame, is_resp3_pubsub, is_resp2_pubsub)
};
let (channel, message) = match parsed_frame {
Ok(data) => data,
Err(err) => {
_warn!(inner, "Invalid message on pubsub interface: {:?}", err);
return None;
}
};
if let Some(ref span) = span {
span.record("channel", &channel.as_str());
}
match parse_keyspace_notification(channel, message) {
Ok(event) => emit_keyspace_event(inner, event),
Err((channel, message)) => emit_pubsub_message(inner, channel, message),
};
None
}
#[cfg(feature = "reconnect-on-auth-error")]
/// Parse the response frame to see if it's an auth error.
fn parse_redis_auth_error(frame: &Resp3Frame) -> Option<RedisError> {
if frame.is_error() {
match frame_to_single_result(frame.clone()) {
Ok(_) => None,
Err(e) => match e.kind() {
RedisErrorKind::Auth => Some(e),
_ => None,
},
}
} else {
None
}
}
#[cfg(not(feature = "reconnect-on-auth-error"))]
/// Parse the response frame to see if it's an auth error.
fn parse_redis_auth_error(_frame: &Resp3Frame) -> Option<RedisError> {
None
}
/// Read the last (oldest) command from the command queue.
fn last_cluster_command(
inner: &Arc<RedisClientInner>,
commands: &Arc<Mutex<BTreeMap<Arc<String>, VecDeque<SentCommand>>>>,
server: &Arc<String>,
) -> Result<Option<SentCommand>, RedisError> {
let last_command = match commands.lock().get_mut(server) {
Some(commands) => match commands.pop_front() {
Some(cmd) => cmd,
None => {
_warn!(inner, "Recv response without a corresponding command from {}", server);
return Ok(None);
}
},
None => {
_error!(inner, "Couldn't find command queue for server {}", server);
return Err(RedisError::new(RedisErrorKind::Unknown, "Missing command queue."));
}
};
Ok(Some(last_command))
}
/// Push the last command back on the command queue.
fn add_back_last_cluster_command(
inner: &Arc<RedisClientInner>,
commands: &Arc<Mutex<BTreeMap<Arc<String>, VecDeque<SentCommand>>>>,
server: &Arc<String>,
command: SentCommand,
) -> Result<(), RedisError> {
match commands.lock().get_mut(server) {
Some(commands) => commands.push_front(command),
None => {
_error!(inner, "Couldn't find command queue for server {}", server);
return Err(RedisError::new(RedisErrorKind::Unknown, "Missing command queue."));
}
};
Ok(())
}
/// Check if the command has a response sender to unblock the multiplexer loop, and if send a message on that channel.
async fn check_command_resp_tx(inner: &Arc<RedisClientInner>, command: &SentCommand) {
if command.command.kind.is_blocking() {
inner.backchannel.write().await.set_unblocked();
}
if let Some(tx) = command.command.take_resp_tx() {
_trace!(inner, "Writing to multiplexer sender to unblock command loop.");
if let Err(e) = tx.send(()) {
_warn!(inner, "Error sending cmd loop response: {:?}", e);
}
}
}
/// Whether or not the most recent command ends a transaction.
async fn last_centralized_command_ends_transaction(commands: &Arc<Mutex<SentCommands>>) -> Option<TransactionEnded> {
commands.lock().back().and_then(|c| {
if c.command.kind.is_exec() {
Some(TransactionEnded::Exec)
} else if c.command.kind.is_discard() {
Some(TransactionEnded::Discard)
} else {
None
}
})
}
/// Whether or not the most recent command ends a transaction.
fn last_clustered_command_ends_transaction(
commands: &Arc<Mutex<BTreeMap<Arc<String>, SentCommands>>>,
server: &Arc<String>,
) -> Option<TransactionEnded> {
commands.lock().get(server).and_then(|commands| {
commands.back().and_then(|c| {
if c.command.kind.is_exec() {
Some(TransactionEnded::Exec)
} else if c.command.kind.is_discard() {
Some(TransactionEnded::Discard)
} else {
None
}
})
})
}
/// Whether or not the response is a QUEUED response to a command within a transaction.
fn response_is_queued(frame: &Resp3Frame) -> bool {
match frame {
Resp3Frame::SimpleString { ref data, .. } => data == "QUEUED",
_ => false,
}
}
/// Read the most recent (newest) command from a centralized command response queue.
fn take_most_recent_centralized_command(commands: &Arc<Mutex<SentCommands>>) -> Option<SentCommand> {
commands.lock().pop_back()
}
/// Read the most recent (newest) command from a clustered command queue.
fn take_most_recent_cluster_command(
commands: &Arc<Mutex<BTreeMap<Arc<String>, SentCommands>>>,
server: &Arc<String>,
) -> Option<SentCommand> {
commands.lock().get_mut(server).and_then(|commands| commands.pop_back())
}
/// Send a `Canceled` error to all commands in a centralized command response queue.
async fn cancel_centralized_multi_commands(inner: &Arc<RedisClientInner>, commands: &Arc<Mutex<SentCommands>>) {
let commands: Vec<SentCommand> = { commands.lock().drain(..).collect() };
for command in commands.into_iter() {
check_command_resp_tx(inner, &command).await;
respond_to_caller_error(inner, command, RedisError::new_canceled());
}
}
/// Send a `Canceled` error to all commands in a clustered command queue.
async fn cancel_clustered_multi_commands(
inner: &Arc<RedisClientInner>,
commands: &Arc<Mutex<BTreeMap<Arc<String>, SentCommands>>>,
server: &Arc<String>,
) {
let commands = if let Some(commands) = commands.lock().get_mut(server) {
commands.drain(..).collect()
} else {
vec![]
};
for command in commands.into_iter() {
check_command_resp_tx(inner, &command).await;
respond_to_caller_error(inner, command, RedisError::new_canceled())
}
}
/// End a transaction on a centralized client instance.
async fn end_centralized_multi_block(
inner: &Arc<RedisClientInner>,
counters: &Counters,
commands: &Arc<Mutex<SentCommands>>,
frame: Resp3Frame,
ending_cmd: TransactionEnded,
) -> Result<(), RedisError> {
if !client_utils::is_locked_some(&inner.multi_block) {
return Err(RedisError::new(
RedisErrorKind::InvalidCommand,
"Expected MULTI block policy.",
));
}
counters.decr_in_flight();
let frame_is_null = protocol_utils::is_null(&frame);
if ending_cmd == TransactionEnded::Discard || (ending_cmd == TransactionEnded::Exec && frame_is_null) {
// the transaction was discarded or aborted due to a WATCH condition failing
_trace!(inner, "Ending transaction with discard or null response");
let recent_cmd = take_most_recent_centralized_command(commands);
cancel_centralized_multi_commands(inner, commands).await;
let _ = client_utils::take_locked(&inner.multi_block);
if let Some(mut recent_cmd) = recent_cmd {
sample_command_latencies(inner, &mut recent_cmd);
check_command_resp_tx(inner, &recent_cmd).await;
respond_to_caller(inner, recent_cmd, frame);
return Ok(());
} else {
return Err(RedisError::new(
RedisErrorKind::InvalidCommand,
"Missing most recent command.",
));
}
}
// return the frame to the caller directly, let them sort out the results
_trace!(inner, "Returning exec result to the caller directly");
let mut last_command = {
match commands.lock().pop_front() {
Some(cmd) => cmd,
None => {
return Err(RedisError::new(
RedisErrorKind::ProtocolError,
"Missing last command from EXEC or DISCARD.",
))
}
}
};
if !last_command.command.kind.ends_transaction() {
return Err(RedisError::new(
RedisErrorKind::InvalidCommand,
"Expected EXEC or DISCARD command.",
));
}
sample_command_latencies(inner, &mut last_command);
check_command_resp_tx(inner, &last_command).await;
respond_to_caller(inner, last_command, frame);
let _ = client_utils::take_locked(&inner.multi_block);
Ok(())
}
/// End a transaction on a clustered client instance.
async fn end_clustered_multi_block(
inner: &Arc<RedisClientInner>,
server: &Arc<String>,