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

   # Description
   
   Fixes the flaky `MongoDbTailableCursorConsumerIT` (CAMEL-26407).
   
   The tests read the persisted tail position right after `stopRoute(...)`. 
They saw the previous record (`299` instead of `300`, or a date one minute 
behind), and after a restart the last record was delivered again (`301` 
messages instead of `300`).
   
   **Root cause.** The tail position is persisted only in the `finally` block 
of the tailing thread. `stop()` waits for that thread, but only up to the 
shutdown await timeout (10 seconds by default), and only while the thread is 
reading from MongoDB. When the thread is still blocked in a read (for example a 
connection that stopped answering), `stop()` interrupts it and returns without 
waiting. The thread never reaches its persist before the caller reads the 
tracking document.
   
   **Fix.**
   - `MongoAbstractConsumerThread.stop()` calls a new `afterStopped()` hook 
once the thread has ended or was left behind. The default is empty.
   - `MongoDbTailingThread` overrides `afterStopped()` to persist the tail 
position. The persist in `doRun()` is kept, so cursor regeneration still saves 
progress.
   - `MongoDbChangeStreamsThread` inherits the empty default, so its behavior 
does not change.
   
   **Tests.**
   - New deterministic unit test 
`MongoDbConsumerStopTest.stopPersistsTheTailPositionOfAConsumerThreadLeftBehind`:
 the consumer thread is blocked on a read that does not return, and the test 
asserts that `stopRoute` persists the position. It fails without the fix and 
passes with it. It uses latches and an explicit await bound, with no sleeps.
   - `MongoDbTailableCursorConsumerIT`: 9/9 pass.
   - All `camel-mongodb` unit tests: 1097 pass.
   
   **Not included.** I did not change how long `awaitStopped()` waits. A 
consumer thread stuck in a read is still left behind after the timeout, and its 
own persist can still race with the caller's persist in that edge case.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   
   - [x] I checked there is a [JIRA 
issue](https://issues.apache.org/jira/browse/CAMEL-26407) filed for the change.
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful 
subject line and body.
   - [x] I ran `mvn formatter:format impsort:sort` on `camel-mongodb`; the 
formatter made no further changes.
   - [ ] I have run `mvn clean install -DskipTests` from the root folder. Not 
run for this PR; only the `camel-mongodb` module and its dependencies were 
built.
   
   # AI-assisted contributions
   
   - [x] This PR includes AI-generated code. The commit has a `Co-authored-by` 
trailer, and this description identifies the AI tool used: Bob (IBM AI coding 
assistant), on behalf of Aurelien Pupier.
   


-- 
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