Search in sources :

Example 6 with PositionImpl

use of org.apache.bookkeeper.mledger.impl.PositionImpl in project pulsar by yahoo.

the class PositionTest method comparisons.

@Test
public void comparisons() {
    PositionImpl pos1_1 = new PositionImpl(1, 1);
    PositionImpl pos2_5 = new PositionImpl(2, 5);
    PositionImpl pos10_0 = new PositionImpl(10, 0);
    PositionImpl pos10_1 = new PositionImpl(10, 1);
    assertEquals(0, pos1_1.compareTo(pos1_1));
    assertEquals(-1, pos1_1.compareTo(pos2_5));
    assertEquals(-1, pos1_1.compareTo(pos10_0));
    assertEquals(-1, pos1_1.compareTo(pos10_1));
    assertEquals(+1, pos2_5.compareTo(pos1_1));
    assertEquals(0, pos2_5.compareTo(pos2_5));
    assertEquals(-1, pos2_5.compareTo(pos10_0));
    assertEquals(-1, pos2_5.compareTo(pos10_1));
    assertEquals(+1, pos10_0.compareTo(pos1_1));
    assertEquals(+1, pos10_0.compareTo(pos2_5));
    assertEquals(0, pos10_0.compareTo(pos10_0));
    assertEquals(-1, pos10_0.compareTo(pos10_1));
    assertEquals(+1, pos10_1.compareTo(pos1_1));
    assertEquals(+1, pos10_1.compareTo(pos2_5));
    assertEquals(+1, pos10_1.compareTo(pos10_0));
    assertEquals(0, pos10_1.compareTo(pos10_1));
}
Also used : PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Test(org.testng.annotations.Test)

Example 7 with PositionImpl

use of org.apache.bookkeeper.mledger.impl.PositionImpl in project pulsar by yahoo.

the class PositionTest method simpleTest.

@Test
public void simpleTest() {
    PositionImpl pos = new PositionImpl(1, 2);
    assertEquals(pos.getLedgerId(), 1);
    assertEquals(pos.getEntryId(), 2);
    assertEquals(pos, new PositionImpl(1, 2));
    assertFalse(pos.equals(new PositionImpl(1, 3)));
    assertFalse(pos.equals(new PositionImpl(3, 2)));
    assertFalse(pos.equals("1:2"));
}
Also used : PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) Test(org.testng.annotations.Test)

Example 8 with PositionImpl

use of org.apache.bookkeeper.mledger.impl.PositionImpl in project pulsar by yahoo.

the class Consumer method sendMessages.

/**
     * Dispatch a list of entries to the consumer.
     *
     * @return a promise that can be use to track when all the data has been written into the socket
     */
public Pair<ChannelPromise, Integer> sendMessages(final List<Entry> entries) {
    final ChannelHandlerContext ctx = cnx.ctx();
    final MutablePair<ChannelPromise, Integer> sentMessages = new MutablePair<ChannelPromise, Integer>();
    final ChannelPromise writePromise = ctx.newPromise();
    sentMessages.setLeft(writePromise);
    if (entries.isEmpty()) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] List of messages is empty, triggering write future immediately for consumerId {}", subscription, consumerId);
        }
        writePromise.setSuccess();
        sentMessages.setRight(0);
        return sentMessages;
    }
    try {
        sentMessages.setRight(updatePermitsAndPendingAcks(entries));
    } catch (PulsarServerException pe) {
        log.warn("[{}] [{}] consumer doesn't support batch-message {}", subscription, consumerId, cnx.getRemoteEndpointProtocolVersion());
        subscription.markTopicWithBatchMessagePublished();
        sentMessages.setRight(0);
        // disconnect consumer: it will update dispatcher's availablePermits and resend pendingAck-messages of this
        // consumer to other consumer
        disconnect();
        return sentMessages;
    }
    ctx.channel().eventLoop().execute(() -> {
        for (int i = 0; i < entries.size(); i++) {
            Entry entry = entries.get(i);
            PositionImpl pos = (PositionImpl) entry.getPosition();
            MessageIdData.Builder messageIdBuilder = MessageIdData.newBuilder();
            MessageIdData messageId = messageIdBuilder.setLedgerId(pos.getLedgerId()).setEntryId(pos.getEntryId()).build();
            ByteBuf metadataAndPayload = entry.getDataBuffer();
            // skip checksum by incrementing reader-index if consumer-client doesn't support checksum verification
            if (cnx.getRemoteEndpointProtocolVersion() < ProtocolVersion.v6.getNumber()) {
                readChecksum(metadataAndPayload);
            }
            // stats
            msgOut.recordEvent(metadataAndPayload.readableBytes());
            if (log.isDebugEnabled()) {
                log.debug("[{}] Sending message to consumerId {}, entry id {}", subscription, consumerId, pos.getEntryId());
            }
            // We only want to pass the "real" promise on the last entry written
            ChannelPromise promise = ctx.voidPromise();
            if (i == (entries.size() - 1)) {
                promise = writePromise;
            }
            ctx.write(Commands.newMessage(consumerId, messageId, metadataAndPayload), promise);
            messageId.recycle();
            messageIdBuilder.recycle();
        }
        ctx.flush();
    });
    return sentMessages;
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Entry(org.apache.bookkeeper.mledger.Entry) MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf)

Example 9 with PositionImpl

use of org.apache.bookkeeper.mledger.impl.PositionImpl in project pulsar by yahoo.

the class Consumer method redeliverUnacknowledgedMessages.

public void redeliverUnacknowledgedMessages(List<MessageIdData> messageIds) {
    int totalRedeliveryMessages = 0;
    List<PositionImpl> pendingPositions = Lists.newArrayList();
    for (MessageIdData msg : messageIds) {
        PositionImpl position = PositionImpl.get(msg.getLedgerId(), msg.getEntryId());
        Integer batchSize = pendingAcks.remove(position);
        if (batchSize != null) {
            totalRedeliveryMessages += batchSize;
            pendingPositions.add(position);
        }
    }
    UNACKED_MESSAGES_UPDATER.addAndGet(this, -totalRedeliveryMessages);
    blockedConsumerOnUnackedMsgs = false;
    subscription.redeliverUnacknowledgedMessages(this, pendingPositions);
    msgRedeliver.recordMultipleEvents(totalRedeliveryMessages, totalRedeliveryMessages);
    int numberOfBlockedPermits = Math.min(totalRedeliveryMessages, PERMITS_RECEIVED_WHILE_CONSUMER_BLOCKED_UPDATER.get(this));
    // if permitsReceivedWhileConsumerBlocked has been accumulated then pass it to Dispatcher to flow messages
    if (numberOfBlockedPermits > 0) {
        PERMITS_RECEIVED_WHILE_CONSUMER_BLOCKED_UPDATER.getAndAdd(this, -numberOfBlockedPermits);
        MESSAGE_PERMITS_UPDATER.getAndAdd(this, numberOfBlockedPermits);
        subscription.consumerFlow(this, numberOfBlockedPermits);
    }
}
Also used : MessageIdData(com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl)

Example 10 with PositionImpl

use of org.apache.bookkeeper.mledger.impl.PositionImpl in project pulsar by yahoo.

the class Consumer method updatePermitsAndPendingAcks.

int updatePermitsAndPendingAcks(final List<Entry> entries) throws PulsarServerException {
    int permitsToReduce = 0;
    Iterator<Entry> iter = entries.iterator();
    boolean unsupportedVersion = false;
    boolean clientSupportBatchMessages = cnx.isBatchMessageCompatibleVersion();
    while (iter.hasNext()) {
        Entry entry = iter.next();
        ByteBuf metadataAndPayload = entry.getDataBuffer();
        int batchSize = getBatchSizeforEntry(metadataAndPayload);
        if (batchSize == -1) {
            // this would suggest that the message might have been corrupted
            iter.remove();
            entry.release();
            PositionImpl pos = PositionImpl.get((PositionImpl) entry.getPosition());
            subscription.acknowledgeMessage(pos, AckType.Individual);
            continue;
        }
        if (pendingAcks != null) {
            PositionImpl pos = PositionImpl.get((PositionImpl) entry.getPosition());
            pendingAcks.put(pos, batchSize);
        }
        // check if consumer supports batch message
        if (batchSize > 1 && !clientSupportBatchMessages) {
            unsupportedVersion = true;
        }
        permitsToReduce += batchSize;
    }
    // reduce permit and increment unackedMsg count with total number of messages in batch-msgs
    int permits = MESSAGE_PERMITS_UPDATER.addAndGet(this, -permitsToReduce);
    incrementUnackedMessages(permitsToReduce);
    if (unsupportedVersion) {
        throw new PulsarServerException("Consumer does not support batch-message");
    }
    if (permits < 0) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] [{}] message permits dropped below 0 - {}", subscription, consumerId, permits);
        }
    }
    return permitsToReduce;
}
Also used : PulsarServerException(com.yahoo.pulsar.broker.PulsarServerException) Entry(org.apache.bookkeeper.mledger.Entry) PositionImpl(org.apache.bookkeeper.mledger.impl.PositionImpl) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

PositionImpl (org.apache.bookkeeper.mledger.impl.PositionImpl)14 ByteBuf (io.netty.buffer.ByteBuf)6 Test (org.testng.annotations.Test)5 MessageIdData (com.yahoo.pulsar.common.api.proto.PulsarApi.MessageIdData)3 PulsarServerException (com.yahoo.pulsar.broker.PulsarServerException)2 PersistentSubscription (com.yahoo.pulsar.broker.service.persistent.PersistentSubscription)2 PersistentTopic (com.yahoo.pulsar.broker.service.persistent.PersistentTopic)2 AddEntryCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.AddEntryCallback)2 CloseCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.CloseCallback)2 DeleteCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.DeleteCursorCallback)2 OpenCursorCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenCursorCallback)2 OpenLedgerCallback (org.apache.bookkeeper.mledger.AsyncCallbacks.OpenLedgerCallback)2 Entry (org.apache.bookkeeper.mledger.Entry)2 ManagedLedgerConfig (org.apache.bookkeeper.mledger.ManagedLedgerConfig)2 ManagedLedgerException (org.apache.bookkeeper.mledger.ManagedLedgerException)2 Matchers.anyObject (org.mockito.Matchers.anyObject)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 NotAllowedException (com.yahoo.pulsar.broker.service.BrokerServiceException.NotAllowedException)1 SubscriptionBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.SubscriptionBusyException)1 TopicBusyException (com.yahoo.pulsar.broker.service.BrokerServiceException.TopicBusyException)1