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

domain: use dedicated lock for expiredTimeStamp4PC #45802

Merged
merged 3 commits into from
Aug 12, 2023
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
36 changes: 21 additions & 15 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,20 @@ type Domain struct {
memoryUsageAlarmHandle *memoryusagealarm.Handle
serverMemoryLimitHandle *servermemorylimit.Handle
// TODO: use Run for each process in future pr
wg *util.WaitGroupEnhancedWrapper
statsUpdating atomicutil.Int32
cancel context.CancelFunc
indexUsageSyncLease time.Duration
dumpFileGcChecker *dumpFileGcChecker
planReplayerHandle *planReplayerHandle
extractTaskHandle *ExtractHandle
expiredTimeStamp4PC types.Time
wg *util.WaitGroupEnhancedWrapper
statsUpdating atomicutil.Int32
cancel context.CancelFunc
indexUsageSyncLease time.Duration
dumpFileGcChecker *dumpFileGcChecker
planReplayerHandle *planReplayerHandle
extractTaskHandle *ExtractHandle
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 atomic.Pointer[ttlworker.JobManager]
Expand Down Expand Up @@ -482,18 +488,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 @@ -1043,7 +1049,6 @@ func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duratio
slowQuery: newTopNSlowQueries(config.GetGlobalConfig().InMemSlowQueryTopNNum, time.Hour*24*7, config.GetGlobalConfig().InMemSlowQueryRecentNum),
indexUsageSyncLease: idxUsageSyncLease,
dumpFileGcChecker: &dumpFileGcChecker{gcLease: dumpFileGcLease, paths: []string{replayer.GetPlanReplayerDirName(), GetOptimizerTraceDirName(), GetExtractTaskDirName()}},
expiredTimeStamp4PC: types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, types.DefaultFsp),
mdlCheckTableInfo: &mdlCheckTableInfo{
mu: sync.Mutex{},
jobsVerMap: make(map[int64]int64),
Expand All @@ -1059,6 +1064,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 @@ -19,6 +19,9 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/pingcap/tidb/parser/mysql"
hawkingrei marked this conversation as resolved.
Show resolved Hide resolved
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/types"
"net"
"runtime"
"testing"
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