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: add config replicate and show all config #573

Merged
merged 13 commits into from
Mar 27, 2017
105 changes: 68 additions & 37 deletions pdctl/command/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import (
)

var (
configPrefix = "pd/api/v1/config"
schedulePrefix = "pd/api/v1/config/schedule"
configPrefix = "pd/api/v1/config"
schedulePrefix = "pd/api/v1/config/schedule"
replicatePrefix = "pd/api/v1/config/replicate"
)

// NewConfigCommand return a config subcommand of rootCmd
Expand All @@ -35,7 +36,8 @@ func NewConfigCommand() *cobra.Command {
Short: "tune pd configs",
}
conf.AddCommand(NewShowConfigCommand())
conf.AddCommand(NewSetConfigCommand())
conf.AddCommand(NewScheduleConfigCommand())
conf.AddCommand(NewReplicationConfigCommand())
return conf
}

Expand All @@ -46,15 +48,36 @@ func NewShowConfigCommand() *cobra.Command {
Short: "show config of PD",
Run: showConfigCommandFunc,
}
sc.AddCommand(NewShowAllConfigCommand())
return sc
}

// NewSetConfigCommand return a set subcommand of configCmd
func NewSetConfigCommand() *cobra.Command {
// NewShowAllConfigCommand return a show all subcommand of show subcommand
func NewShowAllConfigCommand() *cobra.Command {
sc := &cobra.Command{
Use: "set <option> <value>",
Short: "set the option with value",
Run: setConfigCommandFunc,
Use: "all",
Short: "show all config of PD",
Run: showAllConfigCommandFunc,
}
return sc
}

// NewScheduleConfigCommand return a set subcommand of configCmd
func NewScheduleConfigCommand() *cobra.Command {
sc := &cobra.Command{
Use: "schedule <option> <value>",
Short: "set the schedule option with value",
Run: setScheduleConfigCommandFunc,
}
return sc
}

// NewReplicationConfigCommand return a set subcommand of configCmd
func NewReplicationConfigCommand() *cobra.Command {
sc := &cobra.Command{
Use: "replication <option> <value>",
Short: "set the replication option with value",
Run: setReplicationConfigCommandFunc,
}
return sc
}
Expand All @@ -68,50 +91,58 @@ func showConfigCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println(r)
}

func setConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
func showAllConfigCommandFunc(cmd *cobra.Command, args []string) {
r, err := doRequest(cmd, configPrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to get config: %s", err)
return
}
fmt.Println(r)
}

url := getAddressFromCmd(cmd, schedulePrefix)
func postDataWithPath(cmd *cobra.Command, args []string, path string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If args len must be 2, use key string, value string instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/postDataWithPath/postConfigDataWithPath/s

opt, val := args[0], args[1]
var value interface{}
data := make(map[string]interface{})

r, err := http.Get(url)
value, err := strconv.ParseFloat(val, 64)
if err != nil {
fmt.Printf("Failed to set config:[%s]\n", err)
return
value = val
}
if r.StatusCode != http.StatusOK {
printResponseError(r)
r.Body.Close()
return
data[opt] = value
reqData, err := json.Marshal(data)
req, err := getRequest(cmd, path, http.MethodPost, "application/json", bytes.NewBuffer(reqData))
if err != nil {
return err
}

json.NewDecoder(r.Body).Decode(&data)
r.Body.Close()
value, err = strconv.ParseFloat(args[1], 64)
_, err = dail(req)
if err != nil {
value = args[1]
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use schedulePrefix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the API setSchedule and getSchedule are not consistent. considering compatible I just adjust it in here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok to give up supporting old pd-server.

data[args[0]] = value
return nil
}

req, err := json.Marshal(data)
func setScheduleConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
return
}
err := postDataWithPath(cmd, args, schedulePrefix)
if err != nil {
fmt.Printf("Failed to set config:[%s]\n", err)
fmt.Printf("Failed to set config: %s", err)
return
}
fmt.Println("Success!")
}

url = getAddressFromCmd(cmd, configPrefix)
r, err = http.Post(url, "application/json", bytes.NewBuffer(req))
if err != nil {
fmt.Printf("Failed to set config:[%s]\n", err)
func setReplicationConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
return
}
defer r.Body.Close()
if r.StatusCode == http.StatusOK {
fmt.Println("Success!")
} else {
printResponseError(r)
err := postDataWithPath(cmd, args, replicatePrefix)
if err != nil {
fmt.Printf("Failed to set config: %s", err)
return
}
fmt.Println("Success!")
}
21 changes: 17 additions & 4 deletions pdctl/command/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,21 @@ var (
errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it")
)

func doRequest(cmd *cobra.Command, prefix string, method string) (string, error) {
var res string
func getRequest(cmd *cobra.Command, prefix string, method string, bodyType string, body io.Reader) (*http.Request, error) {
if method == "" {
method = http.MethodGet
}
url := getAddressFromCmd(cmd, prefix)
req, err := http.NewRequest(method, url, nil)
req, err := http.NewRequest(method, url, body)
if err != nil {
return res, err
return nil, err
}
req.Header.Set("Content-Type", bodyType)
return req, err
}

func dail(req *http.Request) (string, error) {
var res string
reps, err := dailClient.Do(req)
if err != nil {
return res, err
Expand All @@ -64,6 +69,14 @@ func doRequest(cmd *cobra.Command, prefix string, method string) (string, error)
return res, nil
}

func doRequest(cmd *cobra.Command, prefix string, method string) (string, error) {
req, err := getRequest(cmd, prefix, method, "", nil)
if err != nil {
return "", err
}
return dail(req)
}

func genResponseError(r *http.Response) error {
res, _ := ioutil.ReadAll(r.Body)
return errors.Errorf("[%d] %s", r.StatusCode, res)
Expand Down