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: print warn information when set max-replica to less than current #8983

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tools/pd-ctl/pdctl/command/config_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,9 @@
if err != nil {
val = value
}
if key == "max-replicas" {
checkMaxReplicas(cmd, val)
}
data[key] = val
reqData, err := json.Marshal(data)
if err != nil {
Expand All @@ -386,6 +389,30 @@
return nil
}

func checkMaxReplicas(cmd *cobra.Command, value any) {
newReplica, ok := value.(float64)
lhy1024 marked this conversation as resolved.
Show resolved Hide resolved
if !ok {
// If the type is not float64, it will be handled elsewhere
return
}

Check warning on line 397 in tools/pd-ctl/pdctl/command/config_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/config_command.go#L395-L397

Added lines #L395 - L397 were not covered by tests
header := buildHeader(cmd)
r, err := doRequest(cmd, replicatePrefix, http.MethodGet, header)
if err != nil {
cmd.Printf("Failed to get config when checking config: %s\n", err)
return
}

Check warning on line 403 in tools/pd-ctl/pdctl/command/config_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/config_command.go#L401-L403

Added lines #L401 - L403 were not covered by tests
oldConfig := make(map[string]any)
err = json.Unmarshal([]byte(r), &oldConfig)
if err != nil {
cmd.Printf("Failed to unmarshal config when checking config: %s\n", err)
return
}

Check warning on line 409 in tools/pd-ctl/pdctl/command/config_command.go

View check run for this annotation

Codecov / codecov/patch

tools/pd-ctl/pdctl/command/config_command.go#L407-L409

Added lines #L407 - L409 were not covered by tests
oldReplica, ok := oldConfig["max-replicas"].(float64)
if ok && newReplica < oldReplica {
cmd.Printf("Setting max-replica to %v which is less than the current replicas (%v). This may pose a risk. Please confirm the setting.\n", newReplica, oldReplica)
}
}

func setConfigCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 2 {
cmd.Println(cmd.UsageString())
Expand Down
2 changes: 2 additions & 0 deletions tools/pd-ctl/tests/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,7 @@ func (suite *configTestSuite) checkUpdateDefaultReplicaConfig(cluster *pdTests.T
output, err := tests.ExecuteCommand(cmd, "-u", pdAddr, "config", "set", "max-replicas", "2")
re.NoError(err)
re.Contains(string(output), "Success!")
re.Contains(string(output), "which is less than the current replicas")
checkMaxReplicas(2)
output, err = tests.ExecuteCommand(cmd, "-u", pdAddr, "config", "set", "location-labels", "zone,host")
re.NoError(err)
Expand All @@ -1156,6 +1157,7 @@ func (suite *configTestSuite) checkUpdateDefaultReplicaConfig(cluster *pdTests.T
output, err = tests.ExecuteCommand(cmd, "-u", pdAddr, "config", "set", "max-replicas", "3")
re.NoError(err)
re.Contains(string(output), "Success!")
re.NotContains(string(output), "which is less than the current replicas")
checkMaxReplicas(3)
checkRuleCount(3)

Expand Down
Loading