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

distsql: clean the memory usage of MemTracker when a query ends (#10893) #10898

Merged
merged 5 commits into from
Jun 27, 2019

Conversation

SunRunAway
Copy link
Contributor

What problem does this PR solve?

closes #10893

What is changed and how it works?

Correctly release memory consuming in distsql.

Check List

Tests

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

run a select query like select * from t, ta

mysql> SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST;
+------+------+-----------+------+---------+------+-------+----------------------------------------------+------+
| ID   | USER | HOST      | DB   | COMMAND | TIME | STATE | INFO                                         | MEM  |
+------+------+-----------+------+---------+------+-------+----------------------------------------------+------+
|    1 | root | 127.0.0.1 | test | Sleep   |    3 | 2     |                                              |    0 |
|    2 | root | 127.0.0.1 | test | Query   |    0 | 2     | SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST |    0 |
+------+------+-----------+------+---------+------+-------+----------------------------------------------+------+
2 rows in set (0.00 sec)

Code changes

  • None

Side effects

  • Increased code complexity

Related changes

  • None

@SunRunAway SunRunAway added sig/execution SIG execution type/bugfix This PR fixes a bug. labels Jun 21, 2019
@SunRunAway SunRunAway marked this pull request as ready for review June 21, 2019 04:06
@SunRunAway
Copy link
Contributor Author

/run-all-tests

@qw4990 qw4990 requested a review from XuHuaiyu June 21, 2019 05:59
@SunRunAway
Copy link
Contributor Author

/run-unit-test

@SunRunAway
Copy link
Contributor Author

/run-all-tests

@codecov
Copy link

codecov bot commented Jun 21, 2019

Codecov Report

Merging #10898 into master will not change coverage.
The diff coverage is n/a.

@@             Coverage Diff             @@
##             master     #10898   +/-   ##
===========================================
  Coverage   80.9333%   80.9333%           
===========================================
  Files           418        418           
  Lines         89245      89245           
===========================================
  Hits          72229      72229           
  Misses        11786      11786           
  Partials       5230       5230

@SunRunAway
Copy link
Contributor Author

/run-all-tests

1 similar comment
@SunRunAway
Copy link
Contributor Author

/run-all-tests

@SunRunAway
Copy link
Contributor Author

/rebuild

@@ -106,17 +106,21 @@ func (r *selectResult) fetch(ctx context.Context) {
return
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 check in here?

Copy link
Contributor Author

@SunRunAway SunRunAway Jun 26, 2019

Choose a reason for hiding this comment

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

Yes, we should. Per https://godoc.org/github.com/pingcap/tidb/kv#Response, resultSubset may be nil even if no error take place.

	// Next returns a resultSubset from a single storage unit.
	// When full result set is returned, nil is returned.
	Next(ctx context.Context) (resultSubset ResultSubset, err error)

I will add a comment here.

@@ -231,16 +234,31 @@ func (r *selectResult) readRowsData(chk *chunk.Chunk) (err error) {
}
}
r.selectResp.Chunks[r.respChkIdx].RowsData = rowsData
r.memConsume(-int64(originMemory - r.selectResp.Size()))
Copy link
Contributor

@crazycs520 crazycs520 Jun 24, 2019

Choose a reason for hiding this comment

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

r.memConsume(int64(r.selectResp.Size() - originMemory))

Copy link
Member

Choose a reason for hiding this comment

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

I prefer to directly to use -, it indicates a memory releasing.

Copy link
Member

Choose a reason for hiding this comment

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

By the way, Maybe we should not reduce the used memory size. because the underlying buffer is still referenced by rowsData, the memory is actually not changed.

Copy link
Contributor Author

@SunRunAway SunRunAway Jun 26, 2019

Choose a reason for hiding this comment

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

You're right. I'll try to implement it in another way.

distsql/distsql_test.go Outdated Show resolved Hide resolved
}

select {
case r.results <- result:
case <-r.closed:
// If selectResult called Close() already, make fetch goroutine exit.
if resultSubset != nil {
Copy link
Member

Choose a reason for hiding this comment

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

no need to check nil again

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if line 102 occurs error, resultSubset would be nil.

r.selectResp = new(tipb.SelectResponse)
err := r.selectResp.Unmarshal(re.result.GetData())
if err != nil {
return errors.Trace(err)
}
if r.memTracker != nil && r.selectResp != nil {
r.memTracker.Consume(int64(r.selectResp.Size()))
if r.selectResp != nil {
Copy link
Member

Choose a reason for hiding this comment

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

it's guaranteed to be not nil in line 176.

@@ -231,16 +234,31 @@ func (r *selectResult) readRowsData(chk *chunk.Chunk) (err error) {
}
}
r.selectResp.Chunks[r.respChkIdx].RowsData = rowsData
r.memConsume(-int64(originMemory - r.selectResp.Size()))
Copy link
Member

Choose a reason for hiding this comment

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

By the way, Maybe we should not reduce the used memory size. because the underlying buffer is still referenced by rowsData, the memory is actually not changed.

@CLAassistant
Copy link

CLAassistant commented Jun 26, 2019

CLA assistant check
All committers have signed the CLA.

@SunRunAway
Copy link
Contributor Author

/run-all-tests

@SunRunAway
Copy link
Contributor Author

@zz-jason @crazycs520 PTAL again. Thanks.

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

@qw4990 qw4990 added status/all tests passed status/LGT1 Indicates that a PR has LGTM 1. labels Jun 26, 2019
Copy link
Member

@zz-jason zz-jason left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution status/LGT2 Indicates that a PR has LGTM 2. type/bugfix This PR fixes a bug.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Should clean the memory usage of MemTracker when a query ends.
5 participants