use of org.apache.pulsar.common.events.PulsarEvent 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;
}
use of org.apache.pulsar.common.events.PulsarEvent in project incubator-pulsar by apache.
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;
}
use of org.apache.pulsar.common.events.PulsarEvent in project incubator-pulsar by apache.
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);
}
use of org.apache.pulsar.common.events.PulsarEvent in project incubator-pulsar by apache.
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);
}
});
}
use of org.apache.pulsar.common.events.PulsarEvent in project incubator-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);
}
Aggregations