Search in sources :

Example 6 with RawReader

use of org.apache.pulsar.client.api.RawReader in project incubator-pulsar by apache.

the class ReplicatorTest method verifyChecksumAfterReplication.

@Test(timeOut = 30000)
public void verifyChecksumAfterReplication() throws Exception {
    final String topicName = "persistent://pulsar/global/ns/checksumAfterReplication";
    PulsarClient c1 = PulsarClient.builder().serviceUrl(url1.toString()).build();
    Producer<byte[]> p1 = c1.newProducer().topic(topicName).create();
    PulsarClient c2 = PulsarClient.builder().serviceUrl(url2.toString()).build();
    RawReader reader2 = RawReader.create(c2, topicName, "sub").get();
    p1.send("Hello".getBytes());
    RawMessage msg = reader2.readNextAsync().get();
    ByteBuf b = msg.getHeadersAndPayload();
    assertTrue(Commands.hasChecksum(b));
    int parsedChecksum = Commands.readChecksum(b);
    int computedChecksum = Crc32cIntChecksum.computeChecksum(b);
    assertEquals(parsedChecksum, computedChecksum);
    p1.close();
    reader2.closeAsync().get();
}
Also used : RawReader(org.apache.pulsar.client.api.RawReader) PulsarClient(org.apache.pulsar.client.api.PulsarClient) RawMessage(org.apache.pulsar.client.api.RawMessage) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 7 with RawReader

use of org.apache.pulsar.client.api.RawReader in project incubator-pulsar by apache.

the class RawReaderTest method testSeekToMiddle.

@Test
public void testSeekToMiddle() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    publishMessages(topic, numKeys);
    Set<String> readKeys = new HashSet<>();
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    int i = 0;
    MessageId seekTo = null;
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            i++;
            if (i > numKeys / 2) {
                if (seekTo == null) {
                    seekTo = m.getMessageId();
                }
                readKeys.add(extractKey(m));
            }
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertEquals(readKeys.size(), numKeys / 2);
    // seek to middle, read all keys again,
    // assert that we read all keys we had read previously
    reader.seekAsync(seekTo).get();
    while (true) {
        // should break out with TimeoutException
        try (RawMessage m = reader.readNextAsync().get()) {
            Assert.assertTrue(readKeys.remove(extractKey(m)));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertTrue(readKeys.isEmpty());
}
Also used : RawReader(org.apache.pulsar.client.api.RawReader) RawMessage(org.apache.pulsar.client.api.RawMessage) HashSet(java.util.HashSet) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 8 with RawReader

use of org.apache.pulsar.client.api.RawReader in project incubator-pulsar by apache.

the class RawReaderTest method testAcknowledgeWithProperties.

@Test
public void testAcknowledgeWithProperties() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    Set<String> keys = publishMessages(topic, numKeys);
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            Assert.assertTrue(keys.remove(extractKey(m)));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertTrue(keys.isEmpty());
    Map<String, Long> properties = new HashMap<>();
    properties.put("foobar", 0xdeadbeefdecaL);
    reader.acknowledgeCumulativeAsync(lastMessageId, properties).get();
    PersistentTopic topicRef = (PersistentTopic) pulsar.getBrokerService().getTopicReference(topic);
    ManagedLedger ledger = topicRef.getManagedLedger();
    for (int i = 0; i < 30; i++) {
        if (ledger.openCursor(subscription).getProperties().get("foobar") == Long.valueOf(0xdeadbeefdecaL)) {
            break;
        }
        Thread.sleep(100);
    }
    Assert.assertEquals(ledger.openCursor(subscription).getProperties().get("foobar"), Long.valueOf(0xdeadbeefdecaL));
}
Also used : HashMap(java.util.HashMap) RawReader(org.apache.pulsar.client.api.RawReader) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) RawMessage(org.apache.pulsar.client.api.RawMessage) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 9 with RawReader

use of org.apache.pulsar.client.api.RawReader in project incubator-pulsar by apache.

the class RawReaderTest method testRawReader.

@Test
public void testRawReader() throws Exception {
    int numKeys = 10;
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    Set<String> keys = publishMessages(topic, numKeys);
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            Assert.assertTrue(keys.remove(extractKey(m)));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertTrue(keys.isEmpty());
}
Also used : RawReader(org.apache.pulsar.client.api.RawReader) RawMessage(org.apache.pulsar.client.api.RawMessage) MessageId(org.apache.pulsar.client.api.MessageId) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Example 10 with RawReader

use of org.apache.pulsar.client.api.RawReader in project incubator-pulsar by apache.

the class RawReaderTest method testBatchingRebatch.

@Test
public void testBatchingRebatch() throws Exception {
    String topic = "persistent://my-property/use/my-ns/my-raw-topic";
    try (Producer producer = pulsarClient.newProducer().topic(topic).maxPendingMessages(3).enableBatching(true).batchingMaxMessages(3).batchingMaxPublishDelay(1, TimeUnit.HOURS).create()) {
        producer.sendAsync(MessageBuilder.create().setKey("key1").setContent("my-content-1".getBytes()).build());
        producer.sendAsync(MessageBuilder.create().setKey("key2").setContent("my-content-2".getBytes()).build());
        producer.sendAsync(MessageBuilder.create().setKey("key3").setContent("my-content-3".getBytes()).build()).get();
    }
    RawReader reader = RawReader.create(pulsarClient, topic, subscription).get();
    try {
        RawMessage m1 = reader.readNextAsync().get();
        RawMessage m2 = RawBatchConverter.rebatchMessage(m1, (key, id) -> key.equals("key2")).get();
        List<ImmutablePair<MessageId, String>> idsAndKeys = RawBatchConverter.extractIdsAndKeys(m2);
        Assert.assertEquals(idsAndKeys.size(), 1);
        Assert.assertEquals(idsAndKeys.get(0).getRight(), "key2");
        m2.close();
    } finally {
        reader.closeAsync().get();
    }
}
Also used : RawMessage(org.apache.pulsar.client.api.RawMessage) Producer(org.apache.pulsar.client.api.Producer) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) RawReader(org.apache.pulsar.client.api.RawReader) Test(org.testng.annotations.Test) ClusterData(org.apache.pulsar.common.policies.data.ClusterData) AfterMethod(org.testng.annotations.AfterMethod) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Future(java.util.concurrent.Future) Lists(com.google.common.collect.Lists) Assert(org.testng.Assert) ByteBuf(io.netty.buffer.ByteBuf) ManagedLedger(org.apache.bookkeeper.mledger.ManagedLedger) Map(java.util.Map) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) Commands(org.apache.pulsar.common.api.Commands) PropertyAdmin(org.apache.pulsar.common.policies.data.PropertyAdmin) CancellationException(java.util.concurrent.CancellationException) MessageBuilder(org.apache.pulsar.client.api.MessageBuilder) BeforeMethod(org.testng.annotations.BeforeMethod) Set(java.util.Set) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Sets(com.google.common.collect.Sets) TimeUnit(java.util.concurrent.TimeUnit) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest) List(java.util.List) PersistentTopic(org.apache.pulsar.broker.service.persistent.PersistentTopic) MessageId(org.apache.pulsar.client.api.MessageId) Producer(org.apache.pulsar.client.api.Producer) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) RawReader(org.apache.pulsar.client.api.RawReader) RawMessage(org.apache.pulsar.client.api.RawMessage) Test(org.testng.annotations.Test) MockedPulsarServiceBaseTest(org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)

Aggregations

RawReader (org.apache.pulsar.client.api.RawReader)12 RawMessage (org.apache.pulsar.client.api.RawMessage)11 Test (org.testng.annotations.Test)10 MockedPulsarServiceBaseTest (org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest)8 MessageId (org.apache.pulsar.client.api.MessageId)7 ByteBuf (io.netty.buffer.ByteBuf)5 ArrayList (java.util.ArrayList)5 Future (java.util.concurrent.Future)5 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 TimeoutException (java.util.concurrent.TimeoutException)4 List (java.util.List)3 Map (java.util.Map)3 TimeUnit (java.util.concurrent.TimeUnit)3 PulsarClient (org.apache.pulsar.client.api.PulsarClient)3 Commands (org.apache.pulsar.common.api.Commands)3 MessageMetadata (org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2