Skip to content

Commit

Permalink
Fix NPE in checkSubscriptionTypesEnable (apache#12961)
Browse files Browse the repository at this point in the history
### Motivation

The NPE may appear in `PersistentTopic.checkSubscriptionTypesEnable`.   The `topicPolicies.getSubscriptionTypesEnabled()` may be null.

The problem occurred when upgrading from pulsar version 2.7 to 2.8. We added `SubscriptionTypesEnable` in 2.8: apache#9401. When we initialize a topic's topicPolicy in pulasr 2.7, and then get the topic's topicPolicy in version 2.8, the `SubscriptionTypesEnable` will be null. This leads to the problem.

Here are steps to reproduce:
* Start broker 2.7
* Create a topic and init the topic policy
* Upgrade broker to 2.8
* We will get the `SubscriptionTypesEnable` with the value of null
```sh
➜  pulsar-281 bin/pulsar-admin topics get-subscription-types-enabled  my-topic
null
```
* When we consume from that topic, the issue occurs
```
Caused by: java.lang.NullPointerException
	at org.apache.pulsar.broker.service.persistent.PersistentTopic.checkSubscriptionTypesEnable(PersistentTopic.java:3206) ~[io.streamnative-pulsar-broker-2.8.1.21.jar:2.8.1.21]
	at org.apache.pulsar.broker.service.persistent.PersistentTopic.subscribe(PersistentTopic.java:688) ~[io.streamnative-pulsar-broker-2.8.1.21.jar:2.8.1.21]
	at org.apache.pulsar.broker.service.ServerCnx.lambda$null$13(ServerCnx.java:1029) ~[io.streamnative-pulsar-broker-2.8.1.21.jar:2.8.1.21]
	at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106) ~[?:?]
	... 31 more
```

### Modifications

* Use `CollectionUtils.isEmpty` to determine if the list is empty to fix the problem of throwing NPE.
  • Loading branch information
RobertIndie authored and fangxiaobing committed Dec 19, 2021
1 parent 05bab68 commit 45167de
Showing 1 changed file with 2 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
import org.apache.bookkeeper.mledger.impl.PositionImpl;
import org.apache.bookkeeper.net.BookieId;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.pulsar.broker.PulsarServerException;
Expand Down Expand Up @@ -3232,7 +3233,7 @@ public boolean checkSubscriptionTypesEnable(SubType subType) throws Exception {
if (topicPolicies == null) {
return checkNsAndBrokerSubscriptionTypesEnable(topicName, subType);
} else {
if (topicPolicies.getSubscriptionTypesEnabled().isEmpty()) {
if (CollectionUtils.isEmpty(topicPolicies.getSubscriptionTypesEnabled())) {
return checkNsAndBrokerSubscriptionTypesEnable(topicName, subType);
}
return topicPolicies.getSubscriptionTypesEnabled().contains(subType);
Expand Down

0 comments on commit 45167de

Please sign in to comment.