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
"the message is deleted from the queue after processing or at the time "start processing" (when the message was delivered cosumer grain"
The intended behavior is that the event is deleted from the queue -after- it is processed. All consumers OnNextAsync calls should be completed on the event, -before- it is deleted from the queue.
If this is not the behavior you are seeing, please let us know, as that would indicate a bug.
PS this strange behavior does not pay attention, I'm just experimenting :)
although if you are interested I can tell you why I decided to experiment with such cycles :)
I have confirmed that there is a case where MessagesDeliveredAsync is called prematurely.
I've a test of the SimpleQueueCache that demonstrates the bug.
Looking into a fix.
@gabikliot Sure. I can try :)
The IQueueCacheCursor was initially modeled off of the IEnumerator interface, with a similar usage pattern. In the IEnumerator usage pattern, one gets an enumerator then loops while MoveNext returns true, getting the current value each loop. In this pattern, MoveNext must be called before the Current value is accessed.
One common implementation of IEnumerator is for the initial enumerator to point to data before the intended first value. For instance consider list 'a,b,c,d,e'. If you wanted an enumerator that enumerated from ‘c’ forward, the initial enumerator's Current would point at ‘b’, so the first MoveNext would return true and move the Current to c, the value you actually wanted first. This approach requires some special case logic for the case where one wants to enumerate from the start of the sequence, since there is no value ‘before’ the first to point at.
Another approach is for the enumerator to keep a reference into the list separate from Current, and use Current simply as a storage property. Using the above example, the initial enumerator would have an initial reference into the sequence pointing at ‘c’, and have no Current value. When the MoveNext was called, it would check to see if the reference pointed to a valid list member, if it did (which it does, ‘c’) it would assign that value to Current, and move the reference to the next item in the sequence (‘d’). This pattern requires no special handling for enumerators starting at the first item in the sequence, but depending on the type of sequence, may require special handling for the last item in the sequence.
Since the queue cache cursor requires special handling of cursors that have reached the end of the sequence of cached items, but did not require special logic for cursors pointing at the start of the cache, the second of the two above approaches fit better, so it was used.
When the cache was updated to prevent items from being purged if they were still in use, we used the cursor’s reference into the cached items sequence to determine if an item was still in use, and hence could not be purged. The problem with this is that the cursor enumerator algorithm I selected moves the cursor’s queue reference forward to the next item on the move next, so the reference is always one ahead of the Current item. The Current item is the one being delivered, but it has not yet been delivered. This means it is available for purge before it has been delivered to the consumer.
This issue was not noticed until now because the cache purges items in blocks, so an individual item being available for purge will not in itself usually trigger a purge.
I see. Thanks for explanation.
The way I see it: Indeed, enumerator can be implemented differently. But we discussed not once in the past that a better approach is to always point to current and move next does what it says: Updates current to next. I think this is not the first bug caused by this discrepancy.
I would consider changing the cache, and having current, has next, and move next always point to the current.
Unfortunately, I've blown through the time I allotted towards looking into this and have yet to devise a clean solution. Hopefully something will come to me, but higher priority issues need my attention. I'll revisit this as time allows. Community help would be appreciated.
@jason-bragg
I believe I'm encountering this issue.
I'm using Orleans 2.4.0, and subscribing a stateless grain implicitly to an SQS stream. MessagesDeliveredAsync is called before the subscriber function returns (in the same way Merroy described in their original post). This causes the message to be deleted from the queue before I can guarantee it has been processed correctly.
Would anyone be able to tell me what this issue's current status is, and why it was marked as closed?
I believe this is still an issue with any stream provider using the Simple Queue Cache (which SQS does). It was closed as it's a fairly narrow edge case, which was never prioritized. It only occurs if all of the streams on a partition become idle (that is, none have more data to deliver).
Having said that, it is a bug that should be addressed as it can show up regularly in low throughput streaming applications.
Not really. I have an ugly workaround, but it's pretty invasive and will definitely impact performance. Hopefully I should be able to do something for 4.0, but backporting to 3.x would be painful
It's still in the backlog, not a priority right now for 4.0.
There is multiple issues regarding using of Azure Queue and SQS with streaming. If you can open a PR with a better implementation of the SimpleQueueCache I would be happy to review it
@benjaminpetit is there any way this can be prioritized?
We are still seeing multiple instances where we are losing stream messages.
In this case, Orleans is taking something which is very durable(i.e message queues) and making it far worse.
I'm not very well versed with SimpleQueueCache or Orleans to contribute effectively here
@ReubenBond just wanted to bring this on your radar
we're continuing to see this issue with SimpleQueueCache and lose messages particularly when silos are shutting down
Current main still reproduces this when BatchContainerBatchSize > 1: the pulling agent advances each SimpleQueueCache cursor across all containers before awaiting the combined consumer delivery. That can leave the oldest bucket with no cursor while one or more consumers are still processing, so a concurrent queue read purges the bucket and calls MessagesDeliveredAsync prematurely.
PR #10655 fixes the invariant by pinning the earliest cache bucket for the lifetime of each combined delivery. Every consumer holds its own pin, so acknowledgement becomes possible only after the final in-flight consumer completes. A failed combined delivery marks every contained cache item for redelivery.
The focused regression failed on unmodified main because MessagesDeliveredAsync was observed while two consumers were blocked. It passes with the fix on both net8.0 and net10.0; all 19 PersistentStreamPullingAgentTests pass on each framework, and dotnet build Orleans.slnx -bl completes with zero errors.
Activity
Merroy commentedon Oct 13, 2015
Merroy commentedon Oct 13, 2015
the problem of block 6
I think in the diagram you can see what the problem is
provide the implementation of the "cyclical nature" below
PS this strange behavior does not pay attention, I'm just experimenting :)
although if you are interested I can tell you why I decided to experiment with such cycles :)
jason-bragg commentedon Oct 15, 2015
I have confirmed that there is a case where MessagesDeliveredAsync is called prematurely.
I've a test of the SimpleQueueCache that demonstrates the bug.
Looking into a fix.
gabikliot commentedon Oct 15, 2015
Jason, can you explain how this happens?
jason-bragg commentedon Oct 15, 2015
@gabikliot Sure. I can try :)
The IQueueCacheCursor was initially modeled off of the IEnumerator interface, with a similar usage pattern. In the IEnumerator usage pattern, one gets an enumerator then loops while MoveNext returns true, getting the current value each loop. In this pattern, MoveNext must be called before the Current value is accessed.
One common implementation of IEnumerator is for the initial enumerator to point to data before the intended first value. For instance consider list 'a,b,c,d,e'. If you wanted an enumerator that enumerated from ‘c’ forward, the initial enumerator's Current would point at ‘b’, so the first MoveNext would return true and move the Current to c, the value you actually wanted first. This approach requires some special case logic for the case where one wants to enumerate from the start of the sequence, since there is no value ‘before’ the first to point at.
Another approach is for the enumerator to keep a reference into the list separate from Current, and use Current simply as a storage property. Using the above example, the initial enumerator would have an initial reference into the sequence pointing at ‘c’, and have no Current value. When the MoveNext was called, it would check to see if the reference pointed to a valid list member, if it did (which it does, ‘c’) it would assign that value to Current, and move the reference to the next item in the sequence (‘d’). This pattern requires no special handling for enumerators starting at the first item in the sequence, but depending on the type of sequence, may require special handling for the last item in the sequence.
Since the queue cache cursor requires special handling of cursors that have reached the end of the sequence of cached items, but did not require special logic for cursors pointing at the start of the cache, the second of the two above approaches fit better, so it was used.
When the cache was updated to prevent items from being purged if they were still in use, we used the cursor’s reference into the cached items sequence to determine if an item was still in use, and hence could not be purged. The problem with this is that the cursor enumerator algorithm I selected moves the cursor’s queue reference forward to the next item on the move next, so the reference is always one ahead of the Current item. The Current item is the one being delivered, but it has not yet been delivered. This means it is available for purge before it has been delivered to the consumer.
This issue was not noticed until now because the cache purges items in blocks, so an individual item being available for purge will not in itself usually trigger a purge.
gabikliot commentedon Oct 16, 2015
I see. Thanks for explanation.
The way I see it: Indeed, enumerator can be implemented differently. But we discussed not once in the past that a better approach is to always point to current and move next does what it says: Updates current to next. I think this is not the first bug caused by this discrepancy.
I would consider changing the cache, and having current, has next, and move next always point to the current.
jason-bragg commentedon Oct 20, 2015
Unfortunately, I've blown through the time I allotted towards looking into this and have yet to devise a clean solution. Hopefully something will come to me, but higher priority issues need my attention. I'll revisit this as time allows. Community help would be appreciated.
lgnashold commentedon Aug 16, 2019
@jason-bragg
I believe I'm encountering this issue.
I'm using Orleans 2.4.0, and subscribing a stateless grain implicitly to an SQS stream. MessagesDeliveredAsync is called before the subscriber function returns (in the same way Merroy described in their original post). This causes the message to be deleted from the queue before I can guarantee it has been processed correctly.
Would anyone be able to tell me what this issue's current status is, and why it was marked as closed?
Thank you!
jason-bragg commentedon Aug 17, 2019
I believe this is still an issue with any stream provider using the Simple Queue Cache (which SQS does). It was closed as it's a fairly narrow edge case, which was never prioritized. It only occurs if all of the streams on a partition become idle (that is, none have more data to deliver).
Having said that, it is a bug that should be addressed as it can show up regularly in low throughput streaming applications.
6 remaining items
pawanrao commentedon Mar 14, 2022
Hi @benjaminpetit Any update on a fix here?
benjaminpetit commentedon Mar 17, 2022
Not really. I have an ugly workaround, but it's pretty invasive and will definitely impact performance. Hopefully I should be able to do something for 4.0, but backporting to 3.x would be painful
pawanrao commentedon Jun 7, 2022
Hi @benjaminpetit Is this something that is still being considered for 4.0?
we are still seeing message loss when using persistent queues
benjaminpetit commentedon Jun 8, 2022
It's still in the backlog, not a priority right now for 4.0.
There is multiple issues regarding using of Azure Queue and SQS with streaming. If you can open a PR with a better implementation of the SimpleQueueCache I would be happy to review it
pawanrao commentedon Dec 14, 2022
@benjaminpetit is there any way this can be prioritized?
We are still seeing multiple instances where we are losing stream messages.
In this case, Orleans is taking something which is very durable(i.e message queues) and making it far worse.
I'm not very well versed with SimpleQueueCache or Orleans to contribute effectively here
pawanrao commentedon Feb 7, 2025
@ReubenBond just wanted to bring this on your radar
we're continuing to see this issue with SimpleQueueCache and lose messages particularly when silos are shutting down
any chance this can be fixed?
ReubenBond commentedon Aug 18, 2026
Current
mainstill reproduces this whenBatchContainerBatchSize > 1: the pulling agent advances eachSimpleQueueCachecursor across all containers before awaiting the combined consumer delivery. That can leave the oldest bucket with no cursor while one or more consumers are still processing, so a concurrent queue read purges the bucket and callsMessagesDeliveredAsyncprematurely.PR #10655 fixes the invariant by pinning the earliest cache bucket for the lifetime of each combined delivery. Every consumer holds its own pin, so acknowledgement becomes possible only after the final in-flight consumer completes. A failed combined delivery marks every contained cache item for redelivery.
The focused regression failed on unmodified
mainbecauseMessagesDeliveredAsyncwas observed while two consumers were blocked. It passes with the fix on bothnet8.0andnet10.0; all 19PersistentStreamPullingAgentTestspass on each framework, anddotnet build Orleans.slnx -blcompletes with zero errors.