From b198196e5c088ae01beb238cb4eaa04b1b02209a Mon Sep 17 00:00:00 2001 From: Hu# Date: Mon, 5 Feb 2024 15:13:13 +0800 Subject: [PATCH] api: update evict-leader-scheduler&grant-leader-scheduler to return the same success message (#7802) ref tikv/pd#7672 Signed-off-by: husharp Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com> Signed-off-by: okJiang <819421878@qq.com> --- server/api/scheduler.go | 33 +++++++++++++++++---------------- server/handler.go | 13 +++++++------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/server/api/scheduler.go b/server/api/scheduler.go index 789d5791507..1f01b233d8e 100644 --- a/server/api/scheduler.go +++ b/server/api/scheduler.go @@ -210,10 +210,23 @@ func (h *schedulerHandler) CreateScheduler(w http.ResponseWriter, r *http.Reques return } - case schedulers.GrantLeaderName: - h.addEvictOrGrant(w, input, schedulers.GrantLeaderName) - case schedulers.EvictLeaderName: - h.addEvictOrGrant(w, input, schedulers.EvictLeaderName) + case schedulers.GrantLeaderName, schedulers.EvictLeaderName: + storeID, ok := input["store_id"].(float64) + if !ok { + h.r.JSON(w, http.StatusBadRequest, "missing store id") + return + } + exist, err := h.AddEvictOrGrant(storeID, name) + if err != nil { + h.r.JSON(w, http.StatusInternalServerError, err.Error()) + return + } + // we should ensure whether it is the first time to create evict-leader-scheduler + // or just update the evict-leader. + if exist { + h.r.JSON(w, http.StatusOK, "The scheduler has been applied to the store.") + return + } case schedulers.ShuffleLeaderName: if err := h.AddShuffleLeaderScheduler(); err != nil { h.r.JSON(w, http.StatusInternalServerError, err.Error()) @@ -272,18 +285,6 @@ func (h *schedulerHandler) CreateScheduler(w http.ResponseWriter, r *http.Reques h.r.JSON(w, http.StatusOK, "The scheduler is created.") } -func (h *schedulerHandler) addEvictOrGrant(w http.ResponseWriter, input map[string]interface{}, name string) { - storeID, ok := input["store_id"].(float64) - if !ok { - h.r.JSON(w, http.StatusBadRequest, "missing store id") - return - } - err := h.AddEvictOrGrant(storeID, name) - if err != nil { - h.r.JSON(w, http.StatusInternalServerError, err.Error()) - } -} - // @Tags scheduler // @Summary Delete a scheduler. // @Param name path string true "The name of the scheduler." diff --git a/server/handler.go b/server/handler.go index 67e7dfa5e5b..4c9a1489f80 100644 --- a/server/handler.go +++ b/server/handler.go @@ -1119,10 +1119,10 @@ func (h *Handler) redirectSchedulerUpdate(name string, storeID float64) error { } // AddEvictOrGrant add evict leader scheduler or grant leader scheduler. -func (h *Handler) AddEvictOrGrant(storeID float64, name string) error { - if exist, err := h.IsSchedulerExisted(name); !exist { +func (h *Handler) AddEvictOrGrant(storeID float64, name string) (exist bool, err error) { + if exist, err = h.IsSchedulerExisted(name); !exist { if err != nil && !errors.ErrorEqual(err, errs.ErrSchedulerNotFound.FastGenByArgs()) { - return err + return exist, err } switch name { case schedulers.EvictLeaderName: @@ -1131,15 +1131,16 @@ func (h *Handler) AddEvictOrGrant(storeID float64, name string) error { err = h.AddGrantLeaderScheduler(uint64(storeID)) } if err != nil { - return err + return exist, err } } else { if err := h.redirectSchedulerUpdate(name, storeID); err != nil { - return err + return exist, err } log.Info("update scheduler", zap.String("scheduler-name", name), zap.Uint64("store-id", uint64(storeID))) + return exist, nil } - return nil + return exist, nil } // GetPausedSchedulerDelayAt returns paused unix timestamp when a scheduler is paused