-
Notifications
You must be signed in to change notification settings - Fork 725
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
*: reset config if the input is invalid #8632
Changes from 7 commits
4511fe9
922e1d0
f273a7b
e2ccf3f
fce9f6e
4a9aab0
f8ff32c
114aa19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -103,6 +103,13 @@ | |
return false, errs.ErrScheduleConfigNotExist.FastGenByArgs() | ||
} | ||
|
||
func (conf *evictLeaderSchedulerConfig) removeStore(id uint64) { | ||
conf.Lock() | ||
defer conf.Unlock() | ||
// if the store is not existed, no need to resume leader transfer | ||
_, _ = conf.removeStoreLocked(id) | ||
} | ||
|
||
func (conf *evictLeaderSchedulerConfig) resetStoreLocked(id uint64, keyRange []core.KeyRange) { | ||
if err := conf.cluster.PauseLeaderTransfer(id); err != nil { | ||
log.Error("pause leader transfer failed", zap.Uint64("store-id", id), errs.ZapError(err)) | ||
|
@@ -408,6 +415,7 @@ | |
batchFloat, ok := input["batch"].(float64) | ||
if ok { | ||
if batchFloat < 1 || batchFloat > 10 { | ||
handler.config.removeStore(id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about adding a defer after L411? For now, it is so easy to forget to defer func () {
if err != nil {
....
}
}() |
||
handler.rd.JSON(w, http.StatusBadRequest, "batch is invalid, it should be in [1, 10]") | ||
return | ||
} | ||
|
@@ -417,6 +425,7 @@ | |
ranges, ok := (input["ranges"]).([]string) | ||
if ok { | ||
if !inputHasStoreID { | ||
handler.config.removeStore(id) | ||
handler.rd.JSON(w, http.StatusInternalServerError, errs.ErrSchedulerConfig.FastGenByArgs("id")) | ||
return | ||
} | ||
|
@@ -426,6 +435,7 @@ | |
|
||
newRanges, err = getKeyRanges(ranges) | ||
if err != nil { | ||
handler.config.removeStore(id) | ||
handler.rd.JSON(w, http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -813,6 +813,60 @@ func (suite *schedulerTestSuite) checkSchedulerDiagnostic(cluster *pdTests.TestC | |
checkSchedulerDescribeCommand("balance-leader-scheduler", "normal", "") | ||
} | ||
|
||
func (suite *schedulerTestSuite) TestEvictLeaderScheduler() { | ||
// FIXME: API mode may have the problem | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will we fix it in another PR? |
||
suite.env.RunTestInPDMode(suite.checkEvictLeaderScheduler) | ||
} | ||
|
||
func (suite *schedulerTestSuite) checkEvictLeaderScheduler(cluster *pdTests.TestCluster) { | ||
re := suite.Require() | ||
pdAddr := cluster.GetConfig().GetClientURL() | ||
cmd := ctl.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(), | ||
}, | ||
} | ||
for _, store := range stores { | ||
pdTests.MustPutStore(re, cluster, store) | ||
} | ||
|
||
pdTests.MustPutRegion(re, cluster, 1, 1, []byte("a"), []byte("b")) | ||
output, err := tests.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "2"}...) | ||
re.NoError(err) | ||
re.Contains(string(output), "Success!") | ||
output, err = tests.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "1"}...) | ||
re.NoError(err) | ||
re.Contains(string(output), "Success!") | ||
output, err = tests.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "remove", "evict-leader-scheduler"}...) | ||
re.NoError(err) | ||
re.Contains(string(output), "Success!") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we need to check
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seem not work |
||
output, err = tests.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "add", "evict-leader-scheduler", "1"}...) | ||
re.NoError(err) | ||
re.Contains(string(output), "Success!") | ||
output, err = tests.ExecuteCommand(cmd, []string{"-u", pdAddr, "scheduler", "remove", "evict-leader-scheduler-1"}...) | ||
re.NoError(err) | ||
re.Contains(string(output), "Success!") | ||
} | ||
|
||
func mustExec(re *require.Assertions, cmd *cobra.Command, args []string, v any) string { | ||
output, err := tests.ExecuteCommand(cmd, args...) | ||
re.NoError(err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For invalid config parameters, It's better to validate it, do nothing, and return 4xx + error message to let the users know it, it's not classic and unsafe to reset memory content when meeting invalid inputs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a rollback operation since the memory state has been changed in line 407.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, make sense