-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for (optional) ack timeout and nack delay for consumers #67
Conversation
…nsumer channel and factory
Hi @dionjansen
We have tried adding support for Visual Studio Code before, but sadly it's just not a pleasant road to go down. I have no idea why Microsoft has created two IDE's for C# and doesn't ensure that they behave the same.
Feel free to add one or more of these (if you need them):
Yes.
The question is if you actually need to consumer or just the consumer channel. The tracking should start and end together with the MessageQueue/ConsumerChannel.
That's a really good question. Bookkeeper stores batched messages as one, so if you have a batched message consisting of 5 messages and you ack 4 of them, but the last times out and you ask the broker to redeliver, you will get the entire batch again. You could keep track of this, but it will hurt performance. I don't know what the other clients do here, but maybe you could test that?
I agree that a timer/task per message will hurt performance too much. Have one task for the entire Consumer/MessageQueue is the right solution. Waking up and looking at what needs to be redelivered. Here we need to find a thread-safe and performant way of storing and accessing this information.
Just a boolean check on dequeue and ack? We also need to handle cumulative acknowledgment. Consider this ackTimeout implementation. First ackTimeout should be giving as a TimeSpan (instead of a an int or long as milli- or micro-seconds). When a message is dequeued, and if we have an ackTimeout, then we store the MessageId and StopWatch.GetTimestamp() in an "AwaitingAck" struct in a ConcurrencyQueue, let's call it "AwaitingAcks". Other suggestions for concurrent collections with fast insertion are welcome. When a message is acknowledged, and if we have an ackTimeout, then we store the acks instead of removing them from "AwaitingAcks". If we want to remove them right away, then we need the "AwaitingAcks" collection to support both iteration and random deletion. We only need to store the highest cumulative ack we see (if there is one) and the MessageIds not included by that cumulative ack. When the ackTracker wakes up and has calculated what ackTimeout is in StopWatch ticks (those are not the same as TimeSpan ticks). It will call StopWatch.GetTimestamp(). We will now TryPeek and Dequeue from "AwaitingAck" for as long as the tracker timestamp - AwaitingAck.Timestamp is larger than the calculated timeout. Consider this nackTimeout implementation. When a message is nacked, we added the MessageId(s) to a CommandRedeliverUnacknowledgedMessages that we are reusing (should our "RedeliverUnacknowledgedMessages" taking an enumerable of messageId actually have been called "NegativeAcknowledge"?). When the nackTracker wakes up it will check if the CommandRedeliverUnacknowledgedMessages has MessageIds and if yes, then send it. Writing such a detailed implementation description was not what I intended, but when I first get going.... :-D |
@blankensteiner I've tried to follow your suggestions and simplify the implementation a bit: I focussed just on the unacked message tracker. Perhaps it is anyway a good idea to separate these two mechanisms (nack tracking and unacked tracking) since they are configured independently of each other.
Added
I am starting the thread now in the Pulsar client pulsar-dotpulsar/src/DotPulsar/PulsarClient.cs Lines 81 to 84 in 26cd957
The tracker is then passed to the channel factory, so it can be passed to a message queue that is passed to the channel when created. The tracker loop is stopped when disposed which occurs when the message queue is disposed, which in terms occurs when the channel is disposed. I'm still using
From what I can see in the
done, I kept the configuration options of the consumer to milliseconds though.
I was struggling a bit to create a comparable TimeSpan (since as you point out stopwatch ticks != timespan ticks). I followed this article and concluded: pulsar-dotpulsar/src/DotPulsar/Internal/UnackedMessageTracker.cs Lines 24 to 25 in f4725f5
Not casting frequency explicitly to double results in considerable loss of accuracy.
How can I determine if there is a highest cumulative ack from a
I tried to capture this in pulsar-dotpulsar/src/DotPulsar/Internal/UnackedMessageTracker.cs Lines 81 to 91 in 51f4a98
I will implement the nack tracker if you are happy with the unacked tracker as it stands (which might also be refactored into one single tracker if two trackers are a performance concern to you).
Let me know what you think, this is still a bit of a learning process for me both on the internals of this lib as well as C# / Pulsar details. Thanks in advance 👍 . |
Hi @dionjansen |
@blankensteiner thanks for the comments I managed to fix most of it with a view side comments/ questions (see above). Let me know what you think |
…edgedMessages using MessageIdData
@blankensteiner sorry for the delay, I addressed all your remarks, let me know what you think! |
@blankensteiner @dionjansen any idea when this work will be finished and merged? Looks like a lot of work has been done and reviewed but wondering what is missing to cross the finish line. |
Hi @jbvanzuylen |
@jbvanzuylen @blankensteiner yes this dropped very far off my radar, unfortunately. I see quite a lot has changed since this implementation, I'll try to open a new PR from the latest version. |
@blankensteiner I reworked the implementation from scratch based on version 1.1.2 and opened a new PR #83. I've also added support for negative acknowledgement delays next to the unacked tracking. Closing this PR since it's no longer needed. CC: @jbvanzuylen |
Implements: #45 , #46
General comments
Discussion
I think we can split up the discussion in 2 parts.
@blankensteiner I think firstly I would like to get some thoughts on the current implementation, I mostly focussed now on setting things up so I don't break existing processes:
Integration
IMessageAcksTracker
instance in thePulsarClient
, though when clients do not have the consumer configured to use the tracking, a dummyInactiveMessageAcksTracker
is passed. This instance is passed to theConsumerChannelFactory
and used when a channel is created. Does this setup makes sense to you?ConsumerChannel
now expects aMessageQueue
which wraps the tracker and theAsyncQueue
. The channel now informs the tracker through this queue when acking (and later nacking which I can add later) and the dequeue method automatically starts tracking messages received.RedeliverUnacknowledgedMessages
. Any ideas on how to improve this pattern? Perhaps a static method like some of theStateMonitor
methods.Implementation
MessageAcksTracker
uses a polling mechanism to re-check for timed out messages (either due to being unacked for too long or the nack delay has been exceeded). Is this (generally) what you were thinking about too? Alternatively I could think of an approach where polling is done through a Timer. Or we could create individual Tasks for each added message to the tracker but I'm concerned of the overhead created by this.