Skip to content
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

Add NATS Ack/Nak to nats jetstream V3 Finish #1104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stephen-totty-hpe
Copy link
Contributor

In some cases, an explicit ACK/NACK may be necessary. Currently, there is no way to NACK a message. Also, in some cases, the NATS server may not automatically ACK a message.

This logic explicitly looks for an error that is protocol.ResultNACK, for when to do a Nak.
If the error is nil or protocol.ResultACK, then a Ack is done.
When an unknown error occurs, the original behavior of return nil is invoked.

Testing with a live system can be done by temporarily modifying:
https:/cloudevents/sdk-go/blob/main/v2/protocol/test/test.go#L41
that is called from:
https:/cloudevents/sdk-go/blob/main/test/integration/nats_jetstream/v3/nats_test.go#L29

@stephen-totty-hpe stephen-totty-hpe requested a review from a team as a code owner October 18, 2024 17:24
err: errors.New("unknown"),
},
wants: wants{
err: nil,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we're slurping the error and returning nil? Is this the correct behavior when finishing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the previous behavior. I am open to changing this behavior if there is support to change it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, good question if we want consistency (but ambiguous behavior) or fix it. What do you think? Perhaps someone using v3 would not use the prior implementations and therefore we should do what's right for users?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking at
https:/cloudevents/sdk-go/blob/main/v2/client/client.go#L263
and
https:/cloudevents/sdk-go/blob/main/v2/client/invoker.go#L59

I believe the error returned from Finish is only used for logging:
cecontext.LoggerFrom(ctx).Warn("Error while handling a message: ", err)

So the real question is more about whether we should call Ack() or Nak() in the unknown error case.
In the case that Ack() or Nak() is not called, the ACK/NACK behavior on the server is based on how the NATS consumer is setup.
There are three options: AckNonePolicy, AckAllPolicy, AckExplicitPolicy

So I would think if you had AckNonePolicy or AckAllPolicy, not calling Ack() would result in the server auto-acking.
If you had AckExplicitPolicy set, then failing to call Ack() would result in the message being redelivered.

This might be correct as it would "default" to how the user setup the consumer.
NATS has a MaxDeliver option which would give up after the sending "MaxDeliver times", but the default is "unlimited".

I am inclined to give the user the benefit of defining that behavior, but I am not married to that feeling.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that sounds reasonable! Feel free to document the behavior e.g., adding a README or code comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

},
},
{
name: "jetstream.ErrMsgAlreadyAckd error returned from Nak",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, there's a case when a (protocol) NACK would contain NATS ACK-ed in this implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nak and ack make the same NATS call deep down with a minor difference in the ackType argument passed via the API. The ErrMsgAlreadyAckd error that comes back simply means that the message has been already "marked" as having been processed. It is used for both ack and nak.

In practice, a message should not be marked ack or nak before calling Finish. But there may be cases where a binding may have a behavior to handle MalformedEvent or a responder may be interjected. In these cases, I believe Finish will still only called once. The error check is simply there to not send back an error in the unlikely case somehow Finish is called more than once.

auto-merge was automatically disabled October 22, 2024 18:54

Head branch was pushed to by a user without write access

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants