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.

Make metrics writes idempotent for duplicate samples

rcourtman575f4321831e
diff --git a/docs/release-control/v6/internal/records/known-rc-issue-closure-for-ga-duplicate-metrics-2026-05-01.md b/docs/release-control/v6/internal/records/known-rc-issue-closure-for-ga-duplicate-metrics-2026-05-01.md
new file mode 100644
index 0000000000..0648e3c148
--- /dev/null
+++ b/docs/release-control/v6/internal/records/known-rc-issue-closure-for-ga-duplicate-metrics-2026-05-01.md
@@ -0,0 +1,59 @@
+# Known RC Issue Closure For GA Duplicate Metrics Record
+
+- Date: `2026-05-01`
+- Gate: `known-rc-issue-closure-for-ga`
+- Result: `passed`
+
+## Context
+
+The RC3 issue triage found `#1442` as an additional credible v6 blocker
+candidate. The reporter saw the browser tab grow to roughly 10 GB and the
+server logs repeatedly emitted:
+
+- `UNIQUE constraint failed: metrics.resource_type, metrics.resource_id, metrics.metric_type, metrics.timestamp, metrics.tier`
+
+The current v6 metrics schema already declares that tuple as unique through
+`idx_metrics_unique`, but the raw write path still used a plain insert. Two
+samples for the same resource, metric, timestamp, and tier could therefore
+produce noisy write failures and leave the metrics writer retrying/logging a
+condition that should be idempotent.
+
+## Disposition
+
+`#1442` is fixed in the canonical metrics persistence layer:
+
+- `pkg/metrics/store.go` now writes buffered metrics with an `ON CONFLICT`
+ upsert against the existing uniqueness contract.
+- The duplicate sample keeps one row and the latest buffered value wins.
+- `min_value` and `max_value` are cleared on raw duplicate writes, matching the
+ raw metric shape where those aggregate columns are derived at query time.
+
+This is the root fix for the v6 regression candidate because the duplicate
+tuple is owned by the metrics store schema, not by any one caller or frontend
+view.
+
+## Additional RC3 Triage
+
+The same pass reviewed recent v5 fixes and current issue/discussion candidates:
+
+- `#1447`, `#1446`, `#1438`, and `#1437` already have matching fixes on
+ `pulse/v6-release` for agent identity persistence, mdstat operation gating,
+ linked host/guest filesystems, and snapshot continuity.
+- `#1433` and `#1427` are legacy v5 dashboard-specific reports and do not map
+ to the current v6 default surface as RC3 code blockers.
+- `#1443` is real v6 product feedback on interface density, but it is not a
+ narrow RC3 defect fix candidate for this slice.
+- Discussion `#876` and `#1434` map back to installer and entitlement issues
+ already covered by the earlier RC3 follow-up fixes.
+- Discussion `#1448` is already covered by the PBS threshold fix from the RC3
+ follow-up record.
+
+## Proof
+
+- `go test ./pkg/metrics -count=1`
+
+## Outcome
+
+The remaining credible duplicate metrics RC3 candidate is fixed with targeted
+regression coverage. The `known-rc-issue-closure-for-ga` gate remains satisfied
+for the current v6 release candidate.
diff --git a/docs/release-control/v6/internal/status.json b/docs/release-control/v6/internal/status.json
index 020292cc55..dd219cd4bc 100644
--- a/docs/release-control/v6/internal/status.json
+++ b/docs/release-control/v6/internal/status.json
@@ -4365,6 +4365,12 @@
"kind": "file",
"evidence_tier": "managed-runtime-exercise"
},
+ {
+ "repo": "pulse",
+ "path": "docs/release-control/v6/internal/records/known-rc-issue-closure-for-ga-duplicate-metrics-2026-05-01.md",
+ "kind": "file",
+ "evidence_tier": "managed-runtime-exercise"
+ },
{
"repo": "pulse",
"path": "docs/release-control/v6/internal/records/known-rc-issue-closure-for-ga-rc3-followup-2026-05-01.md",
diff --git a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
index c88f3260c0..b3d89c1894 100644
--- a/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
+++ b/docs/release-control/v6/internal/subsystems/performance-and-scalability.md
@@ -572,10 +572,14 @@ transactions over tiny frequent commits: the default write buffer should stay
large enough to absorb one poll cycle on modest multi-node installs, queued
flush batches should coalesce before commit when the worker is already behind,
and WAL auto-checkpointing should avoid tiny segment rewrite loops that keep
-rewriting `metrics.db` on SSD-backed systems. Any future change that reduces
-that batching headroom or makes WAL checkpoints more aggressive again must
-re-prove the metrics-store hot path with the owned store tests rather than
-assuming the earlier vacuum fixes are sufficient.
+rewriting `metrics.db` on SSD-backed systems. Duplicate samples for the same
+resource type, resource id, metric type, timestamp, and tier must be treated as
+idempotent writes against the metrics table's unique key rather than as noisy
+SQLite constraint failures; for raw writes, the latest buffered value wins and
+aggregate-only min/max columns stay unset. Any future change that reduces that
+batching headroom, makes WAL checkpoints more aggressive again, or reopens
+duplicate-write failures must re-prove the metrics-store hot path with the
+owned store tests rather than assuming the earlier vacuum fixes are sufficient.
contract instead of inventing an infrastructure-local summary filter branch.
For shared line charts on that hot path, the shared sparkline primitive may
isolate the selected series inside the existing render budget, but that
diff --git a/pkg/metrics/store.go b/pkg/metrics/store.go
index 2fe57bd2d8..9d63de2199 100644
--- a/pkg/metrics/store.go
+++ b/pkg/metrics/store.go
@@ -641,6 +641,11 @@ func (s *Store) writeBatch(metrics []bufferedMetric) {
stmt, err := tx.Prepare(`
INSERT INTO metrics (resource_type, resource_id, metric_type, value, timestamp, tier)
VALUES (?, ?, ?, ?, ?, ?)
+ ON CONFLICT(resource_type, resource_id, metric_type, timestamp, tier)
+ DO UPDATE SET
+ value = excluded.value,
+ min_value = excluded.min_value,
+ max_value = excluded.max_value
`)
if err != nil {
tx.Rollback()
diff --git a/pkg/metrics/store_additional_test.go b/pkg/metrics/store_additional_test.go
index c70bd929f5..e48ffc8796 100644
--- a/pkg/metrics/store_additional_test.go
+++ b/pkg/metrics/store_additional_test.go
@@ -69,6 +69,36 @@ func TestStoreWriteBatchSync(t *testing.T) {
}
}
+func TestStoreWriteBatchUpsertsDuplicateRawMetric(t *testing.T) {
+ dir := t.TempDir()
+ cfg := DefaultConfig(dir)
+ cfg.DBPath = filepath.Join(dir, "metrics-duplicates.db")
+ cfg.FlushInterval = time.Hour
+
+ store, err := NewStore(cfg)
+ if err != nil {
+ t.Fatalf("NewStore returned error: %v", err)
+ }
+ defer store.Close()
+
+ ts := time.Now().UTC().Truncate(time.Second)
+ store.writeBatch([]bufferedMetric{
+ {resourceType: "agent", resourceID: "host-1", metricType: "memory", value: 42, timestamp: ts, tier: TierRaw},
+ {resourceType: "agent", resourceID: "host-1", metricType: "memory", value: 55, timestamp: ts, tier: TierRaw},
+ })
+
+ points, err := store.Query("agent", "host-1", "memory", ts.Add(-time.Second), ts.Add(time.Second), 0)
+ if err != nil {
+ t.Fatalf("Query returned error: %v", err)
+ }
+ if len(points) != 1 {
+ t.Fatalf("expected duplicate timestamp to collapse to one point, got %+v", points)
+ }
+ if points[0].Value != 55 {
+ t.Fatalf("expected latest duplicate value to win, got %+v", points[0])
+ }
+}
+
func TestResolveStoreDBPathCanonicalizesOwnedPath(t *testing.T) {
root := t.TempDir()
rawPath := filepath.Join(root, "metrics", "..", "metrics", "metrics.db")