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

   # Description
   
   [CAMEL-25213](https://issues.apache.org/jira/browse/CAMEL-25213)
   
   `FileStateRepository` (the file based `StateRepository`, documented for the 
camel-kafka `offsetRepository` option) could lose the state it stores:
   
   - `doStop` rewrote the file (`trunkStore()`) and cleared the map without the 
lock that `setState` holds. A `setState` during the stop (another route sharing 
the repository, a Kafka consumer thread still committing, the MongoDB change 
stream thread) made the stop fail with a `ConcurrentModificationException`, and 
the file kept only the lines written until then.
   - `trunkStore` truncated the file before writing it again, so a failure or a 
crash during the rewrite (on every stop, and whenever the file reaches 
`maxFileStoreSize`) left it empty or partial.
   - `appendToStore` wrote a line in four writes, and `loadStore` failed with 
`StringIndexOutOfBoundsException` on a line without `=`, so an incomplete last 
line kept the repository (and the consumer starting it) from starting.
   
   This change:
   
   - `doStop` holds `cacheAndStoreLock`, as `reset()` does;
   - `trunkStore` writes into `<file>.tmp`, syncs it and moves it over the 
store (atomic move where supported); on failure the store is unchanged;
   - a line is appended with one write, and `loadStore` skips an incomplete 
line with a WARN.
   
   No API or file format change.
   
   Tests (`FileStateRepositoryTest`, all fail without the change):
   - `shouldNotLoseStateWhenUpdatedWhileStopping`: a `setState` from another 
thread while `stop()` rewrites the file (paused through the cache map) must 
wait for the stop, and all keys must be there after a restart. Without the 
change: `ConcurrentModificationException` and lost keys.
   - `shouldKeepStoreWhenRewritingFails`: the rewrite fails half way; the store 
must keep its previous content. Without the change: the entries after the 
failure are gone.
   - `shouldSkipIncompleteLineWhenLoading`: a store whose last line has no `=` 
must load. Without the change: `StringIndexOutOfBoundsException` on start.
   
   I ran the `org.apache.camel.support` tests of camel-support (122) and 
camel-core (453), all pass.
   
   # 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).
   
   # 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