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

   # Description
   
   [CAMEL-27437](https://issues.apache.org/jira/browse/CAMEL-27437)
   
   A route can have both throttling route policies, for example 
`routePolicy(inflightPolicy, exceptionPolicy)`. Each of them resumed the 
consumer on its own, without looking at the other:
   
   1. With concurrent consumers and a failing downstream (redeliveries keep the 
exchanges in flight), `ThrottlingInflightRoutePolicy` suspends the consumer 
when more than `maxInflightExchanges` are in flight.
   2. The failures reach `failureThreshold`, and 
`ThrottlingExceptionRoutePolicy` opens the circuit. The consumer is already 
suspended, so its suspend does nothing.
   3. The exchanges still in flight complete, and the inflight policy resumed 
the consumer: it only checks its own set of suspended consumers (CAMEL-25018) 
and the route status.
   
   The consumer then took new messages while the circuit was open and sent them 
to the failing downstream, although the route policy docs say that while the 
circuit is open the consumer takes no new messages. The other way round, half 
opening the circuit (or closing it with a ready `halfOpenHandler`) resumed a 
consumer that the inflight policy still kept suspended. Both orders of the two 
policies behave the same.
   
   This generalises the CAMEL-25018 approach (a policy only resumes a consumer 
it suspended itself) to the policies of a route: a policy resumes the consumer 
only when no other policy of the route still keeps it suspended.
   
   - `RoutePolicySupport` has a new protected hook 
`isConsumerKeptSuspended(Route)` (false by default), and 
`isConsumerKeptSuspendedByAnotherPolicy(Route)`, which asks the other 
`RoutePolicySupport` policies of the route (`Route.getRoutePolicyList()`). No 
new shared state: each policy answers from its own state.
   - `ThrottlingInflightRoutePolicy` keeps the consumer suspended while it is 
in its set. When it would resume the consumer, it removes it from its set 
first, and does not resume it if another policy keeps it suspended. If the 
resume throws, the consumer goes back into its set, so the next completion 
tries again, as on main.
   - `ThrottlingExceptionRoutePolicy` keeps the consumer suspended while the 
circuit is open. When it half opens or closes the open circuit, it first lets 
go (a volatile flag, set and cleared under its lock), and does not resume the 
consumer if another policy keeps it suspended. The state changes as before, 
after the resume, so if the resume throws the circuit stays open and keeps the 
consumer suspended again.
   
   Each policy lets go before it checks the other one, so when both let go at 
the same time, at least one of them sees the other has let go and resumes the 
consumer. The claim of the exception policy is two fields (the state and the 
flag), so it reads the flag first: read the other way round, it could see the 
circuit still open and then the flag already cleared, and then neither policy 
would resume the consumer. The policy that still keeps the consumer resumes it 
when it lets go: the inflight policy on a later completion (while it keeps the 
consumer, an exchange is still in flight), the exception policy when it half 
opens the circuit.
   
   Not changed:
   
   - When the circuit opens first and suspends the consumer, the inflight 
policy does not join that suspension (it only suspends a started consumer). The 
half open check then resumes the consumer even if more than 
`maxInflightExchanges` are still in flight, and the next completion suspends it 
again, as on main.
   - A policy checks the other one and then resumes the consumer; the two are 
not one atomic step. If the circuit opens between the check of the inflight 
policy and its resume, the consumer is resumed with the circuit open, until the 
half open check. The window is a few instructions, where on main the inflight 
policy resumed the consumer whatever the state of the circuit.
   - The exception policy keeps its state when its route is removed 
(CamelContext stopped, or the route removed and added again with the same 
policy instance), but not its half open check. A circuit that is still open 
then has no check scheduled; it is half opened by the next exchange that 
completes after `halfOpenAfter`. If the inflight policy lets go of the consumer 
before that, it now waits for the open circuit, and the consumer stays 
suspended until the route is restarted.
   - The quartz `ScheduledRoutePolicy` (route suspend/resume times) suspends 
and resumes the consumer itself and does not take part; neither do custom 
policies that do not override the new hook.
   
   Docs: a paragraph in `route-policy.adoc`, and a 4.24 upgrade guide entry, as 
the inflight policy no longer resumes the consumer while the circuit is open.
   
   Model: a TLA+ model of the two policies on one consumer, with the routing 
threads, redeliveries, the half open timer, the route controller (suspend and 
resume), and the policy steps that read or write shared state as separate 
actions (the decision of each policy is read outside its lock; the check of the 
other policy and the resume are one action, and so is the read of the claim of 
the exception policy, see above). On main, "while the circuit is open, no 
policy has resumed the consumer behind the breaker" is violated with 3 threads 
(inflight policy first: 26 states; exception policy first: 19), and with 4 
threads; "the breaker has not resumed a consumer the inflight policy still 
keeps suspended" is violated too. With the change, both hold together with "an 
open circuit always has a half open check pending", "a consumer the inflight 
policy suspended always has a completion still to come", "a suspended consumer 
has an owner that will resume it" and the inflight count, and the livene
 ss properties "a suspended consumer is resumed" and "an open circuit does not 
stay open": 3 threads 1,247,210 distinct states (inflight first) and 6,732,746 
(exception first), exhaustive; 4 threads with a smaller queue and no route 
controller 2,573,928 and 28,577,014. TLC gives the same counts at 3 threads.
   
   Tests: new `ThrottlingInflightAndExceptionRoutePolicyTest` (package-private, 
JUnit assertions as the module), each test for both policy orders. A seda route 
with 4 concurrent consumers, `maxInflightExchanges=1` and `failureThreshold=2`; 
latches decide the order in which the exchanges complete, and the test waits 
until both policies are done with each one. The test runs the half open check 
and the close itself (the timer is not scheduled), so nothing depends on time:
   
   - `inflightPolicyDoesNotResumeTheConsumerWhileTheCircuitIsOpen`: after the 
success that brings the inflight count down, the consumer must stay suspended 
while the circuit is open; the half open check resumes it, and the next 
exchange closes the circuit.
   - `halfOpenCheckDoesNotResumeTheConsumerTheInflightPolicySuspended`: half 
open with 2 in flight, the consumer stays suspended; the next completion 
resumes it.
   - `closingTheOpenCircuitDoesNotResumeTheConsumerTheInflightPolicySuspended`: 
the same when the open circuit is closed (as a ready `halfOpenHandler` does).
   - `circuitStaysOpenWhenTheResumeOfTheHalfOpenCheckFails`: when the resume of 
the half open check throws, the circuit stays open and the consumer suspended; 
the next check resumes it. (On main it fails earlier, as the inflight policy 
has already resumed the consumer.)
   
   All 8 runs fail on main. The last test is in part a control: the circuit 
staying open when the resume throws holds on main too. The order of the two 
reads of the exception policy has no test in the PR, as it needs a pause 
between them: a separate harness (not in the PR) with a pause hook between the 
two reads, in an instrumented copy of the method, reproduces the stuck consumer 
with the reads the other way round (5 of 5 runs), and resumes it with this 
order (5 of 5). With the change: the camel-core and camel-support tests 
matching `*RoutePolicy*`, `*Throttling*`, `*Throttle*` and 
`*SupervisingRouteController*` pass (camel-core 169, camel-support 3, 0 
failures); the new class 20 times in one JVM, 160 runs, 0 failures. Formatter 
and import-sort checks pass.
   
   A second change for the same policy (the half open check is scheduled again 
when it fails to suspend or resume the consumer) also edits 
`ThrottlingExceptionRoutePolicy`, in the half open task only. The two do not 
overlap and merge cleanly in either order; together the model still holds.
   
   # 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-27437](https://issues.apache.org/jira/browse/CAMEL-27437).
   
   # 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 camel-support and camel-core, including the formatter 
and import-sort plugins. No option changed. 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 commits carry 
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