Skip to content

Let writes in while scans run on a live graph - #2969

Draft
miratepuffin wants to merge 3 commits into
db_v4from
perf/scan-one-segment-at-a-time
Draft

miratepuffin wants to merge 3 commits into
db_v4from
perf/scan-one-segment-at-a-time

Conversation

@miratepuffin

Copy link
Copy Markdown
Collaborator

What changes were proposed in this pull request?

Scans over a live graph no longer hold every segment for the whole scan, so writes can get in while scans run.

  • Scans take one segment at a time. Nodes::iter/par_iter/len, edges(), the LazyNodeState paths and PathFromNode::map used core_graph().lock(), which read-locks every node and edge segment for the iterator's life. On a live graph they now iterate the unlocked storage, which locks each segment as the scan reaches it (the existing *_unlocked iterators already did this). A read-only graph is already GraphStorage::Locked, so it behaves as before. Exporters that need a consistent copy keep lock(): parquet encode, materialize, the cached view.
  • A scan entering a segment lets a waiting writer go first. head_arc()/locked() wait up to 1 ms while a writer is queued on the segment, then take the usual recursive read. The wait is bounded, so a thread that already reads the segment can't deadlock on it.
  • Writers wait instead of spinning. Node segment heads are written with a blocking write(), like edge heads. add_edge between two existing nodes in different segments waits on one segment and only tries the other, swapping on failure. Before, it spun on two try_writer calls with no back-off.
  • The in-memory node segment's locked() uses head_arc() instead of a fair read_arc().

Why are the changes needed?

While any scan ran, every write waited for it to finish. With scans overlapping, writes didn't get in at all: a reader always held each segment, and read_recursive lets the next scan in past a queued writer. With one scan loop running, each write waited about one full scan; with four overlapping loops, no write completed until the scans stopped.

Readers re-take segments they already hold, on the same thread and through rayon, so switching to fair reads would deadlock. Holding one segment at a time removes the overlap instead.

Does this PR introduce any user-facing change? If yes is this documented?

Yes. A scan of a live graph no longer sees one snapshot: it sees writes that land in segments it hasn't reached yet, and not those in segments it has already passed. Read-only graphs (read_only(), served read-only graphs) and exporters still read a consistent snapshot. Writes to a graph that is being scanned now make progress instead of waiting for every scan to end.

How was this patch tested?

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

Still to come in this draft:

  • Regression test. Four back-to-back scan loops, and one writer that must finish 100 add_edge calls within 10 s. I'll add it once it's been checked to fail on db_v4 and pass here.
  • Benchmarks. The CI benchmark suites (base, algobench_fast, algobench_medium, algobench_slow) on db_v4 and on this branch, interleaved and repeated, on live graphs and on the same graphs as read_only() handles. Any benchmark more than 5% slower will be flagged here.

Are there any further changes required?

Parallel aggregates over a live graph now take a read guard per node op instead of reading under one whole-graph lock. If the benchmarks show that costs more than 5%, the parallel paths should lock one segment per rayon task instead.

Security implications if any

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

Scans used core_graph().lock(), which read-locks every node and edge segment for
the whole scan, so writes waited for every overlapping scan to end. On a live
graph, scans now take one segment at a time; read-only graphs are already locked
and exporters keep lock() for a consistent copy. A scan entering a segment gives
a waiting writer up to 1 ms first. Node writers block instead of retrying on a
timer, and add_edge across two node segments waits on one and tries the other
instead of spinning on both.

@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: 3c89629 Previous: d4702e6 Ratio
graphgen_max_out_degree/graphgen_max_out_degree 361965 ns/iter (± 1551) 75323 ns/iter (± 443) 4.81
graphgen_max_in_degree/graphgen_max_in_degree 366335 ns/iter (± 5000) 78791 ns/iter (± 367) 4.65
graphgen_min_out_degree/graphgen_min_out_degree 365941 ns/iter (± 3879) 75813 ns/iter (± 322) 4.83
graphgen_min_in_degree/graphgen_min_in_degree 365926 ns/iter (± 3479) 78947 ns/iter (± 639) 4.64

This comment was automatically generated by workflow using github-action-benchmark.

@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 'RDF Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 5e6a657 Previous: d4702e6 Ratio
bsbm5000_sparqloscope/000/raphtory 20847167 ns/iter (± 3248912) 6499060 ns/iter (± 211072) 3.21
bsbm5000_sparqloscope/001/raphtory 20598202 ns/iter (± 646941) 6511938 ns/iter (± 120273) 3.16
bsbm5000_sparqloscope/008/raphtory 15064075 ns/iter (± 409023) 1423347 ns/iter (± 5555) 10.58
bsbm5000_sparqloscope/009/raphtory 15360728 ns/iter (± 891377) 1414191 ns/iter (± 5627) 10.86
bsbm5000_sparqloscope/010/raphtory 21578909 ns/iter (± 3573139) 6866210 ns/iter (± 867676) 3.14
bsbm5000_sparqloscope/030/raphtory 18416988 ns/iter (± 155093) 3263617 ns/iter (± 33186) 5.64
bsbm5000_sparqloscope/045/raphtory 16314695 ns/iter (± 514465) 3132715 ns/iter (± 17987) 5.21
bsbm5000_sparqloscope/063/raphtory 16234798 ns/iter (± 156319) 2037446 ns/iter (± 52108) 7.97
mat_p/instant/sparql_snapshot@10523 95434768 ns/iter (± 2699957) 15752592 ns/iter (± 73439) 6.06
ver_p/instant/sparql_values_5_versions 474407230 ns/iter (± 5394104) 76149039 ns/iter (± 584816) 6.23

This comment was automatically generated by workflow using github-action-benchmark.

A read guard per node made parallel aggregates on a live graph contend on
the segment lock. Take one graph lock per chunk of 16k nodes instead.

This branch has not been deployed

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

1 participant