Skip to content

Fix accounting for new/delete from different threads for VariableContext::Thread#16122

Closed
azat wants to merge 1 commit intoClickHouse:masterfrom
azat:memory-tracking-Thread-free-overcommit
Closed

Fix accounting for new/delete from different threads for VariableContext::Thread#16122
azat wants to merge 1 commit intoClickHouse:masterfrom
azat:memory-tracking-Thread-free-overcommit

Conversation

@azat
Copy link
Copy Markdown
Member

@azat azat commented Oct 18, 2020

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Fix possible memory drift (due to new/delete from different threads)

Detailed description / Documentation draft:
MemoryTracker assumes that for VariableContext::Thread new/delete may be
called from different threads, hence the amount of memory can go
negative.

However the MemoryTracker is nested, so even if the negative amount is
allowed for VariableContext::Thread it does not allowed for anything
upper, and hence the MemoryTracking will not be decremented properly.

Fix this, by looking at the initial level.

Fixes: #15932 (This PR should fix memory drift for HTTP queries from that PR)

P.S. Marked as Improvement since backporting requirement is doubtful

…ext::Thread

MemoryTracker assumes that for VariableContext::Thread new/delete may be
called from different threads, hence the amount of memory can go
negative.

However the MemoryTracker is nested, so even if the negative amount is
allowed for VariableContext::Thread it does not allowed for anything
upper, and hence the MemoryTracking will not be decremented properly.

Fix this, by looking at the initial level.

This should fix memory drift for HTTP queries.
@robot-clickhouse robot-clickhouse added the pr-improvement Pull request with some product improvements label Oct 18, 2020
@azat azat marked this pull request as draft October 18, 2020 12:06
@azat azat marked this pull request as ready for review October 18, 2020 14:44
@alexey-milovidov alexey-milovidov self-assigned this Oct 21, 2020
}

if (level == VariableContext::Thread)
if (parent_level == VariableContext::Thread)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't understand how initial level can be anything other than thread.
I assume that memory tracking always goes via DB::CurrentThread::getMemoryTracker()

It means we don't need this branch anymore...

Copy link
Copy Markdown
Member Author

@azat azat Oct 22, 2020

Choose a reason for hiding this comment

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

Hm, nice catch, Ok, this PR will be replaced with these two patches from #16121:

  • 17dcdaef751e2eaa60e6f5ed63779dfdafc81eaf
  • 52d247f828c2c297dd4e4539b1f0d0876a53be15

(I can't push them separately since they are required to fix MemoryTracking, otherwise I can't write a test for that PR)

Copy link
Copy Markdown
Member

@alexey-milovidov alexey-milovidov left a comment

Choose a reason for hiding this comment

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

Clarifications needed.

@azat azat closed this Oct 22, 2020
@azat azat deleted the memory-tracking-Thread-free-overcommit branch October 24, 2020 22:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MemoryTracker wrong total: Memory limit (total) exceeded, but no real usage

3 participants