From 4a1b7d6dee8bc55b9d2f6e6d28b0829775e8d67a Mon Sep 17 00:00:00 2001 From: Mingliang Liu Date: Fri, 2 Dec 2016 13:36:04 -0800 Subject: [PATCH] HADOOP-13857. S3AUtils.translateException to map (wrapped) InterruptedExceptions to InterruptedIOEs. Contributed by Steve Loughran (cherry picked from commit 2ff84a00405e977b1fd791cfb974244580dd5ae8) --- .../org/apache/hadoop/fs/s3a/S3AUtils.java | 23 +++++++++++ .../fs/s3a/TestS3AExceptionTranslation.java | 38 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AUtils.java b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AUtils.java index 49f8862c3b4..dedbfd420d0 100644 --- a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AUtils.java +++ b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AUtils.java @@ -40,6 +40,7 @@ import org.slf4j.Logger; import java.io.EOFException; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.InterruptedIOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -113,6 +114,10 @@ public final class S3AUtils { path != null ? (" on " + path) : "", exception); if (!(exception instanceof AmazonServiceException)) { + if (containsInterruptedException(exception)) { + return (IOException)new InterruptedIOException(message) + .initCause(exception); + } return new AWSClientIOException(message, exception); } else { @@ -194,6 +199,24 @@ public final class S3AUtils { return ioe; } + /** + * Recurse down the exception loop looking for any inner details about + * an interrupted exception. + * @param thrown exception thrown + * @return true if down the execution chain the operation was an interrupt + */ + static boolean containsInterruptedException(Throwable thrown) { + if (thrown == null) { + return false; + } + if (thrown instanceof InterruptedException || + thrown instanceof InterruptedIOException) { + return true; + } + // tail recurse + return containsInterruptedException(thrown.getCause()); + } + /** * Get low level details of an amazon exception for logging; multi-line. * @param e exception diff --git a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestS3AExceptionTranslation.java b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestS3AExceptionTranslation.java index a7dafa06c18..e548ac24b9d 100644 --- a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestS3AExceptionTranslation.java +++ b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/TestS3AExceptionTranslation.java @@ -25,9 +25,12 @@ import static org.junit.Assert.*; import java.io.EOFException; import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InterruptedIOException; import java.nio.file.AccessDeniedException; import java.util.Collections; import java.util.Map; +import java.util.concurrent.ExecutionException; import com.amazonaws.AmazonClientException; import com.amazonaws.AmazonServiceException; @@ -124,4 +127,39 @@ public class TestS3AExceptionTranslation { return verifyExceptionClass(clazz, translateException("test", "/", exception)); } + + private void assertContainsInterrupted(boolean expected, Throwable thrown) + throws Throwable { + if (containsInterruptedException(thrown) != expected) { + throw thrown; + } + } + + @Test + public void testInterruptExceptionDetecting() throws Throwable { + InterruptedException interrupted = new InterruptedException("irq"); + assertContainsInterrupted(true, interrupted); + IOException ioe = new IOException("ioe"); + assertContainsInterrupted(false, ioe); + assertContainsInterrupted(true, ioe.initCause(interrupted)); + assertContainsInterrupted(true, + new InterruptedIOException("ioirq")); + } + + @Test(expected = InterruptedIOException.class) + public void testExtractInterrupted() throws Throwable { + throw extractException("", "", + new ExecutionException( + new AmazonClientException( + new InterruptedException("")))); + } + + @Test(expected = InterruptedIOException.class) + public void testExtractInterruptedIO() throws Throwable { + throw extractException("", "", + new ExecutionException( + new AmazonClientException( + new InterruptedIOException("")))); + } + }