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
85 changes: 83 additions & 2 deletions pdctl/command/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"

"github.com/spf13/cobra"
)

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 @@ -46,6 +49,17 @@ func NewShowConfigCommand() *cobra.Command {
Short: "show config of PD",
Run: showConfigCommandFunc,
}
sc.AddCommand(NewShowAllConfigCommand())
return sc
}

// NewShowAllConfigCommand return a show all subcommand of show subcommand
func NewShowAllConfigCommand() *cobra.Command {
sc := &cobra.Command{
Use: "all",
Short: "show all config of PD",
Run: showAllConfigCommandFunc,
}
return sc
}

Expand All @@ -56,6 +70,17 @@ func NewSetConfigCommand() *cobra.Command {
Short: "set the option with value",
Run: setConfigCommandFunc,
}
sc.AddCommand(NewSetReplicationCommand())
return sc
}

// NewSetReplicationCommand return a set replication subcommand of setCmd
func NewSetReplicationCommand() *cobra.Command {
sc := &cobra.Command{
Use: "replicate <option> <value>",
Short: "set the replication option with value",
Run: setReplicateConfigCommandFunc,
}
return sc
}

Expand All @@ -68,6 +93,15 @@ func showConfigCommandFunc(cmd *cobra.Command, args []string) {
fmt.Println(r)
}

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)
}

func setConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
fmt.Println(cmd.UsageString())
Expand Down Expand Up @@ -115,3 +149,50 @@ func setConfigCommandFunc(cmd *cobra.Command, args []string) {
printResponseError(r)
}
}

func setReplicateConfigCommandFunc(cmd *cobra.Command, args []string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use setConfigCommandFunc directly? I think the implementation code is duplicated here.

if len(args) != 2 {
fmt.Println(cmd.UsageString())
return
}

var value interface{}
data := make(map[string]interface{})
r, err := doRequest(cmd, replicatePrefix, http.MethodGet)
if err != nil {
fmt.Printf("Failed to set replication config:[%s]\n", err)
return
}
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.

err = json.Unmarshal([]byte(r), &data)
if err != nil {
fmt.Printf("Failed to set replication config:[%s]\n", err)
return
}
value, err = strconv.ParseFloat(args[1], 64)
if err != nil {
value = args[1]
if strings.Contains(args[1], ",") {
value = strings.Split(args[1], ",")
}
}
data[args[0]] = value
reqData, err := json.Marshal(&data)
if err != nil {
fmt.Printf("Failed to set replication config:[%s]\n", err)
return
}
url := getAddressFromCmd(cmd, replicatePrefix)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(reqData))
if err != nil {
fmt.Printf("Failed to set replication config:[%s]\n", err)
}
defer func() {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}()
if resp.StatusCode == http.StatusOK {
fmt.Println("Success!")
} else {
printResponseError(resp)
}
}