Skip to content

Commit

Permalink
Escape the scheduler name for some commands
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Feb 4, 2024
1 parent 54ffd34 commit 9d20f2d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
13 changes: 10 additions & 3 deletions tools/pd-ctl/pdctl/command/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,18 @@ func pauseSchedulerCommandFunc(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
input := map[string]any{"delay": delay}
postJSON(cmd, path, input)
}

// Since certain scheduler's name is defined by caller such as scatter-range,
// it's possible the name contains special characters, like "#", "&" and so on.
// So we need to escape the scheduler name here before attaching it to the URL.
func getEscapedSchedulerName(schedulerName string) string {
return url.PathEscape(schedulerName)
}

// NewResumeSchedulerCommand returns a command to resume a scheduler.
func NewResumeSchedulerCommand() *cobra.Command {
c := &cobra.Command{
Expand All @@ -92,7 +99,7 @@ func resumeSchedulerCommandFunc(cmd *cobra.Command, args []string) {
cmd.Usage()
return
}
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
input := map[string]any{"delay": 0}
postJSON(cmd, path, input)
}
Expand Down Expand Up @@ -475,7 +482,7 @@ func removeSchedulerCommandFunc(cmd *cobra.Command, args []string) {
case strings.HasPrefix(args[0], grantLeaderSchedulerName) && args[0] != grantLeaderSchedulerName:
redirectRemoveSchedulerToDeleteConfig(cmd, grantLeaderSchedulerName, args)
default:
path := schedulersPrefix + "/" + args[0]
path := schedulersPrefix + "/" + getEscapedSchedulerName(args[0])
_, err := doRequest(cmd, path, http.MethodDelete, http.Header{})
if err != nil {
cmd.Println(err)
Expand Down
27 changes: 27 additions & 0 deletions tools/pd-ctl/tests/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,33 @@ func (suite *schedulerTestSuite) checkScheduler(cluster *pdTests.TestCluster) {
})
}

// test scatter range scheduler
for _, name := range []string{"test", "test#", "tes&t=", "?test"} {
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "add", "scatter-range", "--format=raw", "a", "b", name}, nil)
re.Contains(echo, "Success!")
schedulerName := fmt.Sprintf("scatter-range-%s", name)
// test show scheduler
testutil.Eventually(re, func() bool {
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "show"}, nil)
return strings.Contains(echo, schedulerName)
})
// test pause scheduler
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "pause", schedulerName, "60"}, nil)
re.Contains(echo, "Success!")
checkSchedulerWithStatusCommand("paused", []string{schedulerName})
// test resume scheduler
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "resume", schedulerName}, nil)
re.Contains(echo, "Success!")
checkSchedulerWithStatusCommand("paused", []string{})
// test remove scheduler
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "remove", schedulerName}, nil)
re.Contains(echo, "Success!")
testutil.Eventually(re, func() bool {
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "show"}, nil)
return !strings.Contains(echo, schedulerName)
})
}

mustUsage([]string{"-u", pdAddr, "scheduler", "pause", "balance-leader-scheduler"})
echo = mustExec(re, cmd, []string{"-u", pdAddr, "scheduler", "pause", "balance-leader-scheduler", "60"}, nil)
re.Contains(echo, "Success!")
Expand Down

0 comments on commit 9d20f2d

Please sign in to comment.