allthingssecurity opened a new pull request, #30829:
URL: https://github.com/apache/camel/pull/30829

   # Description
   
   [CAMEL-27495](https://issues.apache.org/jira/browse/CAMEL-27495)
   
   With `synchronousPull=true`, each loop of `GooglePubsubConsumer` builds a 
`SubscriberStub` for every pull, and that loads the credentials of the 
endpoint. When they cannot be loaded (a service account key file that was 
removed or rotated, Application Default Credentials not available, a metadata 
server that does not answer), `getSubscriberStub` throws `IOException`. The 
loop logged it at ERROR with the stack trace ("Reconnecting.") and pulled again 
at once, with no delay and no limit. With a missing key file one loop made 
about 44,000 attempts per second, each with a stack trace in the log (about 40 
MB of log in 1.3 s), and kept a CPU core busy, until the credentials came back. 
The asynchronous consumer waits 5 seconds after a recoverable error.
   
   A retryable `ApiException` returned by a pull was also pulled again at once. 
With the default pull settings the client library only returns it after 
retrying it itself for up to a minute, so that path is not a tight loop in 
practice, but a pull that fails at once with a retryable code (for example a 
code added with `synchronousPullRetryableCodes`) spins the same way.
   
   Change:
   - after a failed attempt (the `IOException` and the retryable `ApiException` 
paths), the synchronous loop waits until the retry delay has passed since the 
attempt started, then pulls again. The delay is the 5 seconds of the 
asynchronous consumer, now one field that both loops use. An attempt that 
already took that long (a retryable error that the client library retried for a 
minute) is followed by the next pull at once, so there is no extra delay after 
a long poll. So a loop makes at most one failed attempt per 5 seconds.
   - the wait is a `BackgroundTask` on the consumer's `PubSubReconnectTask` 
scheduler, as in the asynchronous loop. It is skipped when the consumer no 
longer pulls, and a stop of the consumer (route stop, graceful shutdown) ends a 
wait in progress at once: the waits are kept in a set that `doStop` and 
`deferShutdown` cancel. Without that, a stop of the CamelContext during the 
wait left the loop waiting on a scheduler that `doShutdown` had shut down, 
until the executor's graceful shutdown timed out (10 s by default) and 
interrupted it with an `InterruptedException` stack trace in the log. An 
interrupt during the wait still ends the loop (as an interrupt during the pull 
does) instead of retrying.
   - logging: the first failed attempt in a row is logged at ERROR with its 
stack trace, as before; the next ones in one WARN line with the count and the 
exception (the stack trace at DEBUG); the first pull that works after failed 
attempts at INFO.
   - `PubsubConsumerDstTest` adds `org.apache.camel.support.task` to 
`@DstInstrument(alsoRewrite)`, as the camel-kafka DST test does, so the wait of 
`BackgroundTask.run` is a virtual wait in the DST world (its faults include 
failed pulls).
   
   Behaviour for users: a consumer whose credentials cannot be loaded now 
retries every 5 seconds instead of continuously; nothing to configure, so no 
upgrade guide entry.
   
   Not changed here:
   - a pull that ends the synchronous loop (a non-retryable error such as 
`PERMISSION_DENIED`), the asynchronous loop that ends after a non-retryable 
error or an `IOException` from `getSubscriber`, an `Error` in the loop, and the 
health check that stays UP while no loop runs: a sibling PR (CAMEL-27496) 
restarts such a loop after the same delay and adds a consumer health check. The 
two PRs touch the same file; their shared hunks are identical, and whichever is 
merged second needs a rebase with one import line to resolve.
   - the existing 5 second wait of the asynchronous loop after a retryable 
error is not ended by a stop either (a stop of the CamelContext during it waits 
for the executor's graceful shutdown); I left it as it is here and will file a 
follow-up ticket.
   - every pull and every ack or nack still builds a new stub with a new gRPC 
channel and loads the credentials again. That is a cost, not a leak (the stubs 
are closed); I will file a follow-up ticket to reuse the stub of a loop.
   
   Test: new `GooglePubsubConsumerSyncPullRetryDelayTest` (package-private, no 
sleep, retry delay 1 s through a package-private setter):
   - `credentialsThatCannotBeLoadedAreRetriedAfterTheDelay`: 
`getSubscriberStub` throws `FileNotFoundException` (what loading a missing key 
file throws) until the test clears it: at most 4 attempts within 2 seconds, 
then the message is received.
   - `pullFailingAtOnceWithRetryableErrorIsRetriedAfterTheDelay`: the pull 
fails at once with `UNAVAILABLE` (retryable) until cleared: the same bounds.
   - `stopDuringTheRetryDelayEndsTheWait`: credentials that cannot be loaded 
and a retry delay of one minute; the route is stopped while the loop waits: the 
wait (a `PubSubRetryDelay` task in the task registry) is gone within 5 seconds 
and there was no second attempt.
   
   All three fail on main (thousands of attempts within the 2 seconds, and no 
wait to end); that check ran with the current consumer plus only the test 
setter. The stop test also fails with the first version of this change, whose 
wait a stop did not end. With the change the unit tests of camel-google-pubsub 
pass (49 run, 0 failures, 10 skipped that need the emulator; the DST test 
passes its 1000 seeds) and the new class passes 20 of 20 runs. The ITs of the 
module were not run locally (CI runs them).
   
   A TLA+ model of the pull loops (sync and async, stop/start, credential 
failures, retryable and non-retryable errors, the route health) found it: 
"failed attempts per loop per tick are bounded" fails on main, and passes with 
the modelled backoff.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   - [x] If this is a large change, bug fix, or code improvement, I checked 
there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for 
the change (usually before you start working on it): 
[CAMEL-27495](https://issues.apache.org/jira/browse/CAMEL-27495).
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [ ] I have run `mvn clean install -DskipTests` locally from root folder 
and I have committed all auto-generated changes.
     (I built camel-google-pubsub and the core modules with the formatter and 
import-sort plugins and ran the module's unit tests, offline, so with 
google-cloud-pubsub 1.160.0, the version before the latest dependency bump. I 
did not run the full root build. No option or generated file changed.)
   
   # AI-assisted contributions
   
   - [x] If this PR includes AI-generated code, commits have proper 
co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR 
description identifies the AI tool used.
     This PR was prepared with Claude Code (Claude Opus 5.5). The commit 
carries a `Co-Authored-By` trailer.
   
   _Claude Code on behalf of allthingssecurity_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to