Skip to content

Commit

Permalink
*: don't allocate SessionIndexUsageCollector when indexUsageLease equ…
Browse files Browse the repository at this point in the history
…als 0 (#23861)
  • Loading branch information
rebelice authored Apr 7, 2021
1 parent 7cf12a6 commit 6cff358
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 3 additions & 1 deletion session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,9 @@ func CreateSessionWithOpt(store kv.Storage, opt *Opt) (Session, error) {
// which periodically updates stats using the collected data.
if do.StatsHandle() != nil && do.StatsUpdating() {
s.statsCollector = do.StatsHandle().NewSessionStatsCollector()
s.idxUsageCollector = do.StatsHandle().NewSessionIndexUsageCollector()
if GetIndexUsageSyncLease() > 0 {
s.idxUsageCollector = do.StatsHandle().NewSessionIndexUsageCollector()
}
}

return s, nil
Expand Down
7 changes: 6 additions & 1 deletion session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {

ddlLease := time.Duration(atomic.LoadInt64(&schemaLease))
statisticLease := time.Duration(atomic.LoadInt64(&statsLease))
idxUsageSyncLease := time.Duration(atomic.LoadInt64(&indexUsageSyncLease))
idxUsageSyncLease := GetIndexUsageSyncLease()
err = util.RunWithRetry(util.DefaultMaxRetries, util.RetryInterval, func() (retry bool, err1 error) {
logutil.BgLogger().Info("new domain",
zap.String("store", store.UUID()),
Expand Down Expand Up @@ -161,6 +161,11 @@ func SetIndexUsageSyncLease(lease time.Duration) {
atomic.StoreInt64(&indexUsageSyncLease, int64(lease))
}

// GetIndexUsageSyncLease returns the index usage sync lease time.
func GetIndexUsageSyncLease() time.Duration {
return time.Duration(atomic.LoadInt64(&indexUsageSyncLease))
}

// DisableStats4Test disables the stats for tests.
func DisableStats4Test() {
SetStatsLease(-1)
Expand Down
21 changes: 21 additions & 0 deletions session/tidb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,24 @@ func (s *testMainSuite) TestKeysNeedLock(c *C) {
c.Assert(flag.HasPresumeKeyNotExists(), IsTrue)
c.Assert(keyNeedToLock(indexKey, deleteVal, flag), IsTrue)
}

func (s *testMainSuite) TestIndexUsageSyncLease(c *C) {
store, err := mockstore.NewMockStore()
c.Assert(err, IsNil)
do, err := BootstrapSession(store)
c.Assert(err, IsNil)
do.SetStatsUpdating(true)
st, err := CreateSessionWithOpt(store, nil)
c.Assert(err, IsNil)
se, ok := st.(*session)
c.Assert(ok, IsTrue)
c.Assert(se.idxUsageCollector, IsNil)

SetIndexUsageSyncLease(1)
defer SetIndexUsageSyncLease(0)
st, err = CreateSessionWithOpt(store, nil)
c.Assert(err, IsNil)
se, ok = st.(*session)
c.Assert(ok, IsTrue)
c.Assert(se.idxUsageCollector, NotNil)
}
4 changes: 4 additions & 0 deletions statistics/handle/handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1995,6 +1995,8 @@ type statsSerialSuite struct {

func (s *statsSerialSuite) TestIndexUsageInformation(c *C) {
defer cleanEnv(c, s.store, s.do)
session.SetIndexUsageSyncLease(1)
defer session.SetIndexUsageSyncLease(0)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_idx(a int, b int)")
Expand Down Expand Up @@ -2034,6 +2036,8 @@ func (s *statsSerialSuite) TestIndexUsageInformation(c *C) {

func (s *statsSerialSuite) TestGCIndexUsageInformation(c *C) {
defer cleanEnv(c, s.store, s.do)
session.SetIndexUsageSyncLease(1)
defer session.SetIndexUsageSyncLease(0)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t_idx(a int, b int)")
Expand Down

0 comments on commit 6cff358

Please sign in to comment.