Java Code Examples for com.google.api.client.util.Sleeper#DEFAULT

The following examples show how to use com.google.api.client.util.Sleeper#DEFAULT . 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: 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 2
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 3
Source File: FirebaseProjectManagementServiceImpl.java    From firebase-admin-java with Apache License 2.0 5 votes vote down vote up
FirebaseProjectManagementServiceImpl(FirebaseApp app) {
  this(
      app,
      Sleeper.DEFAULT,
      new FirebaseAppScheduler(app),
      ApiClientUtils.newAuthorizedRequestFactory(app));
}
 
Example 4
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 5
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 6
Source File: BigQueryHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
void waitForDone() throws Exception {
  LOG.info("Waiting for jobs to complete.");
  Sleeper sleeper = Sleeper.DEFAULT;
  while (!pendingJobs.isEmpty()) {
    List<JobInfo> retryJobs = Lists.newArrayList();
    for (JobInfo jobInfo : pendingJobs) {
      if (jobInfo.pendingJob.pollJob()) {
        // Job has completed successfully.
        LOG.info("Job {} completed successfully.", jobInfo.pendingJob.currentJobId);
        Exception e = jobInfo.onSuccess.apply(jobInfo.pendingJob);
        if (e != null) {
          throw e;
        }
      } else {
        // Job not yet complete, schedule it again.
        LOG.info("Job {} pending. retrying.", jobInfo.pendingJob.currentJobId);
        retryJobs.add(jobInfo);
      }
    }
    pendingJobs = retryJobs;
    if (!pendingJobs.isEmpty()) {
      // Sleep before retrying.
      nextBackOff(sleeper, backOff);
      // Run the jobs to retry. If a job has hit the maximum number of retries then runJob
      // will raise an exception.
      for (JobInfo job : pendingJobs) {
        job.pendingJob.runJob();
      }
    }
  }
}
 
Example 7
Source File: BigQueryUtils.java    From hadoop-connectors with Apache License 2.0 4 votes vote down vote up
/**
 * Polls job until it is completed.
 *
 * @param bigquery the Bigquery instance to poll.
 * @param projectId the project that is polling.
 * @param jobReference the job to poll.
 * @param progressable to get progress of task.
 * @throws IOException on IO Error.
 * @throws InterruptedException on sleep interrupt.
 */
public static void waitForJobCompletion(
    Bigquery bigquery, String projectId, JobReference jobReference, Progressable progressable)
    throws IOException, InterruptedException {

  Sleeper sleeper = Sleeper.DEFAULT;
  BackOff pollBackOff =
      new ExponentialBackOff.Builder()
          .setMaxIntervalMillis(POLL_WAIT_INTERVAL_MAX_MILLIS)
          .setInitialIntervalMillis(POLL_WAIT_INITIAL_MILLIS)
          .setMaxElapsedTimeMillis(POLL_WAIT_MAX_ELAPSED_MILLIS)
          .build();

  // Get starting time.
  long startTime = System.currentTimeMillis();
  long elapsedTime = 0;
  boolean notDone = true;

  // While job is incomplete continue to poll.
  while (notDone) {
    BackOff operationBackOff = new ExponentialBackOff();
    Get get =
        bigquery
            .jobs()
            .get(projectId, jobReference.getJobId())
            .setLocation(jobReference.getLocation());

    Job pollJob =
        ResilientOperation.retry(
            ResilientOperation.getGoogleRequestCallable(get),
            operationBackOff,
            RetryDeterminer.RATE_LIMIT_ERRORS,
            IOException.class,
            sleeper);

    elapsedTime = System.currentTimeMillis() - startTime;
    logger.atFine().log(
        "Job status (%s ms) %s: %s",
        elapsedTime, jobReference.getJobId(), pollJob.getStatus().getState());
    if (pollJob.getStatus().getState().equals("DONE")) {
      notDone = false;
      if (pollJob.getStatus().getErrorResult() != null) {
        throw new IOException(
            String.format(
                "Job %s failed: %s. Errors: %s",
                jobReference.getJobId(),
                pollJob.getStatus().getErrorResult(),
                pollJob.getStatus().getErrors()));
      }
    } else {
      long millisToWait = pollBackOff.nextBackOffMillis();
      if (millisToWait == BackOff.STOP) {
        throw new IOException(
            String.format(
                "Job %s failed to complete after %s millis.",
                jobReference.getJobId(), elapsedTime));
      }
      // Pause execution for the configured duration before polling job status again.
      Thread.sleep(millisToWait);
      // Call progress to ensure task doesn't time out.
      progressable.progress();
    }
  }
}
 
Example 8
Source File: StockInjector.java    From cloud-pubsub-samples-java with Apache License 2.0 4 votes vote down vote up
public RetryHttpInitializerWrapper(GoogleCredential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 9
Source File: WebSocketInjectorStub.java    From cloud-pubsub-samples-java with Apache License 2.0 4 votes vote down vote up
public RetryHttpInitializerWrapper(GoogleCredential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 10
Source File: NewsInjector.java    From cloud-pubsub-samples-java with Apache License 2.0 4 votes vote down vote up
public RetryHttpInitializerWrapper(GoogleCredential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 11
Source File: RetryHttpInitializerWrapper.java    From play-work with Apache License 2.0 4 votes vote down vote up
public RetryHttpInitializerWrapper(Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 12
Source File: RetryHttpRequestInitializer.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * @param additionalIgnoredResponseCodes a list of HTTP status codes that should not be logged.
 * @param responseInterceptor HttpResponseInterceptor to be applied on all requests. May be null.
 */
public RetryHttpRequestInitializer(
    Collection<Integer> additionalIgnoredResponseCodes,
    @Nullable HttpResponseInterceptor responseInterceptor) {
  this(NanoClock.SYSTEM, Sleeper.DEFAULT, additionalIgnoredResponseCodes, responseInterceptor);
}
 
Example 13
Source File: RetryHttpInitializerWrapper.java    From cloud-pubsub-mqtt-proxy with Apache License 2.0 2 votes vote down vote up
/**
 * Initialize variables for retry.
 *
 * @param wrappedCredential Google Credentials for retry.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this.wrappedCredential = wrappedCredential;
  this.sleeper = Sleeper.DEFAULT;
}
 
Example 14
Source File: RetryHttpInitializerWrapper.java    From java-docs-samples with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor using the default Sleeper.
 *
 * @param wrappedCredential the credential used to authenticate with a Google Cloud Platform
 *     project
 */
public RetryHttpInitializerWrapper(Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 15
Source File: RetryHttpInitializerWrapper.java    From java-docs-samples with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 16
Source File: RetryHttpInitializerWrapper.java    From java-docs-samples with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 17
Source File: RetryHttpInitializerWrapper.java    From java-docs-samples with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 18
Source File: RetryHttpInitializerWrapper.java    From cloud-pubsub-samples-java with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and
 * used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
    this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 19
Source File: RetryHttpInitializerWrapper.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}
 
Example 20
Source File: RetryHttpInitializerWrapper.java    From daq with Apache License 2.0 2 votes vote down vote up
/**
 * A constructor.
 *
 * @param wrappedCredential Credential which will be wrapped and used for providing auth header.
 */
public RetryHttpInitializerWrapper(final Credential wrappedCredential) {
  this(wrappedCredential, Sleeper.DEFAULT);
}