LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - simulation_logs.rs (source / functions) Coverage Total Hit
Test: 53437f7e869ac68c86c7d3e4c20964c0156f158c.info Lines: 94.6 % 111 105
Test Date: 2024-09-20 16:14:12 Functions: 33.3 % 18 6

            Line data    Source code
       1              : use desim::proto::SimEvent;
       2              : use tracing::debug;
       3              : 
       4              : #[derive(Debug, Clone, PartialEq, Eq)]
       5              : enum NodeKind {
       6              :     Unknown,
       7              :     Safekeeper,
       8              :     WalProposer,
       9              : }
      10              : 
      11              : impl Default for NodeKind {
      12        42759 :     fn default() -> Self {
      13        42759 :         Self::Unknown
      14        42759 :     }
      15              : }
      16              : 
      17              : /// Simulation state of walproposer/safekeeper, derived from the simulation logs.
      18              : #[derive(Clone, Debug, Default)]
      19              : struct NodeInfo {
      20              :     kind: NodeKind,
      21              : 
      22              :     // walproposer
      23              :     is_sync: bool,
      24              :     term: u64,
      25              :     epoch_lsn: u64,
      26              : 
      27              :     // safekeeper
      28              :     commit_lsn: u64,
      29              :     flush_lsn: u64,
      30              : }
      31              : 
      32              : impl NodeInfo {
      33        75988 :     fn init_kind(&mut self, kind: NodeKind) {
      34        75988 :         if self.kind == NodeKind::Unknown {
      35        42761 :             self.kind = kind;
      36        42761 :         } else {
      37        33227 :             assert!(self.kind == kind);
      38              :         }
      39        75988 :     }
      40              : 
      41        75988 :     fn started(&mut self, data: &str) {
      42        75988 :         let mut parts = data.split(';');
      43        75988 :         assert!(parts.next().unwrap() == "started");
      44        75988 :         match parts.next().unwrap() {
      45        75988 :             "safekeeper" => {
      46        39230 :                 self.init_kind(NodeKind::Safekeeper);
      47        39230 :             }
      48        36758 :             "walproposer" => {
      49        36758 :                 self.init_kind(NodeKind::WalProposer);
      50        36758 :                 let is_sync: u8 = parts.next().unwrap().parse().unwrap();
      51        36758 :                 self.is_sync = is_sync != 0;
      52        36758 :             }
      53            0 :             _ => unreachable!(),
      54              :         }
      55        75988 :     }
      56              : }
      57              : 
      58              : /// Global state of the simulation, derived from the simulation logs.
      59              : #[derive(Debug, Default)]
      60              : struct GlobalState {
      61              :     nodes: Vec<NodeInfo>,
      62              :     commit_lsn: u64,
      63              :     write_lsn: u64,
      64              :     max_write_lsn: u64,
      65              : 
      66              :     written_wal: u64,
      67              :     written_records: u64,
      68              : }
      69              : 
      70              : impl GlobalState {
      71         2001 :     fn new() -> Self {
      72         2001 :         Default::default()
      73         2001 :     }
      74              : 
      75       147178 :     fn get(&mut self, id: u32) -> &mut NodeInfo {
      76       147178 :         let id = id as usize;
      77       147178 :         if id >= self.nodes.len() {
      78        42759 :             self.nodes.resize(id + 1, NodeInfo::default());
      79       104419 :         }
      80       147178 :         &mut self.nodes[id]
      81       147178 :     }
      82              : }
      83              : 
      84              : /// Try to find inconsistencies in the simulation log.
      85         2001 : pub fn validate_events(events: Vec<SimEvent>) {
      86              :     const INITDB_LSN: u64 = 21623024;
      87              : 
      88         2001 :     let hook = std::panic::take_hook();
      89         2001 :     scopeguard::defer_on_success! {
      90         2001 :         std::panic::set_hook(hook);
      91         2001 :     };
      92         2001 : 
      93         2001 :     let mut state = GlobalState::new();
      94         2001 :     state.max_write_lsn = INITDB_LSN;
      95              : 
      96       113584 :     for event in events {
      97       111583 :         debug!("{:?}", event);
      98              : 
      99       111583 :         let node = state.get(event.node);
     100       111583 :         if event.data.starts_with("started;") {
     101        75988 :             node.started(&event.data);
     102        75988 :             continue;
     103        35595 :         }
     104        35595 :         assert!(node.kind != NodeKind::Unknown);
     105              : 
     106              :         // drop reference to unlock state
     107        35595 :         let mut node = node.clone();
     108        35595 : 
     109        35595 :         let mut parts = event.data.split(';');
     110        35595 :         match node.kind {
     111        29597 :             NodeKind::Safekeeper => match parts.next().unwrap() {
     112        29597 :                 "tli_loaded" => {
     113        29597 :                     let flush_lsn: u64 = parts.next().unwrap().parse().unwrap();
     114        29597 :                     let commit_lsn: u64 = parts.next().unwrap().parse().unwrap();
     115        29597 :                     node.flush_lsn = flush_lsn;
     116        29597 :                     node.commit_lsn = commit_lsn;
     117        29597 :                 }
     118            0 :                 _ => unreachable!(),
     119              :             },
     120              :             NodeKind::WalProposer => {
     121         5998 :                 match parts.next().unwrap() {
     122         5998 :                     "prop_elected" => {
     123         3089 :                         let prop_lsn: u64 = parts.next().unwrap().parse().unwrap();
     124         3089 :                         let prop_term: u64 = parts.next().unwrap().parse().unwrap();
     125         3089 :                         let prev_lsn: u64 = parts.next().unwrap().parse().unwrap();
     126         3089 :                         let prev_term: u64 = parts.next().unwrap().parse().unwrap();
     127         3089 : 
     128         3089 :                         assert!(prop_lsn >= prev_lsn);
     129         3089 :                         assert!(prop_term >= prev_term);
     130              : 
     131         3089 :                         assert!(prop_lsn >= state.commit_lsn);
     132              : 
     133         3089 :                         if prop_lsn > state.write_lsn {
     134          185 :                             assert!(prop_lsn <= state.max_write_lsn);
     135          185 :                             debug!(
     136            0 :                                 "moving write_lsn up from {} to {}",
     137              :                                 state.write_lsn, prop_lsn
     138              :                             );
     139          185 :                             state.write_lsn = prop_lsn;
     140         2904 :                         }
     141         3089 :                         if prop_lsn < state.write_lsn {
     142          468 :                             debug!(
     143            0 :                                 "moving write_lsn down from {} to {}",
     144              :                                 state.write_lsn, prop_lsn
     145              :                             );
     146          468 :                             state.write_lsn = prop_lsn;
     147         2621 :                         }
     148              : 
     149         3089 :                         node.epoch_lsn = prop_lsn;
     150         3089 :                         node.term = prop_term;
     151              :                     }
     152         2909 :                     "write_wal" => {
     153         1411 :                         assert!(!node.is_sync);
     154         1411 :                         let start_lsn: u64 = parts.next().unwrap().parse().unwrap();
     155         1411 :                         let end_lsn: u64 = parts.next().unwrap().parse().unwrap();
     156         1411 :                         let cnt: u64 = parts.next().unwrap().parse().unwrap();
     157         1411 : 
     158         1411 :                         let size = end_lsn - start_lsn;
     159         1411 :                         state.written_wal += size;
     160         1411 :                         state.written_records += cnt;
     161         1411 : 
     162         1411 :                         // TODO: If we allow writing WAL before winning the election
     163         1411 : 
     164         1411 :                         assert!(start_lsn >= state.commit_lsn);
     165         1411 :                         assert!(end_lsn >= start_lsn);
     166              :                         // assert!(start_lsn == state.write_lsn);
     167         1411 :                         state.write_lsn = end_lsn;
     168         1411 : 
     169         1411 :                         if end_lsn > state.max_write_lsn {
     170         1148 :                             state.max_write_lsn = end_lsn;
     171         1148 :                         }
     172              :                     }
     173         1498 :                     "commit_lsn" => {
     174         1498 :                         let lsn: u64 = parts.next().unwrap().parse().unwrap();
     175         1498 :                         assert!(lsn >= state.commit_lsn);
     176         1498 :                         state.commit_lsn = lsn;
     177              :                     }
     178            0 :                     _ => unreachable!(),
     179              :                 }
     180              :             }
     181            0 :             _ => unreachable!(),
     182              :         }
     183              : 
     184              :         // update the node in the state struct
     185        35595 :         *state.get(event.node) = node;
     186              :     }
     187         2001 : }
        

Generated by: LCOV version 2.1-beta