Search in sources :

Example 1 with PulsarEvent

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

the class NamespaceEventsSystemTopicServiceTest method testSendAndReceiveNamespaceEvents.

@Test
public void testSendAndReceiveNamespaceEvents() throws Exception {
    TopicPoliciesSystemTopicClient systemTopicClientForNamespace1 = systemTopicFactory.createTopicPoliciesSystemTopicClient(NamespaceName.get(NAMESPACE1));
    TopicPolicies policies = TopicPolicies.builder().maxProducerPerTopic(10).build();
    PulsarEvent event = PulsarEvent.builder().eventType(EventType.TOPIC_POLICY).actionType(ActionType.INSERT).topicPoliciesEvent(TopicPoliciesEvent.builder().domain("persistent").tenant("system-topic").namespace(NamespaceName.get(NAMESPACE1).getLocalName()).topic("my-topic").policies(policies).build()).build();
    systemTopicClientForNamespace1.newWriter().write(event);
    SystemTopicClient.Reader reader = systemTopicClientForNamespace1.newReader();
    Message<PulsarEvent> received = reader.readNext();
    log.info("Receive pulsar event from system topic : {}", received.getValue());
    // test event send and receive
    Assert.assertEquals(received.getValue(), event);
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
    // test new reader read
    SystemTopicClient.Reader reader1 = systemTopicClientForNamespace1.newReader();
    Message<PulsarEvent> received1 = reader1.readNext();
    log.info("Receive pulsar event from system topic : {}", received1.getValue());
    Assert.assertEquals(received1.getValue(), event);
    // test writers and readers
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 2);
    SystemTopicClient.Writer writer = systemTopicClientForNamespace1.newWriter();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 2);
    writer.close();
    reader.close();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
    systemTopicClientForNamespace1.close();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 0);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 0);
}
Also used : TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) PulsarEvent(org.apache.pulsar.common.events.PulsarEvent) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 2 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent 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 3 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent in project 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) 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 4 with PulsarEvent

use of org.apache.pulsar.common.events.PulsarEvent 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 5 with PulsarEvent

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

the class NamespaceEventsSystemTopicServiceTest method testSendAndReceiveNamespaceEvents.

@Test
public void testSendAndReceiveNamespaceEvents() throws Exception {
    TopicPoliciesSystemTopicClient systemTopicClientForNamespace1 = systemTopicFactory.createTopicPoliciesSystemTopicClient(NamespaceName.get(NAMESPACE1));
    TopicPolicies policies = TopicPolicies.builder().maxProducerPerTopic(10).build();
    PulsarEvent event = PulsarEvent.builder().eventType(EventType.TOPIC_POLICY).actionType(ActionType.INSERT).topicPoliciesEvent(TopicPoliciesEvent.builder().domain("persistent").tenant("system-topic").namespace(NamespaceName.get(NAMESPACE1).getLocalName()).topic("my-topic").policies(policies).build()).build();
    systemTopicClientForNamespace1.newWriter().write(event);
    SystemTopicClient.Reader reader = systemTopicClientForNamespace1.newReader();
    Message<PulsarEvent> received = reader.readNext();
    log.info("Receive pulsar event from system topic : {}", received.getValue());
    // test event send and receive
    Assert.assertEquals(received.getValue(), event);
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
    // test new reader read
    SystemTopicClient.Reader reader1 = systemTopicClientForNamespace1.newReader();
    Message<PulsarEvent> received1 = reader1.readNext();
    log.info("Receive pulsar event from system topic : {}", received1.getValue());
    Assert.assertEquals(received1.getValue(), event);
    // test writers and readers
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 2);
    SystemTopicClient.Writer writer = systemTopicClientForNamespace1.newWriter();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 2);
    writer.close();
    reader.close();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 1);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 1);
    systemTopicClientForNamespace1.close();
    Assert.assertEquals(systemTopicClientForNamespace1.getWriters().size(), 0);
    Assert.assertEquals(systemTopicClientForNamespace1.getReaders().size(), 0);
}
Also used : TopicPolicies(org.apache.pulsar.common.policies.data.TopicPolicies) 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