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

executor,distsql: fix analyze version 2 memory leak (#28729) #29305

Merged
merged 5 commits into from
Feb 9, 2022

Conversation

ti-srebot
Copy link
Contributor

@ti-srebot ti-srebot commented Nov 1, 2021

cherry-pick #28729 to release-5.1
You can switch your code base to this Pull Request by using git-extras:

# In tidb repo:
git pr https://github.com/pingcap/tidb/pull/29305

After apply modifications, you can push your change to this PR via:

git push git@github.com:ti-srebot/tidb.git pr/29305:release-5.1-d53f9f55a0f9

What problem does this PR solve?

Fix oncall 3776

Problem Summary:

What is changed and how it works?

When error happen during the NextRaw() call, buildSamplingStats returns without close the task channel, cause the subMergeWorker leak and the the memory leak.

image

What's Changed:

Handle the resource close correctly when error happens.

How it Works:

When error happen, the channel is closed, so the worker goroutine can exit.

I write a test using failpoint to mock the error, before the fix, the test log looks like this:

[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:436] ["topNSlowQueryLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:493] ["topologySyncerKeeper exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:898] ["loadPrivilegeInLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:1025] ["globalBindHandleWorkerLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:1099] ["TelemetryReportLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:947] ["LoadSysVarCacheLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:465] ["infoSyncerKeeper exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:1066] ["handleEvolvePlanTasksLoop exited."]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:649] ["domain closed"] ["take time"=110.679µs]
[2021/10/11 18:46:35.904 +08:00] [INFO] [domain.go:1129] ["TelemetryRotateSubWindowLoop exited."]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:567] ["Closing database"]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:592] ["Memtable flushed"]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:596] ["Compaction finished"]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:615] ["BlobManager finished"]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:619] ["ResourceManager finished"]
[2021/10/11 18:46:35.905 +08:00] [INFO] [db.go:625] ["Waiting for closer"]
PASS
goleak: Errors on successful test run: found unexpected goroutines:
[Goroutine 609 in state chan receive, with github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker on top of the stack:
goroutine 609 [chan receive]:
github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker(0xc0062add40, 0xc0062a3440, 0xc0062a34a0, 0x3, 0x0)
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:1210 +0x55b
created by github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).buildSamplingStats
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:859 +0x693

 Goroutine 608 in state chan receive, with github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker on top of the stack:
goroutine 608 [chan receive]:
github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker(0xc0062add40, 0xc0062a3440, 0xc0062a34a0, 0x3, 0x1)
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:1210 +0x55b
created by github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).buildSamplingStats
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:859 +0x693

 Goroutine 626 in state chan receive, with github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker on top of the stack:
goroutine 626 [chan receive]:
github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker(0xc0062add40, 0xc0062a3440, 0xc0062a34a0, 0x3, 0x0)
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:1210 +0x55b
created by github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).buildSamplingStats
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:859 +0x693

 Goroutine 627 in state chan receive, with github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker on top of the stack:
goroutine 627 [chan receive]:
github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).subMergeWorker(0xc0062add40, 0xc0062a3440, 0xc0062a34a0, 0x3, 0x0)
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:1210 +0x55b
created by github.com/pingcap/tidb/executor.(*AnalyzeColumnsExec).buildSamplingStats
	/home/genius/project/src/github.com/pingcap/tidb/executor/analyze.go:859 +0x693
]
exit status 1
FAIL	github.com/pingcap/tidb/executor/seqtest	0.582s

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

Fix a memory leak bug when using @@tidb_analyze_version = 2

Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Nov 1, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • time-and-fate
  • winoros

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-srebot
Copy link
Contributor Author

/run-all-tests

@ti-chi-bot ti-chi-bot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Nov 1, 2021
@ti-srebot ti-srebot added affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. type/5.1-cherry-pick labels Nov 1, 2021
@ti-srebot
Copy link
Contributor Author

@tiancaiamao you're already a collaborator in bot's repo.

@tiancaiamao tiancaiamao added this to the v5.2.2 milestone Nov 2, 2021
@tiancaiamao
Copy link
Contributor

/rebuild

1 similar comment
@tiancaiamao
Copy link
Contributor

/rebuild

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Nov 2, 2021
@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 Nov 2, 2021
@bb7133
Copy link
Member

bb7133 commented Feb 7, 2022

/merge

@ti-chi-bot
Copy link
Member

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

Commit hash: e238810

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 7, 2022
@bb7133
Copy link
Member

bb7133 commented Feb 8, 2022

/merge

@VelocityLight VelocityLight added the cherry-pick-approved Cherry pick PR approved by release team. label Feb 9, 2022
@VelocityLight
Copy link

/merge

@ti-chi-bot ti-chi-bot merged commit 423d188 into pingcap:release-5.1 Feb 9, 2022
@zhouqiang-cl zhouqiang-cl modified the milestones: v5.2.2, v5.1.4 Feb 10, 2022
@tiancaiamao tiancaiamao deleted the release-5.1-d53f9f55a0f9 branch February 21, 2022 06:20
@tiancaiamao
Copy link
Contributor

Ref #32499

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. cherry-pick-approved Cherry pick PR approved by release team. release-note Denotes a PR that will be considered when it comes time to generate release notes. 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. type/5.1-cherry-pick
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants