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

sem/builtins: account for internal memory of some aggregates #46545

Merged
merged 1 commit into from
Mar 26, 2020

Conversation

yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Mar 25, 2020

Release justification: fixes for high-priority or high-severity bugs in
existing functionality.

Previously, we were not accounting for the memory of the first non-null
datum in anyNotNull aggregate. Similarly, we were not accounting for
the byte slice in bytesXor aggregate. This is now fixed.

This commit also fixes an issue with double accounting for the current
datum in min and max aggregates on fixed size types (previously, it
was reported in Size() method as well as separately on the first
non-null datum). Additionally, minAggregate and maxAggregate have
been unexported.

Addresses: #45812.

Release note (bug fix): CockroachDB previously was incorrectly
accounting for some RAM usage when computing aggregate functions which
results in an underestimation of it and could cause an out of memory
crash. Now this has been fixed.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich yuzefovich force-pushed the fix-aggs branch 2 times, most recently from 8ee4131 to 5bc1ed5 Compare March 25, 2020 02:55
a.sawNonNull = false
a.result.Reset()
a.acc.Empty(ctx)
Copy link
Member Author

Choose a reason for hiding this comment

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

This actually might be incorrect, will need to double check this tomorrow.

Copy link
Contributor

Choose a reason for hiding this comment

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

The max aggregate doesn't reset its account here either

@jordanlewis
Copy link
Member

Can you share the memory profile that brought you to this conclusion?

@yuzefovich
Copy link
Member Author

So Rohan has been staring at this:
Screen Shot 2020-03-24 at 10 17 41 PM
and he pointed out that it seems like we're not accounting for all new DInts that are being created. We both looked at the code, and, indeed, anyNotNull aggregate doesn't account for the memory of the first non-null datum it stores. What happens is that we create a new DInt for every row, and then we store the first non-null DInt for each of the group in the aggregate struct, and that memory was never accounted for by mistake.

I looked over all aggregate functions in the same file and found another leak in bytesXor.

rohany
rohany previously requested changes Mar 25, 2020
if a.val == tree.DNull && datum != tree.DNull {
a.val = datum
if err := a.acc.Grow(ctx, int64(datum.Size())); err != nil {
Copy link
Contributor

@rohany rohany Mar 25, 2020

Choose a reason for hiding this comment

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

I think this should be ResizeTo, not Grow

Copy link
Contributor

Choose a reason for hiding this comment

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

ack never mind, we are only going to call this once.

a.sawNonNull = false
a.result.Reset()
a.acc.Empty(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

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

The max aggregate doesn't reset its account here either

Release justification: fixes for high-priority or high-severity bugs in
existing functionality.

Previously, we were not accounting for the memory of the first non-null
datum in `anyNotNull` aggregate. Similarly, we were not accounting for
the byte slice in `bytesXor` aggregate. This is now fixed.

This commit also fixes an issue with double accounting for the current
datum in `min` and `max` aggregates on fixed size types (previously, it
was reported in `Size()` method as well as separately on the first
non-null datum). Additionally, `minAggregate` and `maxAggregate` have
been unexported.

Release note (bug fix): CockroachDB previously was incorrectly
accounting for some RAM usage when computing aggregate functions which
results in an underestimation of it and could cause an out of memory
crash. Now this has been fixed.
Copy link
Member Author

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @asubiotto and @rohany)


pkg/sql/sem/builtins/aggregate_builtins.go, line 521 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

ack never mind, we are only going to call this once.

Yep.


pkg/sql/sem/builtins/aggregate_builtins.go, line 766 at r1 (raw file):

Previously, rohany (Rohan Yadav) wrote…

The max aggregate doesn't reset its account here either

Done.

@rohany
Copy link
Contributor

rohany commented Mar 25, 2020

this LGTM, but i'd wait for someone else more knowledgeable in this area to agree

Copy link
Contributor

@asubiotto asubiotto left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @asubiotto and @rohany)

@yuzefovich
Copy link
Member Author

TFTRs!

bors r+

@craig
Copy link
Contributor

craig bot commented Mar 26, 2020

Build succeeded

@craig craig bot merged commit a81d504 into cockroachdb:master Mar 26, 2020
@yuzefovich yuzefovich deleted the fix-aggs branch March 26, 2020 15:20
@rohany rohany mentioned this pull request Mar 26, 2020
24 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants