shashank created CAMEL-27436:
--------------------------------

             Summary: camel-sql - JdbcOrphanLockAwareIdempotentRepository: 
after another instance took a key over, the old holder removes and keeps alive 
the lease of the new holder
                 Key: CAMEL-27436
                 URL: https://issues.apache.org/jira/browse/CAMEL-27436
             Project: Camel
          Issue Type: Bug
          Components: camel-sql
            Reporter: shashank
            Assignee: shashank


h3. What happens

{{JdbcOrphanLockAwareIdempotentRepository}} documents one limitation of its 
lease: when an instance cannot refresh the lease of a key for longer than 
{{lockMaxAgeMillis}} (a long GC pause, a database outage or a network partition 
of that instance), another instance takes the key over, and the message is 
processed twice, once on each instance (CAMEL-27013 made sure the first 
instance does not take its own key over a second time).

The damage does not stop there. The row has no owner, and the old holder A 
still has the key in its keep-alive set. Its later statements on the key match 
the row by its key only:

* *remove* ({{removeOnFailure}} of the Idempotent Consumer, or the file read 
lock with {{readLockRemoveOnCommit=true}} once the file is processed) deletes 
the row, which is now the lease of the new holder B. B is alive, keeps its 
lease alive and still processes the message, but the next copy of the message 
(a redelivery, the file polled again) is acquired by a third instance C, or by 
A again: a third processing while the holder is healthy. B's keep-alive then 
refreshes C's row, and B's confirm or remove acts on C's row, and so on.
* *keep-alive* refreshes B's lease. If B then crashes, its lease never expires 
while A's exchange runs, so no instance takes the key over.
* symmetrically, when A's processing succeeds, A's confirm sets B's lease to 
null (confirmed); when B's processing then fails, B's remove deletes the 
confirmed key, and the message is processed again although it was processed.

h3. How to reproduce

Three repository instances on one H2 database, only A loses the database: A 
adds the key; during A's outage B takes it over once A's lease is older than 
{{lockMaxAgeMillis}}; the database comes back. A's {{remove}} returns true and 
deletes B's row, and C's {{add}} then returns true while B still holds the key. 
In the second case A confirms, B's {{remove}} deletes the confirmed key, and 
C's {{add}} returns true. In the third case B stops (crash), and C never takes 
the key over while A's keep-alive runs.

Found with a TLA+ model of the lease (instances with exchange and keep-alive 
threads, pauses, crashes, database failures, partitions): "two instances 
process a message at the same time only when one of them missed lockMaxAge" is 
violated in 14 steps.

h3. Fix

Without a schema change: the instance remembers, per key it holds, the 
{{createdAt}} it wrote last (insert, takeover or keep-alive). A takeover writes 
a {{createdAt}} later than that by more than {{lockMaxAgeMillis}}, so the 
remove and the keep-alive of the key only match a {{createdAt}} earlier than 
halfway between {{lockKeepAliveIntervalMillis}} and {{lockMaxAgeMillis}} after 
it (room for a keep-alive of its own still in flight and for the rounding of 
the column; the margin is also added to the local time of the JVM, so that a 
column without time zone still matches the instance's own lease at the end of 
daylight saving time). When they match nothing, the instance logs a WARN and 
stops keeping the key alive, and still treats it as a duplicate until its 
exchange ends. A confirm still confirms the key by its key, as the message was 
processed; the other instance then does not remove the confirmed key.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to