project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use crate::version::Frontiers; |
| 2 | use num::Zero; |
| 3 | use std::{collections::BTreeMap, ops::Range}; |
| 4 | |
| 5 | use super::*; |
| 6 | |
| 7 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 8 | struct IdHeapItem { |
| 9 | id: ID, |
| 10 | lamport: Lamport, |
| 11 | } |
| 12 | |
| 13 | impl PartialOrd for IdHeapItem { |
| 14 | fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 15 | Some(self.cmp(other)) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | impl Ord for IdHeapItem { |
| 20 | fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 21 | self.lamport.cmp(&other.lamport).reverse() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | #[allow(dead_code)] |
| 26 | pub(crate) fn iter_dag_with_vv<T, D: Dag<Node = T>>(dag: &D) -> DagIteratorVV<'_, T> { |
| 27 | DagIteratorVV { |
| 28 | dag, |
| 29 | vv_map: Default::default(), |
| 30 | heap: BinaryHeap::new(), |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | #[allow(dead_code)] |
| 35 | pub(crate) fn iter_dag<T>(dag: &dyn Dag<Node = T>) -> DagIterator<'_, T> { |
| 36 | DagIterator { |
| 37 | dag, |
| 38 | visited: VersionVector::new(), |
| 39 | heap: BinaryHeap::new(), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | pub struct DagIterator<'a, T> { |
| 44 | dag: &'a dyn Dag<Node = T>, |
| 45 | /// Because all deps' lamports are smaller than current node's lamport. |
| 46 | /// We can use the lamport to sort the nodes so that each node's deps are processed before itself. |
| 47 | /// |
| 48 | /// The ids in this heap are start ids of nodes. It won't be a id pointing to the middle of a node. |
| 49 | heap: BinaryHeap<IdHeapItem>, |
| 50 | visited: VersionVector, |
| 51 | } |
| 52 | |
| 53 | /// Should only use it on debug, because it's slow and likely to use lots of mem |
| 54 | impl<T: DagNode> Iterator for DagIterator<'_, T> { |
| 55 | type Item = T; |
| 56 | |
| 57 | fn next(&mut self) -> Option<Self::Item> { |
| 58 | if self.visited.is_empty() { |
| 59 | if self.dag.vv().is_empty() { |
| 60 | return None; |
| 61 | } |
| 62 | |
| 63 | for (&client_id, _) in self.dag.vv().iter() { |
| 64 | if let Some(node) = self.dag.get(ID::new(client_id, 0)) { |
| 65 | self.heap.push(IdHeapItem { |
| 66 | id: ID::new(client_id, 0), |
| 67 | lamport: node.lamport(), |
| 68 | }); |
| 69 | } |
| 70 | |
| 71 | self.visited.insert(client_id, 0); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if !self.heap.is_empty() { |
| 76 | let item = self.heap.pop().unwrap(); |
| 77 | let id = item.id; |
| 78 | let node = self.dag.get(id).unwrap(); |
| 79 | // push next node from the same client to the heap |
| 80 | let next_id = id.inc(node.content_len() as i32); |
| 81 | if self.dag.contains(next_id) { |
| 82 | let next_node = self.dag.get(next_id).unwrap(); |
| 83 | self.heap.push(IdHeapItem { |
| 84 | id: next_id, |
| 85 | lamport: next_node.lamport(), |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | return Some(node); |
| 90 | } |
| 91 | |
| 92 | None |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pub(crate) struct DagIteratorVV<'a, T> { |
| 97 | dag: &'a dyn Dag<Node = T>, |
| 98 | /// we should keep every nodes starting id inside this map |
| 99 | vv_map: FxHashMap<ID, VersionVector>, |
| 100 | /// Because all deps' lamports are smaller than current node's lamport. |
| 101 | /// We can use the lamport to sort the nodes so that each node's deps are processed before itself. |
| 102 | /// |
| 103 | /// The ids in this heap are start ids of nodes. It won't be a id pointing to the middle of a node. |
| 104 | heap: BinaryHeap<IdHeapItem>, |
| 105 | } |
| 106 | |
| 107 | /// Should only use it on debug, because it's slow and likely to use lots of mem |
| 108 | impl<T: DagNode> Iterator for DagIteratorVV<'_, T> { |
| 109 | type Item = (T, VersionVector); |
| 110 | |
| 111 | fn next(&mut self) -> Option<Self::Item> { |
| 112 | if self.vv_map.is_empty() { |
| 113 | if self.dag.vv().is_empty() { |
| 114 | return None; |
| 115 | } |
| 116 | |
| 117 | for (&client_id, _) in self.dag.vv().iter() { |
| 118 | let vv = VersionVector::new(); |
| 119 | if let Some(node) = self.dag.get(ID::new(client_id, 0)) { |
| 120 | if node.lamport() == 0 { |
| 121 | self.vv_map.insert(ID::new(client_id, 0), vv.clone()); |
| 122 | } |
| 123 | |
| 124 | self.heap.push(IdHeapItem { |
| 125 | id: ID::new(client_id, 0), |
| 126 | lamport: node.lamport(), |
| 127 | }); |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if !self.heap.is_empty() { |
| 133 | let item = self.heap.pop().unwrap(); |
| 134 | let id = item.id; |
| 135 | let node = self.dag.get(id).unwrap(); |
| 136 | debug_assert_eq!(id, node.id_start()); |
| 137 | let mut vv = { |
| 138 | // calculate vv |
| 139 | let mut vv: Option<VersionVector> = None; |
| 140 | for dep_id in node.deps().iter() { |
| 141 | let dep = self.dag.get(dep_id).unwrap(); |
| 142 | let dep_vv = self.vv_map.get(&dep.id_start()).unwrap(); |
| 143 | if let Some(vv) = vv.as_mut() { |
| 144 | vv.merge(dep_vv); |
| 145 | } else { |
| 146 | vv = Some(dep_vv.clone()); |
| 147 | } |
| 148 | |
| 149 | if dep.id_start() != dep_id { |
| 150 | vv.as_mut().unwrap().set_last(dep_id); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | vv.unwrap_or_default() |
| 155 | }; |
| 156 | |
| 157 | vv.try_update_last(id); |
| 158 | self.vv_map.insert(id, vv.clone()); |
| 159 | |
| 160 | // push next node from the same client to the heap |
| 161 | let next_id = id.inc(node.content_len() as i32); |
| 162 | if self.dag.contains(next_id) { |
| 163 | let next_node = self.dag.get(next_id).unwrap(); |
| 164 | self.heap.push(IdHeapItem { |
| 165 | id: next_id, |
| 166 | lamport: next_node.lamport(), |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | return Some((node, vv)); |
| 171 | } |
| 172 | |
| 173 | None |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /// Visit every span in the target IdSpanVector. |
| 178 | /// It's guaranteed that the spans are visited in causal order, and each span is visited only once. |
| 179 | /// When visiting a span, we will checkout to the version where the span was created |
| 180 | pub(crate) struct DagCausalIter<'a, Dag> { |
| 181 | dag: &'a Dag, |
| 182 | frontier: Frontiers, |
| 183 | target: IdSpanVector, |
| 184 | /// how many dependencies are inside target for each id |
| 185 | out_degrees: FxHashMap<ID, usize>, |
| 186 | succ: BTreeMap<ID, Vec<ID>>, |
| 187 | stack: Vec<ID>, |
| 188 | } |
| 189 | |
| 190 | #[derive(Debug)] |
| 191 | pub(crate) struct IterReturn<T> { |
| 192 | /// data is a reference, it need to be sliced by the counter_range to get the underlying data |
| 193 | pub data: T, |
| 194 | /// data[slice] is the data we want to return |
| 195 | #[allow(unused)] |
| 196 | pub slice: Range<i32>, |
| 197 | } |
| 198 | |
| 199 | impl<'a, T: DagNode, D: Dag<Node = T> + Debug> DagCausalIter<'a, D> { |
| 200 | #[cfg(test)] |
| 201 | pub fn new(dag: &'a D, from: Frontiers, target: IdSpanVector) -> Self { |
| 202 | Self::try_new(dag, from, target).unwrap() |
| 203 | } |
| 204 | |
| 205 | pub fn try_new(dag: &'a D, from: Frontiers, target: IdSpanVector) -> LoroResult<Self> { |
| 206 | let mut out_degrees: FxHashMap<ID, usize> = FxHashMap::default(); |
| 207 | let mut succ: BTreeMap<ID, Vec<ID>> = BTreeMap::default(); |
| 208 | let mut stack = Vec::new(); |
| 209 | let mut q = vec![]; |
| 210 | for id in target.iter() { |
| 211 | if id.1.content_len() > 0 { |
| 212 | let id = id.id_start(); |
| 213 | q.push(id); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | // traverse all nodes, calculate the out_degrees |
| 218 | // if out_degree is 0, then it can be iterated directly |
| 219 | while let Some(id) = q.pop() { |
| 220 | let client = id.peer; |
| 221 | let node = dag.try_get(id)?.ok_or(LoroError::FrontiersNotFound(id))?; |
| 222 | let deps = node.deps(); |
| 223 | out_degrees.insert( |
| 224 | id, |
| 225 | deps.iter() |
| 226 | .filter(|&dep| { |
| 227 | if let Some(span) = target.get(&dep.peer) { |
| 228 | let included_in_target = |
| 229 | dep.counter >= span.min() && dep.counter <= span.max(); |
| 230 | if included_in_target { |
| 231 | succ.entry(dep).or_default().push(id); |
| 232 | } |
| 233 | included_in_target |
| 234 | } else { |
| 235 | false |
| 236 | } |
| 237 | }) |
| 238 | .count(), |
| 239 | ); |
| 240 | let target_span = target.get(&client).unwrap(); |
| 241 | let last_counter = node.id_last().counter; |
| 242 | if target_span.max() > last_counter { |
| 243 | q.push(ID::new(client, last_counter + 1)) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Enforce per-peer counter order. Within a single peer, the iterator |
| 248 | // must visit tracked node-starts in ascending counter order even when |
| 249 | // the nodes are concurrent (i.e. don't declare each other as deps). |
| 250 | // Without this, two same-peer nodes can both have zero in-degree and |
| 251 | // end up on the stack at once; the LIFO pop order would then violate |
| 252 | // the iterator's invariant that each popped id equals the current |
| 253 | // target_span.min() for its peer. |
| 254 | let mut by_peer: FxHashMap<PeerID, Vec<Counter>> = FxHashMap::default(); |
| 255 | for id in out_degrees.keys() { |
| 256 | by_peer.entry(id.peer).or_default().push(id.counter); |
| 257 | } |
| 258 | for (peer, mut counters) in by_peer { |
| 259 | if counters.len() < 2 { |
| 260 | continue; |
| 261 | } |
| 262 | counters.sort_unstable(); |
| 263 | for pair in counters.windows(2) { |
| 264 | let prev = ID::new(peer, pair[0]); |
| 265 | let curr = ID::new(peer, pair[1]); |
| 266 | if let Some(deg) = out_degrees.get_mut(&curr) { |
| 267 | *deg += 1; |
| 268 | } |
| 269 | succ.entry(prev).or_default().push(curr); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | out_degrees.retain(|k, v| { |
| 274 | if v.is_zero() { |
| 275 | stack.push(*k); |
| 276 | return false; |
| 277 | } |
| 278 | true |
| 279 | }); |
| 280 | |
| 281 | Ok(Self { |
| 282 | dag, |
| 283 | frontier: from, |
| 284 | target, |
| 285 | out_degrees, |
| 286 | succ, |
| 287 | stack, |
| 288 | }) |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | impl<T: DagNode, D: Dag<Node = T>> Iterator for DagCausalIter<'_, D> { |
| 293 | type Item = IterReturn<T>; |
| 294 | |
| 295 | fn next(&mut self) -> Option<Self::Item> { |
| 296 | loop { |
| 297 | if self.stack.is_empty() { |
| 298 | debug_assert_eq!( |
| 299 | 0, |
| 300 | self.target |
| 301 | .iter() |
| 302 | .map(|x| x.1.content_len() as i32) |
| 303 | .sum::<i32>() |
| 304 | ); |
| 305 | return None; |
| 306 | } |
| 307 | |
| 308 | let node_id = self.stack.pop().unwrap(); |
| 309 | let (node, slice_from, slice_end, next_same_peer) = { |
| 310 | let target_span = self.target.get_mut(&node_id.peer).unwrap(); |
| 311 | if node_id.counter != target_span.min() { |
| 312 | debug_assert!( |
| 313 | node_id.counter < target_span.min(), |
| 314 | "{} {:?}", |
| 315 | node_id, |
| 316 | target_span |
| 317 | ); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | // node_id may point into the middle of the node, we need to slice |
| 322 | // try_new has loaded every target node before iteration starts. |
| 323 | let node = self.dag.get(node_id).unwrap(); |
| 324 | // node start_id may be smaller than node_id |
| 325 | let counter = node.id_span().counter; |
| 326 | let slice_from = if counter.start < target_span.start { |
| 327 | target_span.start - counter.start |
| 328 | } else { |
| 329 | 0 |
| 330 | }; |
| 331 | let slice_end = if counter.end < target_span.end { |
| 332 | counter.end - counter.start |
| 333 | } else { |
| 334 | target_span.end - counter.start |
| 335 | }; |
| 336 | |
| 337 | if slice_end <= slice_from { |
| 338 | debug_assert_eq!(slice_end, slice_from, "{node_id} {:?}", target_span); |
| 339 | continue; |
| 340 | } |
| 341 | |
| 342 | let consumed_last_counter = counter.start + slice_end - 1; |
| 343 | target_span.set_start(consumed_last_counter + 1); |
| 344 | let next_same_peer = if target_span.content_len() > 0 { |
| 345 | Some(ID::new(node_id.peer, target_span.min())) |
| 346 | } else { |
| 347 | None |
| 348 | }; |
| 349 | |
| 350 | (node, slice_from, slice_end, next_same_peer) |
| 351 | }; |
| 352 | |
| 353 | if let Some(next_id) = next_same_peer { |
| 354 | if !self.out_degrees.contains_key(&next_id) && !self.stack.contains(&next_id) { |
| 355 | self.stack.push(next_id); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // NOTE: we expect user to update the tracker, to apply node, after visiting the node |
| 360 | self.frontier = Frontiers::from_id(node.id_start().inc(slice_end - 1)); |
| 361 | |
| 362 | let current_peer = node_id.peer; |
| 363 | let mut keys = Vec::new(); |
| 364 | let mut heap = BinaryHeap::new(); |
| 365 | // The in-degree of the successor node minus 1, and if it becomes 0, it is added to the heap |
| 366 | for (key, succ) in self.succ.range(node.id_start()..node.id_end()) { |
| 367 | keys.push(*key); |
| 368 | for succ_id in succ.iter() { |
| 369 | self.out_degrees.entry(*succ_id).and_modify(|i| *i -= 1); |
| 370 | if let Some(in_degree) = self.out_degrees.get(succ_id) { |
| 371 | if in_degree.is_zero() { |
| 372 | heap.push((succ_id.peer != current_peer, *succ_id)); |
| 373 | self.out_degrees.remove(succ_id); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | // Nodes that have been traversed are removed from the graph to avoid being covered by other node ranges again |
| 379 | keys.into_iter().for_each(|k| { |
| 380 | self.succ.remove(&k); |
| 381 | }); |
| 382 | while let Some(id) = heap.pop() { |
| 383 | self.stack.push(id.1) |
| 384 | } |
| 385 | |
| 386 | return Some(IterReturn { |
| 387 | data: node, |
| 388 | slice: slice_from..slice_end, |
| 389 | }); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | #[cfg(test)] |
| 395 | mod tests { |
| 396 | use std::collections::BTreeMap; |
| 397 | |
| 398 | use super::*; |
| 399 | use crate::{ |
| 400 | change::Lamport, |
| 401 | id::ID, |
| 402 | span::{CounterSpan, HasId, HasLamport}, |
| 403 | version::VersionVector, |
| 404 | }; |
| 405 | use rle::{HasLength, Sliceable}; |
| 406 | |
| 407 | #[derive(Debug, Clone)] |
| 408 | struct DummyNode { |
| 409 | id: ID, |
| 410 | lamport: Lamport, |
| 411 | len: usize, |
| 412 | deps: Frontiers, |
| 413 | } |
| 414 | |
| 415 | impl DagNode for DummyNode { |
| 416 | fn deps(&self) -> &Frontiers { |
| 417 | &self.deps |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | impl Sliceable for DummyNode { |
| 422 | fn slice(&self, from: usize, to: usize) -> Self { |
| 423 | Self { |
| 424 | id: self.id.inc(from as i32), |
| 425 | lamport: self.lamport + from as Lamport, |
| 426 | len: to - from, |
| 427 | deps: if from > 0 { |
| 428 | self.id.inc(from as i32 - 1).into() |
| 429 | } else { |
| 430 | self.deps.clone() |
| 431 | }, |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | impl HasLamport for DummyNode { |
| 437 | fn lamport(&self) -> Lamport { |
| 438 | self.lamport |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | impl HasId for DummyNode { |
| 443 | fn id_start(&self) -> ID { |
| 444 | self.id |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | impl HasLength for DummyNode { |
| 449 | fn content_len(&self) -> usize { |
| 450 | self.len |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | #[derive(Debug)] |
| 455 | struct DummyDag { |
| 456 | nodes: BTreeMap<ID, DummyNode>, |
| 457 | vv: VersionVector, |
| 458 | frontier: Frontiers, |
| 459 | } |
| 460 | |
| 461 | impl Dag for DummyDag { |
| 462 | type Node = DummyNode; |
| 463 | |
| 464 | fn get(&self, id: ID) -> Option<Self::Node> { |
| 465 | self.nodes |
| 466 | .range(..=id) |
| 467 | .next_back() |
| 468 | .filter(|(_, node)| node.contains_id(id)) |
| 469 | .map(|(_, node)| node.clone()) |
| 470 | } |
| 471 | |
| 472 | fn frontier(&self) -> &Frontiers { |
| 473 | &self.frontier |
| 474 | } |
| 475 | |
| 476 | fn vv(&self) -> &VersionVector { |
| 477 | &self.vv |
| 478 | } |
| 479 | |
| 480 | fn contains(&self, id: ID) -> bool { |
| 481 | self.nodes |
| 482 | .range(..=id) |
| 483 | .next_back() |
| 484 | .is_some_and(|(_, node)| node.contains_id(id)) |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn stale_iterator_state_repairs_missing_same_peer_continuation() { |
| 490 | let first = DummyNode { |
| 491 | id: ID::new(1, 0), |
| 492 | lamport: 0, |
| 493 | len: 5, |
| 494 | deps: Frontiers::default(), |
| 495 | }; |
| 496 | let second = DummyNode { |
| 497 | id: ID::new(1, 5), |
| 498 | lamport: 5, |
| 499 | len: 5, |
| 500 | deps: ID::new(1, 4).into(), |
| 501 | }; |
| 502 | let mut vv = VersionVector::default(); |
| 503 | vv.set_end(second.id_end()); |
| 504 | let dag = DummyDag { |
| 505 | frontier: second.id_last().into(), |
| 506 | nodes: [(first.id_start(), first), (second.id_start(), second)] |
| 507 | .into_iter() |
| 508 | .collect(), |
| 509 | vv, |
| 510 | }; |
| 511 | |
| 512 | let mut target = IdSpanVector::default(); |
| 513 | target.insert(1, CounterSpan::new(0, 7)); |
| 514 | let mut iter = DagCausalIter { |
| 515 | dag: &dag, |
| 516 | frontier: Frontiers::default(), |
| 517 | target, |
| 518 | out_degrees: Default::default(), |
| 519 | succ: BTreeMap::default(), |
| 520 | // This models the stale iterator state after `new()` saw one large |
| 521 | // node and queued only the first peer segment. |
| 522 | stack: vec![ID::new(1, 0)], |
| 523 | }; |
| 524 | |
| 525 | let first = iter.next().expect("first segment should be yielded"); |
| 526 | assert_eq!(first.data.id_start(), ID::new(1, 0)); |
| 527 | assert_eq!(first.slice, 0..5); |
| 528 | |
| 529 | let second = iter.next().expect("second segment should be re-queued"); |
| 530 | assert_eq!(second.data.id_start(), ID::new(1, 5)); |
| 531 | assert_eq!(second.slice, 0..2); |
| 532 | |
| 533 | assert!(iter.next().is_none()); |
| 534 | } |
| 535 | |
| 536 | /// Regression: before the fix, `DagCausalIter::new` would push both |
| 537 | /// `(peer=1, counter=0)` and `(peer=1, counter=5)` onto the initial |
| 538 | /// stack because neither node has any *in-target* dependency. The LIFO |
| 539 | /// pop would then surface `(peer=1, counter=5)` first while |
| 540 | /// `target_span.min()` is still `0`, tripping the causal-order |
| 541 | /// invariant (historically: "slice_end > slice_from" / "counter < |
| 542 | /// target_span.min()"). With the fix the iter synthesizes a per-peer |
| 543 | /// ordering edge so the second node is only released after the first is |
| 544 | /// consumed. |
| 545 | #[test] |
| 546 | fn two_concurrent_same_peer_nodes_visit_in_counter_order() { |
| 547 | let first = DummyNode { |
| 548 | id: ID::new(1, 0), |
| 549 | lamport: 10, |
| 550 | len: 5, |
| 551 | deps: Frontiers::default(), |
| 552 | }; |
| 553 | // `second` is concurrent with `first` from peer 1's perspective: |
| 554 | // its only dep lives outside the target span (peer 2), so the BFS |
| 555 | // records zero in-degree and would historically race to the stack |
| 556 | // alongside `first`. |
| 557 | let second = DummyNode { |
| 558 | id: ID::new(1, 5), |
| 559 | lamport: 20, |
| 560 | len: 5, |
| 561 | deps: ID::new(2, 0).into(), |
| 562 | }; |
| 563 | let side = DummyNode { |
| 564 | id: ID::new(2, 0), |
| 565 | lamport: 0, |
| 566 | len: 1, |
| 567 | deps: Frontiers::default(), |
| 568 | }; |
| 569 | |
| 570 | let mut vv = VersionVector::default(); |
| 571 | vv.set_end(first.id_end()); |
| 572 | vv.set_end(second.id_end()); |
| 573 | vv.set_end(side.id_end()); |
| 574 | |
| 575 | let dag = DummyDag { |
| 576 | frontier: second.id_last().into(), |
| 577 | nodes: [ |
| 578 | (first.id_start(), first), |
| 579 | (second.id_start(), second), |
| 580 | (side.id_start(), side), |
| 581 | ] |
| 582 | .into_iter() |
| 583 | .collect(), |
| 584 | vv, |
| 585 | }; |
| 586 | |
| 587 | // Target only peer 1. `second`'s dep on (peer 2, 0) is thus |
| 588 | // *outside* the target vector — the exact condition that made both |
| 589 | // same-peer nodes reach the stack concurrently before the fix. |
| 590 | let mut target = IdSpanVector::default(); |
| 591 | target.insert(1, CounterSpan::new(0, 10)); |
| 592 | |
| 593 | let mut iter = DagCausalIter::new(&dag, Frontiers::default(), target); |
| 594 | |
| 595 | let a = iter.next().expect("first peer-1 segment"); |
| 596 | assert_eq!(a.data.id_start(), ID::new(1, 0)); |
| 597 | assert_eq!(a.slice, 0..5); |
| 598 | |
| 599 | let b = iter.next().expect("second peer-1 segment"); |
| 600 | assert_eq!(b.data.id_start(), ID::new(1, 5)); |
| 601 | assert_eq!(b.slice, 0..5); |
| 602 | |
| 603 | assert!(iter.next().is_none()); |
| 604 | } |
| 605 | |
| 606 | #[test] |
| 607 | fn larger_node_only_advances_consumed_slice() { |
| 608 | let node = DummyNode { |
| 609 | id: ID::new(1, 0), |
| 610 | lamport: 0, |
| 611 | len: 10, |
| 612 | deps: Frontiers::default(), |
| 613 | }; |
| 614 | let mut vv = VersionVector::default(); |
| 615 | vv.set_end(node.id_end()); |
| 616 | let dag = DummyDag { |
| 617 | frontier: node.id_last().into(), |
| 618 | nodes: [(node.id_start(), node)].into_iter().collect(), |
| 619 | vv, |
| 620 | }; |
| 621 | |
| 622 | let mut target = IdSpanVector::default(); |
| 623 | target.insert(1, CounterSpan::new(0, 7)); |
| 624 | let mut iter = DagCausalIter { |
| 625 | dag: &dag, |
| 626 | frontier: Frontiers::default(), |
| 627 | target, |
| 628 | out_degrees: Default::default(), |
| 629 | succ: BTreeMap::default(), |
| 630 | stack: vec![ID::new(1, 0)], |
| 631 | }; |
| 632 | |
| 633 | let item = iter.next().expect("target slice should be yielded"); |
| 634 | assert_eq!(item.slice, 0..7); |
| 635 | assert_eq!(iter.target.get(&1).unwrap().content_len(), 0); |
| 636 | } |
| 637 | } |