Skip to content

Commit

Permalink
json unmarshal will convert number to float64
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Mar 26, 2024
1 parent 39108f7 commit 7b321a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
3 changes: 3 additions & 0 deletions cmd/pd-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ func start(cmd *cobra.Command, args []string, services ...string) {
err := cfg.Parse(flagSet)
defer logutil.LogPanic()

// Check the PD version first before running.
server.CheckPDVersion(cfg.ClusterVersion)

if err != nil {
cmd.Println(err)
return
Expand Down
49 changes: 31 additions & 18 deletions server/config/persist_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,29 +257,33 @@ func IsSupportedTTLConfig(key string) bool {

// GetMaxSnapshotCount returns the number of the max snapshot which is allowed to send.
func (o *PersistOptions) GetMaxSnapshotCount() uint64 {
return o.getTTLUintOr(sc.MaxSnapshotCountKey, o.GetScheduleConfig().MaxSnapshotCount)
return o.getTTLNumberOr(sc.MaxSnapshotCountKey, o.GetScheduleConfig().MaxSnapshotCount).(uint64)
}

// GetMaxPendingPeerCount returns the number of the max pending peers.
func (o *PersistOptions) GetMaxPendingPeerCount() uint64 {
return o.getTTLUintOr(sc.MaxPendingPeerCountKey, o.GetScheduleConfig().MaxPendingPeerCount)
return o.getTTLNumberOr(sc.MaxPendingPeerCountKey, o.GetScheduleConfig().MaxPendingPeerCount).(uint64)
}

// GetMaxMergeRegionSize returns the max region size.
func (o *PersistOptions) GetMaxMergeRegionSize() uint64 {
return o.getTTLUintOr(sc.MaxMergeRegionSizeKey, o.GetScheduleConfig().MaxMergeRegionSize)
return o.getTTLNumberOr(sc.MaxMergeRegionSizeKey, o.GetScheduleConfig().MaxMergeRegionSize).(uint64)
}

// GetMaxMergeRegionKeys returns the max number of keys.
// It returns size * 10000 if the key of max-merge-region-Keys doesn't exist.
func (o *PersistOptions) GetMaxMergeRegionKeys() uint64 {
keys, exist, err := o.getTTLUint(sc.MaxMergeRegionKeysKey)
keys, exist, err := o.getTTLNumber(sc.MaxMergeRegionKeysKey)
if exist && err == nil {
return keys
if keys, ok := keys.(uint64); ok {
return keys
}
}
size, exist, err := o.getTTLUint(sc.MaxMergeRegionSizeKey)
size, exist, err := o.getTTLNumber(sc.MaxMergeRegionSizeKey)
if exist && err == nil {
return size * 10000
if size, ok := size.(uint64); ok {
return size * 10000
}
}
return o.GetScheduleConfig().GetMaxMergeRegionKeys()
}
Expand Down Expand Up @@ -419,32 +423,32 @@ func (o *PersistOptions) GetMaxStorePreparingTime() time.Duration {

// GetLeaderScheduleLimit returns the limit for leader schedule.
func (o *PersistOptions) GetLeaderScheduleLimit() uint64 {
return o.getTTLUintOr(sc.LeaderScheduleLimitKey, o.GetScheduleConfig().LeaderScheduleLimit)
return o.getTTLNumberOr(sc.LeaderScheduleLimitKey, o.GetScheduleConfig().LeaderScheduleLimit).(uint64)
}

// GetRegionScheduleLimit returns the limit for region schedule.
func (o *PersistOptions) GetRegionScheduleLimit() uint64 {
return o.getTTLUintOr(sc.RegionScheduleLimitKey, o.GetScheduleConfig().RegionScheduleLimit)
return o.getTTLNumberOr(sc.RegionScheduleLimitKey, o.GetScheduleConfig().RegionScheduleLimit).(uint64)
}

// GetWitnessScheduleLimit returns the limit for region schedule.
func (o *PersistOptions) GetWitnessScheduleLimit() uint64 {
return o.getTTLUintOr(sc.WitnessScheduleLimitKey, o.GetScheduleConfig().WitnessScheduleLimit)
return o.getTTLNumberOr(sc.WitnessScheduleLimitKey, o.GetScheduleConfig().WitnessScheduleLimit).(uint64)
}

// GetReplicaScheduleLimit returns the limit for replica schedule.
func (o *PersistOptions) GetReplicaScheduleLimit() uint64 {
return o.getTTLUintOr(sc.ReplicaRescheduleLimitKey, o.GetScheduleConfig().ReplicaScheduleLimit)
return o.getTTLNumberOr(sc.ReplicaRescheduleLimitKey, o.GetScheduleConfig().ReplicaScheduleLimit).(uint64)
}

// GetMergeScheduleLimit returns the limit for merge schedule.
func (o *PersistOptions) GetMergeScheduleLimit() uint64 {
return o.getTTLUintOr(sc.MergeScheduleLimitKey, o.GetScheduleConfig().MergeScheduleLimit)
return o.getTTLNumberOr(sc.MergeScheduleLimitKey, o.GetScheduleConfig().MergeScheduleLimit).(uint64)
}

// GetHotRegionScheduleLimit returns the limit for hot region schedule.
func (o *PersistOptions) GetHotRegionScheduleLimit() uint64 {
return o.getTTLUintOr(sc.HotRegionScheduleLimitKey, o.GetScheduleConfig().HotRegionScheduleLimit)
return o.getTTLNumberOr(sc.HotRegionScheduleLimitKey, o.GetScheduleConfig().HotRegionScheduleLimit).(uint64)
}

// GetStoreLimit returns the limit of a store.
Expand Down Expand Up @@ -547,7 +551,7 @@ func (o *PersistOptions) GetRegionScoreFormulaVersion() string {

// GetSchedulerMaxWaitingOperator returns the number of the max waiting operators.
func (o *PersistOptions) GetSchedulerMaxWaitingOperator() uint64 {
return o.getTTLUintOr(sc.SchedulerMaxWaitingOperatorKey, o.GetScheduleConfig().SchedulerMaxWaitingOperator)
return o.getTTLNumberOr(sc.SchedulerMaxWaitingOperatorKey, o.GetScheduleConfig().SchedulerMaxWaitingOperator).(uint64)
}

// GetLeaderSchedulePolicy is to get leader schedule policy.
Expand Down Expand Up @@ -870,17 +874,26 @@ func (o *PersistOptions) SetTTLData(parCtx context.Context, client *clientv3.Cli
return nil
}

func (o *PersistOptions) getTTLUint(key string) (uint64, bool, error) {
func (o *PersistOptions) getTTLNumber(key string) (interface{}, bool, error) {
stringForm, ok := o.GetTTLData(key)
if !ok {
return 0, false, nil
}
r, err := strconv.ParseUint(stringForm, 10, 64)
return r, true, err
if err == nil {
return r, true, nil
}
// try to parse float64
// json unmarshal will convert number(such as `uint64(math.MaxInt32)`) to float64
f, err := strconv.ParseFloat(stringForm, 64)
if err != nil {
return 0, false, err
}
return f, true, nil
}

func (o *PersistOptions) getTTLUintOr(key string, defaultValue uint64) uint64 {
if v, ok, err := o.getTTLUint(key); ok {
func (o *PersistOptions) getTTLNumberOr(key string, defaultValue interface{}) interface{} {
if v, ok, err := o.getTTLNumber(key); ok {
if err == nil {
return v
}
Expand Down
15 changes: 15 additions & 0 deletions tests/integrations/client/http_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ func (suite *httpClientTestSuite) checkConfig(mode mode, client pd.Client) {
resp, err = env.cluster.GetEtcdClient().Get(env.ctx, sc.TTLConfigPrefix+"/schedule.leader-schedule-limit")
re.NoError(err)
re.Empty(resp.Kvs)

// Test the config with TTL for storing float64 as uint64.
newConfig = map[string]any{
"schedule.max-pending-peer-count": uint64(math.MaxInt32),
}
err = client.SetConfig(env.ctx, newConfig, 4)
re.NoError(err)
c := env.cluster.GetLeaderServer().GetRaftCluster().GetOpts().GetMaxPendingPeerCount()
re.Equal(uint64(math.MaxInt32), c)

err = client.SetConfig(env.ctx, newConfig, 0)
re.NoError(err)
resp, err = env.cluster.GetEtcdClient().Get(env.ctx, sc.TTLConfigPrefix+"/schedule.max-pending-peer-count")
re.NoError(err)
re.Empty(resp.Kvs)
}

func (suite *httpClientTestSuite) TestScheduleConfig() {
Expand Down

0 comments on commit 7b321a5

Please sign in to comment.