Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

store/tikv: make cache ttl configurable(#12647) #12683

Merged
merged 3 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -395,6 +398,8 @@ var defaultConf = Config{
OverloadThreshold: 200,
MaxBatchWaitTime: 0,
BatchWaitSize: 8,

RegionCacheTTL: 600,
},
Binlog: Binlog{
WriteTimeout: "15s",
Expand Down
4 changes: 4 additions & 0 deletions config/config.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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))
Expand Down
10 changes: 6 additions & 4 deletions store/tikv/region_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,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")
Expand Down Expand Up @@ -121,7 +123,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) {
Expand Down
1 change: 1 addition & 0 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down