-
Notifications
You must be signed in to change notification settings - Fork 247
Align read window with part boundaries #1618
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -28,6 +28,8 @@ pub struct BackpressureConfig { | |
| pub read_window_size_multiplier: usize, | ||
| /// Request range to apply backpressure | ||
| pub request_range: Range<u64>, | ||
| /// Enable alignment of read window end to part boundary | ||
| pub align_read_window: bool, | ||
| } | ||
|
|
||
| /// A [BackpressureController] should be given to consumers of a byte stream. | ||
|
|
@@ -50,6 +52,9 @@ pub struct BackpressureController { | |
| read_window_end_offset: u64, | ||
| /// Next offset of the data to be read, relative to the start of the S3 object. | ||
| next_read_offset: u64, | ||
| /// Start offset of the second "main" request to S3. This helps in aligning window ends of the | ||
| /// request with part boundaries. | ||
| second_request_start: u64, | ||
| /// End offset within the S3 object for the request. | ||
| /// | ||
| /// The request can return data up to this offset *exclusively*. | ||
|
|
@@ -58,6 +63,8 @@ pub struct BackpressureController { | |
| /// | ||
| /// For example, when memory is low we should scale down [Self::preferred_read_window_size]. | ||
| mem_limiter: Arc<MemoryLimiter>, | ||
| /// Enable alignment of read window end to part boundary | ||
| align_read_window: bool, | ||
| } | ||
|
|
||
| /// The [BackpressureLimiter] is used on producer side of a stream, for example, | ||
|
|
@@ -100,8 +107,10 @@ pub fn new_backpressure_controller( | |
| read_window_size_multiplier: config.read_window_size_multiplier.max(MIN_WINDOW_SIZE_MULTIPLIER), | ||
| read_window_end_offset, | ||
| next_read_offset: config.request_range.start, | ||
| second_request_start: config.request_range.start + config.initial_read_window_size as u64, | ||
| request_end_offset: config.request_range.end, | ||
| mem_limiter, | ||
| align_read_window: config.align_read_window, | ||
| }; | ||
|
|
||
| let limiter = BackpressureLimiter { | ||
|
|
@@ -136,8 +145,17 @@ impl BackpressureController { | |
| { | ||
| let new_read_window_end_offset = self | ||
| .next_read_offset | ||
| .saturating_add(self.preferred_read_window_size as u64) | ||
| .min(self.request_end_offset); | ||
| .saturating_add(self.preferred_read_window_size as u64); | ||
| let new_read_window_end_offset = Self::round_up_to_part_boundary( | ||
| new_read_window_end_offset, | ||
| self.second_request_start, | ||
| self.min_read_window_size as u64, | ||
|
Contributor
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. We are assuming this is actually the read part size, right? We should make it explicit. |
||
| self.align_read_window, | ||
| ); | ||
| // NOTE: in the end of the object we still have up to "part_size" of unaccounted memory. | ||
| // For more accurate memory limiting we could reserve in "full parts", but that would make | ||
| // release more complicated. So accept this inaccuracy, and reserve only till `request_end_offset`. | ||
| let new_read_window_end_offset = new_read_window_end_offset.min(self.request_end_offset); | ||
|
Contributor
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. Try to avoid shadowing here. |
||
| // We can skip if the new `read_window_end_offset` is less than or equal to the current one, this | ||
| // could happen after the read window is scaled down. | ||
| if new_read_window_end_offset <= self.read_window_end_offset { | ||
|
|
@@ -231,6 +249,33 @@ impl BackpressureController { | |
| .record((self.preferred_read_window_size / 1024 / 1024) as f64); | ||
| } | ||
| } | ||
|
|
||
| /// Round up `read_window_end` to next part boundary (relative to request start). | ||
| /// | ||
| /// This function ensures that read window end offsets are aligned to part boundaries for the second request, | ||
| /// which helps optimize memory usage. | ||
| /// | ||
| /// If the `read_window_end` is before the second request start or `align_read_window` is false, it returns the `read_window_end` unchanged. | ||
| /// | ||
| /// Note: window excludes the last byte, denoted by `read_window_end`. | ||
| fn round_up_to_part_boundary( | ||
| read_window_end: u64, | ||
| second_req_start: u64, | ||
| part_size: u64, | ||
| align_read_window: bool, | ||
| ) -> u64 { | ||
| if align_read_window && read_window_end > second_req_start { | ||
| let relative_end_offset = read_window_end - second_req_start; | ||
| if !relative_end_offset.is_multiple_of(part_size) { | ||
| let aligned_relative_offset = part_size * (relative_end_offset / part_size + 1); | ||
| second_req_start + aligned_relative_offset | ||
| } else { | ||
| read_window_end | ||
| } | ||
| } else { | ||
| read_window_end | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for BackpressureController { | ||
|
|
@@ -346,6 +391,7 @@ mod tests { | |
| max_read_window_size: 2 * 1024 * 1024 * 1024, | ||
| read_window_size_multiplier, | ||
| request_range, | ||
| align_read_window: true, | ||
| }; | ||
|
|
||
| let (mut backpressure_controller, _backpressure_limiter) = | ||
|
|
@@ -373,6 +419,7 @@ mod tests { | |
| max_read_window_size: 2 * 1024 * 1024 * 1024, | ||
| read_window_size_multiplier, | ||
| request_range, | ||
| align_read_window: true, | ||
| }; | ||
|
|
||
| let (mut backpressure_controller, _backpressure_limiter) = | ||
|
|
@@ -402,6 +449,7 @@ mod tests { | |
| max_read_window_size: 2 * GIB, | ||
| read_window_size_multiplier: 2, | ||
| request_range: 0..(5 * GIB as u64), | ||
| align_read_window: true, | ||
| }; | ||
|
|
||
| let (mut backpressure_controller, mut backpressure_limiter) = | ||
|
|
@@ -434,6 +482,18 @@ mod tests { | |
| }); | ||
| } | ||
|
|
||
| #[test_case(500, 1000, 100, 500; "offset before second request start")] | ||
| #[test_case(1000, 1000, 512, 1000; "offset at second request start")] | ||
| #[test_case(1500, 1000, 512, 1512; "offset after second request start, needs rounding up")] | ||
| #[test_case(2024, 1000, 512, 2024; "offset after second request start, already aligned")] | ||
| #[test_case(1001, 1000, 512, 1512; "offset just after second request start, needs rounding up")] | ||
| #[test_case(1512, 1000, 512, 1512; "offset exactly at part boundary")] | ||
| #[test_case(1513, 1000, 512, 2024; "offset just past part boundary")] | ||
| fn test_round_up_to_part_boundary(offset: u64, second_req_start: u64, part_size: u64, expected: u64) { | ||
| let result = BackpressureController::round_up_to_part_boundary(offset, second_req_start, part_size, true); | ||
| assert_eq!(result, expected); | ||
| } | ||
|
|
||
| fn new_backpressure_controller_for_test( | ||
| backpressure_config: BackpressureConfig, | ||
| ) -> (BackpressureController, BackpressureLimiter) { | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Is this the right place? Or should we align in
scale_upandscale_down, where we already modifypreferred_read_window_size?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.
With the existing incrementing logic, I think, it is the right place. Above this line we "blindly" adding
preferred_read_window_sizetonew_read_window_end_offset. Having a fixedpreferred_read_window_sizewon't work for arbitrarynew_read_window_end_offset, which is dictated by the reading side (e.g. we may need to increment the window at offset4,194,305or4,194,306depending on how much data was read).