diff --git a/tools/pd-ctl/pdctl/command/config_command.go b/tools/pd-ctl/pdctl/command/config_command.go index 6176c64d116..1b4f87436d8 100644 --- a/tools/pd-ctl/pdctl/command/config_command.go +++ b/tools/pd-ctl/pdctl/command/config_command.go @@ -28,7 +28,6 @@ import ( "github.com/spf13/cobra" - sc "github.com/tikv/pd/pkg/schedule/config" "github.com/tikv/pd/pkg/schedule/placement" "github.com/tikv/pd/pkg/utils/apiutil" "github.com/tikv/pd/pkg/utils/reflectutil" @@ -374,13 +373,10 @@ func postConfigDataWithPath(cmd *cobra.Command, key, value, path string) error { if err != nil { val = value } - data[key] = val if key == "max-replicas" { - replica, err := strconv.ParseInt(value, 10, 64) - if err == nil && replica < sc.DefaultMaxReplicas { - cmd.Println("Setting max-replica to less than 3 may be a mistake and carries high risk. Please confirm the setting.") - } + checkMaxReplicas(cmd, val) } + data[key] = val reqData, err := json.Marshal(data) if err != nil { return err @@ -393,6 +389,30 @@ func postConfigDataWithPath(cmd *cobra.Command, key, value, path string) error { return nil } +func checkMaxReplicas(cmd *cobra.Command, value any) { + newReplica, ok := value.(float64) + if !ok { + // If the type is not float64, it will be handled elsewhere + return + } + 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 + } + 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 + } + 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()) diff --git a/tools/pd-ctl/tests/config/config_test.go b/tools/pd-ctl/tests/config/config_test.go index f0b02620fdd..b5123b1d785 100644 --- a/tools/pd-ctl/tests/config/config_test.go +++ b/tools/pd-ctl/tests/config/config_test.go @@ -1136,7 +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), "Setting max-replica to less than 3 may be a mistake and carries high risk. Please confirm the setting.") + 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) @@ -1157,7 +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), "Setting max-replica to less than 3 may be a mistake and carries high risk. Please confirm the setting.") + re.NotContains(string(output), "which is less than the current replicas") checkMaxReplicas(3) checkRuleCount(3)