From 0a05851b3ad9d6138f57c726ea888d4675ef375b Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 24 Nov 2015 15:38:54 +0100 Subject: [PATCH 1/2] Translate SocketTimeoutException to retryable service exception --- .../com/google/gcloud/spi/DefaultDatastoreRpc.java | 13 ++++++++++--- .../com/google/gcloud/spi/DefaultStorageRpc.java | 7 ++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index 4b39f7a3c4be..fbe3bff780da 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -42,6 +42,7 @@ import org.json.JSONTokener; import java.net.InetAddress; +import java.net.SocketTimeoutException; import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -121,9 +122,15 @@ private static DatastoreRpcException translate(DatastoreException exception) { if (reason == null) { reason = HTTP_STATUS_TO_REASON.get(exception.getCode()); } - return reason != null - ? new DatastoreRpcException(reason) - : new DatastoreRpcException("Unknown", exception.getCode(), false, message); + if (reason != null) { + return new DatastoreRpcException(reason); + } else { + boolean retryable = false; + if (exception.getCause() instanceof SocketTimeoutException) { + retryable = true; + } + return new DatastoreRpcException("Unknown", exception.getCode(), retryable, message); + } } @Override diff --git a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java index 1e2549550544..d874f99ebb4c 100644 --- a/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java +++ b/gcloud-java-storage/src/main/java/com/google/gcloud/spi/DefaultStorageRpc.java @@ -68,6 +68,7 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -101,7 +102,11 @@ private static StorageException translate(IOException exception) { && ((GoogleJsonResponseException) exception).getDetails() != null) { translated = translate(((GoogleJsonResponseException) exception).getDetails()); } else { - translated = new StorageException(0, exception.getMessage(), false); + boolean retryable = false; + if (exception instanceof SocketTimeoutException) { + retryable = true; + } + translated = new StorageException(0, exception.getMessage(), retryable); } translated.initCause(exception); return translated; From a0f2d206477129511fde909ae57a9ce46e8858cf Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Tue, 24 Nov 2015 19:01:55 +0100 Subject: [PATCH 2/2] Set special reason for timeout exceptions --- .../main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java index fbe3bff780da..028027f4bc33 100644 --- a/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java +++ b/gcloud-java-datastore/src/main/java/com/google/gcloud/spi/DefaultDatastoreRpc.java @@ -126,10 +126,12 @@ private static DatastoreRpcException translate(DatastoreException exception) { return new DatastoreRpcException(reason); } else { boolean retryable = false; + reasonStr = "Unknown"; if (exception.getCause() instanceof SocketTimeoutException) { retryable = true; + reasonStr = "Request timeout"; } - return new DatastoreRpcException("Unknown", exception.getCode(), retryable, message); + return new DatastoreRpcException(reasonStr, exception.getCode(), retryable, message); } }