Skip to content

Commit

Permalink
*: reset config if the input is invalid (tikv#8632) (tikv#8658)
Browse files Browse the repository at this point in the history
close tikv#8619

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Signed-off-by: Ryan Leung <rleungx@gmail.com>

Co-authored-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
ti-chi-bot and rleungx authored Oct 25, 2024
1 parent 69d2142 commit 2da9d84
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 9 deletions.
20 changes: 16 additions & 4 deletions plugin/scheduler_example/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ func init() {
}

// SchedulerType returns the type of the scheduler
//nolint
// nolint
func SchedulerType() string {
return EvictLeaderType
}

// SchedulerArgs returns the args for the scheduler
//nolint
// nolint
func SchedulerArgs() []string {
args := []string{"1"}
return args
Expand Down Expand Up @@ -271,9 +271,21 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
args = append(args, handler.config.getRanges(id)...)
}

handler.config.BuildWithArgs(args)
err := handler.config.Persist()
err := handler.config.BuildWithArgs(args)
if err != nil {
handler.config.mu.Lock()
handler.config.cluster.ResumeLeaderTransfer(id)
handler.config.mu.Unlock()
handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}

err = handler.config.Persist()
if err != nil {
handler.config.mu.Lock()
delete(handler.config.StoreIDWitRanges, id)
handler.config.cluster.ResumeLeaderTransfer(id)
handler.config.mu.Unlock()
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
}
handler.rd.JSON(w, http.StatusOK, nil)
Expand Down
14 changes: 12 additions & 2 deletions server/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ func (conf *evictLeaderSchedulerConfig) getStores() []uint64 {
}

func (conf *evictLeaderSchedulerConfig) BuildWithArgs(args []string) error {
failpoint.Inject("buildWithArgsErr", func() {
failpoint.Return(errors.New("fail to build with args"))
})
if len(args) != 1 {
return errs.ErrSchedulerConfig.FastGenByArgs("id")
}
Expand Down Expand Up @@ -367,8 +370,15 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
args = append(args, handler.config.getRanges(id)...)
}

handler.config.BuildWithArgs(args)
err := handler.config.Persist()
err := handler.config.BuildWithArgs(args)
if err != nil {
handler.config.mu.Lock()
handler.config.cluster.ResumeLeaderTransfer(id)
handler.config.mu.Unlock()
handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
err = handler.config.Persist()
if err != nil {
handler.config.removeStore(id)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
Expand Down
13 changes: 10 additions & 3 deletions server/schedulers/grant_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,17 @@ func (handler *grantLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
args = append(args, handler.config.getRanges(id)...)
}

handler.config.BuildWithArgs(args)
err := handler.config.Persist()
err := handler.config.BuildWithArgs(args)
if err != nil {
handler.config.removeStore(id)
handler.config.mu.Lock()
handler.config.cluster.ResumeLeaderTransfer(id)
handler.config.mu.Unlock()
handler.rd.JSON(w, http.StatusBadRequest, err.Error())
return
}
err = handler.config.Persist()
if err != nil {
_, _ = handler.config.removeStore(id)
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
Expand Down
58 changes: 58 additions & 0 deletions tests/pdctl/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/config"
Expand Down Expand Up @@ -399,3 +400,60 @@ func (s *schedulerTestSuite) TestScheduler(c *C) {
c.Assert(err, IsNil)
checkSchedulerWithStatusCommand(nil, "disabled", nil)
}

func (s *schedulerTestSuite) TestEvictLeaderScheduler(c *C) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1)
c.Assert(err, IsNil)
defer cluster.Destroy()
err = cluster.RunInitialServers()
c.Assert(err, IsNil)
cluster.WaitLeader()
pdAddr := cluster.GetConfig().GetClientURL()
cmd := pdctlCmd.GetRootCmd()

stores := []*metapb.Store{
{
Id: 1,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 2,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 3,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 4,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
}
leaderServer := cluster.GetServer(cluster.GetLeader())
c.Assert(leaderServer.BootstrapCluster(), IsNil)
for _, store := range stores {
pdctl.MustPutStore(c, leaderServer.GetServer(), store)
}

pdctl.MustPutRegion(c, cluster, 1, 1, []byte("a"), []byte("b"))
output, err := pdctl.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "2"}...)
c.Assert(err, IsNil)
c.Assert(strings.Contains(string(output), "Success!"), IsTrue)
failpoint.Enable("github.com/tikv/pd/server/schedulers/buildWithArgsErr", "return(true)")
output, err = pdctl.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "1"}...)
c.Assert(err, IsNil)
c.Assert(strings.Contains(string(output), "fail to build with args"), IsTrue)
failpoint.Disable("github.com/tikv/pd/server/schedulers/buildWithArgsErr")
output, err = pdctl.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "remove", "evict-leader-scheduler"}...)
c.Assert(err, IsNil)
c.Assert(strings.Contains(string(output), "Success!"), IsTrue)
output, err = pdctl.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "1"}...)
c.Assert(err, IsNil)
c.Assert(strings.Contains(string(output), "Success!"), IsTrue)
}

0 comments on commit 2da9d84

Please sign in to comment.