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

   # Description
   
   [CAMEL-27584](https://issues.apache.org/jira/browse/CAMEL-27584)
   
   With `readLock=idempotent` or `idempotent-changed`, 
`readLockIdempotentReleaseAsync=true` and a `readLockIdempotentReleaseDelay`, 
the read lock releases a file (removes or confirms its key in the idempotent 
repository) on its own scheduled thread pool, after the delay. When the 
consumer stops, the read lock shuts that pool down with `shutdownGraceful(pool, 
30000)`: `BaseExecutorServiceManager` waits up to 30 seconds, then calls 
`shutdownNow()` and nobody runs the tasks it hands back.
   
   So with a delay longer than 30 seconds, a release that is still pending when 
the route stops is lost:
   
   - after a rollback (the default `readLockRemoveOnRollback=true`) the key 
stays in the repository. After the route is started again every poll is refused 
the read lock and the file is never processed. With a persistent or clustered 
repository (what this read lock is for) that lasts until someone deletes the 
key by hand;
   - with `readLockRemoveOnCommit=true` the key of a committed file stays, so a 
new file with the same name is never picked up;
   - and every graceful stop with a release pending blocks until the release is 
due, at most 30 seconds.
   
   The fix: the read lock keeps the release tasks it scheduled and has not run 
yet (`DelayedReadLockReleaseTasks`, shared by the two strategies). When it 
stops, it runs them at once in the stopping thread, then shuts the pool down, 
which then has no delayed task left to wait for. A task runs only once, in the 
pool or at stop (a flag decides, and the scheduled future is cancelled). It 
runs them again after the pool is shut down, for a release scheduled while the 
pool was shutting down. This also applies when a shared pool is set with 
`readLockIdempotentReleaseExecutorService`. One INFO line says how many read 
locks are released early; a release that fails at stop is logged at WARN and 
does not stop the others.
   
   Releasing before the delay is over is what a stop needs: the delay keeps a 
committed file locked a little longer so that another node with an older 
directory listing does not pick it up. On commit the file is normally moved or 
deleted before the release is scheduled, and every node checks that the file 
still exists before it adds the key, so an early release at stop only shortens 
that extra window, once, for the files committed in the last 
`readLockIdempotentReleaseDelay` millis. Dropping the release left the key for 
good. For a rolled back file, releasing at once lets the file be retried 
sooner, by this node after a restart or by another node.
   
   Also in the same classes (a few lines): with `idempotent-changed` and 
`idempotent-rename`, the read lock recorded that it owns the key 
(`CamelFileLockIdempotentAcquired`) only after the changed or rename check. If 
that check throws, or the removal of the key after a failed check throws (a 
repository error), the abort that follows skipped the removal, and the file was 
never processed again. The read lock now records that it owns the key right 
after the add, so the abort removes it. When the check fails and the removal 
works, nothing changes.
   
   Model: found with a TLA+ model of several nodes sharing a directory and an 
idempotent repository (release delay longer than the stop cap, stops and 
restarts, forced stops, repository failures, crashes). With the fix, all 
properties hold with a delay longer than the stop cap, with 3 nodes and 2 
stops, including that every pending release runs. For the changed and rename 
strategies, the key is no longer left behind when the sub-check or its cleanup 
throws.
   
   The upgrade guide entry also covers a 
`readLockIdempotentReleaseExecutorService` of your own: nothing was dropped 
there before, and its pending releases now also run at the stop.
   
   Tests: new `FileIdempotentReadLockReleaseOnStopTest`, with the real file 
consumer, a `KeyValueIdempotentRepository` subclass that counts the removes and 
a temporary directory, no 30 second waits:
   
   - a route that fails once, with a 120 second release delay, for `idempotent` 
and `idempotent-changed`: stopping the route must finish within 10 seconds and 
remove the key, and after a restart the file is processed;
   - `readLockRemoveOnCommit=true` with the same delay: the stop removes the 
key of the committed file, and after a restart a new file with the same name is 
processed;
   - `idempotent-changed` with a file shorter than `readLockMinLength` and a 
repository whose next removal fails: the key must be removed by the abort, so 
the file is processed once it is long enough;
   - a control that also passes on main: a release that already ran in the pool 
is not run again when the route stops.
   
   On main the first 4 fail (the stop is still blocked after 10 seconds: it 
waits 30 seconds and then drops the release; the changed key is never removed). 
With the change they pass, and so do the file tests of camel-core and 
camel-file: 624 run, 0 failures, 12 skipped (and camel-master: 20 run, 0 
failures). The new class 20 times in one JVM: 100 run, 0 failures.
   
   Upgrade guide: a 4.24 entry, as a stop now releases the pending read locks 
at once instead of waiting.
   
   Not changed (follow-up JIRA if wanted):
   
   - When the graceful shutdown times out and an exchange completes after the 
consumer stopped, its release can still be lost (scheduled on a pool that is 
shut down, or a synchronous delayed release interrupted in its sleep).
   - A release that throws in the pool is not logged, and is not retried.
   
   # 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-27584](https://issues.apache.org/jira/browse/CAMEL-27584).
   
   # 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