-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix][client] Fixed stuck issues caused by an exception encountered in the receiveIndividualMessagesFromBatch() #25386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1786,6 +1786,7 @@ void receiveIndividualMessagesFromBatch(BrokerEntryMetadata brokerEntryMetadata, | |
|
|
||
| SingleMessageMetadata singleMessageMetadata = new SingleMessageMetadata(); | ||
| int skippedMessages = 0; | ||
| int processedMessages = 0; | ||
| try { | ||
| for (int i = 0; i < batchSize; ++i) { | ||
| final MessageImpl<T> message = newSingleMessage(i, batchSize, brokerEntryMetadata, msgMetadata, | ||
|
|
@@ -1815,13 +1816,15 @@ void receiveIndividualMessagesFromBatch(BrokerEntryMetadata brokerEntryMetadata, | |
| continue; | ||
| } | ||
| executeNotifyCallback(message); | ||
| processedMessages++; | ||
| } | ||
| if (ackBitSet != null) { | ||
| ackBitSet.recycle(); | ||
| } | ||
| } catch (IllegalStateException e) { | ||
| log.warn("[{}] [{}] unable to obtain message in batch", subscription, consumerName, e); | ||
| discardCorruptedMessage(messageId, cnx, ValidationError.BatchDeSerializeError); | ||
| } catch (IllegalStateException | IllegalArgumentException e) { | ||
|
gosonzhang marked this conversation as resolved.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commands.deSerializeSingleMessageInBatch() may also throw IndexOutOfBoundsException for truncated payload (triggered by readUnsignedInt() or retainedSlice()), whereas newSingleMessage() currently does not wrap it into IllegalStateException. As a result, encountering a corrupted batch with truncation or length mismatch can still bypass discardCorruptedBatchMessage, and permit leak or stuck issues would persist. |
||
| // For IllegalArgumentException see PR: https://github.com/apache/pulsar/pull/24061 | ||
| discardCorruptedBatchMessage(messageId, cnx, batchSize, | ||
| skippedMessages, processedMessages, ValidationError.BatchDeSerializeError); | ||
| } | ||
|
|
||
| if (deadLetterPolicy != null && possibleSendToDeadLetterTopicMessages != null) { | ||
|
|
@@ -2153,6 +2156,38 @@ private void discardCorruptedMessage(MessageIdData messageId, ClientCnx currentC | |
| discardMessage(messageId, currentCnx, validationError, 1); | ||
| } | ||
|
|
||
| /** | ||
| * When batch index ack is enabled, ack the messages that failed to deserialize by their index, | ||
| * while keeping successfully enqueued messages unacknowledged to avoid message loss. | ||
| */ | ||
| private void discardCorruptedBatchMessage(MessageIdData messageId, ClientCnx currentCnx, | ||
| int batchSize, int skipped, int processed, ValidationError validationError) { | ||
| log.error("[{}] [{}] Discarding corrupted batch messages with batch index ack at {}:{}, " | ||
| + "batchSize={}, skipped={}, processed={}, exception={}", | ||
| subscription, consumerName, messageId.getLedgerId(), messageId.getEntryId(), | ||
| batchSize, skipped, processed, validationError); | ||
| BitSetRecyclable ackBitSet = null; | ||
| int corruptedStartIndex = skipped + processed; | ||
| if (conf.isBatchIndexAckEnabled()) { | ||
| // When batch index ack is enabled, only ack the messages that failed to deserialize. | ||
| // Messages that have been successfully enqueued remain unacknowledged, | ||
| // waiting for the user to consume and acknowledge them normally. | ||
| ackBitSet = BitSetRecyclable.create(); | ||
| ackBitSet.set(0, batchSize); | ||
| for (int i = corruptedStartIndex; i < batchSize; i++) { | ||
| ackBitSet.clear(i); | ||
| } | ||
| } | ||
| ByteBuf cmd = Commands.newAck(consumerId, messageId.getLedgerId(), messageId.getEntryId(), | ||
| ackBitSet, AckType.Individual, validationError, Collections.emptyMap(), -1); | ||
| currentCnx.ctx().writeAndFlush(cmd, currentCnx.ctx().voidPromise()); | ||
| if (ackBitSet != null) { | ||
| ackBitSet.recycle(); | ||
| } | ||
| increaseAvailablePermits(currentCnx, batchSize - corruptedStartIndex); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is not advisable to directly use It might be better to pass in the current inbound ackBitSet, count only the bits after the failing index that are still deliverable, and construct a new ack set based on that. |
||
| stats.incrementNumReceiveFailed(); | ||
| } | ||
|
|
||
| private void discardMessage(MessageIdData messageId, ClientCnx currentCnx, ValidationError validationError, | ||
| int batchMessages) { | ||
| ByteBuf cmd = Commands.newAck(consumerId, messageId.getLedgerId(), messageId.getEntryId(), null, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.