Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Reduce memory usage in reader by reset all underlying readers #24912

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

Merged
merged 1 commit into from
May 5, 2025

Conversation

chenyangfb
Copy link
Contributor

@chenyangfb chenyangfb commented Apr 14, 2025

Description

Currently for reader with multiple underlying readers (e.g. dictionaryReader and directReader in LongSelectiveStreamReader), only the current reader get reset in startStripe(), the other reader won't get reset, leads to extra memory usage and OOM.

This PR avoid those extra memory usage by reset all underlying readers in startStripe(). This behavior is controlled by resetAllReaders in OrcReaderOptions, and it's disabled by default

Impact

Reduce memory usage in reader.

Test Plan

Tested with Spark workload.
Around 10% workload triggered the code path which reset all underlying readers, leading to less memory usage and OOM.

== RELEASE NOTES ==
General change
* Improve memory usage in reader with nested readers by resetting all nested readers.

@chenyangfb chenyangfb changed the title Support reset all readers in startStripe() Reduce wasted memory usage in reader by reset all underlying readers Apr 14, 2025
@chenyangfb chenyangfb marked this pull request as ready for review April 14, 2025 17:33
@chenyangfb chenyangfb requested review from sdruzkin and a team as code owners April 14, 2025 17:33
@chenyangfb chenyangfb requested a review from presto-oss April 14, 2025 17:33
@chenyangfb chenyangfb changed the title Reduce wasted memory usage in reader by reset all underlying readers Reduce memory usage in reader by reset all underlying readers Apr 14, 2025
@sdruzkin
Copy link
Collaborator

Please add unit tests checking various combinations of stripe encodings.

@@ -40,7 +40,7 @@ public static BatchStreamReader createStreamReader(Type type, StreamDescriptor s
case INT:
case LONG:
case DATE:
return new LongBatchStreamReader(type, streamDescriptor, systemMemoryContext);
return new LongBatchStreamReader(type, streamDescriptor, systemMemoryContext, options.isResetAllReaders());
Copy link
Collaborator

@sdruzkin sdruzkin Apr 20, 2025

Choose a reason for hiding this comment

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

Why are you changing batch readers? They are not used in Spark.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, those are not used in spark. but I think not reset all underlying readers is a generic bug, hence including them. Let me know if you prefer to keep the current behavior in this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Batch reader is rarely used in prod. I'd suggest to not touch it.

@@ -52,9 +52,10 @@ public LongSelectiveStreamReader(
Optional<Type> outputType,
OrcAggregatedMemoryContext systemMemoryContext,
boolean isLowMemory,
long maxSliceSize)
long maxSliceSize,
boolean resetAllReaders)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't introduce resetAllReaders, it does not make much sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The main purpose of resetAllReaders for performance comparison in prod.
I plan to remove this option if we decide to enable it by default later.
Let me know if this make sense

Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove it now to make it a default behavior.

@@ -73,12 +74,20 @@ public void startStripe(Stripe stripe)
directReader = new LongDirectSelectiveStreamReader(context);
}
currentReader = directReader;
if (dictionaryReader != null && context.isResetAllReaders()) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to close and nullify the other reader if it exists. Reader creation is not very expensive, and usually stripes stick with the same encoding.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sure. will try that and run some test.

Copy link
Collaborator

@sdruzkin sdruzkin left a comment

Choose a reason for hiding this comment

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

Overall LGTM. Asks are to 1) remove the new flag and make it default behavior; 2) remove setting system properties.

@@ -40,7 +40,7 @@ public static BatchStreamReader createStreamReader(Type type, StreamDescriptor s
case INT:
case LONG:
case DATE:
return new LongBatchStreamReader(type, streamDescriptor, systemMemoryContext);
return new LongBatchStreamReader(type, streamDescriptor, systemMemoryContext, options.isResetAllReaders());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Batch reader is rarely used in prod. I'd suggest to not touch it.

@@ -52,9 +52,10 @@ public LongSelectiveStreamReader(
Optional<Type> outputType,
OrcAggregatedMemoryContext systemMemoryContext,
boolean isLowMemory,
long maxSliceSize)
long maxSliceSize,
boolean resetAllReaders)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove it now to make it a default behavior.

break;
case DICTIONARY:
if (dictionaryReader == null) {
dictionaryReader = new LongDictionarySelectiveStreamReader(context);
}
currentReader = dictionaryReader;
if (directReader != null && context.isResetAllReaders()) {
directReader.startStripe(stripe);
System.setProperty("RESET_LONG_READER", "RESET_LONG_READER");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't set system properties in OSS, remove this from all classes. If you need it for testing use a build with local changes.

@steveburnett
Copy link
Contributor

Thanks for the release note entry! Nits of formatting, so the automation can pick it up for the next release note PR.

== RELEASE NOTES ==

General Changes
* Reduce memory usage in reader with multiple underlying readers by reset all underlying readers.

@sdruzkin sdruzkin merged commit a0562c8 into prestodb:master May 5, 2025
98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants