From 8671a921b708b4a3081af3c132393b9a935aa106 Mon Sep 17 00:00:00 2001 From: Chris Lu Date: Sun, 8 Mar 2026 20:40:57 -0700 Subject: [PATCH] fix: seed trace server counts from ActiveTopology to match detection logic The decision trace was building serverVolumeCounts only from metrics, missing zero-volume servers seeded from ActiveTopology by Detection. This could cause the trace to report wrong server counts, incorrect imbalance ratios, or spurious "too few servers" messages. Pass activeTopology into the trace function and seed server counts the same way Detection does. --- weed/plugin/worker/volume_balance_handler.go | 21 ++++++++++++++++++- .../worker/volume_balance_handler_test.go | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/weed/plugin/worker/volume_balance_handler.go b/weed/plugin/worker/volume_balance_handler.go index 8a25a09cf..1a3af2b0d 100644 --- a/weed/plugin/worker/volume_balance_handler.go +++ b/weed/plugin/worker/volume_balance_handler.go @@ -230,7 +230,7 @@ func (h *VolumeBalanceHandler) Detect( return err } - if traceErr := emitVolumeBalanceDetectionDecisionTrace(sender, metrics, workerConfig.TaskConfig, results); traceErr != nil { + if traceErr := emitVolumeBalanceDetectionDecisionTrace(sender, metrics, activeTopology, workerConfig.TaskConfig, results); traceErr != nil { glog.Warningf("Plugin worker failed to emit volume_balance detection trace: %v", traceErr) } @@ -262,6 +262,7 @@ func (h *VolumeBalanceHandler) Detect( func emitVolumeBalanceDetectionDecisionTrace( sender DetectionSender, metrics []*workertypes.VolumeHealthMetrics, + activeTopology *topology.ActiveTopology, taskConfig *balancetask.Config, results []*workertypes.TaskDetectionResult, ) error { @@ -357,7 +358,25 @@ func emitVolumeBalanceDetectionDecisionTrace( continue } + // Seed server counts from topology so zero-volume servers are included, + // matching the same logic used in balancetask.Detection. serverVolumeCounts := make(map[string]int) + if activeTopology != nil { + topologyInfo := activeTopology.GetTopologyInfo() + if topologyInfo != nil { + for _, dc := range topologyInfo.DataCenterInfos { + for _, rack := range dc.RackInfos { + for _, node := range rack.DataNodeInfos { + for diskTypeName := range node.DiskInfos { + if diskTypeName == diskType { + serverVolumeCounts[node.Id] = 0 + } + } + } + } + } + } + } for _, metric := range diskMetrics { serverVolumeCounts[metric.Server]++ } diff --git a/weed/plugin/worker/volume_balance_handler_test.go b/weed/plugin/worker/volume_balance_handler_test.go index 86c4453c1..ace71ae3a 100644 --- a/weed/plugin/worker/volume_balance_handler_test.go +++ b/weed/plugin/worker/volume_balance_handler_test.go @@ -228,7 +228,7 @@ func TestEmitVolumeBalanceDetectionDecisionTraceNoTasks(t *testing.T) { {VolumeID: 4, Server: "server-b", DiskType: "hdd"}, } - if err := emitVolumeBalanceDetectionDecisionTrace(sender, metrics, config, nil); err != nil { + if err := emitVolumeBalanceDetectionDecisionTrace(sender, metrics, nil, config, nil); err != nil { t.Fatalf("emitVolumeBalanceDetectionDecisionTrace error: %v", err) } if len(sender.events) < 2 {