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

   ## Description
   
   [CAMEL-27432](https://issues.apache.org/jira/browse/CAMEL-27432): 
`KafkaIdempotentRepository.add` and `remove` change the local cache at once and 
then write their record to the topic. The background poller reads the whole 
topic, including the instance's own records, and applied every record when it 
reached it. Nothing ordered the two, so an older record of a key could undo a 
newer local change of the same key, with only a poller that lags a little:
   
   - an exchange adds K, the route fails and the idempotent consumer removes K; 
the poller then reads the earlier `add K` and puts K back, so the redelivery is 
skipped as a duplicate and the message is never processed;
   - the other way round, an earlier `remove K` read after a new add drops the 
key, and a duplicate is accepted.
   
   The window lasts until the poller reads the newer record (about one poll), 
but the outcome is a lost or a duplicated message.
   
   ### Fix
   
   - `KafkaChangelog.send` returns the `RecordMetadata` of the record.
   - Each `add` and `remove` keeps the partition and offset of its record until 
the poller reads it back. The poller skips a record of that key in the same 
partition with a lower offset (and the record of the write itself); a newer 
record, e.g. of a peer, is applied as before.
   - A record of the key read while the send is still in flight is held and 
decided once the offset is known. When the send fails, the held records are 
applied as they come, and the rollback of `add` still keeps a key added by a 
peer (CAMEL-26711).
   - The local changes and the records applied by the poller share one lock, 
which is never held while a record is sent. With `startupOnly=true` there is no 
background poller, and nothing is tracked. At most `maxCacheSize` writes are 
tracked.
   
   ### Not changed (follow-ups)
   
   - An `add` whose send fails without telling whether the record was written 
(delivery timeout, interrupt) rolls back locally; if the broker wrote the 
record, it still rejects later copies of the message. The JIRA proposes a 
compensating `remove`; it is a larger change and left out here.
   - `clear()` is not ordered against the keys, and `KafkaKeyValueRepository` 
has the same order problem.
   
   ## Tests
   
   - 5 new tests in `KafkaIdempotentRepositoryTest`: an older own add or 
remove, an older peer record, and records read while the send is in flight. 
Three of them fail on main (and one more assertion of the in-flight test); the 
other two guard that newer records are still applied.
   - `KafkaIdempotentRepositoryTest`, `KafkaKeyValueRepositoryTest`, 
`KafkaChangelogProducerConfigTest` pass.
   - `KafkaIdempotentRepositoryEagerIT`, `NonEagerIT` and `PersistenceIT` (13 
tests) pass against a Kafka container.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_
   
   🤖 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