Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,38 @@ public void testMaxPendingChunkMessages() throws Exception {
assertNull(consumer.receive(5, TimeUnit.SECONDS));
}

@Test
public void testResendChunkMessagesWithoutAckHole() throws Exception {
log.info("-- Starting {} test --", methodName);
final String topicName = "persistent://my-property/my-ns/testResendChunkMessagesWithoutAckHole";
final String subName = "my-subscriber-name";
@Cleanup
Consumer<String> consumer = pulsarClient.newConsumer(Schema.STRING)
.topic(topicName)
.subscriptionName(subName)
.maxPendingChunkedMessage(10)
.autoAckOldestChunkedMessageOnQueueFull(true)
.subscribe();
@Cleanup
Producer<String> producer = pulsarClient.newProducer(Schema.STRING)
.topic(topicName)
.chunkMaxMessageSize(100)
.enableChunking(true)
.enableBatching(false)
.create();

sendSingleChunk(producer, "0", 0, 2);

sendSingleChunk(producer, "0", 0, 2); // Resending the first chunk
sendSingleChunk(producer, "0", 1, 2);

Message<String> receivedMsg = consumer.receive(5, TimeUnit.SECONDS);
assertEquals(receivedMsg.getValue(), "chunk-0-0|chunk-0-1|");
consumer.acknowledge(receivedMsg);
assertEquals(admin.topics().getStats(topicName).getSubscriptions().get(subName)
Comment thread
liangyepianzhou marked this conversation as resolved.
.getNonContiguousDeletedMessagesRanges(), 0);
}

@Test
public void testResendChunkMessages() throws Exception {
log.info("-- Starting {} test --", methodName);
Expand Down Expand Up @@ -395,6 +427,7 @@ public void testResendChunkMessages() throws Exception {
receivedMsg = consumer.receive(5, TimeUnit.SECONDS);
assertEquals(receivedMsg.getValue(), "chunk-1-0|chunk-1-1|chunk-1-2|");
consumer.acknowledge(receivedMsg);
Assert.assertEquals(((ConsumerImpl<String>) consumer).getAvailablePermits(), 8);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,9 @@ void messageReceived(CommandMessage cmdMessage, ByteBuf headersAndPayload, Clien

private ByteBuf processMessageChunk(ByteBuf compressedPayload, MessageMetadata msgMetadata, MessageIdImpl msgId,
MessageIdData messageId, ClientCnx cnx) {

if (msgMetadata.getChunkId() != (msgMetadata.getNumChunksFromMsg() - 1)) {
increaseAvailablePermits(cnx);
}
// Lazy task scheduling to expire incomplete chunk message
if (expireTimeOfIncompleteChunkedMessageMillis > 0 && expireChunkMessageTaskScheduled.compareAndSet(false,
true)) {
Expand All @@ -1437,6 +1439,37 @@ private ByteBuf processMessageChunk(ByteBuf compressedPayload, MessageMetadata m

if (msgMetadata.getChunkId() == 0) {
if (chunkedMsgCtx != null) {
// Handle ack hole case when receive duplicated chunks.
// There are two situation that receives chunks with the same sequence ID and chunk ID.
// Situation 1 - Message redeliver:
// For example:
// Chunk-1 sequence ID: 0, chunk ID: 0, msgID: 1:1
// Chunk-2 sequence ID: 0, chunk ID: 1, msgID: 1:2
// Chunk-3 sequence ID: 0, chunk ID: 0, msgID: 1:1
// Chunk-4 sequence ID: 0, chunk ID: 1, msgID: 1:2
// Chunk-5 sequence ID: 0, chunk ID: 2, msgID: 1:3
// In this case, chunk-3 and chunk-4 have the same msgID with chunk-1 and chunk-2.
// This may be caused by message redeliver, we can't ack any chunk in this case here.
// Situation 2 - Corrupted chunk message
// For example:
// Chunk-1 sequence ID: 0, chunk ID: 0, msgID: 1:1
// Chunk-2 sequence ID: 0, chunk ID: 1, msgID: 1:2
// Chunk-3 sequence ID: 0, chunk ID: 0, msgID: 1:3
// Chunk-4 sequence ID: 0, chunk ID: 1, msgID: 1:4
// Chunk-5 sequence ID: 0, chunk ID: 2, msgID: 1:5
// In this case, all the chunks with different msgIDs and are persistent in the topic.
// But Chunk-1 and Chunk-2 belong to a corrupted chunk message that must be skipped since
// they will not be delivered to end users. So we should ack them here to avoid ack hole.
boolean isCorruptedChunkMessageDetected = Arrays.stream(chunkedMsgCtx.chunkedMessageIds)
.noneMatch(messageId1 -> messageId1 != null && messageId1.ledgerId == messageId.getLedgerId()
&& messageId1.entryId == messageId.getEntryId());
if (isCorruptedChunkMessageDetected) {
Arrays.stream(chunkedMsgCtx.chunkedMessageIds).forEach(messageId1 -> {
if (messageId1 != null) {
doAcknowledge(messageId1, AckType.Individual, Collections.emptyMap(), null);
}
});
}
Comment thread
liangyepianzhou marked this conversation as resolved.
// The first chunk of a new chunked-message received before receiving other chunks of previous
// chunked-message
// so, remove previous chunked-message from map and release buffer
Expand Down Expand Up @@ -1476,11 +1509,12 @@ private ByteBuf processMessageChunk(ByteBuf compressedPayload, MessageMetadata m
msgMetadata.getProducerName(), msgId, chunkedMsgCtx.lastChunkedMessageId,
msgMetadata.getChunkId(), msgMetadata.getSequenceId());
compressedPayload.release();
increaseAvailablePermits(cnx);
boolean repeatedlyReceived = Arrays.stream(chunkedMsgCtx.chunkedMessageIds)
.anyMatch(messageId1 -> messageId1 != null && messageId1.ledgerId == messageId.getLedgerId()
// Just like the above logic of receiving the first chunk again. We only ack this chunk in the message
// duplication case.
boolean isDuplicatedChunk = Arrays.stream(chunkedMsgCtx.chunkedMessageIds)
.noneMatch(messageId1 -> messageId1 != null && messageId1.ledgerId == messageId.getLedgerId()
&& messageId1.entryId == messageId.getEntryId());
if (!repeatedlyReceived) {
if (isDuplicatedChunk) {
doAcknowledge(msgId, AckType.Individual, Collections.emptyMap(), null);
}
return null;
Expand All @@ -1497,7 +1531,6 @@ private ByteBuf processMessageChunk(ByteBuf compressedPayload, MessageMetadata m
}
chunkedMessagesMap.remove(msgMetadata.getUuid());
compressedPayload.release();
increaseAvailablePermits(cnx);
if (expireTimeOfIncompleteChunkedMessageMillis > 0
&& System.currentTimeMillis() > (msgMetadata.getPublishTime()
+ expireTimeOfIncompleteChunkedMessageMillis)) {
Expand All @@ -1516,7 +1549,6 @@ private ByteBuf processMessageChunk(ByteBuf compressedPayload, MessageMetadata m
// if final chunk is not received yet then release payload and return
if (msgMetadata.getChunkId() != (msgMetadata.getNumChunksFromMsg() - 1)) {
compressedPayload.release();
increaseAvailablePermits(cnx);
return null;
}

Expand Down