use of com.google.api.client.http.HttpResponseException in project elasticsearch by elastic.
the class RetryHttpInitializerWrapperTests method testRetryWaitTooLong.
public void testRetryWaitTooLong() throws Exception {
TimeValue maxWaitTime = TimeValue.timeValueMillis(10);
int maxRetryTimes = 50;
FailThenSuccessBackoffTransport fakeTransport = new FailThenSuccessBackoffTransport(HttpStatusCodes.STATUS_CODE_SERVER_ERROR, maxRetryTimes);
JsonFactory jsonFactory = new JacksonFactory();
MockGoogleCredential credential = RetryHttpInitializerWrapper.newMockCredentialBuilder().build();
MockSleeper oneTimeSleeper = new MockSleeper() {
@Override
public void sleep(long millis) throws InterruptedException {
Thread.sleep(maxWaitTime.getMillis());
// important number, use this to get count
super.sleep(0);
}
};
RetryHttpInitializerWrapper retryHttpInitializerWrapper = new RetryHttpInitializerWrapper(credential, oneTimeSleeper, maxWaitTime);
Compute client = new Compute.Builder(fakeTransport, jsonFactory, null).setHttpRequestInitializer(retryHttpInitializerWrapper).setApplicationName("test").build();
HttpRequest request1 = client.getRequestFactory().buildRequest("Get", new GenericUrl("http://elasticsearch.com"), null);
try {
request1.execute();
fail("Request should fail if wait too long");
} catch (HttpResponseException e) {
assertThat(e.getStatusCode(), equalTo(HttpStatusCodes.STATUS_CODE_SERVER_ERROR));
// should only retry once.
assertThat(oneTimeSleeper.getCount(), lessThan(maxRetryTimes));
}
}
use of com.google.api.client.http.HttpResponseException in project OpenRefine by OpenRefine.
the class FusionTableSerializer method sendBatch.
private boolean sendBatch(int batchSize) {
try {
// TODO: we really want to do GZIP compression here
// FIXME: text/csv doesn't work even though that's what the content is
AbstractInputStreamContent content = ByteArrayContent.fromString("application/octet-stream", sbBatch.toString());
Long count = FusionTableHandler.insertRows(service, tableId, content);
if (count != null && count.intValue() != batchSize) {
exceptions.add(new IOException("only imported " + count + " of " + batchSize + " rows"));
}
} catch (IOException e) {
exceptions.add(e);
if (e instanceof HttpResponseException) {
int code = ((HttpResponseException) e).getStatusCode();
if (code >= 400 && code < 500) {
return false;
}
// 500s appear to be retried automatically by li
}
} finally {
sbBatch = null;
}
return true;
}
use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.
the class HttpStorageRpc method write.
@Override
public void write(String uploadId, byte[] toWrite, int toWriteOffset, long destOffset, int length, boolean last) {
try {
if (length == 0 && !last) {
return;
}
GenericUrl url = new GenericUrl(uploadId);
HttpRequest httpRequest = storage.getRequestFactory().buildPutRequest(url, new ByteArrayContent(null, toWrite, toWriteOffset, length));
long limit = destOffset + length;
StringBuilder range = new StringBuilder("bytes ");
if (length == 0) {
range.append('*');
} else {
range.append(destOffset).append('-').append(limit - 1);
}
range.append('/');
if (last) {
range.append(limit);
} else {
range.append('*');
}
httpRequest.getHeaders().setContentRange(range.toString());
int code;
String message;
IOException exception = null;
try {
HttpResponse response = httpRequest.execute();
code = response.getStatusCode();
message = response.getStatusMessage();
} catch (HttpResponseException ex) {
exception = ex;
code = ex.getStatusCode();
message = ex.getStatusMessage();
}
if (!last && code != 308 || last && !(code == 200 || code == 201)) {
if (exception != null) {
throw exception;
}
GoogleJsonError error = new GoogleJsonError();
error.setCode(code);
error.setMessage(message);
throw translate(error);
}
} catch (IOException ex) {
throw translate(ex);
}
}
use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.
the class StorageExceptionTest method testStorageException.
@Test
public void testStorageException() {
StorageException exception = new StorageException(500, "message");
assertEquals(500, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(502, "message");
assertEquals(502, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(503, "message");
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(504, "message");
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(429, "message");
assertEquals(429, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(408, "message");
assertEquals(408, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertTrue(exception.isRetryable());
exception = new StorageException(400, "message");
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertFalse(exception.isRetryable());
IOException cause = new SocketTimeoutException();
exception = new StorageException(cause);
assertNull(exception.getReason());
assertNull(exception.getMessage());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
GoogleJsonError error = new GoogleJsonError();
error.setCode(503);
error.setMessage("message");
exception = new StorageException(error);
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertTrue(exception.isRetryable());
exception = new StorageException(400, "message", cause);
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertFalse(exception.isRetryable());
assertSame(cause, exception.getCause());
HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(404, exception.getCode());
assertFalse(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(503, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(502, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(500, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(429, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(429, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(408, null, new HttpHeaders()).build();
exception = new StorageException(httpResponseException);
assertEquals(408, exception.getCode());
assertTrue(exception.isRetryable());
}
use of com.google.api.client.http.HttpResponseException in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryExceptionTest method testBigqueryException.
@Test
public void testBigqueryException() {
BigQueryException exception = new BigQueryException(500, "message");
assertEquals(500, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(502, "message");
assertEquals(502, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(503, "message");
assertEquals(503, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(504, "message");
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
exception = new BigQueryException(400, "message");
assertEquals(400, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertFalse(exception.isRetryable());
BigQueryError error = new BigQueryError("reason", null, null);
exception = new BigQueryException(504, "message", error);
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertEquals("reason", exception.getReason());
assertEquals(error, exception.getError());
assertTrue(exception.isRetryable());
IOException cause = new SocketTimeoutException("socketTimeoutMessage");
exception = new BigQueryException(cause);
assertEquals(BigQueryException.UNKNOWN_CODE, exception.getCode());
assertNull(exception.getReason());
assertEquals("socketTimeoutMessage", exception.getMessage());
assertEquals(cause, exception.getCause());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
exception = new BigQueryException(504, "message", cause);
assertEquals(504, exception.getCode());
assertEquals("message", exception.getMessage());
assertNull(exception.getReason());
assertNull(exception.getError());
assertTrue(exception.isRetryable());
assertSame(cause, exception.getCause());
HttpResponseException httpResponseException = new HttpResponseException.Builder(404, "Service Unavailable", new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(404, exception.getCode());
assertFalse(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(504, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(504, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(503, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(503, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(502, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(502, exception.getCode());
assertTrue(exception.isRetryable());
httpResponseException = new HttpResponseException.Builder(500, null, new HttpHeaders()).build();
exception = new BigQueryException(httpResponseException);
assertEquals(500, exception.getCode());
assertTrue(exception.isRetryable());
}
Aggregations