com.google.api.client.util.BackOffUtils Java Examples

The following examples show how to use com.google.api.client.util.BackOffUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RetryUnsuccessfulResponseHandler.java    From firebase-admin-java with Apache License 2.0 6 votes vote down vote up
private boolean waitAndRetry(HttpResponse response) throws IOException, InterruptedException {
  String retryAfterHeader = response.getHeaders().getRetryAfter();
  if (!Strings.isNullOrEmpty(retryAfterHeader)) {
    long intervalMillis = parseRetryAfterHeaderIntoMillis(retryAfterHeader.trim());
    // Retry-after header can specify very long delay intervals (e.g. 24 hours). If we cannot
    // wait that long, we should not perform any retries at all. In general it is not correct to
    // retry earlier than what the server has recommended to us.
    if (intervalMillis > retryConfig.getMaxIntervalMillis()) {
      return false;
    }

    if (intervalMillis > 0) {
      sleeper.sleep(intervalMillis);
      return true;
    }
  }

  return BackOffUtils.next(sleeper, backOff);
}
 
Example #2
Source File: FakeJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #3
Source File: BigQueryToTableIT.java    From beam with Apache License 2.0 6 votes vote down vote up
private List<TableRow> getTableRowsFromQuery(String query, int maxRetry) throws Exception {
  FluentBackoff backoffFactory =
      FluentBackoff.DEFAULT
          .withMaxRetries(maxRetry)
          .withInitialBackoff(Duration.standardSeconds(1L));
  Sleeper sleeper = Sleeper.DEFAULT;
  BackOff backoff = BackOffAdapter.toGcpBackOff(backoffFactory.backoff());
  do {
    LOG.info("Starting querying {}", query);
    QueryResponse response = BQ_CLIENT.queryWithRetries(query, project);
    if (response.getRows() != null) {
      LOG.info("Got table content with query {}", query);
      return response.getRows();
    }
  } while (BackOffUtils.next(sleeper, backoff));
  LOG.info("Got empty table for query {} with retry {}", query, maxRetry);
  return Collections.emptyList();
}
 
Example #4
Source File: HttpBackOffUnsuccessfulResponseHandler.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Handles the request with {@link BackOff}. That means that if back-off is required a call to
 * {@link Sleeper#sleep(long)} will be made.
 */
@Override
public boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)
    throws IOException {
  if (!supportsRetry) {
    return false;
  }
  // check if back-off is required for this response
  if (backOffRequired.isRequired(response)) {
    try {
      return BackOffUtils.next(sleeper, backOff);
    } catch (InterruptedException exception) {
      // Mark thread as interrupted since we cannot throw InterruptedException here.
      Thread.currentThread().interrupt();
    }
  }
  return false;
}
 
Example #5
Source File: GoogleAccountCredential.java    From google-api-java-client with Apache License 2.0 6 votes vote down vote up
/**
 * 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
      }
    }
  }
}
 
Example #6
Source File: BaseWorkflowSample.java    From googleads-shopping-samples with Apache License 2.0 6 votes vote down vote up
protected <T extends GenericJson> T retryFailures(
    AbstractGoogleClientRequest<T> request, BackOff backOff) throws IOException {
  while (true) {
    try {
      return request.execute();
    } catch (GoogleJsonResponseException e) {
      try {
        long nextPause = backOff.nextBackOffMillis();
        if (nextPause == BackOff.STOP) {
          throw e;
        }
        System.out.printf("Operation failed, retrying in %f seconds.%n", nextPause / 1000.0);
        BackOffUtils.next(Sleeper.DEFAULT, backOff);
      } catch (InterruptedException ie) {
        // Just go straight into retry if interrupted.
      }
    }
  }
}
 
Example #7
Source File: TrafficMaxLaneFlowIT.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testE2ETrafficMaxLaneFlow() throws Exception {
  this.options.setBigQuerySchema(FormatMaxesFn.getSchema());
  this.options.setProject(this.projectId);
  this.options.setBigQueryDataset(this.outputDatasetId);
  this.options.setBigQueryTable(this.outputTable);
  TrafficMaxLaneFlow.runTrafficMaxLaneFlow(this.options);
  FluentBackoff backoffFactory =
      FluentBackoff.DEFAULT.withMaxRetries(4).withInitialBackoff(Duration.standardSeconds(1L));
  Sleeper sleeper = Sleeper.DEFAULT;
  BackOff backoff = BackOffAdapter.toGcpBackOff(backoffFactory.backoff());
  String res = "empty_result";
  // Having 4 retries to get ride of the failure caused by the latency that between data wrote
  // to BigQuery and be able to query from BigQuery.
  // Partial results are still returned making traversal of nested result object NPE prone.
  do {
    QueryResponse response =
        this.bqClient.queryWithRetries(
            String.format(
                "SELECT count(*) as total FROM [%s:%s.%s]",
                this.projectId, this.outputDatasetId, this.outputTable),
            this.projectId);
    // Having 4 retries to get ride of the failure caused by the latency that between data wrote
    // to BigQuery and be able to query from BigQuery.
    // Partial results are still returned making traversal of nested result object NPE prone.
    try {
      res = response.getRows().get(0).getF().get(0).getV().toString();
      break;
    } catch (NullPointerException e) {
      // Ignore NullPointerException during retry.
    }
  } while (BackOffUtils.next(sleeper, backoff));
  assertEquals("9763", res);
}
 
Example #8
Source File: TrafficRoutesIT.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testE2ETrafficRoutes() throws Exception {
  this.options.setBigQuerySchema(FormatStatsFn.getSchema());
  this.options.setProject(this.projectId);
  this.options.setBigQueryDataset(this.outputDatasetId);
  this.options.setBigQueryTable(this.outputTable);
  TrafficRoutes.runTrafficRoutes(options);
  FluentBackoff backoffFactory =
      FluentBackoff.DEFAULT.withMaxRetries(4).withInitialBackoff(Duration.standardSeconds(1L));
  Sleeper sleeper = Sleeper.DEFAULT;
  BackOff backoff = BackOffAdapter.toGcpBackOff(backoffFactory.backoff());
  String res = "empty_result";
  do {
    QueryResponse response =
        this.bqClient.queryWithRetries(
            String.format(
                "SELECT count(*) as total FROM [%s:%s.%s]",
                this.projectId, this.outputDatasetId, this.outputTable),
            this.projectId);
    // Having 4 retries to get ride of the failure caused by the latency that between data wrote
    // to BigQuery and be able to query from BigQuery.
    // Partial results are still returned making traversal of nested result object NPE prone.
    try {
      res = response.getRows().get(0).getF().get(0).getV().toString();
      break;
    } catch (NullPointerException e) {
      // Ignore NullPointerException during retry.
    }
  } while (BackOffUtils.next(sleeper, backoff));

  assertEquals("27", res);
}
 
Example #9
Source File: BigQueryHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example #10
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example #11
Source File: DynamicDestinationsHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example #12
Source File: DataflowPipelineJob.java    From beam with Apache License 2.0 5 votes vote down vote up
/** 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);
  }
}
 
Example #13
Source File: HttpBackOffIOExceptionHandler.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Handles the request with {@link BackOff}. That means that if back-off is required a call to
 * {@link Sleeper#sleep(long)} will be made.
 */
public boolean handleIOException(HttpRequest request, boolean supportsRetry) throws IOException {
  if (!supportsRetry) {
    return false;
  }
  try {
    return BackOffUtils.next(sleeper, backOff);
  } catch (InterruptedException exception) {
    // Mark thread as interrupted since we cannot throw InterruptedException here.
    Thread.currentThread().interrupt();
    return false;
  }
}
 
Example #14
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
@Nullable
Table tryCreateTable(Table table, BackOff backoff, Sleeper sleeper) throws IOException {
  boolean retry = false;
  while (true) {
    try {
      return client
          .tables()
          .insert(
              table.getTableReference().getProjectId(),
              table.getTableReference().getDatasetId(),
              table)
          .execute();
    } catch (IOException e) {
      ApiErrorExtractor extractor = new ApiErrorExtractor();
      if (extractor.itemAlreadyExists(e)) {
        // The table already exists, nothing to return.
        return null;
      } else if (extractor.rateLimited(e)) {
        // The request failed because we hit a temporary quota. Back off and try again.
        try {
          if (BackOffUtils.next(sleeper, backoff)) {
            if (!retry) {
              LOG.info(
                  "Quota limit reached when creating table {}:{}.{}, retrying up to {} minutes",
                  table.getTableReference().getProjectId(),
                  table.getTableReference().getDatasetId(),
                  table.getTableReference().getTableId(),
                  TimeUnit.MILLISECONDS.toSeconds(RETRY_CREATE_TABLE_DURATION_MILLIS) / 60.0);
              retry = true;
            }
            continue;
          }
        } catch (InterruptedException e1) {
          // Restore interrupted state and throw the last failure.
          Thread.currentThread().interrupt();
          throw e;
        }
      }
      throw e;
    }
  }
}
 
Example #15
Source File: DataflowPipelineJob.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Waits until the pipeline finishes and returns the final status.
 *
 * @param duration The time to wait for the job to finish. Provide a value less than 1 ms for an
 *     infinite wait.
 * @param messageHandler If non null this handler will be invoked for each batch of messages
 *     received.
 * @param sleeper A sleeper to use to sleep between attempts.
 * @param nanoClock A nanoClock used to time the total time taken.
 * @return The final state of the job or null on timeout.
 * @throws IOException If there is a persistent problem getting job information.
 * @throws InterruptedException if the thread is interrupted.
 */
@Nullable
@VisibleForTesting
State waitUntilFinish(
    Duration duration,
    @Nullable MonitoringUtil.JobMessagesHandler messageHandler,
    Sleeper sleeper,
    NanoClock nanoClock,
    MonitoringUtil monitor)
    throws IOException, InterruptedException {

  BackOff backoff = getMessagesBackoff(duration);

  // This function tracks the cumulative time from the *first request* to enforce the wall-clock
  // limit. Any backoff instance could, at best, track the the time since the first attempt at a
  // given request. Thus, we need to track the cumulative time ourselves.
  long startNanos = nanoClock.nanoTime();

  State state = State.UNKNOWN;
  Exception exception;
  do {
    exception = null;
    try {
      // Get the state of the job before listing messages. This ensures we always fetch job
      // messages after the job finishes to ensure we have all them.
      state =
          getStateWithRetries(
              BackOffAdapter.toGcpBackOff(STATUS_BACKOFF_FACTORY.withMaxRetries(0).backoff()),
              sleeper);
    } catch (IOException e) {
      exception = e;
      LOG.warn("Failed to get job state: {}", e.getMessage());
      LOG.debug("Failed to get job state: {}", e);
      continue;
    }

    exception = processJobMessages(messageHandler, monitor);

    if (exception != null) {
      continue;
    }

    // We can stop if the job is done.
    if (state.isTerminal()) {
      logTerminalState(state);
      return state;
    }

    // Reset attempts count and update cumulative wait time.
    backoff = resetBackoff(duration, nanoClock, startNanos);
  } while (BackOffUtils.next(sleeper, backoff));

  // At this point Backoff decided that we retried enough times.
  // This can be either due to exceeding allowed timeout for job to complete, or receiving
  // error multiple times in a row.

  if (exception == null) {
    LOG.warn("No terminal state was returned within allotted timeout. State value {}", state);
  } else {
    LOG.error("Failed to fetch job metadata with error: {}", exception);
  }

  return null;
}