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

   # Description
   
   [CAMEL-27434](https://issues.apache.org/jira/browse/CAMEL-27434)
   
   The Pub/Sub API consumer resubscribes when its subscription stream fails. 
CAMEL-24160 added a check of the client
   status before and after the back-off sleep, so a stop during the sleep is 
handled. Three cases are still open:
   
   1. **A route stop racing a resubscribe leaves a subscription open.** The 
resubscribe passes the check after the sleep,
      and `asyncStub.subscribe` starts the new call. The route is stopped at 
this point: `PubSubApiClient.doStop`
      half-closes only the streams that are already in `observerMap`, and the 
new stream is not there yet.
      `channel.shutdown()` lets calls that already exist go on, and the result 
of `awaitTermination` is ignored. Then the
      resubscribe puts the stream into the map and sends the fetch request. The 
server delivers events on a stream that
      nobody closes: they go to the stopped consumer, and after the route is 
started again (a new client for the same
      consumer) every event is processed twice. The channel does not terminate 
while that stream is open.
   2. **A stream ended by the server leaves the consumer without a 
subscription.** `FetchResponseObserver.onCompleted`
      only removes the stream from the map. There is no resubscribe and nothing 
is reported, so the route looks started
      but receives nothing more. I could not check how often the Salesforce 
service, or a proxy in front of it, ends a
      subscribe stream with status OK, but when it does the consumer stops 
silently.
   3. **A failed call can replace the live stream in `observerMap`.** The map 
is keyed by consumer and `onError` removes
      by key. If the call of a resubscribe fails right away and its own error 
callback resubscribes before the first
      thread reaches `observerMap.put` (that thread must stall for longer than 
the back-off), the failed stream overwrites
      the live one. A later, ordinary stop half-closes only the failed stream, 
and the live one stays open as in 1. This
      needs a long pause of one thread, so it is rare.
   
   The change, all in `PubSubApiClient`:
   
   - `subscribe()` holds a lock from a check of the client status until the 
stream is stored and the fetch request is
     sent. `doStop()` takes the same lock before it half-closes the streams in 
the map. The status is set to stopping
     before `doStop()` runs, so a resubscribe either stores its stream before 
`doStop()` closes the open streams, or it
     sees the client stopping and does not subscribe. The back-off sleep and 
the login after an auth error stay outside
     the lock.
   - `onError` and `onCompleted` remove only their own stream from the map 
(`remove(consumer, stream)`). If a call has
     already ended when `subscribe()` stores it, `subscribe()` removes it again 
and sends no fetch request; the callback of
     that call does the resubscribe. So a failed stream never replaces the live 
one.
   - `onCompleted` while the client is started resubscribes the same way as 
`onError`: the same back-off and the same
     replay id (the last one received, else the initial preset or replay id). 
It logs one WARN line naming the topic.
     When the client is stopping, `onCompleted` is the server ending a stream 
that `doStop()` half-closed, and nothing
     happens, as before.
   - When `awaitTermination` times out, `doStop()` logs one INFO line and calls 
`channel.shutdownNow()`, which cancels
     the calls that are left, for example when the server does not end a 
half-closed stream. The producer uses the same
     client class, so a publish that is still running 10 seconds after the 
producer stops is cancelled too.
   - `onError` of a call that ends while the client is stopping (a call 
cancelled by `shutdownNow()`, or a half-closed
     stream the server ends with an error) logs one DEBUG line and returns. 
Before, it logged the gRPC error at ERROR
     level and, for an auth error code, ran the login loop after the route had 
stopped; the resubscribe was already
     skipped.
   
   No option or public method changed. Lock contention is limited to the 
moments a subscription is opened or the client
   stops.
   
   Not changed here, on purpose:
   
   - Events queued on the consumer worker pool (`consumerWorkerPoolEnabled`) 
can still be dropped when the route stop
     waits longer than the executor's shutdown timeout. That is the generic 
worker-pool shutdown of Camel, not specific
     to this consumer.
   - The replay id still moves to the end of a batch once the events are handed 
to the route, before their exchanges
     complete. The consumer's replay id lives in memory and is not reused after 
a restart, so this is its at-most-once
     design, and a failed exchange is reported by the consumer callback.
   - A resubscribe with the default `replayPreset=LATEST` before the first 
response, and the opt-in
     `fallbackToLatestReplayId`, can skip events published in between. Both are 
as designed.
   
   Found with a TLA+ model of the client and consumer: the gRPC callbacks, the 
resubscribe thread (check, sleep,
   subscribe in its steps), stop and start with a new client each time, server 
errors, server completion, crashes and
   the worker pool. On main, "no open stream and no event processed after stop" 
and "at most one active subscription"
   are violated through the stop race, and "while started and quiet, exactly 
one live subscription" is violated in two
   steps by a server completion. With the change (the code as written, not a 
simplified lock) all four hold: 48,139
   distinct states with two starts and one error, and 17,959,556 with three 
starts, two errors, a crash and two worker
   threads. "No open stream after stop" also holds when the server ignores the 
half-close (the forced shutdown).
   TLC and everystate agree on the smaller runs.
   
   Tests: new `PubSubApiClientResubscribeTest` runs the real client against an 
in-process fake Pub/Sub gRPC server. A
   client interceptor holds the resubscribing thread inside 
`asyncStub.subscribe`, and the test releases it once the other
   thread has reached the point that matters, read from the thread's state (no 
sleeps):
   
   - a stop while the resubscribe opens its call: no stream is open after stop, 
nothing reaches the stopped consumer,
     and after a restart one event is processed once;
   - the server ends the stream: the client subscribes again within the bound, 
from the last replay id, and receives the
     next event;
   - the call of a resubscribe is rejected at once and its callback 
resubscribes while the first thread is held: a
     normal stop leaves no stream open;
   - the server ends the half-closed stream of a stop with an auth error: no 
login and no new subscription.
   
   The forced shutdown after the 10 second `awaitTermination` has no unit test, 
since the test would have to wait those
   10 seconds; the model covers it (a server that ignores the half-close), and 
I ran it once by hand against the fake
   server: stop returned after 10 seconds, the server saw the stream cancelled, 
and only the INFO line was logged.
   
   On main the four tests fail. The camel-salesforce unit tests pass (222 run, 
0 failures, 1 skipped). The new class 20 times in one JVM: 80 run, 0 failures.
   
   # 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-27434](https://issues.apache.org/jira/browse/CAMEL-27434).
   
   # 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 and tested the affected module, including the formatter and 
import-sort plugins. No generated file changes.
     I did not run the full root build.)
   
   # 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