Search in sources :

Example 11 with RawReader

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

the class RawReaderTest method testSeekToStart.

@Test
public void testSeekToStart() 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();
    MessageId lastMessageId = reader.getLastMessageIdAsync().get();
    while (true) {
        try (RawMessage m = reader.readNextAsync().get()) {
            readKeys.add(extractKey(m));
            if (lastMessageId.compareTo(m.getMessageId()) == 0) {
                break;
            }
        }
    }
    Assert.assertEquals(readKeys.size(), numKeys);
    // seek to start, read all keys again,
    // assert that we read all keys we had read previously
    reader.seekAsync(MessageId.earliest).get();
    while (true) {
        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 12 with RawReader

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

the class TwoPhaseCompactor method phaseTwoSeekThenLoop.

private CompletableFuture<Long> phaseTwoSeekThenLoop(RawReader reader, MessageId from, MessageId to, Map<String, MessageId> latestForKey, BookKeeper bk, LedgerHandle ledger) {
    CompletableFuture<Long> promise = new CompletableFuture<>();
    reader.seekAsync(from).thenCompose((v) -> {
        Semaphore outstanding = new Semaphore(MAX_OUTSTANDING);
        CompletableFuture<Void> loopPromise = new CompletableFuture<Void>();
        phaseTwoLoop(reader, to, latestForKey, ledger, outstanding, loopPromise);
        return loopPromise;
    }).thenCompose((v) -> closeLedger(ledger)).thenCompose((v) -> reader.acknowledgeCumulativeAsync(to, ImmutableMap.of(COMPACTED_TOPIC_LEDGER_PROPERTY, ledger.getId()))).whenComplete((res, exception) -> {
        if (exception != null) {
            deleteLedger(bk, ledger).whenComplete((res2, exception2) -> {
                if (exception2 != null) {
                    log.warn("Cleanup of ledger {} for failed", ledger, exception2);
                }
                // complete with original exception
                promise.completeExceptionally(exception);
            });
        } else {
            promise.complete(ledger.getId());
        }
    });
    return promise;
}
Also used : RawBatchConverter(org.apache.pulsar.client.impl.RawBatchConverter) RawMessage(org.apache.pulsar.client.api.RawMessage) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RawReader(org.apache.pulsar.client.api.RawReader) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ByteBuf(io.netty.buffer.ByteBuf) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MessageMetadata(org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata) PulsarClient(org.apache.pulsar.client.api.PulsarClient) LedgerHandle(org.apache.bookkeeper.client.LedgerHandle) Commands(org.apache.pulsar.common.api.Commands) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Semaphore(java.util.concurrent.Semaphore) ServiceConfiguration(org.apache.pulsar.broker.ServiceConfiguration) IOException(java.io.IOException) BookKeeper(org.apache.bookkeeper.client.BookKeeper) BKException(org.apache.bookkeeper.client.BKException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MessageId(org.apache.pulsar.client.api.MessageId) Optional(java.util.Optional) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) Semaphore(java.util.concurrent.Semaphore)

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