use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class AdminApiTest method persistentTopicsCursorReset.
@Test(dataProvider = "topicName")
public void persistentTopicsCursorReset(String topicName) throws Exception {
admin.namespaces().setRetention("prop-xyz/use/ns1", new RetentionPolicies(10, 10));
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
topicName = "persistent://prop-xyz/use/ns1/" + topicName;
// create consumer and subscription
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList("my-sub"));
publishMessagesOnPersistentTopic(topicName, 5, 0);
// Allow at least 1ms for messages to have different timestamps
Thread.sleep(1);
long messageTimestamp = System.currentTimeMillis();
publishMessagesOnPersistentTopic(topicName, 5, 5);
List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(topicName, "my-sub", 10);
assertEquals(messages.size(), 10);
for (int i = 0; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
}
// messages should still be available due to retention
admin.persistentTopics().resetCursor(topicName, "my-sub", messageTimestamp);
int receivedAfterReset = 0;
for (int i = 4; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
++receivedAfterReset;
String expected = "message-" + i;
assertEquals(message.getData(), expected.getBytes());
}
assertEquals(receivedAfterReset, 6);
consumer.close();
admin.persistentTopics().deleteSubscription(topicName, "my-sub");
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList());
admin.persistentTopics().delete(topicName);
}
use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class AdminApiTest method persistentTopicsCursorResetAfterReset.
@Test(dataProvider = "topicName")
public void persistentTopicsCursorResetAfterReset(String topicName) throws Exception {
admin.namespaces().setRetention("prop-xyz/use/ns1", new RetentionPolicies(10, 10));
assertEquals(admin.persistentTopics().getList("prop-xyz/use/ns1"), Lists.newArrayList());
topicName = "persistent://prop-xyz/use/ns1/" + topicName;
// create consumer and subscription
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Exclusive).subscribe();
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList("my-sub"));
publishMessagesOnPersistentTopic(topicName, 5, 0);
// Allow at least 1ms for messages to have different timestamps
Thread.sleep(1);
long firstTimestamp = System.currentTimeMillis();
publishMessagesOnPersistentTopic(topicName, 3, 5);
Thread.sleep(1);
long secondTimestamp = System.currentTimeMillis();
publishMessagesOnPersistentTopic(topicName, 2, 8);
List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(topicName, "my-sub", 10);
assertEquals(messages.size(), 10);
messages.forEach(message -> {
LOG.info("Peeked message: {}", new String(message.getData()));
});
for (int i = 0; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
}
admin.persistentTopics().resetCursor(topicName, "my-sub", firstTimestamp);
int receivedAfterReset = 0;
// Should received messages from 4-9
for (int i = 4; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
++receivedAfterReset;
String expected = "message-" + i;
assertEquals(new String(message.getData()), expected);
}
assertEquals(receivedAfterReset, 6);
// Reset at 2nd timestamp
receivedAfterReset = 0;
admin.persistentTopics().resetCursor(topicName, "my-sub", secondTimestamp);
// Should received messages from 7-9
for (int i = 7; i < 10; i++) {
Message<byte[]> message = consumer.receive();
consumer.acknowledge(message);
++receivedAfterReset;
String expected = "message-" + i;
assertEquals(new String(message.getData()), expected);
}
assertEquals(receivedAfterReset, 3);
consumer.close();
admin.persistentTopics().deleteSubscription(topicName, "my-sub");
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList());
admin.persistentTopics().delete(topicName);
}
use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class AdminApiTest2 method testResetCursorOnPosition.
/**
* Verifies reset-cursor at specific position using admin-api.
*
* <pre>
* 1. Publish 50 messages
* 2. Consume 20 messages
* 3. reset cursor position on 10th message
* 4. consume 40 messages from reset position
* </pre>
*
* @param namespaceName
* @throws Exception
*/
@Test(dataProvider = "namespaceNames", timeOut = 10000)
public void testResetCursorOnPosition(String namespaceName) throws Exception {
final String topicName = "persistent://prop-xyz/use/" + namespaceName + "/resetPosition";
final int totalProducedMessages = 50;
// set retention
admin.namespaces().setRetention("prop-xyz/use/ns1", new RetentionPolicies(10, 10));
// create consumer and subscription
Consumer<byte[]> consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Shared).subscribe();
assertEquals(admin.persistentTopics().getSubscriptions(topicName), Lists.newArrayList("my-sub"));
publishMessagesOnPersistentTopic(topicName, totalProducedMessages, 0);
List<Message<byte[]>> messages = admin.persistentTopics().peekMessages(topicName, "my-sub", 10);
assertEquals(messages.size(), 10);
Message<byte[]> message = null;
MessageIdImpl resetMessageId = null;
int resetPositionId = 10;
for (int i = 0; i < 20; i++) {
message = consumer.receive(1, TimeUnit.SECONDS);
consumer.acknowledge(message);
if (i == resetPositionId) {
resetMessageId = (MessageIdImpl) message.getMessageId();
}
}
// close consumer which will clean up intenral-receive-queue
consumer.close();
// messages should still be available due to retention
MessageIdImpl messageId = new MessageIdImpl(resetMessageId.getLedgerId(), resetMessageId.getEntryId(), -1);
// reset position at resetMessageId
admin.persistentTopics().resetCursor(topicName, "my-sub", messageId);
consumer = pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscriptionType(SubscriptionType.Shared).subscribe();
MessageIdImpl msgId2 = (MessageIdImpl) consumer.receive(1, TimeUnit.SECONDS).getMessageId();
assertEquals(resetMessageId, msgId2);
// start with 1 because we have already received 1 msg
int receivedAfterReset = 1;
for (int i = 0; i < totalProducedMessages; i++) {
message = consumer.receive(500, TimeUnit.MILLISECONDS);
if (message == null) {
break;
}
consumer.acknowledge(message);
++receivedAfterReset;
}
assertEquals(receivedAfterReset, totalProducedMessages - resetPositionId);
// invalid topic name
try {
admin.persistentTopics().resetCursor(topicName + "invalid", "my-sub", messageId);
fail("It should have failed due to invalid topic name");
} catch (PulsarAdminException.NotFoundException e) {
// Ok
}
// invalid cursor name
try {
admin.persistentTopics().resetCursor(topicName, "invalid-sub", messageId);
fail("It should have failed due to invalid subscription name");
} catch (PulsarAdminException.NotFoundException e) {
// Ok
}
// invalid position
try {
messageId = new MessageIdImpl(0, 0, -1);
admin.persistentTopics().resetCursor(topicName, "my-sub", messageId);
fail("It should have failed due to invalid subscription name");
} catch (PulsarAdminException.PreconditionFailedException e) {
// Ok
}
consumer.close();
}
use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class FunctionResultRouterTest method testChoosePartitionWithKeySequenceId.
@Test
public void testChoosePartitionWithKeySequenceId() {
String key1 = "key1";
String key2 = "key2";
Message msg1 = mock(Message.class);
when(msg1.hasKey()).thenReturn(true);
when(msg1.getKey()).thenReturn(key1);
// make sure sequence id is different from hashcode, so the test can be tested correctly.
when(msg1.getSequenceId()).thenReturn((long) ((key1.hashCode() % 100) + 1));
Message msg2 = mock(Message.class);
when(msg2.hasKey()).thenReturn(true);
when(msg2.getKey()).thenReturn(key2);
when(msg1.getSequenceId()).thenReturn((long) ((key2.hashCode() % 100) + 1));
FunctionResultRouter router = new FunctionResultRouter(0);
TopicMetadata metadata = mock(TopicMetadata.class);
when(metadata.numPartitions()).thenReturn(100);
assertEquals(hash.makeHash(key1) % 100, router.choosePartition(msg1, metadata));
assertEquals(hash.makeHash(key2) % 100, router.choosePartition(msg2, metadata));
}
use of org.apache.pulsar.client.api.Message in project incubator-pulsar by apache.
the class FunctionResultRouterTest method testChoosePartitionWithKeyWithoutSequenceId.
@Test
public void testChoosePartitionWithKeyWithoutSequenceId() {
String key1 = "key1";
String key2 = "key2";
Message msg1 = mock(Message.class);
when(msg1.hasKey()).thenReturn(true);
when(msg1.getKey()).thenReturn(key1);
when(msg1.getSequenceId()).thenReturn(-1L);
Message msg2 = mock(Message.class);
when(msg2.hasKey()).thenReturn(true);
when(msg2.getKey()).thenReturn(key2);
when(msg1.getSequenceId()).thenReturn(-1L);
FunctionResultRouter router = new FunctionResultRouter(0);
TopicMetadata metadata = mock(TopicMetadata.class);
when(metadata.numPartitions()).thenReturn(100);
assertEquals(hash.makeHash(key1) % 100, router.choosePartition(msg1, metadata));
assertEquals(hash.makeHash(key2) % 100, router.choosePartition(msg2, metadata));
}
Aggregations