Search in sources :

Example 41 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project failsafe by jhalterman.

the class Issue55 method shouldOnlyFallbackOnFailure.

public void shouldOnlyFallbackOnFailure() throws Throwable {
    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
    AtomicInteger counter = new AtomicInteger();
    Failsafe.with(new RetryPolicy()).with(executor).withFallback(() -> counter.incrementAndGet()).get(() -> null);
    Thread.sleep(100);
    assertEquals(counter.get(), 0);
    Failsafe.with(new RetryPolicy().withMaxRetries(1)).with(executor).withFallback(() -> counter.incrementAndGet()).run(() -> {
        throw new RuntimeException();
    });
    Thread.sleep(100);
    assertEquals(counter.get(), 1);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 42 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project ddf by codice.

the class HttpSolrClientFactory method getHttpSolrClient.

/**
     * Creates a new {@link HttpSolrClient} using the URL, core name and configuration file name
     * provided.
     *
     * @param url        Solr server URL. If {@code null}, defaults to the system's base URL
     *                   followed by {@code /solr}.
     * @param coreName   name of the Solr core to create
     * @param configFile configuration file name. If {@code null}, defaults to
     *                   {@value #DEFAULT_SOLRCONFIG_XML}.
     * @return {@code Future} used to retrieve the new {@link HttpSolrClient} instance
     */
public static Future<SolrClient> getHttpSolrClient(@Nullable String url, String coreName, @Nullable String configFile) {
    String solrUrl = StringUtils.defaultIfBlank(url, SystemBaseUrl.constructUrl("/solr"));
    String coreUrl = url + "/" + coreName;
    if (System.getProperty("solr.data.dir") != null) {
        ConfigurationStore.getInstance().setDataDirectoryPath(System.getProperty("solr.data.dir"));
    }
    RetryPolicy retryPolicy = new RetryPolicy().withBackoff(10, TimeUnit.MINUTES.toMillis(1), TimeUnit.MILLISECONDS);
    return Failsafe.with(retryPolicy).with(executor).onRetry((c, failure, ctx) -> LOGGER.debug("Attempt {} failed to create HTTP Solr client ({}). Retrying again.", ctx.getExecutions(), coreName)).onFailedAttempt(failure -> LOGGER.debug("Attempt failed to create HTTP Solr client (" + coreName + ")", failure)).onSuccess(client -> LOGGER.debug("Successfully created HTTP Solr client ({})", coreName)).onFailure(failure -> LOGGER.warn("All attempts failed to create HTTP Solr client (" + coreName + ")", failure)).get(() -> createSolrHttpClient(solrUrl, coreName, configFile, coreUrl));
}
Also used : Arrays(java.util.Arrays) SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) HttpHead(org.apache.http.client.methods.HttpHead) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) CoreAdminResponse(org.apache.solr.client.solrj.response.CoreAdminResponse) LoggerFactory(org.slf4j.LoggerFactory) KeyStoreException(java.security.KeyStoreException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Future(java.util.concurrent.Future) HttpClient(org.apache.http.client.HttpClient) UnrecoverableKeyException(java.security.UnrecoverableKeyException) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) Nullable(javax.annotation.Nullable) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) Logger(org.slf4j.Logger) SystemBaseUrl(org.codice.ddf.configuration.SystemBaseUrl) SSLContexts(org.apache.http.conn.ssl.SSLContexts) IOException(java.io.IOException) KeyStore(java.security.KeyStore) RetryPolicy(net.jodah.failsafe.RetryPolicy) FileInputStream(java.io.FileInputStream) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) Executors(java.util.concurrent.Executors) SolrClient(org.apache.solr.client.solrj.SolrClient) Failsafe(net.jodah.failsafe.Failsafe) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Paths(java.nio.file.Paths) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) Collections(java.util.Collections) CoreAdminRequest(org.apache.solr.client.solrj.request.CoreAdminRequest) HttpClients(org.apache.http.impl.client.HttpClients) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 43 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project divolte-collector by divolte.

the class GoogleCloudStorageRetryConfiguration method createRetryPolicy.

public RetryPolicy createRetryPolicy() {
    final RetryPolicy policyBeforeJitter = new RetryPolicy().withMaxRetries(maxAttempts - 1).withMaxDuration(totalTimeout.toNanos(), TimeUnit.NANOSECONDS).withBackoff(initialRetryDelay.toNanos(), maxRetryDelay.toNanos(), TimeUnit.NANOSECONDS, retryDelayMultiplier);
    final RetryPolicy policyWithJitterFactor = jitterFactor.map(policyBeforeJitter::withJitter).orElse(policyBeforeJitter);
    return jitterDelay.map(d -> policyWithJitterFactor.withJitter(d.toNanos(), TimeUnit.NANOSECONDS)).orElse(policyWithJitterFactor);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) EitherJitterDurationOrFactorButNotBoth(io.divolte.server.config.constraint.EitherJitterDurationOrFactorButNotBoth) ParametersAreNullableByDefault(javax.annotation.ParametersAreNullableByDefault) Duration(java.time.Duration) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) MoreObjects(com.google.common.base.MoreObjects) Optional(java.util.Optional) RetryPolicy(net.jodah.failsafe.RetryPolicy) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 44 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project alliance by codice.

the class CatalogUpdateRetry method submitUpdateRequestWithRetry.

public void submitUpdateRequestWithRetry(CatalogFramework catalogFramework, UpdateRequest updateRequest, long initialSleepSeconds, long initialRetryWaitMilliseconds, long maxRetryMilliseconds, Consumer<Update> updateConsumer) {
    if (sleep(TimeUnit.SECONDS.toMillis(initialSleepSeconds))) {
        return;
    }
    RetryPolicy retryPolicy = new RetryPolicy().retryOn(IngestException.class, SourceUnavailableException.class).withBackoff(initialRetryWaitMilliseconds, maxRetryMilliseconds, TimeUnit.MILLISECONDS);
    Failsafe.with(retryPolicy).onFailedAttempt(throwable -> LOGGER.debug("failed to update catalog, will retry: updateRequest={}", updateRequest)).onFailure(throwable -> LOGGER.debug("failed to update catalog: updateRequest={}", updateRequest)).run(() -> catalogFramework.update(updateRequest).getUpdatedMetacards().forEach(updateConsumer));
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Logger(org.slf4j.Logger) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) CatalogFramework(ddf.catalog.CatalogFramework) IngestException(ddf.catalog.source.IngestException) UpdateRequest(ddf.catalog.operation.UpdateRequest) LoggerFactory(org.slf4j.LoggerFactory) RetryPolicy(net.jodah.failsafe.RetryPolicy) Update(ddf.catalog.operation.Update) Failsafe(net.jodah.failsafe.Failsafe) SourceUnavailableException(ddf.catalog.source.SourceUnavailableException) IngestException(ddf.catalog.source.IngestException) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 45 with RetryPolicy

use of net.jodah.failsafe.RetryPolicy in project ddf by codice.

the class ContentProducerDataAccessObject method waitForAvailableSource.

private void waitForAvailableSource(CatalogFramework catalogFramework) throws SourceUnavailableException {
    RetryPolicy retryPolicy = new RetryPolicy().withDelay(3, TimeUnit.SECONDS).withMaxDuration(3, TimeUnit.MINUTES).retryIf((Predicate<Set>) Set::isEmpty).retryIf((Set<SourceDescriptor> result) -> !result.stream().findFirst().get().isAvailable());
    Failsafe.with(retryPolicy).get(() -> catalogFramework.getSourceInfo(new SourceInfoRequestLocal(false)).getSourceInfo());
}
Also used : Set(java.util.Set) SourceInfoRequestLocal(ddf.catalog.operation.impl.SourceInfoRequestLocal) RetryPolicy(net.jodah.failsafe.RetryPolicy) Predicate(net.jodah.failsafe.function.Predicate)

Aggregations

RetryPolicy (net.jodah.failsafe.RetryPolicy)56 Failsafe (net.jodah.failsafe.Failsafe)16 IOException (java.io.IOException)8 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 TimeUnit (java.util.concurrent.TimeUnit)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 Executors (java.util.concurrent.Executors)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Test (org.testng.annotations.Test)5 ConnectException (java.net.ConnectException)4 Duration (java.time.Duration)4 Map (java.util.Map)4 FailsafeException (net.jodah.failsafe.FailsafeException)4 Assert.assertEquals (org.testng.Assert.assertEquals)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3