scylla 1.6.0

Async CQL driver for Rust, optimized for ScyllaDB, fully compatible with Apache Cassandraâ„¢
Documentation
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
//! Collecting history of request executions - retries, speculative, etc.
use std::{
    collections::BTreeMap,
    fmt::{Debug, Display},
    net::SocketAddr,
    sync::Mutex,
    time::SystemTime,
};

use crate::errors::{RequestAttemptError, RequestError};
use crate::policies::retry::RetryDecision;
use chrono::{DateTime, Utc};

use tracing::warn;

/// Id of a single request, i.e. a single call to Session::{query,execute}_{unpaged,single_page}/etc.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct RequestId(pub usize);

/// Id of a single attempt within a request run - a single request sent on some connection.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct AttemptId(pub usize);

/// Id of a speculative execution fiber.
/// When speculative execution is enabled the driver will start multiple
/// speculative threads, each of them performing sequential attempts.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct SpeculativeId(pub usize);

/// Any type implementing this trait can be passed to Session
/// to collect execution history of specific requests.\
/// In order to use it call `set_history_listener` on
/// `Statement`, `PreparedStatement`, etc...\
/// The listener has to generate unique IDs for new requests, attempts and speculative fibers.
/// These ids are then used by the caller to identify them.\
/// It's important to note that even after a request is finished there still might come events related to it.
/// These events come from speculative futures that didn't notice the request is done already.
pub trait HistoryListener: Debug + Send + Sync {
    /// Log that a request has started on request start - right after the call to Session::{query,execute}_*/batch.
    fn log_request_start(&self) -> RequestId;

    /// Log that request was successful - called right before returning the result from Session::query_*, execute_*, etc.
    fn log_request_success(&self, request_id: RequestId);

    /// Log that request ended with an error - called right before returning the error from Session::query_*, execute_*, etc.
    fn log_request_error(&self, request_id: RequestId, error: &RequestError);

    /// Log that a new speculative fiber has started.
    fn log_new_speculative_fiber(&self, request_id: RequestId) -> SpeculativeId;

    /// Log that an attempt has started - request has been sent on some Connection, now awaiting for an answer.
    fn log_attempt_start(
        &self,
        request_id: RequestId,
        speculative_id: Option<SpeculativeId>,
        node_addr: SocketAddr,
    ) -> AttemptId;

    /// Log that an attempt succeeded.
    fn log_attempt_success(&self, attempt_id: AttemptId);

    /// Log that an attempt ended with an error. The error and decision whether to retry the attempt are also included in the log.
    fn log_attempt_error(
        &self,
        attempt_id: AttemptId,
        error: &RequestAttemptError,
        retry_decision: &RetryDecision,
    );
}

/// A point in time when an event happened.
pub type TimePoint = DateTime<Utc>;

/// HistoryCollector can be used as [HistoryListener] to collect all the request history events.
/// Each event is marked with a UTC timestamp.
#[derive(Debug, Default)]
pub struct HistoryCollector {
    data: Mutex<HistoryCollectorData>,
}

/// Data collected by [HistoryCollector].
#[derive(Debug, Clone)]
pub struct HistoryCollectorData {
    events: Vec<(HistoryEvent, TimePoint)>,
    next_request_id: RequestId,
    next_speculative_fiber_id: SpeculativeId,
    next_attempt_id: AttemptId,
}

/// Events that can happen during request execution,
/// which are collected by [HistoryCollector].
#[derive(Debug, Clone)]
pub enum HistoryEvent {
    /// A new request has started, with a unique [RequestId].
    NewRequest(RequestId),
    /// Request with [RequestId] has finished successfully.
    RequestSuccess(RequestId),
    /// Request with [RequestId] has finished with a given [RequestError].
    RequestError(RequestId, RequestError),
    /// A new speculative fiber with a unique [SpeculativeId] has started
    /// for a request with [RequestId].
    NewSpeculativeFiber(SpeculativeId, RequestId),
    /// A new attempt with a unique [AttemptId] has started for a request with [RequestId].
    NewAttempt(AttemptId, RequestId, Option<SpeculativeId>, SocketAddr),
    /// Attempt with [AttemptId] has finished successfully.
    AttemptSuccess(AttemptId),
    /// Attempt with [AttemptId] has finished with an error,
    /// including the error and retry decision.
    AttemptError(AttemptId, RequestAttemptError, RetryDecision),
}

impl HistoryCollectorData {
    fn new() -> HistoryCollectorData {
        HistoryCollectorData {
            events: Vec::new(),
            next_request_id: RequestId(0),
            next_speculative_fiber_id: SpeculativeId(0),
            next_attempt_id: AttemptId(0),
        }
    }

    fn add_event(&mut self, event: HistoryEvent) {
        let event_time: TimePoint = SystemTime::now().into();
        self.events.push((event, event_time));
    }
}

impl Default for HistoryCollectorData {
    fn default() -> HistoryCollectorData {
        HistoryCollectorData::new()
    }
}

impl HistoryCollector {
    /// Creates a new HistoryCollector with empty data.
    pub fn new() -> HistoryCollector {
        HistoryCollector::default()
    }

    /// Clones the data collected by the collector.
    pub fn clone_collected(&self) -> HistoryCollectorData {
        self.do_with_data(|data| data.clone())
    }

    /// Takes the data out of the collector. The collected events are cleared.\
    /// It's possible that after finishing a request and taking out the events
    /// new ones will still come - from requests that haven't been cancelled yet.
    pub fn take_collected(&self) -> HistoryCollectorData {
        self.do_with_data(|data| {
            let mut data_to_swap = HistoryCollectorData {
                events: Vec::new(),
                next_request_id: data.next_request_id,
                next_speculative_fiber_id: data.next_speculative_fiber_id,
                next_attempt_id: data.next_attempt_id,
            };
            std::mem::swap(&mut data_to_swap, data);
            data_to_swap
        })
    }

    /// Clone the collected events and convert them to StructuredHistory.
    pub fn clone_structured_history(&self) -> StructuredHistory {
        StructuredHistory::from(&self.clone_collected())
    }

    /// Take the collected events out, just like in `take_collected` and convert them to StructuredHistory.
    pub fn take_structured_history(&self) -> StructuredHistory {
        StructuredHistory::from(&self.take_collected())
    }

    /// Lock the data mutex and perform an operation on it.
    fn do_with_data<OpRetType>(
        &self,
        do_fn: impl Fn(&mut HistoryCollectorData) -> OpRetType,
    ) -> OpRetType {
        match self.data.lock() {
            Ok(mut data) => do_fn(&mut data),
            Err(poison_error) => {
                // Avoid panicking on poisoned mutex - HistoryCollector isn't that important.
                // Print a warning and do the operation on dummy data so that the code compiles.
                warn!("HistoryCollector - mutex poisoned! Error: {}", poison_error);
                let mut dummy_data: HistoryCollectorData = HistoryCollectorData::default();
                do_fn(&mut dummy_data)
            }
        }
    }
}

impl HistoryListener for HistoryCollector {
    fn log_request_start(&self) -> RequestId {
        self.do_with_data(|data| {
            let new_request_id: RequestId = data.next_request_id;
            data.next_request_id.0 += 1;
            data.add_event(HistoryEvent::NewRequest(new_request_id));
            new_request_id
        })
    }

    fn log_request_success(&self, request_id: RequestId) {
        self.do_with_data(|data| {
            data.add_event(HistoryEvent::RequestSuccess(request_id));
        })
    }

    fn log_request_error(&self, request_id: RequestId, error: &RequestError) {
        self.do_with_data(|data| {
            data.add_event(HistoryEvent::RequestError(request_id, error.clone()))
        })
    }

    fn log_new_speculative_fiber(&self, request_id: RequestId) -> SpeculativeId {
        self.do_with_data(|data| {
            let new_speculative_id: SpeculativeId = data.next_speculative_fiber_id;
            data.next_speculative_fiber_id.0 += 1;
            data.add_event(HistoryEvent::NewSpeculativeFiber(
                new_speculative_id,
                request_id,
            ));
            new_speculative_id
        })
    }

    fn log_attempt_start(
        &self,
        request_id: RequestId,
        speculative_id: Option<SpeculativeId>,
        node_addr: SocketAddr,
    ) -> AttemptId {
        self.do_with_data(|data| {
            let new_attempt_id: AttemptId = data.next_attempt_id;
            data.next_attempt_id.0 += 1;
            data.add_event(HistoryEvent::NewAttempt(
                new_attempt_id,
                request_id,
                speculative_id,
                node_addr,
            ));
            new_attempt_id
        })
    }

    fn log_attempt_success(&self, attempt_id: AttemptId) {
        self.do_with_data(|data| data.add_event(HistoryEvent::AttemptSuccess(attempt_id)))
    }

    fn log_attempt_error(
        &self,
        attempt_id: AttemptId,
        error: &RequestAttemptError,
        retry_decision: &RetryDecision,
    ) {
        self.do_with_data(|data| {
            data.add_event(HistoryEvent::AttemptError(
                attempt_id,
                error.clone(),
                retry_decision.clone(),
            ))
        })
    }
}

/// Structured representation of requests history.\
/// [HistoryCollector] collects raw events which later can be converted
/// to this pretty representation.\
/// It has a `Display` impl which can be used for printing pretty request history.
#[derive(Debug, Clone)]
pub struct StructuredHistory {
    /// List of requests with their history.
    pub requests: Vec<RequestHistory>,
}

/// History of a single request, including its speculative fibers and attempts.
#[derive(Debug, Clone)]
pub struct RequestHistory {
    /// Time when the request started.
    pub start_time: TimePoint,
    /// History of the primary (non-speculative) fiber, which includes its start time
    /// and attempts made within it.
    pub non_speculative_fiber: FiberHistory,
    /// List of speculative fibers for this request, each with its independent history.
    pub speculative_fibers: Vec<FiberHistory>,
    /// Result of the request execution, if it has finished.
    pub result: Option<RequestHistoryResult>,
}

/// Result of a request execution, either successful or with an error,
/// including the time when it finished execution.
#[derive(Debug, Clone)]
pub enum RequestHistoryResult {
    /// Request was successful, with the time it finished.
    Success(TimePoint),
    /// Request ended with an error, with the time it finished and the error itself.
    Error(TimePoint, RequestError),
}

/// History of a speculative fiber, which includes its start time and attempts made within it.
#[derive(Debug, Clone)]
pub struct FiberHistory {
    /// Time when the speculative fiber started.
    pub start_time: TimePoint,
    /// List of attempts made within this speculative fiber.
    pub attempts: Vec<AttemptHistory>,
}

/// History of a single attempt, including the time it was sent, the node it was sent to,
/// and the result of the attempt (success or error).
#[derive(Debug, Clone)]
pub struct AttemptHistory {
    /// Time when the attempt was sent.
    pub send_time: TimePoint,
    /// Address of the node to which the attempt was sent.
    pub node_addr: SocketAddr,
    /// Result of the attempt, if it has finished.
    /// If the attempt has been sent but another speculative fiber completed,
    /// this will be `None`, because the driver no longer tracks it.
    pub result: Option<AttemptResult>,
}

/// Result of an attempt execution, either successful or with an error,
/// including the time when it finished execution and the retry decision made.
#[derive(Debug, Clone)]
pub enum AttemptResult {
    /// Attempt was successful, with the time it finished.
    Success(TimePoint),
    /// Attempt ended with an error, with the time it finished,
    /// the error itself, and the retry decision made in response to the error.
    Error(TimePoint, RequestAttemptError, RetryDecision),
}

impl From<&HistoryCollectorData> for StructuredHistory {
    fn from(data: &HistoryCollectorData) -> StructuredHistory {
        let mut attempts: BTreeMap<AttemptId, AttemptHistory> = BTreeMap::new();
        let mut requests: BTreeMap<RequestId, RequestHistory> = BTreeMap::new();
        let mut fibers: BTreeMap<SpeculativeId, FiberHistory> = BTreeMap::new();

        // Collect basic data about requests, attempts and speculative fibers
        for (event, event_time) in &data.events {
            match event {
                HistoryEvent::NewAttempt(attempt_id, _, _, node_addr) => {
                    attempts.insert(
                        *attempt_id,
                        AttemptHistory {
                            send_time: *event_time,
                            node_addr: *node_addr,
                            result: None,
                        },
                    );
                }
                HistoryEvent::AttemptSuccess(attempt_id) => {
                    if let Some(attempt) = attempts.get_mut(attempt_id) {
                        attempt.result = Some(AttemptResult::Success(*event_time));
                    }
                }
                HistoryEvent::AttemptError(attempt_id, error, retry_decision) => {
                    match attempts.get_mut(attempt_id) {
                        Some(attempt) => {
                            if attempt.result.is_some() {
                                warn!(
                                    "StructuredHistory - attempt with id {:?} has multiple results",
                                    attempt_id
                                );
                            }
                            attempt.result = Some(AttemptResult::Error(
                                *event_time,
                                error.clone(),
                                retry_decision.clone(),
                            ));
                        }
                        None => warn!(
                            "StructuredHistory - attempt with id {:?} finished with an error but not created",
                            attempt_id
                        ),
                    }
                }
                HistoryEvent::NewRequest(request_id) => {
                    requests.insert(
                        *request_id,
                        RequestHistory {
                            start_time: *event_time,
                            non_speculative_fiber: FiberHistory {
                                start_time: *event_time,
                                attempts: Vec::new(),
                            },
                            speculative_fibers: Vec::new(),
                            result: None,
                        },
                    );
                }
                HistoryEvent::RequestSuccess(request_id) => {
                    if let Some(request) = requests.get_mut(request_id) {
                        request.result = Some(RequestHistoryResult::Success(*event_time));
                    }
                }
                HistoryEvent::RequestError(request_id, error) => {
                    if let Some(request) = requests.get_mut(request_id) {
                        request.result =
                            Some(RequestHistoryResult::Error(*event_time, error.clone()));
                    }
                }
                HistoryEvent::NewSpeculativeFiber(speculative_id, _) => {
                    fibers.insert(
                        *speculative_id,
                        FiberHistory {
                            start_time: *event_time,
                            attempts: Vec::new(),
                        },
                    );
                }
            }
        }

        // Move attempts to their speculative fibers
        for (event, _) in &data.events {
            if let HistoryEvent::NewAttempt(attempt_id, request_id, speculative_id, _) = event
                && let Some(attempt) = attempts.remove(attempt_id)
            {
                match speculative_id {
                    Some(spec_id) => {
                        if let Some(spec_fiber) = fibers.get_mut(spec_id) {
                            spec_fiber.attempts.push(attempt);
                        }
                    }
                    None => {
                        if let Some(request) = requests.get_mut(request_id) {
                            request.non_speculative_fiber.attempts.push(attempt);
                        }
                    }
                }
            }
        }

        // Move speculative fibers to their requests
        for (event, _) in &data.events {
            if let HistoryEvent::NewSpeculativeFiber(speculative_id, request_id) = event
                && let Some(fiber) = fibers.remove(speculative_id)
                && let Some(request) = requests.get_mut(request_id)
            {
                request.speculative_fibers.push(fiber);
            }
        }

        StructuredHistory {
            requests: requests.into_values().collect(),
        }
    }
}

/// StructuredHistory should be used for printing request history.
impl Display for StructuredHistory {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "Requests History:")?;
        for (i, request) in self.requests.iter().enumerate() {
            writeln!(f, "=== Request #{i} ===")?;
            writeln!(f, "| start_time: {}", request.start_time)?;
            writeln!(f, "| Non-speculative attempts:")?;
            write_fiber_attempts(&request.non_speculative_fiber, f)?;
            for (spec_i, speculative_fiber) in request.speculative_fibers.iter().enumerate() {
                writeln!(f, "|")?;
                writeln!(f, "|")?;
                writeln!(f, "| > Speculative fiber #{spec_i}")?;
                writeln!(f, "| fiber start time: {}", speculative_fiber.start_time)?;
                write_fiber_attempts(speculative_fiber, f)?;
            }
            writeln!(f, "|")?;
            match &request.result {
                Some(RequestHistoryResult::Success(succ_time)) => {
                    writeln!(f, "| Request successful at {succ_time}")?;
                }
                Some(RequestHistoryResult::Error(err_time, error)) => {
                    writeln!(f, "| Request failed at {err_time}")?;
                    writeln!(f, "| Error: {error}")?;
                }
                None => writeln!(f, "| Request still running - no final result yet")?,
            };
            writeln!(f, "=================")?;
        }
        Ok(())
    }
}

fn write_fiber_attempts(fiber: &FiberHistory, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    for (i, attempt) in fiber.attempts.iter().enumerate() {
        if i != 0 {
            writeln!(f, "|")?;
        }
        writeln!(f, "| - Attempt #{} sent to {}", i, attempt.node_addr)?;
        writeln!(f, "|   request send time: {}", attempt.send_time)?;
        match &attempt.result {
            Some(AttemptResult::Success(time)) => writeln!(f, "|   Success at {time}")?,
            Some(AttemptResult::Error(time, err, retry_decision)) => {
                writeln!(f, "|   Error at {time}")?;
                writeln!(f, "|   Error: {err}")?;
                writeln!(f, "|   Retry decision: {retry_decision:?}")?;
            }
            None => writeln!(f, "|   No result yet")?,
        };
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::net::{IpAddr, Ipv4Addr, SocketAddr};

    use crate::{
        errors::{DbError, RequestAttemptError, RequestError},
        policies::retry::RetryDecision,
        test_utils::setup_tracing,
    };

    use super::{
        AttemptId, AttemptResult, HistoryCollector, HistoryListener, RequestHistoryResult,
        RequestId, SpeculativeId, StructuredHistory, TimePoint,
    };
    use assert_matches::assert_matches;
    use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc};
    use scylla_cql::{Consistency, frame::response::CqlResponseKind};

    // Set a single time for all timestamps within StructuredHistory.
    // HistoryCollector sets the timestamp to current time which changes with each test.
    // Setting it to one makes it possible to test displaying consistently.
    fn set_one_time(mut history: StructuredHistory) -> StructuredHistory {
        let the_time: TimePoint = DateTime::<Utc>::from_naive_utc_and_offset(
            NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2022, 2, 22).unwrap(),
                NaiveTime::from_hms_opt(20, 22, 22).unwrap(),
            ),
            Utc,
        );

        for request in &mut history.requests {
            request.start_time = the_time;
            match &mut request.result {
                Some(RequestHistoryResult::Success(succ_time)) => *succ_time = the_time,
                Some(RequestHistoryResult::Error(err_time, _)) => *err_time = the_time,
                None => {}
            };

            for fiber in std::iter::once(&mut request.non_speculative_fiber)
                .chain(request.speculative_fibers.iter_mut())
            {
                fiber.start_time = the_time;
                for attempt in &mut fiber.attempts {
                    attempt.send_time = the_time;
                    match &mut attempt.result {
                        Some(AttemptResult::Success(succ_time)) => *succ_time = the_time,
                        Some(AttemptResult::Error(err_time, _, _)) => *err_time = the_time,
                        None => {}
                    }
                }
            }
        }

        history
    }

    fn node1_addr() -> SocketAddr {
        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 19042)
    }

    fn node2_addr() -> SocketAddr {
        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 2)), 19042)
    }

    fn node3_addr() -> SocketAddr {
        SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 3)), 19042)
    }

    fn unexpected_response(kind: CqlResponseKind) -> RequestAttemptError {
        RequestAttemptError::UnexpectedResponse(kind)
    }

    fn unavailable_error() -> RequestAttemptError {
        RequestAttemptError::DbError(
            DbError::Unavailable {
                consistency: Consistency::Quorum,
                required: 2,
                alive: 1,
            },
            "Not enough nodes to satisfy consistency".to_string(),
        )
    }

    fn no_stream_id_error() -> RequestAttemptError {
        RequestAttemptError::UnableToAllocStreamId
    }

    #[test]
    fn empty_history() {
        setup_tracing();
        let history_collector = HistoryCollector::new();
        let history: StructuredHistory = history_collector.clone_structured_history();

        assert!(history.requests.is_empty());

        let displayed = "Requests History:
";
        assert_eq!(displayed, format!("{history}"));
    }

    #[test]
    fn empty_request() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let _request_id: RequestId = history_collector.log_request_start();

        let history: StructuredHistory = history_collector.clone_structured_history();

        assert_eq!(history.requests.len(), 1);
        assert!(
            history.requests[0]
                .non_speculative_fiber
                .attempts
                .is_empty()
        );
        assert!(history.requests[0].speculative_fibers.is_empty());

        let displayed = "Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
|
| Request still running - no final result yet
=================
";

        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }

    #[test]
    fn one_attempt() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let request_id: RequestId = history_collector.log_request_start();
        let attempt_id: AttemptId =
            history_collector.log_attempt_start(request_id, None, node1_addr());
        history_collector.log_attempt_success(attempt_id);
        history_collector.log_request_success(request_id);

        let history: StructuredHistory = history_collector.clone_structured_history();

        assert_eq!(history.requests.len(), 1);
        assert_eq!(history.requests[0].non_speculative_fiber.attempts.len(), 1);
        assert!(history.requests[0].speculative_fibers.is_empty());
        assert_matches!(
            history.requests[0].non_speculative_fiber.attempts[0].result,
            Some(AttemptResult::Success(_))
        );

        let displayed = "Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Success at 2022-02-22 20:22:22 UTC
|
| Request successful at 2022-02-22 20:22:22 UTC
=================
";
        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }

    #[test]
    fn two_error_atempts() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let request_id: RequestId = history_collector.log_request_start();

        let attempt_id: AttemptId =
            history_collector.log_attempt_start(request_id, None, node1_addr());
        history_collector.log_attempt_error(
            attempt_id,
            &unexpected_response(CqlResponseKind::Ready),
            &RetryDecision::RetrySameTarget(Some(Consistency::Quorum)),
        );

        let second_attempt_id: AttemptId =
            history_collector.log_attempt_start(request_id, None, node1_addr());
        history_collector.log_attempt_error(
            second_attempt_id,
            &unavailable_error(),
            &RetryDecision::DontRetry,
        );

        history_collector.log_request_error(
            request_id,
            &RequestError::LastAttemptError(unavailable_error()),
        );

        let history: StructuredHistory = history_collector.clone_structured_history();

        let displayed =
"Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Received unexpected response from the server: READY. Expected RESULT or ERROR response.
|   Retry decision: RetrySameTarget(Some(Quorum))
|
| - Attempt #1 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Database returned an error: Not enough nodes are alive to satisfy required consistency level (consistency: Quorum, required: 2, alive: 1), Error message: Not enough nodes to satisfy consistency
|   Retry decision: DontRetry
|
| Request failed at 2022-02-22 20:22:22 UTC
| Error: Database returned an error: Not enough nodes are alive to satisfy required consistency level (consistency: Quorum, required: 2, alive: 1), Error message: Not enough nodes to satisfy consistency
=================
";
        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }

    #[test]
    fn empty_fibers() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let request_id: RequestId = history_collector.log_request_start();
        history_collector.log_new_speculative_fiber(request_id);
        history_collector.log_new_speculative_fiber(request_id);
        history_collector.log_new_speculative_fiber(request_id);

        let history: StructuredHistory = history_collector.clone_structured_history();

        assert_eq!(history.requests.len(), 1);
        assert!(
            history.requests[0]
                .non_speculative_fiber
                .attempts
                .is_empty()
        );
        assert_eq!(history.requests[0].speculative_fibers.len(), 3);
        assert!(
            history.requests[0].speculative_fibers[0]
                .attempts
                .is_empty()
        );
        assert!(
            history.requests[0].speculative_fibers[1]
                .attempts
                .is_empty()
        );
        assert!(
            history.requests[0].speculative_fibers[2]
                .attempts
                .is_empty()
        );

        let displayed = "Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
|
|
| > Speculative fiber #0
| fiber start time: 2022-02-22 20:22:22 UTC
|
|
| > Speculative fiber #1
| fiber start time: 2022-02-22 20:22:22 UTC
|
|
| > Speculative fiber #2
| fiber start time: 2022-02-22 20:22:22 UTC
|
| Request still running - no final result yet
=================
";
        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }

    #[test]
    fn complex() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let request_id: RequestId = history_collector.log_request_start();

        let attempt1: AttemptId =
            history_collector.log_attempt_start(request_id, None, node1_addr());

        let speculative1: SpeculativeId = history_collector.log_new_speculative_fiber(request_id);

        let spec1_attempt1: AttemptId =
            history_collector.log_attempt_start(request_id, Some(speculative1), node2_addr());

        history_collector.log_attempt_error(
            attempt1,
            &unexpected_response(CqlResponseKind::Event),
            &RetryDecision::RetryNextTarget(Some(Consistency::Quorum)),
        );
        let _attempt2: AttemptId =
            history_collector.log_attempt_start(request_id, None, node3_addr());

        let speculative2: SpeculativeId = history_collector.log_new_speculative_fiber(request_id);

        let spec2_attempt1: AttemptId =
            history_collector.log_attempt_start(request_id, Some(speculative2), node1_addr());
        history_collector.log_attempt_error(
            spec2_attempt1,
            &no_stream_id_error(),
            &RetryDecision::RetrySameTarget(Some(Consistency::Quorum)),
        );

        let spec2_attempt2: AttemptId =
            history_collector.log_attempt_start(request_id, Some(speculative2), node1_addr());

        let _speculative3: SpeculativeId = history_collector.log_new_speculative_fiber(request_id);
        let speculative4: SpeculativeId = history_collector.log_new_speculative_fiber(request_id);

        history_collector.log_attempt_error(
            spec1_attempt1,
            &unavailable_error(),
            &RetryDecision::RetryNextTarget(Some(Consistency::Quorum)),
        );

        let _spec4_attempt1: AttemptId =
            history_collector.log_attempt_start(request_id, Some(speculative4), node2_addr());

        history_collector.log_attempt_success(spec2_attempt2);
        history_collector.log_request_success(request_id);

        let history: StructuredHistory = history_collector.clone_structured_history();

        let displayed = "Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Received unexpected response from the server: EVENT. Expected RESULT or ERROR response.
|   Retry decision: RetryNextTarget(Some(Quorum))
|
| - Attempt #1 sent to 127.0.0.3:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   No result yet
|
|
| > Speculative fiber #0
| fiber start time: 2022-02-22 20:22:22 UTC
| - Attempt #0 sent to 127.0.0.2:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Database returned an error: Not enough nodes are alive to satisfy required consistency level (consistency: Quorum, required: 2, alive: 1), Error message: Not enough nodes to satisfy consistency
|   Retry decision: RetryNextTarget(Some(Quorum))
|
|
| > Speculative fiber #1
| fiber start time: 2022-02-22 20:22:22 UTC
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Unable to allocate stream id
|   Retry decision: RetrySameTarget(Some(Quorum))
|
| - Attempt #1 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Success at 2022-02-22 20:22:22 UTC
|
|
| > Speculative fiber #2
| fiber start time: 2022-02-22 20:22:22 UTC
|
|
| > Speculative fiber #3
| fiber start time: 2022-02-22 20:22:22 UTC
| - Attempt #0 sent to 127.0.0.2:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   No result yet
|
| Request successful at 2022-02-22 20:22:22 UTC
=================
";
        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }

    #[test]
    fn multiple_requests() {
        setup_tracing();
        let history_collector = HistoryCollector::new();

        let request1_id: RequestId = history_collector.log_request_start();
        let request1_attempt1: AttemptId =
            history_collector.log_attempt_start(request1_id, None, node1_addr());
        history_collector.log_attempt_error(
            request1_attempt1,
            &unexpected_response(CqlResponseKind::Supported),
            &RetryDecision::RetryNextTarget(Some(Consistency::Quorum)),
        );
        let request1_attempt2: AttemptId =
            history_collector.log_attempt_start(request1_id, None, node2_addr());
        history_collector.log_attempt_success(request1_attempt2);
        history_collector.log_request_success(request1_id);

        let request2_id: RequestId = history_collector.log_request_start();
        let request2_attempt1: AttemptId =
            history_collector.log_attempt_start(request2_id, None, node1_addr());
        history_collector.log_attempt_success(request2_attempt1);
        history_collector.log_request_success(request2_id);

        let history: StructuredHistory = history_collector.clone_structured_history();

        let displayed = "Requests History:
=== Request #0 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Error at 2022-02-22 20:22:22 UTC
|   Error: Received unexpected response from the server: SUPPORTED. Expected RESULT or ERROR response.
|   Retry decision: RetryNextTarget(Some(Quorum))
|
| - Attempt #1 sent to 127.0.0.2:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Success at 2022-02-22 20:22:22 UTC
|
| Request successful at 2022-02-22 20:22:22 UTC
=================
=== Request #1 ===
| start_time: 2022-02-22 20:22:22 UTC
| Non-speculative attempts:
| - Attempt #0 sent to 127.0.0.1:19042
|   request send time: 2022-02-22 20:22:22 UTC
|   Success at 2022-02-22 20:22:22 UTC
|
| Request successful at 2022-02-22 20:22:22 UTC
=================
";
        assert_eq!(displayed, format!("{}", set_one_time(history)));
    }
}