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

session: fix deadlock when init domain failed #40409

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ func (do *Domain) Close() {
const resourceIdleTimeout = 3 * time.Minute // resources in the ResourcePool will be recycled after idleTimeout

// NewDomain creates a new domain. Should not create multiple domains for the same store.
func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duration, idxUsageSyncLease time.Duration, dumpFileGcLease time.Duration, factory pools.Factory, onClose func()) *Domain {
func NewDomain(store kv.Storage, ddlLease time.Duration, statsLease time.Duration, idxUsageSyncLease time.Duration, dumpFileGcLease time.Duration, factory pools.Factory) *Domain {
capacity := 200 // capacity of the sysSessionPool size
do := &Domain{
store: store,
Expand All @@ -909,7 +909,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{replayer.GetPlanReplayerDirName(), GetOptimizerTraceDirName()}},
onClose: onClose,
expiredTimeStamp4PC: types.NewTime(types.ZeroCoreTime, mysql.TypeTimestamp, types.DefaultFsp),
mdlCheckTableInfo: &mdlCheckTableInfo{
mu: sync.Mutex{},
Expand Down Expand Up @@ -1082,6 +1081,11 @@ func (do *Domain) Init(
return nil
}

// SetOnClose used to set do.onClose func.
func (do *Domain) SetOnClose(onClose func()) {
do.onClose = onClose
}

func (do *Domain) initLogBackup(ctx context.Context, pdClient pd.Client) error {
cfg := config.GetGlobalConfig()
if pdClient == nil || do.etcdClient == nil {
Expand Down
6 changes: 3 additions & 3 deletions domain/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestInfo(t *testing.T) {
Storage: s,
pdAddrs: []string{cluster.Members[0].GRPCURL()}}
ddlLease := 80 * time.Millisecond
dom := NewDomain(mockStore, ddlLease, 0, 0, 0, mockFactory, nil)
dom := NewDomain(mockStore, ddlLease, 0, 0, 0, mockFactory)
defer func() {
dom.Close()
err := s.Close()
Expand Down Expand Up @@ -171,7 +171,7 @@ func TestStatWorkRecoverFromPanic(t *testing.T) {
require.NoError(t, err)

ddlLease := 80 * time.Millisecond
dom := NewDomain(store, ddlLease, 0, 0, 0, mockFactory, nil)
dom := NewDomain(store, ddlLease, 0, 0, 0, mockFactory)

metrics.PanicCounter.Reset()
// Since the stats lease is 0 now, so create a new ticker will panic.
Expand Down Expand Up @@ -238,7 +238,7 @@ func TestClosestReplicaReadChecker(t *testing.T) {
require.NoError(t, err)

ddlLease := 80 * time.Millisecond
dom := NewDomain(store, ddlLease, 0, 0, 0, mockFactory, nil)
dom := NewDomain(store, ddlLease, 0, 0, 0, mockFactory)
defer func() {
dom.Close()
require.Nil(t, store.Close())
Expand Down
12 changes: 6 additions & 6 deletions session/tidb.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ import (
"github.com/pingcap/tidb/util/dbterror"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/syncutil"
"go.uber.org/zap"
)

type domainMap struct {
mu sync.Mutex
mu syncutil.Mutex
domains map[string]*domain.Domain
}

Expand Down Expand Up @@ -81,10 +82,7 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
zap.Stringer("index usage sync lease", idxUsageSyncLease))
factory := createSessionFunc(store)
sysFactory := createSessionWithDomainFunc(store)
onClose := func() {
dm.Delete(store)
}
d = domain.NewDomain(store, ddlLease, statisticLease, idxUsageSyncLease, planReplayerGCLease, factory, onClose)
d = domain.NewDomain(store, ddlLease, statisticLease, idxUsageSyncLease, planReplayerGCLease, factory)

var ddlInjector func(ddl.DDL) *schematracker.Checker
if injector, ok := store.(schematracker.StorageDDLInjector); ok {
Expand All @@ -102,8 +100,10 @@ func (dm *domainMap) Get(store kv.Storage) (d *domain.Domain, err error) {
if err != nil {
return nil, err
}

dm.domains[key] = d
d.SetOnClose(func() {
dm.Delete(store)
})

return
}
Expand Down