-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1c812f9
distsql: clean the memory usage of MemTracker when a query ends (#10893)
SunRunAway 311d1bc
ddl: fix data race
SunRunAway d13a063
fix
SunRunAway 7d963fb
address code review
SunRunAway b2b869d
Merge branch 'master' into memory
SunRunAway File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,8 +66,9 @@ type selectResult struct { | |
fieldTypes []*types.FieldType | ||
ctx sessionctx.Context | ||
|
||
selectResp *tipb.SelectResponse | ||
respChkIdx int | ||
selectResp *tipb.SelectResponse | ||
selectRespSize int // record the selectResp.Size() when it is initialized. | ||
respChkIdx int | ||
|
||
feedback *statistics.QueryFeedback | ||
partialCount int64 // number of partial results. | ||
|
@@ -103,20 +104,25 @@ func (r *selectResult) fetch(ctx context.Context) { | |
if err != nil { | ||
result.err = err | ||
} else if resultSubset == nil { | ||
// If the result is drained, the resultSubset would be nil | ||
return | ||
} else { | ||
result.result = resultSubset | ||
if r.memTracker != nil { | ||
r.memTracker.Consume(int64(resultSubset.MemSize())) | ||
} | ||
r.memConsume(int64(resultSubset.MemSize())) | ||
} | ||
|
||
select { | ||
case r.results <- result: | ||
case <-r.closed: | ||
// If selectResult called Close() already, make fetch goroutine exit. | ||
if resultSubset != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if line 102 occurs error, |
||
r.memConsume(-int64(resultSubset.MemSize())) | ||
} | ||
return | ||
case <-ctx.Done(): | ||
if resultSubset != nil { | ||
r.memConsume(-int64(resultSubset.MemSize())) | ||
} | ||
return | ||
} | ||
} | ||
|
@@ -161,24 +167,21 @@ func (r *selectResult) getSelectResp() error { | |
if re.err != nil { | ||
return errors.Trace(re.err) | ||
} | ||
if r.memTracker != nil && r.selectResp != nil { | ||
r.memTracker.Consume(-int64(r.selectResp.Size())) | ||
if r.selectResp != nil { | ||
r.memConsume(-int64(r.selectRespSize)) | ||
} | ||
if re.result == nil { | ||
r.selectResp = nil | ||
return nil | ||
} | ||
if r.memTracker != nil { | ||
r.memTracker.Consume(-int64(re.result.MemSize())) | ||
} | ||
r.memConsume(-int64(re.result.MemSize())) | ||
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())) | ||
} | ||
r.selectRespSize = r.selectResp.Size() | ||
r.memConsume(int64(r.selectRespSize)) | ||
qw4990 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err := r.selectResp.Error; err != nil { | ||
return terror.ClassTiKV.New(terror.ErrCode(err.Code), err.Msg) | ||
} | ||
|
@@ -234,13 +237,27 @@ func (r *selectResult) readRowsData(chk *chunk.Chunk) (err error) { | |
return nil | ||
} | ||
|
||
func (r *selectResult) memConsume(bytes int64) { | ||
if r.memTracker != nil { | ||
r.memTracker.Consume(bytes) | ||
} | ||
} | ||
|
||
// Close closes selectResult. | ||
func (r *selectResult) Close() error { | ||
// Close this channel tell fetch goroutine to exit. | ||
if r.feedback.Actual() >= 0 { | ||
metrics.DistSQLScanKeysHistogram.Observe(float64(r.feedback.Actual())) | ||
} | ||
metrics.DistSQLPartialCountHistogram.Observe(float64(r.partialCount)) | ||
// Close this channel to tell the fetch goroutine to exit. | ||
close(r.closed) | ||
for re := range r.results { | ||
if re.result != nil { | ||
r.memConsume(-int64(re.result.MemSize())) | ||
} | ||
} | ||
if r.selectResp != nil { | ||
r.memConsume(-int64(r.selectRespSize)) | ||
} | ||
return r.resp.Close() | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
I will add a comment here.