You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Flakiness issues with subscriptionKeySharedUseConsistentHashing=true / PIP-119 in CPP tests #13965
I tried to use three Java consumers with Key_Shared subscription to consume the topic produced by C++ test KeySharedConsumerTest.testMultiTopics. Sometimes not all messages can be received as well. It looks like there is something wrong with the consistent hashing implementation of Key_Shared dispatcher.
I also made similar observations based on C++ test logs:
Yeah, here is a screenshot of my Java consumer application. The topic was created by C++ UT and received 3000 messages from C++ producer. Java consumers should have received 3000 messages in total, and sometimes it works well.
Yeah. It's weird that I also tried C++ client to consume the topic produce by Java client with subscriptionKeySharedUseConsistentHashing=false, the message loss still happened.
#include<assert.h>
#include<iostream>
#include<vector>
#include<pulsar/Client.h>usingnamespacepulsar;intmain() {
Client client("pulsar://localhost:6650");
std::vector<Consumer> consumers(3);
for (size_t i = 0; i < consumers.size(); i++) {
ConsumerConfiguration conf;
conf.setConsumerType(ConsumerType::ConsumerKeyShared);
conf.setSchema(SchemaInfo(STRING, "String", ""));
conf.setSubscriptionInitialPosition(InitialPositionEarliest);
conf.setPatternAutoDiscoveryPeriod(1);
auto result =
client.subscribeWithRegex(".*KeySharedConsumerTest-multi-topics.*", "my-sub", conf, consumers[i]);
assert(result == ResultOk);
}
std::vector<int> numReceivedList;
int numTotal = 0;
for (Consumer& consumer : consumers) {
int n = 0;
Message msg;
while (true) {
auto result = consumer.receive(msg, 3000);
if (result == ResultTimeout) {
break;
}
assert(result == ResultOk);
n++;
}
numReceivedList.emplace_back(n);
numTotal += n;
}
for (int n : numReceivedList) {
std::cout << n << std::endl;
}
std::cout << numTotal << std::endl;
client.close();
}
The code above is nearly the same as Consume.java in my repo. And a sample output is:
154
0
0
154
But when I run C++ UT, it never failed now when subscriptionKeySharedUseConsistentHashing=false
I think there is a problem with the repro code above and with the original C++ test and the production code is actually correct here.
The repro code is doing:
Create 3 producers and publish on 3 different topics
Create 3 consumers on the 3 topics with key-shared, starting from "earliest"
The problem is that consumers are created after the messages are published, therefore the pre-fetching is already done
The first consumer will get pushed 1K message when it's first added, then other keys will get stalled.
There are 2 problems in the way the repro code (and the test) are consuming:
There's no ack, therefore the keys that were sent to first consumer cannot be sent to any other consumers
The test is sequentially receiving from the 3 consumers, stopping when 1 consumers has no more messages for 1 second.
What happens is that 1000 messages are pushed to the 1st consumer, and then the other consumers are stalled because the 1st consumer didn't acknowledge.
But I'm still confused that what makes the difference that the C++ test KeySharedConsumerTest.testMultiTopics is easily to fail with subscriptionKeySharedUseConsistentHashing=true but always succeed with subscriptionKeySharedUseConsistentHashing=false.
I found another problem with subscriptionKeySharedUseConsistentHashing=true. I changed the C++ tests that each consumer calls receive in a separated thread. After that, testMultiTopics never failed even with subscriptionKeySharedUseConsistentHashing=true.
However, the testKeyBasedBatching failed sometimes. In this test, a producer is created to send messages with interleaved keys and key based batching. i.e. send: ABABABAB......
Then, two consumers are created to consume these messages. I expect each consumer can consume a key. However, sometimes it failed with
2022-01-27 20:50:02.410 INFO [0x10e646600] KeySharedConsumerTest:149 | messagesPerConsumer: {0 => 200, 1 => 0}
pulsar-client-cpp/tests/KeySharedConsumerTest.cc:160: Failure
Expected: (fabs(count - expectedMessagesPerConsumer)) < (expectedMessagesPerConsumer * PERCENT_ERROR), actual: 100 vs 50
pulsar-client-cpp/tests/KeySharedConsumerTest.cc:260: Failure
Expected equality of these values:
messagesPerConsumer[i]
Which is: 200
NUM_MESSAGES_PER_KEY
Which is: 100
We can see one consumer received all messages. Is it an expected behavior when subscriptionKeySharedUseConsistentHashing=true? @merlimat
I've tried both solutions above in C++ client and it works well. But I'm confused about why it works, could you explain a little more? @merlimat
While it was clear for the repro code that you share, I couldn't find the problem in the C++ test (since it was acking the messages).
However, the testKeyBasedBatching failed sometimes. In this test, a producer is created to send messages with interleaved keys and key based batching. i.e. send: ABABABAB......
The random behavior is due to the auto-generated consumer names. The selection of consumers is done like:
Take the consumer name and place 100 points in the hash ring
The points are selected like hash(consumerName + "0"), hash(consumerName + "1"), hash(consumerName + "2") ...
For 2 given message keys, you're not guaranteed that they will be assigned to 2 different consumers, although statistically, the keys will be evenly distributed across consumers.
To make the test deterministic, you can set the consumer names (different) on the 2 consumers.
changed the title [-]Message loss with subscriptionKeySharedUseConsistentHashing=true / PIP-119 detected in CPP tests[/-][+]Flakiness issues with subscriptionKeySharedUseConsistentHashing=true / PIP-119 in CPP tests[/+]on Jan 31, 2022
We are facing this issue that yields a constantly increasing backlog with millions of messages. Is there a fix for it or a guidance to implement our consumers? We end-up with 2 out of 16 consumer service pods that can subscribe with the others idling and not receiving any message.
Activity
BewareMyPower commentedon Jan 26, 2022
Yeah, here is a screenshot of my Java consumer application. The topic was created by C++ UT and received 3000 messages from C++ producer. Java consumers should have received 3000 messages in total, and sometimes it works well.
The code is
But I cannot reproduce it with Java UT easily at the moment.
BewareMyPower commentedon Jan 26, 2022
It's weird that it's hard to reproduce it in a unit test but easy to reproduce with a standalone.
Here is my project to reproduce it: https://github.com/BewareMyPower/pulsar-issue-13965-reproduce
/cc @codelipenghui
lhotari commentedon Jan 26, 2022
Great work on the repro case @BewareMyPower !
lhotari commentedon Jan 26, 2022
the problem can be reproduced with Pulsar 2.8.2 and Pulsar 2.7.4 too.
A quick way to start Pulsar 2.8.2 standalone with
subscriptionKeySharedUseConsistentHashing=trueis with this docker command:lhotari commentedon Jan 26, 2022
The result in the repro seems to be the same with
subscriptionKeySharedUseConsistentHashing=false.lhotari commentedon Jan 26, 2022
Also tried by starting the container without
--rmand stopping itthen running
The problem reproduces after restart too.
BewareMyPower commentedon Jan 26, 2022
Yeah. It's weird that I also tried C++ client to consume the topic produce by Java client with
subscriptionKeySharedUseConsistentHashing=false, the message loss still happened.The code above is nearly the same as
Consume.javain my repo. And a sample output is:But when I run C++ UT, it never failed now when
subscriptionKeySharedUseConsistentHashing=false./tests/main --gtest_filter='*testMultiTopics'Here are 5 test results in a row:
merlimat commentedon Jan 26, 2022
The messages should not get acknowledged (and thus lost), right?
merlimat commentedon Jan 26, 2022
I just checked, adding consumer acks, and the messages are there in the backlog, so this shouldn't be characterized
as "message loss".
merlimat commentedon Jan 26, 2022
I think there is a problem with the repro code above and with the original C++ test and the production code is actually correct here.
The repro code is doing:
There are 2 problems in the way the repro code (and the test) are consuming:
What happens is that 1000 messages are pushed to the 1st consumer, and then the other consumers are stalled because the 1st consumer didn't acknowledge.
merlimat commentedon Jan 26, 2022
A couple of ways to fix the test:
BewareMyPower commentedon Jan 27, 2022
Yes, I applied BewareMyPower/pulsar-issue-13965-reproduce@8e85c77 and the test works now.
But I'm still confused that what makes the difference that the C++ test
KeySharedConsumerTest.testMultiTopicsis easily to fail withsubscriptionKeySharedUseConsistentHashing=truebut always succeed withsubscriptionKeySharedUseConsistentHashing=false.BewareMyPower commentedon Jan 27, 2022
I've tried both solutions above in C++ client and it works well. But I'm confused about why it works, could you explain a little more? @merlimat
BewareMyPower commentedon Jan 27, 2022
I found another problem with
subscriptionKeySharedUseConsistentHashing=true. I changed the C++ tests that each consumer callsreceivein a separated thread. After that,testMultiTopicsnever failed even withsubscriptionKeySharedUseConsistentHashing=true.However, the
testKeyBasedBatchingfailed sometimes. In this test, a producer is created to send messages with interleaved keys and key based batching. i.e. send: ABABABAB......Then, two consumers are created to consume these messages. I expect each consumer can consume a key. However, sometimes it failed with
We can see one consumer received all messages. Is it an expected behavior when
subscriptionKeySharedUseConsistentHashing=true? @merlimatmerlimat commentedon Jan 27, 2022
@BewareMyPower
While it was clear for the repro code that you share, I couldn't find the problem in the C++ test (since it was acking the messages).
The random behavior is due to the auto-generated consumer names. The selection of consumers is done like:
For 2 given message keys, you're not guaranteed that they will be assigned to 2 different consumers, although statistically, the keys will be evenly distributed across consumers.
To make the test deterministic, you can set the consumer names (different) on the 2 consumers.
[-]Message loss with subscriptionKeySharedUseConsistentHashing=true / PIP-119 detected in CPP tests[/-][+]Flakiness issues with subscriptionKeySharedUseConsistentHashing=true / PIP-119 in CPP tests[/+]lhotari commentedon Jan 31, 2022
I agree. I renamed the issue. I'll close this issue since it seems to be addressed. @BewareMyPower Please reopen if there's more to do.
lightistor commentedon Jan 19, 2023
We are facing this issue that yields a constantly increasing backlog with millions of messages. Is there a fix for it or a guidance to implement our consumers? We end-up with 2 out of 16 consumer service pods that can subscribe with the others idling and not receiving any message.