Search in sources :

Example 1 with Sleeper

use of com.google.api.client.util.Sleeper in project beam by apache.

the class BigQueryTableRowIterator method executeWithBackOff.

// Execute a BQ request with exponential backoff and return the result.
// client - BQ request to be executed
// error - Formatted message to log if when a request fails. Takes exception message as a
// formatter parameter.
private 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;
}
Also used : Sleeper(com.google.api.client.util.Sleeper) IOException(java.io.IOException) BackOff(com.google.api.client.util.BackOff)

Example 2 with Sleeper

use of com.google.api.client.util.Sleeper in project beam by apache.

the class FakeJobService method pollJob.

@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 && status.getState() != null && (status.getState().equals("DONE") || status.getState().equals("FAILED"))) {
                    return job;
                }
            }
        } while (BackOffUtils.next(sleeper, backoff));
    } catch (IOException e) {
        return null;
    }
    return null;
}
Also used : JobStatus(com.google.api.services.bigquery.model.JobStatus) Sleeper(com.google.api.client.util.Sleeper) IOException(java.io.IOException) Job(com.google.api.services.bigquery.model.Job) BackOff(com.google.api.client.util.BackOff)

Example 3 with Sleeper

use of com.google.api.client.util.Sleeper in project beam by apache.

the class BigQueryServicesImplTest method testStartLoadJobRetry.

/**
   * 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();
}
Also used : JobReference(com.google.api.services.bigquery.model.JobReference) FastNanoClockAndSleeper(org.apache.beam.sdk.util.FastNanoClockAndSleeper) MockSleeper(com.google.api.client.testing.util.MockSleeper) FastNanoClockAndSleeper(org.apache.beam.sdk.util.FastNanoClockAndSleeper) Sleeper(com.google.api.client.util.Sleeper) Job(com.google.api.services.bigquery.model.Job) ApiErrorExtractor(com.google.cloud.hadoop.util.ApiErrorExtractor) Test(org.junit.Test)

Example 4 with Sleeper

use of com.google.api.client.util.Sleeper in project beam by apache.

the class BigQueryServicesImplTest method testStartLoadJobSucceeds.

/**
   * 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));
}
Also used : JobReference(com.google.api.services.bigquery.model.JobReference) FastNanoClockAndSleeper(org.apache.beam.sdk.util.FastNanoClockAndSleeper) MockSleeper(com.google.api.client.testing.util.MockSleeper) FastNanoClockAndSleeper(org.apache.beam.sdk.util.FastNanoClockAndSleeper) Sleeper(com.google.api.client.util.Sleeper) Job(com.google.api.services.bigquery.model.Job) ApiErrorExtractor(com.google.cloud.hadoop.util.ApiErrorExtractor) Test(org.junit.Test)

Example 5 with Sleeper

use of com.google.api.client.util.Sleeper in project beam by apache.

the class DataflowPipelineJobTest method testWaitUntilFinishNoRepeatedLogs.

/**
   * Tests that a {@link DataflowPipelineJob} does not duplicate messages.
   */
@Test
public void testWaitUntilFinishNoRepeatedLogs() throws Exception {
    DataflowPipelineJob job = new DataflowPipelineJob(mockDataflowClient, JOB_ID, options, null);
    Sleeper sleeper = new ZeroSleeper();
    NanoClock nanoClock = mock(NanoClock.class);
    Instant separatingTimestamp = new Instant(42L);
    JobMessage theMessage = infoMessage(separatingTimestamp, "nothing");
    MonitoringUtil mockMonitor = mock(MonitoringUtil.class);
    when(mockMonitor.getJobMessages(anyString(), anyLong())).thenReturn(ImmutableList.of(theMessage));
    // The Job just always reports "running" across all calls
    Job fakeJob = new Job();
    fakeJob.setCurrentState("JOB_STATE_RUNNING");
    when(mockDataflowClient.getJob(anyString())).thenReturn(fakeJob);
    // After waitUntilFinish the DataflowPipelineJob should record the latest message timestamp
    when(nanoClock.nanoTime()).thenReturn(0L).thenReturn(2000000000L);
    job.waitUntilFinish(Duration.standardSeconds(1), mockHandler, sleeper, nanoClock, mockMonitor);
    verify(mockHandler).process(ImmutableList.of(theMessage));
    // Second waitUntilFinish should request jobs with `separatingTimestamp` so the monitor
    // will only return new messages
    when(nanoClock.nanoTime()).thenReturn(3000000000L).thenReturn(6000000000L);
    job.waitUntilFinish(Duration.standardSeconds(1), mockHandler, sleeper, nanoClock, mockMonitor);
    verify(mockMonitor).getJobMessages(anyString(), eq(separatingTimestamp.getMillis()));
}
Also used : MonitoringUtil(org.apache.beam.runners.dataflow.util.MonitoringUtil) Instant(org.joda.time.Instant) JobMessage(com.google.api.services.dataflow.model.JobMessage) FastNanoClockAndSleeper(org.apache.beam.sdk.util.FastNanoClockAndSleeper) Sleeper(com.google.api.client.util.Sleeper) NanoClock(com.google.api.client.util.NanoClock) Job(com.google.api.services.dataflow.model.Job) Test(org.junit.Test)

Aggregations

Sleeper (com.google.api.client.util.Sleeper)7 Job (com.google.api.services.bigquery.model.Job)4 FastNanoClockAndSleeper (org.apache.beam.sdk.util.FastNanoClockAndSleeper)4 Test (org.junit.Test)4 MockSleeper (com.google.api.client.testing.util.MockSleeper)3 JobReference (com.google.api.services.bigquery.model.JobReference)3 ApiErrorExtractor (com.google.cloud.hadoop.util.ApiErrorExtractor)3 BackOff (com.google.api.client.util.BackOff)2 IOException (java.io.IOException)2 HttpTransport (com.google.api.client.http.HttpTransport)1 MockHttpTransport (com.google.api.client.testing.http.MockHttpTransport)1 NanoClock (com.google.api.client.util.NanoClock)1 JobStatus (com.google.api.services.bigquery.model.JobStatus)1 Job (com.google.api.services.dataflow.model.Job)1 JobMessage (com.google.api.services.dataflow.model.JobMessage)1 Storage (com.google.api.services.storage.Storage)1 MonitoringUtil (org.apache.beam.runners.dataflow.util.MonitoringUtil)1 Instant (org.joda.time.Instant)1 Before (org.junit.Before)1 Matchers.anyString (org.mockito.Matchers.anyString)1