diff --git a/conf/config.toml b/conf/config.toml index 5abde1ae861..03355c83187 100644 --- a/conf/config.toml +++ b/conf/config.toml @@ -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 diff --git a/server/config/config.go b/server/config/config.go index c76bb0ee94f..eca08eac644 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -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. @@ -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 { @@ -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() } diff --git a/server/config/config_test.go b/server/config/config_test.go index db78722da4f..dc0f6c13cae 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -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)