use of io.cdap.cdap.api.retry.RetryableException in project cdap by cdapio.
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 cdapio.
the class AppCreator method execute.
@Override
public void execute(Arguments arguments) throws Exception {
ApplicationId appId = arguments.getId();
ArtifactSummary artifactSummary = arguments.getArtifact();
if (appExists(appId) && !arguments.overwrite) {
return;
}
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 {
appLifecycleService.deployApp(appId.getParent(), appId.getApplication(), appId.getVersion(), artifactSummary, configString, x -> {
}, ownerPrincipalId, arguments.canUpdateSchedules(), false, Collections.emptyMap());
} catch (NotFoundException | UnauthorizedException | InvalidArtifactException e) {
// up to the default time limit
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 cdapio.
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 cdapio.
the class DefaultNamespaceCreator method execute.
@Override
public void execute(EmptyArguments arguments) throws FileAlreadyExistsException {
try {
if (!namespaceAdmin.exists(NamespaceId.DEFAULT)) {
namespaceAdmin.create(NamespaceMeta.DEFAULT);
LOG.info("Successfully created namespace '{}'.", NamespaceMeta.DEFAULT);
}
} catch (FileAlreadyExistsException e) {
// avoid retrying if its a FileAlreadyExistsException
throw e;
} catch (NamespaceAlreadyExistsException e) {
// default namespace already exists, move on
} catch (Exception e) {
// the default namespace is valid so any exception here is transient and should be retried.
throw new RetryableException(e);
}
}
use of io.cdap.cdap.api.retry.RetryableException in project cdap by cdapio.
the class AdminEventPublisher method publishMessage.
private void publishMessage(EntityId entityId, MetadataMessage.Type type, Object payload) {
MetadataMessage message = new MetadataMessage(type, entityId, GSON.toJsonTree(payload));
LOG.trace("Publishing message: {}", message);
try {
Retries.supplyWithRetries(() -> {
try {
messagingContext.getMessagePublisher().publish(NamespaceId.SYSTEM.getNamespace(), topic.getTopic(), GSON.toJson(message));
} catch (TopicNotFoundException | ServiceUnavailableException e) {
throw new RetryableException(e);
} catch (IOException | AccessException e) {
throw Throwables.propagate(e);
}
return null;
}, retryStrategy);
} catch (Exception e) {
throw new RuntimeException(String.format("Failed to publish profile metadata request for entity id %s", entityId), e);
}
}
Aggregations