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

   # Description
   
   [CAMEL-27567](https://issues.apache.org/jira/browse/CAMEL-27567)
   
   `FileLockClusterView.acquireLock` opens the lock file through the cluster 
task executor. When an attempt does not finish within `clusterDataTaskTimeout` 
(a slow network mount), `FileLockClusterTaskExecutor` calls `cancel(true)` on 
the `CompletableFuture` and starts the next attempt. Cancelling a 
`CompletableFuture` does not interrupt its task, so the first attempt keeps 
running. The retry opens the file, the view locks it and becomes LEADER, and 
then the first attempt returns a second `RandomAccessFile` on the lock file 
that nothing references.
   
   The 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 it (the `FileLock` javadoc, and CAMEL-27322 for the same effect between two 
views of a JVM). After a garbage collection the JDK closes the unreferenced 
file, and the lock of the leader is gone at OS level. The leader does not 
notice: `lockIsValid` checks `FileLock.isValid()`, which stays true, and that 
the file exists. From then on a member in another JVM that finds the leader's 
heartbeat stale (a GC pause, the scheduler thread busy with the retries of 
another view, a slow I/O call) takes the lock and becomes LEADER too, which is 
the case the lock is there for.
   
   The fix: the view never leaves a file on the lock file open for the garbage 
collector.
   
   - Each open of the lock file by `acquireLock` tracks its attempts. When the 
executor returns, the files of the other attempts that have finished are closed 
before the lock is tried (no lock is held then).
   - If an attempt that timed out is still running when the executor returns 
(or gives up), the view does not try the lock: it closes the file it got and 
keeps the lock file registered in the JVM-wide registry of CAMEL-27322, so no 
other view of the JVM tries it either. The late attempt closes its file as soon 
as it has opened it, which releases nothing as no view of the JVM holds the 
lock, and the last one to end removes the registry entry. Until then each check 
skips the lock (DEBUG). The start of such a wait is logged once at WARN, naming 
the lock file and saying that the wait lasts until the JVM restarts if the open 
never returns; its end is logged at INFO. When the open of the lock file is 
still running, `acquireLock` no longer opens the data file just to close it 
again.
   - The actual `new RandomAccessFile(..)` moved to a package-private 
`openFile(Path)`, so a test can make it slow.
   - It contains the one-line change of CAMEL-27566 (#30862), with the same 
text, so it does not depend on the merge order (see below).
   
   **Depends on CAMEL-27566, which is included.** A member that lost the 
leadership (one failed read of the cluster data is enough) finds its own id in 
the data file and tries to take the leadership back. If the open of the lock 
file for that times out once, `acquireLock` returns false while the open still 
runs, and the member keeps its own id as the previous observation. Without 
CAMEL-27566 every later check then takes the "Backoff so the current cluster 
leader can notice" return, also after the open has ended, and a single-member 
cluster never has a leader again. On main the retry takes the lock in that case 
(with the orphan file above), so this change alone would add a new and common 
way into that backoff. The commit therefore carries the CAMEL-27566 line 
(`clusterLeaderInfoRef.compareAndSet(latestClusterLeaderInfo, null)` with its 
comment) byte for byte: `git merge-tree` of this branch with the #30862 branch 
is clean in either order, and whichever is merged first, the other one n
 o longer changes that line. A backport needs CAMEL-27322 and CAMEL-27566 too.
   
   **The trade-off, if an open of the lock file never returns (a hung mount):** 
no view of that JVM tries the lock on that file again until the JVM restarts. 
The count of running opens lives in the view and the deferred key in the static 
registry, so neither a stop and start of the service or the CamelContext nor a 
new CamelContext in the same JVM helps (the new view finds the old one as 
holder of the key). The view reports FOLLOWER meanwhile and `getLeader()` is 
empty, and a member in another JVM on a healthy mount takes the leadership, but 
a single-member cluster stays without a leader. In exactly that case main is 
safe and live: no late file ever arrives, so nothing is left to the garbage 
collector, and the retry takes the lock. So the change gives up liveness on a 
mount where one open hangs for good while a later one returns, to never have 
two leaders on a slow mount. The WARN at the start of the wait says so, and the 
4.24 upgrade guide has a short entry.
   
   Why wait rather than keep the late file: keeping a late file that arrives 
while the view holds or is trying the lock, and closing it when the lock is 
released, would keep the member live in the hung case. But closing such a file 
always releases the lock, so the file has to be handed over on every release 
path (lost lock, stop, a try that is not published) under `stateLock`, with a 
"trying" state for the window between registering the key and publishing the 
lock; that changes every release path that the waiting version was 
model-checked and stress-tested on (see below). It would not help a new 
CamelContext of the same JVM either: as long as the hung open may still return, 
its file must not be closed while another view holds the lock, so the key stays 
registered for the old view. A check of the lock itself cannot help: from 
inside the JVM, `FileLock.isValid()` stays true and a `tryLock` of the same JVM 
throws `OverlappingFileLockException` whatever the OS lock is. The cost on a 
slow
  but working mount: the view takes the lock a check or two after the slow open 
ended (with CAMEL-27566; without it, never, see above).
   
   Model: found with a TLA+ model of the file lock cluster view (attempts that 
keep running after their timeout, the close of their file by the garbage 
collector). On main the model reaches a LEADER that holds no OS lock, and two 
LEADERs with a member in another JVM; with no file left to the garbage 
collector (the model's abstraction of this change, not the code itself) at most 
one view is leader and the leader holds the OS lock, also with the master 
consumer on top. A second model with the mechanism of this change (a late open 
per view, its explicit close, the key kept meanwhile) plus the CAMEL-27566 
backoff: at most one leader and the leader holds the OS lock with two JVMs and 
with two views of one JVM; some view is eventually leader if every late open 
ends, with one and with two views. If a late open may never end, that liveness 
property fails with one view (the trade-off above), and without the CAMEL-27566 
change it fails with one view even when the late open ends.
   
   Tests: new `FileLockClusterViewLateLockFileOpenTest` (camel-core, next to 
the other file cluster tests). The first open of the lock file waits on a latch 
past a 200 ms task timeout, as on a slow mount; the test keeps every lock file 
the view opens.
   
   - one view: while the slow open runs, the view must not be leader and none 
of its lock files may be open; once the slow open ended the view becomes leader 
with exactly one lock file open;
   - two views in one JVM: the first view stops while its slow open runs; the 
second view must not open the lock file until that open has ended, and then 
becomes leader; no file of the stopped view stays open;
   - the only member of the cluster: one failed read of the cluster data makes 
the leader step down, and the first open of the lock file in its attempt to 
take the leadership back is the slow one; it must not lead while that open 
runs, and must lead again (with exactly one lock file open) once it has ended.
   
   The test needs the new `openFile` hook, so for the negative control I ran it 
against main with only the hook added: all three tests fail (the view takes the 
lock while the slow open runs; the second view opens the lock file while the 
stopped view's open runs). Against the previous version of this change without 
the CAMEL-27566 line, the first two pass and the third fails in 3 of 3 runs: 
the member is not leader again within 10 seconds after the slow open ended. 
With the change all three pass, and so do the tests of camel-file (54), the 
file and cluster tests of camel-core (630 run, 0 failures, 12 skipped) and the 
tests of camel-master (34, with the failover tests and 
`singleClusterMemberRecoversLeadershipIfLockFileDeleted`). The new class with 
`FileLockClusterViewSameJvmTest` and `FileLockClusterViewStopTest` 20 times in 
one JVM: 200 run, 0 failures. The existing tests that hook 
`createRandomAccessFile` (`FileLockClusterViewSameJvmTest`, 
`FileLockClusterViewStopTest`) are unchange
 d and pass. The test of CAMEL-27566 
(`FileLockClusterViewReclaimLeadershipTest`, #30862, not part of this commit) 
on this branch: 39 of 40 pass; the one failure is in its control test and 
happens on #30862 alone too (1 in 80), a timing window of that test, which 
#30862 has since fixed (its tests now record the leadership changes with a 
listener).
   
   I also ran the two-JVM reproduction of the model run with the same slow 
first open (300 ms task timeout): on main with only the hook, an external 
`fcntl` probe gets the lock after one `System.gc()` while the view reports 
LEADER, and a member in a child JVM becomes LEADER too while the view's checks 
stall for 4 seconds. With the change the view becomes leader only after the 
slow open ended, the probe stays blocked over 47 GC rounds, and the child JVM 
stays FOLLOWER.
   
   Upgrade guide (4.24): a short entry under "camel-file - a file lock cluster 
member waits for an open of the lock file that timed out", for the hung-mount 
case above. No option or default changes.
   
   The fix builds on the registry of CAMEL-27322 (4.24) and needs the backoff 
change of CAMEL-27566 (included here). The 4.14.x and 4.18.x lines have the 
same retry of a timed-out open (since CAMEL-22430), but not the registry, so a 
backport needs CAMEL-27322 and CAMEL-27566 too.
   
   Not changed:
   
   - An attempt to open the data file that times out can also leave a file for 
the garbage collector. Nothing locks the data file, so closing it releases 
nothing.
   - A heartbeat write that timed out can still land after another member took 
over (one extra leadership change, not two leaders); it needs a write that 
blocks in the kernel across a close, as on a hung NFS 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-27567](https://issues.apache.org/jira/browse/CAMEL-27567).
   
   Follow-up commit 17e67f96d7ec (review): the WARN for a lock file open that 
is still running is shorter.
   Follow-up commit 4a526ab2c44c (review): the INFO says only that the late 
open has ended (also right when the view was stopped meanwhile), and the WARN 
says to raise `clusterDataTaskTimeout` if every open is that slow.
   
   # 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 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