Repository navigation
Stop add_edge waiting for an edge segment while holding node segments - #2967
miratepuffin wants to merge 1 commit into
Conversation
add_edge held its node segments while it blocked on the edge segment. A reader that holds that edge segment and then reads one of those nodes (a filtered neighbour walk, or reading an edge's endpoints) deadlocked with it. add_edge now only tries the edge segment; when it is busy it drops the node writers, waits for the edge segment holding nothing, and starts again.
There was a problem hiding this comment.
⚠️ Performance Alert ⚠️
Possible performance regression was detected for benchmark 'Rust Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.
| Benchmark suite | Current: 3104400 | Previous: d4702e6 | Ratio |
|---|---|---|---|
lotr_graph/num_edges_temporal |
314 ns/iter (± 2) |
138 ns/iter (± 2) |
2.28 |
lotr_graph/num_edges_temporal_cached |
312 ns/iter (± 1) |
136 ns/iter (± 5) |
2.29 |
lotr_graph_window_100/num_edges_temporal |
321 ns/iter (± 1) |
160 ns/iter (± 31) |
2.01 |
lotr_graph_subgraph_10pc/num_nodes |
14 ns/iter (± 0) |
5 ns/iter (± 0) |
2.80 |
This comment was automatically generated by workflow using github-action-benchmark.
ljeub-pometry
left a comment
There was a problem hiding this comment.
This does not work in the slightest and infinitely recurses in the contested scenario! The real fix for this problem would be on the read side which is being naughty with the lock ordering by grabbing a node lock while holding an edge lock! We need to add an unlocked method for the edge entry to avoid this.
| ) -> EdgeWriter<'a, ArcRwLockWriteGuard<MemEdgeSegment>, ES> { | ||
| let mut slot = self.free_segments.slot().write(); | ||
| self.free_writer(true) | ||
| .expect("a blocking free writer always succeeds") |
There was a problem hiding this comment.
This should not be returning an option then!
| Some(edge_id) => drop(edges.get_writer(edge_id)), | ||
| None => drop(edges.get_free_writer()), |
There was a problem hiding this comment.
No, there must be a better way than getting and immediately dropping a lock!
| Some(edge_id) => drop(edges.get_writer(edge_id)), | ||
| None => drop(edges.get_free_writer()), | ||
| } | ||
| let mut add = self.atomic_add_edge( |
There was a problem hiding this comment.
here is the stack overflow :)
|
Actually, having looked at the read side again, fixing it there is hard. However, the try locking here needs to be improved so it actually works without recursing into atomic_add_edge again. We also need to be extremely careful that we do not unlock a segment that we already modified by reserving new rows as that leaks initialised nodes/edges into the graph which is a recipe for race conditions and extremely unexpected behaviour... |
What changes were proposed in this pull request?
add_edgeno longer waits for an edge segment while it holds node segment writers.atomic_add_edgetakes the edge writer withtry_get_writer/try_get_free_writer.EdgeSegmentOpsgainstry_head_mut_arc(the node segment trait already has it), andEdgeStorageInnergainstry_get_writerandtry_get_free_writer. The free writer only tries the free slot as well, since another writer can hold the slot while it waits for the segment.Why are the changes needed?
A reader can hold an edge segment and then read a node. Examples: the filter in
filtered_edges_iterwhen node and edge filters are both active (thecore_edgetemporary is still alive whilecore_node(remote)runs),node_has_valid_edges, and anything that reads an edge's endpoints while iterating edges.add_edgetook its node segments first and then blocked on the edge segment. So a reader holding edge segment E and waiting for node segment B deadlocked with anadd_edgeholding B and waiting for E.read_recursivedoesn't help here, because B is write-held, not just queued.Fixing the writer covers every such reader at once.
Does this PR introduce any user-facing change? If yes is this documented?
No API change.
add_edgeon a busy edge segment now releases its node locks and retries instead of blocking while holding them.How was this patch tested?
New test
graph_tests::test_edge_lock_order::filtered_neighbour_walk_does_not_deadlock_with_add_edge. On small segments, four threads add edges between existing nodes while four threads takedegree()on a view with both a node filter and a window (FilterState::Both). A watchdog fails the test if any thread hasn't finished 10 s after it is told to stop.db_v4it hangs in 5 of 5 runs. A stack sample shows the writer parked on the edge segment inatomic_add_edgeand the readers parked on node segments.The
db4-storage,raphtory-storageandraphtory-testssuites pass (release build).Are there any further changes required?
Any other
EdgeSegmentOpsimplementation needstry_head_mut_arc, a non-blockingtry_write_arcon the segment head.Security implications if any