diff --git a/weed/admin/dash/ec_balancer_overlap.go b/weed/admin/dash/ec_balancer_overlap.go new file mode 100644 index 0000000000..312c183561 --- /dev/null +++ b/weed/admin/dash/ec_balancer_overlap.go @@ -0,0 +1,103 @@ +package dash + +import ( + "strings" + + "github.com/seaweedfs/seaweedfs/weed/admin/maintenance" + "github.com/seaweedfs/seaweedfs/weed/glog" + "github.com/seaweedfs/seaweedfs/weed/worker/types" +) + +const ( + ecBalancerPlugin = "plugin ec_balance job" + ecBalancerLegacy = "legacy maintenance ec_balance task" + ecBalancerAdminScript = "admin_script job running ec.balance -apply" + + pluginECBalanceJobType = "ec_balance" + pluginAdminScriptJobType = "admin_script" +) + +type ecBalancerState struct { + PluginECBalanceEnabled bool + LegacyECBalanceEnabled bool + AdminScriptEnabled bool + AdminScript string +} + +func activeECBalancers(state ecBalancerState) []string { + var active []string + if state.PluginECBalanceEnabled { + active = append(active, ecBalancerPlugin) + } + if state.LegacyECBalanceEnabled { + active = append(active, ecBalancerLegacy) + } + if state.AdminScriptEnabled && scriptAppliesECBalance(state.AdminScript) { + active = append(active, ecBalancerAdminScript) + } + return active +} + +func scriptAppliesECBalance(script string) bool { + for _, line := range strings.Split(strings.ReplaceAll(script, "\r\n", "\n"), "\n") { + line = strings.TrimSpace(line) + if line == "" || strings.HasPrefix(line, "#") { + continue + } + for _, command := range strings.Split(line, ";") { + fields := strings.Fields(command) + if len(fields) == 0 || fields[0] != "ec.balance" { + continue + } + for _, arg := range fields[1:] { + name, value, hasValue := strings.Cut(strings.TrimLeft(strings.Trim(arg, "\"'"), "-"), "=") + if name != "apply" && name != "force" { + continue + } + if !hasValue || (value != "false" && value != "0") { + return true + } + } + } + } + return false +} + +func (s *AdminServer) collectECBalancerState() ecBalancerState { + var state ecBalancerState + if s.maintenanceManager != nil { + state.LegacyECBalanceEnabled = s.maintenanceManager.IsTaskTypeEnabled(maintenance.MaintenanceTaskType(types.TaskTypeECBalance)) + } + pluginSvc := s.GetPlugin() + if pluginSvc == nil { + return state + } + states, err := pluginSvc.ListSchedulerStates() + if err != nil { + glog.V(1).Infof("ec balancer check: list scheduler states: %v", err) + return state + } + for _, jobState := range states { + switch jobState.JobType { + case pluginECBalanceJobType: + state.PluginECBalanceEnabled = jobState.Enabled + case pluginAdminScriptJobType: + state.AdminScriptEnabled = jobState.Enabled + } + } + if state.AdminScriptEnabled { + cfg, err := pluginSvc.LoadJobTypeConfig(pluginAdminScriptJobType) + if err != nil { + glog.V(1).Infof("ec balancer check: load %s config: %v", pluginAdminScriptJobType, err) + } else if cfg != nil { + state.AdminScript = cfg.GetAdminConfigValues()["script"].GetStringValue() + } + } + return state +} + +// ActiveECBalancers lists the EC balancers that would move shards on their own. +// More than one entry means they plan moves from the same topology independently. +func (s *AdminServer) ActiveECBalancers() []string { + return activeECBalancers(s.collectECBalancerState()) +} diff --git a/weed/admin/dash/ec_balancer_overlap_test.go b/weed/admin/dash/ec_balancer_overlap_test.go new file mode 100644 index 0000000000..be81a51ef1 --- /dev/null +++ b/weed/admin/dash/ec_balancer_overlap_test.go @@ -0,0 +1,52 @@ +package dash + +import ( + "reflect" + "testing" +) + +func TestScriptAppliesECBalance(t *testing.T) { + cases := []struct { + script string + want bool + }{ + {"", false}, + {"fs.log.purge -daysAgo=7\nvolume.fix.replication -apply", false}, + {"ec.balance", false}, + {"ec.balance -collection=x", false}, + {"ec.balance -apply", true}, + {"ec.balance -force", true}, + {"ec.balance --apply", true}, + {"ec.balance -apply=true", true}, + {"ec.balance -apply=false", false}, + {"# ec.balance -apply", false}, + {"volume.list; ec.balance -collection=a -apply", true}, + {"ec.rebuild -apply\r\nec.balance -apply\r\n", true}, + {"ec.balancer -apply", false}, + } + for _, c := range cases { + if got := scriptAppliesECBalance(c.script); got != c.want { + t.Errorf("scriptAppliesECBalance(%q) = %v, want %v", c.script, got, c.want) + } + } +} + +func TestActiveECBalancers(t *testing.T) { + cases := []struct { + name string + state ecBalancerState + want []string + }{ + {"none", ecBalancerState{}, nil}, + {"plugin only", ecBalancerState{PluginECBalanceEnabled: true}, []string{ecBalancerPlugin}}, + {"disabled script ignored", ecBalancerState{PluginECBalanceEnabled: true, AdminScript: "ec.balance -apply"}, []string{ecBalancerPlugin}}, + {"script without ec.balance", ecBalancerState{PluginECBalanceEnabled: true, AdminScriptEnabled: true, AdminScript: "volume.fix.replication -apply"}, []string{ecBalancerPlugin}}, + {"all three", ecBalancerState{PluginECBalanceEnabled: true, LegacyECBalanceEnabled: true, AdminScriptEnabled: true, AdminScript: "ec.balance -apply"}, []string{ecBalancerPlugin, ecBalancerLegacy, ecBalancerAdminScript}}, + {"legacy and script", ecBalancerState{LegacyECBalanceEnabled: true, AdminScriptEnabled: true, AdminScript: "ec.balance -force"}, []string{ecBalancerLegacy, ecBalancerAdminScript}}, + } + for _, c := range cases { + if got := activeECBalancers(c.state); !reflect.DeepEqual(got, c.want) { + t.Errorf("%s: activeECBalancers = %v, want %v", c.name, got, c.want) + } + } +}