Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: update evict-leader-scheduler&grant-leader-scheduler to return the same success message #7802

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions server/api/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,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.")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to keep consistent with

handler.rd.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())
Expand Down Expand Up @@ -204,18 +217,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]any, 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."
Expand Down
13 changes: 7 additions & 6 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,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:
Expand All @@ -595,13 +595,14 @@ 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
}
10 changes: 8 additions & 2 deletions tools/pd-ctl/tests/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ func (suite *schedulerTestSuite) TearDownTest() {
}

func (suite *schedulerTestSuite) TestScheduler() {
suite.env.RunTestInTwoModes(suite.checkScheduler)
// use a new environment to avoid affecting other tests
env := pdTests.NewSchedulingTestEnvironment(suite.T())
env.RunTestInTwoModes(suite.checkScheduler)
env.Cleanup()
}

func (suite *schedulerTestSuite) checkScheduler(cluster *pdTests.TestCluster) {
Expand Down Expand Up @@ -633,7 +636,10 @@ func (suite *schedulerTestSuite) checkScheduler(cluster *pdTests.TestCluster) {
}

func (suite *schedulerTestSuite) TestSchedulerDiagnostic() {
suite.env.RunTestInTwoModes(suite.checkSchedulerDiagnostic)
// use a new environment to avoid affecting other tests
env := pdTests.NewSchedulingTestEnvironment(suite.T())
env.RunTestInTwoModes(suite.checkSchedulerDiagnostic)
env.Cleanup()
}

func (suite *schedulerTestSuite) checkSchedulerDiagnostic(cluster *pdTests.TestCluster) {
Expand Down
Loading