#4685 PR 1 — ProjectionUpdateBatch insert-only flush-mode plumbing#4697
Merged
Conversation
First PR in the BulkWriter (binary COPY) rebuild optimization series tracked in #4685. Foundation only -- adds the seam where subsequent PRs plug in: * PR 2 will TRUNCATE the projection table at rebuild start, so every batch that follows is naturally INSERT-only. * PR 3 will dispatch the actual BeginBinaryImport flush path when the batch reports BatchFlushMode.InsertOnly. * Continuous catch-up keeps the existing per-row UPSERT path -- it mixes inserts, updates, upserts, patches, and the classifier reports Mixed for those batches. **No behavior change in this PR.** The flush path is unchanged; only a new public read-only property exposes how the batch classifies itself. ## What ships * `Marten.Events.Daemon.Internals.BatchFlushMode` -- enum with `Mixed` (the default; current flush path applies) and `InsertOnly` (eligible for the BulkWriter path in PR 3). * `BatchFlushModeClassifier` -- pure, unit-testable transition rule. Initial state is `InsertOnly` (empty batch is trivially insert-only; the BulkWriter path is a no-op on zero rows); `WithOperation(current, role)` demotes to `Mixed` on any non-Insert and is monotonic -- once Mixed, stays Mixed for the lifetime of the batch. * `ProjectionUpdateBatch.FlushMode` -- public read-only property maintained incrementally in `applyOperation`. Subsequent PRs in the series dispatch on this signal. ## Tests `CoreTests/Events/Daemon/BatchFlushModeClassifier_pin.cs` (10 cases): * Initial state is InsertOnly * Inserts keep InsertOnly across multiple arrivals * Each of the five non-Insert roles (Update, Upsert, Patch, Deletion, Events, Other) demotes to Mixed independently * Monotonic: once Mixed, subsequent Inserts can't bring it back * Interleaved sequence: insert -> insert (still InsertOnly) -> update (Mixed) -> insert (still Mixed) DaemonTests pass 188/188 on net9.0 -- the projection rebuild + continuous catch-up paths exercise applyOperation across every operation role on every projection lifecycle, and the new property maintenance does not change any existing assertion. ## Series sketch * PR 1 (this PR): classifier + property. * PR 2: TRUNCATE step at the start of a per-(tenant, projection) rebuild. Partition-aware under UseTenantPartitionedEvents. * PR 3: BulkWriter (binary COPY) flush path that activates when FlushMode == InsertOnly. Reuses BulkInsertAsync's column binders. * PR 4: Composite-stage propagation review -- verify the Upstream cache propagation handles stage 2 reading stage 1's un-flushed COPY data. * PR 5: ScaleTesting harness validation. Phase E (#4684) measurement confirms the ≥30% wall-clock reduction target on the write-side of a 20M-event Telehealth composite rebuild. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This was referenced Jun 10, 2026
Merged
This was referenced Jun 17, 2026
This was referenced Jun 19, 2026
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.
First PR in the BulkWriter (binary
COPY) rebuild optimization series tracked in #4685. Foundation only — no behavior change. Adds the seam where subsequent PRs plug in.Series sketch
BatchFlushModeproperty onProjectionUpdateBatch. No behavior change.TRUNCATEstep at the start of a per-(tenant, projection) rebuild. Partition-aware underUseTenantPartitionedEvents.COPY) flush path that activates whenFlushMode == InsertOnly. ReusesBulkInsertAsync's column binders.Upstreamcache propagation handles stage 2 reading stage 1's un-flushed COPY data.What ships in this PR
Marten.Events.Daemon.Internals.BatchFlushModeMixed(default; current flush path applies) andInsertOnly(eligible for the BulkWriter path in PR 3).BatchFlushModeClassifierInitial = InsertOnly(empty batch is trivially insert-only -- BulkWriter is a no-op on zero rows);WithOperation(current, role)demotes toMixedon any non-Insert role and is monotonic -- onceMixed, staysMixedfor the lifetime of the batch.ProjectionUpdateBatch.FlushModeapplyOperation. PR 3 dispatches on this signal.Tests
10 cases in
CoreTests/Events/Daemon/BatchFlushModeClassifier_pin.cs:InsertOnlyInsertkeepsInsertOnlyacross multiple arrivalsUpdate,Upsert,Patch,Deletion,Events,Other) independently demotes toMixedMixed, subsequentInserts can't bring it backInsert→Insert(stillInsertOnly) →Update(Mixed) →Insert(stillMixed)DaemonTests(rebuild + continuous catch-up exerciseapplyOperationon every role)The new property maintenance is one increment per operation arrival -- the perf footprint on the hot path is unmeasurable. The plumbing for PR 3 has somewhere to plug in without any of this PR changing observable Marten behavior.
🤖 Generated with Claude Code