com.google.api.services.bigquery.model.JobReference Java Examples

The following examples show how to use com.google.api.services.bigquery.model.JobReference. 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: BigQueryIO.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
void cleanup(PassThroughThenCleanup.ContextContainer c) throws Exception {
  PipelineOptions options = c.getPipelineOptions();
  BigQueryOptions bqOptions = options.as(BigQueryOptions.class);
  String jobUuid = c.getJobId();
  final String extractDestinationDir =
      resolveTempLocation(bqOptions.getTempLocation(), "BigQueryExtractTemp", jobUuid);
  final String executingProject = bqOptions.getProject();
  JobReference jobRef =
      new JobReference()
          .setProjectId(executingProject)
          .setJobId(getExtractJobId(createJobIdToken(bqOptions.getJobName(), jobUuid)));

  Job extractJob = getBigQueryServices().getJobService(bqOptions).getJob(jobRef);

  if (extractJob != null) {
    List<ResourceId> extractFiles =
        getExtractFilePaths(extractDestinationDir, extractJob);
    if (extractFiles != null && !extractFiles.isEmpty()) {
      FileSystems.delete(
          extractFiles, MoveOptions.StandardMoveOptions.IGNORE_MISSING_FILES);
    }
  }
}
 
Example #2
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#startLoadJob} succeeds. */
@Test
public void testStartLoadJobSucceeds() throws IOException, InterruptedException {
  Job testJob = new Job();
  JobReference jobRef = new JobReference();
  jobRef.setJobId("jobId");
  jobRef.setProjectId("projectId");
  testJob.setJobReference(jobRef);

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  Sleeper sleeper = new FastNanoClockAndSleeper();
  JobServiceImpl.startJob(
      testJob,
      new ApiErrorExtractor(),
      bigquery,
      sleeper,
      BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()));

  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
  expectedLogs.verifyInfo(String.format("Started BigQuery job: %s", jobRef));
}
 
Example #3
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that {@link BigQueryServicesImpl.JobServiceImpl#startLoadJob} succeeds with an already
 * exist job.
 */
@Test
public void testStartLoadJobSucceedsAlreadyExists() throws IOException, InterruptedException {
  Job testJob = new Job();
  JobReference jobRef = new JobReference();
  jobRef.setJobId("jobId");
  jobRef.setProjectId("projectId");
  testJob.setJobReference(jobRef);

  when(response.getStatusCode()).thenReturn(409); // 409 means already exists

  Sleeper sleeper = new FastNanoClockAndSleeper();
  JobServiceImpl.startJob(
      testJob,
      new ApiErrorExtractor(),
      bigquery,
      sleeper,
      BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()));

  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
  expectedLogs.verifyNotLogged("Started BigQuery job");
}
 
Example #4
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 #5
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#startLoadJob} succeeds with a retry. */
@Test
public void testStartLoadJobRetry() throws IOException, InterruptedException {
  Job testJob = new Job();
  JobReference jobRef = new JobReference();
  jobRef.setJobId("jobId");
  jobRef.setProjectId("projectId");
  testJob.setJobReference(jobRef);

  // First response is 403 rate limited, second response has valid payload.
  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(403).thenReturn(200);
  when(response.getContent())
      .thenReturn(toStream(errorWithReasonAndStatus("rateLimitExceeded", 403)))
      .thenReturn(toStream(testJob));

  Sleeper sleeper = new FastNanoClockAndSleeper();
  JobServiceImpl.startJob(
      testJob,
      new ApiErrorExtractor(),
      bigquery,
      sleeper,
      BackOffAdapter.toGcpBackOff(FluentBackoff.DEFAULT.backoff()));

  verify(response, times(2)).getStatusCode();
  verify(response, times(2)).getContent();
  verify(response, times(2)).getContentType();
}
 
Example #6
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} succeeds. */
@Test
public void testPollJobSucceeds() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus().setState("DONE"));

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #7
Source File: FakeJobService.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public void startExtractJob(JobReference jobRef, JobConfigurationExtract extractConfig)
    throws IOException {
  checkArgument(
      "AVRO".equals(extractConfig.getDestinationFormat()), "Only extract to AVRO is supported");
  synchronized (allJobs) {
    verifyUniqueJobId(jobRef.getJobId());
    ++numExtractJobCalls;

    Job job = new Job();
    job.setJobReference(jobRef);
    job.setConfiguration(new JobConfiguration().setExtract(extractConfig));
    job.setKind(" bigquery#job");
    job.setStatus(new JobStatus().setState("PENDING"));
    allJobs.put(jobRef.getProjectId(), jobRef.getJobId(), new JobInfo(job));
  }
}
 
Example #8
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} fails. */
@Test
public void testPollJobFailed() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus().setState("DONE").setErrorResult(new ErrorProto()));

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #9
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Tests that {@link BigQueryServicesImpl.JobServiceImpl#pollJob} returns UNKNOWN. */
@Test
public void testPollJobUnknown() throws IOException, InterruptedException {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus());

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.pollJob(jobRef, Sleeper.DEFAULT, BackOff.STOP_BACKOFF);

  assertEquals(null, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #10
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJobSucceeds() throws Exception {
  Job testJob = new Job();
  testJob.setStatus(new JobStatus());

  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(200);
  when(response.getContent()).thenReturn(toStream(testJob));

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.getJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(testJob, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #11
Source File: BigqueryPollJobAction.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the provided job succeeded, false if it failed, and throws an exception if it
 * is still pending.
 */
private boolean checkJobOutcome() {
  Job job = null;
  String jobRefString =
      toJobReferenceString(new JobReference().setProjectId(projectId).setJobId(jobId));

  try {
    job = bigquery.jobs().get(projectId, jobId).execute();
  } catch (IOException e) {
    // We will throw a new exception because done==false, but first log this exception.
    logger.atWarning().withCause(e).log("Error checking outcome of BigQuery job %s.", jobId);
  }
  // If job is not yet done, then throw an exception so that we'll return a failing HTTP status
  // code and the task will be retried.
  if (job == null || !job.getStatus().getState().equals("DONE")) {
    throw new NotModifiedException(jobRefString);
  }

  // Check if the job ended with an error.
  if (job.getStatus().getErrorResult() != null) {
    logger.atSevere().log("Bigquery job failed - %s - %s", jobRefString, job);
    return false;
  }
  logger.atInfo().log("Bigquery job succeeded - %s", jobRefString);
  return true;
}
 
Example #12
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public JobStatistics dryRunQuery(
    String projectId, JobConfigurationQuery queryConfig, String location)
    throws InterruptedException, IOException {
  JobReference jobRef = new JobReference().setLocation(location).setProjectId(projectId);
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setQuery(queryConfig).setDryRun(true));
  return executeWithRetries(
          client.jobs().insert(projectId, job),
          String.format(
              "Unable to dry run query: %s, aborting after %d retries.",
              queryConfig, MAX_RPC_RETRIES),
          Sleeper.DEFAULT,
          createDefaultBackoff(),
          ALWAYS_RETRY)
      .getStatistics();
}
 
Example #13
Source File: BigqueryPollJobActionTest.java    From nomulus with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccess_enqueuePollTask_withChainedTask() throws Exception {
  TaskOptions chainedTask = TaskOptions.Builder
      .withUrl("/_dr/something")
      .method(Method.POST)
      .header("X-Testing", "foo")
      .param("testing", "bar");
  new BigqueryPollJobEnqueuer(TASK_QUEUE_UTILS).enqueuePollTask(
      new JobReference().setProjectId(PROJECT_ID).setJobId(JOB_ID),
      chainedTask,
      getQueue(CHAINED_QUEUE_NAME));
  assertTasksEnqueued(BigqueryPollJobAction.QUEUE, newPollJobTaskMatcher("POST"));
  TaskStateInfo taskInfo = getOnlyElement(
      TaskQueueHelper.getQueueInfo(BigqueryPollJobAction.QUEUE).getTaskInfo());
  ByteArrayInputStream taskBodyBytes = new ByteArrayInputStream(taskInfo.getBodyAsBytes());
  TaskOptions taskOptions = (TaskOptions) new ObjectInputStream(taskBodyBytes).readObject();
  assertThat(taskOptions).isEqualTo(chainedTask);
}
 
Example #14
Source File: BigQueryHelper.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new JobReference with a unique jobId generated from {@code jobIdPrefix} plus a
 * randomly generated UUID String.
 */
public JobReference createJobReference(
    String projectId, String jobIdPrefix, @Nullable String location) {
  checkArgument(projectId != null, "projectId must not be null.");
  checkArgument(jobIdPrefix != null, "jobIdPrefix must not be null.");
  checkArgument(
      jobIdPrefix.matches(BIGQUERY_JOB_ID_PATTERN),
      "jobIdPrefix '%s' must match pattern '%s'",
      jobIdPrefix,
      BIGQUERY_JOB_ID_PATTERN);

  String fullJobId = String.format("%s-%s", jobIdPrefix, UUID.randomUUID());
  checkArgument(
      fullJobId.length() <= BIGQUERY_JOB_ID_MAX_LENGTH,
      "fullJobId '%s' has length '%s'; must be less than or equal to %s",
      fullJobId,
      fullJobId.length(),
      BIGQUERY_JOB_ID_MAX_LENGTH);
  return new JobReference().setProjectId(projectId).setJobId(fullJobId).setLocation(location);
}
 
Example #15
Source File: BigqueryPollJobAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a task to poll for the success or failure of the referenced BigQuery job and to
 * launch the provided task in the specified queue if the job succeeds.
 */
public TaskHandle enqueuePollTask(
    JobReference jobRef, TaskOptions chainedTask, Queue chainedTaskQueue) throws IOException {
  // Serialize the chainedTask into a byte array to put in the task payload.
  ByteArrayOutputStream taskBytes = new ByteArrayOutputStream();
  new ObjectOutputStream(taskBytes).writeObject(chainedTask);
  return taskQueueUtils.enqueue(
      getQueue(QUEUE),
      createCommonPollTask(jobRef)
          .method(Method.POST)
          .header(CHAINED_TASK_QUEUE_HEADER, chainedTaskQueue.getQueueName())
          .payload(taskBytes.toByteArray()));
}
 
Example #16
Source File: BigqueryConnection.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Synchronously waits for a job to complete that's already been launched.
 *
 * @throws BigqueryJobFailureException
 */
private Job waitForJob(Job job) {
  verify(job.getStatus() != null);
  while (!job.getStatus().getState().equals("DONE")) {
    sleeper.sleepUninterruptibly(pollInterval);
    JobReference ref = job.getJobReference();
    try {
      job = bigquery.jobs().get(ref.getProjectId(), ref.getJobId()).execute();
    } catch (IOException e) {
      throw BigqueryJobFailureException.create(e);
    }
  }
  return job;
}
 
Example #17
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetJobThrows() throws Exception {
  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(401);

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  thrown.expect(IOException.class);
  thrown.expectMessage(String.format("Unable to find BigQuery job: %s", jobRef));

  jobService.getJob(jobRef, Sleeper.DEFAULT, BackOff.STOP_BACKOFF);
}
 
Example #18
Source File: BigQueryServicesImplTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetJobNotFound() throws Exception {
  when(response.getContentType()).thenReturn(Json.MEDIA_TYPE);
  when(response.getStatusCode()).thenReturn(404);

  BigQueryServicesImpl.JobServiceImpl jobService =
      new BigQueryServicesImpl.JobServiceImpl(bigquery);
  JobReference jobRef = new JobReference().setProjectId("projectId").setJobId("jobId");
  Job job = jobService.getJob(jobRef, Sleeper.DEFAULT, BackOff.ZERO_BACKOFF);

  assertEquals(null, job);
  verify(response, times(1)).getStatusCode();
  verify(response, times(1)).getContent();
  verify(response, times(1)).getContentType();
}
 
Example #19
Source File: BigqueryPollJobAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
/**
 * Enqueue a task to poll for the success or failure of the referenced BigQuery job and to
 * launch the provided task in the specified queue if the job succeeds.
 */
private static TaskOptions createCommonPollTask(JobReference jobRef) {
  // Omit host header so that task will be run on the current backend/module.
  return withUrl(PATH)
      .countdownMillis(POLL_COUNTDOWN.getMillis())
      .header(PROJECT_ID_HEADER, jobRef.getProjectId())
      .header(JOB_ID_HEADER, jobRef.getJobId());
}
 
Example #20
Source File: UploadDatastoreBackupAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private String uploadBackup(String backupId, String backupFolderUrl, Iterable<String> kinds)
    throws IOException {
  Bigquery bigquery = checkedBigquery.ensureDataSetExists(projectId, BACKUP_DATASET);
  String loadMessage =
      String.format("Loading Datastore backup %s from %s...", backupId, backupFolderUrl);
  logger.atInfo().log(loadMessage);

  String sanitizedBackupId = sanitizeForBigquery(backupId);
  StringBuilder builder = new StringBuilder(loadMessage + "\n");
  builder.append("Load jobs:\n");

  for (String kindName : kinds) {
    String jobId = String.format("load-backup-%s-%s", sanitizedBackupId, kindName);
    JobReference jobRef = new JobReference().setProjectId(projectId).setJobId(jobId);
    String sourceUri = getBackupInfoFileForKind(backupFolderUrl, kindName);
    String tableId = String.format("%s_%s", sanitizedBackupId, kindName);

    // Launch the load job.
    Job job = makeLoadJob(jobRef, sourceUri, tableId);
    bigquery.jobs().insert(projectId, job).execute();

    // Enqueue a task to check on the load job's completion, and if it succeeds, to update a
    // well-known view in BigQuery to point at the newly loaded backup table for this kind.
    bigqueryPollEnqueuer.enqueuePollTask(
        jobRef,
        createViewUpdateTask(BACKUP_DATASET, tableId, kindName, LATEST_BACKUP_VIEW_NAME),
        getQueue(UpdateSnapshotViewAction.QUEUE));

    builder.append(String.format(" - %s:%s\n", projectId, jobId));
    logger.atInfo().log("Submitted load job %s:%s", projectId, jobId);
  }
  return builder.toString();
}
 
Example #21
Source File: UploadDatastoreBackupAction.java    From nomulus with Apache License 2.0 5 votes vote down vote up
private Job makeLoadJob(JobReference jobRef, String sourceUri, String tableId) {
  TableReference tableReference =
      new TableReference()
          .setProjectId(jobRef.getProjectId())
          .setDatasetId(BACKUP_DATASET)
          .setTableId(tableId);
  return new Job()
      .setJobReference(jobRef)
      .setConfiguration(new JobConfiguration()
          .setLoad(new JobConfigurationLoad()
              .setWriteDisposition(WriteDisposition.WRITE_EMPTY.toString())
              .setSourceFormat(SourceFormat.DATASTORE_BACKUP.toString())
              .setSourceUris(ImmutableList.of(sourceUri))
              .setDestinationTable(tableReference)));
}
 
Example #22
Source File: AbstractExportToCloudStorage.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
@Override
public void beginExport() throws IOException {
  // Create job and configuration.
  JobConfigurationExtract extractConfig = new JobConfigurationExtract();

  // Set source.
  extractConfig.setSourceTable(tableToExport.getTableReference());

  // Set destination.
  extractConfig.setDestinationUris(getExportPaths());
  extractConfig.set(DESTINATION_FORMAT_KEY, fileFormat.getFormatIdentifier());

  JobConfiguration config = new JobConfiguration();
  config.setExtract(extractConfig);

  JobReference jobReference =
      bigQueryHelper.createJobReference(
          projectId, "exporttocloudstorage", tableToExport.getLocation());

  Job job = new Job();
  job.setConfiguration(config);
  job.setJobReference(jobReference);

  // Insert and run job.
  try {
    Job response = bigQueryHelper.insertJobOrFetchDuplicate(projectId, job);
    logger.atFine().log("Got response '%s'", response);
    exportJobReference = response.getJobReference();
  } catch (IOException e) {
    String error = String.format(
        "Error while exporting table %s",
        BigQueryStrings.toString(tableToExport.getTableReference()));
    throw new IOException(error, e);
  }
}
 
Example #23
Source File: BigQueryUtilsTest.java    From hadoop-connectors with Apache License 2.0 5 votes vote down vote up
/**
 * Mocks result of BigQuery for polling for job completion.
 *
 * @throws IOException on IOError.
 */
@Before
public void setUp() throws IOException {

  jobReference = new JobReference().setJobId("test-job-id").setLocation("test-job-location");

  // Create the unfinished job result.
  notDoneJob = new Job();
  notDoneJobStatus = new JobStatus();
  notDoneJobStatus.setState("NOT DONE");
  notDoneJobStatus.setErrorResult(null);
  notDoneJob.setStatus(notDoneJobStatus);
  notDoneJob.setJobReference(jobReference);

  // Create the finished job result.
  job = new Job();
  jobStatus = new JobStatus();
  jobStatus.setState("DONE");
  jobStatus.setErrorResult(null);
  job.setStatus(jobStatus);
  job.setJobReference(jobReference);

  // Mock BigQuery.
  mockBigQuery = mock(Bigquery.class);
  mockBigQueryJobs = mock(Bigquery.Jobs.class);
  mockJobsGet = mock(Bigquery.Jobs.Get.class);
  when(mockBigQuery.jobs()).thenReturn(mockBigQueryJobs);
  when(mockBigQueryJobs.get(projectId, jobReference.getJobId()))
      .thenReturn(mockJobsGet)
      .thenReturn(mockJobsGet);
  when(mockJobsGet.setLocation(any(String.class))).thenReturn(mockJobsGet);
  when(mockJobsGet.execute()).thenReturn(job);

  // Constructor coverage
  new BigQueryUtils();

  // Mock Progressable.
  mockProgressable = mock(Progressable.class);
}
 
Example #24
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Tries executing the RPC for at most {@code MAX_RPC_RETRIES} times until it succeeds.
 *
 * @throws IOException if it exceeds {@code MAX_RPC_RETRIES} attempts.
 */
@Override
public void startLoadJob(JobReference jobRef, JobConfigurationLoad loadConfig)
    throws InterruptedException, IOException {
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setLoad(loadConfig));

  startJob(job, errorExtractor, client);
}
 
Example #25
Source File: FakeJobService.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public void startCopyJob(JobReference jobRef, JobConfigurationTableCopy copyConfig)
    throws IOException {
  synchronized (allJobs) {
    verifyUniqueJobId(jobRef.getJobId());
    Job job = new Job();
    job.setJobReference(jobRef);
    job.setConfiguration(new JobConfiguration().setCopy(copyConfig));
    job.setKind(" bigquery#job");
    job.setStatus(new JobStatus().setState("PENDING"));
    allJobs.put(jobRef.getProjectId(), jobRef.getJobId(), new JobInfo(job));
  }
}
 
Example #26
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Tries executing the RPC for at most {@code MAX_RPC_RETRIES} times until it succeeds.
 *
 * @throws IOException if it exceeds {@code MAX_RPC_RETRIES} attempts.
 */
@Override
public void startExtractJob(JobReference jobRef, JobConfigurationExtract extractConfig)
    throws InterruptedException, IOException {
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setExtract(extractConfig));

  startJob(job, errorExtractor, client);
}
 
Example #27
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Tries executing the RPC for at most {@code MAX_RPC_RETRIES} times until it succeeds.
 *
 * @throws IOException if it exceeds {@code MAX_RPC_RETRIES} attempts.
 */
@Override
public void startQueryJob(JobReference jobRef, JobConfigurationQuery queryConfig)
    throws IOException, InterruptedException {
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setQuery(queryConfig));

  startJob(job, errorExtractor, client);
}
 
Example #28
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Tries executing the RPC for at most {@code MAX_RPC_RETRIES} times until it succeeds.
 *
 * @throws IOException if it exceeds {@code MAX_RPC_RETRIES} attempts.
 */
@Override
public void startCopyJob(JobReference jobRef, JobConfigurationTableCopy copyConfig)
    throws IOException, InterruptedException {
  Job job =
      new Job()
          .setJobReference(jobRef)
          .setConfiguration(new JobConfiguration().setCopy(copyConfig));

  startJob(job, errorExtractor, client);
}
 
Example #29
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static void startJob(
    Job job,
    ApiErrorExtractor errorExtractor,
    Bigquery client,
    Sleeper sleeper,
    BackOff backoff)
    throws IOException, InterruptedException {
  JobReference jobRef = job.getJobReference();
  Exception lastException;
  do {
    try {
      client.jobs().insert(jobRef.getProjectId(), job).execute();
      LOG.info(
          "Started BigQuery job: {}.\n{}",
          jobRef,
          formatBqStatusCommand(jobRef.getProjectId(), jobRef.getJobId()));
      return; // SUCCEEDED
    } catch (IOException e) {
      if (errorExtractor.itemAlreadyExists(e)) {
        LOG.info("BigQuery job " + jobRef + " already exists, will not retry inserting it:", e);
        return; // SUCCEEDED
      }
      // ignore and retry
      LOG.info("Failed to insert job " + jobRef + ", will retry:", e);
      lastException = e;
    }
  } while (nextBackOff(sleeper, backoff));
  throw new IOException(
      String.format(
          "Unable to insert job: %s, aborting after %d .", jobRef.getJobId(), MAX_RPC_RETRIES),
      lastException);
}
 
Example #30
Source File: BigQueryServicesImpl.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Job pollJob(JobReference jobRef, int maxAttempts) throws InterruptedException {
  BackOff backoff =
      BackOffAdapter.toGcpBackOff(
          FluentBackoff.DEFAULT
              .withMaxRetries(maxAttempts)
              .withInitialBackoff(INITIAL_JOB_STATUS_POLL_BACKOFF)
              .withMaxBackoff(Duration.standardMinutes(1))
              .backoff());
  return pollJob(jobRef, Sleeper.DEFAULT, backoff);
}