/** Returns true iff performing the backoff was successful. */ private boolean backOffWasSuccessful(BackOff backOff) { try { return next(sleeper, backOff); } catch (InterruptedException | IOException e) { return false; } }
/** Identical to {@link BackOffUtils#next} but without checked IOException. */ private static boolean nextBackOff(Sleeper sleeper, BackOff backOff) throws InterruptedException { try { return BackOffUtils.next(sleeper, backOff); } catch (IOException e) { throw new RuntimeException(e); } } }
/** Identical to {@link BackOffUtils#next} but without checked IOException. */ private static boolean nextBackOff(Sleeper sleeper, BackOff backoff) throws InterruptedException { try { return BackOffUtils.next(sleeper, backoff); } catch (IOException e) { throw new RuntimeException(e); } }
/** Identical to {@link BackOffUtils#next} but without checked exceptions. */ private boolean nextBackOff(Sleeper sleeper, BackOff backoff) { try { return BackOffUtils.next(sleeper, backoff); } catch (InterruptedException | IOException e) { if (e instanceof InterruptedException) { Thread.currentThread().interrupt(); } throw new RuntimeException(e); } }
/** * Returns an OAuth 2.0 access token. * * <p> * Must be run from a background thread, not the main UI thread. * </p> */ public String getToken() throws IOException, GoogleAuthException { if (backOff != null) { backOff.reset(); } while (true) { try { return GoogleAuthUtil.getToken(context, accountName, scope); } catch (IOException e) { // network or server error, so retry using back-off policy try { if (backOff == null || !BackOffUtils.next(sleeper, backOff)) { throw e; } } catch (InterruptedException e2) { // ignore } } } }
public static <T> T executeWithBackOff(AbstractGoogleClientRequest<T> client, String error) throws IOException, InterruptedException { Sleeper sleeper = Sleeper.DEFAULT; BackOff backOff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT .withMaxRetries(MAX_RETRIES).withInitialBackoff(INITIAL_BACKOFF_TIME).backoff()); T result = null; while (true) { try { result = client.execute(); break; } catch (IOException e) { LOG.error("{}", error, e); if (!BackOffUtils.next(sleeper, backOff)) { String errorMessage = String.format( "%s Failing to execute job after %d attempts.", error, MAX_RETRIES + 1); LOG.error("{}", errorMessage, e); throw new IOException(errorMessage, e); } } } return result; }
@Nonnull public QueryResponse queryWithRetries(String query, String projectId, boolean typed) throws IOException, InterruptedException { Sleeper sleeper = Sleeper.DEFAULT; BackOff backoff = BackOffAdapter.toGcpBackOff(BACKOFF_FACTORY.backoff()); IOException lastException = null; QueryRequest bqQueryRequest = new QueryRequest().setQuery(query); do { if (lastException != null) { LOG.warn("Retrying query ({}) after exception", bqQueryRequest.getQuery(), lastException); } try { QueryResponse response = bqClient.jobs().query(projectId, bqQueryRequest).execute(); if (response != null) { return typed ? getTypedTableRows(response) : response; } else { lastException = new IOException("Expected valid response from query job, but received null."); } } catch (IOException e) { // ignore and retry lastException = e; } } while (BackOffUtils.next(sleeper, backoff)); throw new RuntimeException( String.format( "Unable to get BigQuery response after retrying %d times using query (%s)", MAX_QUERY_RETRIES, bqQueryRequest.getQuery()), lastException); }
if (BackOffUtils.next(sleeper, backoff)) { if (!retry) { LOG.info(
retriesAttempted, maxRetries, resourceIdString); try { boolean backOffSuccessful = BackOffUtils.next(sleeper, readBackOff.get()); if (!backOffSuccessful) { logger.atSevere().log(
@Override public Job pollJob(JobReference jobRef, int maxAttempts) throws InterruptedException { BackOff backoff = BackOffAdapter.toGcpBackOff( FluentBackoff.DEFAULT .withMaxRetries(maxAttempts) .withInitialBackoff(Duration.millis(10)) .withMaxBackoff(Duration.standardSeconds(1)) .backoff()); Sleeper sleeper = Sleeper.DEFAULT; try { do { Job job = getJob(jobRef); if (job != null) { JobStatus status = job.getStatus(); if (status != null && ("DONE".equals(status.getState()) || "FAILED".equals(status.getState()))) { return job; } } } while (BackOffUtils.next(sleeper, backoff)); } catch (IOException e) { return null; } return null; }
} while (BackOffUtils.next(sleeper, backoff)); LOG.warn("No terminal state was returned. State value {}", state); return null; // Timed out.