relate_executor/graph/label_mutation.rs
1//! LABS-425 (stellar round 1, the mod.rs 1k-creep finding): the
2//! label-mutation twins — `add_labels` / `remove_labels`, one input
3//! domain, one direction each — in their own impl-block file (the
4//! graph module's methods-in-their-own-file precedent, the sibling of
5//! the degree-stats family). The twins' shape is deliberately paired:
6//! one effect-shaped guard per twin (the request's ids resolved against
7//! the live label set, so an empty request, an all-already-carried add,
8//! an all-uninterned remove, and a missing node all no-op), one effect id
9//! vector, the same actually-mutated-ids guard on the redo/dirty pair,
10//! the same effect-derived WAL payload, and the same unguarded hand-off to
11//! the degree-stats family's own empty-set early returns (LABS-425
12//! round-1 F-1-17, rounds 3-4).
13//! `Graph` (graph/mod.rs) keeps the struct; every label-set mutation
14//! seam lives here.
15
16use super::*;
17
18impl Graph {
19 pub fn add_labels(&self, id: NodeId, labels: &[String]) {
20 let before = self.node(id);
21 if before.is_none() {
22 return;
23 }
24 let before_shape = self.node_chain_shape(id);
25 // The effect, not the request: the ids the node does not already
26 // carry. ADD mints a name it has never seen (unlike REMOVE, whose
27 // drop set is lookups only), so each request label is interned here
28 // — before the scope probe takes the map lock (interner shard read,
29 // then map lock, the seam lock order). A request of only carried
30 // labels has no effect and no-ops below.
31 let live = self.live_label_ids(id);
32 let mut added_ids: Vec<u32> = Vec::new();
33 for label in labels {
34 let lid = self.interner.intern(label);
35 if !live.contains(&lid) && !added_ids.contains(&lid) {
36 added_ids.push(lid);
37 }
38 }
39 if added_ids.is_empty() {
40 return;
41 }
42 // ADR-0687: the added label ids are the footprint tokens whose row
43 // counts just moved.
44 self.note_changed_tokens(added_ids.iter().copied());
45 // ADR-0439: adding a constraint label introduces the node's
46 // constrained tuple; capture the before-image now. A label no
47 // constraint carries cannot change any tuple — the map's scope is
48 // the only source of that answer.
49 let keys = self.tracked_node_edit(id, |scope| scope.touches_node_labels(&added_ids));
50 let mut n = self
51 .cow_node(id)
52 .expect("a live node has a copy-on-write version");
53 n.labels.extend(added_ids.iter().copied());
54 crate::memory::fit_reserved(&mut n.labels);
55 // New label pairs must pick up existing properties.
56 let string_kids: Vec<u32> = n.props.iter().map(|(kid, _)| kid).collect();
57 // The edited live view is in hand: hand it to the map before it
58 // moves into the version chain (the canonical completion arm).
59 self.finish_node_edit(keys, id, Some(&n));
60 self.put_node(id, n);
61 // The WAL/catalog names derive from the effect ids, through the
62 // canonical id→name lookup (remove_labels' policy): the ids are node
63 // labels, so the published-before-use invariant holds.
64 let added: Vec<String> = added_ids
65 .iter()
66 .map(|&lid| String::from(&*self.interner.get(lid)))
67 .collect();
68 self.bump_stats(|s| s.labels_added += added_ids.len() as i64);
69 self.catalog
70 .lock()
71 .unwrap()
72 .record_labels(added.iter().cloned());
73 self.push_redo(RedoOp::add_labels(id, added));
74 self.mark_node_dirty(id);
75 // ADR-0595: a first label takes the node out of the empty-label
76 // bucket — that posting dies here, at the removal site, so the
77 // tail drain reclaims it.
78 if live.is_empty() {
79 self.suspect_postings(id, &[]);
80 }
81 self.index_add(id);
82 for &lid in &added_ids {
83 for &kid in &string_kids {
84 self.declared_dirty(lid, kid, id);
85 }
86 }
87 self.charge_node_after_edit(id, before.as_ref(), before_shape);
88 // The one count-family entry: the degree rebucket (LABS-351), the
89 // incident pair counts and the exact label counts (LABS-370).
90 self.apply_label_delta_stats(id, &live, &added_ids, CountDelta::Add);
91 }
92
93 pub fn remove_labels(&self, id: NodeId, labels: &[String]) {
94 // A name never interned cannot be on the node (its label set is
95 // interned ids): the drop set resolves through lookups only, so
96 // a stray request name neither mints an id nor charges
97 // interned-string bytes.
98 let drop: HashSet<u32> = labels
99 .iter()
100 .filter_map(|l| self.interner.get_id(l))
101 .collect();
102 // The effect guard, one early exit for every no-op shape: an
103 // empty request, one whose names are all uninterned, one whose
104 // names are interned elsewhere but not carried by this node, or
105 // a missing node — none of it intersects the live label set the
106 // COW pass would walk (`live_label_ids`, the xmax == 0 version —
107 // the in-write set included), so nothing is removed. The
108 // request's resolution alone is not the effect: without this
109 // guard, `REMOVE n:Foo` with Foo interned elsewhere passed it
110 // and paid a full version push (an identical clone per `cow`)
111 // for a no-op — a content-free version, invisible to reads at
112 // any ts and absent from the checkpoint payload (which
113 // serializes the visible version), but charged to `used_bytes`
114 // all the same.
115 let live = self.live_label_ids(id);
116 if !drop.iter().any(|l| live.contains(l)) {
117 return;
118 }
119 let before = self.node(id);
120 let before_shape = self.node_chain_shape(id);
121 // ADR-0439: a removed constraint label frees the node's constrained
122 // tuple; a label no constraint carries cannot change any tuple. The
123 // map's scope is the only source of that answer.
124 // `drop` is non-empty here (the effect guard above returned
125 // otherwise), so one scope read answers for the whole request.
126 let drop_ids: Vec<u32> = drop.iter().copied().collect();
127 let keys = self.tracked_node_edit(id, |scope| scope.touches_node_labels(&drop_ids));
128 let mut n = self
129 .cow_node(id)
130 .expect("a live node has a copy-on-write version");
131 // One pass, the overlay twin's canonical move: extract_if both
132 // collects the actually-removed ids and retains the rest.
133 let actually_removed: Vec<u32> = n.labels.extract_if(.., |l| drop.contains(l)).collect();
134 crate::memory::fit_reserved(&mut n.labels);
135 // The edited live view is in hand: hand it to the map before it
136 // moves into the version chain (the canonical completion arm).
137 self.finish_node_edit(keys, id, Some(&n));
138 self.put_node(id, n);
139 self.bump_stats(|s| s.labels_removed += actually_removed.len() as i64);
140 // The redo/dirty pair is guarded on the actually-mutated ids; the
141 // degree-stats family calls below ride the family's own
142 // empty-set early returns.
143 if !actually_removed.is_empty() {
144 // ADR-0687: the removed label ids are the footprint tokens whose
145 // row counts just moved.
146 self.note_changed_tokens(actually_removed.iter().copied());
147 // The effect, not the request: the WAL row records the names
148 // the node actually lost (the add and overlay twins' policy,
149 // ADR-0309), derived from the effect ids through the
150 // canonical id→name reverse lookup — the ids are node
151 // labels, so the published-before-use invariant holds.
152 let lost: Vec<String> = actually_removed
153 .iter()
154 .map(|&lid| String::from(&*self.interner.get(lid)))
155 .collect();
156 self.push_redo(RedoOp::remove_labels(id, lost));
157 self.mark_node_dirty(id);
158 // ADR-0595: the dropped labels' postings become reclamation
159 // candidates here, at the removal site — the apply's tail
160 // drain decides their fate, not a probe's walk.
161 self.suspect_postings(id, &actually_removed);
162 }
163 self.index_add(id);
164 self.charge_node_after_edit(id, before.as_ref(), before_shape);
165 // The one count-family entry, in the removal direction (the ids are
166 // the labels ACTUALLY removed, never the requested list).
167 self.apply_label_delta_stats(id, &live, &actually_removed, CountDelta::Remove);
168 }
169}
170
171#[cfg(test)]
172mod tests;