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.

Invalidate the source-target index on every mapping mutation

rcourtman2fc47df47c1d
diff --git a/internal/unifiedresources/registry.go b/internal/unifiedresources/registry.go
index 96538e5ca9..a091252357 100644
--- a/internal/unifiedresources/registry.go
+++ b/internal/unifiedresources/registry.go
@@ -142,6 +142,16 @@ type agentNodeCandidate struct {
resource *Resource
}
+// invalidateSourceTargetsLocked drops the inverse source-target index the
+// moment any bySource mapping mutates. Batch ingests release the registry
+// lock between records and only mark viewsDirty in their epilogue, so
+// without this a reader interleaving mid-batch would be served pre-batch
+// targets from the cache; dropping it sends those readers to the legacy
+// live scan, exactly the freshness the pre-index code had.
+func (rr *ResourceRegistry) invalidateSourceTargetsLocked() {
+ rr.cachedSourceTargets = nil
+}
+
// NewRegistry creates a new registry using the provided store for overrides.
func NewRegistry(store ResourceStore) *ResourceRegistry {
rr := &ResourceRegistry{
@@ -782,6 +792,7 @@ func (rr *ResourceRegistry) seedSourceMappingsFromResourceLocked(resource *Resou
if resource == nil {
return
}
+ rr.invalidateSourceTargetsLocked()
sources := make([]DataSource, 0, len(resource.Sources)+len(resource.SourceStatus))
seen := make(map[DataSource]struct{}, len(resource.Sources)+len(resource.SourceStatus))
@@ -2602,6 +2613,7 @@ func (rr *ResourceRegistry) ingestKubernetesReplicaSet(cluster models.Kubernetes
func (rr *ResourceRegistry) ingest(source DataSource, sourceID string, resource Resource, identity ResourceIdentity) string {
rr.mu.Lock()
defer rr.mu.Unlock()
+ rr.invalidateSourceTargetsLocked()
sourceID = normalizeSourceID(sourceID)
if sourceID == "" {
return ""
@@ -2700,6 +2712,7 @@ func (rr *ResourceRegistry) ingestAvailabilityCheckLocked(
resource Resource,
identity ResourceIdentity,
) string {
+ rr.invalidateSourceTargetsLocked()
checkResourceID := rr.sourceSpecificID(resource.Type, SourceAvailability, sourceID)
if mappedID := CanonicalResourceID(rr.bySource[SourceAvailability][sourceID]); mappedID != "" {
if existing := rr.resources[mappedID]; existing != nil &&
@@ -2858,6 +2871,7 @@ func (rr *ResourceRegistry) mergeLinkedKubernetesNode(
rr.mu.Lock()
defer rr.mu.Unlock()
+ rr.invalidateSourceTargetsLocked()
existingID := rr.bySource[SourceAgent][linkedAgentSourceID]
existing := rr.resources[existingID]
@@ -4178,6 +4192,7 @@ func (rr *ResourceRegistry) mergeResourceData(primary *Resource, other *Resource
}
func (rr *ResourceRegistry) updateSourceMappings(fromID, toID string) {
+ rr.invalidateSourceTargetsLocked()
fromID = CanonicalResourceID(strings.TrimSpace(fromID))
toID = CanonicalResourceID(strings.TrimSpace(toID))
if fromID == "" || toID == "" || fromID == toID {
diff --git a/internal/unifiedresources/registry_source_targets_test.go b/internal/unifiedresources/registry_source_targets_test.go
index 19a6a90b8f..7484ef65e4 100644
--- a/internal/unifiedresources/registry_source_targets_test.go
+++ b/internal/unifiedresources/registry_source_targets_test.go
@@ -95,6 +95,37 @@ func TestMetricsTargetIndexMatchesLegacyScan(t *testing.T) {
}
}
+// Any bySource mutation must drop the cached inverse index immediately:
+// batch ingests release the lock between records and only mark viewsDirty in
+// their epilogue, so a reader interleaving mid-batch must fall back to the
+// live scan rather than being served pre-batch targets.
+func TestSourceTargetIndexInvalidatesOnIngest(t *testing.T) {
+ rr := sourceTargetsFixtureRegistry()
+
+ rr.VMs()
+ rr.mu.RLock()
+ built := rr.cachedSourceTargets != nil
+ rr.mu.RUnlock()
+ if !built {
+ t.Fatal("view read did not build the source-target index")
+ }
+
+ rr.IngestRecords(SourceAvailability, []IngestRecord{{
+ SourceID: "avail-probe-1",
+ Resource: Resource{Type: ResourceTypeAgent, Name: "probe"},
+ }})
+
+ // The epilogue marks views dirty, but the index must already have been
+ // dropped by the per-record mutation path, not merely masked by the
+ // dirty flag.
+ rr.mu.RLock()
+ stillCached := rr.cachedSourceTargets != nil
+ rr.mu.RUnlock()
+ if stillCached {
+ t.Fatal("bySource mutation left the stale source-target index in place")
+ }
+}
+
// View-embedded metrics targets must match what the public per-ID API
// resolves for the same resource.
func TestRebuiltViewMetricsTargetsMatchPublicAPI(t *testing.T) {