Skip to content

[fix][ML] Fix offload read handle NPE. - #17056

Merged
codelipenghui merged 8 commits into
apache:masterfrom
horizonzy:fix-reset-cursor-read-slow
Aug 13, 2022
Merged

[fix][ML] Fix offload read handle NPE.#17056
codelipenghui merged 8 commits into
apache:masterfrom
horizonzy:fix-reset-cursor-read-slow

Conversation

@horizonzy

@horizonzy horizonzy commented Aug 11, 2022

Copy link
Copy Markdown
Member

Motivation

This issue was introduced by #11389

Now, when we get offload ReadHandle(BlobStoreBackedReadHandleImpl) and then use it to read data (Non-durable cursor read). Maybe the offload ReadHandle we get already is invalidated.

There is a race condition, maybe after we get the ReadHandle, before use it to read.
The method ManagedLedgerImpl#internalTrimLedgers invalidate this ReadHandle (Cause in this method, it just calculate by durable-cursor, but there maybe a non-durable cursor read data) so we use the invalidated offload read handle to read data, invalidated will close the read handle.

In the close method, it will set index.segmentMetadata=null, so when getLastAddConfirmed, case NPE.

The NPE stack info

rg.apache.bookkeeper.mledger.ManagedLedgerException: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
	at org.apache.bookkeeper.mledger.offload.jcloud.impl.BlobStoreBackedReadHandleImpl.getLastAddConfirmed(BlobStoreBackedReadHandleImpl.java:200) ~[?:?]
	at org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.internalReadFromLedger(ManagedLedgerImpl.java:1870) ~[io.streamnative-managed-ledger-2.9.2.23.jar:2.9.2.23]
	at org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.lambda$asyncReadEntries$17(ManagedLedgerImpl.java:1719) ~[io.streamnative-managed-ledger-2.9.2.23.jar:2.9.2.23]
	at java.util.concurrent.CompletableFuture$UniAccept.tryFire(CompletableFuture.java:714) ~[?:?]
	at java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:506) ~[?:?]
	at java.util.concurrent.CompletableFuture.complete(CompletableFuture.java:2073) ~[?:?]
	at org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.lambda$null$20(ManagedLedgerImpl.java:1790) ~[io.streamnative-managed-ledger-2.9.2.23.jar:2.9.2.23]
	at java.util.concurrent.CompletableFuture.uniWhenComplete(CompletableFuture.java:859) [?:?]
	at java.util.concurrent.CompletableFuture$UniWhenComplete.tryFire(CompletableFuture.java:837) [?:?]
	at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:478) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [io.netty-netty-common-4.1.77.Final.jar:4.1.77.Final]
	at java.lang.Thread.run(Thread.java:829) [?:?]
03:30:51.930 [broker-topic-workers-OrderedExecutor-3-0] ERROR org.apache.pulsar.broker.service.persistent.PersistentDispatcherSingleActiveConsumer - [persistent://public/default/test / reader-51d0ba5f20-Consumer{subscription=PersistentSubscription{topic=persistent://public/default/test, name=reader-51d0ba5f20}, consumerId=0, consumerName=reader-0, address=/10.244.1.143:51782}] Error reading entries at 0:101 : java.lang.NullPointerException - Retrying to read in 15.0 seconds

Modifications

1.Fix the NPE.
2.Check the invalidate ledgerId is less than the slowest non-durable, reduce the npe probability.

Documentation

Check the box below or label this PR directly.

Need to update docs?

  • doc-required
    (Your PR needs to update docs and you will update later)

  • doc-not-needed
    (Please explain why)

  • doc
    (Your PR contains doc changes)

  • doc-complete
    (Docs have been already added)

@hangc0276 hangc0276 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice catch! LGTM.

}
});
if (slowestNonDurableReadPosition.get() != null) {
ManagedLedger managedLedger = getManagedLedger();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can directly use ledger.updateTheSlowestNonDurableReadPosition

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

@horizonzy horizonzy Aug 11, 2022

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Agrre, and there will reset ManagedLedgerImpl.slowestNonDurableReadPosition when there is no non-duranble cursor, fixed it in next commit.

@hangc0276 hangc0276 added type/bug The PR fixed a bug or issue reported a bug release/2.10.2 release/2.9.4 release/2.8.5 labels Aug 11, 2022
@hangc0276 hangc0276 added this to the 2.12.0 milestone Aug 11, 2022
@hangc0276 hangc0276 added the doc-not-needed Your PR changes do not impact docs label Aug 11, 2022
}
} else if (exception.getCause() instanceof TransactionBufferException.TransactionNotSealedException) {
} else if (exception.getCause() instanceof TransactionBufferException.TransactionNotSealedException
|| exception.getCause() instanceof ManagedLedgerException.OffloadReadHandleClosedException) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like we don't want to handle the race condition of trimming ledgers and reading data, instead, to re-trigger the read operation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it's difficult to handle the race condition, the offload read handle execute read operation in offload-executor, before the read real execute, the read handle maybe invalidated.

@codelipenghui codelipenghui Aug 11, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

+1

@codelipenghui

Copy link
Copy Markdown
Contributor

And can we add a test to make sure the reader handle will not invalidate if we the slowest read position haven't go through it yet.

@Technoboy- Technoboy- changed the title Fix offload read handle npe. [fix][ML] Fix offload read handle NPE. Aug 12, 2022
@horizonzy

Copy link
Copy Markdown
Member Author

Please review this pr again, use the new way to get the slowest non-durable read position.

@codelipenghui
codelipenghui merged commit 6fc48d1 into apache:master Aug 13, 2022
@michaeljmarshall

michaeljmarshall commented Aug 16, 2022

Copy link
Copy Markdown
Member

Heads up, cherry picking this commit to older branches broke them because of a missing import. I fixed 2.11 here: 74390d4

hangc0276 pushed a commit that referenced this pull request Aug 16, 2022
@zymap zymap added the cherry-picked/branch-2.9 Archived: 2.9 is end of life label Aug 16, 2022
Technoboy- pushed a commit to merlimat/pulsar that referenced this pull request Aug 16, 2022
@Jason918

Jason918 commented Sep 4, 2022

Copy link
Copy Markdown
Contributor

@horizonzy Can you help open a new PR to branch-2.10? There are a lot conflict when cherry-pick directly.

@horizonzy

Copy link
Copy Markdown
Member Author

alright.

horizonzy added a commit to horizonzy/pulsar that referenced this pull request Sep 5, 2022
This pr cherry-pick from apache#17056.
@horizonzy

Copy link
Copy Markdown
Member Author

@horizonzy Can you help open a new PR to branch-2.10? There are a lot conflict when cherry-pick directly.

#17478

@Jason918

Jason918 commented Sep 7, 2022

Copy link
Copy Markdown
Contributor

move release/2.10.2 to #17478

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-picked/branch-2.9 Archived: 2.9 is end of life cherry-picked/branch-2.11 doc-not-needed Your PR changes do not impact docs release/2.7.6 release/2.8.5 release/2.9.4 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.

7 participants