use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.
the class ServerCnx method newMessageAndIntercept.
public ByteBufPair newMessageAndIntercept(long consumerId, long ledgerId, long entryId, int partition, int redeliveryCount, ByteBuf metadataAndPayload, long[] ackSet, String topic, long epoch) {
BaseCommand command = Commands.newMessageCommand(consumerId, ledgerId, entryId, partition, redeliveryCount, ackSet, epoch);
ByteBufPair res = Commands.serializeCommandMessageWithSize(command, metadataAndPayload);
try {
val brokerInterceptor = getBrokerService().getInterceptor();
if (brokerInterceptor != null) {
brokerInterceptor.onPulsarCommand(command, this);
CompletableFuture<Consumer> consumerFuture = consumers.get(consumerId);
if (consumerFuture != null && consumerFuture.isDone() && !consumerFuture.isCompletedExceptionally()) {
Consumer consumer = consumerFuture.getNow(null);
brokerInterceptor.messageDispatched(this, consumer, ledgerId, entryId, metadataAndPayload);
}
} else {
log.debug("BrokerInterceptor is not set in newMessageAndIntercept");
}
} catch (Exception e) {
log.error("Exception occur when intercept messages.", e);
}
return res;
}
use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.
the class MessageChecksumTest method testTamperingMessageIsDetected.
@Test
public void testTamperingMessageIsDetected() throws Exception {
// GIVEN
ProducerImpl<byte[]> producer = (ProducerImpl<byte[]>) pulsarClient.newProducer().topic("persistent://prop/use/ns-abc/testTamperingMessageIsDetected").enableBatching(false).messageRoutingMode(MessageRoutingMode.SinglePartition).create();
TypedMessageBuilderImpl<byte[]> msgBuilder = (TypedMessageBuilderImpl<byte[]>) producer.newMessage().value("a message".getBytes());
MessageMetadata msgMetadata = msgBuilder.getMetadataBuilder().setProducerName("test").setSequenceId(1).setPublishTime(10L);
ByteBuf payload = Unpooled.wrappedBuffer(msgBuilder.getContent());
// WHEN
// protocol message is created with checksum
ByteBufPair cmd = Commands.newSend(1, 1, 1, ChecksumType.Crc32c, msgMetadata, payload);
OpSendMsg op = OpSendMsg.create((MessageImpl<byte[]>) msgBuilder.getMessage(), cmd, 1, null);
// THEN
// the checksum validation passes
assertTrue(producer.verifyLocalBufferIsNotCorrupted(op));
// WHEN
// the content of the message is tampered
msgBuilder.getContent().put(0, (byte) 'b');
// the checksum validation fails
assertFalse(producer.verifyLocalBufferIsNotCorrupted(op));
}
use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.
the class BatchMessageContainerImpl method createOpSendMsg.
@Override
public OpSendMsg createOpSendMsg() throws IOException {
ByteBuf encryptedPayload = producer.encryptMessage(messageMetadata, getCompressedBatchMetadataAndPayload());
if (encryptedPayload.readableBytes() > ClientCnx.getMaxMessageSize()) {
discard(new PulsarClientException.InvalidMessageException("Message size is bigger than " + ClientCnx.getMaxMessageSize() + " bytes"));
return null;
}
messageMetadata.setNumMessagesInBatch(numMessagesInBatch);
messageMetadata.setHighestSequenceId(highestSequenceId);
if (currentTxnidMostBits != -1) {
messageMetadata.setTxnidMostBits(currentTxnidMostBits);
}
if (currentTxnidLeastBits != -1) {
messageMetadata.setTxnidLeastBits(currentTxnidLeastBits);
}
ByteBufPair cmd = producer.sendMessage(producer.producerId, messageMetadata.getSequenceId(), messageMetadata.getHighestSequenceId(), numMessagesInBatch, messageMetadata, encryptedPayload);
OpSendMsg op = OpSendMsg.create(messages, cmd, messageMetadata.getSequenceId(), messageMetadata.getHighestSequenceId(), firstCallback);
op.setNumMessagesInBatch(numMessagesInBatch);
op.setBatchSizeByte(currentBatchSizeBytes);
lowestSequenceId = -1L;
return op;
}
use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.
the class BatchMessageKeyBasedContainer method createOpSendMsg.
private ProducerImpl.OpSendMsg createOpSendMsg(KeyedBatch keyedBatch) throws IOException {
ByteBuf encryptedPayload = producer.encryptMessage(keyedBatch.messageMetadata, keyedBatch.getCompressedBatchMetadataAndPayload());
if (encryptedPayload.readableBytes() > ClientCnx.getMaxMessageSize()) {
keyedBatch.discard(new PulsarClientException.InvalidMessageException("Message size is bigger than " + ClientCnx.getMaxMessageSize() + " bytes"));
return null;
}
final int numMessagesInBatch = keyedBatch.messages.size();
long currentBatchSizeBytes = 0;
for (MessageImpl<?> message : keyedBatch.messages) {
currentBatchSizeBytes += message.getDataBuffer().readableBytes();
}
keyedBatch.messageMetadata.setNumMessagesInBatch(numMessagesInBatch);
if (currentTxnidMostBits != -1) {
keyedBatch.messageMetadata.setTxnidMostBits(currentTxnidMostBits);
}
if (currentTxnidLeastBits != -1) {
keyedBatch.messageMetadata.setTxnidLeastBits(currentTxnidLeastBits);
}
ByteBufPair cmd = producer.sendMessage(producer.producerId, keyedBatch.sequenceId, numMessagesInBatch, keyedBatch.messageMetadata, encryptedPayload);
ProducerImpl.OpSendMsg op = ProducerImpl.OpSendMsg.create(keyedBatch.messages, cmd, keyedBatch.sequenceId, keyedBatch.firstCallback);
op.setNumMessagesInBatch(numMessagesInBatch);
op.setBatchSizeByte(currentBatchSizeBytes);
return op;
}
use of org.apache.pulsar.common.protocol.ByteBufPair in project pulsar by apache.
the class ProducerImpl method verifyLocalBufferIsNotCorrupted.
/**
* Computes checksum again and verifies it against existing checksum. If checksum doesn't match it means that
* message is corrupt.
*
* @param op
* @return returns true only if message is not modified and computed-checksum is same as previous checksum else
* return false that means that message is corrupted. Returns true if checksum is not present.
*/
protected boolean verifyLocalBufferIsNotCorrupted(OpSendMsg op) {
ByteBufPair msg = op.cmd;
if (msg != null) {
ByteBuf headerFrame = msg.getFirst();
headerFrame.markReaderIndex();
try {
// skip bytes up to checksum index
// skip [total-size]
headerFrame.skipBytes(4);
int cmdSize = (int) headerFrame.readUnsignedInt();
headerFrame.skipBytes(cmdSize);
// verify if checksum present
if (hasChecksum(headerFrame)) {
int checksum = readChecksum(headerFrame);
// msg.readerIndex is already at header-payload index, Recompute checksum for headers-payload
int metadataChecksum = computeChecksum(headerFrame);
long computedChecksum = resumeChecksum(metadataChecksum, msg.getSecond());
return checksum == computedChecksum;
} else {
log.warn("[{}] [{}] checksum is not present into message with id {}", topic, producerName, op.sequenceId);
}
} finally {
headerFrame.resetReaderIndex();
}
return true;
} else {
log.warn("[{}] Failed while casting empty ByteBufPair, ", producerName);
return false;
}
}
Aggregations