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;
}
}
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);
}
}
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));
}
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());
}
}
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);
}
}
Aggregations