-
Notifications
You must be signed in to change notification settings - Fork 2
Add autosplit committed-window reader #1152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bootjp
wants to merge
2
commits into
main
Choose a base branch
from
feature/hotspot-m3-observe-reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package autosplit | ||
|
|
||
| import ( | ||
| "sort" | ||
| "time" | ||
|
|
||
| "github.com/bootjp/elastickv/distribution" | ||
| "github.com/bootjp/elastickv/keyviz" | ||
| ) | ||
|
|
||
| // SnapshotSource is the narrow keyviz snapshot surface the observe-only | ||
| // autosplit reader needs. | ||
| type SnapshotSource interface { | ||
| Snapshot(from, to time.Time) []keyviz.MatrixColumn | ||
| } | ||
|
|
||
| // SnapshotReadConfig controls one off-path autosplit sampler read. | ||
| type SnapshotReadConfig struct { | ||
| Step time.Duration | ||
| CandidateWindows int | ||
| LastProcessedAt time.Time | ||
| Now time.Time | ||
| } | ||
|
|
||
| // SnapshotReadResult is the committed keyviz material consumed by the detector. | ||
| type SnapshotReadResult struct { | ||
| Windows []ColumnWindow | ||
| NewestCommittedAt time.Time | ||
| SnapshotFrom time.Time | ||
| SnapshotTo time.Time | ||
| SkippedInvalid int | ||
| } | ||
|
|
||
| // ObserveSnapshot reads committed keyviz windows and runs the pure detector. | ||
| // Callers may log Result.Decisions in observe-only mode; this helper never | ||
| // mutates the route catalog and never calls SplitRange. | ||
| func ObserveSnapshot( | ||
| cfg Config, | ||
| state *DetectorState, | ||
| routes []distribution.RouteDescriptor, | ||
| source SnapshotSource, | ||
| readCfg SnapshotReadConfig, | ||
| ) (Result, SnapshotReadResult) { | ||
| read := ReadCommittedWindows(source, readCfg) | ||
| result := Evaluate(cfg, state, Input{ | ||
| Routes: routes, | ||
| Windows: read.Windows, | ||
| Now: read.SnapshotTo, | ||
| }) | ||
| return result, read | ||
| } | ||
|
|
||
| // ReadCommittedWindows converts a time-range keyviz snapshot into detector | ||
| // windows, excluding columns that have already been processed and columns whose | ||
| // committed lower boundary is not proven. | ||
| func ReadCommittedWindows(source SnapshotSource, cfg SnapshotReadConfig) SnapshotReadResult { | ||
| if source == nil { | ||
| return SnapshotReadResult{} | ||
| } | ||
| step := cfg.Step | ||
| if step <= 0 { | ||
| step = keyviz.DefaultStep | ||
| } | ||
| candidateWindows := cfg.CandidateWindows | ||
| if candidateWindows <= 0 { | ||
| candidateWindows = defaultCandidateWindows | ||
| } | ||
| now := cfg.Now | ||
| if now.IsZero() { | ||
| now = time.Now() | ||
| } | ||
|
|
||
| from := now.Add(-time.Duration(candidateWindows+1) * step) | ||
| if !cfg.LastProcessedAt.IsZero() { | ||
| from = cfg.LastProcessedAt.Add(-step) | ||
| } | ||
| cols := source.Snapshot(from, now) | ||
| windows, newest, skipped := CommittedWindowsFromColumns(cols, cfg.LastProcessedAt) | ||
| return SnapshotReadResult{ | ||
| Windows: windows, | ||
| NewestCommittedAt: newest, | ||
| SnapshotFrom: from, | ||
| SnapshotTo: now, | ||
| SkippedInvalid: skipped, | ||
| } | ||
| } | ||
|
|
||
| // CommittedWindowsFromColumns normalizes raw keyviz columns into detector | ||
| // windows. WindowStart is authoritative when present. For older in-memory | ||
| // columns without WindowStart, the immediately previous column boundary is the | ||
| // only accepted fallback. Columns whose lower boundary is not proven are | ||
| // returned as zero-duration reset sentinels so the detector clears stale | ||
| // confidence instead of carrying it across an unknown interval. | ||
| func CommittedWindowsFromColumns(cols []keyviz.MatrixColumn, lastProcessedAt time.Time) ([]ColumnWindow, time.Time, int) { | ||
| ordered := append([]keyviz.MatrixColumn(nil), cols...) | ||
| sort.SliceStable(ordered, func(i, j int) bool { | ||
| return ordered[i].At.Before(ordered[j].At) | ||
| }) | ||
|
|
||
| var newest time.Time | ||
| windows := make([]ColumnWindow, 0, len(ordered)) | ||
| skipped := 0 | ||
| lastBoundary := lastProcessedAt | ||
| for i, col := range ordered { | ||
| if col.At.After(newest) { | ||
| newest = col.At | ||
| } | ||
| if !col.At.After(lastProcessedAt) { | ||
| continue | ||
| } | ||
| start := committedWindowStart(ordered, i) | ||
| if start.IsZero() || !start.Before(col.At) { | ||
| skipped++ | ||
| windows = append(windows, ColumnWindow{Column: keyviz.MatrixColumn{At: col.At}}) | ||
| lastBoundary = col.At | ||
| continue | ||
| } | ||
| if needsBoundaryReset(lastBoundary, start) { | ||
| windows = append(windows, ColumnWindow{Column: keyviz.MatrixColumn{At: col.At}}) | ||
| lastBoundary = col.At | ||
| continue | ||
|
bootjp marked this conversation as resolved.
|
||
| } | ||
| windows = append(windows, ColumnWindow{ | ||
| Column: col, | ||
| Duration: col.At.Sub(start), | ||
| }) | ||
| lastBoundary = col.At | ||
| } | ||
| return windows, newest, skipped | ||
| } | ||
|
|
||
| func committedWindowStart(cols []keyviz.MatrixColumn, i int) time.Time { | ||
| start := cols[i].WindowStart | ||
| if start.IsZero() && i > 0 && cols[i-1].At.Before(cols[i].At) { | ||
| start = cols[i-1].At | ||
| } | ||
| return start | ||
| } | ||
|
|
||
| func needsBoundaryReset(lastBoundary, start time.Time) bool { | ||
| return !lastBoundary.IsZero() && !start.Equal(lastBoundary) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.