LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - simulation_logs.rs (source / functions) Coverage Total Hit
Test: b4ae4c4857f9ef3e144e982a35ee23bc84c71983.info Lines: 94.6 % 111 105
Test Date: 2024-10-22 22:13:45 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        10626 :     fn default() -> Self {
      13        10626 :         Self::Unknown
      14        10626 :     }
      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        18795 :     fn init_kind(&mut self, kind: NodeKind) {
      34        18795 :         if self.kind == NodeKind::Unknown {
      35        10629 :             self.kind = kind;
      36        10629 :         } else {
      37         8166 :             assert!(self.kind == kind);
      38              :         }
      39        18795 :     }
      40              : 
      41        18795 :     fn started(&mut self, data: &str) {
      42        18795 :         let mut parts = data.split(';');
      43        18795 :         assert!(parts.next().unwrap() == "started");
      44        18795 :         match parts.next().unwrap() {
      45        18795 :             "safekeeper" => {
      46         9669 :                 self.init_kind(NodeKind::Safekeeper);
      47         9669 :             }
      48         9126 :             "walproposer" => {
      49         9126 :                 self.init_kind(NodeKind::WalProposer);
      50         9126 :                 let is_sync: u8 = parts.next().unwrap().parse().unwrap();
      51         9126 :                 self.is_sync = is_sync != 0;
      52         9126 :             }
      53            0 :             _ => unreachable!(),
      54              :         }
      55        18795 :     }
      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          501 :     fn new() -> Self {
      72          501 :         Default::default()
      73          501 :     }
      74              : 
      75        36315 :     fn get(&mut self, id: u32) -> &mut NodeInfo {
      76        36315 :         let id = id as usize;
      77        36315 :         if id >= self.nodes.len() {
      78        10626 :             self.nodes.resize(id + 1, NodeInfo::default());
      79        25689 :         }
      80        36315 :         &mut self.nodes[id]
      81        36315 :     }
      82              : }
      83              : 
      84              : /// Try to find inconsistencies in the simulation log.
      85          501 : pub fn validate_events(events: Vec<SimEvent>) {
      86              :     const INITDB_LSN: u64 = 21623024;
      87              : 
      88          501 :     let hook = std::panic::take_hook();
      89          501 :     scopeguard::defer_on_success! {
      90          501 :         std::panic::set_hook(hook);
      91          501 :     };
      92          501 : 
      93          501 :     let mut state = GlobalState::new();
      94          501 :     state.max_write_lsn = INITDB_LSN;
      95              : 
      96        28056 :     for event in events {
      97        27555 :         debug!("{:?}", event);
      98              : 
      99        27555 :         let node = state.get(event.node);
     100        27555 :         if event.data.starts_with("started;") {
     101        18795 :             node.started(&event.data);
     102        18795 :             continue;
     103         8760 :         }
     104         8760 :         assert!(node.kind != NodeKind::Unknown);
     105              : 
     106              :         // drop reference to unlock state
     107         8760 :         let mut node = node.clone();
     108         8760 : 
     109         8760 :         let mut parts = event.data.split(';');
     110         8760 :         match node.kind {
     111         7316 :             NodeKind::Safekeeper => match parts.next().unwrap() {
     112         7316 :                 "tli_loaded" => {
     113         7316 :                     let flush_lsn: u64 = parts.next().unwrap().parse().unwrap();
     114         7316 :                     let commit_lsn: u64 = parts.next().unwrap().parse().unwrap();
     115         7316 :                     node.flush_lsn = flush_lsn;
     116         7316 :                     node.commit_lsn = commit_lsn;
     117         7316 :                 }
     118            0 :                 _ => unreachable!(),
     119              :             },
     120              :             NodeKind::WalProposer => {
     121         1444 :                 match parts.next().unwrap() {
     122         1444 :                     "prop_elected" => {
     123          750 :                         let prop_lsn: u64 = parts.next().unwrap().parse().unwrap();
     124          750 :                         let prop_term: u64 = parts.next().unwrap().parse().unwrap();
     125          750 :                         let prev_lsn: u64 = parts.next().unwrap().parse().unwrap();
     126          750 :                         let prev_term: u64 = parts.next().unwrap().parse().unwrap();
     127          750 : 
     128          750 :                         assert!(prop_lsn >= prev_lsn);
     129          750 :                         assert!(prop_term >= prev_term);
     130              : 
     131          750 :                         assert!(prop_lsn >= state.commit_lsn);
     132              : 
     133          750 :                         if prop_lsn > state.write_lsn {
     134           49 :                             assert!(prop_lsn <= state.max_write_lsn);
     135           49 :                             debug!(
     136            0 :                                 "moving write_lsn up from {} to {}",
     137              :                                 state.write_lsn, prop_lsn
     138              :                             );
     139           49 :                             state.write_lsn = prop_lsn;
     140          701 :                         }
     141          750 :                         if prop_lsn < state.write_lsn {
     142          108 :                             debug!(
     143            0 :                                 "moving write_lsn down from {} to {}",
     144              :                                 state.write_lsn, prop_lsn
     145              :                             );
     146          108 :                             state.write_lsn = prop_lsn;
     147          642 :                         }
     148              : 
     149          750 :                         node.epoch_lsn = prop_lsn;
     150          750 :                         node.term = prop_term;
     151              :                     }
     152          694 :                     "write_wal" => {
     153          339 :                         assert!(!node.is_sync);
     154          339 :                         let start_lsn: u64 = parts.next().unwrap().parse().unwrap();
     155          339 :                         let end_lsn: u64 = parts.next().unwrap().parse().unwrap();
     156          339 :                         let cnt: u64 = parts.next().unwrap().parse().unwrap();
     157          339 : 
     158          339 :                         let size = end_lsn - start_lsn;
     159          339 :                         state.written_wal += size;
     160          339 :                         state.written_records += cnt;
     161          339 : 
     162          339 :                         // TODO: If we allow writing WAL before winning the election
     163          339 : 
     164          339 :                         assert!(start_lsn >= state.commit_lsn);
     165          339 :                         assert!(end_lsn >= start_lsn);
     166              :                         // assert!(start_lsn == state.write_lsn);
     167          339 :                         state.write_lsn = end_lsn;
     168          339 : 
     169          339 :                         if end_lsn > state.max_write_lsn {
     170          275 :                             state.max_write_lsn = end_lsn;
     171          275 :                         }
     172              :                     }
     173          355 :                     "commit_lsn" => {
     174          355 :                         let lsn: u64 = parts.next().unwrap().parse().unwrap();
     175          355 :                         assert!(lsn >= state.commit_lsn);
     176          355 :                         state.commit_lsn = lsn;
     177              :                     }
     178            0 :                     _ => unreachable!(),
     179              :                 }
     180              :             }
     181            0 :             _ => unreachable!(),
     182              :         }
     183              : 
     184              :         // update the node in the state struct
     185         8760 :         *state.get(event.node) = node;
     186              :     }
     187          501 : }
        

Generated by: LCOV version 2.1-beta