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

   # Description
   
   [CAMEL-27418](https://issues.apache.org/jira/browse/CAMEL-27418)
   
   Since CAMEL-26107, `WriteAheadResumeStrategy.start()` reads the log of the 
previous run and keeps its unconfirmed entries in the new log until 
`loadCache()` replays them, "in case the system crashes again before", and the 
4.24 upgrade guide says they stay in the log until the wrapped strategy 
confirms them. But `start()` created the new log with `new LogWriter(...)`, 
which opens the same file with `TRUNCATE_EXISTING` and writes the header, and 
only then appended the carried entries, which were forced to disk at the next 
supervisor tick. So the entries were lost when, in that window:
   
   - the process crashed (between the truncate and the end of the carry);
   - `start()` threw, for example when the flush task could not be scheduled or 
an append of a carried entry failed with an `IOException` (disk full). 
`start()` failed with the log already truncated, and the next start found an 
empty log. This one is deterministic once the error happens;
   - the power failed before the next tick: the truncate can be on disk while 
the appended entries are not.
   
   The next start then replayed nothing, and the consumer resumed from an older 
offset and processed those messages again, which is what the write-ahead log is 
there to prevent. Example: run 1 appends offset 1 of key A, the wrapped 
strategy has not confirmed it, the process stops. Run 2 reads A=1, truncates 
the log, and its `start()` fails. Run 3 replays nothing.
   
   Change (`LogWriter`, `WriteAheadResumeStrategy`): a new package-private 
`LogWriter` constructor takes the entries the new log starts with. With 
entries, it writes the header and the entries to a file next to the log 
(`<log>.tmp`), forces it, moves it over the log with `ATOMIC_MOVE` and forces 
the directory where the platform allows it. Until the move the previous log is 
unchanged, and after it the new log holds the entries on disk, so a crash, an 
exception or a power loss in `start()` no longer loses them. The writer then 
opens the moved log and writes the header and the entries again at the same 
positions (the same bytes, from the same serialization code), so its positions, 
layers and record count are built exactly as for a log it writes itself; it 
forces the file and only then schedules the flush task. Where the log cannot be 
replaced that way (the temporary file cannot be created, written or forced, or 
the move fails: no permission to create files in the directory of the log, a
  log file that is itself a mount point such as a single file mounted into a 
container, a file system without atomic rename), the temporary file is deleted 
(best effort, and only if this code created it), one WARN names the log and the 
reason, and `start()` takes the path of main: truncate the log in place, write 
the header and the carried entries, but then force the file right away, before 
the flush task is scheduled. So `start()` never fails where main starts, and in 
that fallback the carried entries are on disk before the flush schedule or 
anything after it can fail, and before a power loss after `start()` can drop 
them; only a crash during the in-place rewrite itself still loses them, as on 
main. A failure of the in-place rewrite itself (for example a read-only log) 
fails `start()`, as on main. There is no non-atomic move on purpose: 
`Files.move` without `ATOMIC_MOVE` deletes the log first and, where it cannot 
rename, copies the file and deletes the source. `start()` passes its c
 arried entries to that constructor and takes their entry information from the 
writer, instead of appending them after the truncate. Without entries to carry, 
the log is truncated in place as before. The public constructors, and the 
3-argument one the tests use, behave as before. Two details keep what 
truncating in place gave: the new log gets the POSIX permissions of the 
previous one (not its owner or group; a file system that cannot change them 
does not prevent the replacement), and when the log path is a symbolic link, 
the file it points to is replaced, not the link.
   
   Docs: the 4.24 upgrade guide section of camel-wal (added by CAMEL-26107) 
gets a paragraph about the temporary file, the atomic move, and the in-place 
rewrite with a WARN (forced right away) where the log cannot be replaced 
atomically.
   
   Related: CAMEL-26107 (the carry of the unconfirmed entries this fixes).
   
   Model: found with a TLA+ model of the write-ahead log (consumer threads, 
delegate callbacks in any order, synchronous throws, failed and timed-out 
sends, roll-over, flush ticks, process crashes and power losses at every step, 
including during `start()`). I added the fixed `start()` (temporary file, 
force, move, then the flush schedule) as a switch, and an action where 
`start()` throws after it read the previous log. On main, "every unconfirmed 
offset is replayed or superseded after a restart" is violated by a second crash 
in `start()`, by a failed `start()`, and by a power loss before the tick. With 
the fix it holds, together with the other invariants of the model (a processed 
entry is never replayed, the ring is on disk, no replay regression outside the 
documented roll-over case): 2 keys, 2 offsets each, one roll-over, 2 crashes 
and 1 failed start, 3,356,049 distinct states; the same with one of the crashes 
a power loss, 16,401,393 distinct states. TLC gives the same verdicts and
  the same counts on a smaller scope (265,771 and 657,813 distinct states). The 
model assumes the directory force succeeds. The in-place fallback is not in the 
model: it is the `start()` of main plus a force after the carried entries, so 
it has the verdicts of main for a crash during the rewrite and of the fix for a 
failed `start()` after the entries are written and for a power loss after 
`start()`.
   
   Tests: new `WriteAheadResumeStrategyStartFailureTest` (package-private, 
JUnit assertions as the module, no sleeps). A first run leaves A=1 unconfirmed 
in the log.
   
   - `unconfirmedEntryIsReplayedAfterAStartThatFailedAfterReadingIt`: the next 
`start()` fails because the flush task is rejected; the log must still hold A=1 
and the start after that must replay it. On main it fails (the log is empty 
after the failed start).
   - `logIsRewrittenInPlaceWhenTheNewLogCannotReplaceIt`: the temporary file 
cannot be created (a directory has its name), so the log cannot be replaced 
atomically; `start()` succeeds, the log holds A=1 right after it, the directory 
is not deleted, and the next start replays A=1 and confirms it and a new offset 
in the log. It fails on the previous version of this PR, whose `start()` failed 
there (it passes on main, which never uses a temporary file).
   - `previousLogIsKeptWhenNeitherTheNewLogNorTheLogCanBeWritten`: as above, 
and the log is read-only, so the in-place rewrite fails too: `start()` fails, 
the log still holds A=1, and the next start (log writable again) replays it. 
Skipped where the process can write to a read-only file (root). Passes on main 
too: it pins that a failure of the fallback fails the start without touching 
the log.
   - `unconfirmedEntryIsReplayedAfterTheNextStart`: control, no failure; A=1 is 
replayed and confirmed in the new log, a new offset is appended after it, and 
no temporary file is left. Passes on main too.
   - `newLogKeepsThePermissionsOfThePreviousOne` and 
`newLogReplacesTheFileASymbolicLinkPointsTo` (not on Windows): pin what 
truncating in place gave. They pass on main; each fails when its part of the 
change is removed.
   
   With the change: camel-wal 64 tests, 0 failures. The new class 20 times in 
one JVM: 120 run, 0 failures; `WriteAheadResumeStrategyRestartTest` 20 times: 
540 run, 0 failures. Formatter and import-sort checks pass. The WARN is not 
asserted: the module's tests have no log capture.
   
   I also ran the crash cases with real process crashes, outside the test 
suite: a child JVM appends A=1 and halts (`Runtime.halt`), a second child JVM 
halts inside `start()` (or `start()` throws), then the real `start()` and 
`loadCache()` run on the file left. On main both replay nothing; with the 
change both replay A=1. I did not add these to the module because the module's 
tests do not start JVMs.
   
   Not tested on Windows. The temporary file is closed before the move, and 
`ATOMIC_MOVE` replaces an existing file there; forcing a directory is not 
possible there, so the code skips it, and the move is then as durable as the 
file system makes it. Where the move fails there, the in-place fallback applies.
   
   Not changed (other results of the same model, all documented or low):
   
   - A crash while an append rewrites the log after a roll-over can leave a 
short log in the wrong order (documented in the 4.24 upgrade guide and in the 
code).
   - The WAL hands an offset to the wrapped strategy before it is forced to 
disk, so a power loss before the next tick can replay an older offset after a 
newer one was stored.
   - A full log that rolled over and looks fully confirmed is discarded without 
the WARN that a full log with unconfirmed entries gets.
   - A crash while the temporary file is written leaves it next to the log; the 
next start that carries entries truncates and reuses it.
   
   # 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-27418](https://issues.apache.org/jira/browse/CAMEL-27418).
   
   # 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 module, including the formatter and 
import-sort plugins. No option or generated file changed. 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