-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Partition spooled pages while encoding data to segments #26013
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.operator; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.spi.Page; | ||
|
||
import java.util.ArrayDeque; | ||
import java.util.ArrayList; | ||
import java.util.Deque; | ||
import java.util.List; | ||
|
||
import static com.google.common.base.Verify.verify; | ||
import static io.trino.execution.buffer.PageSplitterUtil.splitPage; | ||
import static java.lang.Math.clamp; | ||
|
||
public class SpoolingPagePartitioner | ||
{ | ||
static final double LOWER_BOUND = 0.05; // 5% of the target size | ||
static final double UPPER_BOUND = 0.1; // 10% of the target size | ||
|
||
private SpoolingPagePartitioner() {} | ||
|
||
public static List<List<Page>> partition(List<Page> pages, long targetSize) | ||
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. why operate on multiple pages 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 buffer multiple pages 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. I get it - but why not partition page by page. Passing multiple pages makes interface more complex. Let me read deeper
wendigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Deque<Page> queue = new ArrayDeque<>(pages); | ||
List<Page> currentPartition = new ArrayList<>(); | ||
ImmutableList.Builder<List<Page>> partitions = ImmutableList.builder(); | ||
|
||
while (!queue.isEmpty()) { | ||
Page currentPage = queue.removeFirst(); | ||
|
||
long remainingSize = targetSize - size(currentPartition); | ||
verify(remainingSize >= 0, "Current partition size %s is larger than target size %s", size(currentPartition), targetSize); | ||
|
||
if (currentPage.getSizeInBytes() < remainingSize) { | ||
currentPartition.add(currentPage); | ||
|
||
if (withinThreshold(size(currentPartition), targetSize)) { | ||
partitions.add(ImmutableList.copyOf(currentPartition)); | ||
currentPartition.clear(); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
List<Page> currentPartitioned = new ArrayList<>(takeFromHead(currentPage, remainingSize, targetSize)); | ||
currentPartition.add(currentPartitioned.removeFirst()); | ||
|
||
// Add the remaining split pages back to the queue in the original order | ||
currentPartitioned.reversed().forEach(queue::addFirst); | ||
wendigo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (withinThreshold(size(currentPartition), targetSize)) { | ||
partitions.add(ImmutableList.copyOf(currentPartition)); | ||
currentPartition.clear(); | ||
} | ||
} | ||
|
||
// If there are any remaining pages in the current partition, add them as a final partition | ||
if (!currentPartition.isEmpty()) { | ||
partitions.add(ImmutableList.copyOf(currentPartition)); | ||
} | ||
|
||
return partitions.build(); | ||
} | ||
|
||
private static boolean withinThreshold(long page, long targetSize) | ||
{ | ||
return page >= targetSize * (1 - LOWER_BOUND) && page <= targetSize * (1 + UPPER_BOUND); | ||
} | ||
|
||
private static List<Page> takeFromHead(Page page, long targetHeadSize, long tailSplitSize) | ||
{ | ||
verify(page.getSizeInBytes() >= targetHeadSize, "Page size %s must be greater than head size %s", page.getSizeInBytes(), targetHeadSize); | ||
ImmutableList.Builder<Page> builder = ImmutableList.builder(); | ||
|
||
int positions = positionsWithBytes(page, targetHeadSize); | ||
builder.add(page.getRegion(0, positions)); | ||
|
||
if (positions == page.getPositionCount()) { | ||
return builder.build(); | ||
} | ||
builder.addAll(splitPage(page.getRegion(positions, page.getPositionCount() - positions), tailSplitSize)); | ||
return builder.build(); | ||
} | ||
|
||
private static long averageSizePerPosition(Page page) | ||
{ | ||
return clamp(page.getSizeInBytes() / (long) page.getPositionCount(), 1, Integer.MAX_VALUE); | ||
} | ||
|
||
private static int positionsWithBytes(Page page, long bytes) | ||
{ | ||
long positions = bytes / averageSizePerPosition(page); | ||
return clamp(positions, 1, page.getPositionCount()); | ||
} | ||
|
||
private static long size(List<Page> pages) | ||
{ | ||
return pages.stream().mapToLong(Page::getSizeInBytes).sum(); | ||
} | ||
} |
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.
those could be added in separate commit