Skip to content

Commit 4db15f3

Browse files
lifepuzzlefunTechnoboy-
authored andcommitted
[fix][broker] fix MessageDeduplication throw NPE when enable broker dedup and set namespace disable deduplication. (#20905)
1 parent 0883150 commit 4db15f3

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

pulsar-broker/src/main/java/org/apache/pulsar/broker/service/persistent/MessageDeduplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,10 @@ public long getLastPublishedSequenceId(String producerName) {
482482
}
483483

484484
public void takeSnapshot() {
485+
if (!isEnabled()) {
486+
return;
487+
}
488+
485489
Integer interval = topic.getHierarchyTopicPolicies().getDeduplicationSnapshotIntervalSeconds().get();
486490
long currentTimeStamp = System.currentTimeMillis();
487491
if (interval == null || interval <= 0

pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/TopicDuplicationTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,43 @@ public void testDisableNamespacePolicyTakeSnapshot() throws Exception {
492492

493493
}
494494

495+
@Test(timeOut = 30000)
496+
public void testDisableNamespacePolicyTakeSnapshotShouldNotThrowException() throws Exception {
497+
cleanup();
498+
conf.setBrokerDeduplicationEnabled(true);
499+
conf.setBrokerDeduplicationSnapshotFrequencyInSeconds(1);
500+
conf.setBrokerDeduplicationSnapshotIntervalSeconds(1);
501+
conf.setBrokerDeduplicationEntriesInterval(20000);
502+
setup();
503+
504+
final String topicName = testTopic + UUID.randomUUID().toString();
505+
final String producerName = "my-producer";
506+
@Cleanup
507+
Producer<String> producer = pulsarClient
508+
.newProducer(Schema.STRING).topic(topicName).enableBatching(false).producerName(producerName).create();
509+
510+
// disable deduplication
511+
admin.namespaces().setDeduplicationStatus(myNamespace, false);
512+
513+
int msgNum = 50;
514+
CountDownLatch countDownLatch = new CountDownLatch(msgNum);
515+
for (int i = 0; i < msgNum; i++) {
516+
producer.newMessage().value("msg" + i).sendAsync().whenComplete((res, e) -> countDownLatch.countDown());
517+
}
518+
countDownLatch.await();
519+
PersistentTopic persistentTopic = (PersistentTopic) pulsar.getBrokerService()
520+
.getTopicIfExists(topicName).get().get();
521+
ManagedCursor managedCursor = persistentTopic.getMessageDeduplication().getManagedCursor();
522+
523+
// when disable topic deduplication the cursor should be deleted.
524+
assertNull(managedCursor);
525+
526+
// this method will be called at brokerService forEachTopic.
527+
// if topic level disable deduplication.
528+
// this method should be skipped without throw exception.
529+
persistentTopic.checkDeduplicationSnapshot();
530+
}
531+
495532
private void waitCacheInit(String topicName) throws Exception {
496533
pulsarClient.newConsumer().topic(topicName).subscriptionName("my-sub").subscribe().close();
497534
TopicName topic = TopicName.get(topicName);

0 commit comments

Comments
 (0)