Skip to content

Stop add_edge waiting for an edge segment while holding node segments - #2967

Closed
miratepuffin wants to merge 1 commit into
db_v4from
fix/filtered-walk-edge-node-deadlock
Closed

miratepuffin wants to merge 1 commit into
db_v4from
fix/filtered-walk-edge-node-deadlock

Conversation

@miratepuffin

Copy link
Copy Markdown
Collaborator

What changes were proposed in this pull request?

add_edge no longer waits for an edge segment while it holds node segment writers.

  • atomic_add_edge takes the edge writer with try_get_writer / try_get_free_writer.
  • If the edge segment is busy, it drops its node writers, waits for the edge segment holding nothing, and starts again with the node ids it already resolved. Nodes it created on the first attempt are still reported as new.
  • To support this, EdgeSegmentOps gains try_head_mut_arc (the node segment trait already has it), and EdgeStorageInner gains try_get_writer and try_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_iter when node and edge filters are both active (the core_edge temporary is still alive while core_node(remote) runs), node_has_valid_edges, and anything that reads an edge's endpoints while iterating edges.

add_edge took 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 an add_edge holding B and waiting for E. read_recursive doesn'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_edge on 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 take degree() 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.

  • On db_v4 it hangs in 5 of 5 runs. A stack sample shows the writer parked on the edge segment in atomic_add_edge and the readers parked on node segments.
  • With this change it passes in 10 of 10 runs.

The db4-storage, raphtory-storage and raphtory-tests suites pass (release build).

Are there any further changes required?

Any other EdgeSegmentOps implementation needs try_head_mut_arc, a non-blocking try_write_arc on the segment head.

Security implications if any

  • I have considered the security impact of this change (secrets, dependencies, access, data handling)

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.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ 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 ljeub-pometry left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be returning an option then!

Comment on lines +689 to +690
Some(edge_id) => drop(edges.get_writer(edge_id)),
None => drop(edges.get_free_writer()),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here is the stack overflow :)

@ljeub-pometry

Copy link
Copy Markdown
Collaborator

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants