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

   # Description
   
   [CAMEL-27322](https://issues.apache.org/jira/browse/CAMEL-27322)
   
   `FileLockClusterView.acquireLock` opens a new `RandomAccessFile` on the lock 
file and calls `tryLock`. When another view in the same JVM holds the lock, the 
JDK throws `OverlappingFileLockException`, and the `finally` closes the new 
channel. The JDK file locks are locks of the process (POSIX `fcntl` locks on 
Linux and macOS), and closing any channel of a file releases all the locks of 
the process on that file. The `FileLock` javadoc warns about this and asks for 
one channel per file in a program. After that failed attempt:
   
   - the leader view stays LEADER and its `FileLock.isValid()` still returns 
true, so it never notices;
   - the lock file is no longer locked at OS level, and a member in another JVM 
that tries the lock gets it and becomes LEADER too.
   
   Mutual exclusion then rests on the heartbeat timing only. The lock is meant 
for exactly the case where that fails: a leader that stalls longer than 
`acquireLockInterval * heartbeatTimeoutMultiplier` (a GC pause, a blocking NFS 
call). And the follower in the same JVM only tries the lock when it finds the 
heartbeat stale or the data file empty, which is that case (or a cold start). 
Several views in one JVM on the same root is a supported setup: several 
CamelContexts with a `FileLockClusterService` on the same root (the static 
`contextStartLock` is there for it), as in the failover tests of camel-master.
   
   The fix: a JVM-wide registry (a static `ConcurrentHashMap`) of the lock 
files that a view holds or is trying to lock, keyed by the absolute path of the 
lock file (with the symbolic links of its directory resolved, so two services 
configured with different paths to one directory share the key). `acquireLock` 
registers the path before it opens the lock file; if another view of the JVM 
has registered it, the view does not try the lock at this check (the try would 
fail with `OverlappingFileLockException` anyway). The entry is removed by the 
view that added it, after it has released the lock and closed the files: on a 
failed or unpublished try, on `doStop`, and when the leadership is lost.
   
   Model: the bug was found with a TLA+ model of the file lock view (check 
thread, start and stop, the OS lock, the data file), with nodes that can share 
a JVM. I added the registry to it. On main, with two nodes in one JVM, a node 
is LEADER without holding the OS lock, and with a third node in another JVM, 
two nodes are LEADER. With the registry, at most one leader and the leader 
holds the OS lock: 103,934 distinct states with two nodes in one JVM (TLC and 
everystate agree), and 53,659,792 with the third node in another JVM 
(everystate).
   
   Tests: new `FileLockClusterViewSameJvmTest` (camel-core, next to the other 
file cluster tests):
   
   - two views in one JVM; the leader's checks stall so its heartbeat gets 
stale; the follower must find the leader stale and try to take over, and must 
not open the lock file while the leader holds it;
   - the follower takes over once the leader of its JVM stops (control: the 
registry entry is released on stop).
   
   The OS effect itself (another process can take the lock) cannot be seen from 
inside the JVM, as any `tryLock` of the same JVM throws 
`OverlappingFileLockException` whatever the OS lock is. A test that checks it 
needs a second JVM (and a POSIX system), which would be slow and platform 
dependent for a unit test. So the test pins the contract of the `FileLock` 
javadoc (one channel per lock file in the JVM), and I checked the OS effect 
separately with the reproduction of the model run: two views in the test JVM 
plus a member in a child JVM, the leader's checks stalled. On main an external 
`fcntl` probe acquires the lock while view 1 is LEADER and the child JVM 
becomes LEADER too; with the change the probe stays blocked and the child JVM 
never becomes LEADER (20 s).
   
   On main the first test fails (the follower opened the lock file twice); the 
second passes on both. With the change they pass, and so do the tests of 
camel-file (47), the file and cluster tests of camel-core (612 run, 0 failures, 
12 skipped) and the tests of camel-master (34, with the same-JVM failover 
tests). The new class 20 times in one JVM: 40 run, 0 failures.
   
   No upgrade guide entry: nothing to change for users.
   
   Not changed:
   
   - Views in different class loaders of one JVM (for example two applications 
in one server) do not share the registry, and they still close each other's 
lock. Neither does any other code of the JVM that opens the lock file.
   - A "leader" event of the check can still be delivered after a `doStop` that 
fired "no leader" (the check fires it outside the state lock); Camel's own 
listeners read the leadership again and are not affected. Separate issue.
   - When the leader loses its lock (or its view stops) the 
`ClusteredRoutePolicy` stops its routes on its own thread after the lock is 
released, so another node can start them while they still run. Separate issue, 
with a design to agree on first.
   
   # 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-27322](https://issues.apache.org/jira/browse/CAMEL-27322).
   
   # 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 modules, including the formatter and 
import-sort plugins. 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