Commit 30f9049
authored
feat(keyviz): in-memory Sampler + ring buffer + RunFlusher (#639)
## Summary
First slice of Phase 2 of the Key Visualizer per
[docs/admin_ui_key_visualizer_design.md
§5](https://github.com/bootjp/elastickv/blob/main/docs/admin_ui_key_visualizer_design.md).
This PR only adds the \`keyviz/\` package; wiring into
\`kv.ShardedCoordinator\`, \`adapter.AdminServer.GetKeyVizMatrix\`, and
\`main.go\` will follow in subsequent PRs to keep diffs reviewable.
### What's in
- \`Sampler\` interface — nil-safe (nil receiver makes every method a
no-op).
- \`MemSampler\` hot path: \`atomic.Pointer[routeTable].Load\` + map
lookup + 2× \`atomic.AddUint64\` (count + bytes). Matches design §10's
per-call budget.
- \`Flush\` drains via \`atomic.SwapUint64\` — no pointer retirement, no
late-writer race past the snapshot.
- COW route table publish (\`RegisterRoute\` / \`RemoveRoute\`) under
\`routesMu\` off the hot path; one \`atomic.Pointer.Store\` on publish.
- Route-budget cap (\`MaxTrackedRoutes\`, default 10000): excess routes
fold into a virtual aggregate slot (\`MatrixRow.Aggregate=true\` +
\`MemberRoutes\`), per design §5.3.
- \`ringBuffer\` with chronological [from, to) Range queries; oldest
entries drop on overflow.
- \`RunFlusher\` convenience for the step-interval ticker.
### What's out (later PRs)
- Coordinator call site (\`sampler.Observe(routeID, op, …)\`).
- \`AdminServer.GetKeyVizMatrix\` RPC implementation.
- \`main.go\` flag wiring + sampler construction.
- Adaptive sub-sampling (§5.2) — MVP samples every op.
- Persistence (§5.6) — Phase 3.
## Test plan
- [x] Unit tests under \`-race\`: nil-safe contract, basic
Observe→Flush, no-counts-lost across flush boundary under writer churn,
route-budget coarsening, snapshot range filtering, ring eviction,
RemoveRoute, unknown-route Observe, concurrent register+observe.
- [x] \`go vet ./...\` and \`golangci-lint\` clean.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* In-memory sampling to track reads/writes with size metrics.
* Periodic flushing of accumulated samples at a configurable interval.
* Route-based tracking with automatic aggregation when route capacity is
exceeded.
* Time-range snapshot retrieval with retention and eviction of old
samples.
* Graceful flusher lifecycle that starts/stops with the application
without losing data.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->5 files changed
Lines changed: 2031 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
0 commit comments