Search in sources :

Example 11 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project incubator-pulsar by apache.

the class SystemTopicBasedTopicPoliciesService method refreshTopicPoliciesCache.

private void refreshTopicPoliciesCache(Message<PulsarEvent> msg) {
    // delete policies
    if (msg.getValue() == null) {
        TopicName topicName = TopicName.get(TopicName.get(msg.getKey()).getPartitionedTopicName());
        if (hasReplicateTo(msg)) {
            globalPoliciesCache.remove(topicName);
        } else {
            policiesCache.remove(topicName);
        }
        return;
    }
    if (EventType.TOPIC_POLICY.equals(msg.getValue().getEventType())) {
        TopicPoliciesEvent event = msg.getValue().getTopicPoliciesEvent();
        TopicName topicName = TopicName.get(event.getDomain(), event.getTenant(), event.getNamespace(), event.getTopic());
        switch(msg.getValue().getActionType()) {
            case INSERT:
                TopicPolicies old = event.getPolicies().isGlobalPolicies() ? globalPoliciesCache.putIfAbsent(topicName, event.getPolicies()) : policiesCache.putIfAbsent(topicName, event.getPolicies());
                if (old != null) {
                    log.warn("Policy insert failed, the topic: {} policy already exist", topicName);
                }
                break;
            case UPDATE:
                if (event.getPolicies().isGlobalPolicies()) {
                    globalPoliciesCache.put(topicName, event.getPolicies());
                } else {
                    policiesCache.put(topicName, event.getPolicies());
                }
                break;
            case DELETE:
                // Since PR #11928, this branch is no longer needed.
                // However, due to compatibility, it is temporarily retained here
                // and can be deleted in the future.
                policiesCache.remove(topicName);
                try {
                    createSystemTopicFactoryIfNeeded();
                } catch (PulsarServerException e) {
                    log.error("Failed to create system topic factory");
                    break;
                }
                SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(topicName.getNamespaceObject());
                systemTopicClient.newWriterAsync().thenAccept(writer -> writer.deleteAsync(getPulsarEvent(topicName, ActionType.DELETE, null)).whenComplete((result, e) -> writer.closeAsync().whenComplete((res, ex) -> {
                    if (ex != null) {
                        log.error("close writer failed ", ex);
                    }
                })));
                break;
            case NONE:
                break;
            default:
                log.warn("Unknown event action type: {}", msg.getValue().getActionType());
                break;
        }
    }
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) NamespaceBundleOwnershipListener(org.apache.pulsar.broker.namespace.NamespaceBundleOwnershipListener) TopicName(org.apache.pulsar.common.naming.TopicName) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) Message(org.apache.pulsar.client.api.Message) HashSet(java.util.HashSet) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) RetryUtil(org.apache.pulsar.client.util.RetryUtil) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ActionType(org.apache.pulsar.common.events.ActionType) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) TopicPoliciesEvent(org.apache.pulsar.common.events.TopicPoliciesEvent) Logger(org.slf4j.Logger) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PulsarService(org.apache.pulsar.broker.PulsarService) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) Backoff(org.apache.pulsar.client.impl.Backoff) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) EventType(org.apache.pulsar.common.events.EventType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NamespaceEventsSystemTopicFactory(org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) TopicPoliciesEvent(org.apache.pulsar.common.events.TopicPoliciesEvent) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 12 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesService method refreshTopicPoliciesCache.

private void refreshTopicPoliciesCache(Message<PulsarEvent> msg) {
    // delete policies
    if (msg.getValue() == null) {
        TopicName topicName = TopicName.get(TopicName.get(msg.getKey()).getPartitionedTopicName());
        if (hasReplicateTo(msg)) {
            globalPoliciesCache.remove(topicName);
        } else {
            policiesCache.remove(topicName);
        }
        return;
    }
    if (EventType.TOPIC_POLICY.equals(msg.getValue().getEventType())) {
        TopicPoliciesEvent event = msg.getValue().getTopicPoliciesEvent();
        TopicName topicName = TopicName.get(event.getDomain(), event.getTenant(), event.getNamespace(), event.getTopic());
        switch(msg.getValue().getActionType()) {
            case INSERT:
                TopicPolicies old = event.getPolicies().isGlobalPolicies() ? globalPoliciesCache.putIfAbsent(topicName, event.getPolicies()) : policiesCache.putIfAbsent(topicName, event.getPolicies());
                if (old != null) {
                    log.warn("Policy insert failed, the topic: {} policy already exist", topicName);
                }
                break;
            case UPDATE:
                if (event.getPolicies().isGlobalPolicies()) {
                    globalPoliciesCache.put(topicName, event.getPolicies());
                } else {
                    policiesCache.put(topicName, event.getPolicies());
                }
                break;
            case DELETE:
                // Since PR #11928, this branch is no longer needed.
                // However, due to compatibility, it is temporarily retained here
                // and can be deleted in the future.
                policiesCache.remove(topicName);
                try {
                    createSystemTopicFactoryIfNeeded();
                } catch (PulsarServerException e) {
                    log.error("Failed to create system topic factory");
                    break;
                }
                SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(topicName.getNamespaceObject());
                systemTopicClient.newWriterAsync().thenAccept(writer -> writer.deleteAsync(getPulsarEvent(topicName, ActionType.DELETE, null)).whenComplete((result, e) -> writer.closeAsync().whenComplete((res, ex) -> {
                    if (ex != null) {
                        log.error("close writer failed ", ex);
                    }
                })));
                break;
            case NONE:
                break;
            default:
                log.warn("Unknown event action type: {}", msg.getValue().getActionType());
                break;
        }
    }
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) NamespaceBundleOwnershipListener(org.apache.pulsar.broker.namespace.NamespaceBundleOwnershipListener) TopicName(org.apache.pulsar.common.naming.TopicName) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) Message(org.apache.pulsar.client.api.Message) HashSet(java.util.HashSet) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) RetryUtil(org.apache.pulsar.client.util.RetryUtil) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ActionType(org.apache.pulsar.common.events.ActionType) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) TopicPoliciesEvent(org.apache.pulsar.common.events.TopicPoliciesEvent) Logger(org.slf4j.Logger) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PulsarService(org.apache.pulsar.broker.PulsarService) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) Backoff(org.apache.pulsar.client.impl.Backoff) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) EventType(org.apache.pulsar.common.events.EventType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NamespaceEventsSystemTopicFactory(org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) TopicPoliciesEvent(org.apache.pulsar.common.events.TopicPoliciesEvent) TopicName(org.apache.pulsar.common.naming.TopicName)

Example 13 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesService method sendTopicPolicyEvent.

private CompletableFuture<Void> sendTopicPolicyEvent(TopicName topicName, ActionType actionType, TopicPolicies policies) {
    CompletableFuture<Void> result = new CompletableFuture<>();
    try {
        createSystemTopicFactoryIfNeeded();
    } catch (PulsarServerException e) {
        result.completeExceptionally(e);
        return result;
    }
    SystemTopicClient<PulsarEvent> systemTopicClient = namespaceEventsSystemTopicFactory.createTopicPoliciesSystemTopicClient(topicName.getNamespaceObject());
    CompletableFuture<SystemTopicClient.Writer<PulsarEvent>> writerFuture = systemTopicClient.newWriterAsync();
    writerFuture.whenComplete((writer, ex) -> {
        if (ex != null) {
            result.completeExceptionally(ex);
        } else {
            PulsarEvent event = getPulsarEvent(topicName, actionType, policies);
            CompletableFuture<MessageId> actionFuture = ActionType.DELETE.equals(actionType) ? writer.deleteAsync(event) : writer.writeAsync(event);
            actionFuture.whenComplete(((messageId, e) -> {
                if (e != null) {
                    result.completeExceptionally(e);
                } else {
                    if (messageId != null) {
                        result.complete(null);
                    } else {
                        result.completeExceptionally(new RuntimeException("Got message id is null."));
                    }
                }
                writer.closeAsync().whenComplete((v, cause) -> {
                    if (cause != null) {
                        log.error("[{}] Close writer error.", topicName, cause);
                    } else {
                        if (log.isDebugEnabled()) {
                            log.debug("[{}] Close writer success.", topicName);
                        }
                    }
                });
            }));
        }
    });
    return result;
}
Also used : PulsarServerException(org.apache.pulsar.broker.PulsarServerException) NamespaceBundleOwnershipListener(org.apache.pulsar.broker.namespace.NamespaceBundleOwnershipListener) TopicName(org.apache.pulsar.common.naming.TopicName) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) NamespaceService(org.apache.pulsar.broker.namespace.NamespaceService) Message(org.apache.pulsar.client.api.Message) HashSet(java.util.HashSet) MessageImpl(org.apache.pulsar.client.impl.MessageImpl) Lists(com.google.common.collect.Lists) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TopicMessageImpl(org.apache.pulsar.client.impl.TopicMessageImpl) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) RetryUtil(org.apache.pulsar.client.util.RetryUtil) NamespaceBundle(org.apache.pulsar.common.naming.NamespaceBundle) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ActionType(org.apache.pulsar.common.events.ActionType) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) TopicPoliciesEvent(org.apache.pulsar.common.events.TopicPoliciesEvent) Logger(org.slf4j.Logger) TopicPoliciesCacheNotInitException(org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PulsarService(org.apache.pulsar.broker.PulsarService) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) List(java.util.List) FutureUtil(org.apache.pulsar.common.util.FutureUtil) MessageId(org.apache.pulsar.client.api.MessageId) Backoff(org.apache.pulsar.client.impl.Backoff) PulsarServerException(org.apache.pulsar.broker.PulsarServerException) EventType(org.apache.pulsar.common.events.EventType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NamespaceEventsSystemTopicFactory(org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory) CompletableFuture(java.util.concurrent.CompletableFuture) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) MessageId(org.apache.pulsar.client.api.MessageId)

Example 14 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesServiceTest method testGetTopicPoliciesWithRetry.

@Test
public void testGetTopicPoliciesWithRetry() throws Exception {
    Field initMapField = SystemTopicBasedTopicPoliciesService.class.getDeclaredField("policyCacheInitMap");
    initMapField.setAccessible(true);
    Map<NamespaceName, Boolean> initMap = (Map) initMapField.get(systemTopicBasedTopicPoliciesService);
    initMap.remove(NamespaceName.get(NAMESPACE1));
    Field readerCaches = SystemTopicBasedTopicPoliciesService.class.getDeclaredField("readerCaches");
    readerCaches.setAccessible(true);
    Map<NamespaceName, CompletableFuture<SystemTopicClient.Reader<PulsarEvent>>> readers = (Map) readerCaches.get(systemTopicBasedTopicPoliciesService);
    readers.remove(NamespaceName.get(NAMESPACE1));
    Backoff backoff = new BackoffBuilder().setInitialTime(500, TimeUnit.MILLISECONDS).setMandatoryStop(5000, TimeUnit.MILLISECONDS).setMax(1000, TimeUnit.MILLISECONDS).create();
    TopicPolicies initPolicy = TopicPolicies.builder().maxConsumerPerTopic(10).build();
    ScheduledExecutorService executors = Executors.newScheduledThreadPool(1);
    executors.schedule(new Runnable() {

        @Override
        public void run() {
            try {
                systemTopicBasedTopicPoliciesService.updateTopicPoliciesAsync(TOPIC1, initPolicy).get();
            } catch (Exception ignore) {
            }
        }
    }, 2000, TimeUnit.MILLISECONDS);
    Awaitility.await().untilAsserted(() -> {
        Optional<TopicPolicies> topicPolicies = systemTopicBasedTopicPoliciesService.getTopicPoliciesAsyncWithRetry(TOPIC1, backoff, pulsar.getExecutor(), false).get();
        Assert.assertTrue(topicPolicies.isPresent());
        if (topicPolicies.isPresent()) {
            Assert.assertEquals(topicPolicies.get(), initPolicy);
        }
    });
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Backoff(org.apache.pulsar.client.impl.Backoff) 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) Field(java.lang.reflect.Field) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) CompletableFuture(java.util.concurrent.CompletableFuture) TopicPoliciesSystemTopicClient(org.apache.pulsar.broker.systopic.TopicPoliciesSystemTopicClient) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) Map(java.util.Map) BackoffBuilder(org.apache.pulsar.client.impl.BackoffBuilder) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 15 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project pulsar by yahoo.

the class SystemTopicBasedTopicPoliciesServiceTest method testCreatSystemTopicClientWithRetry.

@Test
public void testCreatSystemTopicClientWithRetry() throws Exception {
    SystemTopicBasedTopicPoliciesService service = spy((SystemTopicBasedTopicPoliciesService) pulsar.getTopicPoliciesService());
    Field field = SystemTopicBasedTopicPoliciesService.class.getDeclaredField("namespaceEventsSystemTopicFactory");
    field.setAccessible(true);
    NamespaceEventsSystemTopicFactory factory = spy((NamespaceEventsSystemTopicFactory) field.get(service));
    SystemTopicClient<PulsarEvent> client = mock(TopicPoliciesSystemTopicClient.class);
    doReturn(client).when(factory).createTopicPoliciesSystemTopicClient(any());
    field.set(service, factory);
    SystemTopicClient.Reader<PulsarEvent> reader = mock(SystemTopicClient.Reader.class);
    // Throw an exception first, create successfully after retrying
    doReturn(FutureUtil.failedFuture(new PulsarClientException("test"))).doReturn(CompletableFuture.completedFuture(reader)).when(client).newReaderAsync();
    SystemTopicClient.Reader<PulsarEvent> reader1 = service.createSystemTopicClientWithRetry(null).get();
    assertEquals(reader1, reader);
}
Also used : Field(java.lang.reflect.Field) TopicPoliciesSystemTopicClient(org.apache.pulsar.broker.systopic.TopicPoliciesSystemTopicClient) SystemTopicClient(org.apache.pulsar.broker.systopic.SystemTopicClient) NamespaceEventsSystemTopicFactory(org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

PulsarEvent (org.apache.pulsar.common.events.PulsarEvent)18 CompletableFuture (java.util.concurrent.CompletableFuture)12 SystemTopicClient (org.apache.pulsar.broker.systopic.SystemTopicClient)12 PulsarClientException (org.apache.pulsar.client.api.PulsarClientException)12 Backoff (org.apache.pulsar.client.impl.Backoff)12 TopicPolicies (org.apache.pulsar.common.policies.data.TopicPolicies)12 Map (java.util.Map)9 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)9 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)9 TopicPoliciesCacheNotInitException (org.apache.pulsar.broker.service.BrokerServiceException.TopicPoliciesCacheNotInitException)9 NamespaceEventsSystemTopicFactory (org.apache.pulsar.broker.systopic.NamespaceEventsSystemTopicFactory)9 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)9 Test (org.testng.annotations.Test)9 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 Lists (com.google.common.collect.Lists)6 Sets (com.google.common.collect.Sets)6 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)6 List (java.util.List)6 Objects (java.util.Objects)6