Skip to content

Cluster migration optimization umbrella (#291) - #292

Merged
nockawa merged 2 commits into
mainfrom
feature/291-cluster-migration-optimization-umbrella
Apr 30, 2026
Merged

nockawa merged 2 commits into
mainfrom
feature/291-cluster-migration-optimization-umbrella

Conversation

@nockawa

@nockawa nockawa commented Apr 30, 2026

Copy link
Copy Markdown
Collaborator

Closes #291.

Summary

What started as a perf hunt on DatabaseEngine.ExecuteMigrations (28 ms / tick on TickDriver during cluster cell-crossing storms) surfaced an architectural bug in tick-fence ChangeSet ownership, a profiler observability gap that hid it, and a missing DI registration in AntHill that silently downgraded it to WAL-less mode. All fixed; the migration loop also got a focused micro-optimization. AntHill storm tick: 28 ms → 2.96 ms (≈9.5×).

See #291 for the full story (trigger, what was delivered, why this is umbrella-shaped, follow-ups out of scope).

What's in this PR

A. Tick-fence ChangeSet ownership architectural correction. Four tick-fence methods (ExecuteMigrations, ProcessClusterShadowEntries, ProcessShadowEntries, ProcessSpatialEntries) plus UowRegistry.RecordCommit were creating private ChangeSets and calling SaveChanges themselves, bypassing the UoW lifecycle. Threaded _currentUow.ChangeSet from TyphonRuntime.OnTickEndInternal through WriteTickFence → WriteClusterTickFence → all four callees, and through RecordCommit → WriteEntryFields(externalCs). Public API preserved via optional parameter (test/admin callers without a UoW get a one-shot local CS owned by WriteTickFence).

B. Phases as real spans + click-to-detail. New typed kind RuntimePhaseSpan = 243. InspectorPhase opens a real span instead of emitting Start/End instants, so child spans (PageCacheFlush, ClusterMigration, …) attach via parentSpanId. Phase span lands on both the tick-wide phase track AND the TickDriver swimlane. Phase-track marker glyphs for UoW Create / UoW Flush (palette green for both themes). New ProfilerSelection variants 'phase' + 'phase-marker' with dedicated DetailPane sections.

C. Suppression deny-list re-tier. The TyphonEvent static-ctor deny-list was over-tiered — bundled diagnostic-grade kinds (PageCacheFlush, DiskRead/Write, FlushCompleted, UoWState, etc.) with the truly extreme-frequency ones (PageCacheFetch). Result: 12 kinds were unreachable from JSON config alone. After the re-tier the deny-list contains only kinds that fire ≥ 10⁵/sec on realistic workloads. Diagnostic-grade kinds are now gated solely by their JSON category like everything else.

D. DetailPane UX. Relative timestamps (subtract globalStartUs to match the ruler) via shared useGlobalStartUs() hook. select-text opt-in for value cells (the body-level user-select: none app-feel default is preserved everywhere else). Kind-specific fields surfaced for PageCacheFlush (page count) and disk-IO spans (file page index, page count) — the page-count fingerprint was the smoking gun that proved the 1050-page flush was the migration's full dirty page set.

E.1. AntHill WAL registration fix (the big one). DatabaseEngine.InitializeWalManager requires both WalWriterOptions AND IWalFileIO registered in DI. AntHill set the options but didn't register IWalFileIO, so the manager silently stayed null and AntHill ran WAL-less Deferred — every UoW.Flush wrote 1050 dirty migration pages synchronously on TickDriver. Single line: .AddSingleton<IWalFileIO, WalFileIO>(). Migration storm: 28 ms → 3.18 ms.

E.2. EntityMap TryUpdateInPlace primitive. New public primitive on RawValuePagedHashMap:

public bool TryUpdateInPlace<TUpdater>(TKey key, ref TUpdater updater,
    ref ChunkAccessor<TStore> accessor, ChangeSet changeSet)
    where TUpdater : struct, IRawValueUpdater

Single chain scan + single OLC write critical section instead of TryGet + Upsert's two scans + two OLC ops + full-record memcpy. Migration step 9 uses a tiny ClusterLocationUpdater to write the 5 bytes that change. Storm: 3.18 → 2.96 ms. The primitive is generic and reusable.

Net effect on AntHill storm tick

Metric Before After
Migration storm total ~28 ms 2.96 ms
UoW.Flush phase on TickDriver 22 ms (sync SaveChanges) µs-range
WAL/Checkpoint visibility absent spans on dedicated lanes

Test plan

  • dotnet test — Engine: 3638/3671 pass (one pre-existing flaky concurrent BTree test, unrelated)
  • npx vitest run — Workbench: 246/246 pass; tsc --noEmit clean
  • AntHill builds clean and profiles cleanly with WAL/Checkpoint spans visible on dedicated lanes
  • AntHill storm tick measured at 2.96 ms (down from 28 ms baseline) with Cluster migration storm warn-level log line confirming throughput

🤖 Generated with Claude Code

nockawa and others added 2 commits April 30, 2026 17:10
The committed `schema/openapi.json` predated the v9 (`overloadLevel`,
`tickMultiplier`, `metronomeWaitUs`, `metronomeIntentClass`) and v11
(`consecutiveOverrun`, `consecutiveUnderrun`) field additions to the C#
`TickSummaryDto`. CI's TypeScript build failed with 11 errors because Orval
generated `tickSummaryDto.ts` from the stale schema — the consumer code in
`dtoConverters.ts` and `OverloadStrip.tsx` referenced fields that didn't
exist on the generated DTO. Locally the regenerated TS types (gitignored)
had the fields because someone had run `npm run generate-types` against a
fresh schema at some point — but the source of truth was never committed.

Refreshed `schema/openapi.json` from the live Kestrel `:5200/openapi.json`
and adjusted four consumers for the freshly generated `number | string`
field types (the standard .NET OpenAPI dual representation that all integer
fields now use):

* `dtoConverters.ts` — wrap the 6 new fields in `toInt(...)` for parity
  with `tickNumber`, `eventCount`, etc.
* `tickOverview.ts` — `TickSummaryLike` widened to accept `number | string`
  for the 6 v9/v11 fields; `buildTickRows` coerces via `Number(...)` when
  assigning to the `TickRow` consumer shape.
* `OverloadStrip.tsx` — wrap the 6 new field reads in `Number(...)`.
* `useProfilerSessionStore.test.ts` — `tick()` factory provides the 6 now-
  required fields (default 0).

`npx tsc --noEmit` clean; `npx vitest run` 246/246.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@nockawa
nockawa merged commit 35e43cf into main Apr 30, 2026
1 check passed
@nockawa
nockawa deleted the feature/291-cluster-migration-optimization-umbrella branch April 30, 2026 15:27
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.

Cluster migration optimization umbrella

1 participant