Search in sources :

Example 1 with NonTransientException

use of org.apache.gobblin.exception.NonTransientException in project incubator-gobblin by apache.

the class RetryWriterTest method retryTestNonTransientException.

public void retryTestNonTransientException() throws IOException {
    DataWriter<Void> writer = mock(DataWriter.class);
    doThrow(new NonTransientException()).when(writer).writeEnvelope(any(RecordEnvelope.class));
    DataWriterWrapperBuilder<Void> builder = new DataWriterWrapperBuilder<>(writer, new State());
    DataWriter<Void> retryWriter = builder.build();
    try {
        retryWriter.writeEnvelope(new RecordEnvelope<>(null));
        Assert.fail("Should have failed.");
    } catch (Exception e) {
    }
    verify(writer, atMost(1)).writeEnvelope(any(RecordEnvelope.class));
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) RecordEnvelope(org.apache.gobblin.stream.RecordEnvelope) State(org.apache.gobblin.configuration.State) FinalState(org.apache.gobblin.util.FinalState) NonTransientException(org.apache.gobblin.exception.NonTransientException) IOException(java.io.IOException)

Example 2 with NonTransientException

use of org.apache.gobblin.exception.NonTransientException in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractorTest method testPollForCompletionFailure.

public void testPollForCompletionFailure() throws IOException {
    wuState = new WorkUnitState();
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_TIME_OUT_MS, TimeUnit.SECONDS.toMillis(30L));
    wuState.setProp(POLL_RETRY_PREFIX + RETRY_INTERVAL_MS, 1L);
    GoogleAnalyticsUnsampledExtractor extractor = setup(ReportCreationStatus.FAILED, wuState, false);
    UnsampledReport requestedReport = new UnsampledReport().setAccountId("testAccountId").setWebPropertyId("testWebPropertyId").setProfileId("testProfileId").setId("testId");
    try {
        extractor.pollForCompletion(wuState, gaService, requestedReport);
        Assert.fail("Should have failed with failed status");
    } catch (Exception e) {
        Assert.assertTrue(e.getCause().getCause() instanceof NonTransientException);
    }
    verify(getReq, atLeast(5)).execute();
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) WorkUnitState(org.apache.gobblin.configuration.WorkUnitState) NonTransientException(org.apache.gobblin.exception.NonTransientException) IOException(java.io.IOException) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport)

Example 3 with NonTransientException

use of org.apache.gobblin.exception.NonTransientException in project incubator-gobblin by apache.

the class SalesforceRestWriter method onConnect.

/**
 * Retrieve access token, if needed, retrieve instance url, and set server host URL
 * {@inheritDoc}
 * @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost)
 */
@Override
public void onConnect(URI serverHost) throws IOException {
    if (!StringUtils.isEmpty(accessToken)) {
        // No need to be called if accessToken is active.
        return;
    }
    try {
        getLog().info("Getting Oauth2 access token.");
        OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()).setGrantType(GrantType.PASSWORD).setClientId(clientId).setClientSecret(clientSecret).setUsername(userId).setPassword(password + securityToken).buildQueryMessage();
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST);
        accessToken = response.getAccessToken();
        setCurServerHost(new URI(response.getParam("instance_url")));
    } catch (OAuthProblemException e) {
        throw new NonTransientException("Error while authenticating with Oauth2", e);
    } catch (OAuthSystemException e) {
        throw new RuntimeException("Failed getting access token", e);
    } catch (URISyntaxException e) {
        throw new RuntimeException("Failed due to invalid instance url", e);
    }
}
Also used : OAuthProblemException(org.apache.oltu.oauth2.common.exception.OAuthProblemException) NonTransientException(org.apache.gobblin.exception.NonTransientException) URLConnectionClient(org.apache.oltu.oauth2.client.URLConnectionClient) OAuthClient(org.apache.oltu.oauth2.client.OAuthClient) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) OAuthJSONAccessTokenResponse(org.apache.oltu.oauth2.client.response.OAuthJSONAccessTokenResponse) URISyntaxException(java.net.URISyntaxException) OAuthClientRequest(org.apache.oltu.oauth2.client.request.OAuthClientRequest) URI(java.net.URI)

Example 4 with NonTransientException

use of org.apache.gobblin.exception.NonTransientException in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractor method requestUnsampledReport.

@VisibleForTesting
UnsampledReport requestUnsampledReport(UnsampledReport request) throws IOException {
    String accountId = request.getAccountId();
    String webPropertyId = request.getWebPropertyId();
    String profileId = request.getProfileId();
    // GA somehow does not allow these values in it.
    request.setAccountId(null).setWebPropertyId(null).setProfileId(null);
    final String endDate = request.getEndDate();
    final Insert insertRequest = gaService.management().unsampledReports().insert(accountId, webPropertyId, profileId, request);
    Config config = ConfigBuilder.create().loadProps(wuState.getProperties(), REQUEST_RETRY_PREFIX).build();
    Retryer<UnsampledReport> retryer = RetryerFactory.newInstance(config);
    LOG.info("Requesting to create unsampled report " + request);
    try {
        return retryer.call(new Callable<UnsampledReport>() {

            @Override
            public UnsampledReport call() throws Exception {
                UnsampledReport response = insertRequest.execute();
                if (ReportCreationStatus.FAILED.name().equals(response.getStatus())) {
                    // No retry if it's explicitly failed from server
                    throw new NonTransientException("Failed to create unsampled report " + response);
                }
                // response does not have end date where we need it later for next watermark calculation.
                response.setEndDate(endDate);
                return response;
            }
        });
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (RetryException e) {
        throw new RuntimeException(e);
    }
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) Config(com.typesafe.config.Config) IOException(java.io.IOException) Insert(com.google.api.services.analytics.Analytics.Management.UnsampledReports.Insert) RetryException(com.github.rholder.retry.RetryException) NonTransientException(org.apache.gobblin.exception.NonTransientException) RetryException(com.github.rholder.retry.RetryException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) ExecutionException(java.util.concurrent.ExecutionException) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 5 with NonTransientException

use of org.apache.gobblin.exception.NonTransientException in project incubator-gobblin by apache.

the class GoogleAnalyticsUnsampledExtractor method pollForCompletion.

@VisibleForTesting
UnsampledReport pollForCompletion(State state, final Analytics gaService, final UnsampledReport requestedReport) throws IOException {
    Config config = ConfigBuilder.create().loadProps(state.getProperties(), POLL_RETRY_PREFIX).build().withFallback(POLL_RETRY_DEFAULTS);
    Retryer<UnsampledReport> retryer = RetryerFactory.newInstance(config);
    LOG.info("Will poll for completion on unsampled report with retry config: " + config);
    final Stopwatch stopwatch = Stopwatch.createStarted();
    UnsampledReport result = null;
    try {
        result = retryer.call(new Callable<UnsampledReport>() {

            @Override
            public UnsampledReport call() throws Exception {
                UnsampledReport response = null;
                try {
                    response = gaService.management().unsampledReports().get(requestedReport.getAccountId(), requestedReport.getWebPropertyId(), requestedReport.getProfileId(), requestedReport.getId()).execute();
                } catch (Exception e) {
                    LOG.warn("Encountered exception while polling for unsampled report. Will keep polling. " + "Elasped so far: " + stopwatch.elapsed(TimeUnit.SECONDS) + " seconds", e);
                    throw e;
                }
                ReportCreationStatus status = ReportCreationStatus.valueOf(response.getStatus());
                switch(status) {
                    case FAILED:
                        // Stop retrying if it explicitly failed from server.
                        throw new NonTransientException("Unsampled report has failed to be generated. " + response);
                    case PENDING:
                        LOG.info("Waiting for report completion. Elasped so far: " + stopwatch.elapsed(TimeUnit.SECONDS) + " seconds for unsampled report: " + response);
                        // Throw so that Retryer will retry
                        throw new RuntimeException("Not completed yet. This will be retried. " + response);
                    case COMPLETED:
                        return response;
                    default:
                        throw new NonTransientException(status + " is not supported. " + response);
                }
            }
        });
    } catch (ExecutionException e) {
        throw new IOException(e);
    } catch (RetryException e) {
        throw new RuntimeException(e);
    }
    LOG.info("Unsampled report creation has been completed. " + result);
    Preconditions.checkArgument(DOWNLOAD_TYPE_GOOGLE_DRIVE.equals(result.getDownloadType()), result.getDownloadType() + " DownloadType is not supported.");
    return result;
}
Also used : NonTransientException(org.apache.gobblin.exception.NonTransientException) Config(com.typesafe.config.Config) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) RetryException(com.github.rholder.retry.RetryException) Callable(java.util.concurrent.Callable) NonTransientException(org.apache.gobblin.exception.NonTransientException) RetryException(com.github.rholder.retry.RetryException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DataRecordException(org.apache.gobblin.source.extractor.DataRecordException) UnsampledReport(com.google.api.services.analytics.model.UnsampledReport) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

NonTransientException (org.apache.gobblin.exception.NonTransientException)5 IOException (java.io.IOException)4 UnsampledReport (com.google.api.services.analytics.model.UnsampledReport)3 RetryException (com.github.rholder.retry.RetryException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Config (com.typesafe.config.Config)2 ExecutionException (java.util.concurrent.ExecutionException)2 DataRecordException (org.apache.gobblin.source.extractor.DataRecordException)2 Insert (com.google.api.services.analytics.Analytics.Management.UnsampledReports.Insert)1 Stopwatch (com.google.common.base.Stopwatch)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Callable (java.util.concurrent.Callable)1 State (org.apache.gobblin.configuration.State)1 WorkUnitState (org.apache.gobblin.configuration.WorkUnitState)1 RecordEnvelope (org.apache.gobblin.stream.RecordEnvelope)1 FinalState (org.apache.gobblin.util.FinalState)1 OAuthClient (org.apache.oltu.oauth2.client.OAuthClient)1 URLConnectionClient (org.apache.oltu.oauth2.client.URLConnectionClient)1 OAuthClientRequest (org.apache.oltu.oauth2.client.request.OAuthClientRequest)1