Skip to content

Commit

Permalink
Don't retry S3 operations that return an http 403
Browse files Browse the repository at this point in the history
  • Loading branch information
nezihyigitbasi authored and martint committed Jan 8, 2015
1 parent 7ae39a9 commit c32f7f7
Showing 1 changed file with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.amazonaws.AbortedException;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
Expand Down Expand Up @@ -82,6 +83,8 @@
import static java.lang.Math.max;
import static java.nio.file.Files.createDirectories;
import static java.nio.file.Files.createTempFile;
import static org.apache.http.HttpStatus.SC_FORBIDDEN;
import static org.apache.http.HttpStatus.SC_NOT_FOUND;

public class PrestoS3FileSystem
extends FileSystem
Expand Down Expand Up @@ -365,22 +368,37 @@ private Iterator<LocatedFileStatus> statusFromObjects(List<S3ObjectSummary> obje
return list.iterator();
}

/**
* This exception is for stopping retries for S3 calls that shouldn't be retried.
* For example, "Caused by: com.amazonaws.services.s3.model.AmazonS3Exception: Forbidden (Service: Amazon S3; Status Code: 403 ..."
*/
private static class UnrecoverableS3OperationException extends Exception
{
public UnrecoverableS3OperationException(Throwable cause)
{
super(cause);
}
}

private ObjectMetadata getS3ObjectMetadata(final Path path)
throws IOException
{
try {
return retry()
.maxAttempts(maxClientRetries)
.exponentialBackoff(new Duration(1, TimeUnit.SECONDS), maxBackoffTime, maxRetryTime, 2.0)
.stopOn(InterruptedException.class)
.stopOn(InterruptedException.class, UnrecoverableS3OperationException.class)
.run("getS3ObjectMetadata", () -> {
try {
return s3.getObjectMetadata(uri.getHost(), keyFromPath(path));
}
catch (AmazonS3Exception e) {
if (e.getStatusCode() == 404) {
if (e.getStatusCode() == SC_NOT_FOUND) {
return null;
}
else if (e.getStatusCode() == SC_FORBIDDEN) {
throw new UnrecoverableS3OperationException(e);
}
throw Throwables.propagate(e);
}
});
Expand Down Expand Up @@ -565,8 +583,18 @@ private S3Object getS3Object(final Path path, final long start)
return retry()
.maxAttempts(maxClientRetry)
.exponentialBackoff(new Duration(1, TimeUnit.SECONDS), maxBackoffTime, maxRetryTime, 2.0)
.stopOn(InterruptedException.class)
.run("getS3Object", () -> s3.getObject(new GetObjectRequest(host, keyFromPath(path)).withRange(start, Long.MAX_VALUE)));
.stopOn(InterruptedException.class, UnrecoverableS3OperationException.class)
.run("getS3Object", () -> {
try {
return s3.getObject(new GetObjectRequest(host, keyFromPath(path)).withRange(start, Long.MAX_VALUE));
}
catch (AmazonServiceException e) {
if (e.getStatusCode() == SC_FORBIDDEN) {
throw new UnrecoverableS3OperationException(e);
}
throw Throwables.propagate(e);
}
});
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
Expand Down

0 comments on commit c32f7f7

Please sign in to comment.