|
| 1 | +//! The replica's B+tree (`KvMap` in `client/replica_store.rs`) once lost |
| 2 | +//! keys. When a delete emptied the first child of an internal node, the child |
| 3 | +//! was removed and the next one became the first, keeping its own lower |
| 4 | +//! bound, above the bound the parent had for the node. Inside the node the |
| 5 | +//! first bound is never read, so a key between the two went to that child. |
| 6 | +//! Once the node merged into its left neighbor, that bound was read: lookups |
| 7 | +//! of such a key went to the neighbor's last child, so `get` and `has` missed |
| 8 | +//! a row the map still held. A mutator or a poke then couldn't delete the |
| 9 | +//! row, and putting it again added a second entry and an add of a row the |
| 10 | +//! IVM source had, which panicked ("Row already exists"). |
| 11 | +//! |
| 12 | +//! The steps are the shortest construction found: ascending inserts make |
| 13 | +//! leaves of 32 and a root over two internal nodes, P0 and P1. JS runs the |
| 14 | +//! same mutations (through the conformance harness's `harden.ops`) and |
| 15 | +//! deletes the row, then shows it again after the insert. |
| 16 | +
|
| 17 | +use std::cell::Cell; |
| 18 | +use std::rc::Rc; |
| 19 | +use std::sync::Arc; |
| 20 | +use zero_client::ast::System; |
| 21 | +use zero_client::client::{ |
| 22 | + MaterializedView, Mutator, SocketFactory, Transaction, Zero, ZeroOptions, |
| 23 | +}; |
| 24 | +use zero_client::json::{self, Object, Value}; |
| 25 | +use zero_client::query::AnyQuery; |
| 26 | +use zero_client::schema::Schema; |
| 27 | +use zero_client::testing::{ManualScheduler, MockSocketFactory, RecordingLogSink}; |
| 28 | +use zero_client::ttl::Ttl; |
| 29 | + |
| 30 | +fn schema() -> Arc<Schema> { |
| 31 | + let schema = json::parse( |
| 32 | + r#"{"tables": {"issue": {"name": "issue", "columns": { |
| 33 | + "id": {"type": "string"}, "title": {"type": "string"}}, "primaryKey": ["id"]}}}"#, |
| 34 | + ) |
| 35 | + .unwrap(); |
| 36 | + Arc::new(Schema::from_json(&schema).unwrap()) |
| 37 | +} |
| 38 | + |
| 39 | +fn id(i: u32) -> String { |
| 40 | + format!("i{i:06}") |
| 41 | +} |
| 42 | + |
| 43 | +/// `ids` as the mutator's arguments. |
| 44 | +fn args(ids: impl IntoIterator<Item = u32>) -> Option<Value> { |
| 45 | + Some(Value::Array( |
| 46 | + ids.into_iter().map(|i| Value::from(id(i))).collect(), |
| 47 | + )) |
| 48 | +} |
| 49 | + |
| 50 | +struct Client { |
| 51 | + zero: Zero, |
| 52 | + scheduler: Rc<ManualScheduler>, |
| 53 | +} |
| 54 | + |
| 55 | +impl Client { |
| 56 | + /// A local client with a view of every issue. |
| 57 | + fn new() -> (Client, MaterializedView) { |
| 58 | + let scheduler = ManualScheduler::new(1_700_000_000_000.0); |
| 59 | + let insert = Mutator::new("insert", |tx: &Transaction, args, _: &()| { |
| 60 | + for v in args.unwrap().as_array().unwrap() { |
| 61 | + let mut row = Object::new(); |
| 62 | + row.insert("id", v.clone()); |
| 63 | + row.insert("title", Value::from("t")); |
| 64 | + tx.table("issue").insert(&row)?; |
| 65 | + } |
| 66 | + Ok(None) |
| 67 | + }); |
| 68 | + let delete = Mutator::new("delete", |tx: &Transaction, args, _: &()| { |
| 69 | + for v in args.unwrap().as_array().unwrap() { |
| 70 | + let mut row = Object::new(); |
| 71 | + row.insert("id", v.clone()); |
| 72 | + tx.table("issue").delete(&row)?; |
| 73 | + } |
| 74 | + Ok(None) |
| 75 | + }); |
| 76 | + let zero = Zero::new( |
| 77 | + schema(), |
| 78 | + (), |
| 79 | + vec![insert, delete], |
| 80 | + ZeroOptions { |
| 81 | + log_sink: Some(Rc::new(RecordingLogSink::default())), |
| 82 | + ..ZeroOptions::default() |
| 83 | + }, |
| 84 | + scheduler.clone(), |
| 85 | + None, |
| 86 | + ) |
| 87 | + .unwrap(); |
| 88 | + scheduler.run_checkpoints(); |
| 89 | + let view = zero |
| 90 | + .materialize( |
| 91 | + &AnyQuery::new(schema(), "issue", System::Client), |
| 92 | + &Ttl::default(), |
| 93 | + ) |
| 94 | + .unwrap(); |
| 95 | + scheduler.run_checkpoints(); |
| 96 | + (Client { zero, scheduler }, view) |
| 97 | + } |
| 98 | + |
| 99 | + fn mutate(&self, name: &str, ids: impl IntoIterator<Item = u32>) { |
| 100 | + self.zero.mutate(name, args(ids)); |
| 101 | + self.scheduler.run_checkpoints(); |
| 102 | + } |
| 103 | + |
| 104 | + /// Leaves `i010300` where the B+tree couldn't find it. |
| 105 | + fn lose_a_row(&self) { |
| 106 | + // Leaves of 32 keys; P1's first leaf has 10240..=10550, its second |
| 107 | + // 10560..=10870. |
| 108 | + self.mutate("insert", (0..2081).map(|i| i * 10)); |
| 109 | + // The second leaf full (64), so the first can't merge into it. |
| 110 | + self.mutate("insert", (0..32).map(|i| 10561 + i * 10)); |
| 111 | + // The first leaf emptied and removed: the second, bound 10560, is |
| 112 | + // P1's first child, and P1's bound is 10240. |
| 113 | + self.mutate("delete", (0..32).map(|i| 10240 + i * 10)); |
| 114 | + self.mutate("insert", [10300]); |
| 115 | + // P1 shrunk until it merges into P0. |
| 116 | + self.mutate("delete", (0..563).map(|i| 20800 - i * 10)); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +fn has(view: &MaterializedView, i: u32) -> bool { |
| 121 | + view.data() |
| 122 | + .entries() |
| 123 | + .iter() |
| 124 | + .any(|e| e.row.get("id").and_then(Value::as_str) == Some(&id(i))) |
| 125 | +} |
| 126 | + |
| 127 | +#[test] |
| 128 | +fn a_row_after_a_merge_of_the_replica_tree_can_be_deleted() { |
| 129 | + let (client, view) = Client::new(); |
| 130 | + client.lose_a_row(); |
| 131 | + assert!(has(&view, 10300)); |
| 132 | + client.mutate("delete", [10300]); |
| 133 | + assert!(!has(&view, 10300), "the deleted row is still in the view"); |
| 134 | +} |
| 135 | + |
| 136 | +#[test] |
| 137 | +fn a_row_after_a_merge_of_the_replica_tree_can_be_inserted_again() { |
| 138 | + let (client, view) = Client::new(); |
| 139 | + client.lose_a_row(); |
| 140 | + client.mutate("delete", [10300]); |
| 141 | + client.mutate("insert", [10300]); |
| 142 | + assert!(has(&view, 10300)); |
| 143 | +} |
| 144 | + |
| 145 | +/// The steps of [`Client::lose_a_row`] as pokes: puts and dels of the |
| 146 | +/// server's rows, then a del and a put of the row. |
| 147 | +#[test] |
| 148 | +fn a_row_after_a_merge_of_the_replica_tree_follows_the_server() { |
| 149 | + let scheduler = ManualScheduler::new(1_700_000_000_000.0); |
| 150 | + let factory = MockSocketFactory::new(); |
| 151 | + let zero = Zero::new( |
| 152 | + schema(), |
| 153 | + (), |
| 154 | + Vec::new(), |
| 155 | + ZeroOptions { |
| 156 | + cache_url: Some("http://zero.test".into()), |
| 157 | + user_id: Some("u1".into()), |
| 158 | + log_sink: Some(Rc::new(RecordingLogSink::default())), |
| 159 | + ..ZeroOptions::default() |
| 160 | + }, |
| 161 | + scheduler.clone(), |
| 162 | + Some(factory.clone() as Rc<dyn SocketFactory>), |
| 163 | + ) |
| 164 | + .unwrap(); |
| 165 | + let view = zero |
| 166 | + .materialize( |
| 167 | + &AnyQuery::new(schema(), "issue", System::Client), |
| 168 | + &Ttl::default(), |
| 169 | + ) |
| 170 | + .unwrap(); |
| 171 | + scheduler.advance(0.0); |
| 172 | + let socket = factory.sockets()[0].clone(); |
| 173 | + socket.receive_text(r#"["connected",{"wsid":"w1"}]"#); |
| 174 | + let cookie = Cell::new(0); |
| 175 | + let poke = |patch: Vec<String>| { |
| 176 | + let n = cookie.get() + 1; |
| 177 | + let base = if n == 1 { |
| 178 | + "null".to_owned() |
| 179 | + } else { |
| 180 | + format!(r#""{:02}""#, n - 1) |
| 181 | + }; |
| 182 | + cookie.set(n); |
| 183 | + socket.receive_text(&format!( |
| 184 | + r#"["pokeStart",{{"pokeID":"p{n}","baseCookie":{base}}}]"# |
| 185 | + )); |
| 186 | + socket.receive_text(&format!( |
| 187 | + r#"["pokePart",{{"pokeID":"p{n}","rowsPatch":[{}]}}]"#, |
| 188 | + patch.join(",") |
| 189 | + )); |
| 190 | + socket.receive_text(&format!( |
| 191 | + r#"["pokeEnd",{{"pokeID":"p{n}","cookie":"{n:02}"}}]"# |
| 192 | + )); |
| 193 | + scheduler.advance(20.0); |
| 194 | + }; |
| 195 | + let put = |ids: Vec<u32>| { |
| 196 | + poke( |
| 197 | + ids.into_iter() |
| 198 | + .map(|i| { |
| 199 | + format!( |
| 200 | + r#"{{"op":"put","tableName":"issue","value":{{"id":"{}","title":"t"}}}}"#, |
| 201 | + id(i) |
| 202 | + ) |
| 203 | + }) |
| 204 | + .collect(), |
| 205 | + ) |
| 206 | + }; |
| 207 | + let del = |ids: Vec<u32>| { |
| 208 | + poke( |
| 209 | + ids.into_iter() |
| 210 | + .map(|i| { |
| 211 | + format!( |
| 212 | + r#"{{"op":"del","tableName":"issue","id":{{"id":"{}"}}}}"#, |
| 213 | + id(i) |
| 214 | + ) |
| 215 | + }) |
| 216 | + .collect(), |
| 217 | + ) |
| 218 | + }; |
| 219 | + put((0..2081).map(|i| i * 10).collect()); |
| 220 | + put((0..32).map(|i| 10561 + i * 10).collect()); |
| 221 | + del((0..32).map(|i| 10240 + i * 10).collect()); |
| 222 | + put(vec![10300]); |
| 223 | + del((0..563).map(|i| 20800 - i * 10).collect()); |
| 224 | + assert!(has(&view, 10300)); |
| 225 | + del(vec![10300]); |
| 226 | + assert!( |
| 227 | + !has(&view, 10300), |
| 228 | + "the row the server deleted is still in the view" |
| 229 | + ); |
| 230 | + put(vec![10300]); |
| 231 | + assert!(has(&view, 10300)); |
| 232 | + assert_eq!(view.data().entries().len(), 2081 + 32 - 32 + 1 - 563); |
| 233 | +} |
0 commit comments