-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[core] Optimize partition discovery with projected manifest scans #9321
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
Open
zhoulii
wants to merge
3
commits into
apache:master
Choose a base branch
from
zhoulii:feature/partition-entry-narrow-projection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
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
176 changes: 176 additions & 0 deletions
176
paimon-core/src/main/java/org/apache/paimon/operation/PartitionEntryScanner.java
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 |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.paimon.operation; | ||
|
|
||
| import org.apache.paimon.data.BinaryRow; | ||
| import org.apache.paimon.io.DataFileMeta; | ||
| import org.apache.paimon.manifest.BucketFilter; | ||
| import org.apache.paimon.manifest.ManifestEntry; | ||
| import org.apache.paimon.manifest.ManifestFile; | ||
| import org.apache.paimon.manifest.ManifestFileMeta; | ||
| import org.apache.paimon.manifest.PartitionEntry; | ||
| import org.apache.paimon.manifest.ProjectedManifestEntry; | ||
| import org.apache.paimon.partition.PartitionPredicate; | ||
| import org.apache.paimon.types.RowType; | ||
| import org.apache.paimon.utils.CloseableIterator; | ||
| import org.apache.paimon.utils.Filter; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.apache.paimon.utils.ManifestReadThreadPool.getExecutorService; | ||
| import static org.apache.paimon.utils.ThreadPoolUtils.randomlyOnlyExecute; | ||
|
|
||
| /** | ||
| * Scans and aggregates partition statistics from manifest entries. | ||
| * | ||
| * <p>It uses a narrow, streaming projection when the manifest cannot benefit from the cache and | ||
| * falls back to the caller's complete entry reader when other filters need the full schema. | ||
| */ | ||
| final class PartitionEntryScanner { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(PartitionEntryScanner.class); | ||
| private static final ProjectedManifestEntry.Projection PARTITION_ENTRY_PROJECTION = | ||
| createPartitionEntryProjection(); | ||
|
|
||
| private final ManifestFile.Factory manifestFileFactory; | ||
| private final Function<ManifestFileMeta, List<PartitionEntry>> fullEntryReader; | ||
| @Nullable private final PartitionPredicate partitionFilter; | ||
| @Nullable private final BucketFilter bucketFilter; | ||
| @Nullable private final Integer specifiedLevel; | ||
| @Nullable private final Filter<Integer> levelFilter; | ||
| @Nullable private final Filter<String> fileNameFilter; | ||
| private final boolean requiresFullManifestEntry; | ||
| @Nullable private final Integer parallelism; | ||
|
|
||
| PartitionEntryScanner( | ||
| ManifestFile.Factory manifestFileFactory, | ||
| Function<ManifestFileMeta, List<PartitionEntry>> fullEntryReader, | ||
| @Nullable PartitionPredicate partitionFilter, | ||
| @Nullable BucketFilter bucketFilter, | ||
| @Nullable Integer specifiedLevel, | ||
| @Nullable Filter<Integer> levelFilter, | ||
| @Nullable Filter<String> fileNameFilter, | ||
| boolean requiresFullManifestEntry, | ||
| @Nullable Integer parallelism) { | ||
| this.manifestFileFactory = manifestFileFactory; | ||
| this.fullEntryReader = fullEntryReader; | ||
| this.partitionFilter = partitionFilter; | ||
| this.bucketFilter = bucketFilter; | ||
| this.specifiedLevel = specifiedLevel; | ||
| this.levelFilter = levelFilter; | ||
| this.fileNameFilter = fileNameFilter; | ||
| this.requiresFullManifestEntry = requiresFullManifestEntry; | ||
| this.parallelism = parallelism; | ||
| } | ||
|
|
||
| List<PartitionEntry> scan(List<ManifestFileMeta> manifests) { | ||
| Map<BinaryRow, PartitionEntry> partitions = new ConcurrentHashMap<>(); | ||
| randomlyOnlyExecute( | ||
| getExecutorService(parallelism), | ||
| manifest -> scanManifest(manifest, partitions), | ||
| manifests); | ||
| return partitions.values().stream() | ||
| .filter(partition -> partition.fileCount() > 0) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private void scanManifest( | ||
| ManifestFileMeta manifest, Map<BinaryRow, PartitionEntry> partitions) { | ||
| // Projected scans read the file directly, so preserve the normal path for cached manifests | ||
| // and filters which require fields outside the partition projection. | ||
| if (requiresFullManifestEntry || manifestFileFactory.isCacheable(manifest.fileSize())) { | ||
| PartitionEntry.merge(fullEntryReader.apply(manifest), partitions); | ||
| return; | ||
| } | ||
|
|
||
| long count = 0; | ||
| try (CloseableIterator<ProjectedManifestEntry> entries = | ||
| manifestFileFactory | ||
| .create() | ||
| .scan( | ||
| manifest.fileName(), | ||
| PARTITION_ENTRY_PROJECTION, | ||
| partitionFilter, | ||
| bucketFilter)) { | ||
| while (entries.hasNext()) { | ||
| ProjectedManifestEntry entry = entries.next(); | ||
| if (!filter(entry)) { | ||
| continue; | ||
| } | ||
|
|
||
| PartitionEntry partitionEntry = PartitionEntry.fromManifestEntry(entry); | ||
| partitions.compute( | ||
| partitionEntry.partition(), | ||
| (partition, previous) -> | ||
| previous == null ? partitionEntry : previous.merge(partitionEntry)); | ||
| count++; | ||
| } | ||
| } catch (Exception e) { | ||
| throw new RuntimeException("Failed to scan manifest " + manifest.fileName(), e); | ||
| } | ||
| LOG.info("Read {} projected manifest entries from {}", count, manifest.fileName()); | ||
| } | ||
|
|
||
| private boolean filter(ProjectedManifestEntry entry) { | ||
| int level = entry.level(); | ||
| if (specifiedLevel != null && level != specifiedLevel) { | ||
| return false; | ||
| } | ||
| if (levelFilter != null && !levelFilter.test(level)) { | ||
| return false; | ||
| } | ||
| return fileNameFilter == null || fileNameFilter.test(entry.fileName()); | ||
| } | ||
|
|
||
| /** | ||
| * Keeps the fields required to aggregate {@link PartitionEntry}: kind controls the sign of | ||
| * added/deleted files, partition is the grouping key, total buckets is part of the result, and | ||
| * file size, row count and creation time form its statistics. File name and level are also kept | ||
| * to preserve the corresponding structural filters. | ||
| */ | ||
| private static ProjectedManifestEntry.Projection createPartitionEntryProjection() { | ||
| RowType manifestType = ManifestEntry.MANIFEST_ROW_TYPE; | ||
| return ProjectedManifestEntry.Projection.create( | ||
| new RowType( | ||
| false, | ||
| Arrays.asList( | ||
| manifestType.getField(ManifestEntry.KIND), | ||
| manifestType.getField(ManifestEntry.PARTITION), | ||
| manifestType.getField(ManifestEntry.TOTAL_BUCKETS), | ||
| manifestType | ||
| .getField(ManifestEntry.FILE) | ||
| .newType( | ||
| DataFileMeta.SCHEMA.project( | ||
| DataFileMeta.FILE_NAME, | ||
| DataFileMeta.FILE_SIZE, | ||
| DataFileMeta.ROW_COUNT, | ||
| DataFileMeta.LEVEL, | ||
| DataFileMeta.CREATION_TIME))))); | ||
| } | ||
| } |
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.
Why not just use isCacheEnabled?
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.
isCacheable(fileSize) is intentional. When a manifest exceeds maxElementSize, ObjectsCache bypasses it even if caching is enabled. Using isCacheEnabled() would unnecessarily disable projected scans for
these large, non-cacheable manifests.
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.
Why we need to bypass it? Could you describe this scenario in more detail?
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.
Do you mean why cached reads cannot use projection as well?
Currently, projection is only supported by the file reader, while the cache stores and materializes full manifest entries. This change preserves the existing full-cache path for cacheable manifests and applies file-level projection only when the manifest cannot be cached.
Cache-side projected materialization could be added as a separate optimization. Is that what you are suggesting?
Uh oh!
There was an error while loading. Please reload this page.
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.
When does the manifest size exceed the maxElementSize?
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 default caching catalog settings, maxElementSize is 1 MB (cache.manifest.small-file-threshold), while manifest.target-file-size defaults to 8 MB. Therefore, normal manifest files can exceed maxElementSize.