Skip to content

QueueCacheMissException #844 Fix - #849

Merged
gabikliot merged 2 commits into
dotnet:masterfrom
jason-bragg:CacheCursorRefreshFix
Sep 30, 2015
Merged

gabikliot merged 2 commits into
dotnet:masterfrom
jason-bragg:CacheCursorRefreshFix

Conversation

@jason-bragg

Copy link
Copy Markdown
Contributor

The cache cursor for the first stream is becoming inactive while there is data still being delivered on the second stream. Since data is being delivered on the second stream the cache is moving forward in time and when we try to refresh the cache cursor for the first stream, it is older than any of the data in the cache, so it throws a cache miss.

When we refresh a cursor, if it's token is no longer in the cache, we start from the oldest data in the cache.

The cache cursor for the first stream is becoming inactive while there is data still being delivered on the second stream. Since data is being delivered on the second stream the cache is moving forward in time and when we try to refresh the cache cursor for the first stream, it is older than any of the data in the cache, so it throws a cache miss.

When we refresh a cursor, if it's token is no longer in the cache, we start from the oldest data in the cache.
@gabikliot

Copy link
Copy Markdown
Contributor

Jason, you will have to explain it better to me. I don't dig this change. Maybe concrete example with sequence tokens will help.
Also, in the new code MoveNext can throw and it is OK, and we just ignore and move on? I don't get it. Or maybe we don't ignore? I don't understand that logic.

@jason-bragg

Copy link
Copy Markdown
Contributor Author

"in the new code MoveNext can throw and it is OK"
In the old code, the MoveNext was in the while loop and if it threw we didn't handle it as we handled other errors. In the new code MoveNext is called in the same try as GetCurrent, so if a cache miss (or any other exception) is thrown in MoveNex it is handled the same as errors from GetCurrent.

"You will have to explain it better to me"
The change to MoveNext is not the primary fix. It is a related bug I encountered when testing.
The primary fix is in the cache and cache cursor.
Scenario:

  1. Send events on stream 1. (Sequence tokens, 1,2,3)

  2. All events on stream 1 are consumed and purged from cache.

  3. Stream 1's consumer's cursor becomes inactive and is set to next message that should be added to the cache. (sequence token 4)

  4. Send events on stream 2. (Sequence tokens 4,5,6)

  5. All events on stream 2 are consumed and purged from cache.

  6. Stream 2's consumer's cursor becomes inactive and is set to next message that should be added to the cache. (sequence token 7)

  7. Send more events on stream 1. (Sequence tokens, 7,8,9)

  8. When new data shows up on stream 1, it's cursor is reinitialized and tries to find the next event in the stream at or after its Sequence token (4). However, it's sequence token is below the lowest value in the cache (7), so we get a cache miss.

The fix is to change the cursor and cache interaction such that, when reinitializing an existing cursor (not a new cursor), we make a best effort to read from the cursor's value, but if that fails, we try from the start of the cache.

@gabikliot

Copy link
Copy Markdown
Contributor

Thanks! I will take detailed look at that.

@gabikliot

Copy link
Copy Markdown
Contributor

Ok, Thanks! I got it now. Very detailed and good explanation.

  1. Makes sense about MoveNext and the fact it is totally unrelated to the other change. You know how much I like bundled unrelated changes.
  2. Got it about sequence token. I would like to suggest 2 improvements:
    a) if we stick with the current fix for now (which we probably should, given timing constrains), can we still revert the argument from best effort to "enforceSequenceToken", so the logic will be reversed and also not use default value but explicitely pass true or false everywhere. I think it will make comprehension and code maintenance easier.
    b) longer term we should review this whole "next token" approach, maybe switch to last consumed token approach. Plus review it in light of the MoveNext/GetCurrent, like we discussed in the past, change it to HasNext and MoveNext that returns void and can never fail.

@gabikliot

Copy link
Copy Markdown
Contributor

And its great you added the test that reproes it.

@jason-bragg

Copy link
Copy Markdown
Contributor Author

@gabikliot
2.a - +1, will update PR.
2.b - "and can never fail" needs more discussion. Design has always included the possibility of cursors becoming invalid at any time. This detail was not arbitrary.

@jason-bragg

Copy link
Copy Markdown
Contributor Author

"bundled unrelated changes" - Ninjaneering for the win!! :)

@gabikliot

Copy link
Copy Markdown
Contributor

Re: 2.b - "and can never fail" needs more discussion
That is what HasNext will be for. So logically, at high level, of course cursor can fail. But code wise, you will have one place to ask IsItValid and that will be HasNext. And that should allow now to write MoveNext and GetCurrent that indeed can never fail.
The situation would be radically different if that was a multi threaded code. But due to the awesomeness of Orleans and its single threaded execution, all those 3 calls will be in the same turn, so one check should be enough.

- InitializeCursor bestEffort was replaced with enforceQuenceToken and default value was dropped.
gabikliot added a commit that referenced this pull request Sep 30, 2015
@gabikliot
gabikliot merged commit 9795912 into dotnet:master Sep 30, 2015
@jason-bragg
jason-bragg deleted the CacheCursorRefreshFix branch October 2, 2015 18:26
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants