Skip to content

Commit

Permalink
config:add config for api ratelimit ref tikv#4207
Browse files Browse the repository at this point in the history
Signed-off-by: qidi1 <1083369179@qq.com>
  • Loading branch information
qidi1 committed Oct 25, 2021
1 parent fab46b3 commit ac322ae
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
5 changes: 4 additions & 1 deletion conf/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@
# metric-storage = ""
## There are some values supported: "auto", "none", or a specific address, default: "auto".
# dashboard-address = "auto"

## the max token store in bucket which use in limit api access rate
# api-bucket-capacity=20
## increase the number of tokens which use in limit api access rate per second
# api-bucket-rate= 3.0
[schedule]
## Controls the size limit of Region Merge.
# max-merge-region-size = 20
Expand Down
18 changes: 15 additions & 3 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ const (
)

var (
defaultEnableTelemetry = true
defaultRuntimeServices = []string{}
defaultLocationLabels = []string{}
defaultEnableTelemetry = true
defaultRuntimeServices = []string{}
defaultLocationLabels = []string{}
defalutAPIBucketCapacity = 20
defalutAPIBucketRate = 3.0
// DefaultStoreLimit is the default store limit of add peer and remove peer.
DefaultStoreLimit = StoreLimit{AddPeer: 15, RemovePeer: 15}
// DefaultTiFlashStoreLimit is the default TiFlash store limit of add peer and remove peer.
Expand Down Expand Up @@ -1081,6 +1083,10 @@ type PDServerConfig struct {
TraceRegionFlow bool `toml:"trace-region-flow" json:"trace-region-flow,string,omitempty"`
// FlowRoundByDigit used to discretization processing flow information.
FlowRoundByDigit int `toml:"flow-round-by-digit" json:"flow-round-by-digit"`
// the max token store in bucket which use in limit api access rate
APIBucketCapacity int64 `toml:"api-bucket-capacity" json:"api-bucket-capacity"`
// increase the number of tokens which use in limit api access rate per second
APIBucketRate float64 `toml:"api-bucket-rate" json:"api-bucket-rate"`
}

func (c *PDServerConfig) adjust(meta *configMetaData) error {
Expand All @@ -1103,6 +1109,12 @@ func (c *PDServerConfig) adjust(meta *configMetaData) error {
if !meta.IsDefined("flow-round-by-digit") {
adjustInt(&c.FlowRoundByDigit, defaultFlowRoundByDigit)
}
if !meta.IsDefined("api-bucket-capacity") {
adjustInt64(&c.APIBucketCapacity, int64(defalutAPIBucketCapacity))
}
if !meta.IsDefined("api-bucket-rate") {
adjustFloat64(&c.APIBucketRate, defalutAPIBucketRate)
}
c.migrateConfigurationFromFile(meta)
return c.Validate()
}
Expand Down
46 changes: 46 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,52 @@ wait-store-timeout = "120s"
c.Assert(cfg.ReplicationMode.ReplicationMode, Equals, "majority")
}

func (s *testConfigSuite) TestHotRegionConfig(c *C) {
cfgData := `
[schedule]
hot-regions-reserved-days= 30
hot-regions-write-interval= "30m"
`
cfg := NewConfig()
meta, err := toml.Decode(cfgData, &cfg)
c.Assert(err, IsNil)
err = cfg.Adjust(&meta, false)
c.Assert(err, IsNil)
c.Assert(cfg.Schedule.HotRegionsWriteInterval.Duration, Equals, time.Minute*30)
c.Assert(cfg.Schedule.HotRegionsResevervedDays, Equals, int64(30))

cfg = NewConfig()
meta, err = toml.Decode("", &cfg)
c.Assert(err, IsNil)
err = cfg.Adjust(&meta, false)
c.Assert(err, IsNil)
c.Assert(cfg.Schedule.HotRegionsWriteInterval.Duration, Equals, defaultHotRegionsWriteInterval)
c.Assert(cfg.Schedule.HotRegionsResevervedDays, Equals, int64(defaultHotRegionsResevervedDays))
}

func (s *testConfigSuite) TestApiRatelimitConfig(c *C) {
cfgData := `
[pd-server]
api-bucket-capacity=10
api-bucket-rate=4.0
`
cfg := NewConfig()
meta, err := toml.Decode(cfgData, &cfg)
c.Assert(err, IsNil)
err = cfg.Adjust(&meta, false)
c.Assert(err, IsNil)
c.Assert(cfg.PDServerCfg.APIBucketCapacity, Equals, int64(10))
c.Assert(cfg.PDServerCfg.APIBucketRate, Equals, 4.0)

cfg = NewConfig()
meta, err = toml.Decode("", &cfg)
c.Assert(err, IsNil)
err = cfg.Adjust(&meta, false)
c.Assert(err, IsNil)
c.Assert(cfg.PDServerCfg.APIBucketCapacity, Equals, int64(defalutAPIBucketCapacity))
c.Assert(cfg.PDServerCfg.APIBucketRate, Equals, defalutAPIBucketRate)
}

func (s *testConfigSuite) TestConfigClone(c *C) {
cfg := &Config{}
cfg.Adjust(nil, false)
Expand Down

0 comments on commit ac322ae

Please sign in to comment.