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, statistics, util: fix data race of Handle.mu.ctx #33732

Merged
merged 7 commits into from
Apr 13, 2022

Conversation

xuyifangreeneyes
Copy link
Contributor

@xuyifangreeneyes xuyifangreeneyes commented Apr 6, 2022

What problem does this PR solve?

Issue Number: close #32867 #33001 #32822

Problem Summary:

We call (*Handle).execRestrictedSQL concurrently, which uses Handle.mu.ctx to fetch another session from session pool to execute internal SQL.

func (h *Handle) execRestrictedSQL(ctx context.Context, sql string, params ...interface{}) ([]chunk.Row, []*ast.ResultField, error) {
return h.mu.ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(ctx, []sqlexec.OptionFuncAlias{sqlexec.ExecOptionUseSessionPool}, sql, params...)
}

However, the method may cause data race in the following code.

tidb/session/session.go

Lines 1748 to 1764 in bd8c710

return se, func() {
se.sessionVars.AnalyzeVersion = prevStatsVer
if err := se.sessionVars.SetSystemVar(variable.TiDBSnapshot, ""); err != nil {
logutil.BgLogger().Error("set tidbSnapshot error", zap.Error(err))
}
se.sessionVars.SnapshotInfoschema = nil
if !execOption.IgnoreWarning {
if se != nil && se.GetSessionVars().StmtCtx.WarningCount() > 0 {
warnings := se.GetSessionVars().StmtCtx.GetWarnings()
s.GetSessionVars().StmtCtx.AppendWarnings(warnings)
}
}
se.sessionVars.PartitionPruneMode.Store(prePruneMode)
se.sessionVars.OptimizerUseInvisibleIndexes = false
se.sessionVars.InspectionTableCache = nil
s.sysSessionPool().Put(tmp)
}, nil

At Line 1757, we append warnings for Handle.mu.ctx, which causes data race.

What is changed and how it works?

IMO it is better to make (*Handle).execRestrictedSQL inrelevant with Handle.mu.ctx.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

@ti-chi-bot
Copy link
Member

ti-chi-bot commented Apr 6, 2022

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • hawkingrei
  • qw4990

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found here.

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added do-not-merge/needs-linked-issue release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Apr 6, 2022
@xuyifangreeneyes
Copy link
Contributor Author

/cc @chrysan @hawkingrei

@xuyifangreeneyes
Copy link
Contributor Author

/cc @tiancaiamao

if err != nil {
return nil, nil, errors.Trace(err)
}
defer h.pool.Put(se)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we put this line before the if check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we should check error first. According to the implementation of (*sessionPool).Get, if the returned err is not nil, then the returned se must be nil. Besides, in the following code we also check error first and then do the Put operation.

tidb/session/session.go

Lines 1713 to 1765 in 37d86da

func (s *session) getInternalSession(execOption sqlexec.ExecOption) (*session, func(), error) {
tmp, err := s.sysSessionPool().Get()
if err != nil {
return nil, nil, errors.Trace(err)
}
se := tmp.(*session)
// The special session will share the `InspectionTableCache` with current session
// if the current session in inspection mode.
if cache := s.sessionVars.InspectionTableCache; cache != nil {
se.sessionVars.InspectionTableCache = cache
}
if ok := s.sessionVars.OptimizerUseInvisibleIndexes; ok {
se.sessionVars.OptimizerUseInvisibleIndexes = true
}
prePruneMode := se.sessionVars.PartitionPruneMode.Load()
if execOption.SnapshotTS != 0 {
se.sessionVars.SnapshotInfoschema, err = getSnapshotInfoSchema(s, execOption.SnapshotTS)
if err != nil {
return nil, nil, err
}
if err := se.sessionVars.SetSystemVar(variable.TiDBSnapshot, strconv.FormatUint(execOption.SnapshotTS, 10)); err != nil {
return nil, nil, err
}
}
prevStatsVer := se.sessionVars.AnalyzeVersion
if execOption.AnalyzeVer != 0 {
se.sessionVars.AnalyzeVersion = execOption.AnalyzeVer
}
// for analyze stmt we need let worker session follow user session that executing stmt.
se.sessionVars.PartitionPruneMode.Store(s.sessionVars.PartitionPruneMode.Load())
return se, func() {
se.sessionVars.AnalyzeVersion = prevStatsVer
if err := se.sessionVars.SetSystemVar(variable.TiDBSnapshot, ""); err != nil {
logutil.BgLogger().Error("set tidbSnapshot error", zap.Error(err))
}
se.sessionVars.SnapshotInfoschema = nil
if !execOption.IgnoreWarning {
if se != nil && se.GetSessionVars().StmtCtx.WarningCount() > 0 {
warnings := se.GetSessionVars().StmtCtx.GetWarnings()
s.GetSessionVars().StmtCtx.AppendWarnings(warnings)
}
}
se.sessionVars.PartitionPruneMode.Store(prePruneMode)
se.sessionVars.OptimizerUseInvisibleIndexes = false
se.sessionVars.InspectionTableCache = nil
s.sysSessionPool().Put(tmp)
}, nil
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should all internal sql executions using internal session pools ignore warnings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently we don't check warnings when executing internal sql through (*Handle).execRestrictedSQL, (*Handle).execRestrictedSQLWithStatsVer and (*Handle).execRestrictedSQLWithSnapshot. Maybe we can ignore warnings for now and make improvements if later warnings are needed.

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Apr 6, 2022
@sre-bot
Copy link
Contributor

sre-bot commented Apr 7, 2022

@xuyifangreeneyes
Copy link
Contributor Author

/cc @winoros

@ti-chi-bot ti-chi-bot requested a review from winoros April 11, 2022 02:35
@xuyifangreeneyes
Copy link
Contributor Author

/cc @time-and-fate

Comment on lines +143 to +144
return h.withRestrictedSQLExecutor(ctx, func(ctx context.Context, exec sqlexec.RestrictedSQLExecutor) ([]chunk.Row, []*ast.ResultField, error) {
return exec.ExecRestrictedSQL(ctx, []sqlexec.OptionFuncAlias{sqlexec.ExecOptionUseCurSession}, sql, params...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that we can decouple/detach execRestrictedSQL from Handle thoroughly in the future.

@qw4990 qw4990 added the sig/planner SIG: Planner label Apr 13, 2022
Copy link
Contributor

@qw4990 qw4990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Apr 13, 2022
@qw4990
Copy link
Contributor

qw4990 commented Apr 13, 2022

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: 6f2e509

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Apr 13, 2022
@ti-chi-bot ti-chi-bot merged commit 9c836a5 into pingcap:master Apr 13, 2022
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Apr 13, 2022
@ti-srebot
Copy link
Contributor

cherry pick to release-6.0 in PR #33919

@purelind
Copy link
Contributor

TiDB MergeCI notify

🟠 Bad News! New failing [1] after this pr merged.
These new failed integration tests seem to be caused by the current PR, please try to fix these new failed integration tests, thanks!

CI Name Result Duration Compare with Parent commit
idc-jenkins-ci/integration-cdc-test 🟥 failed 12, success 22, total 34 27 min New failing
idc-jenkins-ci-tidb/integration-br-test 🟥 failed 11, success 18, total 29 41 min Existing failure
idc-jenkins-ci-tidb/code-coverage ✅ Lines coverage: 63.48% 13 min Fixed
idc-jenkins-ci-tidb/integration-copr-test ✅ all 1 tests passed 12 min Fixed
idc-jenkins-ci-tidb/common-test ✅ all 12 tests passed 10 min Fixed
idc-jenkins-ci-tidb/integration-compatibility-test ✅ all 1 tests passed 10 min Fixed
idc-jenkins-ci-tidb/tics-test 🟢 all 1 tests passed 19 min Existing passed
idc-jenkins-ci-tidb/integration-common-test 🟢 all 11 tests passed 14 min Existing passed
idc-jenkins-ci-tidb/sqllogic-test-2 🟢 all 28 tests passed 9 min 4 sec Existing passed
idc-jenkins-ci-tidb/sqllogic-test-1 🟢 all 26 tests passed 8 min 30 sec Existing passed
idc-jenkins-ci-tidb/integration-ddl-test 🟢 all 6 tests passed 8 min 26 sec Existing passed
idc-jenkins-ci-tidb/mybatis-test 🟢 all 1 tests passed 3 min 37 sec Existing passed
idc-jenkins-ci-tidb/plugin-test 🟢 build success, plugin test success 4min Existing passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note-none Denotes a PR that doesn't merit a release note. sig/planner SIG: Planner size/M Denotes a PR that changes 30-99 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DATA RACE in the StatementContext
9 participants