allthingssecurity opened a new pull request, #30819: URL: https://github.com/apache/camel/pull/30819
# Description [CAMEL-27517](https://issues.apache.org/jira/browse/CAMEL-27517) A correlation manager is configured as a bean (`correlationManager=#myManager`), so the producers of one endpoint (two routes that send to the same URI, a producer cache) and the producers of every endpoint that refers to the same bean use the same instance. Each producer started it in `doStart` and stopped it in `doStop`, and `ServiceSupport` start and stop are not counted. So when one producer stopped (its route was stopped, for example), a `TimeoutCorrelationManagerSupport` was stopped under the producers that still ran: - the requests of the other producers that waited for their reply were completed with an `ExchangeTimedOutException` by the drain that CAMEL-27335 added to the stop (before CAMEL-27335 they were dropped and never completed), although their channel was open and the reply could still come; - the purge task was cancelled, so the requests the other producers sent next stayed in the map of a stopped manager and never timed out. With a server that does not answer, the exchange waited forever; - when the stopped producer started again, the manager got a new, empty map, and the reply to a state put in the old map was ignored. Change: - `TimeoutCorrelationManagerSupport` counts the producers that use it, with two package-private methods called by `NettyProducer`: `retain()` starts the manager when it is not started and adds a user, `release()` removes one and stops the manager when it was the last. Both run under the lock of the service (the lock `start()` and `stop()` take), so two producers that stop at the same time do not both, or neither, stop it, and a producer that starts while the last one stops waits for the stop and then starts the manager again. - The stop of the last producer drains the requests that still wait, as CAMEL-27335 does for a single producer, so the existing stop tests are unchanged. - A producer releases only a reference it took: when its start fails before that (for example `lazyChannelCreation=false` and no server), the stop that `BaseService.start` makes does not stop the manager of the others, also when the producer used it in an earlier start. - `putState` fails the request with an `IllegalStateException` ("The correlation manager is not started") when the manager or its timeout map does not run, for example a bean stopped by the application while a producer runs. Before, the state was put in the map of a stopped manager and never timed out. The map is checked as well because a manager that starts again still has the stopped map of its previous start until its start replaced it. - `putState` checks again after the put. If the manager stopped meanwhile, its stop drained the map either before the put (then the state is in a map that the stop cleared, or is about to clear, and that no purge task looks at any more) or after it (then the drain completes the state with a timeout); if the manager also started again, the state is in the map of the previous start, and the reply would be looked up in the new one. In all these cases `putState` takes the state back (`remove`, which finds nothing when the drain or the clear already took it) and claims the completion of the exchange with `NettyCamelState.markDone()`, the same claim the eviction task, the reply and the write failure make. If it wins, it fails the request with the `IllegalStateException` and the producer completes the exchange with it; the eviction task of the drain then leaves the exchange alone. If the drain already completed it, `putState` returns normally and does not touch it. So the exchange is com pleted exactly once. Why reference counting: a `TimeoutCorrelationManagerSupport` is always a bean (the class is abstract), and CAMEL-27335 relies on the stop of its producer to complete the requests that wait. Never stopping a bean the producer did not create, as the producer does with a configured `workerGroup`, would leave those requests waiting for their full timeout when a route stops, and the manager's threads running until the CamelContext shuts its thread pools down. Handing the bean to the CamelContext (`addService`) has the same first problem. Counting the users keeps CAMEL-27335 for one producer and stops the manager only when nothing uses it. camel-ehcache does the same for its shared cache manager (`EhcacheManager`). Behaviour for users: with a manager shared by several producers, a request of a producer that stops is now completed by its timeout, or by the stop of the last producer, instead of right away at the stop of its own producer (its channel is closed, so no reply comes). A shared manager is no longer stopped while a producer uses it. No upgrade guide entry: nothing needs to change in an application, and the old behaviour of a shared manager was the bug. Not changed: - the default correlation manager, which each producer creates for itself, and custom implementations of `NettyCamelStateCorrelationManager` that are not a `TimeoutCorrelationManagerSupport`, are started and stopped by each producer as before. A custom manager shared by several producers that keeps state across them still sees one stop per producer; the counter lives in `TimeoutCorrelationManagerSupport` and the interface has no lifecycle methods to count with. - `camel-netty-http`: `correlationManager` is not an endpoint option there, but it is a component-level configuration option (`netty-http.json` lists it under the component properties), and `NettyHttpProducer` extends `NettyProducer`. A `TimeoutCorrelationManagerSupport` configured on the netty-http component is therefore retained and released by its producers the same way, with the same `putState` checks. - a request whose correlation id is used by another request that waits for its reply: separate issue (CAMEL-27517, the second PR of this batch). Tests (new `NettyTimeoutCorrelationManagerSharedTest`, package-private, a plain socket server that replies only when the test says so): - `testStoppingAndRestartingOneProducerKeepsTheRequestOfTheOther`: two producers of one endpoint, 60 s timeout. The second sends; the first stops and starts again; the server replies: the second gets the reply. On main the stop had already failed it with `ExchangeTimedOutException`. - `testRequestOfTheRunningProducerTimesOutAfterTheOtherStopped`: 300 ms timeout. After the first producer stopped, the manager still runs and a request of the second times out. - `testLastProducerThatStopsCompletesThePendingRequests`: both producers have a request waiting; the first stop leaves the manager running, the second completes both requests and stops the manager. - `testProducerThatFailsToStartAgainDoesNotStopTheManager`: a producer with `lazyChannelCreation=false` is stopped, its server goes away and its restart fails: the manager of the other producer still runs and that producer gets its reply. - `testRequestFailsWhenTheManagerIsStopped`: a bean stopped while its producer runs: the request fails with `IllegalStateException` instead of waiting forever. - `testStateFailsWhenTheManagerStopsWhilePutting`: a manager that stops itself while it reads the correlation id (after the first check of `putState`, before the put): `putState` throws, the state is marked done by `putState` (so nothing else completes it) and its callback was not called (the producer completes the exchange with the exception). - `testStatePutAfterTheDrainOfTheStopFails`: the stop of the last producer is held after its drain took the entries of the map (a listener on the Evict event of a pending request), the state is put, and `putState` is held after the put (a listener on the Put event) until the stop ended, which cleared the map. `putState` must throw, the stop must not complete the state, and the drain completes the pending request. - `testStateFailsWhenTheManagerStopsAndStartsAgainWhilePutting`: while `putState` reads the correlation id, the last producer releases the manager and another one retains it (new map): `putState` throws, and the state is in neither map. - `testStateFailsWhenPutWhileTheManagerStartsAgain`: the manager was started and stopped, and its next start is held in `doStart`, when it still has the stopped map of the previous start: `putState` throws. - `testStateCompletedByTheDrainWhilePuttingIsCompletedOnce`: the last producer releases the manager right after the put (in the Put listener), so the drain takes the state and completes it with a timeout before `putState` checks again: `putState` returns normally and the callback is called once. The race tests are deterministic: `getRequestCorrelationId` and `doStart` of a test subclass run hooks, the listeners hold the threads on latches, and a package-private `getTimeoutMap()` gives the test the map to listen on. No sleeps. Negative controls: against the `NettyProducer` and `TimeoutCorrelationManagerSupport` of main all 10 tests fail (6 on their assertions, the 4 that call `retain()` directly with `NoSuchMethodError`). Against the classes of the first version of this change (with only the package-private `getTimeoutMap()` added, so the tests compile), 4 fail in 3 of 3 runs: the three new race tests (`putState` returns normally) and `testStateFailsWhenTheManagerStopsWhilePutting` (the state is not marked done). With the check after the put that a red-team review suggested (`!isRunAllowed() || map != current`, then `remove` and `markDone`), only `testStateFailsWhenPutWhileTheManagerStartsAgain` fails, 3 of 3: the manager is already STARTING again with its old map, so that check passes, which is why the map's own state is checked as well. With the check after the put throwing without `markDone`, 4 fail, among them `testStateCompletedByTheDrainWhilePuttingIsCompletedOnce` (`putState` throws for an exchan ge the drain already completed, so the producer would complete it a second time). With the change: the camel-netty unit tests pass (190 run, 9 skipped; one failure in `NettySSLConsumerClientModeHostnameVerificationTest`, a 10 s mock wait while the machine was under load (load average about 50), which does not use a correlation manager and passed 5 of 5 runs on its own), and the new class passes 20 of 20 runs (200 tests). The waits are latches and a bounded queue poll, no sleeps. Merged locally with the other PR of this batch (both change `NettyProducer` and `TimeoutCorrelationManagerSupport`, and merge without a conflict), both new test classes and the existing correlation manager tests (stop, write failure, custom manager) pass 3 of 3 runs (69 tests). Found with a TLA+ model of the producer, the timeout map, the worker pool and stop/start with two producers sharing the manager (the invariant "a running producer has a started manager" fails on main, also with the drain of CAMEL-27335), and reproduced against the real producer. # 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-27517](https://issues.apache.org/jira/browse/CAMEL-27517). # 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-netty and the core modules offline with the formatter and import-sort plugins and ran the tests listed above. 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]
