[ 
https://issues.apache.org/jira/browse/CAMEL-27432?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Work on CAMEL-27432 started by shashank.
----------------------------------------
> camel-kafka - KafkaIdempotentRepository applies an older record of a key over 
> a newer local add or remove, so a redelivery is dropped or a duplicate 
> accepted
> -------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-27432
>                 URL: https://issues.apache.org/jira/browse/CAMEL-27432
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-kafka
>            Reporter: shashank
>            Assignee: shashank
>            Priority: Major
>
> h3. What happens
> {{KafkaIdempotentRepository.add}} and {{remove}} change the local cache at 
> once and then write their record to the topic. The background poller applies 
> every record of the topic when it reaches it, including the instance's own 
> earlier records and older records of other instances. Nothing orders the two, 
> so an older record of a key can be applied after a newer local change of the 
> same key. No fault is needed, only a poller that lags a little:
> *1. A redelivery is dropped.* An exchange adds K, the route fails, and the 
> idempotent consumer calls {{remove(K)}}. The poller then reads the instance's 
> own earlier {{add K}} and puts K back. The redelivery gets {{add(K) == 
> false}} and is skipped as a duplicate, so the message is never processed.
> *2. A duplicate is accepted on one instance.* The redelivery adds K again, 
> then the poller reads the earlier {{remove K}} and deletes the new entry. A 
> duplicate taken by another thread of the same instance gets {{add(K) == 
> true}}.
> *3.* The same happens with an older record of another instance, so skipping 
> the instance's own records is not enough.
> A related case: when the send of an {{add}} fails without telling whether the 
> record was written (a delivery timeout, or an interrupt while waiting), 
> {{add}} rolls back and throws. If the broker wrote the record after all, 
> every instance applies it and every later start replays it, so every later 
> copy of the message is rejected everywhere.
> h3. How to reproduce
> With a {{MockProducer}} and a {{MockConsumer}} under the real background 
> poller: {{add(K)}} (offset 0), then {{remove(K)}} (offset 1) while the poller 
> has not read offset 0 yet; the poller reads offset 0; {{add(K)}} now returns 
> false. For the timeout case: the send of {{add(K)}} fails with a delivery 
> {{TimeoutException}}, the record is in the topic at offset 0, the poller 
> reads it, and {{add(K)}} of the redelivery returns false.
> h3. Fix
> Each {{add}} and {{remove}} remembers the partition and offset of its record 
> (from the {{RecordMetadata}}), and the poller does not apply a record of that 
> key at the same partition with a lower or equal offset; a record read while 
> the send is in flight is decided when it completes. The key is forgotten once 
> the poller has read the instance's own record back (at most {{maxCacheSize}} 
> keys are kept). After an {{add}} whose outcome is unknown, the repository 
> writes a {{remove}} of the key and waits for it before {{add}} throws, 
> holding the key until then; the {{remove}} is not sent under the repository's 
> lock, as the producer can block up to {{max.block.ms}}. Such an {{add}} can 
> take up to {{delivery.timeout.ms}} longer to fail. That is best effort: if an 
> {{add}} of the key was read while waiting, the instance cannot tell it from 
> one of another instance and keeps the key, as the rollback does since 
> CAMEL-26711. {{clear()}} on a topic with more than one partition is not 
> ordered against the keys (docs recommend one partition), and 
> {{KafkaKeyValueRepository}} has the same order problem; both are not changed 
> here.
> Found with a TLA+ model (several instances and threads, duplicates, failures, 
> timeouts, crashes): on main an add after a remove on the same instance can be 
> rejected and two threads of one instance can hold the same key; with the fix 
> neither happens.
> _Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to