-
Notifications
You must be signed in to change notification settings - Fork 726
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
Changes from 8 commits
22dd34d
c930dc8
dfc6089
42bd63b
44bf81c
85a93a1
956e1b1
c0ff6f5
66abfb5
55de956
0f38dfe
585673e
a97ec1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
} | ||
|
@@ -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 { | ||
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 | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not use schedulePrefix? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!") | ||
} |
There was a problem hiding this comment.
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
insteadThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/postDataWithPath/postConfigDataWithPath/s