use of org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata in project incubator-pulsar by apache.
the class MessageImpl method deserialize.
public static MessageImpl<byte[]> deserialize(ByteBuf headersAndPayload) throws IOException {
@SuppressWarnings("unchecked") MessageImpl<byte[]> msg = (MessageImpl<byte[]>) RECYCLER.get();
MessageMetadata msgMetadata = Commands.parseMessageMetadata(headersAndPayload);
msg.msgMetadataBuilder = MessageMetadata.newBuilder(msgMetadata);
msgMetadata.recycle();
msg.payload = headersAndPayload;
msg.messageId = null;
msg.cnx = null;
msg.properties = Collections.emptyMap();
return msg;
}
use of org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata in project incubator-pulsar by apache.
the class Commands method parseMessageMetadata.
public static MessageMetadata parseMessageMetadata(ByteBuf buffer) {
try {
// initially reader-index may point to start_of_checksum : increment reader-index to start_of_metadata to parse
// metadata
skipChecksumIfPresent(buffer);
int metadataSize = (int) buffer.readUnsignedInt();
int writerIndex = buffer.writerIndex();
buffer.writerIndex(buffer.readerIndex() + metadataSize);
ByteBufCodedInputStream stream = ByteBufCodedInputStream.get(buffer);
MessageMetadata.Builder messageMetadataBuilder = MessageMetadata.newBuilder();
MessageMetadata res = messageMetadataBuilder.mergeFrom(stream, null).build();
buffer.writerIndex(writerIndex);
messageMetadataBuilder.recycle();
stream.recycle();
return res;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata in project incubator-pulsar by apache.
the class CommandsTest method testChecksumSendCommand.
@Test
public void testChecksumSendCommand() throws Exception {
// test checksum in send command
String producerName = "prod-name";
int sequenceId = 0;
ByteBuf data = Unpooled.buffer(1024);
MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()).setProducerName(producerName).setSequenceId(sequenceId).build();
int expectedChecksum = computeChecksum(messageMetadata, data);
ByteBufPair clientCommand = Commands.newSend(1, 0, 1, ChecksumType.Crc32c, messageMetadata, data);
clientCommand.retain();
ByteBuf receivedBuf = ByteBufPair.coalesce(clientCommand);
// skip [total-size]
receivedBuf.skipBytes(4);
int cmdSize = (int) receivedBuf.readUnsignedInt();
receivedBuf.readerIndex(8 + cmdSize);
int startMessagePos = receivedBuf.readerIndex();
/**
* 1. verify checksum and metadataParsing **
*/
boolean hasChecksum = Commands.hasChecksum(receivedBuf);
int checksum = Commands.readChecksum(receivedBuf);
// verify checksum is present
assertTrue(hasChecksum);
// verify checksum value
assertEquals(expectedChecksum, checksum);
MessageMetadata metadata = Commands.parseMessageMetadata(receivedBuf);
// verify metadata parsing
assertEquals(metadata.getProducerName(), producerName);
/**
* 2. parseMessageMetadata should skip checksum if present *
*/
receivedBuf.readerIndex(startMessagePos);
metadata = Commands.parseMessageMetadata(receivedBuf);
// verify metadata parsing
assertEquals(metadata.getProducerName(), producerName);
}
use of org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata in project incubator-pulsar by apache.
the class ManagedLedgerImpl method checkBackloggedCursors.
@Override
public void checkBackloggedCursors() {
// activate caught up cursors
cursors.forEach(cursor -> {
if (cursor.getNumberOfEntries() < maxActiveCursorBacklogEntries) {
cursor.setActive();
}
});
// deactivate backlog cursors
Iterator<ManagedCursor> cursors = activeCursors.iterator();
while (cursors.hasNext()) {
ManagedCursor cursor = cursors.next();
long backlogEntries = cursor.getNumberOfEntries();
if (backlogEntries > maxActiveCursorBacklogEntries) {
PositionImpl readPosition = (PositionImpl) cursor.getReadPosition();
readPosition = isValidPosition(readPosition) ? readPosition : getNextValidPosition(readPosition);
if (readPosition == null) {
if (log.isDebugEnabled()) {
log.debug("[{}] Couldn't find valid read position [{}] {}", name, cursor.getName(), cursor.getReadPosition());
}
continue;
}
try {
asyncReadEntry(readPosition, new ReadEntryCallback() {
@Override
public void readEntryFailed(ManagedLedgerException e, Object ctx) {
log.warn("[{}] Failed while reading entries on [{}] {}", name, cursor.getName(), e.getMessage(), e);
}
@Override
public void readEntryComplete(Entry entry, Object ctx) {
MessageMetadata msgMetadata = null;
try {
msgMetadata = Commands.parseMessageMetadata(entry.getDataBuffer());
long msgTimeSincePublish = (System.currentTimeMillis() - msgMetadata.getPublishTime());
if (msgTimeSincePublish > maxMessageCacheRetentionTimeMillis) {
cursor.setInactive();
}
} finally {
if (msgMetadata != null) {
msgMetadata.recycle();
}
entry.release();
}
}
}, null);
} catch (Exception e) {
log.warn("[{}] Failed while reading entries from cache on [{}] {}", name, cursor.getName(), e.getMessage(), e);
}
}
}
}
use of org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata in project incubator-pulsar by apache.
the class ServerCnxTest method testSendCommand.
@Test(timeOut = 30000)
public void testSendCommand() throws Exception {
resetChannel();
setChannelConnected();
ByteBuf clientCommand = Commands.newProducer(successTopicName, 1, /* producer id */
1, /* request id */
"prod-name", Collections.emptyMap());
channel.writeInbound(clientCommand);
assertTrue(getResponse() instanceof CommandProducerSuccess);
// test SEND success
MessageMetadata messageMetadata = MessageMetadata.newBuilder().setPublishTime(System.currentTimeMillis()).setProducerName("prod-name").setSequenceId(0).build();
ByteBuf data = Unpooled.buffer(1024);
clientCommand = ByteBufPair.coalesce(Commands.newSend(1, 0, 1, ChecksumType.None, messageMetadata, data));
channel.writeInbound(Unpooled.copiedBuffer(clientCommand));
clientCommand.release();
assertTrue(getResponse() instanceof CommandSendReceipt);
channel.finish();
}
Aggregations