Skip to content

Commit

Permalink
domain: use dedicated lock for expiredTimeStamp4PC (#45802) (#46211)
Browse files Browse the repository at this point in the history
close #45400
  • Loading branch information
ti-chi-bot authored Aug 18, 2023
1 parent 5c14437 commit d2edf5c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
3 changes: 3 additions & 0 deletions domain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,16 @@ go_test(
"//metrics",
"//parser/ast",
"//parser/model",
"//parser/mysql",
"//parser/terror",
"//server",
"//session",
"//sessionctx/stmtctx",
"//sessionctx/variable",
"//store/mockstore",
"//testkit",
"//testkit/testsetup",
"//types",
"//util",
"//util/mock",
"@com_github_ngaut_pools//:pools",
Expand Down
27 changes: 16 additions & 11 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,15 @@ type Domain struct {
indexUsageSyncLease time.Duration
dumpFileGcChecker *dumpFileGcChecker
planReplayerHandle *planReplayerHandle
expiredTimeStamp4PC types.Time
logBackupAdvancer *daemon.OwnerDaemon
historicalStatsWorker *HistoricalStatsWorker
ttlJobManager *ttlworker.JobManager
expiredTimeStamp4PC struct {
// let `expiredTimeStamp4PC` use its own lock to avoid any block across domain.Reload()
// and compiler.Compile(), see issue https://github.com/pingcap/tidb/issues/45400
sync.RWMutex
expiredTimeStamp types.Time
}
logBackupAdvancer *daemon.OwnerDaemon
historicalStatsWorker *HistoricalStatsWorker
ttlJobManager *ttlworker.JobManager

serverID uint64
serverIDSession *concurrency.Session
Expand Down Expand Up @@ -394,18 +399,18 @@ func (do *Domain) GetSnapshotMeta(startTS uint64) (*meta.Meta, error) {

// ExpiredTimeStamp4PC gets expiredTimeStamp4PC from domain.
func (do *Domain) ExpiredTimeStamp4PC() types.Time {
do.m.Lock()
defer do.m.Unlock()
do.expiredTimeStamp4PC.RLock()
defer do.expiredTimeStamp4PC.RUnlock()

return do.expiredTimeStamp4PC
return do.expiredTimeStamp4PC.expiredTimeStamp
}

// SetExpiredTimeStamp4PC sets the expiredTimeStamp4PC from domain.
func (do *Domain) SetExpiredTimeStamp4PC(time types.Time) {
do.m.Lock()
defer do.m.Unlock()
do.expiredTimeStamp4PC.Lock()
defer do.expiredTimeStamp4PC.Unlock()

do.expiredTimeStamp4PC = time
do.expiredTimeStamp4PC.expiredTimeStamp = time
}

// DDL gets DDL from domain.
Expand Down Expand Up @@ -898,7 +903,6 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio
slowQuery: newTopNSlowQueries(30, time.Hour*24*7, 500),
indexUsageSyncLease: idxUsageSyncLease,
dumpFileGcChecker: &dumpFileGcChecker{gcLease: dumpFileGcLease, paths: []string{GetPlanReplayerDirName(), GetOptimizerTraceDirName()}},
expiredTimeStamp4PC: types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, types.DefaultFsp),
mdlCheckTableInfo: &mdlCheckTableInfo{
mu: sync.Mutex{},
jobsVerMap: make(map[int64]int64),
Expand All @@ -913,6 +917,7 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio
do.serverMemoryLimitHandle = servermemorylimit.NewServerMemoryLimitHandle(do.exit)
do.sysProcesses = SysProcesses{mu: &sync.RWMutex{}, procMap: make(map[uint64]sessionctx.Context)}
do.initDomainSysVars()
do.expiredTimeStamp4PC.expiredTimeStamp = types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, types.DefaultFsp)
return do
}

Expand Down
14 changes: 14 additions & 0 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ import (
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/parser/mysql"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/mock"
dto "github.com/prometheus/client_model/go"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -187,6 +190,17 @@ func TestStatWorkRecoverFromPanic(t *testing.T) {
scope := dom.GetScope("status")
require.Equal(t, variable.DefaultStatusVarScopeFlag, scope)

// default expiredTimeStamp4PC = "0000-00-00 00:00:00"
ts := types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, types.DefaultFsp)
expiredTimeStamp := dom.ExpiredTimeStamp4PC()
require.Equal(t, expiredTimeStamp, ts)

// set expiredTimeStamp4PC to "2023-08-02 12:15:00"
ts, _ = types.ParseTimestamp(&stmtctx.StatementContext{TimeZone: time.UTC}, "2023-08-02 12:15:00")
dom.SetExpiredTimeStamp4PC(ts)
expiredTimeStamp = dom.ExpiredTimeStamp4PC()
require.Equal(t, expiredTimeStamp, ts)

err = store.Close()
require.NoError(t, err)

Expand Down

0 comments on commit d2edf5c

Please sign in to comment.