-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark 3.4: Fixup for RewritePositionDeleteFilesSparkAction #7565
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
szehon-ho
merged 3 commits into
apache:master
from
szehon-ho:rewrite_position_delete_fixup
May 16, 2023
Merged
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 |
|---|---|---|
|
|
@@ -47,7 +47,6 @@ | |
| import org.apache.iceberg.exceptions.ValidationException; | ||
| import org.apache.iceberg.expressions.Expression; | ||
| import org.apache.iceberg.io.CloseableIterable; | ||
| import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
|
|
@@ -68,11 +67,12 @@ | |
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** Spark implementation of {@link RewritePositionDeleteFiles}. */ | ||
| public class RewritePositionDeleteSparkAction | ||
| extends BaseSnapshotUpdateSparkAction<RewritePositionDeleteSparkAction> | ||
| public class RewritePositionDeleteFilesSparkAction | ||
| extends BaseSnapshotUpdateSparkAction<RewritePositionDeleteFilesSparkAction> | ||
| implements RewritePositionDeleteFiles { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(RewritePositionDeleteSparkAction.class); | ||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(RewritePositionDeleteFilesSparkAction.class); | ||
| private static final Set<String> VALID_OPTIONS = | ||
| ImmutableSet.of( | ||
| MAX_CONCURRENT_FILE_GROUP_REWRITES, | ||
|
|
@@ -90,19 +90,19 @@ public class RewritePositionDeleteSparkAction | |
| private boolean partialProgressEnabled; | ||
| private RewriteJobOrder rewriteJobOrder; | ||
|
|
||
| RewritePositionDeleteSparkAction(SparkSession spark, Table table) { | ||
| RewritePositionDeleteFilesSparkAction(SparkSession spark, Table table) { | ||
| super(spark); | ||
| this.table = table; | ||
| this.rewriter = new SparkBinPackPositionDeletesRewriter(spark(), table); | ||
| } | ||
|
|
||
| @Override | ||
| protected RewritePositionDeleteSparkAction self() { | ||
| protected RewritePositionDeleteFilesSparkAction self() { | ||
| return this; | ||
| } | ||
|
|
||
| @Override | ||
| public RewritePositionDeleteSparkAction filter(Expression expression) { | ||
| public RewritePositionDeleteFilesSparkAction filter(Expression expression) { | ||
| throw new UnsupportedOperationException("Regular filters not supported yet."); | ||
| } | ||
|
|
||
|
|
@@ -115,7 +115,7 @@ public RewritePositionDeleteFiles.Result execute() { | |
|
|
||
| validateAndInitOptions(); | ||
|
|
||
| Map<StructLike, List<List<PositionDeletesScanTask>>> fileGroupsByPartition = planFileGroups(); | ||
| StructLikeMap<List<List<PositionDeletesScanTask>>> fileGroupsByPartition = planFileGroups(); | ||
| RewriteExecutionContext ctx = new RewriteExecutionContext(fileGroupsByPartition); | ||
|
|
||
| if (ctx.totalGroupCount() == 0) { | ||
|
|
@@ -125,63 +125,68 @@ public RewritePositionDeleteFiles.Result execute() { | |
|
|
||
| Stream<RewritePositionDeletesGroup> groupStream = toGroupStream(ctx, fileGroupsByPartition); | ||
|
|
||
| RewritePositionDeletesCommitManager commitManager = commitManager(); | ||
| if (partialProgressEnabled) { | ||
| return doExecuteWithPartialProgress(ctx, groupStream, commitManager); | ||
| return doExecuteWithPartialProgress(ctx, groupStream, commitManager()); | ||
| } else { | ||
| return doExecute(ctx, groupStream, commitManager); | ||
| return doExecute(ctx, groupStream, commitManager()); | ||
| } | ||
| } | ||
|
|
||
| private Map<StructLike, List<List<PositionDeletesScanTask>>> planFileGroups() { | ||
|
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. Optional: A note to the above snippet. What about getting rid of |
||
| Table deletesTable = | ||
| MetadataTableUtils.createMetadataTableInstance(table, MetadataTableType.POSITION_DELETES); | ||
| CloseableIterable<PositionDeletesScanTask> scanTasks = | ||
| CloseableIterable.transform( | ||
| deletesTable.newBatchScan().ignoreResiduals().planFiles(), | ||
| t -> (PositionDeletesScanTask) t); | ||
| private StructLikeMap<List<List<PositionDeletesScanTask>>> planFileGroups() { | ||
| CloseableIterable<PositionDeletesScanTask> fileTasks = planFiles(); | ||
|
|
||
| try { | ||
| StructType partitionType = Partitioning.partitionType(table); | ||
| StructLikeMap<List<PositionDeletesScanTask>> filesByPartition = | ||
| StructLikeMap.create(partitionType); | ||
|
|
||
| for (PositionDeletesScanTask task : scanTasks) { | ||
| StructLike coerced = coercePartition(task, partitionType); | ||
|
|
||
| List<PositionDeletesScanTask> partitionTasks = filesByPartition.get(coerced); | ||
| if (partitionTasks == null) { | ||
| partitionTasks = Lists.newArrayList(); | ||
| } | ||
| partitionTasks.add(task); | ||
| filesByPartition.put(coerced, partitionTasks); | ||
| } | ||
|
|
||
| StructLikeMap<List<List<PositionDeletesScanTask>>> fileGroupsByPartition = | ||
| StructLikeMap.create(partitionType); | ||
|
|
||
| filesByPartition.forEach( | ||
| (partition, partitionTasks) -> { | ||
| Iterable<List<PositionDeletesScanTask>> plannedFileGroups = | ||
| rewriter.planFileGroups(partitionTasks); | ||
| List<List<PositionDeletesScanTask>> groups = ImmutableList.copyOf(plannedFileGroups); | ||
| if (groups.size() > 0) { | ||
| fileGroupsByPartition.put(partition, groups); | ||
| } | ||
| }); | ||
|
|
||
| return fileGroupsByPartition; | ||
| StructLikeMap<List<PositionDeletesScanTask>> fileTasksByPartition = | ||
| groupByPartition(partitionType, fileTasks); | ||
| return fileGroupsByPartition(fileTasksByPartition); | ||
szehon-ho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } finally { | ||
| try { | ||
| scanTasks.close(); | ||
| fileTasks.close(); | ||
| } catch (IOException io) { | ||
| LOG.error("Cannot properly close file iterable while planning for rewrite", io); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| RewritePositionDeletesGroup rewriteDeleteFiles( | ||
| private CloseableIterable<PositionDeletesScanTask> planFiles() { | ||
| Table deletesTable = | ||
| MetadataTableUtils.createMetadataTableInstance(table, MetadataTableType.POSITION_DELETES); | ||
|
|
||
| return CloseableIterable.transform( | ||
| deletesTable.newBatchScan().ignoreResiduals().planFiles(), | ||
| task -> (PositionDeletesScanTask) task); | ||
| } | ||
|
|
||
| private StructLikeMap<List<PositionDeletesScanTask>> groupByPartition( | ||
| StructType partitionType, Iterable<PositionDeletesScanTask> tasks) { | ||
| StructLikeMap<List<PositionDeletesScanTask>> filesByPartition = | ||
| StructLikeMap.create(partitionType); | ||
|
|
||
| for (PositionDeletesScanTask task : tasks) { | ||
| StructLike coerced = coercePartition(task, partitionType); | ||
|
|
||
| List<PositionDeletesScanTask> partitionTasks = filesByPartition.get(coerced); | ||
| if (partitionTasks == null) { | ||
| partitionTasks = Lists.newArrayList(); | ||
| } | ||
| partitionTasks.add(task); | ||
| filesByPartition.put(coerced, partitionTasks); | ||
| } | ||
|
|
||
| return filesByPartition; | ||
| } | ||
|
|
||
| private StructLikeMap<List<List<PositionDeletesScanTask>>> fileGroupsByPartition( | ||
szehon-ho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| StructLikeMap<List<PositionDeletesScanTask>> filesByPartition) { | ||
| return filesByPartition.transformValues(this::planFileGroups); | ||
| } | ||
|
|
||
| private List<List<PositionDeletesScanTask>> planFileGroups(List<PositionDeletesScanTask> tasks) { | ||
| return ImmutableList.copyOf(rewriter.planFileGroups(tasks)); | ||
| } | ||
|
|
||
| private RewritePositionDeletesGroup rewriteDeleteFiles( | ||
| RewriteExecutionContext ctx, RewritePositionDeletesGroup fileGroup) { | ||
| String desc = jobDesc(fileGroup, ctx); | ||
| Set<DeleteFile> addedFiles = | ||
|
|
@@ -204,8 +209,7 @@ private ExecutorService rewriteService() { | |
| .build())); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| RewritePositionDeletesCommitManager commitManager() { | ||
| private RewritePositionDeletesCommitManager commitManager() { | ||
| return new RewritePositionDeletesCommitManager(table); | ||
| } | ||
|
|
||
|
|
@@ -282,12 +286,12 @@ private Result doExecuteWithPartialProgress( | |
| RewritePositionDeletesCommitManager commitManager) { | ||
| ExecutorService rewriteService = rewriteService(); | ||
|
|
||
| // Start Commit Service | ||
| // start commit service | ||
| int groupsPerCommit = IntMath.divide(ctx.totalGroupCount(), maxCommits, RoundingMode.CEILING); | ||
| CommitService commitService = commitManager.service(groupsPerCommit); | ||
| commitService.start(); | ||
|
|
||
| // Start rewrite tasks | ||
| // start rewrite tasks | ||
| Tasks.foreach(groupStream) | ||
| .suppressFailureWhenFinished() | ||
| .executeWith(rewriteService) | ||
|
|
@@ -298,7 +302,7 @@ private Result doExecuteWithPartialProgress( | |
| .run(fileGroup -> commitService.offer(rewriteDeleteFiles(ctx, fileGroup))); | ||
| rewriteService.shutdown(); | ||
|
|
||
| // Stop Commit service | ||
| // stop commit service | ||
szehon-ho marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| commitService.close(); | ||
| List<RewritePositionDeletesGroup> commitResults = commitService.results(); | ||
| if (commitResults.size() == 0) { | ||
|
|
@@ -319,31 +323,31 @@ private Result doExecuteWithPartialProgress( | |
| .build(); | ||
| } | ||
|
|
||
| Stream<RewritePositionDeletesGroup> toGroupStream( | ||
| private Stream<RewritePositionDeletesGroup> toGroupStream( | ||
| RewriteExecutionContext ctx, | ||
| Map<StructLike, List<List<PositionDeletesScanTask>>> groupsByPartition) { | ||
| Stream<RewritePositionDeletesGroup> rewriteFileGroupStream = | ||
| groupsByPartition.entrySet().stream() | ||
| .flatMap( | ||
| e -> { | ||
| StructLike partition = e.getKey(); | ||
| List<List<PositionDeletesScanTask>> scanGroups = e.getValue(); | ||
| return scanGroups.stream() | ||
| .map( | ||
| tasks -> { | ||
| int globalIndex = ctx.currentGlobalIndex(); | ||
| int partitionIndex = ctx.currentPartitionIndex(partition); | ||
| FileGroupInfo info = | ||
| ImmutableRewritePositionDeleteFiles.FileGroupInfo.builder() | ||
| .globalIndex(globalIndex) | ||
| .partitionIndex(partitionIndex) | ||
| .partition(partition) | ||
| .build(); | ||
| return new RewritePositionDeletesGroup(info, tasks); | ||
| }); | ||
| }); | ||
|
|
||
| return rewriteFileGroupStream.sorted(RewritePositionDeletesGroup.comparator(rewriteJobOrder)); | ||
| return groupsByPartition.entrySet().stream() | ||
| .filter(e -> e.getValue().size() != 0) | ||
| .flatMap( | ||
| e -> { | ||
| StructLike partition = e.getKey(); | ||
| List<List<PositionDeletesScanTask>> scanGroups = e.getValue(); | ||
| return scanGroups.stream().map(tasks -> newRewriteGroup(ctx, partition, tasks)); | ||
| }) | ||
| .sorted(RewritePositionDeletesGroup.comparator(rewriteJobOrder)); | ||
| } | ||
|
|
||
| private RewritePositionDeletesGroup newRewriteGroup( | ||
| RewriteExecutionContext ctx, StructLike partition, List<PositionDeletesScanTask> tasks) { | ||
| int globalIndex = ctx.currentGlobalIndex(); | ||
| int partitionIndex = ctx.currentPartitionIndex(partition); | ||
| FileGroupInfo info = | ||
| ImmutableRewritePositionDeleteFiles.FileGroupInfo.builder() | ||
| .globalIndex(globalIndex) | ||
| .partitionIndex(partitionIndex) | ||
| .partition(partition) | ||
| .build(); | ||
| return new RewritePositionDeletesGroup(info, tasks); | ||
| } | ||
|
|
||
| private void validateAndInitOptions() { | ||
|
|
@@ -418,16 +422,14 @@ private String jobDesc(RewritePositionDeletesGroup group, RewriteExecutionContex | |
| } | ||
|
|
||
| static class RewriteExecutionContext { | ||
| private final Map<StructLike, Integer> numGroupsByPartition; | ||
| private final StructLikeMap<Integer> numGroupsByPartition; | ||
| private final int totalGroupCount; | ||
| private final Map<StructLike, Integer> partitionIndexMap; | ||
| private final AtomicInteger groupIndex; | ||
|
|
||
| RewriteExecutionContext( | ||
| Map<StructLike, List<List<PositionDeletesScanTask>>> groupsByPartition) { | ||
| this.numGroupsByPartition = | ||
| groupsByPartition.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size())); | ||
| StructLikeMap<List<List<PositionDeletesScanTask>>> fileTasksByPartition) { | ||
| this.numGroupsByPartition = fileTasksByPartition.transformValues(List::size); | ||
| this.totalGroupCount = numGroupsByPartition.values().stream().reduce(Integer::sum).orElse(0); | ||
| this.partitionIndexMap = Maps.newConcurrentMap(); | ||
| this.groupIndex = new AtomicInteger(1); | ||
|
|
||
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
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.
Your idea of making this a static method also makes sense to me. It would be more in line with Guava and other similar libs. If you decide to make it static, keep in mind the ordering of methods. Static methods should be right after the constructor.