Skip to content

Commit

Permalink
JAMES-2725 add retry on exception for s3 upload
Browse files Browse the repository at this point in the history
  • Loading branch information
remk committed May 10, 2019
1 parent e483291 commit 8e73a1d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
Expand Up @@ -61,8 +61,9 @@ public class AwsS3ObjectStorage {
public static final int MAX_THREADS = 5;
private static final ExecutorService EXECUTOR_SERVICE = Executors.newFixedThreadPool(MAX_THREADS, NamedThreadFactory.withClassName(AwsS3ObjectStorage.class));
private static final boolean DO_NOT_SHUTDOWN_THREAD_POOL = false;
public static final int MAX_UPLOAD_THREADS = 5;
private static final int MAX_ERROR_RETRY = 5;
private static final int FIRST_TRY = 0;
private static final int MAX_RETRY_ON_EXCEPTION = 3;
public static Size MULTIPART_UPLOAD_THRESHOLD;

static {
Expand All @@ -83,8 +84,7 @@ public static Optional<PutBlobFunction> putBlob(BlobId.Factory blobIdFactory, Co
try {
file = File.createTempFile(UUID.randomUUID().toString(), ".tmp");
FileUtils.copyToFile(blob.getPayload().openStream(), file);

put(blobIdFactory, containerName, configuration, blob, file);
putWithRetry(blobIdFactory, containerName, configuration, blob, file, FIRST_TRY);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
Expand All @@ -95,6 +95,18 @@ public static Optional<PutBlobFunction> putBlob(BlobId.Factory blobIdFactory, Co
});
}

private static void putWithRetry(BlobId.Factory blobIdFactory, ContainerName containerName, AwsS3AuthConfiguration configuration, Blob blob, File file, int tried) {
try {
put(blobIdFactory, containerName, configuration, blob, file);
} catch (RuntimeException e) {
if (tried < MAX_RETRY_ON_EXCEPTION) {
putWithRetry(blobIdFactory, containerName, configuration, blob, file, tried + 1);
} else {
throw e;
}
}
}

private static void put(BlobId.Factory blobIdFactory, ContainerName containerName, AwsS3AuthConfiguration configuration, Blob blob, File file) {
try {
PutObjectRequest request = new PutObjectRequest(containerName.value(),
Expand Down
Expand Up @@ -43,7 +43,6 @@ interface MailsShouldBeWellReceived {
.and().pollDelay(ONE_HUNDRED_MILLISECONDS)
.await();


@Test
default void mailsShouldBeWellReceived(GuiceJamesServer server) throws Exception {
server.getProbe(DataProbeImpl.class).fluent()
Expand Down

0 comments on commit 8e73a1d

Please sign in to comment.