pulsar: [Bug] parseMessageMetadata error when broker entry metadata enable with high loading
Search before asking
- 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.
Version
- 3.2.2
- 3.1.2
Minimal reproduce step
publish event in about 6k QPS and 100Mbits/sec with metaData BatcherBuilder.KEY_BASED mode and producer and send message by high concurrent/parallel producer process. it happens only in almost real time consumer (almost zero backlog)
What did you expect to see?
no lost event
What did you see instead?
could see error log in broker and show Failed to peek sticky key from the message metadata
it look like thread safe issue, because it happen randomly. in 1M events, it only happen few times but the consumer will lose few events
Anything else?
the error similar to https://github.com/apache/pulsar/issues/10967 but I think it’s different issue.
the data in bookkeeper is correct. I can download the event from bookkeeper and parse it successfully. or consume the same event few minutes later and it could consume successfully. but all subscriptions will get the same error in the same event in real time consumer(zero backlog).
I have traced source code. it happens in PersistentDispatcherMultipleConsumers.readEntriesComplete -> AbstractBaseDispatcher.filterEntriesForConsumer -> Commands.peekAndCopyMessageMetadata
and I also print the ByteBuf contents, it’s I could clearly see the data isn’t the same in bookkeeper
in normal event , the hex code usually start by 010e (magicCrc32c)
0000000 010e 9529 5fbc 0000 0a03 3a0a 6e69 7267
in one of our error event, the bytebuf have about 48 bytes strange data, then continue with normal data
0000000 0000 a610 0000 0000 0200 7239 0000 0000 <== from here
0000020 0200 1339 0000 0000 ea17 a8b0 8b8e fa5e
0000040 2af0 2675 f645 1623 d17e dc34 526d ef44 <=== until here is garbage
0000060 010e 9529 5fbc 0000 0a03 3a0a 6e69 7267 <== from here is normal data
this is just an example, but sometimes the first few bytes are correct and something wrong after few bytes later.
I am still trying to debug when and how the ByteBuf returns incorrect data, and why it only happens during stress testing. It is still not easy to reproduce using the perf tool, but we can 100% reproduce it in our producer code.
Does anyone have any idea what could be causing this issue, and any suggestions on which library or class may have potential issues? Additionally, any suggestions on how to debug this issue or if I need to print any specific information to help identify the root cause would be appreciated. Thank you.
Are you willing to submit a PR?
- I’m willing to submit a PR!
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 68 (40 by maintainers)
Commits related to this issue
- Optimize wrap buffer cumulation in SslHandler and don't mutate input buffers (#14086) Motivation: SslHandler doesn't support sharing the input buffers since the input buffers get mutated. This i... — committed to netty/netty by lhotari 2 years ago
- Optimize wrap buffer cumulation in SslHandler and don't mutate input buffers (#14086) Motivation: SslHandler doesn't support sharing the input buffers since the input buffers get mutated. This i... — committed to netty/netty by lhotari 2 years ago
- Optimize wrap buffer cumulation in SslHandler and don't mutate input buffers (#14086) Motivation: SslHandler doesn't support sharing the input buffers since the input buffers get mutated. This i... — committed to yawkat/netty by lhotari 2 years ago
I finally found the root cause in Bookkeeper client. The client had several buffer leaks since the ByteBufList lifecycle was incorrectly handled and write promises were dropped and ignored when authentication was in progress. The fix is in https://github.com/apache/bookkeeper/pull/4293.
I couldn’t reproduce with Pulsar Standalone, but I have a way with a local Microk8s cluster where I could also attach a debugger. With break points in java.lang.IllegalArgumentException and java.nio.BufferUnderflowException, I can see the problem.
This issue happens when
.copy()is called on this line: https://github.com/apache/pulsar/blob/82237d3684fe506bcb6426b3b23f413422e6e4fb/pulsar-common/src/main/java/org/apache/pulsar/common/protocol/ByteBufPair.java#L149There’s a feature in Netty that
.copy()isn’t thread safe. If it’s called from multiple threads at a time, there will be a race condition. This happens here in Netty code:https://github.com/netty/netty/blob/243de91df2e9a9bf0ad938f54f76063c14ba6e3d/buffer/src/main/java/io/netty/buffer/ReadOnlyByteBufferBuf.java#L412-L433
io.netty.buffer.ReadOnlyByteBufferBuf#internalNioBuffer()returns a shared instance which gets corrupted. One could argue that this is a bug in ReadOnlyByteBufferBuf. At least this this extremely surprising behavior..copy()was added in https://github.com/apache/pulsar/issues/2401 . It looks like the root cause wasn’t properly fixed and the problem moved to a different location.In Netty, the SslHandler will access the underlying ByteBuffer instances directly. This leads to a similar multi-threading problem as the use of
.copy().I think that the problem is now clear where it happens, but the solution to fix this isn’t yet known.
Hi @lhotari I update perf tool in https://github.com/semistone/pulsar/tree/debug_ssues_22601
it only include one commit which modify PerformanceProducer.java to include big payload ( -bp 5 means 5 percent big payload) and BatcherBuilder.KEY_BASED (-kb)
my consumer command is
and producer command is
that error happen when
Batch builder is KEY_BASE with random event key and few big payload (in my environment 3% could reproduce 10% will crash producer)
in my test I use normal payload 2K bytes , big payload 20K bytes if I removed any above conditions, that error will either reduced or disappear.
when it happen it will have WARN message in pulsar-broker.log
unfortunately I can’t preproduce in docker, I guess docker standalone is different from my pulsar cluster. my pulsar cluster is almost default config but with TLS auth in broker/bookkeeper/zookeeper.
Please help to check it, if have any problem to reproduce this issue in your environment, then I will try to simplify my pulsar cluster to reproduce it.
Thanks
@lhotari I had tested pulsar 3.2.3 + bookkeeper 4.16.6-SNAPSHOT.jar + netty buffer 4.1.111.Final-SNAPSHOT
and I think this issue had been fixed. I will test more scenario Thanks.
@lhotari I tested in standalone server. and it’s all good. here is my test report https://github.com/semistone/personal_notes/blob/main/pulsar_issue_22601/20240528Test.md
I will deploy to our cluster and test again few days later.
@semistone I have created a PR #22760 to fix the problem. It’s currently in draft state since I’m currently testing the solution to verify that it mitigates the problem.
Looking up in Netty issue tracker.
Found these issues that provide a lot of context:
It seems that this is a long time issue and it has been partially fixed. However, it’s not fixed for many locations in the Netty code base and it’s not safe to share ByteBuf instances in all cases. We need to find a workaround for the Pulsar use cases. In Pulsar, the sharing happens in this case at least via the broker cache (RangeEntryCacheManagerImpl) and the pending reads manager (PendingReadsManager).
There’s some thought about this in #22110. That’s a WIP PR and it might not make sense in the end. Since then, I have found a pattern used in Netty to detect issues. I’ll look that up.
I almost could reproduce by perf tool when very few payload > 30K bytes. others are 3K bytes then error happen when messageKeyGenerationMode=random if without messageKeyGenerationMode, then error disappear
I guess in batch mode, payload size have some restriction.
let me confirm again tomorrow to make sure I didn’t make any stupid mistake during my test.
I will try to reproduce in perf tool.
I tried to upgrade to bookkeeper 4.17.0 but still have the same issue 😦
@lhotari we do many tests current broker setting is
and producer setting (small batch message and big max bytes)
payload 98% < 3K bytes 2% between 10-20K bytes
then it will show that error and publish throughput isn’t good.
but if we change to
and filter all data bigger than 15K bytes then that error disappear
so we decide to create one batch publisher to publish data < 15000 bytes and one chunk publisher to publish data >= 15000 bytes then it worked and performance is also better than previous test
we still don’t known why but at least we have workaround solution now.
I don’t how which batch producer configuration could fix this errors. if you have any suggestions, we will still try it .
and we also publish in multi thread programs, seems like it’s not directly related to loading but related to payload size but maybe if the publish rate is low, it’s more difficult to reproduce .
we also tried to reproduce by perf tool but it didn’t always happen.
thanks