Search in sources :

Example 1 with RetryableException

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));
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException)

Example 2 with RetryableException

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;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) RetryStrategy(io.cdap.cdap.common.service.RetryStrategy) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) StandardCopyOption(java.nio.file.StandardCopyOption) HttpHeaders(com.google.common.net.HttpHeaders) CharStreams(com.google.common.io.CharStreams) RemoteClient(io.cdap.cdap.common.internal.remote.RemoteClient) Map(java.util.Map) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) FileUtils(io.cdap.cdap.common.utils.FileUtils) HttpMethod(io.cdap.common.http.HttpMethod) Logger(org.slf4j.Logger) Files(java.nio.file.Files) RetryableException(io.cdap.cdap.api.retry.RetryableException) Collection(java.util.Collection) IOException(java.io.IOException) Reader(java.io.Reader) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ZoneId(java.time.ZoneId) List(java.util.List) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) DateTimeFormatter(java.time.format.DateTimeFormatter) Constants(io.cdap.cdap.common.conf.Constants) DirUtils(io.cdap.cdap.common.utils.DirUtils) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) InputStream(java.io.InputStream) RetryableException(io.cdap.cdap.api.retry.RetryableException) ZonedDateTime(java.time.ZonedDateTime) List(java.util.List)

Example 3 with RetryableException

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);
    }
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) RetryableException(io.cdap.cdap.api.retry.RetryableException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) RetryableException(io.cdap.cdap.api.retry.RetryableException) NamespaceAlreadyExistsException(io.cdap.cdap.common.NamespaceAlreadyExistsException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException)

Example 4 with RetryableException

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);
    }
}
Also used : RetryableException(io.cdap.cdap.api.retry.RetryableException) AccessException(io.cdap.cdap.api.security.AccessException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) MetadataMessage(io.cdap.cdap.data2.metadata.writer.MetadataMessage) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) AccessException(io.cdap.cdap.api.security.AccessException) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) RetryableException(io.cdap.cdap.api.retry.RetryableException) IOException(java.io.IOException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException)

Example 5 with RetryableException

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);
    }
}
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) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) RetryableException(io.cdap.cdap.api.retry.RetryableException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) NotFoundException(io.cdap.cdap.common.NotFoundException)

Aggregations

RetryableException (io.cdap.cdap.api.retry.RetryableException)14 IOException (java.io.IOException)8 NotFoundException (io.cdap.cdap.common.NotFoundException)3 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)3 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)3 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)2 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)2 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)2 AccessException (io.cdap.cdap.api.security.AccessException)2 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)2 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)2 Constants (io.cdap.cdap.common.conf.Constants)2 Retries (io.cdap.cdap.common.service.Retries)2 Notification (io.cdap.cdap.proto.Notification)2 KerberosPrincipalId (io.cdap.cdap.proto.id.KerberosPrincipalId)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)2 HttpResponse (io.cdap.common.http.HttpResponse)2 Stopwatch (com.google.common.base.Stopwatch)1 CharStreams (com.google.common.io.CharStreams)1