Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Refresh the resource store at most once per 2s of agent reports (#2242)

rcourtman0abe518219f7
diff --cc internal/monitoring/monitor_host_agents_test.go
index 5bfa88eb30,c13e1f9781..36e36095e9
--- a/internal/monitoring/monitor_host_agents_test.go
+++ b/internal/monitoring/monitor_host_agents_test.go
@@@ -6480,62 -6481,97 +6481,152 @@@ func TestAgentReportRefreshClonesRegist
}
m.refreshUnifiedResourceStoreAfterAgentStateChange()
- if counting.getAll != 1 {
- t.Fatalf("registry clones for one agent report refresh = %d, want 1", counting.getAll)
+ if got := counting.getAll.Load(); got != 1 {
+ t.Fatalf("registry clones for one agent report refresh = %d, want 1", got)
+ }
+ }
+
+ // newAgentReportThrottleMonitor returns a monitor whose report-driven store
+ // refreshes are throttled to window, with one online host agent.
+ func newAgentReportThrottleMonitor(t *testing.T, window time.Duration) (*Monitor, *countingResourceStore) {
+ t.Helper()
+ state := models.NewState()
+ state.UpsertHost(models.Host{ID: "agent-1", Hostname: "pve1.example", Status: "online", CPUUsage: 1, LastSeen: time.Now().UTC()})
+ counting := &countingResourceStore{MonitorAdapter: unifiedresources.NewMonitorAdapter(unifiedresources.NewRegistry(nil))}
+ m := &Monitor{
+ state: state,
+ resourceStore: counting,
+ metricsHistory: NewMetricsHistory(32, time.Hour),
+ agentReportRefreshWindow: window,
+ }
+ t.Cleanup(m.stopAgentReportRefresh)
+ return m, counting
+ }
+
+ func reportAgentCPU(m *Monitor, cpu float64) {
+ m.state.UpsertHost(models.Host{ID: "agent-1", Hostname: "pve1.example", Status: "online", CPUUsage: cpu, LastSeen: time.Now().UTC()})
+ m.refreshUnifiedResourceStoreAfterAgentReport()
+ }
+
+ func publishedAgentCPU(t *testing.T, store *countingResourceStore) float64 {
+ t.Helper()
+ for _, resource := range store.MonitorAdapter.GetAll() {
+ if resource.Type == unifiedresources.ResourceTypeAgent && resource.Metrics != nil && resource.Metrics.CPU != nil {
+ return resource.Metrics.CPU.Value
+ }
+ }
+ t.Fatal("no agent CPU in the canonical store")
+ return 0
+ }
+
+ // Accepted agent reports refresh the estate-wide store at most once per
+ // window: the first report after a quiet window is published before the call
+ // returns, and reports inside the window fold into one trailing refresh that
+ // publishes the latest state (#2199).
+ func TestAgentReportRefreshFoldsReportsWithinWindow(t *testing.T) {
+ const window = 500 * time.Millisecond
+ m, store := newAgentReportThrottleMonitor(t, window)
+
+ reportAgentCPU(m, 10)
+ if got := store.getAll.Load(); got != 1 {
+ t.Fatalf("refreshes after the leading report = %d, want 1", got)
+ }
+ if got := publishedAgentCPU(t, store); got != 10 {
+ t.Fatalf("leading report published CPU %v, want 10", got)
+ }
+
+ for cpu := 20.0; cpu <= 60; cpu += 10 {
+ reportAgentCPU(m, cpu)
+ }
+ if got := store.getAll.Load(); got != 1 {
+ t.Fatalf("refreshes during the window = %d, want the burst deferred", got)
+ }
+
+ deadline := time.Now().Add(5 * time.Second)
+ for store.getAll.Load() < 2 && time.Now().Before(deadline) {
+ time.Sleep(10 * time.Millisecond)
+ }
+ if got := store.getAll.Load(); got != 2 {
+ t.Fatalf("refreshes after the window = %d, want one trailing refresh", got)
+ }
+ if got := publishedAgentCPU(t, store); got != 60 {
+ t.Fatalf("trailing refresh published CPU %v, want the latest report's 60", got)
+ }
+
+ time.Sleep(window + 100*time.Millisecond)
+ reportAgentCPU(m, 70)
+ if got := store.getAll.Load(); got != 3 {
+ t.Fatalf("refreshes after a quiet window = %d, want a synchronous leading refresh", got)
+ }
+ if got := publishedAgentCPU(t, store); got != 70 {
+ t.Fatalf("leading report after a quiet window published CPU %v, want 70", got)
+ }
+ }
+
+ func TestStopCancelsPendingAgentReportRefresh(t *testing.T) {
+ const window = 100 * time.Millisecond
+ m, store := newAgentReportThrottleMonitor(t, window)
+
+ reportAgentCPU(m, 10)
+ reportAgentCPU(m, 20)
+ m.stopAgentReportRefresh()
+ time.Sleep(3 * window)
+ if got := store.getAll.Load(); got != 1 {
+ t.Fatalf("refreshes after stop = %d, want the pending trailing refresh cancelled", got)
}
}
+
+// A standalone agent offline across a restart is added back to every canonical
+// read-state lookup from host continuity. Alert evaluation makes one lookup per
+// resource per poll, so the overlay must be reused, not rebuilt per lookup.
+func TestStandaloneHostContinuityReadStateReusedAcrossLookups(t *testing.T) {
+ now := time.Now().UTC()
+ state := models.NewState()
+ state.UpsertHost(models.Host{ID: "agent-live", Hostname: "live.example", Status: "online", LastSeen: now})
+ continuity := config.NewHostContinuityStore(t.TempDir(), nil)
+ if err := continuity.Upsert(config.HostContinuityEntry{HostID: "agent-gone", Hostname: "gone.example", LastSeen: now.Add(-time.Hour)}); err != nil {
+ t.Fatalf("seed host continuity: %v", err)
+ }
+ adapter := unifiedresources.NewMonitorAdapter(unifiedresources.NewRegistry(nil))
+ m := &Monitor{state: state, resourceStore: adapter, hostContinuityStore: continuity}
+ m.refreshUnifiedResourceStoreAfterAgentStateChange()
+
+ first := m.GetUnifiedReadStateOrSnapshot()
+ if first == unifiedresources.ReadState(adapter) {
+ t.Fatal("read state has no continuity overlay for the offline standalone agent")
+ }
+ found := false
+ for _, host := range first.Hosts() {
+ found = found || host.Hostname() == "gone.example"
+ }
+ if !found {
+ t.Fatal("continuity overlay does not include the offline standalone agent")
+ }
+ if second := m.GetUnifiedReadStateOrSnapshot(); second != first {
+ t.Fatal("a second read-state lookup rebuilt the continuity overlay")
+ }
+}
+
+// GetLiveHostsSnapshot runs on every agent report, config fetch, and host
+// continuity lookup, so it must copy the hosts alone, not the whole state.
+func TestGetLiveHostsSnapshotCopiesOnlyHosts(t *testing.T) {
+ state := models.NewState()
+ state.UpsertHost(models.Host{ID: "agent-1", Hostname: "pve1.example", Status: "online"})
+ vms := make([]models.VM, 1000)
+ for i := range vms {
+ vms[i] = models.VM{
+ ID: fmt.Sprintf("lab:pve1:%d", 100+i), VMID: 100 + i, Name: fmt.Sprintf("vm-%d", i),
+ Node: "pve1", Instance: "lab", Status: "running", Type: "qemu",
+ Disks: []models.Disk{{Mountpoint: "/", Total: 10 << 30, Used: 1 << 30}},
+ }
+ }
+ state.UpdateVMs(vms)
+ m := &Monitor{state: state}
+
+ if hosts := m.GetLiveHostsSnapshot(); len(hosts) != 1 || hosts[0].ID != "agent-1" {
+ t.Fatalf("live hosts = %+v, want the one registered agent", hosts)
+ }
+ if allocs := testing.AllocsPerRun(10, func() { _ = m.GetLiveHostsSnapshot() }); allocs > 50 {
+ t.Fatalf("GetLiveHostsSnapshot allocated %.0f times with 1 host and 1000 guests; it is copying guests", allocs)
+ }
+}