allthingssecurity opened a new pull request, #30844: URL: https://github.com/apache/camel/pull/30844
# Description [CAMEL-27460](https://issues.apache.org/jira/browse/CAMEL-27460) `DeviceClientPool` (new in CAMEL-22624) closes a device client after `deviceClientIdleTimeout` without use. The "last used" time it compares with was not kept up to date: - `acquire` read the clock once, before it could block in `getOrCreate` for the whole `open(true)` of a new client, and stamped the lease with that old time; - a new client was put in the pool with the time read before `open(true)`; - when the last lease was given back, `touchAndMaybeEvict` compared the clock with that old time before updating it. So a client was closed as idle right after a long lease, or just after it had been opened. A client was never closed while it had a lease; it was closed when the lease was given back, or before its first lease: - a send or twin update that takes longer than `deviceClientIdleTimeout` closed its client when the lease was given back, and the next message opened a new connection; - when the hub is slow to accept connections and `open(true)` takes longer than `deviceClientIdleTimeout`, the new client was closed as soon as its first (fast) send gave it back, so every message paid a new slow open and a close: 5 messages 100 ms apart opened 5 connections (idle timeout 10 s, open 15 s). The extra opens add load to a hub that is already slow; - a client that was just opened could also be evicted by the eviction task, or by a concurrent `acquire`, before its creator leased it. With the defaults it does not happen: the idle timeout is 5 minutes, each SDK call of a send is bounded by the 10 s `sendTimeout`, and the SDK gives up an open after about 4 minutes. It shows with a `deviceClientIdleTimeout` shorter than the time the hub takes to accept a connection or to answer a send (for example 10 s against a hub that needs 15 s). It stops once the hub is fast again. Change, in `DeviceClientPool` only: - a client with a lease is still never evicted (the eviction task and `acquire` already skipped it); now its idle time counts from the last release: the lease touches the client when it is taken and right before it is given back (before the in-use count drops, so the eviction task never sees a free client with an old time), and giving the lease back no longer runs an idle check. `touchAndMaybeEvict` is removed; the eviction task and `acquire` still evict idle clients, with the same `inUse == 0` and idle-time test (now one `isIdle` helper); - a new client gets its time after `open(true)`; - the clock is read at each touch instead of once per `acquire`, and the time never moves back (`lastUsed` is an `AtomicLong` updated with `max`), so a thread that read the clock a little earlier cannot shorten the idle time of a client another thread just gave back. What this changes for users: a client given back after a long send now stays in the pool until the eviction task finds it idle, like any other client, so it is closed between `deviceClientIdleTimeout` and `deviceClientIdleTimeout` + `deviceClientEvictionInterval` after the release (before, it was closed at the release). A send that took longer than the idle timeout no longer reconnects the device as a side effect; a connection the SDK reports as DISCONNECTED is still retired at once, as before. The public API of the pool, the options and their descriptions do not change (the option already says "idle time"). No upgrade guide entry: the component is new in 4.24, and nothing is to be done by users. Not changed here (found while looking at the pool; I will file follow-up tickets): - a terminal DISCONNECTED that the SDK reports before the new client is in the pool (the callback is registered before the client is put in the map) is lost, so the dead client is pooled and used for every later send until the traffic pauses for the idle timeout. It needs the status to arrive in a short window; - `Lease.close` reads `retired` after the in-use count dropped to 0 without checking it again, so it can close a client that another producer leased between that release and a disconnect callback that retired it. That client is already disconnected, so the send fails either way; - `shutdown()` closes clients that still have a lease and clears the creation futures without completing them. Camel stops the routes before the component, so this only matters after a graceful-shutdown timeout; - a slow `close()` of a retired client runs on the eviction thread and delays the next eviction run (bounded by one SDK reconnect attempt). Tests, new in `DeviceClientPoolTest` (3, package-private, fake clock through `setNowMillisSupplier`, mocked `DeviceClient`, and a mocked eviction executor whose scheduled eviction run the test calls itself, so no waits and no sleep): - `testSlowSendKeepsItsClientAndIdleTimeCountsFromRelease`: a lease held for 15 s (idle timeout 10 s) keeps its client through an eviction run during the lease, the release and an eviction run right after it; the next lease gets the same client; an eviction run 10.001 s after the release closes it; - `testSlowlyOpenedClientIsNotEvictedBeforeItsFirstLease`: an `open(true)` that takes 15 s, then an eviction run before the first lease: the client stays, and the lease gets it; - `testSlowHubOpensOneConnectionForManyMessages`: open takes 15 s, 5 messages 100 ms apart: one client is opened and none is closed. The three tests fail with the current `DeviceClientPool` (client evicted at release, by the eviction run, and a new client for the second message). With the change the camel-azure-iothub unit tests pass (42 run, 0 failures), and `DeviceClientPoolTest` passes 20 of 20 runs. There are no ITs in the module. A stress run that is not part of this PR (4 producer threads, the eviction run and DISCONNECTED callbacks on another thread, 60,000 leases, fake clock) found no client closed while leased, none left open after the idle timeout and none closed twice, with the change and with the current code; with the change it opened about 2.5 to 9 times fewer clients. A TLA+ model of the pool (producers, the eviction task, the SDK status callback, a slow hub) found this: its "an idle eviction only takes a client with no lease that was unused for longer than the idle timeout" property fails on the current code. Backport: not needed, the component is new in 4.24. # 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-27460](https://issues.apache.org/jira/browse/CAMEL-27460). # 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-azure-iothub and the core modules with the formatter and import-sort plugins and ran the camel-azure-iothub unit tests. I did not run the full root build. No generated file changes.) # 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]
