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

pd-ctl: escape the scheduler name for some commands #7799

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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