use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.
the class AbstractServiceRetryableMacroEvaluator method evaluateMap.
@Override
public Map<String, String> evaluateMap(String macroFunction, String... args) throws InvalidMacroException {
if (!functionName.equals(macroFunction)) {
// This shouldn't happen
throw new IllegalArgumentException("Invalid function name " + macroFunction + ". Expecting " + functionName);
}
// Make call with exponential delay on failure retry.
long delay = RETRY_BASE_DELAY_MILLIS;
double minMultiplier = RETRY_DELAY_MULTIPLIER - RETRY_DELAY_MULTIPLIER * RETRY_RANDOMIZE_FACTOR;
double maxMultiplier = RETRY_DELAY_MULTIPLIER + RETRY_DELAY_MULTIPLIER * RETRY_RANDOMIZE_FACTOR;
Stopwatch stopWatch = new Stopwatch().start();
try {
while (stopWatch.elapsedTime(TimeUnit.MILLISECONDS) < TIMEOUT_MILLIS) {
try {
return evaluateMacroMap(macroFunction, args);
} catch (RetryableException e) {
TimeUnit.MILLISECONDS.sleep(delay);
delay = (long) (delay * (minMultiplier + Math.random() * (maxMultiplier - minMultiplier + 1)));
delay = Math.min(delay, RETRY_MAX_DELAY_MILLIS);
} catch (IOException e) {
throw new RuntimeException("Failed to evaluate the macro function '" + functionName + "' with args " + Arrays.asList(args), e);
}
}
} catch (InterruptedException e) {
throw new RuntimeException("Thread interrupted while trying to evaluate the macro function '" + functionName + "' with args " + Arrays.asList(args), e);
}
throw new IllegalStateException("Timed out when trying to evaluate the macro function '" + functionName + "' with args " + Arrays.asList(args));
}
use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.
the class AbstractArtifactLocalizer method getLastModifiedHeader.
/**
* Helper function for verifying, extracting and converting the Last-Modified header from the URL connection.
*/
private ZonedDateTime getLastModifiedHeader(HttpURLConnection urlConn) {
Map<String, List<String>> headers = urlConn.getHeaderFields();
ZonedDateTime lastModified = headers.entrySet().stream().filter(headerEntry -> HttpHeaders.LAST_MODIFIED.equalsIgnoreCase(headerEntry.getKey())).map(Map.Entry::getValue).flatMap(Collection::stream).findFirst().map(s -> ZonedDateTime.parse(s, DateTimeFormatter.RFC_1123_DATE_TIME)).orElse(null);
if (lastModified == null) {
// If it does happen we should retry.
throw new RetryableException(String.format("The response from %s did not contain the %s header.", urlConn.getURL(), HttpHeaders.LAST_MODIFIED));
}
return lastModified;
}
use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.
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 caskdata.
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);
}
}
use of io.cdap.cdap.api.retry.RetryableException in project cdap by caskdata.
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);
}
}
Aggregations