Repository navigation
Description
Search before reporting
- I searched in the issues and found nothing similar.
Read release policy
- I understand that unsupported versions don't get bug fixes. I will attempt to reproduce the issue on a supported version of Pulsar client and Pulsar broker.
User environment
- Broker: master (5.1.0-SNAPSHOT); the same code is in 5.0.x and 4.2.x (4.2.0 and later). 4.1.x and 4.0.x aren't affected.
- Any OS and Java version: the race is in the managed ledger's cursor code.
Issue Description
Since the managed ledger moved to LightProto (4.2.0), ManagedCursorImpl.persistPositionToLedger builds and serializes a cursor's position in one PositionInfo per cursor, reusablePositionInfo, instead of a new one for each persist. It clears the instance, sets the position, adds the batch index acks, properties and individually deleted ranges, and serializes it with toByteArray(), without synchronization.
Two persists of the same cursor can run at the same time:
- A mark-delete persists the position from
internalMarkDeleteon the thread that flushes the mark-deletes. - Closing the cursor (
asyncClose→persistPositionWhenClosing) persists the last mark-delete position on the closing thread. The close path doesn't take thependingMarkDeleteOpsmonitor that orders the mark-deletes, so it isn't ordered with a mark-delete in progress.
When they overlap, one can clear or change the shared PositionInfo while the other is building or serializing it. Then either:
- A wrong position is written to the cursor ledger: the position or the individually deleted ranges of one persist, mixed with or replaced by those of the other. After the topic is loaded again, the cursor recovers from that entry. That can mean the acked messages are delivered again, or another cursor state than the last one written.
- The serialization fails:
toByteArray()throwsIllegalStateException(below). The exception propagates out ofpersistPositionToLedgerbefore the entry is added to the cursor ledger, so that persist's callback is never completed: the mark-delete or the close doesn't complete.
What should happen: each persist writes exactly the position it was asked to persist, and concurrent persists don't affect each other.
Error messages
java.lang.IllegalStateException: Some required fields are missing
at org.apache.bookkeeper.mledger.proto.PositionInfo.checkRequiredFields(PositionInfo.java:568)
at org.apache.bookkeeper.mledger.proto.PositionInfo.toByteArray(PositionInfo.java:735)
at org.apache.bookkeeper.mledger.impl.ManagedCursorImpl.persistPositionToLedger(ManagedCursorImpl.java)
Reproducing the issue
The overlap of a close and a mark-delete is a narrow window, so it's easiest to reproduce in a unit test that calls persistPositionToLedger from two threads with the same cursor:
- Create a
ManagedCursorImpland pass a mockedLedgerHandlewhoseasyncAddEntryrecords the bytes and completes the callback. - From each of two threads, call
persistPositionToLedger20,000 times, each thread with its own position (for example ledger 1 and ledger 2) and its own properties. - Parse every recorded entry as
PositionInfoand check that it is one thread's position with that thread's properties.
Without synchronization, the test fails with the IllegalStateException above.
Additional information
A fix: build and serialize the reused PositionInfo under its monitor, taken before the cursor's read/write lock that building the ranges takes. No caller of persistPositionToLedger holds that lock, so the order is safe, and the monitor is uncontended except during a close. This keeps the reuse, which avoids allocating a PositionInfo for every persist.
Are you willing to submit a PR?
- I'm willing to submit a PR!
Activity
[fix][ml] Serialize a cursor's reused position info under its monitor