From effd07dac495285064a2ac5183eb60381d89c558 Mon Sep 17 00:00:00 2001 From: pingcap-github-bot Date: Mon, 21 Oct 2019 20:18:50 +0800 Subject: [PATCH] store/tikv: make cache ttl configurable (#12683) (#12758) --- config/config.go | 5 +++++ config/config.toml.example | 4 ++++ config/config_test.go | 3 +++ store/tikv/region_cache.go | 10 ++++++---- tidb-server/main.go | 1 + 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 6c22db2d3b8ca..854546efda698 100644 --- a/config/config.go +++ b/config/config.go @@ -276,6 +276,9 @@ type TiKVClient struct { MaxBatchWaitTime time.Duration `toml:"max-batch-wait-time" json:"max-batch-wait-time"` // BatchWaitSize is the max wait size for batch. BatchWaitSize uint `toml:"batch-wait-size" json:"batch-wait-size"` + // If a Region has not been accessed for more than the given duration (in seconds), it + // will be reloaded from the PD. + RegionCacheTTL uint `toml:"region-cache-ttl" json:"region-cache-ttl"` } // Binlog is the config for binlog. @@ -395,6 +398,8 @@ var defaultConf = Config{ OverloadThreshold: 200, MaxBatchWaitTime: 0, BatchWaitSize: 8, + + RegionCacheTTL: 600, }, Binlog: Binlog{ WriteTimeout: "15s", diff --git a/config/config.toml.example b/config/config.toml.example index ce7366a302b92..fbd375955719c 100644 --- a/config/config.toml.example +++ b/config/config.toml.example @@ -267,6 +267,10 @@ max-batch-wait-time = 0 # Batch wait size, to avoid waiting too long. batch-wait-size = 8 +# If a Region has not been accessed for more than the given duration (in seconds), it +# will be reloaded from the PD. +region-cache-ttl = 600 + [txn-local-latches] # Enable local latches for transactions. Enable it when # there are lots of conflicts between transactions. diff --git a/config/config_test.go b/config/config_test.go index 819212c9ad692..8d5d166e936f2 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -39,6 +39,7 @@ func (s *testConfigSuite) TestConfig(c *C) { conf.Binlog.IgnoreError = true conf.Binlog.Strategy = "hash" conf.TiKVClient.CommitTimeout = "10s" + conf.TiKVClient.RegionCacheTTL = 600 configFile := "config.toml" _, localFile, _, _ := runtime.Caller(0) configFile = path.Join(path.Dir(localFile), configFile) @@ -65,6 +66,7 @@ split-region-max-num=10000 [tikv-client] commit-timeout="41s" max-batch-size=128 +region-cache-ttl=6000 [stmt-summary] max-stmt-count=1000 max-sql-length=1024 @@ -81,6 +83,7 @@ max-sql-length=1024 c.Assert(conf.TiKVClient.CommitTimeout, Equals, "41s") c.Assert(conf.TiKVClient.MaxBatchSize, Equals, uint(128)) + c.Assert(conf.TiKVClient.RegionCacheTTL, Equals, uint(6000)) c.Assert(conf.TokenLimit, Equals, uint(1000)) c.Assert(conf.SplitRegionMaxNum, Equals, uint64(10000)) c.Assert(conf.StmtSummary.MaxStmtCount, Equals, uint(1000)) diff --git a/store/tikv/region_cache.go b/store/tikv/region_cache.go index 7b94edd3d8e87..8407ac2fa5120 100644 --- a/store/tikv/region_cache.go +++ b/store/tikv/region_cache.go @@ -34,11 +34,13 @@ import ( ) const ( - btreeDegree = 32 - rcDefaultRegionCacheTTLSec = 600 - invalidatedLastAccessTime = -1 + btreeDegree = 32 + invalidatedLastAccessTime = -1 ) +// RegionCacheTTLSec is the max idle time for regions in the region cache. +var RegionCacheTTLSec int64 = 600 + var ( tikvRegionCacheCounterWithInvalidateRegionFromCacheOK = metrics.TiKVRegionCacheCounter.WithLabelValues("invalidate_region_from_cache", "ok") tikvRegionCacheCounterWithSendFail = metrics.TiKVRegionCacheCounter.WithLabelValues("send_fail", "ok") @@ -142,7 +144,7 @@ func (r *Region) compareAndSwapStore(oldStore, newStore *RegionStore) bool { func (r *Region) checkRegionCacheTTL(ts int64) bool { for { lastAccess := atomic.LoadInt64(&r.lastAccess) - if ts-lastAccess > rcDefaultRegionCacheTTLSec { + if ts-lastAccess > RegionCacheTTLSec { return false } if atomic.CompareAndSwapInt64(&r.lastAccess, lastAccess, ts) { diff --git a/tidb-server/main.go b/tidb-server/main.go index 01284006e2695..57ae25ef05a8b 100644 --- a/tidb-server/main.go +++ b/tidb-server/main.go @@ -498,6 +498,7 @@ func setGlobalVars() { tikv.CommitMaxBackoff = int(parseDuration(cfg.TiKVClient.CommitTimeout).Seconds() * 1000) tikv.PessimisticLockTTL = uint64(parseDuration(cfg.PessimisticTxn.TTL).Seconds() * 1000) + tikv.RegionCacheTTLSec = int64(cfg.TiKVClient.RegionCacheTTL) } func setupLog() {