fix(nodes): prefer live observations for shared descendants

This commit is contained in:
Hamed Sanaei
2026-12-03 04:02:59 +03:30
parent b49a705f5c
commit 44ed457985
2 changed files with 99 additions and 7 deletions
+68
View File
@@ -191,6 +191,74 @@ func TestNodeOverviewCountsHostMembershipsAndUniqueFleetClients(t *testing.T) {
}
}
func TestNodeOverviewUsesBestDescendantObservation(t *testing.T) {
cleanup, err := testpg.IsolatePackage("node_fleet_descendants")
if err != nil {
t.Fatal(err)
}
t.Cleanup(cleanup)
dbtest.InitDB(t, filepath.Join(t.TempDir(), "fleet-descendants.db"))
db := database.GetDB()
svc := &NodeService{}
t.Cleanup(func() { svc.PruneNodeDescendants(nil) })
parents := []model.Node{
{Name: "parent-a", Address: "192.0.2.1", Port: 2053, Guid: "parent-a", Enable: true},
{Name: "parent-b", Address: "192.0.2.2", Port: 2053, Guid: "parent-b", Enable: true},
}
if err := db.Create(&parents).Error; err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
status [2]string
age [2]time.Duration
wantParent int
wantStatus string
}{
{"lower ID offline", [2]string{"offline", "online"}, [2]time.Duration{}, 1, "online"},
{"higher ID offline", [2]string{"online", "offline"}, [2]time.Duration{}, 0, "online"},
{"lower ID disabled", [2]string{"disabled", "online"}, [2]time.Duration{}, 1, "online"},
{"higher ID disabled", [2]string{"online", "disabled"}, [2]time.Duration{}, 0, "online"},
{"both unavailable", [2]string{"offline", "disabled"}, [2]time.Duration{}, 0, "unknown"},
{"first sample stale", [2]string{"online", "online"}, [2]time.Duration{time.Minute, 0}, 1, "online"},
{"newer sample preferred", [2]string{"online", "online"}, [2]time.Duration{time.Second, 0}, 1, "online"},
{"equal samples keep lower ID", [2]string{"online", "online"}, [2]time.Duration{}, 0, "online"},
} {
t.Run(tc.name, func(t *testing.T) {
now := time.Now()
for i, parent := range parents {
if err := db.Model(&parent).Updates(map[string]any{
"enable": tc.status[i] != "disabled", "status": tc.status[i],
}).Error; err != nil {
t.Fatal(err)
}
nodeDescendantsMu.Lock()
nodeDescendantsCache[parent.Id] = []model.NodeSummary{{
Guid: "shared-child", Name: "shared-child", ParentGuid: parent.Guid, Status: "online",
Telemetry: &model.NodeTelemetry{SampledAt: now.Add(-tc.age[i]).UnixMilli(), CpuPct: new(float64(20 + i))},
}}
nodeDescendantsMu.Unlock()
}
view, err := svc.GetNodeOverview(nil)
if err != nil || len(view.Nodes) != 3 {
t.Fatalf("expected two parents and one child: %+v, %v", view, err)
}
child := view.Nodes[2]
if !child.Transitive || child.Guid != "shared-child" || child.ParentGuid != parents[tc.wantParent].Guid || child.Status != tc.wantStatus {
t.Fatalf("unexpected descendant observation: %+v", child)
}
if tc.wantStatus == "unknown" {
if child.Telemetry != nil {
t.Fatalf("unavailable descendant has telemetry: %+v", child.Telemetry)
}
} else if child.Telemetry == nil || child.Telemetry.CpuPct == nil || *child.Telemetry.CpuPct != float64(20+tc.wantParent) {
t.Fatalf("wrong descendant telemetry: %+v", child.Telemetry)
}
})
}
}
func BenchmarkNodeOverviewFleetScale(b *testing.B) {
dbtest.InitDB(b, filepath.Join(b.TempDir(), "fleet-scale.db"))
db := database.GetDB()
+31 -7
View File
@@ -140,6 +140,23 @@ func cachedDescendants(nodes []*model.Node) []model.NodeSummary {
return out
}
func preferDescendantObservation(candidate, current *model.Node, now time.Time) bool {
next, previous := liveNodeTelemetry(candidate, now), liveNodeTelemetry(current, now)
if next != nil {
return previous == nil || next.SampledAt > previous.SampledAt
}
if previous != nil {
return false
}
candidateOnline := candidate.Enable && candidate.Status == "online"
currentOnline := current.Enable && current.Status == "online"
if candidateOnline != currentOnline {
return candidateOnline
}
// A known state is more informative than the placeholder from a lost branch.
return current.Status == "unknown" && candidate.Status != "unknown"
}
// GetNodeTree returns the direct nodes plus any transitive sub-nodes learned
// from them, with per-GUID counts so each node shows only the inbounds/online
// it physically hosts (#4983). Direct nodes carry the master's own GUID as
@@ -165,7 +182,8 @@ func (s *NodeService) getNodeTreeWithCounts(overview bool) ([]*model.Node, NodeF
}
}
seen := make(map[string]struct{})
now := time.Now()
indexByGUID := make(map[string]int)
var transitive []*model.Node
for _, sum := range cachedDescendants(nodes) {
if sum.Guid == "" || sum.Guid == selfGuid {
@@ -174,11 +192,7 @@ func (s *NodeService) getNodeTreeWithCounts(overview bool) ([]*model.Node, NodeF
if _, ok := directGuids[sum.Guid]; ok {
continue // already shown as a direct node
}
if _, ok := seen[sum.Guid]; ok {
continue
}
seen[sum.Guid] = struct{}{}
transitive = append(transitive, &model.Node{
descendant := &model.Node{
Guid: sum.Guid,
ParentGuid: sum.ParentGuid,
Name: sum.Name,
@@ -196,7 +210,17 @@ func (s *NodeService) getNodeTreeWithCounts(overview bool) ([]*model.Node, NodeF
Enable: sum.Enabled == nil || *sum.Enabled,
BasePath: sum.BasePath,
Telemetry: sum.Telemetry,
})
}
if index, ok := indexByGUID[sum.Guid]; ok {
// Replace in place to preserve row order. Equal observations keep
// the first parent (ListOperational orders parents by ascending ID).
if preferDescendantObservation(descendant, transitive[index], now) {
transitive[index] = descendant
}
continue
}
indexByGUID[sum.Guid] = len(transitive)
transitive = append(transitive, descendant)
}
all := append(nodes, transitive...)
master, fleet, err := s.countNodeFleet(all, selfGuid, overview)