-
Notifications
You must be signed in to change notification settings - Fork 4.6k
AddFiles: extract bounded async task plumbing and Parquet footer reads #39896
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 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run.", | ||
| "modification": 4 | ||
| "modification": 5 | ||
| } |
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
113 changes: 113 additions & 0 deletions
113
sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/BoundedAsyncTasks.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,113 @@ | ||
| /* | ||
| * 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.beam.sdk.io.iceberg; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.Iterator; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.ExecutorService; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.Future; | ||
| import java.util.function.Consumer; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.ThreadFactoryBuilder; | ||
|
|
||
| /** | ||
| * Runs tasks on a fixed thread pool while bounding how many are in flight. Results are handed to | ||
| * {@code onDone} on the caller's thread (from {@link #submit} and {@link #awaitAll}), so a DoFn can | ||
| * emit them without a thread-safe output. | ||
| */ | ||
| class BoundedAsyncTasks<T> { | ||
| private final ExecutorService executor; | ||
| private final int maxInFlight; | ||
| private final Deque<Future<T>> active = new ArrayDeque<>(); | ||
|
|
||
| BoundedAsyncTasks(int threads, int maxInFlight) { | ||
| Preconditions.checkArgument(threads > 0, "threads must be positive, got: %s", threads); | ||
| Preconditions.checkArgument( | ||
| maxInFlight > 0, "maxInFlight must be positive, got: %s", maxInFlight); | ||
| this.executor = | ||
| Executors.newFixedThreadPool( | ||
| threads, | ||
| new ThreadFactoryBuilder() | ||
| .setDaemon(true) | ||
| .setNameFormat("iceberg-async-task-%d") | ||
| .build()); | ||
| this.maxInFlight = maxInFlight; | ||
| } | ||
|
|
||
| /** | ||
| * Submits a task, first delivering any finished results. Blocks while {@code maxInFlight} tasks | ||
| * are outstanding. If a task failed, its exception is rethrown and every other outstanding task | ||
| * is cancelled. | ||
| */ | ||
| void submit(Callable<T> task, Consumer<T> onDone) throws Exception { | ||
| try { | ||
| drainFinished(onDone); | ||
| while (active.size() >= maxInFlight) { | ||
| Future<T> oldest = active.removeFirst(); | ||
| onDone.accept(oldest.get()); // blocks until the oldest task completes | ||
|
claudevdm marked this conversation as resolved.
|
||
| } | ||
| active.add(executor.submit(task)); | ||
| } catch (Exception e) { | ||
| cancelAll(); | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Delivers every outstanding result. Finished tasks drained during execution may have been | ||
| * delivered out of submission order; remaining tasks are delivered in queue order. The queue is | ||
| * empty afterwards. | ||
| */ | ||
| void awaitAll(Consumer<T> onDone) throws Exception { | ||
| try { | ||
| while (!active.isEmpty()) { | ||
| Future<T> oldest = active.removeFirst(); | ||
| onDone.accept(oldest.get()); | ||
| } | ||
| } finally { | ||
| cancelAll(); | ||
| } | ||
| } | ||
|
|
||
| /** Cancels and forgets every outstanding task. Results already delivered are unaffected. */ | ||
| void cancelAll() { | ||
| for (Future<T> future : active) { | ||
| future.cancel(true); | ||
| } | ||
| active.clear(); | ||
| } | ||
|
|
||
| void shutdown() { | ||
| cancelAll(); | ||
| executor.shutdownNow(); | ||
| } | ||
|
|
||
| private void drainFinished(Consumer<T> onDone) throws Exception { | ||
| Iterator<Future<T>> iterator = active.iterator(); | ||
| while (iterator.hasNext()) { | ||
| Future<T> future = iterator.next(); | ||
| if (future.isDone()) { | ||
| iterator.remove(); | ||
| onDone.accept(future.get()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/ParquetFooters.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,51 @@ | ||
| /* | ||
| * 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.beam.sdk.io.iceberg; | ||
|
|
||
| import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.channels.SeekableByteChannel; | ||
| import org.apache.beam.sdk.io.Compression; | ||
| import org.apache.beam.sdk.io.FileSystems; | ||
| import org.apache.beam.sdk.io.fs.ResourceId; | ||
| import org.apache.beam.sdk.io.parquet.ParquetIO.ReadFiles.BeamParquetInputFile; | ||
| import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Iterables; | ||
| import org.apache.parquet.hadoop.ParquetFileReader; | ||
| import org.apache.parquet.hadoop.metadata.ParquetMetadata; | ||
| import org.apache.parquet.io.InputFile; | ||
|
|
||
| /** Reads Parquet footers through Beam's {@link FileSystems}, so no table or FileIO is needed. */ | ||
| final class ParquetFooters { | ||
| private ParquetFooters() {} | ||
|
|
||
| static ParquetMetadata read(String filePath) throws IOException { | ||
| try (ParquetFileReader reader = ParquetFileReader.open(inputFile(filePath))) { | ||
| return reader.getFooter(); | ||
|
claudevdm marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| private static InputFile inputFile(String filePath) throws IOException { | ||
| ResourceId resourceId = | ||
| Iterables.getOnlyElement(FileSystems.match(filePath).metadata()).resourceId(); | ||
| Compression compression = Compression.detect(checkStateNotNull(resourceId.getFilename())); | ||
| SeekableByteChannel channel = | ||
| (SeekableByteChannel) compression.readDecompressed(FileSystems.open(resourceId)); | ||
| return new BeamParquetInputFile(channel); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.