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 84530 : fn default() -> Self {
13 84530 : Self::Unknown
14 84530 : }
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 149554 : fn init_kind(&mut self, kind: NodeKind) {
34 149554 : if self.kind == NodeKind::Unknown {
35 84532 : self.kind = kind;
36 84532 : } else {
37 65022 : assert!(self.kind == kind);
38 : }
39 149554 : }
40 :
41 149554 : fn started(&mut self, data: &str) {
42 149554 : let mut parts = data.split(';');
43 149554 : assert!(parts.next().unwrap() == "started");
44 149554 : match parts.next().unwrap() {
45 149554 : "safekeeper" => {
46 77028 : self.init_kind(NodeKind::Safekeeper);
47 77028 : }
48 72526 : "walproposer" => {
49 72526 : self.init_kind(NodeKind::WalProposer);
50 72526 : let is_sync: u8 = parts.next().unwrap().parse().unwrap();
51 72526 : self.is_sync = is_sync != 0;
52 72526 : }
53 0 : _ => unreachable!(),
54 : }
55 149554 : }
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 4002 : fn new() -> Self {
72 4002 : Default::default()
73 4002 : }
74 :
75 287736 : fn get(&mut self, id: u32) -> &mut NodeInfo {
76 287736 : let id = id as usize;
77 287736 : if id >= self.nodes.len() {
78 84530 : self.nodes.resize(id + 1, NodeInfo::default());
79 203206 : }
80 287736 : &mut self.nodes[id]
81 287736 : }
82 : }
83 :
84 : /// Try to find inconsistencies in the simulation log.
85 4002 : pub fn validate_events(events: Vec<SimEvent>) {
86 4002 : const INITDB_LSN: u64 = 21623024;
87 4002 :
88 4002 : let hook = std::panic::take_hook();
89 : scopeguard::defer_on_success! {
90 : std::panic::set_hook(hook);
91 : };
92 :
93 4002 : let mut state = GlobalState::new();
94 4002 : state.max_write_lsn = INITDB_LSN;
95 :
96 222647 : for event in events {
97 218645 : debug!("{:?}", event);
98 :
99 218645 : let node = state.get(event.node);
100 218645 : if event.data.starts_with("started;") {
101 149554 : node.started(&event.data);
102 149554 : continue;
103 69091 : }
104 69091 : assert!(node.kind != NodeKind::Unknown);
105 :
106 : // drop reference to unlock state
107 69091 : let mut node = node.clone();
108 69091 :
109 69091 : let mut parts = event.data.split(';');
110 69091 : match node.kind {
111 57990 : NodeKind::Safekeeper => match parts.next().unwrap() {
112 57990 : "tli_loaded" => {
113 57990 : let flush_lsn: u64 = parts.next().unwrap().parse().unwrap();
114 57990 : let commit_lsn: u64 = parts.next().unwrap().parse().unwrap();
115 57990 : node.flush_lsn = flush_lsn;
116 57990 : node.commit_lsn = commit_lsn;
117 57990 : }
118 0 : _ => unreachable!(),
119 : },
120 : NodeKind::WalProposer => {
121 11101 : match parts.next().unwrap() {
122 11101 : "prop_elected" => {
123 5961 : let prop_lsn: u64 = parts.next().unwrap().parse().unwrap();
124 5961 : let prop_term: u64 = parts.next().unwrap().parse().unwrap();
125 5961 : let prev_lsn: u64 = parts.next().unwrap().parse().unwrap();
126 5961 : let prev_term: u64 = parts.next().unwrap().parse().unwrap();
127 5961 :
128 5961 : assert!(prop_lsn >= prev_lsn);
129 5961 : assert!(prop_term >= prev_term);
130 :
131 5961 : assert!(prop_lsn >= state.commit_lsn);
132 :
133 5961 : if prop_lsn > state.write_lsn {
134 419 : assert!(prop_lsn <= state.max_write_lsn);
135 419 : debug!(
136 0 : "moving write_lsn up from {} to {}",
137 : state.write_lsn, prop_lsn
138 : );
139 419 : state.write_lsn = prop_lsn;
140 5542 : }
141 5961 : if prop_lsn < state.write_lsn {
142 982 : debug!(
143 0 : "moving write_lsn down from {} to {}",
144 : state.write_lsn, prop_lsn
145 : );
146 982 : state.write_lsn = prop_lsn;
147 4979 : }
148 :
149 5961 : node.epoch_lsn = prop_lsn;
150 5961 : node.term = prop_term;
151 : }
152 5140 : "write_wal" => {
153 2606 : assert!(!node.is_sync);
154 2606 : let start_lsn: u64 = parts.next().unwrap().parse().unwrap();
155 2606 : let end_lsn: u64 = parts.next().unwrap().parse().unwrap();
156 2606 : let cnt: u64 = parts.next().unwrap().parse().unwrap();
157 2606 :
158 2606 : let size = end_lsn - start_lsn;
159 2606 : state.written_wal += size;
160 2606 : state.written_records += cnt;
161 2606 :
162 2606 : // TODO: If we allow writing WAL before winning the election
163 2606 :
164 2606 : assert!(start_lsn >= state.commit_lsn);
165 2606 : assert!(end_lsn >= start_lsn);
166 : // assert!(start_lsn == state.write_lsn);
167 2606 : state.write_lsn = end_lsn;
168 2606 :
169 2606 : if end_lsn > state.max_write_lsn {
170 2128 : state.max_write_lsn = end_lsn;
171 2128 : }
172 : }
173 2534 : "commit_lsn" => {
174 2534 : let lsn: u64 = parts.next().unwrap().parse().unwrap();
175 2534 : assert!(lsn >= state.commit_lsn);
176 2534 : state.commit_lsn = lsn;
177 : }
178 0 : _ => unreachable!(),
179 : }
180 : }
181 0 : _ => unreachable!(),
182 : }
183 :
184 : // update the node in the state struct
185 69091 : *state.get(event.node) = node;
186 : }
187 4002 : }
|