Skip to content

Commit 47a5532

Browse files
committed
address comment
Signed-off-by: bufferflies <1045931706@qq.com>
1 parent 011fe79 commit 47a5532

File tree

4 files changed

+61
-7
lines changed

4 files changed

+61
-7
lines changed

pkg/schedule/schedulers/balance_range_test.go

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,24 @@ func TestAddBalanceRangeJobWithInvalidFieldType(t *testing.T) {
533533
config: conf,
534534
rd: render.New(render.Options{IndentJSON: true}),
535535
}
536+
count := 0
537+
checkFn := func(data []byte, pass bool) {
538+
req := httptest.NewRequest(http.MethodPut, "/job", bytes.NewReader(data))
539+
resp := httptest.NewRecorder()
540+
re.NotPanics(func() {
541+
handler.addJob(resp, req)
542+
})
543+
if pass {
544+
re.Equal(http.StatusOK, resp.Code)
545+
count++
546+
re.Len(conf.jobs, count)
547+
} else {
548+
re.Equal(http.StatusBadRequest, resp.Code)
549+
}
550+
re.Len(conf.jobs, count)
551+
}
552+
553+
// invalid engine type
536554
body, err := json.Marshal(map[string]any{
537555
"alias": "a",
538556
"engine": 1,
@@ -541,11 +559,41 @@ func TestAddBalanceRangeJobWithInvalidFieldType(t *testing.T) {
541559
"end-key": "200",
542560
})
543561
re.NoError(err)
544-
req := httptest.NewRequest(http.MethodPut, "/job", bytes.NewReader(body))
545-
resp := httptest.NewRecorder()
546-
re.NotPanics(func() {
547-
handler.addJob(resp, req)
562+
checkFn(body, false)
563+
564+
// invalid timeout type
565+
body, err = json.Marshal(map[string]any{
566+
"alias": "a",
567+
"engine": "tikv",
568+
"rule": "leader-scatter",
569+
"start-key": "100",
570+
"end-key": "200",
571+
"timeout": "123",
548572
})
549-
re.Equal(http.StatusBadRequest, resp.Code)
550-
re.Empty(conf.jobs)
573+
re.NoError(err)
574+
checkFn(body, false)
575+
576+
// normal case
577+
body, err = json.Marshal(map[string]any{
578+
"alias": "a",
579+
"engine": "tikv",
580+
"rule": "leader-scatter",
581+
"start-key": "100",
582+
"end-key": "200",
583+
"timeout": "123s",
584+
})
585+
re.NoError(err)
586+
checkFn(body, true)
587+
588+
// invalidate case
589+
body, err = json.Marshal(map[string]any{
590+
"alias": "a",
591+
"engine": "tikv",
592+
"rule": "leader-scatter",
593+
"start-key": "100",
594+
"end-key": "200",
595+
"timeout": "0s",
596+
})
597+
re.NoError(err)
598+
checkFn(body, false)
551599
}

pkg/schedule/schedulers/init.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,9 @@ func schedulersRegister() {
593593
if err != nil {
594594
return errs.ErrURLParse.Wrap(err)
595595
}
596+
if duration <= 0 {
597+
return errs.ErrURLParse.FastGenByArgs("timeout must be greater than 0")
598+
}
596599
alias, err := url.QueryUnescape(args[3])
597600
if err != nil {
598601
return errs.ErrURLParse.Wrap(err)

server/api/scheduler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func (h *schedulerHandler) CreateScheduler(w http.ResponseWriter, r *http.Reques
249249
}
250250

251251
if err := h.AddScheduler(tp, args...); err != nil {
252-
h.r.JSON(w, http.StatusInternalServerError, err.Error())
252+
h.r.JSON(w, http.StatusBadRequest, err.Error())
253253
return
254254
}
255255

tools/pd-ctl/tests/scheduler/scheduler_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ func (suite *schedulerTestSuite) checkSchedulerConfig(cluster *pdTests.TestClust
522522
re.Equal(core.HexRegionKeyStr([]byte("a")), ranges["start-key"])
523523
re.Equal(core.HexRegionKeyStr([]byte("b")), ranges["end-key"])
524524

525+
echo = tests.MustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "add", "balance-range-scheduler", "--format=raw", "tiflash", "learner-scatter", "learner", "a", "b", "timeout", "0s"}, nil)
526+
re.NotContains(echo, "Success!")
527+
525528
echo = tests.MustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "add", "balance-range-scheduler", "--format=raw", "tiflash", "learner-scatter", "learner", "a", "b"}, nil)
526529
re.Contains(echo, "Success!")
527530
testutil.Eventually(re, func() bool {

0 commit comments

Comments
 (0)