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

   # Description
   
   [CAMEL-27509](https://issues.apache.org/jira/browse/CAMEL-27509)
   
   The Infinispan cluster views refresh the leader key from a single-thread 
scheduled executor. Each run of that task took the lock of the view's 
`LeadershipService` (the `BaseService` lock). `BaseService.stop()` holds the 
same lock for the whole `doStop()`, and `doStop()` waited for the executor 
(`shutdownGraceful`) before it called `setLeader(false)` and removed the leader 
key.
   
   So the stop and a leadership run could wait for each other:
   
   - a run that starts while the stop is in `doStop()` (the periodic run, a run 
queued by a removed or expired event, or the first run after a quick start and 
stop) blocks in `lock.lock()`, which an interrupt does not end. The executor 
cannot terminate, so the stop waits the shutdown await termination (10 s by 
default), calls `shutdownNow`, and waits it again: the stop takes twice the 
timeout.
   - a run that waits for the server (a slow Hot Rod call) when the stop is 
called holds the lock, and `stop()` waits until that call returns.
   
   During that time the stopping node keeps `leader = true` and fires no 
leadership lost event, but it no longer refreshes the leader key. When the key 
expires meanwhile, another node takes it and both nodes are leader, so 
clustered routes and master consumers run on both. With the default lifespan 
(30 s, refreshed every 15 s) this happens when the last refresh was more than 
about 10 s before the stop.
   
   Change (`InfinispanRemoteClusterView` and `InfinispanEmbeddedClusterView`, 
the same in both):
   
   - `doStop` sets `running` to false and gives up the leadership 
(`setLeader(false)`, which fires the lost event) first, before it removes the 
cache listener and waits for the executor. The leader key is still removed 
after the executor ended, with the version (remote) or the local id (embedded), 
as before.
   - A leadership run no longer takes the lock of the service. It returns at 
once when the service is stopped, so a run that starts during the stop does not 
hold up the executor, and the stop waits only for a run that is in progress. A 
Hot Rod call of that run that still waits for the server after the shutdown 
await termination is interrupted by `shutdownNow` (the Hot Rod client then 
throws), so the second wait of `shutdownGraceful` ends at once.
   - A run takes the leadership only while the service runs. `running` is set 
to false and the leadership is taken under a small new lock (`leadershipLock`), 
so a run that acquires or resumes the key while the view stops cannot make the 
stopped node leader again. It still reads the version, so the stop removes the 
key it acquired. A run also skips the acquire and the membership refresh once 
the service is stopped.
   - An error of the leader key call while stopping (such as the interrupted 
call) is logged at DEBUG instead of the "will try again" WARN. The membership 
refresh keeps its WARN.
   - `version` (remote) is now volatile, as the stop can read it while a run 
that did not end in time still writes it.
   
   The lock of the service is no longer held while a run talks to the cache, so 
runs no longer serialize with `start()`/`stop()` through it. The executor still 
runs one task at a time. A run can only outlive a stop when its cache call does 
not end within the shutdown await termination and ignores the interrupt; it 
then acts like a member that crashed (it cannot take the leadership, and a key 
it writes expires after the lifespan). With a shutdown await termination of 0 
or less, the stop does not wait for a run that is in progress (before, it 
waited for it through the lock of the service).
   
   No upgrade guide entry: a stopped view gives up the leadership earlier than 
before, and a stop no longer takes twice the shutdown timeout; nothing to 
migrate.
   
   Not changed here (found in the same review; they depend on the server's 
behaviour, which I have not checked against a real Infinispan server yet, or 
are low):
   - a resumed leadership (the `putIfAbsent` that returns the local id after a 
failed refresh) does not renew the key, and the resume can happen just as the 
key expires;
   - the version read with `getWithMetadata` after the write is not checked 
against the local id, so a slow resume can take another node's version and then 
overwrite its key;
   - a lifespan under one second is sent to the refresh as 0 seconds, which the 
Hot Rod client sends as "default lifespan".
   
   I can file follow-ups for these.
   
   Tests (package-private, no sleep, latches and Awaitility; the test pool 
factory keeps the leadership executor so the test can start a run at the moment 
it needs, as the periodic run would):
   
   - `InfinispanRemoteClusterViewStopTest` (camel-infinispan, a fake 
`RemoteCache` with versioned entries; as the Hot Rod client, an interrupted 
call throws `HotRodClientException`):
     - `runStartedWhileStoppingDoesNotDelayTheStop`: the stop is held in 
`removeClientListener` while a run starts. The node is no longer leader and the 
lost event was fired when the listener is removed, the stop takes less than one 
shutdown await termination (1 s), and the key is released.
     - `stopGivesUpTheLeadershipWhileARunWaitsForTheServer`: a refresh waits 
for the server; after the stop begins, the node is no longer leader while the 
refresh still waits, the stop takes between one and two timeouts (one wait, 
then the interrupt ends the call), and the key is released.
     - `runThatAcquiresTheLeaderKeyWhileStoppingDoesNotTakeTheLeadership`: the 
first `putIfAbsent` answers "acquired" only after the stop began; the node 
never becomes leader, no event is fired, and the key is released.
   - `InfinispanEmbeddedClusterViewStopTest` (camel-infinispan-embedded, a 
local cache with a delegating cache that holds `removeListener` or 
`putIfAbsent`): the first and the third case above.
   
   All five new tests fail on main (checked by running the new classes against 
the views of main): 3 of 3 remote and 2 of 2 embedded. On main the stop of the 
first case takes 2012 ms (remote) and 2034 ms (embedded) with a 1 s shutdown 
await termination, the second remote case keeps the node leader while the 
refresh waits, and in the third case the stop does not even reach the removal 
of the listener while the acquire waits for the server. With the change the two 
new classes pass 20 of 20 runs each (100 tests).
   
   The camel-infinispan unit tests pass (17 run, 0 failures). The 
camel-infinispan-embedded unit tests pass (104 run, 0 failures), without 
`InfinispanEmbeddedClusteredConsumerTest` and 
`InfinispanEmbeddedRepositoriesInMemoryTest`: these form a two-node JGroups 
cluster, which could not form on my machine (the view timed out before any code 
of this change runs), so CI has to cover them. The integration tests need an 
Infinispan server container and were not run locally (CI runs them).
   
   Found with a TLA+ model of the view's leadership thread, its stop and the 
Hot Rod server ("a stop does not wait on its executor while that executor's 
thread waits on the stop's lock" and "at most one leader" fail with a stop 
alone), and reproduced against the real remote view with an in-memory fake of 
the Hot Rod server.
   
   # 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-27509](https://issues.apache.org/jira/browse/CAMEL-27509).
   
   # 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 the infinispan modules 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]

Reply via email to