mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-28 16:58:31 +01:00
Build scheduler staleness responses in one allocation (#5328)
Copy only the fields Scheduler Health returns, then calculate every score outside the tracker lock using the captured clock and bounds. Large diagnostic responses no longer allocate an intermediate copy of all internal freshness records. Contract-Neutral: Snapshot allocation only; typed target identity, timestamps, scores, empty responses and polling inputs are unchanged. --- Fleet candidate `20261127T233028Z-core-runtime` from the `core-runtime` lane. Reviewed tip `4f466783d797350602e7b47bd912f7812bf09ba1` on `main`. [How Pulse handles triage](https://github.com/rcourtman/Pulse/blob/main/docs/AI_TRANSPARENCY.md)
This commit is contained in:
@@ -215,23 +215,23 @@ func (t *StalenessTracker) Snapshot() []StalenessSnapshot {
|
||||
}
|
||||
|
||||
t.mu.RLock()
|
||||
entries := make([]FreshnessSnapshot, 0, len(t.entries))
|
||||
result := make([]StalenessSnapshot, 0, len(t.entries))
|
||||
maxStale := t.maxStale
|
||||
now := time.Now()
|
||||
for _, entry := range t.entries {
|
||||
entries = append(entries, entry)
|
||||
}
|
||||
t.mu.RUnlock()
|
||||
|
||||
result := make([]StalenessSnapshot, 0, len(entries))
|
||||
for _, entry := range entries {
|
||||
result = append(result, StalenessSnapshot{
|
||||
Instance: entry.Instance,
|
||||
Type: string(entry.InstanceType),
|
||||
Score: stalenessAt(now, entry.LastSuccess, maxStale),
|
||||
LastSuccess: entry.LastSuccess,
|
||||
LastError: entry.LastError,
|
||||
})
|
||||
}
|
||||
t.mu.RUnlock()
|
||||
|
||||
// Score the copied rows without delaying a poll result waiting to update
|
||||
// the tracker. Every row uses the same clock and bounds as the snapshot.
|
||||
for i := range result {
|
||||
result[i].Score = stalenessAt(now, result[i].LastSuccess, maxStale)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package monitoring
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"testing"
|
||||
"testing/synctest"
|
||||
"time"
|
||||
@@ -317,6 +318,56 @@ func TestStalenessTracker_Snapshot_Empty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalenessTracker_SnapshotTypedPosture(t *testing.T) {
|
||||
synctest.Test(t, func(t *testing.T) {
|
||||
tracker := NewStalenessTracker(nil)
|
||||
tracker.SetBounds(0, time.Minute)
|
||||
now := time.Now()
|
||||
lastSuccess := now.Add(-15 * time.Second)
|
||||
lastError := now.Add(-5 * time.Second)
|
||||
tracker.setSnapshot(FreshnessSnapshot{
|
||||
InstanceType: InstanceTypePVE,
|
||||
Instance: "shared",
|
||||
LastSuccess: lastSuccess,
|
||||
LastError: lastError,
|
||||
LastMutated: now,
|
||||
ChangeHash: "not exposed",
|
||||
})
|
||||
tracker.UpdateError(InstanceTypePBS, "shared")
|
||||
want := map[string]StalenessSnapshot{
|
||||
string(InstanceTypePVE): {Instance: "shared", Type: string(InstanceTypePVE), Score: 0.25, LastSuccess: lastSuccess, LastError: lastError},
|
||||
string(InstanceTypePBS): {Instance: "shared", Type: string(InstanceTypePBS), Score: 1, LastError: now},
|
||||
}
|
||||
rows := tracker.Snapshot()
|
||||
if len(rows) != len(want) {
|
||||
t.Fatalf("snapshot has %d rows, want %d", len(rows), len(want))
|
||||
}
|
||||
for i, row := range rows {
|
||||
if expected, ok := want[row.Type]; !ok || row != expected {
|
||||
t.Errorf("snapshot row = %+v, want %+v", row, expected)
|
||||
}
|
||||
delete(want, row.Type)
|
||||
rows[i] = StalenessSnapshot{}
|
||||
}
|
||||
if got := tracker.Snapshot(); len(got) != 2 || got[0].Instance != "shared" || got[1].Instance != "shared" {
|
||||
t.Fatalf("editing returned rows changed the tracker: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkStalenessTrackerSnapshot(b *testing.B) {
|
||||
tracker := NewStalenessTracker(nil)
|
||||
for i := range 1000 {
|
||||
tracker.UpdateSuccess(InstanceTypeAvailability, strconv.Itoa(i), nil)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
for b.Loop() {
|
||||
if rows := tracker.Snapshot(); len(rows) != 1000 {
|
||||
b.Fatalf("snapshot has %d rows, want 1000", len(rows))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestStalenessTracker_Snapshot_Nil(t *testing.T) {
|
||||
var tracker *StalenessTracker
|
||||
snapshots := tracker.Snapshot()
|
||||
|
||||
Reference in New Issue
Block a user