Search in sources :

Example 16 with RetryableException

use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.

the class RemoteTaskExecutor method runTask.

/**
 * Sends the {@link RunnableTaskRequest} to a remote worker and returns the result.
 * Retries sending the request if the workers are busy.
 *
 * @param runnableTaskRequest {@link RunnableTaskRequest} with details of task
 * @return byte[] response from remote task
 * @throws Exception returned by remote task if any
 */
public byte[] runTask(RunnableTaskRequest runnableTaskRequest) throws Exception {
    // initialize start time for collecting latency metric
    long startTime = System.currentTimeMillis();
    ByteBuffer requestBody = encodeTaskRequest(runnableTaskRequest);
    try {
        return Retries.callWithRetries((retryContext) -> {
            try {
                HttpRequest.Builder requestBuilder = remoteClient.requestBuilder(HttpMethod.POST, workerUrl).withBody(requestBody.duplicate());
                if (compression) {
                    requestBuilder.addHeader(HttpHeaders.CONTENT_ENCODING, "gzip");
                    requestBuilder.addHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate");
                }
                HttpRequest httpRequest = requestBuilder.build();
                HttpResponse httpResponse = remoteClient.execute(httpRequest);
                if (httpResponse.getResponseCode() == HttpResponseStatus.TOO_MANY_REQUESTS.code()) {
                    throw new RetryableException(String.format("Received response code %s for %s", httpResponse.getResponseCode(), runnableTaskRequest.getClassName()));
                }
                if (httpResponse.getResponseCode() != HttpURLConnection.HTTP_OK) {
                    BasicThrowable basicThrowable = GSON.fromJson(new String(getResponseBody(httpResponse)), BasicThrowable.class);
                    throw RemoteExecutionException.fromBasicThrowable(basicThrowable);
                }
                byte[] result = getResponseBody(httpResponse);
                // emit metrics with successful result
                emitMetrics(startTime, true, runnableTaskRequest, retryContext.getRetryAttempt());
                return result;
            } catch (NoRouteToHostException e) {
                throw new RetryableException(String.format("Received exception %s for %s", e.getMessage(), runnableTaskRequest.getClassName()));
            }
        }, retryStrategy, Retries.DEFAULT_PREDICATE);
    } catch (Exception e) {
        // emit metrics with failed result
        emitMetrics(startTime, false, runnableTaskRequest, getAttempts(e));
        throw e;
    }
}
Also used : HttpRequest(io.cdap.common.http.HttpRequest) RetryableException(io.cdap.cdap.api.retry.RetryableException) HttpResponse(io.cdap.common.http.HttpResponse) BasicThrowable(io.cdap.cdap.proto.BasicThrowable) ByteBuffer(java.nio.ByteBuffer) NoRouteToHostException(java.net.NoRouteToHostException) RemoteExecutionException(io.cdap.cdap.api.service.worker.RemoteExecutionException) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) NoRouteToHostException(java.net.NoRouteToHostException)

Example 17 with RetryableException

use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.

the class SystemAppEnableExecutor method deploySystemApp.

private ApplicationWithPrograms deploySystemApp(Arguments arguments) throws Exception {
    ApplicationId appId = arguments.getId();
    ArtifactSummary artifactSummary = arguments.getArtifact();
    KerberosPrincipalId ownerPrincipalId = arguments.getOwnerPrincipal() == null ? null : new KerberosPrincipalId(arguments.getOwnerPrincipal());
    // if we don't null check, it gets serialized to "null"
    String configString = arguments.getConfig() == null ? null : GSON.toJson(arguments.getConfig());
    try {
        return appLifecycleService.deployApp(appId.getParent(), appId.getApplication(), appId.getVersion(), artifactSummary, configString, x -> {
        }, ownerPrincipalId, arguments.canUpdateSchedules(), false, Collections.emptyMap());
    } catch (UnauthorizedException | InvalidArtifactException e) {
        throw e;
    } catch (DatasetManagementException e) {
        if (e.getCause() instanceof UnauthorizedException) {
            throw (UnauthorizedException) e.getCause();
        } else {
            throw new RetryableException(e);
        }
    } catch (Exception e) {
        throw new RetryableException(e);
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) RetryableException(io.cdap.cdap.api.retry.RetryableException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) RetryableException(io.cdap.cdap.api.retry.RetryableException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ConflictException(io.cdap.cdap.common.ConflictException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Example 18 with RetryableException

use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.

the class SparkProgramStatusMetricsProvider method retrieveMetrics.

@Override
public ExecutionMetrics retrieveMetrics(ProgramRunId runId) {
    if (!runId.getType().equals(ProgramType.SPARK)) {
        return ExecutionMetrics.emptyMetrics();
    }
    String runIdStr = runId.getRun();
    String sparkHistoricBaseURL = cConf.get(Constants.Spark.SPARK_METRICS_PROVIDER_HOST);
    String applicationsURL = String.format("%s%s?minEndDate=%s", sparkHistoricBaseURL, sparkApplicationsEndpoint, generateMaxTerminationDateParam());
    return Retries.supplyWithRetries(() -> {
        ExecutionMetrics metrics;
        HttpResponse applicationResponse;
        try {
            applicationResponse = doGet(applicationsURL);
            String attemptId = extractAttemptId(applicationResponse.getResponseBodyAsString(), runIdStr);
            if (Objects.nonNull(attemptId) && !attemptId.isEmpty()) {
                HttpResponse stagesResponse;
                String stagesURL = String.format("%s/%s/%s/%s/stages", sparkHistoricBaseURL, sparkApplicationsEndpoint, runIdStr, attemptId);
                stagesResponse = doGet(stagesURL);
                metrics = extractMetrics(stagesResponse.getResponseBodyAsString());
                if (Objects.isNull(metrics)) {
                    logger.error("Error during metrics extraction");
                    return ExecutionMetrics.emptyMetrics();
                } else {
                    return metrics;
                }
            } else {
                throw new RetryableException("Error during attemptId extraction");
            }
        } catch (IOException e) {
            logger.warn("Error retrieving application response", e);
            throw new RetryableException(e);
        }
    }, RetryStrategies.fromConfiguration(this.cConf, Constants.Spark.SPARK_METRICS_PROVIDER_RETRY_STRATEGY_PREFIX));
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) HttpResponse(io.cdap.common.http.HttpResponse) IOException(java.io.IOException)

Example 19 with RetryableException

use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.

the class BaseStepExecutor method execute.

@Override
public BootstrapStepResult execute(String label, JsonObject argumentsObj) throws InterruptedException {
    T arguments;
    try {
        arguments = GSON.fromJson(argumentsObj, getArgumentsType());
    } catch (JsonParseException e) {
        LOG.warn("Bootstrap step {} failed because its arguments are malformed: {}", label, e.getMessage());
        return new BootstrapStepResult(label, BootstrapStepResult.Status.FAILED, String.format("Argument decoding failed. Reason: %s", e.getMessage()));
    }
    try {
        arguments.validate();
    } catch (RuntimeException e) {
        LOG.warn("Bootstrap step {} failed due to invalid arguments: {}", label, e.getMessage());
        return new BootstrapStepResult(label, BootstrapStepResult.Status.FAILED, e.getMessage());
    }
    try {
        LOG.debug("Executing bootstrap step {}", label);
        Retries.runWithInterruptibleRetries(() -> execute(arguments), getRetryStrategy(), t -> t instanceof RetryableException);
        LOG.debug("Bootstrap step {} completed successfully", label);
        return new BootstrapStepResult(label, BootstrapStepResult.Status.SUCCEEDED);
    } catch (InterruptedException e) {
        throw e;
    } catch (Exception e) {
        LOG.warn("Bootstrap step {} failed to execute", label, e);
        return new BootstrapStepResult(label, BootstrapStepResult.Status.FAILED, e.getMessage());
    }
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) JsonParseException(com.google.gson.JsonParseException) BootstrapStepResult(io.cdap.cdap.proto.bootstrap.BootstrapStepResult) JsonParseException(com.google.gson.JsonParseException) RetryableException(io.cdap.cdap.api.retry.RetryableException)

Example 20 with RetryableException

use of io.cdap.cdap.api.retry.RetryableException in project cdap by cdapio.

the class SystemAppEnableExecutor method deploySystemApp.

private ApplicationWithPrograms deploySystemApp(Arguments arguments) throws Exception {
    ApplicationId appId = arguments.getId();
    ArtifactSummary artifactSummary = arguments.getArtifact();
    KerberosPrincipalId ownerPrincipalId = arguments.getOwnerPrincipal() == null ? null : new KerberosPrincipalId(arguments.getOwnerPrincipal());
    // if we don't null check, it gets serialized to "null"
    String configString = arguments.getConfig() == null ? null : GSON.toJson(arguments.getConfig());
    try {
        return appLifecycleService.deployApp(appId.getParent(), appId.getApplication(), appId.getVersion(), artifactSummary, configString, x -> {
        }, ownerPrincipalId, arguments.canUpdateSchedules(), false, Collections.emptyMap());
    } catch (UnauthorizedException | InvalidArtifactException e) {
        throw e;
    } catch (DatasetManagementException e) {
        if (e.getCause() instanceof UnauthorizedException) {
            throw (UnauthorizedException) e.getCause();
        } else {
            throw new RetryableException(e);
        }
    } catch (Exception e) {
        throw new RetryableException(e);
    }
}
Also used : DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) RetryableException(io.cdap.cdap.api.retry.RetryableException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) RetryableException(io.cdap.cdap.api.retry.RetryableException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ConflictException(io.cdap.cdap.common.ConflictException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Aggregations

RetryableException (io.cdap.cdap.api.retry.RetryableException)28 IOException (java.io.IOException)16 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)6 NotFoundException (io.cdap.cdap.common.NotFoundException)5 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)5 Constants (io.cdap.cdap.common.conf.Constants)4 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)4 HttpResponse (io.cdap.common.http.HttpResponse)4 File (java.io.File)4 HttpURLConnection (java.net.HttpURLConnection)4 Path (java.nio.file.Path)4 ZonedDateTime (java.time.ZonedDateTime)4 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)3 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)3 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)3 AccessException (io.cdap.cdap.api.security.AccessException)3 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)3 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)3 Retries (io.cdap.cdap.common.service.Retries)3 Notification (io.cdap.cdap.proto.Notification)3