Browse Source

fix: nil-guard clusterInfo, uncap legacy DetectionFunc, deterministic disk type order

- Add early nil guard for clusterInfo in Detection to prevent panics
  in downstream helpers (detectForDiskType, createBalanceTask)
- Change register.go DetectionFunc wrapper from maxResults=1 to 0
  (no cap) so the legacy code path returns all detected tasks
- Sort disk type keys before iteration so results are deterministic
  when maxResults spans multiple disk types (HDD/SSD)
pull/8559/head
Chris Lu 4 days ago
parent
commit
6be0ee61d1
  1. 16
      weed/worker/tasks/balance/detection.go
  2. 2
      weed/worker/tasks/balance/register.go

16
weed/worker/tasks/balance/detection.go

@ -3,6 +3,7 @@ package balance
import ( import (
"fmt" "fmt"
"math" "math"
"sort"
"time" "time"
"github.com/seaweedfs/seaweedfs/weed/admin/topology" "github.com/seaweedfs/seaweedfs/weed/admin/topology"
@ -20,6 +21,9 @@ func Detection(metrics []*types.VolumeHealthMetrics, clusterInfo *types.ClusterI
if !config.IsEnabled() { if !config.IsEnabled() {
return nil, nil return nil, nil
} }
if clusterInfo == nil {
return nil, nil
}
balanceConfig := config.(*Config) balanceConfig := config.(*Config)
@ -33,14 +37,22 @@ func Detection(metrics []*types.VolumeHealthMetrics, clusterInfo *types.ClusterI
volumesByDiskType[metric.DiskType] = append(volumesByDiskType[metric.DiskType], metric) volumesByDiskType[metric.DiskType] = append(volumesByDiskType[metric.DiskType], metric)
} }
// Sort disk types for deterministic iteration order when maxResults
// spans multiple disk types.
diskTypes := make([]string, 0, len(volumesByDiskType))
for dt := range volumesByDiskType {
diskTypes = append(diskTypes, dt)
}
sort.Strings(diskTypes)
var allParams []*types.TaskDetectionResult var allParams []*types.TaskDetectionResult
for diskType, diskMetrics := range volumesByDiskType {
for _, diskType := range diskTypes {
remaining := maxResults - len(allParams) remaining := maxResults - len(allParams)
if remaining <= 0 { if remaining <= 0 {
break break
} }
tasks := detectForDiskType(diskType, diskMetrics, balanceConfig, clusterInfo, remaining)
tasks := detectForDiskType(diskType, volumesByDiskType[diskType], balanceConfig, clusterInfo, remaining)
allParams = append(allParams, tasks...) allParams = append(allParams, tasks...)
} }

2
weed/worker/tasks/balance/register.go

@ -59,7 +59,7 @@ func RegisterBalanceTask() {
), nil ), nil
}, },
DetectionFunc: func(metrics []*types.VolumeHealthMetrics, info *types.ClusterInfo, config base.TaskConfig) ([]*types.TaskDetectionResult, error) { DetectionFunc: func(metrics []*types.VolumeHealthMetrics, info *types.ClusterInfo, config base.TaskConfig) ([]*types.TaskDetectionResult, error) {
return Detection(metrics, info, config, 1)
return Detection(metrics, info, config, 0)
}, },
ScanInterval: 30 * time.Minute, ScanInterval: 30 * time.Minute,
SchedulingFunc: Scheduling, SchedulingFunc: Scheduling,

Loading…
Cancel
Save