use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class ResendRequestTest method testExclusiveSingleAckedNormalTopic.
@Test(timeOut = testTimeout)
public void testExclusiveSingleAckedNormalTopic() throws Exception {
String key = "testExclusiveSingleAckedNormalTopic";
final String topicName = "persistent://prop/use/ns-abc/topic-" + key;
final String subscriptionName = "my-ex-subscription-" + key;
final String messagePredicate = "my-message-" + key + "-";
final int totalMessages = 10;
HashSet<MessageId> messageIdHashSet = new HashSet<MessageId>();
HashSet<String> messageDataHashSet = new HashSet<String>();
// 1. producer connect
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
// 2. Create consumer
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName(subscriptionName).receiverQueueSize(7).subscribe();
// 3. producer publish messages
for (int i = 0; i < totalMessages; i++) {
String message = messagePredicate + i;
producer.send(message.getBytes());
}
// 4. Receive messages
Message<byte[]> message = consumer.receive();
log.info("Message received " + new String(message.getData()));
for (int i = 1; i < totalMessages; i++) {
Message<byte[]> msg = consumer.receive();
log.info("Message received " + new String(msg.getData()));
messageDataHashSet.add(new String(msg.getData()));
}
printIncomingMessageQueue(consumer);
// 5. Ack 1st message and ask for resend
consumer.acknowledge(message);
log.info("Message acked " + new String(message.getData()));
messageIdHashSet.add(message.getMessageId());
messageDataHashSet.add(new String(message.getData()));
consumer.redeliverUnacknowledgedMessages();
log.info("Resend Messages Request sent");
// 6. Check if messages resent in correct order
for (int i = 0; i < totalMessages - 1; i++) {
message = consumer.receive();
log.info("Message received " + new String(message.getData()));
if (i < 2) {
messageIdHashSet.add(message.getMessageId());
consumer.acknowledge(message);
}
log.info("Message acked " + new String(message.getData()));
assertTrue(messageDataHashSet.contains(new String(message.getData())));
}
assertEquals(messageIdHashSet.size(), 3);
assertEquals(messageDataHashSet.size(), totalMessages);
printIncomingMessageQueue(consumer);
// 7. Request resend 2nd time - you should receive 4 messages
consumer.redeliverUnacknowledgedMessages();
log.info("Resend Messages Request sent");
message = consumer.receive(2000, TimeUnit.MILLISECONDS);
while (message != null) {
log.info("Message received " + new String(message.getData()));
consumer.acknowledge(message);
log.info("Message acked " + new String(message.getData()));
messageIdHashSet.add(message.getMessageId());
messageDataHashSet.add(new String(message.getData()));
message = consumer.receive(5000, TimeUnit.MILLISECONDS);
}
assertEquals(messageIdHashSet.size(), totalMessages);
assertEquals(messageDataHashSet.size(), totalMessages);
printIncomingMessageQueue(consumer);
// 9. Calling resend after acking all messages - expectin 0 messages
consumer.redeliverUnacknowledgedMessages();
assertEquals(consumer.receive(2000, TimeUnit.MILLISECONDS), null);
// 10. Checking message contents
for (int i = 0; i < totalMessages; i++) {
assertTrue(messageDataHashSet.contains(messagePredicate + i));
}
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class ServerCnxTest method testProducerSuccessOnEncryptionRequiredTopic.
@Test(timeOut = 30000)
public void testProducerSuccessOnEncryptionRequiredTopic() throws Exception {
resetChannel();
setChannelConnected();
// Set encryption_required to true
ZooKeeperDataCache<Policies> zkDataCache = mock(ZooKeeperDataCache.class);
Policies policies = mock(Policies.class);
policies.encryption_required = true;
policies.clusterDispatchRate = Maps.newHashMap();
doReturn(Optional.of(policies)).when(zkDataCache).get(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
doReturn(CompletableFuture.completedFuture(Optional.of(policies))).when(zkDataCache).getAsync(AdminResource.path(POLICIES, TopicName.get(encryptionRequiredTopicName).getNamespace()));
doReturn(zkDataCache).when(configCacheService).policiesCache();
// test success case: encrypted producer can connect
ByteBuf clientCommand = Commands.newProducer(encryptionRequiredTopicName, 1, /* producer id */
1, /* request id */
"encrypted-producer", true, null);
channel.writeInbound(clientCommand);
Object response = getResponse();
assertEquals(response.getClass(), CommandProducerSuccess.class);
PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(encryptionRequiredTopicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
channel.finish();
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class ServerCnxTest method testNonExistentTopicSuperUserAccess.
@Test(timeOut = 30000)
public void testNonExistentTopicSuperUserAccess() throws Exception {
AuthorizationService authorizationService = spy(new AuthorizationService(svcConfig, configCacheService));
doReturn(authorizationService).when(brokerService).getAuthorizationService();
doReturn(true).when(brokerService).isAuthorizationEnabled();
Field providerField = AuthorizationService.class.getDeclaredField("provider");
providerField.setAccessible(true);
PulsarAuthorizationProvider authorizationProvider = spy(new PulsarAuthorizationProvider(svcConfig, configCacheService));
providerField.set(authorizationService, authorizationProvider);
doReturn(true).when(authorizationProvider).isSuperUser(Mockito.anyString());
// Test producer creation
resetChannel();
setChannelConnected();
ByteBuf newProducerCmd = Commands.newProducer(nonExistentTopicName, 1, /* producer id */
1, /* request id */
"prod-name", Collections.emptyMap());
channel.writeInbound(newProducerCmd);
assertTrue(getResponse() instanceof CommandProducerSuccess);
PersistentTopic topicRef = (PersistentTopic) brokerService.getTopicReference(nonExistentTopicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
channel.finish();
// Test consumer creation
resetChannel();
setChannelConnected();
ByteBuf newSubscribeCmd = //
Commands.newSubscribe(//
nonExistentTopicName, successSubName, 1, /* consumer id */
1, /* request id */
SubType.Exclusive, 0, "test");
channel.writeInbound(newSubscribeCmd);
topicRef = (PersistentTopic) brokerService.getTopicReference(nonExistentTopicName);
assertNotNull(topicRef);
assertTrue(topicRef.getSubscriptions().containsKey(successSubName));
assertTrue(topicRef.getSubscription(successSubName).getDispatcher().isConsumerConnected());
assertTrue(getResponse() instanceof CommandSuccess);
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class SubscriptionSeekTest method testSeek.
@Test
public void testSeek() throws Exception {
final String topicName = "persistent://prop/use/ns-abc/testSeek";
Producer<byte[]> producer = pulsarClient.newProducer().topic(topicName).create();
// Disable pre-fetch in consumer to track the messages received
org.apache.pulsar.client.api.Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-subscription").receiverQueueSize(0).subscribe();
PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topicName);
assertNotNull(topicRef);
assertEquals(topicRef.getProducers().size(), 1);
assertEquals(topicRef.getSubscriptions().size(), 1);
List<MessageId> messageIds = new ArrayList<>();
for (int i = 0; i < 10; i++) {
String message = "my-message-" + i;
MessageId msgId = producer.send(message.getBytes());
messageIds.add(msgId);
}
PersistentSubscription sub = topicRef.getSubscription("my-subscription");
assertEquals(sub.getNumberOfEntriesInBacklog(), 10);
consumer.seek(MessageId.latest);
assertEquals(sub.getNumberOfEntriesInBacklog(), 0);
// Wait for consumer to reconnect
Thread.sleep(500);
consumer.seek(MessageId.earliest);
assertEquals(sub.getNumberOfEntriesInBacklog(), 10);
Thread.sleep(500);
consumer.seek(messageIds.get(5));
assertEquals(sub.getNumberOfEntriesInBacklog(), 5);
}
use of org.apache.pulsar.broker.service.persistent.PersistentTopic in project incubator-pulsar by apache.
the class PersistentDispatcherFailoverConsumerTest method testMultipleDispatcherGetNextConsumerWithDifferentPriorityLevel.
@Test
public void testMultipleDispatcherGetNextConsumerWithDifferentPriorityLevel() throws Exception {
PersistentTopic topic = new PersistentTopic(successTopicName, ledgerMock, brokerService);
PersistentDispatcherMultipleConsumers dispatcher = new PersistentDispatcherMultipleConsumers(topic, cursorMock);
Consumer consumer1 = createConsumer(0, 2, false, 1);
Consumer consumer2 = createConsumer(0, 2, false, 2);
Consumer consumer3 = createConsumer(0, 2, false, 3);
Consumer consumer4 = createConsumer(1, 2, false, 4);
Consumer consumer5 = createConsumer(1, 1, false, 5);
Consumer consumer6 = createConsumer(1, 2, false, 6);
Consumer consumer7 = createConsumer(2, 1, false, 7);
Consumer consumer8 = createConsumer(2, 1, false, 8);
Consumer consumer9 = createConsumer(2, 1, false, 9);
dispatcher.addConsumer(consumer1);
dispatcher.addConsumer(consumer2);
dispatcher.addConsumer(consumer3);
dispatcher.addConsumer(consumer4);
dispatcher.addConsumer(consumer5);
dispatcher.addConsumer(consumer6);
dispatcher.addConsumer(consumer7);
dispatcher.addConsumer(consumer8);
dispatcher.addConsumer(consumer9);
Assert.assertEquals(getNextConsumer(dispatcher), consumer1);
Assert.assertEquals(getNextConsumer(dispatcher), consumer2);
Assert.assertEquals(getNextConsumer(dispatcher), consumer3);
Assert.assertEquals(getNextConsumer(dispatcher), consumer1);
Assert.assertEquals(getNextConsumer(dispatcher), consumer2);
Assert.assertEquals(getNextConsumer(dispatcher), consumer3);
Assert.assertEquals(getNextConsumer(dispatcher), consumer4);
Assert.assertEquals(getNextConsumer(dispatcher), consumer5);
Assert.assertEquals(getNextConsumer(dispatcher), consumer6);
Assert.assertEquals(getNextConsumer(dispatcher), consumer4);
Assert.assertEquals(getNextConsumer(dispatcher), consumer6);
Assert.assertEquals(getNextConsumer(dispatcher), consumer7);
Assert.assertEquals(getNextConsumer(dispatcher), consumer8);
// in between add upper priority consumer with more permits
Consumer consumer10 = createConsumer(0, 2, false, 10);
dispatcher.addConsumer(consumer10);
Assert.assertEquals(getNextConsumer(dispatcher), consumer10);
Assert.assertEquals(getNextConsumer(dispatcher), consumer10);
Assert.assertEquals(getNextConsumer(dispatcher), consumer9);
}
Aggregations