Skip to content

Commit

Permalink
[pulsar-client] Fix message corruption on OOM for batch messages (apa…
Browse files Browse the repository at this point in the history
…che#5443)

* [pulsar-client] Fix message corruption on OOM for batch messages

* remove comments

* Address comments: index in local-var + remove lastSerializedMessageIndex var
  • Loading branch information
rdhabalia authored Oct 25, 2019
1 parent 6823628 commit bf9a901
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,31 @@ public void add(MessageImpl<?> msg, SendCallback callback) {
}

private ByteBuf getCompressedBatchMetadataAndPayload() {
for (MessageImpl<?> msg : messages) {
int batchWriteIndex = batchedMessageMetadataAndPayload.writerIndex();
int batchReadIndex = batchedMessageMetadataAndPayload.readerIndex();

for (int i = 0, n = messages.size(); i < n; i++) {
MessageImpl<?> msg = messages.get(i);
PulsarApi.MessageMetadata.Builder msgBuilder = msg.getMessageBuilder();
batchedMessageMetadataAndPayload = Commands.serializeSingleMessageInBatchWithPayload(msgBuilder,
msg.getDataBuffer(), batchedMessageMetadataAndPayload);
msgBuilder.recycle();
msg.getDataBuffer().markReaderIndex();
try {
batchedMessageMetadataAndPayload = Commands.serializeSingleMessageInBatchWithPayload(msgBuilder,
msg.getDataBuffer(), batchedMessageMetadataAndPayload);
} catch (Throwable th) {
// serializing batch message can corrupt the index of message and batch-message. Reset the index so,
// next iteration doesn't send corrupt message to broker.
for (int j = 0; j <= i; j++) {
MessageImpl<?> previousMsg = messages.get(j);
previousMsg.getDataBuffer().resetReaderIndex();
}
batchedMessageMetadataAndPayload.writerIndex(batchWriteIndex);
batchedMessageMetadataAndPayload.readerIndex(batchReadIndex);
throw new RuntimeException(th);
}
}
// Recycle messages only once they serialized successfully in batch
for (MessageImpl<?> msg : messages) {
msg.getMessageBuilder().recycle();
}
int uncompressedSize = batchedMessageMetadataAndPayload.readableBytes();
ByteBuf compressedPayload = compressor.encode(batchedMessageMetadataAndPayload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,7 @@ protected void triggerFlush() {
private void batchMessageAndSend() {
if (log.isDebugEnabled()) {
log.debug("[{}] [{}] Batching the messages from the batch container with {} messages", topic, producerName,
batchMessageContainer.getNumMessagesInBatch());
batchMessageContainer.getNumMessagesInBatch());
}
if (!batchMessageContainer.isEmpty()) {
try {
Expand Down

0 comments on commit bf9a901

Please sign in to comment.