diff --git a/tests/pdctl/config/config_test.go b/tests/pdctl/config/config_test.go index a1319fc9530b..b708abcabd50 100644 --- a/tests/pdctl/config/config_test.go +++ b/tests/pdctl/config/config_test.go @@ -15,6 +15,7 @@ package config_test import ( "encoding/json" + "strings" "testing" "github.com/coreos/go-semver/semver" @@ -96,6 +97,10 @@ func (s *configTestSuite) TestConfig(c *C) { c.Assert(json.Unmarshal(output, &clusterVersion), IsNil) c.Assert(clusterVersion, DeepEquals, svr.GetClusterVersion()) + args2 = []string{"-u", pdAddr, "config", "set", "namespace", "ts1", "region-schedule-limit", "128"} + _, output, err = pdctl.ExecuteCommandC(cmd, args2...) + c.Assert(err, IsNil) + c.Assert(strings.Contains(string(output), "Failed"), IsTrue) // config show namespace && config set namespace args = []string{"-u", pdAddr, "table_ns", "create", "ts1"} _, _, err = pdctl.ExecuteCommandC(cmd, args...) diff --git a/tools/pd-ctl/pdctl/command/config_command.go b/tools/pd-ctl/pdctl/command/config_command.go index 6cf2505a5758..a58e356995b7 100644 --- a/tools/pd-ctl/pdctl/command/config_command.go +++ b/tools/pd-ctl/pdctl/command/config_command.go @@ -301,7 +301,7 @@ func setNamespaceConfigCommandFunc(cmd *cobra.Command, args []string) { prefix := path.Join(namespacePrefix, name) err := postConfigDataWithPath(cmd, opt, val, prefix) if err != nil { - cmd.Printf("Failed to set namespace:%s config: %s\n", name, err) + cmd.Printf("Failed to set namespace: %s error: %s\n", name, err) return } cmd.Println("Success!") diff --git a/tools/pd-ctl/pdctl/command/global.go b/tools/pd-ctl/pdctl/command/global.go index 733455258fe6..e1ee87734b5a 100644 --- a/tools/pd-ctl/pdctl/command/global.go +++ b/tools/pd-ctl/pdctl/command/global.go @@ -77,8 +77,10 @@ func doRequest(cmd *cobra.Command, prefix string, method string, o(b) } var resp string - var err error - tryURLs(cmd, func(endpoint string) error { + + endpoints := getEndpoints(cmd) + err := tryURLs(cmd, endpoints, func(endpoint string) error { + var err error url := endpoint + "/" + prefix if method == "" { method = http.MethodGet @@ -113,7 +115,7 @@ func dail(req *http.Request) (string, error) { if err != nil { return "", err } - return fmt.Sprintf("[%d] %s", resp.StatusCode, msg), nil + return "", errors.Errorf("[%d] %s", resp.StatusCode, msg) } content, err := ioutil.ReadAll(resp.Body) @@ -128,13 +130,8 @@ type DoFunc func(endpoint string) error // tryURLs issues requests to each URL and tries next one if there // is an error -func tryURLs(cmd *cobra.Command, f DoFunc) { - addrs, err := cmd.Flags().GetString("pd") - if err != nil { - cmd.Println("get pd address failed, should set flag with '-u'") - os.Exit(1) - } - endpoints := strings.Split(addrs, ",") +func tryURLs(cmd *cobra.Command, endpoints []string, f DoFunc) error { + var err error for _, endpoint := range endpoints { var u *url.URL u, err = url.Parse(endpoint) @@ -156,9 +153,19 @@ func tryURLs(cmd *cobra.Command, f DoFunc) { } break } + if len(endpoints) > 1 && err != nil { + err = errors.Errorf("after trying all endpoints, no endpoint is available, the last error we met: %s", err) + } + return err +} + +func getEndpoints(cmd *cobra.Command) []string { + addrs, err := cmd.Flags().GetString("pd") if err != nil { - cmd.Println("after trying all endpoints, no endpoint is available, the last error we met:", err) + cmd.Println("get pd address failed, should set flag with '-u'") + os.Exit(1) } + return strings.Split(addrs, ",") } func printResponseError(r *http.Response) error { @@ -174,7 +181,8 @@ func postJSON(cmd *cobra.Command, prefix string, input map[string]interface{}) { return } - tryURLs(cmd, func(endpoint string) error { + endpoints := getEndpoints(cmd) + tryURLs(cmd, endpoints, func(endpoint string) error { url := endpoint + "/" + prefix r, err := dialClient.Post(url, "application/json", bytes.NewBuffer(data)) if err != nil {