Commit e7f3bad
committed
feat(keyviz): sampler GroupID + LeaderTerm stamping (Phase 2-C+ PR-3a)
Add the sampler-side API for the Phase 2-C+ cluster fan-out merge
described in `docs/design/2026_04_27_proposed_keyviz_cluster_fanout.md`
§9.1: every emitted MatrixRow now carries the route's Raft identity
(RaftGroupID + LeaderTerm) so the fan-out aggregator can dedupe write
samples by `(routeID, raftGroupID, leaderTerm, columnAt)` instead of
the conservative max-merge that may undercount during a leadership
flip.
Sampler changes:
- `routeSlot.GroupID` field — stamped at RegisterRoute time, copied
into MatrixRow at Flush time.
- `MatrixRow.RaftGroupID + LeaderTerm` fields — both zero means
"term not tracked", which the fan-out merge interprets as the
legacy max-merge fallback for cells whose LeaderTerm is 0.
- `MemSampler.SetLeaderTerm(groupID, term uint64)` — publishes the
current leader term for a group; called by main.go on a periodic
ticker (wiring lands in a follow-up PR; this PR ships the API
with no callers).
- `MemSampler.snapshotGroupTerms()` — internal helper that copies
the per-group term map at the top of Flush so every row in a
column observes a stable view, even if SetLeaderTerm fires
concurrently.
- `RegisterRoute(routeID, start, end []byte, groupID uint64)` —
signature extended with groupID; all call sites updated
(`main.go` `seedKeyVizRoutes` reads `r.GroupID` from
`distribution.Engine.Stats()`; legacy single-group / synthetic
virtual-bucket call sites pass 0).
Behavior:
- With `SetLeaderTerm` never called, every row emits LeaderTerm=0
→ fan-out merge falls back to today's max-merge → no behavior
change for legacy deployments.
- With `SetLeaderTerm` called per group, rows carry the term →
fan-out merge can apply the §9.1 (group, term)-keyed dedupe.
Not in this PR (deferred to follow-ups):
- The periodic ticker in main.go that polls the engine Status and
calls SetLeaderTerm.
- Proto + JSON wire-format additions for RaftGroupID + LeaderTerm
(currently MatrixRow-internal only).
- The fan-out aggregator side of the (group, term)-keyed dedupe.
Tests:
- All keyviz tests pass with `-race -count=1`.
- `TestSeedKeyVizRoutesNoOpOnNilSampler` and
`TestStartKeyVizFlusherReturnsAfterCancel` updated to the new
RegisterRoute signature.
- New tests in `keyviz/sampler_test.go` cover SetLeaderTerm
publishing, Flush snapshot consistency, and the
unknown-term/known-term row stamping paths.
Five-lens self-review:
1. Data loss — no on-disk format change; legacy max-merge fallback
preserves today's behavior when SetLeaderTerm is never called.
2. Concurrency — `groupTermsMu` is a fine-grained RWMutex; Observe
never touches it; `snapshotGroupTerms` clones at Flush start so
the column sees a stable view.
3. Performance — Observe hot path is unchanged (no new map lookup,
no new lock); Flush gains one RLock + map clone per column,
bounded by `len(groupTerms)` (typically ≤ a few groups).
4. Data consistency — `(routeID, raftGroupID, leaderTerm)` is a
strict superset of today's `routeID` dedupe key; rows with
LeaderTerm=0 collapse to the legacy max-merge.
5. Test coverage — new SetLeaderTerm + snapshot tests under
`keyviz/sampler_test.go`; existing RegisterRoute test
signatures updated; race detector clean.1 parent e6004f3 commit e7f3bad
5 files changed
Lines changed: 203 additions & 59 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
160 | 160 | | |
161 | 161 | | |
162 | 162 | | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
163 | 170 | | |
164 | 171 | | |
165 | 172 | | |
| |||
237 | 244 | | |
238 | 245 | | |
239 | 246 | | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
240 | 255 | | |
241 | 256 | | |
242 | 257 | | |
| |||
266 | 281 | | |
267 | 282 | | |
268 | 283 | | |
269 | | - | |
| 284 | + | |
270 | 285 | | |
271 | 286 | | |
272 | 287 | | |
| |||
276 | 291 | | |
277 | 292 | | |
278 | 293 | | |
| 294 | + | |
279 | 295 | | |
280 | 296 | | |
281 | 297 | | |
| |||
299 | 315 | | |
300 | 316 | | |
301 | 317 | | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
302 | 328 | | |
303 | 329 | | |
304 | 330 | | |
| |||
331 | 357 | | |
332 | 358 | | |
333 | 359 | | |
334 | | - | |
335 | | - | |
336 | | - | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
337 | 364 | | |
338 | 365 | | |
339 | 366 | | |
340 | 367 | | |
341 | 368 | | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
342 | 403 | | |
343 | 404 | | |
344 | 405 | | |
| |||
385 | 446 | | |
386 | 447 | | |
387 | 448 | | |
388 | | - | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
389 | 456 | | |
390 | 457 | | |
391 | | - | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
392 | 461 | | |
393 | 462 | | |
394 | 463 | | |
| |||
397 | 466 | | |
398 | 467 | | |
399 | 468 | | |
400 | | - | |
| 469 | + | |
401 | 470 | | |
402 | 471 | | |
403 | 472 | | |
| |||
418 | 487 | | |
419 | 488 | | |
420 | 489 | | |
| 490 | + | |
421 | 491 | | |
422 | 492 | | |
423 | 493 | | |
| |||
431 | 501 | | |
432 | 502 | | |
433 | 503 | | |
| 504 | + | |
434 | 505 | | |
435 | 506 | | |
436 | 507 | | |
| |||
601 | 672 | | |
602 | 673 | | |
603 | 674 | | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
604 | 682 | | |
605 | 683 | | |
606 | | - | |
| 684 | + | |
607 | 685 | | |
608 | 686 | | |
609 | 687 | | |
610 | 688 | | |
611 | | - | |
| 689 | + | |
612 | 690 | | |
613 | 691 | | |
614 | 692 | | |
| |||
628 | 706 | | |
629 | 707 | | |
630 | 708 | | |
631 | | - | |
| 709 | + | |
632 | 710 | | |
633 | 711 | | |
634 | | - | |
| 712 | + | |
635 | 713 | | |
636 | 714 | | |
637 | 715 | | |
| |||
825 | 903 | | |
826 | 904 | | |
827 | 905 | | |
828 | | - | |
| 906 | + | |
829 | 907 | | |
830 | 908 | | |
831 | 909 | | |
832 | 910 | | |
833 | 911 | | |
834 | 912 | | |
835 | 913 | | |
836 | | - | |
| 914 | + | |
837 | 915 | | |
838 | 916 | | |
| 917 | + | |
| 918 | + | |
839 | 919 | | |
840 | 920 | | |
841 | 921 | | |
| |||
0 commit comments