Skip to content

Handling error in creation of non-durable cursor - #7355

Merged
merlimat merged 2 commits into
apache:masterfrom
merlimat:handle-non-durable-cursor-error
Jun 27, 2020
Merged

merlimat merged 2 commits into
apache:masterfrom
merlimat:handle-non-durable-cursor-error

Conversation

@merlimat

@merlimat merlimat commented Jun 24, 2020 •

Copy link
Copy Markdown
Contributor

Motivation

We're getting an NPE when the creation of a non-durable cursor fails.

2020-06-20 00:00:23.872000+00:00 [WARN ] [he.pulsar.broker.service.ServerCnx]  [/127.0.0.1:47412][persistent://TOPIC][reader-7369753eb6] Failed to create consumer: null
        java.util.concurrent.CompletionException: java.lang.NullPointerException
        at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:314) [?:?]
        at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1113) [?:?]
        at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2235) [?:?]
        at org.apache.pulsar.broker.service.ServerCnx.lambda$null$14(ServerCnx.java:802) [org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:642) [?:?]
        at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506) [?:?]
        at java.util.concurrent.CompletableFuture.postFire(CompletableFuture.java:610) [?:?]
        at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:1085) [?:?]
        at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:478) [?:?]
        at java.lang.Thread.run(Thread.java:834) [?:?]
Caused by: java.lang.NullPointerException
        at org.apache.pulsar.broker.service.persistent.PersistentMessageExpiryMonitor.<init>(PersistentMessageExpiryMonitor.java:56) ~[org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.broker.service.persistent.PersistentSubscription.<init>(PersistentSubscription.java:148) ~[org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.broker.service.persistent.PersistentTopic.lambda$getNonDurableSubscription$13(PersistentTopic.java:695) ~[org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap$Section.put(ConcurrentOpenHashMap.java:274) ~[org.apache.pulsar-pulsar-common-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap.computeIfAbsent(ConcurrentOpenHashMap.java:129) ~[org.apache.pulsar-pulsar-common-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.broker.service.persistent.PersistentTopic.getNonDurableSubscription(PersistentTopic.java:673) ~[org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.broker.service.persistent.PersistentTopic.subscribe(PersistentTopic.java:581) ~[org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at org.apache.pulsar.broker.service.ServerCnx.lambda$null$11(ServerCnx.java:817) [org.apache.pulsar-pulsar-broker-2.5.0.SPLK.6584581.jar:2.5.0.SPLK.6584581]
        at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1106) ~[?:?]
        ... 8 more

The reason is that, we fail the future but we go on in creating the subscription instance:

try {
    cursor = ledger.newNonDurableCursor(startPosition, subscriptionName);
} catch (ManagedLedgerException e) {
    subscriptionFuture.completeExceptionally(e);
}

return new PersistentSubscription(this, subscriptionName, cursor, false);

Additionally, the NPE leads to the topic usage count to not be decremented, leaking 1 usage increment. At the time of deletion, this will prevent the topic from being deleted, even when using the force flag.

@merlimat merlimat self-assigned this Jun 24, 2020
@merlimat merlimat added the type/bug The PR fixed a bug or issue reported a bug label Jun 24, 2020
@merlimat merlimat added this to the 2.7.0 milestone Jun 24, 2020
@merlimat
merlimat merged commit 0a5f0a0 into apache:master Jun 27, 2020
merlimat added a commit to merlimat/pulsar that referenced this pull request Jun 30, 2020
codelipenghui pushed a commit to streamnative/pulsar-archived that referenced this pull request Jul 14, 2020
* Handling error in creation of non-durable cursor

* Fixed tests

(cherry picked from commit 0a5f0a0)
cdbartholomew pushed a commit to kafkaesque-io/pulsar that referenced this pull request Jul 21, 2020
* Handling error in creation of non-durable cursor

* Fixed tests
cdbartholomew pushed a commit to kafkaesque-io/pulsar that referenced this pull request Jul 24, 2020
* Handling error in creation of non-durable cursor

* Fixed tests
wolfstudy pushed a commit that referenced this pull request Jul 29, 2020
* Handling error in creation of non-durable cursor

* Fixed tests

(cherry picked from commit 0a5f0a0)
huangdx0726 pushed a commit to huangdx0726/pulsar that referenced this pull request Aug 24, 2020
* Handling error in creation of non-durable cursor

* Fixed tests

synchronized (ledger) {
// Create a new non-durable cursor only for the first consumer that connects
Subscription subscription = subscriptions.computeIfAbsent(subscriptionName, name -> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merlimat - I know this has been merged for a while, but I was reading through this class today, and I am concerned about a possible race condition that would lead to an incorrect update to the subscriptions map.

With this PR, the subscriptions map is no longer updated using the computeIfAbsent method here, but is instead modified with a get and then a subsequent put. The getDurableSubscription updates the subscriptions map concurrently as well. I believe this opens us up to a race under the following conditions:

  1. We call get for a subscription on line 695, and get null.
  2. Then, a durable subscription with the same name is added to the subscriptions map on line 665.
  3. Then, the non durable subscription is put into the subscriptions map on new line 722, and in doing so, overwrites the durable subscription of the same name.

Do you agree this is a risk here? If so, I will work on contributing a fix. I wanted to check first though, just in case there is context I'm missing. Thanks!

@merlimat
merlimat deleted the handle-non-durable-cursor-error branch October 24, 2026 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release/2.6.1 type/bug The PR fixed a bug or issue reported a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants