Repository navigation
Description
Flaky-test: org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopic
Number of failures: 20
org.apache.pulsar.client.api.DeadLetterTopicTest is flaky. The testDeadLetterTopic test method fails sporadically.
java.lang.AssertionError: expected [null] but found [org.apache.pulsar.client.impl.MessageImpl@151b99cf]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotSame(Assert.java:1033)
at org.testng.Assert.assertNull(Assert.java:965)
at org.testng.Assert.assertNull(Assert.java:953)
at org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopic(DeadLetterTopicTest.java:124)
Usage tip: To enable automatic navigation to failure message, open the following links with CTRL/CMD-click.
example failure 2021-03-10T21:36:48.2364681Z
example failure 2021-03-15T15:38:25.7377495Z
example failure 2021-03-12T01:03:28.7485909Z
example failure 2021-02-26T21:40:51.3841331Z
example failure 2021-03-10T15:26:59.3094996Z
example failure 2021-03-11T11:35:33.6131220Z
example failure 2021-03-11T11:23:56.2390495Z
example failure 2021-03-08T00:39:47.2234098Z
example failure 2021-03-11T11:12:15.4495348Z
example failure 2021-02-27T07:05:57.1162853Z
example failure 2021-03-09T02:10:02.7834249Z
example failure 2021-03-10T15:39:08.1125019Z
example failure 2021-03-07T09:48:29.6697086Z
example failure 2021-03-03T21:36:00.2774777Z
example failure 2021-03-09T15:52:33.5570341Z
example failure 2021-03-13T04:18:16.5544068Z
example failure 2021-03-09T16:03:43.0219246Z
example failure 2021-03-08T01:01:26.6950037Z
example failure 2021-03-14T06:24:52.5153419Z
example failure 2021-03-11T14:34:28.9441969Z
Full exception stacktrace
java.lang.AssertionError: expected [null] but found [org.apache.pulsar.client.impl.MessageImpl@151b99cf]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotSame(Assert.java:1033)
at org.testng.Assert.assertNull(Assert.java:965)
at org.testng.Assert.assertNull(Assert.java:953)
at org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopic(DeadLetterTopicTest.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:45)
at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:73)
at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:11)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Flaky-test: org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopicByCustomTopicName
Number of failures: 8
org.apache.pulsar.client.api.DeadLetterTopicTest is flaky. The testDeadLetterTopicByCustomTopicName test method fails sporadically.
java.lang.AssertionError: expected [null] but found [org.apache.pulsar.client.impl.MessageImpl@4490d295]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotSame(Assert.java:1033)
at org.testng.Assert.assertNull(Assert.java:965)
at org.testng.Assert.assertNull(Assert.java:953)
at org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopicByCustomTopicName(DeadLetterTopicTest.java:397)
Usage tip: To enable automatic navigation to failure message, open the following links with CTRL/CMD-click.
example failure 2021-03-08T00:50:56.6220381Z
example failure 2021-03-06T02:41:58.7663850Z
example failure 2021-03-16T00:25:57.4946126Z
example failure 2021-03-15T15:47:58.3723431Z
example failure 2021-02-26T21:53:13.5461069Z
example failure 2021-03-11T17:33:24.1271868Z
example failure 2021-03-09T02:10:02.8676980Z
example failure 2021-03-10T17:22:30.4745269Z
Full exception stacktrace
java.lang.AssertionError: expected [null] but found [org.apache.pulsar.client.impl.MessageImpl@4490d295]
at org.testng.Assert.fail(Assert.java:99)
at org.testng.Assert.failNotSame(Assert.java:1033)
at org.testng.Assert.assertNull(Assert.java:965)
at org.testng.Assert.assertNull(Assert.java:953)
at org.apache.pulsar.client.api.DeadLetterTopicTest.testDeadLetterTopicByCustomTopicName(DeadLetterTopicTest.java:397)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:132)
at org.testng.internal.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:45)
at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:73)
at org.testng.internal.InvokeMethodRunnable.call(InvokeMethodRunnable.java:11)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Activity
[-]Flaky-test: [test class].[test method][/-][+]Flaky-test: DeadLetterTopicTest.testDeadLetterTopic[/+]lhotari commentedon Mar 16, 2021
I tried to reproduce this locally on latest master branch, but didn't get the test to fail. If someone is able to reproduce, please comment here.
michaeljmarshall commentedon Mar 19, 2021
I made some solid progress running through this test with a debugger. It does indeed look like there is a race condition based on the design of the test and the way that we send messages to dead letter queues. Essentially, we send a message to a DLQ before we acknowledge it on the original topic. Combine that with the fact that when a consumer client is closed, it releases all unacknowledged messages to any other consumers, and you get the flaky test that results from a race condition.
At this point, I have clearly identified why we have a flaky test, but I haven't determined the right solution (change the test or change the source). I will spend more time thinking about this and either post with follow up questions and/or options or submit a PR. Fundamentally, I don't think this test should be too hard to fix.
I need to sign off for the night, but I am very interested in solving this one. I should be able to get more time with it tomorrow or this weekend.
michaeljmarshall commentedon Mar 19, 2021
Quick note: it does seem rather intentional that we send to the DLQ first and only acknowledge the message on the original topic upon successful delivery to the DLQ. This ordering is essential to our DLQ delivery semantics. If we switched the order, we'd end up with an at most once delivery to the DLQ, which seems undesirable to me and like a breaking change from the current behavior.
I have a few potential solutions that I need to spend a little more time thinking through. I'll post an update tomorrow.
lhotari commentedon Mar 19, 2021
Thanks for putting effort into fixing this @michaeljmarshall . Fixing this test seems pretty challenging. If there are obstacles that are hard to overcome, perhaps there's a way to simplify the test.
One thing that I've noticed before with testing DLQ behavior is that disabling batching in the producer is necessary to get consistent behavior. That's one thing to try out when fixing this one.
michaeljmarshall commentedon Mar 22, 2021
I agree this test is a bit complicated, but annotating the test should help to discuss the behavior that I believe is leading to test flakiness. Because there are no timeouts in the test, I only see one place for a race condition, and I'm able to consistently get failures by adding a
sleepin one key location.Annotated Test
In the following code block, we create a normal consumer, a DLQ consumer, and a producer. We then produce
sendMessagesto thetopic. This all appears deterministic to me.After running the above, we have a topic with 100 messages. The while loop will complete when we have completed 300 loops (100 * (2 + 1)). We start the while loop by consuming from the topic with the basic consumer. We consume 100 messages very quickly, and then wait 1 second for each of them to timeout because we haven't ack'ed any of them. We consume 100 messages again, then wait 1 second again. We do it a third time, which is when the
whileclause is false and we move on to the next block (before the messages time out from not being ack'ed).Up to this point, all of the behavior is deterministic as far as message delivery and acking. Even if the redeliveries are delayed, the code waits indefinitely until the messages are delivered.
The next code block waits 1 second at
deadLetterConsumer.receive()because that consumer is looking for messages on the DLQ topic, and messages aren't produced to the topic until they timeout from not being ack'ed. We then consume all 100 messages from the DLQ topic.This is the where we find the race condition. The redelivery logic lives in the client. The client produces each expired message to the DLQ topic and when that call completes, the client sends acks to the source topic for each message. Given that order, the
deadLetterConsumercould consume messages from the DLQ topic before they are removed (ack'ed) from the source topic.We then close the clients. If a client is closed before the call to ack the messages, the ack calls will fail. Closing the client does not include any type of wait to give a best effort at delivering those acks.
Next, we create a consumer to consume from the source topic to see if any messages are on it. If any messages are there, we fail the test (this is exactly what we've been seeing in the above failures).
Thread Sleep And Reproducing Failure
To further prove this point, I added a
Thread.sleep(1000);to the line immediately before the following:pulsar/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java
Line 1687 in a8b921c
And it results in the test consistently failing with the following log line in the test logs:
Potential Solutions
I am still uncertain about how to best solve this test's flakiness. This test is a bit contrived, and the race condition is likely not one seen by end users. A few possible solutions:
I prefer the second option. The first option leaves the race condition in the client. The third is likely not easy because the server doesn't have the necessary information, and further, the current paradigm is that the redelivery logic lives in the client.
If there is agreement that the second solution is the right direction, I'm happy to look into implementing it.
codelipenghui commentedon Mar 22, 2021
A quick fix is to close the consumer before ensuring the messages already get acked at the broker side? I think we can check the backlog by using the pulsar-admin?
michaeljmarshall commentedon Mar 23, 2021
After looking at this a little more, I am wondering if it is inexpensive enough to use a transaction to produce to the DLQ topic and acknowledge the original message atomically. Then, the messages would not be end up available on both the source topic and the DLQ topic, as this test shows is currently possible.
@codelipenghui - I do not think this solution will work. When a consumer is closed, it drops any messages that are in the
possibleSendToDeadLetterTopicMessageswithout moving them to the DLQ topic. That means the messages won't ever make it to the DLQ topic. As I mentioned in the annotations above, the DLQ topic consumer is the logic that waits 1 second for the original messages to time out and get delivered to the DLQ topic. All of the redelivery logic lives within the client, so closing it prematurely will lead to improper redelivery semantics.I noticed while digging into this test that the
redeliveryCountis not maintained from the first consumer to the second one. That seems unexpected to me, and I am wondering this is expected.codelipenghui commentedon Mar 24, 2021
@michaeljmarshall We can close the original consumer after check the DLQ has enough messages?
michaeljmarshall commentedon Mar 25, 2021
That is essentially what we're currently doing in the test. We consume all of the messages from the DLQ and then close the two clients. I think we're seeing flakiness here because of the race condition in the consumer client. We publish messages to the DLQ topic and then we ack those messages on the original topic. If we close the consumer after sending a message to the DLQ topic and before acknowledging the message, this test will fail.
Regarding my earlier comment about using transactions, it looks like the transaction client isn't available in the consumer client class. I am going to look into delaying the closing of the client if there are outstanding messages to be ack'ed
michaeljmarshall commentedon Mar 25, 2021
I took a look at updating the client so that when closeAsync is called, it waits for the DLQ steps to complete. It doesn't look like that would fit into the paradigm for how the client is currently closed.
I'm not sure about the right way to move forward here, but I'll keep thinking about it.
michaeljmarshall commentedon Mar 26, 2021
I added this issue to the upcoming Pulsar Community meeting. There are a few design decisions I'd like to quickly talk through. Hopefully that will help identify a good solution to the existing race condition I exposed above.
devinbost commentedon Apr 7, 2021
I just got this flaky test failure in #10154
The fundamental problem with this test (and many of the other Flaky tests) is that we are basing test logic on timing, which falls apart when the test runner non-deterministically encounters a "stop the world" garbage collection event (or some other kind of event that creates random delays) somewhere in the middle of that. If the test runner is very resource-constrained, those kinds of events will happen a lot as the JVM tries to free up resources. A large testbed that has lots of temporary one-time-use objects like Pulsar's is the perfect breeding ground for generating lots of dead objects that need to be garbage collected, resulting in long garbage collection times, further exasperating the issue.
Sharing resources between tests may help with that, but it's not a perfect solution because it would be impossible to completely eliminate garbage collection in the test runner. So, as long as we're depending upon timing in our tests, we will continue facing this issue.
michaeljmarshall commentedon Apr 7, 2021
@devinbost - I agree that those conditions make these tests more likely to fail. This test is also problematic because the underlying dead letter logic has a data race that can lead to messages being read from both the DLQ topic and then from the source topic. Any chance that you looked up the logs before triggering another run of that test?
devinbost commentedon Apr 7, 2021
[CI] Move flaky tests that fail very often to "quarantine" test group (…
codelipenghui commentedon Mar 4, 2022
The issue had no activity for 30 days, mark with Stale label.
tisonkun commentedon Dec 11, 2022
Closed as stale. Please create a new issue if it's still relevant to the maintained versions.