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));
}
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"));
}
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;
}
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);
}
}
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;
}
Aggregations