Search in sources :

Example 21 with RetryPolicy

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

the class SolrClientAdaptor method init.

public void init() {
    RetryPolicy retryPolicy = new RetryPolicy();
    Failsafe.with(retryPolicy).onRetry((exception) -> LOGGER.debug("Failed to get Solr client for SolrCache. Retrying...", exception)).onSuccess((result, context) -> LOGGER.debug("SolrCache successfully initialized after {} retries", context.getExecutions())).run(this::getSolrClient);
}
Also used : Logger(org.slf4j.Logger) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy) FilterAdapter(ddf.catalog.filter.FilterAdapter) Function(java.util.function.Function) SolrClient(org.apache.solr.client.solrj.SolrClient) Failsafe(net.jodah.failsafe.Failsafe) ExecutionException(java.util.concurrent.ExecutionException) SolrServerException(org.apache.solr.client.solrj.SolrServerException) Future(java.util.concurrent.Future) SolrFilterDelegateFactory(ddf.catalog.source.solr.SolrFilterDelegateFactory) SECONDS(java.util.concurrent.TimeUnit.SECONDS) UpdateResponse(org.apache.solr.client.solrj.response.UpdateResponse) SolrMetacardClient(ddf.catalog.source.solr.SolrMetacardClient) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 22 with RetryPolicy

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

the class SolrCloudClientFactory method isCollectionReady.

private static boolean isCollectionReady(CloudSolrClient client, String collection) {
    RetryPolicy retryPolicy = new RetryPolicy().retryWhen(false).withMaxRetries(30).withDelay(1, TimeUnit.SECONDS);
    boolean collectionCreated = Failsafe.with(retryPolicy).onFailure(failure -> LOGGER.debug("All attempts failed to read Zookeeper state for collection existence (" + collection + ")", failure)).get(() -> client.getZkStateReader().getClusterState().hasCollection(collection));
    if (!collectionCreated) {
        LOGGER.debug("Timeout while waiting for collection to be created: " + collection);
        return false;
    }
    boolean shardsStarted = Failsafe.with(retryPolicy).onFailure(failure -> LOGGER.debug("All attempts failed to read Zookeeper state for collection's shard count (" + collection + ")", failure)).get(() -> client.getZkStateReader().getClusterState().getSlices(collection).size() == SHARD_COUNT);
    if (!shardsStarted) {
        LOGGER.debug("Timeout while waiting for collection shards to start: " + collection);
    }
    return shardsStarted;
}
Also used : Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) SolrClientFactory(org.codice.solr.factory.SolrClientFactory) LoggerFactory(org.slf4j.LoggerFactory) CollectionAdminResponse(org.apache.solr.client.solrj.response.CollectionAdminResponse) IOException(java.io.IOException) RetryPolicy(net.jodah.failsafe.RetryPolicy) Executors(java.util.concurrent.Executors) NumberUtils(org.apache.commons.lang.math.NumberUtils) SolrClient(org.apache.solr.client.solrj.SolrClient) Failsafe(net.jodah.failsafe.Failsafe) TimeUnit(java.util.concurrent.TimeUnit) SolrServerException(org.apache.solr.client.solrj.SolrServerException) List(java.util.List) Future(java.util.concurrent.Future) Paths(java.nio.file.Paths) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Path(java.nio.file.Path) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 23 with RetryPolicy

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

the class RetryPolicyTest method shouldRequireValidDelay.

public void shouldRequireValidDelay() {
    shouldFail(() -> new RetryPolicy().withDelay(5, null), NullPointerException.class);
    shouldFail(() -> new RetryPolicy().withMaxDuration(1, TimeUnit.MILLISECONDS).withDelay(100, TimeUnit.MILLISECONDS), IllegalStateException.class);
    shouldFail(() -> new RetryPolicy().withBackoff(1, 2, TimeUnit.MILLISECONDS).withDelay(100, TimeUnit.MILLISECONDS), IllegalStateException.class);
    shouldFail(() -> new RetryPolicy().withDelay(-1, TimeUnit.MILLISECONDS), IllegalArgumentException.class);
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Example 24 with RetryPolicy

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

the class RetryPolicyTest method testCanRetryForFailurePredicate.

public void testCanRetryForFailurePredicate() {
    RetryPolicy policy = new RetryPolicy().retryOn(failure -> failure instanceof ConnectException);
    assertTrue(policy.canRetryFor(null, new ConnectException()));
    assertFalse(policy.canRetryFor(null, new IllegalStateException()));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy) ConnectException(java.net.ConnectException)

Example 25 with RetryPolicy

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

the class RetryPolicyTest method testCanRetryForResultPredicate.

public void testCanRetryForResultPredicate() {
    RetryPolicy policy = new RetryPolicy().retryIf((Integer result) -> result > 100);
    assertTrue(policy.canRetryFor(110, null));
    assertFalse(policy.canRetryFor(50, null));
}
Also used : RetryPolicy(net.jodah.failsafe.RetryPolicy)

Aggregations

RetryPolicy (net.jodah.failsafe.RetryPolicy)34 Failsafe (net.jodah.failsafe.Failsafe)12 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 IOException (java.io.IOException)7 Test (org.testng.annotations.Test)7 Executors (java.util.concurrent.Executors)6 Assert.assertEquals (org.testng.Assert.assertEquals)6 ConnectException (java.net.ConnectException)5 Assert.fail (org.testng.Assert.fail)5 Future (java.util.concurrent.Future)4 TimeUnit (java.util.concurrent.TimeUnit)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 BeforeMethod (org.testng.annotations.BeforeMethod)4 Waiter (net.jodah.concurrentunit.Waiter)3 Paths (java.nio.file.Paths)2 List (java.util.List)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2