forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][client] Fix deserialized BatchMessageIdImpl acknowledgment failure
Fixes apache#19030 ### Motivation When a `BatchMessageIdImpl` is created from a deserialization, the `BatchMessageAcker` object cannot be shared by all instances in the same batch, which leads to an acknowledgment failure when batch index ACK is disabled (by default). ### Modifications Maintain a map from the `(ledger id, entry id)` pair to the `BatchMessageAcker` in `ConsumerImpl`. If the `BatchMessageIdImpl` doesn't carry a valid `BatchMessageAcker`, create and cache a `BatchMessageAcker` instance and remove it when all messages in the batch are acknowledged. It requires a change in `MessageIdImpl#fromByteArray` that a `BatchMessageAckerDisabled` will be created to indicate there is no shared acker. To avoid making code more complicated, this patch refactors the existing code that many logics about consumer are moved from the ACK tracker to the consumer. It also removes the `AckType` parameter when acknowledging a list of messages.
- Loading branch information
1 parent
41edd2e
commit ba52f1e
Showing
9 changed files
with
261 additions
and
187 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
pulsar-broker/src/test/java/org/apache/pulsar/client/api/MessageIdSerializationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.api; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertTrue; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
import lombok.Cleanup; | ||
import org.apache.pulsar.client.impl.BatchMessageIdImpl; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
@Test(groups = "broker-api") | ||
public class MessageIdSerializationTest extends ProducerConsumerBase { | ||
|
||
@BeforeClass | ||
@Override | ||
protected void setup() throws Exception { | ||
super.internalSetup(); | ||
super.producerBaseSetup(); | ||
} | ||
|
||
@AfterClass(alwaysRun = true) | ||
@Override | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
} | ||
|
||
@Test(timeOut = 30000) | ||
public void testSerialization() throws Exception { | ||
String topic = "test-serialization-origin"; | ||
@Cleanup Producer<Integer> producer = pulsarClient.newProducer(Schema.INT32) | ||
.topic(topic) | ||
.batchingMaxMessages(100) | ||
.batchingMaxPublishDelay(1, TimeUnit.DAYS) | ||
.create(); | ||
Consumer<Integer> consumer = pulsarClient.newConsumer(Schema.INT32) | ||
.topic(topic) | ||
.subscriptionName("sub") | ||
.isAckReceiptEnabled(true) | ||
.subscribe(); | ||
|
||
final int numMessages = 10; | ||
for (int i = 0; i < numMessages; i++) { | ||
producer.sendAsync(i); | ||
} | ||
producer.flush(); | ||
final List<MessageId> msgIds = new ArrayList<>(); | ||
for (int i = 0; i < numMessages; i++) { | ||
msgIds.add(consumer.receive().getMessageId()); | ||
} | ||
final AtomicLong ledgerId = new AtomicLong(-1L); | ||
final AtomicLong entryId = new AtomicLong(-1L); | ||
for (int i = 0; i < numMessages; i++) { | ||
assertTrue(msgIds.get(i) instanceof BatchMessageIdImpl); | ||
final BatchMessageIdImpl batchMessageId = (BatchMessageIdImpl) msgIds.get(i); | ||
ledgerId.compareAndSet(-1L, batchMessageId.getLedgerId()); | ||
assertEquals(batchMessageId.getLedgerId(), ledgerId.get()); | ||
entryId.compareAndSet(-1L, batchMessageId.getEntryId()); | ||
assertEquals(batchMessageId.getEntryId(), entryId.get()); | ||
assertEquals(batchMessageId.getBatchSize(), numMessages); | ||
} | ||
|
||
final List<MessageId> deserializedMsgIds = new ArrayList<>(); | ||
for (MessageId msgId : msgIds) { | ||
MessageId deserialized = MessageId.fromByteArray(msgId.toByteArray()); | ||
assertTrue(deserialized instanceof BatchMessageIdImpl); | ||
deserializedMsgIds.add(deserialized); | ||
} | ||
for (MessageId msgId : deserializedMsgIds) { | ||
consumer.acknowledge(msgId); | ||
} | ||
consumer.close(); | ||
|
||
consumer = pulsarClient.newConsumer(Schema.INT32) | ||
.topic(topic) | ||
.subscriptionName("sub") | ||
.isAckReceiptEnabled(true) | ||
.subscribe(); | ||
MessageId newMsgId = producer.send(0); | ||
MessageId receivedMessageId = consumer.receive().getMessageId(); | ||
assertEquals(newMsgId, receivedMessageId); | ||
consumer.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.