Restore write_unit=txn_fragment for shape consumers#3906
Draft
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3906 +/- ##
=======================================
Coverage 87.20% 87.20%
=======================================
Files 25 25
Lines 2391 2391
Branches 600 599 -1
=======================================
Hits 2085 2085
Misses 304 304
Partials 2 2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
|
Found 1 test failure on Blacksmith runners: Failure
|
c5100b8 to
4348ebe
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
PR #3783 introduced the infrastructure for streaming transaction fragments directly to storage (
write_unit=txn_fragment) instead of buffering entire transactions in consumer memory. This dramatically reduces memory usage for large transactions (9GB → 500MB in benchmarks).However, correctness issues emerged with subquery shapes, and the final version of #3783 sets
write_unit=txnfor all shapes to ship a safe baseline. All the fragment-streaming code paths remain in the codebase but are currently unreachable.This PR tracks re-enabling
write_unit=txn_fragment, starting with the simpler case (standalone shapes) and eventually covering all shapes.Phase 1: Restore
txn_fragmentfor standalone shapes (no subquery dependencies)Standalone shapes have no materializer subscribers and no shape dependencies. The fragment-streaming code path was already working for these shapes before it was disabled.
State.initialize_shape/3, setwrite_unit=txn_fragmentfor shapes whereshape_dependencies == []andis_subquery_shape? == falsemainPhase 2: Restore
txn_fragmentfor inner (dependency) subquery shapesFor inner shapes each consumer process has a materializer process subscribed to it. Outer shape's consumer is in turn subscribed to the inner shape's materializer to correctly handle move-ins and move-outs. Fragment streaming for these shapes requires the materializer to correctly defer event processing until all changes for the current transaction have been processed.
subscribe_materializer, (AI hallucations:return the last committed offset from storage ()Storage.fetch_latest_offset) instead ofstate.latest_offset, which can be a mid-transaction fragment offset ahead of the committed boundarylib/electric/shapes/consumer.ex,handle_call({:subscribe_materializer, ...})write_unit=txn_fragmentfor shapes withis_subquery_shape? == true(and noshape_dependenciesof their own)commit: false/commit: truedeferred notification path is exercised end-to-endwrite_unit=txn_fragmentand a materializer subscriber receives a multi-fragment transaction; the materializer'spending_eventsaccumulate across fragments and only flush on commitPhase 3: Restore
txn_fragmentfor outer (parent) subquery shapesOuter shapes have
shape_dependencies != []and process materializer events (move-ins/move-outs) as part of their transaction handling. This is the hardest case.write_txn_fragment_to_storagefor move-in/move-out correctnesswrite_unit=txnAdditional items
write_unit=txn_fragmentis later adopted as an inner shape for a newly created outer subquery shapeReferences