Search in sources :

Example 1 with Backoff

use of org.apache.pulsar.client.impl.Backoff in project pulsar by apache.

the class SystemTopicBasedTopicPoliciesService method createSystemTopicClientWithRetry.

protected CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> createSystemTopicClientWithRetry(NamespaceName namespace) {
    CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> result = new CompletableFuture<>();
    try {
        createSystemTopicFactoryIfNeeded();
    } catch (PulsarServerException e) {
        result.completeExceptionally(e);
        return result;
    }
    SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(namespace);
    Backoff backoff = new Backoff(1, TimeUnit.SECONDS, 3, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
    RetryUtil.retryAsynchronously(systemTopicClient::newReaderAsync, backoff, pulsarService.getExecutor(), result);
    return result;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Backoff(org.apache.pulsar.client.impl.Backoff)

Example 2 with Backoff

use of org.apache.pulsar.client.impl.Backoff in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesService method createSystemTopicClientWithRetry.

protected CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> createSystemTopicClientWithRetry(NamespaceName namespace) {
    CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> result = new CompletableFuture<>();
    try {
        createSystemTopicFactoryIfNeeded();
    } catch (PulsarServerException e) {
        result.completeExceptionally(e);
        return result;
    }
    SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(namespace);
    Backoff backoff = new Backoff(1, TimeUnit.SECONDS, 3, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
    RetryUtil.retryAsynchronously(systemTopicClient::newReaderAsync, backoff, pulsarService.getExecutor(), result);
    return result;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Backoff(org.apache.pulsar.client.impl.Backoff)

Example 3 with Backoff

use of org.apache.pulsar.client.impl.Backoff in project pulsar by yahoo.

the class TopicPoliciesService method getTopicPoliciesAsyncWithRetry.

/**
 * When getting TopicPolicies, if the initialization has not been completed,
 * we will go back off and try again until time out.
 * @param topicName topic name
 * @param backoff back off policy
 * @param isGlobal is global policies
 * @return CompletableFuture&lt;Optional&lt;TopicPolicies&gt;&gt;
 */
default CompletableFuture<Optional<TopicPolicies>> getTopicPoliciesAsyncWithRetry(TopicName topicName, final Backoff backoff, ScheduledExecutorService scheduledExecutorService, boolean isGlobal) {
    CompletableFuture<Optional<TopicPolicies>> response = new CompletableFuture<>();
    Backoff usedBackoff = backoff == null ? new BackoffBuilder().setInitialTime(500, TimeUnit.MILLISECONDS).setMandatoryStop(DEFAULT_GET_TOPIC_POLICY_TIMEOUT, TimeUnit.MILLISECONDS).setMax(DEFAULT_GET_TOPIC_POLICY_TIMEOUT, TimeUnit.MILLISECONDS).create() : backoff;
    try {
        RetryUtil.retryAsynchronously(() -> {
            CompletableFuture<Optional<TopicPolicies>> future = new CompletableFuture<>();
            try {
                future.complete(Optional.ofNullable(getTopicPolicies(topicName, isGlobal)));
            } catch (BrokerServiceException.TopicPoliciesCacheNotInitException exception) {
                future.completeExceptionally(exception);
            }
            return future;
        }, usedBackoff, scheduledExecutorService, response);
    } catch (Exception e) {
        response.completeExceptionally(e);
    }
    return response;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Optional(java.util.Optional) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) Backoff(org.apache.pulsar.client.impl.Backoff) BackoffBuilder(org.apache.pulsar.client.impl.BackoffBuilder) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException)

Example 4 with Backoff

use of org.apache.pulsar.client.impl.Backoff in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesServiceTest method testGetPolicyTimeout.

@Test
public void testGetPolicyTimeout() throws Exception {
    SystemTopicBasedTopicPoliciesService service = (SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService();
    Awaitility.await().untilAsserted(() -> assertTrue(service.policyCacheInitMap.get(TOPIC1.getNamespaceObject())));
    service.policyCacheInitMap.put(TOPIC1.getNamespaceObject(), false);
    long start = System.currentTimeMillis();
    Backoff backoff = new BackoffBuilder().setInitialTime(500, TimeUnit.MILLISECONDS).setMandatoryStop(5000, TimeUnit.MILLISECONDS).setMax(1000, TimeUnit.MILLISECONDS).create();
    try {
        service.getTopicPoliciesAsyncWithRetry(TOPIC1, backoff, pulsar.getExecutor(), false).get();
    } catch (Exception e) {
        assertTrue(e.getCause() instanceof TopicPoliciesCacheNotInitException);
    }
    long cost = System.currentTimeMillis() - start;
    assertTrue("actual:" + cost, cost >= 5000 - 1000);
}
Also used : TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) Backoff(org.apache.pulsar.client.impl.Backoff) BackoffBuilder(org.apache.pulsar.client.impl.BackoffBuilder) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 5 with Backoff

use of org.apache.pulsar.client.impl.Backoff in project incubator-pulsar by apache.

the class SystemTopicBasedTopicPoliciesService method createSystemTopicClientWithRetry.

protected CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> createSystemTopicClientWithRetry(NamespaceName namespace) {
    CompletableFuture<SystemTopicClient.Reader<PulsarEvent>> result = new CompletableFuture<>();
    try {
        createSystemTopicFactoryIfNeeded();
    } catch (PulsarServerException e) {
        result.completeExceptionally(e);
        return result;
    }
    SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(namespace);
    Backoff backoff = new Backoff(1, TimeUnit.SECONDS, 3, TimeUnit.SECONDS, 10, TimeUnit.SECONDS);
    RetryUtil.retryAsynchronously(systemTopicClient::newReaderAsync, backoff, pulsarService.getExecutor(), result);
    return result;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Backoff(org.apache.pulsar.client.impl.Backoff)

Aggregations

Backoff (org.apache.pulsar.client.impl.Backoff)13 CompletableFuture (java.util.concurrent.CompletableFuture)10 TopicPoliciesCacheNotInitException (org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException)9 BackoffBuilder (org.apache.pulsar.client.impl.BackoffBuilder)9 ExecutionException (java.util.concurrent.ExecutionException)6 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)6 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)6 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)6 PulsarEvent (org.apache.pulsar.common.events.PulsarEvent)6 Test (org.testng.annotations.Test)6 Field (java.lang.reflect.Field)3 Map (java.util.Map)3 Optional (java.util.Optional)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)3 SystemTopicClient (org.apache.pulsar.broker.systopic.SystemTopicClient)3 TopicPoliciesSystemTopicClient (org.apache.pulsar.broker.systopic.TopicPoliciesSystemTopicClient)3 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)3 TopicPolicies (org.apache.pulsar.common.policies.data.TopicPolicies)3