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);
}
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;
}
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;
}
}
}
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;
}
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);
}
Aggregations