JetStream ordered consume stability fixes #202
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Because ordered consume operations use ephemeral consumers it's important to be able to create new consumers when things go wrong. There are two major events that might strongly indicate we won't be able to find our consumer on the server: disconnects and idle heartbeat timeouts.
With this approach introduced with this fix, we recreate the consumer on server disconnects and idle heartbeat timeouts, making sure the consumer can carry on from where it's left off (sequence state is maintained when this happens).
Justification for large code duplication: NatsJSOrderedConsume.cs is a copy of NatsJSConsume.cs subscription class. Reason for this is to maintain stability of the normal consumer since the main behaviour is fairly different. There is also a chance the behaviours might diverge even greater as we discover other issues. We may consider to merge these classes in a tidy-up effort later on.
We also introduced a general timeout (same as connection request timeout) for all JetStream API calls. We needed this because the consumer deletion process was sometimes hanging due to the server receiving the request being killed and never sending a response back. Before this we were relying on the CommandTimeout (which is 1 minute by default) to kick in. Now we use RequestTimeout (5 seconds by default) on the subscription waiting for the reply.