nodece commented on code in PR #22794:
URL: https://github.com/apache/pulsar/pull/22794#discussion_r1621699092
##########
pulsar-broker/src/test/java/org/apache/pulsar/broker/service/BrokerServiceTest.java:
##########
@@ -1313,6 +1314,67 @@ public void
testCheckInactiveSubscriptionsShouldNotDeleteCompactionCursor() thro
}
+ @Test
+ public void testCheckInactiveSubscriptionWhenNoMessageToAck() throws
Exception {
+ String namespace = "prop/testInactiveSubscriptionWhenNoMessageToAck";
+
+ try {
+ admin.namespaces().createNamespace(namespace);
+ } catch (PulsarAdminException.ConflictException e) {
+ // Ok.. (if test fails intermittently and namespace is already
created)
+ }
+ // set enable subscription expiration.
+ admin.namespaces().setSubscriptionExpirationTime(namespace, 1);
+
+ String topic = "persistent://" + namespace + "/my-topic";
+ Producer<byte[]> producer =
pulsarClient.newProducer().topic(topic).create();
+ producer.send("test".getBytes());
+ producer.close();
+
+ // create consumer to consume all messages
+ Consumer<byte[]> consumer =
pulsarClient.newConsumer().topic(topic).subscriptionName("sub1")
+
.subscriptionInitialPosition(SubscriptionInitialPosition.Earliest).subscribe();
+ consumer.acknowledge(consumer.receive());
+
+ Optional<Topic> topicOptional =
pulsar.getBrokerService().getTopic(topic, true).get();
+ assertTrue(topicOptional.isPresent());
+ PersistentTopic persistentTopic = (PersistentTopic)
topicOptional.get();
+
+ // wait for 1min, but consumer is still connected all the time.
+ // so subscription should not be deleted.
+ Thread.sleep(60000);
+ persistentTopic.checkInactiveSubscriptions();
Review Comment:
The 60s are too long, please add
`org.apache.pulsar.broker.service.persistent.PersistentTopic#checkInactiveSubscriptions(long)`,
so like:
```
@VisibleForTesting
public void checkInactiveSubscriptions(long expirationTimeMillis) {
if (expirationTimeMillis > 0) {
subscriptions.forEach((subName, sub) -> {
if (sub.dispatcher != null &&
sub.dispatcher.isConsumerConnected()
|| sub.isReplicated()
|| isCompactionSubscription(subName)) {
return;
}
if (System.currentTimeMillis() - sub.cursor.getLastActive()
> expirationTimeMillis) {
sub.delete().thenAccept(v -> log.info("[{}][{}] The
subscription was deleted due to expiration "
+ "with last active [{}]", topic, subName,
sub.cursor.getLastActive()));
}
});
}
}
```
And then you call `persistentTopic.checkInactiveSubscriptions(1);`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]