Repository navigation
Stop the unary RPC client crash-looping on exactly-once ack failures - #119
reidmorrison wants to merge 6 commits into
Conversation
On exactly-once subscriptions, Pub/Sub rejects expired or invalid ack ids with INVALID_ARGUMENT and an ErrorInfo detail (reason EXACTLY_ONCE_ACKID_FAILURE) listing them per ack id. ErrorClassifier treats INVALID_ARGUMENT as terminal, so UnaryRpcClient stopped before looking at the details. AckBatcher then sent the same batch to the restarted client, which got the same answer and stopped again, on every flush until the ack retry deadline (600s under exactly-once). A terminal status that carries per-ack-id details is now handled like a retryable one: the permanently failed ids are counted on the :permanent_failure telemetry event and the client keeps running. A terminal status without per-ack-id details still stops it.
- AckResult.parse_error_details/1 now reads only ErrorInfo details whose reason is EXACTLY_ONCE_ACKID_FAILURE. Before, any ErrorInfo with metadata (for example IAM_PERMISSION_DENIED with a "permission" key) looked like a per-ack-id answer, so PERMISSION_DENIED and other terminal errors kept the client running silently instead of stopping it. The parser now owns the rule for what counts as a per-ack-id answer. - Log a warning when a terminal status is handled as per-ack-id rejections, so these failures stay visible. - Document that :permanent_failure counts rejected requests, not unique ack ids, since rejected ids are still resent until the ack retry deadline. The producer docs said they were "dropped and not retried", which was wrong. - Stop the new test from asserting that "ack-expired" is retained, which is a known limitation, not intended behavior. - Fix AckResult tests to use the real ErrorInfo reason, and add tests for a general ErrorInfo being ignored and for PERMISSION_DENIED still stopping the client.
49634df to
2b0aa06
Compare
rockneurotiko
left a comment
There was a problem hiding this comment.
I was getting some weird feelings reviewing the PR, because I remember doing a research on official pubsub libraries and did not see anything about that kind of error, so I did the research again, and I think we should try to emulate what they do, I'll leave here my research but feel free to do one on your own:
I checked how the official Go (pubsub/v2/[iterator.go](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/pubsub/v2/iterator.go)) and Node.js (src/[message-queues.ts](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/src/message-queues.ts), src/[ack-metadata.ts](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/src/ack-metadata.ts)) clients handle these cases. They agree with each other, so there's a good model to copy.
What the other clients do
- Per-ack-id results: both read the
ErrorInfometadata map asack_id => code.TRANSIENT_*is retried.- Any other value fails that id immediately, and it is never resent.
- An id that is in the request but not in the metadata, with no RPC error, counts as a success.
- So valid ids in a partly rejected request are not penalised.
- Only the transient ids are re-sent. The batch isn't retried as a whole. Retries use a 1s to 64s jittered backoff, with an overall deadline of 600s in Go and 10 minutes in Node.
- Errors with no per-id details: the RPC status code decides.
DEADLINE_EXCEEDED,RESOURCE_EXHAUSTED,ABORTED,INTERNALandUNAVAILABLEare retried.- Anything else, such as
PERMISSION_DENIED, fails all the ids with that error. - Neither client crashes or restarts on a terminal status. They report the failure per message and keep going.
- Receipt modack under exactly-once:
- A message is delivered only if its receipt modack succeeds.
- A transient failure is retried a few times (3 in Node, 4 in Go).
- A permanent failure, or running out of retries, drops the message so the server redelivers it.
- The other ids in the same request are unaffected.
- One difference from this PR: neither client checks
reason == EXACTLY_ONCE_ACKID_FAILURE. Your check is the safer choice here, because it stops IAM metadata keys being read as ack ids. I'd keep it.
Suggested follow-up
Rather than a {retry_ids, failed_ids} split, I'd return a per-id outcome from the unary client, for example %{ack_id => :ok | :transient | {:permanent, reason}}.
chunked_rpc/3merges the per-chunk maps instead of collapsing a failed chunk into "retry everything".AckBatcherretries only:transientids and drops:okand{:permanent, _}ids. That stops the resends until the deadline, and:permanent_failuretelemetry would then count each id once.- The receipt-modack path in
StreamManagerdelivers:okmessages and drops permanent ones. For transient ones it either retries a bounded number of times or drops them. A chunk with one expired id then no longer loses all its valid messages. - Ids in the request but absent from the metadata resolve to
:okwhen there is no error, as in Go and Node. When there is an RPC error, the status code decides, as above. - Longer term, terminal statuses without per-id details could fail the ids and keep the client running, as the official clients do, instead of stopping it. That removes the restart path altogether.
This also settles your concern about the shared {:ok, failed_ids} contract. Receipt modacks would use the same per-id map, so permanent and transient failures stay distinguishable and nothing gets delivered whose modack failed.
| Logger.warning( | ||
| "Cloud Pub/Sub rejected some ack ids while trying to #{operation} via gRPC - reason: #{inspect(error)}" | ||
| ) |
There was a problem hiding this comment.
I'm not a big fan of having logs in libraries, at least for warning messages, I prefer to have telemetry events and let the users decide what to do with that information.
If we keeep the logs, it would be better to have the permanent and transient counts than the raw reason
The ids Pub/Sub rejects are already counted on the :permanent_failure telemetry event, and the RPC error itself is available to telemetry handlers, so the library does not log a warning for them.
|
Thanks for the research, it's a much better model to follow. I read the Go and Node sources as well; they match what you found, with three small corrections, which I've listed in #121. The most important one: when the RPC returns an error, ids missing from the I've written up the per-ack-id outcome design in #121, along with four decisions I'd like your view on before I build it. I'd keep this PR to the crash-loop fix (now without the warning log) and do the rest in follow-up PRs, if that works for you. |
…nce-ack-id-failure # Conflicts: # CHANGELOG.md
On an exactly-once subscription, Pub/Sub answers an Acknowledge (or ModifyAckDeadline) that contains expired or invalid ack ids with
INVALID_ARGUMENTand agoogle.rpc.ErrorInfodetail (reasonEXACTLY_ONCE_ACKID_FAILURE) whose metadata lists the failed ids, for examplePERMANENT_FAILURE_INVALID_ACK_ID.ErrorClassifiertreatsINVALID_ARGUMENTas terminal, soUnaryRpcClient.handle_rpc_error/4stops the process before it reads those details.AckBatcherkeeps the batch and sends it again to the restarted client, which gets the same answer and stops again. This repeats on every flush until the ack retry deadline, which is 600s under exactly-once.What we saw
With the StreamingPull producer on an exactly-once subscription, one pipeline's
UnaryRpcClientstopped with{:terminal_error, %GRPC.RPCError{status: 3, message: "Some acknowledgement ids in the request were invalid. This could be because the acknowledgement ids have expired or the acknowledgement ids were malformed.", details: [%Google.Protobuf.Any{type_url: "type.googleapis.com/google.rpc.ErrorInfo", ...}]}}about 4, 12, 19 and then 33 times a minute in the first minutes after a deploy, each time with an{:acknowledge, ...}fromAckBatcheras its last message. The application stayed up, but the log filled with crash reports and the same ack batches were resent until their retry deadline.The change
handle_rpc_error/4now parses the per-ack-id details first. A terminal status that carries them is handled like a retryable status with details: the permanently failed ids are counted on[:broadway_cloud_pub_sub, :streaming, :unary, :permanent_failure], and the client replies with the error and keeps running. A terminal status without per-ack-id details (for exampleNOT_FOUND) still stops it, as before.Only an
ErrorInfowhose reason isEXACTLY_ONCE_ACKID_FAILUREcounts as per-ack-id details;AckResult.parse_error_details/1now ignores any otherErrorInfo, so a terminal error with a general detail (for examplePERMISSION_DENIEDwith anIAM_PERMISSION_DENIEDErrorInfo) still stops the client. When the client keeps running it does not log anything; the rejected ids are reported only through telemetry. The:permanent_failuredocs now say the count is per rejected request, not per unique ack id, since rejected ids are still resent until the ack retry deadline (see below).Tests cover these cases:
INVALID_ARGUMENTwith anErrorInfodetail (permanent, transient and unlisted ids) keeps the client alive, logs nothing and emits:permanent_failurewith the permanent count;INVALID_ARGUMENTwithout details still stops it; andPERMISSION_DENIEDwith a generalErrorInfostill stops it.A related issue, left out of this PR
When
handle_rpc_error/4works out the ids to retry and replies{:error, {error, transient_ids}},chunked_rpc/3turns any{:error, _}into "retry the whole chunk", soAckBatchernever receives that list.AckBatcher.extract_retained_ids/2has a clause for{:error, {_rpc_error, transient_ids}}, but nothing reaches it. As a result, ids Pub/Sub rejected for good are resent until the retry deadline, for retryable statuses as well as with this fix.I did not change that here because the same
{:ok, failed_ids}result is also used for exactly-once receipt modacks, whereStreamManager.handle_receipt_modack_success/3must not deliver a message whose receipt modack failed, whether permanently or not. Dropping permanent ids from the list would deliver those messages. Separating "retry these" from "these did not succeed" in the result looks like the right fix, but it changes a contract three callers share, so I would like your view on the shape first. Happy to follow up with a PR.Testing
mix format --check-formatted,mix deps.unlock --check-unused,mix compile --warnings-as-errorsandmix testpass (Elixir 1.18.4, OTP 28; three full runs). I saw one unrelated failure in a single earlier full run, instream_manager_test.exs"in exactly-once mode, messages are NOT immediately forwarded to producer" (arefute_receivedracing the receipt modack); it did not reproduce in 14 further runs, with or without this change.