Java Code Examples for com.google.api.services.bigquery.model.Job#getStatus()

The following examples show how to use com.google.api.services.bigquery.model.Job#getStatus() . 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: BigqueryConnection.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Checks completed job for errors.
 *
 * @throws BigqueryJobFailureException
 */
private static Job checkJob(Job job) {
  verify(job.getStatus() != null);
  JobStatus jobStatus = job.getStatus();
  if (jobStatus.getErrorResult() != null) {
    throw BigqueryJobFailureException.create(jobStatus);
  } else {
    logger.atInfo().log(summarizeCompletedJob(job));
    if (jobStatus.getErrors() != null) {
      for (ErrorProto error : jobStatus.getErrors()) {
        logger.atWarning().log("%s: %s", error.getReason(), error.getMessage());
      }
    }
    return job;
  }
}
 
Example 3
Source File: BigQueryHelpers.java    From beam with Apache License 2.0 5 votes vote down vote up
static Status parseStatus(@Nullable Job job) {
  if (job == null) {
    return Status.UNKNOWN;
  }
  JobStatus status = job.getStatus();
  if (status.getErrorResult() != null) {
    return Status.FAILED;
  } else if (status.getErrors() != null && !status.getErrors().isEmpty()) {
    return Status.FAILED;
  } else {
    return Status.SUCCEEDED;
  }
}
 
Example 4
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
Job pollJob(JobReference jobRef, Sleeper sleeper, BackOff backoff) throws InterruptedException {
  do {
    try {
      Job job =
          client
              .jobs()
              .get(jobRef.getProjectId(), jobRef.getJobId())
              .setLocation(jobRef.getLocation())
              .execute();
      if (job == null) {
        LOG.info("Still waiting for BigQuery job {} to start", jobRef);
        continue;
      }
      JobStatus status = job.getStatus();
      if (status == null) {
        LOG.info("Still waiting for BigQuery job {} to enter pending state", jobRef);
        continue;
      }
      if ("DONE".equals(status.getState())) {
        LOG.info("BigQuery job {} completed in state DONE", jobRef);
        return job;
      }
      // The job is not DONE, wait longer and retry.
      LOG.info(
          "Still waiting for BigQuery job {}, currently in status {}\n{}",
          jobRef.getJobId(),
          status,
          formatBqStatusCommand(jobRef.getProjectId(), jobRef.getJobId()));
    } catch (IOException e) {
      // ignore and retry
      LOG.info("Ignore the error and retry polling job status.", e);
    }
  } while (nextBackOff(sleeper, backoff));
  LOG.warn("Unable to poll job status: {}, aborting after reached max .", jobRef.getJobId());
  return null;
}
 
Example 5
Source File: BigQueryHelpers.java    From beam with Apache License 2.0 4 votes vote down vote up
static RetryJobIdResult getRetryJobId(
    RetryJobId currentJobId, SerializableFunction<RetryJobId, Job> lookupJob) {
  for (int retryIndex = currentJobId.getRetryIndex(); ; retryIndex++) {
    RetryJobId jobId = new RetryJobId(currentJobId.getJobIdPrefix(), retryIndex);
    try {
      Job loadJob = lookupJob.apply(jobId);
      if (loadJob == null) {
        LOG.info("job id {} not found, so retrying with that id", jobId);
        // This either means that the original job was never properly issued (on the first
        // iteration of the loop) or that we've found a retry id that has not been used yet. Try
        // again with this job id.
        return new RetryJobIdResult(jobId, true);
      }
      JobStatus jobStatus = loadJob.getStatus();
      if (jobStatus == null) {
        LOG.info("job status for {} not found, so retrying with that job id", jobId);
        return new RetryJobIdResult(jobId, true);
      }
      if ("PENDING".equals(jobStatus.getState()) || "RUNNING".equals(jobStatus.getState())) {
        // The job id has been issued and is currently pending. This can happen after receiving
        // an error from the load or copy job creation (e.g. that error might come because the
        // job already exists). Return to the caller which job id is pending (it might not be the
        // one passed in) so the caller can then wait for this job to finish.
        LOG.info("job {} in pending or running state, so continuing with that job id", jobId);
        return new RetryJobIdResult(jobId, false);
      }
      if (jobStatus.getErrorResult() == null
          && (jobStatus.getErrors() == null || jobStatus.getErrors().isEmpty())) {
        // Import succeeded. No retry needed.
        LOG.info("job {} succeeded, so not retrying ", jobId);
        return new RetryJobIdResult(jobId, false);
      }
      // This job has failed, so we assume the data cannot enter BigQuery. We will check the next
      // job in the sequence (with the same unique prefix) to see if is either pending/succeeded
      // or can be used to generate a retry job.
      LOG.info("job {} is failed. Checking the next job id", jobId);
    } catch (RuntimeException e) {
      LOG.info("caught exception while querying job {}", jobId);
      return new RetryJobIdResult(jobId, true);
    }
  }
}