use of com.google.api.client.util.ExponentialBackOff in project beam by apache.
the class RetryHttpInitializerWrapper method initialize.
/**
* Initializes the given request.
*/
@Override
public final void initialize(final HttpRequest request) {
// 2 minutes read timeout
request.setReadTimeout(2 * ONEMINITUES);
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException {
if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
// and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of
// our internal backoff handler.
LOG.info("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
use of com.google.api.client.util.ExponentialBackOff in project ddf by codice.
the class MetadataConfigurationParser method buildEntityDescriptor.
private void buildEntityDescriptor(String entityDescription) throws IOException {
EntityDescriptor entityDescriptor = null;
entityDescription = entityDescription.trim();
if (entityDescription.startsWith(HTTPS) || entityDescription.startsWith(HTTP)) {
if (entityDescription.startsWith(HTTP)) {
LOGGER.warn("Retrieving metadata via HTTP instead of HTTPS. The metadata configuration is unsafe!!!");
}
PropertyResolver propertyResolver = new PropertyResolver(entityDescription);
HttpTransport httpTransport = new NetHttpTransport();
HttpRequest httpRequest = httpTransport.createRequestFactory().buildGetRequest(new GenericUrl(propertyResolver.getResolvedString()));
httpRequest.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setBackOffRequired(HttpBackOffUnsuccessfulResponseHandler.BackOffRequired.ALWAYS));
httpRequest.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
ListenableFuture<HttpResponse> httpResponseFuture = service.submit(httpRequest::execute);
Futures.addCallback(httpResponseFuture, new FutureCallback<HttpResponse>() {
@Override
public void onSuccess(HttpResponse httpResponse) {
if (httpResponse != null) {
try {
String parsedResponse = httpResponse.parseAsString();
buildEntityDescriptor(parsedResponse);
} catch (IOException e) {
LOGGER.info("Unable to parse metadata from: {}", httpResponse.getRequest().getUrl().toString(), e);
}
}
}
@Override
public void onFailure(Throwable throwable) {
LOGGER.info("Unable to retrieve metadata.", throwable);
}
});
service.shutdown();
} else if (entityDescription.startsWith(FILE + System.getProperty("ddf.home"))) {
String pathStr = StringUtils.substringAfter(entityDescription, FILE);
Path path = Paths.get(pathStr);
if (Files.isReadable(path)) {
try (InputStream fileInputStream = Files.newInputStream(path)) {
entityDescriptor = readEntityDescriptor(new InputStreamReader(fileInputStream, "UTF-8"));
}
}
} else if (entityDescription.startsWith("<") && entityDescription.endsWith(">")) {
entityDescriptor = readEntityDescriptor(new StringReader(entityDescription));
} else {
LOGGER.info("Skipping unknown metadata configuration value: {}", entityDescription);
}
if (entityDescriptor != null) {
entityDescriptorMap.put(entityDescriptor.getEntityID(), entityDescriptor);
if (updateCallback != null) {
updateCallback.accept(entityDescriptor);
}
}
}
use of com.google.api.client.util.ExponentialBackOff in project google-cloud-java by GoogleCloudPlatform.
the class SpannerImpl method runWithRetries.
/**
* Helper to execute some work, retrying with backoff on retryable errors.
*
* <p>TODO: Consider replacing with RetryHelper from gcloud-core.
*/
static <T> T runWithRetries(Callable<T> callable) {
// Use same backoff setting as abort, somewhat arbitrarily.
ExponentialBackOff backOff = newBackOff();
Context context = Context.current();
while (true) {
try {
return callable.call();
} catch (SpannerException e) {
if (!e.isRetryable()) {
throw e;
}
logger.log(Level.FINE, "Retryable exception, will sleep and retry", e);
backoffSleep(context, backOff);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
}
use of com.google.api.client.util.ExponentialBackOff in project gradle by gradle.
the class RetryHttpInitializerWrapper method initialize.
@Override
public void initialize(HttpRequest request) {
// Turn off request logging, this can end up logging OAUTH
// tokens which should not ever be in a build log
final boolean loggingEnabled = false;
request.setLoggingEnabled(loggingEnabled);
request.setCurlLoggingEnabled(loggingEnabled);
disableHttpTransportLogging();
request.setReadTimeout((int) DEFAULT_READ_TIMEOUT_MILLIS);
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
final Credential credential = credentialSupplier.get();
request.setInterceptor(credential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry) throws IOException {
// Turn off request logging unless debug mode is enabled
request.setLoggingEnabled(loggingEnabled);
request.setCurlLoggingEnabled(loggingEnabled);
if (credential.handleResponse(request, response, supportsRetry)) {
// something specific to authentication, and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of our internal backoff handler.
LOG.info("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
use of com.google.api.client.util.ExponentialBackOff in project DataflowJavaSDK-examples by GoogleCloudPlatform.
the class RetryHttpInitializerWrapper method initialize.
/**
* Initializes the given request.
*/
@Override
public final void initialize(final HttpRequest request) {
// 2 minutes read timeout
request.setReadTimeout(2 * ONEMINITUES);
final HttpUnsuccessfulResponseHandler backoffHandler = new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setSleeper(sleeper);
request.setInterceptor(wrappedCredential);
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
@Override
public boolean handleResponse(final HttpRequest request, final HttpResponse response, final boolean supportsRetry) throws IOException {
if (wrappedCredential.handleResponse(request, response, supportsRetry)) {
// and no backoff is desired.
return true;
} else if (backoffHandler.handleResponse(request, response, supportsRetry)) {
// Otherwise, we defer to the judgement of
// our internal backoff handler.
LOG.info("Retrying " + request.getUrl().toString());
return true;
} else {
return false;
}
}
});
request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(sleeper));
}
Aggregations