-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Spark 3.3: Harmonize RewriteDataFilesSparkAction #7676
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
Changes from 1 commit
Commits
Show all changes
2 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ | |
| import java.io.IOException; | ||
| import java.math.RoundingMode; | ||
| import java.util.Arrays; | ||
| import java.util.Comparator; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
@@ -83,6 +83,9 @@ public class RewriteDataFilesSparkAction | |
| USE_STARTING_SEQUENCE_NUMBER, | ||
| REWRITE_JOB_ORDER); | ||
|
|
||
| private static final RewriteDataFilesSparkAction.Result EMPTY_RESULT = | ||
| ImmutableRewriteDataFiles.Result.builder().rewriteResults(ImmutableList.of()).build(); | ||
|
|
||
| private final Table table; | ||
|
|
||
| private Expression filter = Expressions.alwaysTrue(); | ||
|
|
@@ -146,7 +149,7 @@ public RewriteDataFilesSparkAction filter(Expression expression) { | |
| @Override | ||
| public RewriteDataFiles.Result execute() { | ||
| if (table.currentSnapshot() == null) { | ||
| return ImmutableRewriteDataFiles.Result.builder().rewriteResults(ImmutableList.of()).build(); | ||
| return EMPTY_RESULT; | ||
| } | ||
|
|
||
| long startingSnapshotId = table.currentSnapshot().snapshotId(); | ||
|
|
@@ -158,26 +161,25 @@ public RewriteDataFiles.Result execute() { | |
|
|
||
| validateAndInitOptions(); | ||
|
|
||
| Map<StructLike, List<List<FileScanTask>>> fileGroupsByPartition = | ||
| StructLikeMap<List<List<FileScanTask>>> fileGroupsByPartition = | ||
| planFileGroups(startingSnapshotId); | ||
| RewriteExecutionContext ctx = new RewriteExecutionContext(fileGroupsByPartition); | ||
|
|
||
| if (ctx.totalGroupCount() == 0) { | ||
| LOG.info("Nothing found to rewrite in {}", table.name()); | ||
| return ImmutableRewriteDataFiles.Result.builder().rewriteResults(ImmutableList.of()).build(); | ||
| return EMPTY_RESULT; | ||
| } | ||
|
|
||
| Stream<RewriteFileGroup> groupStream = toGroupStream(ctx, fileGroupsByPartition); | ||
|
|
||
| RewriteDataFilesCommitManager commitManager = commitManager(startingSnapshotId); | ||
| if (partialProgressEnabled) { | ||
| return doExecuteWithPartialProgress(ctx, groupStream, commitManager); | ||
| return doExecuteWithPartialProgress(ctx, groupStream, commitManager(startingSnapshotId)); | ||
| } else { | ||
| return doExecute(ctx, groupStream, commitManager); | ||
| return doExecute(ctx, groupStream, commitManager(startingSnapshotId)); | ||
| } | ||
| } | ||
|
|
||
| Map<StructLike, List<List<FileScanTask>>> planFileGroups(long startingSnapshotId) { | ||
| StructLikeMap<List<List<FileScanTask>>> planFileGroups(long startingSnapshotId) { | ||
| CloseableIterable<FileScanTask> fileScanTasks = | ||
| table | ||
| .newScan() | ||
|
|
@@ -188,43 +190,9 @@ Map<StructLike, List<List<FileScanTask>>> planFileGroups(long startingSnapshotId | |
|
|
||
| try { | ||
| StructType partitionType = table.spec().partitionType(); | ||
| StructLikeMap<List<FileScanTask>> filesByPartition = StructLikeMap.create(partitionType); | ||
| StructLike emptyStruct = GenericRecord.create(partitionType); | ||
|
|
||
| fileScanTasks.forEach( | ||
| task -> { | ||
| // If a task uses an incompatible partition spec the data inside could contain values | ||
| // which | ||
| // belong to multiple partitions in the current spec. Treating all such files as | ||
| // un-partitioned and | ||
| // grouping them together helps to minimize new files made. | ||
| StructLike taskPartition = | ||
| task.file().specId() == table.spec().specId() | ||
| ? task.file().partition() | ||
| : emptyStruct; | ||
|
|
||
| List<FileScanTask> files = filesByPartition.get(taskPartition); | ||
| if (files == null) { | ||
| files = Lists.newArrayList(); | ||
| } | ||
|
|
||
| files.add(task); | ||
| filesByPartition.put(taskPartition, files); | ||
| }); | ||
|
|
||
| StructLikeMap<List<List<FileScanTask>>> fileGroupsByPartition = | ||
| StructLikeMap.create(partitionType); | ||
|
|
||
| filesByPartition.forEach( | ||
| (partition, tasks) -> { | ||
| Iterable<List<FileScanTask>> plannedFileGroups = rewriter.planFileGroups(tasks); | ||
| List<List<FileScanTask>> fileGroups = ImmutableList.copyOf(plannedFileGroups); | ||
| if (fileGroups.size() > 0) { | ||
| fileGroupsByPartition.put(partition, fileGroups); | ||
| } | ||
| }); | ||
|
|
||
| return fileGroupsByPartition; | ||
| StructLikeMap<List<FileScanTask>> filesByPartition = | ||
| groupByPartition(partitionType, fileScanTasks); | ||
| return fileGroupsByPartition(filesByPartition); | ||
| } finally { | ||
| try { | ||
| fileScanTasks.close(); | ||
|
|
@@ -234,6 +202,38 @@ Map<StructLike, List<List<FileScanTask>>> planFileGroups(long startingSnapshotId | |
| } | ||
| } | ||
|
|
||
| private StructLikeMap<List<FileScanTask>> groupByPartition( | ||
| StructType partitionType, Iterable<FileScanTask> tasks) { | ||
| StructLikeMap<List<FileScanTask>> filesByPartition = StructLikeMap.create(partitionType); | ||
| StructLike emptyStruct = GenericRecord.create(partitionType); | ||
|
|
||
| for (FileScanTask task : tasks) { | ||
| // If a task uses an incompatible partition spec the data inside could contain values | ||
| // which belong to multiple partitions in the current spec. Treating all such files as | ||
| // un-partitioned and grouping them together helps to minimize new files made. | ||
| StructLike taskPartition = | ||
| task.file().specId() == table.spec().specId() ? task.file().partition() : emptyStruct; | ||
|
|
||
| List<FileScanTask> files = filesByPartition.get(taskPartition); | ||
| if (files == null) { | ||
| files = Lists.newArrayList(); | ||
| } | ||
|
|
||
| files.add(task); | ||
| filesByPartition.put(taskPartition, files); | ||
| } | ||
| return filesByPartition; | ||
| } | ||
|
|
||
| private StructLikeMap<List<List<FileScanTask>>> fileGroupsByPartition( | ||
| StructLikeMap<List<FileScanTask>> filesByPartition) { | ||
| return filesByPartition.transformValues(this::planFileGroups); | ||
| } | ||
|
|
||
| private List<List<FileScanTask>> planFileGroups(List<FileScanTask> tasks) { | ||
| return ImmutableList.copyOf(rewriter.planFileGroups(tasks)); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| RewriteFileGroup rewriteFiles(RewriteExecutionContext ctx, RewriteFileGroup fileGroup) { | ||
| String desc = jobDesc(fileGroup, ctx); | ||
|
|
@@ -299,7 +299,7 @@ private Result doExecute( | |
|
|
||
| Tasks.foreach(rewrittenGroups) | ||
| .suppressFailureWhenFinished() | ||
| .run(group -> commitManager.abortFileGroup(group)); | ||
| .run(commitManager::abortFileGroup); | ||
| throw e; | ||
| } finally { | ||
| rewriteService.shutdown(); | ||
|
|
@@ -331,24 +331,31 @@ private Result doExecuteWithPartialProgress( | |
| RewriteDataFilesCommitManager commitManager) { | ||
| ExecutorService rewriteService = rewriteService(); | ||
|
|
||
| // Start Commit Service | ||
| // start commit service | ||
| int groupsPerCommit = IntMath.divide(ctx.totalGroupCount(), maxCommits, RoundingMode.CEILING); | ||
| RewriteDataFilesCommitManager.CommitService commitService = | ||
| commitManager.service(groupsPerCommit); | ||
| commitService.start(); | ||
|
|
||
| // Start rewrite tasks | ||
| Collection<FileGroupFailureResult> rewriteFailures = new ConcurrentLinkedQueue<>(); | ||
| // start rewrite tasks | ||
| Tasks.foreach(groupStream) | ||
| .suppressFailureWhenFinished() | ||
| .executeWith(rewriteService) | ||
| .noRetry() | ||
| .onFailure( | ||
| (fileGroup, exception) -> | ||
| LOG.error("Failure during rewrite group {}", fileGroup.info(), exception)) | ||
| (fileGroup, exception) -> { | ||
| LOG.error("Failure during rewrite group {}", fileGroup.info(), exception); | ||
| rewriteFailures.add( | ||
| ImmutableRewriteDataFiles.FileGroupFailureResult.builder() | ||
| .info(fileGroup.info()) | ||
| .dataFilesCount(fileGroup.numFiles()) | ||
| .build()); | ||
| }) | ||
| .run(fileGroup -> commitService.offer(rewriteFiles(ctx, fileGroup))); | ||
| rewriteService.shutdown(); | ||
|
|
||
| // Stop Commit service | ||
| // stop commit service | ||
| commitService.close(); | ||
| List<RewriteFileGroup> commitResults = commitService.results(); | ||
| if (commitResults.size() == 0) { | ||
|
|
@@ -362,49 +369,36 @@ private Result doExecuteWithPartialProgress( | |
|
|
||
| List<FileGroupRewriteResult> rewriteResults = | ||
| commitResults.stream().map(RewriteFileGroup::asResult).collect(Collectors.toList()); | ||
| return ImmutableRewriteDataFiles.Result.builder().rewriteResults(rewriteResults).build(); | ||
| return ImmutableRewriteDataFiles.Result.builder() | ||
| .rewriteResults(rewriteResults) | ||
| .rewriteFailures(rewriteFailures) | ||
| .build(); | ||
| } | ||
|
|
||
| Stream<RewriteFileGroup> toGroupStream( | ||
| RewriteExecutionContext ctx, | ||
| Map<StructLike, List<List<FileScanTask>>> fileGroupsByPartition) { | ||
| Stream<RewriteFileGroup> rewriteFileGroupStream = | ||
| fileGroupsByPartition.entrySet().stream() | ||
| .flatMap( | ||
| e -> { | ||
| StructLike partition = e.getKey(); | ||
| List<List<FileScanTask>> fileGroups = e.getValue(); | ||
| return fileGroups.stream() | ||
| .map( | ||
| tasks -> { | ||
| int globalIndex = ctx.currentGlobalIndex(); | ||
| int partitionIndex = ctx.currentPartitionIndex(partition); | ||
| FileGroupInfo info = | ||
| ImmutableRewriteDataFiles.FileGroupInfo.builder() | ||
| .globalIndex(globalIndex) | ||
| .partitionIndex(partitionIndex) | ||
| .partition(partition) | ||
| .build(); | ||
| return new RewriteFileGroup(info, tasks); | ||
| }); | ||
| }); | ||
|
|
||
| return rewriteFileGroupStream.sorted(rewriteGroupComparator()); | ||
| RewriteExecutionContext ctx, Map<StructLike, List<List<FileScanTask>>> groupsByPartition) { | ||
|
Member
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. (Not related to this pr). This is my fault, but I think this should be 'StructLikeMap' all across to be safer, I guess we can do it later. |
||
| return groupsByPartition.entrySet().stream() | ||
| .filter(e -> e.getValue().size() != 0) | ||
| .flatMap( | ||
| e -> { | ||
| StructLike partition = e.getKey(); | ||
| List<List<FileScanTask>> scanGroups = e.getValue(); | ||
| return scanGroups.stream().map(tasks -> newRewriteGroup(ctx, partition, tasks)); | ||
| }) | ||
| .sorted(RewriteFileGroup.comparator(rewriteJobOrder)); | ||
| } | ||
|
|
||
| private Comparator<RewriteFileGroup> rewriteGroupComparator() { | ||
| switch (rewriteJobOrder) { | ||
| case BYTES_ASC: | ||
| return Comparator.comparing(RewriteFileGroup::sizeInBytes); | ||
| case BYTES_DESC: | ||
| return Comparator.comparing(RewriteFileGroup::sizeInBytes, Comparator.reverseOrder()); | ||
| case FILES_ASC: | ||
| return Comparator.comparing(RewriteFileGroup::numFiles); | ||
| case FILES_DESC: | ||
| return Comparator.comparing(RewriteFileGroup::numFiles, Comparator.reverseOrder()); | ||
| default: | ||
| return (fileGroupOne, fileGroupTwo) -> 0; | ||
| } | ||
| private RewriteFileGroup newRewriteGroup( | ||
| RewriteExecutionContext ctx, StructLike partition, List<FileScanTask> tasks) { | ||
| int globalIndex = ctx.currentGlobalIndex(); | ||
| int partitionIndex = ctx.currentPartitionIndex(partition); | ||
| FileGroupInfo info = | ||
| ImmutableRewriteDataFiles.FileGroupInfo.builder() | ||
| .globalIndex(globalIndex) | ||
| .partitionIndex(partitionIndex) | ||
| .partition(partition) | ||
| .build(); | ||
| return new RewriteFileGroup(info, tasks); | ||
| } | ||
|
|
||
| void validateAndInitOptions() { | ||
|
|
@@ -484,15 +478,13 @@ private String jobDesc(RewriteFileGroup group, RewriteExecutionContext ctx) { | |
|
|
||
| @VisibleForTesting | ||
| 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<FileScanTask>>> fileGroupsByPartition) { | ||
| this.numGroupsByPartition = | ||
| fileGroupsByPartition.entrySet().stream() | ||
| .collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().size())); | ||
| RewriteExecutionContext(StructLikeMap<List<List<FileScanTask>>> fileGroupsByPartition) { | ||
| this.numGroupsByPartition = fileGroupsByPartition.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.
Looks like we are also backporting #7361 to Spark 3.3? No problem with me, but we may want to note that in pr description in case some issues arise because of this.
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.
you are right. It looks confusing to handle from this PR.
I have reverted it now. It can be done in another PR along with testcase and procedure changes.