From cb8a4b93e78efece71cfe1e460879e9ca12b02dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=AB=A5=E5=89=91?= <1045931706@qq.com> Date: Mon, 13 Jan 2025 17:17:36 +0800 Subject: [PATCH] pass ut MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 童剑 <1045931706@qq.com> --- pkg/mcs/scheduling/server/cluster.go | 3 ++- pkg/schedule/operator/kind.go | 1 + pkg/schedule/schedulers/balance_key_range.go | 9 +++------ pkg/schedule/schedulers/init.go | 17 +++++++++++++---- pkg/schedule/schedulers/scheduler.go | 2 -- server/api/scheduler.go | 10 ++++++++++ server/cluster/cluster_test.go | 9 ++++++++- tools/pd-ctl/tests/scheduler/scheduler_test.go | 1 + 8 files changed, 38 insertions(+), 14 deletions(-) diff --git a/pkg/mcs/scheduling/server/cluster.go b/pkg/mcs/scheduling/server/cluster.go index e45611b6feb..f0b87e82c06 100644 --- a/pkg/mcs/scheduling/server/cluster.go +++ b/pkg/mcs/scheduling/server/cluster.go @@ -316,7 +316,8 @@ func (c *Cluster) updateScheduler() { for _, scheduler := range latestSchedulersConfig { schedulerType, ok := types.ConvertOldStrToType[scheduler.Type] if !ok { - log.Warn("scheduler not found ", zap.String("type", scheduler.Type)) + log.Error("scheduler not found ", zap.String("type", scheduler.Type)) + continue } s, err := schedulers.CreateScheduler( schedulerType, diff --git a/pkg/schedule/operator/kind.go b/pkg/schedule/operator/kind.go index c6e4614f525..0a7ccb34245 100644 --- a/pkg/schedule/operator/kind.go +++ b/pkg/schedule/operator/kind.go @@ -62,6 +62,7 @@ var flagToName = map[OpKind]string{ OpHotRegion: "hot-region", OpReplica: "replica", OpMerge: "merge", + OpKeyRange: "key-range", OpRange: "range", OpWitness: "witness", OpWitnessLeader: "witness-leader", diff --git a/pkg/schedule/schedulers/balance_key_range.go b/pkg/schedule/schedulers/balance_key_range.go index aace3cc057b..c21c62af71f 100644 --- a/pkg/schedule/schedulers/balance_key_range.go +++ b/pkg/schedule/schedulers/balance_key_range.go @@ -17,11 +17,7 @@ import ( "github.com/tikv/pd/pkg/schedule/operator" "github.com/tikv/pd/pkg/schedule/plan" "github.com/tikv/pd/pkg/schedule/types" -) - -const ( - // DefaultTimeout is the default balance key range scheduler timeout. - DefaultTimeout = 1 * time.Hour + "github.com/tikv/pd/pkg/utils/syncutil" ) type balanceKeyRangeSchedulerHandler struct { @@ -52,7 +48,8 @@ func (handler *balanceKeyRangeSchedulerHandler) listConfig(w http.ResponseWriter } type balanceKeyRangeSchedulerConfig struct { - baseDefaultSchedulerConfig + syncutil.RWMutex + schedulerConfig balanceKeyRangeSchedulerParam } diff --git a/pkg/schedule/schedulers/init.go b/pkg/schedule/schedulers/init.go index 4734b162203..37d17ddd9ae 100644 --- a/pkg/schedule/schedulers/init.go +++ b/pkg/schedule/schedulers/init.go @@ -19,6 +19,7 @@ import ( "strconv" "strings" "sync" + "time" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/errs" @@ -548,7 +549,7 @@ func schedulersRegister() { }) // balance key range scheduler - // args: [role, engine, range1, range2, ...] + // args: [role, engine, timeout, range1, range2, ...] RegisterSliceDecoderBuilder(types.BalanceKeyRangeScheduler, func(args []string) ConfigDecoder { return func(v any) error { conf, ok := v.(*balanceKeyRangeSchedulerConfig) @@ -566,14 +567,22 @@ func schedulersRegister() { if err != nil { return errs.ErrQueryUnescape.Wrap(err) } - ranges, err := getKeyRanges(args[2:]) + timeout, err := url.QueryUnescape(args[2]) + if err != nil { + return errs.ErrQueryUnescape.Wrap(err) + } + duration, err := time.ParseDuration(timeout) + if err != nil { + return errs.ErrURLParse.Wrap(err) + } + ranges, err := getKeyRanges(args[3:]) if err != nil { return err } conf.Ranges = ranges conf.Engine = engine conf.Role = role - conf.Timeout = DefaultTimeout + conf.Timeout = duration return nil } }) @@ -581,7 +590,7 @@ func schedulersRegister() { RegisterScheduler(types.BalanceKeyRangeScheduler, func(opController *operator.Controller, storage endpoint.ConfigStorage, decoder ConfigDecoder, _ ...func(string) error) (Scheduler, error) { conf := &balanceKeyRangeSchedulerConfig{ - baseDefaultSchedulerConfig: newBaseDefaultSchedulerConfig(), + schedulerConfig: newBaseDefaultSchedulerConfig(), } if err := decoder(conf); err != nil { return nil, err diff --git a/pkg/schedule/schedulers/scheduler.go b/pkg/schedule/schedulers/scheduler.go index fd6d6710350..8976c3a1928 100644 --- a/pkg/schedule/schedulers/scheduler.go +++ b/pkg/schedule/schedulers/scheduler.go @@ -157,9 +157,7 @@ func CreateScheduler( removeSchedulerCb ...func(string) error, ) (Scheduler, error) { fn, ok := schedulerMap[typ] - log.Info("create scheduler", zap.Any("typ", typ)) if !ok { - log.Warn("create scheduler not found", zap.Any("typ", typ)) return nil, errs.ErrSchedulerCreateFuncNotRegistered.FastGenByArgs(typ) } diff --git a/server/api/scheduler.go b/server/api/scheduler.go index f8b62864c0c..e50e563e5b8 100644 --- a/server/api/scheduler.go +++ b/server/api/scheduler.go @@ -113,6 +113,16 @@ func (h *schedulerHandler) CreateScheduler(w http.ResponseWriter, r *http.Reques h.r.JSON(w, http.StatusInternalServerError, err.Error()) return } + defaultTimeout := "1h" + if err := apiutil.CollectStringOption("timeout", input, collector); err != nil { + if errors.ErrorEqual(err, errs.ErrOptionNotExist) { + collector(defaultTimeout) + } else { + h.r.JSON(w, http.StatusInternalServerError, err.Error()) + return + } + } + if err := apiutil.CollectEscapeStringOption("start_key", input, collector); err != nil { h.r.JSON(w, http.StatusInternalServerError, err.Error()) return diff --git a/server/cluster/cluster_test.go b/server/cluster/cluster_test.go index 1fdac79f539..ca78b4cfdd7 100644 --- a/server/cluster/cluster_test.go +++ b/server/cluster/cluster_test.go @@ -3212,9 +3212,16 @@ func TestAddScheduler(t *testing.T) { _, err = schedulers.CreateScheduler(types.BalanceKeyRangeScheduler, oc, storage.NewStorageWithMemoryBackend(), schedulers.ConfigSliceDecoder(types.BalanceKeyRangeScheduler, []string{}), controller.RemoveScheduler) re.Error(err) - gls, err = schedulers.CreateScheduler(types.BalanceKeyRangeScheduler, oc, storage.NewStorageWithMemoryBackend(), schedulers.ConfigSliceDecoder(types.BalanceKeyRangeScheduler, []string{"leaner", "tiflash", "100", "200"}), controller.RemoveScheduler) + gls, err = schedulers.CreateScheduler(types.BalanceKeyRangeScheduler, oc, storage.NewStorageWithMemoryBackend(), schedulers.ConfigSliceDecoder(types.BalanceKeyRangeScheduler, []string{"learner", "tiflash", "1h", "100", "200"}), controller.RemoveScheduler) re.NoError(err) re.NoError(controller.AddScheduler(gls)) + conf, err = gls.EncodeConfig() + re.NoError(err) + data = make(map[string]any) + re.NoError(json.Unmarshal(conf, &data)) + re.Equal("learner", data["role"]) + re.Equal("tiflash", data["engine"]) + re.Equal(float64(time.Hour.Nanoseconds()), data["timeout"]) hb, err := schedulers.CreateScheduler(types.BalanceHotRegionScheduler, oc, storage.NewStorageWithMemoryBackend(), schedulers.ConfigJSONDecoder([]byte("{}"))) re.NoError(err) diff --git a/tools/pd-ctl/tests/scheduler/scheduler_test.go b/tools/pd-ctl/tests/scheduler/scheduler_test.go index e5575677cb6..a895c0d4e73 100644 --- a/tools/pd-ctl/tests/scheduler/scheduler_test.go +++ b/tools/pd-ctl/tests/scheduler/scheduler_test.go @@ -551,6 +551,7 @@ func (suite *schedulerTestSuite) checkSchedulerConfig(cluster *pdTests.TestClust mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "config", "balance-key-range-scheduler", "show"}, &conf) re.Equal("learner", conf["role"]) re.Equal("tiflash", conf["engine"]) + re.Equal(float64(time.Hour.Nanoseconds()), conf["timeout"]) ranges := conf["ranges"].([]any)[0].(map[string]any) re.Equal(base64.StdEncoding.EncodeToString([]byte("a")), ranges["start-key"]) re.Equal(base64.StdEncoding.EncodeToString([]byte("b")), ranges["end-key"])