-
Notifications
You must be signed in to change notification settings - Fork 9.2k
HADOOP-17531. DistCp: Reduce memory usage on copying huge directories. (#2732). #2808
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
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
153 changes: 153 additions & 0 deletions
153
...hadoop-common/src/main/java/org/apache/hadoop/util/functional/CommonCallableSupplier.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,153 @@ | ||
| /* | ||
| * 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.hadoop.util.functional; | ||
|
|
||
| import javax.annotation.Nullable; | ||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.List; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.CancellationException; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.concurrent.CompletionException; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import org.apache.hadoop.util.DurationInfo; | ||
|
|
||
| import static org.apache.hadoop.fs.impl.FutureIOSupport.raiseInnerCause; | ||
|
|
||
| /** | ||
| * A bridge from Callable to Supplier; catching exceptions | ||
| * raised by the callable and wrapping them as appropriate. | ||
| * @param <T> return type. | ||
| */ | ||
| public final class CommonCallableSupplier<T> implements Supplier { | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(CommonCallableSupplier.class); | ||
|
|
||
| private final Callable<T> call; | ||
|
|
||
| /** | ||
| * Create. | ||
| * @param call call to invoke. | ||
| */ | ||
| public CommonCallableSupplier(final Callable<T> call) { | ||
| this.call = call; | ||
| } | ||
|
|
||
| @Override | ||
| public Object get() { | ||
| try { | ||
| return call.call(); | ||
| } catch (RuntimeException e) { | ||
| throw e; | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } catch (Exception e) { | ||
| throw new UncheckedIOException(new IOException(e)); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Submit a callable into a completable future. | ||
| * RTEs are rethrown. | ||
| * Non RTEs are caught and wrapped; IOExceptions to | ||
| * {@code RuntimeIOException} instances. | ||
| * @param executor executor. | ||
| * @param call call to invoke | ||
| * @param <T> type | ||
| * @return the future to wait for | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> CompletableFuture<T> submit(final Executor executor, | ||
| final Callable<T> call) { | ||
| return CompletableFuture | ||
| .supplyAsync(new CommonCallableSupplier<T>(call), executor); | ||
| } | ||
|
|
||
| /** | ||
| * Wait for a list of futures to complete. If the list is empty, | ||
| * return immediately. | ||
| * @param futures list of futures. | ||
| * @throws IOException if one of the called futures raised an IOE. | ||
| * @throws RuntimeException if one of the futures raised one. | ||
| */ | ||
| public static <T> void waitForCompletion( | ||
| final List<CompletableFuture<T>> futures) throws IOException { | ||
| if (futures.isEmpty()) { | ||
| return; | ||
| } | ||
| // await completion | ||
| waitForCompletion( | ||
| CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))); | ||
| } | ||
|
|
||
| /** | ||
| * Wait for a single of future to complete, extracting IOEs afterwards. | ||
| * @param future future to wait for. | ||
| * @throws IOException if one of the called futures raised an IOE. | ||
| * @throws RuntimeException if one of the futures raised one. | ||
| */ | ||
| public static <T> void waitForCompletion(final CompletableFuture<T> future) | ||
| throws IOException { | ||
| try (DurationInfo ignore = new DurationInfo(LOG, false, | ||
| "Waiting for task completion")) { | ||
| future.join(); | ||
| } catch (CancellationException e) { | ||
| throw new IOException(e); | ||
| } catch (CompletionException e) { | ||
| raiseInnerCause(e); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wait for a single of future to complete, ignoring exceptions raised. | ||
| * @param future future to wait for. | ||
| */ | ||
| public static <T> void waitForCompletionIgnoringExceptions( | ||
| @Nullable final CompletableFuture<T> future) { | ||
| if (future != null) { | ||
| try (DurationInfo ignore = new DurationInfo(LOG, false, | ||
| "Waiting for task completion")) { | ||
| future.join(); | ||
| } catch (Exception e) { | ||
| LOG.debug("Ignoring exception raised in task completion: "); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Block awaiting completion for any non-null future passed in; | ||
| * No-op if a null arg was supplied. | ||
| * @param future future | ||
| * @throws IOException if one of the called futures raised an IOE. | ||
| * @throws RuntimeException if one of the futures raised one. | ||
| */ | ||
| public static void maybeAwaitCompletion( | ||
| @Nullable final CompletableFuture<Void> future) throws IOException { | ||
| if (future != null) { | ||
| waitForCompletion(future); | ||
| } | ||
| } | ||
| } |
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.
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.
java.nio.charset.StandardCharsets.UTF_8 can be used instead of shaded guava.
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.
Hey @aajisaka
This is actually a backport of #2732
Will it be ok, if I merge this as is and raise an Addendum PR for trunk as well?
I pulled it up from
ITestPartialRenamesDeletesas is