allthingssecurity opened a new pull request, #30834: URL: https://github.com/apache/camel/pull/30834
# Description [CAMEL-27436](https://issues.apache.org/jira/browse/CAMEL-27436) `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, a network partition), 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 did not stop there. The row has no owner, and the old holder A still has the key in its keep-alive set, so its later statements on the key matched the row by its key only: 1. **A's remove deleted the lease of the new holder B.** The remove runs when A's processing fails (`removeOnFailure`), or with the file read lock and `readLockRemoveOnCommit=true` once the file is processed. B is alive, keeps its lease alive and still processes the message, but the next copy of the message was acquired by a third instance (or by A again): a third processing while the holder is healthy. B's keep-alive and B's confirm or remove then acted on the row of that third instance, and so on. 2. **A's keep-alive refreshed B's lease.** If B then crashes, its lease does not expire while A's exchange runs, so no instance takes the key over. 3. **B's remove deleted a key that A confirmed.** When A's processing succeeds, A's confirm sets B's lease to null (confirmed); when B's processing then fails, B's remove deleted the confirmed key, and the message was processed again although it was processed. Change (no schema change, no new option): - Per key it holds, the repository now remembers the `createdAt` it wrote last (insert, takeover, or a keep-alive that refreshed it), in a map whose key set is the existing keep-alive set (`getProcessorNameMessageIdSet()` returns that key set: a key can still be removed from it, but no longer added; the upgrade guide says so). - A takeover only happens when the stored `createdAt` is older than `lockMaxAgeMillis`, and writes a `createdAt` later than it by more than `lockMaxAgeMillis` (whatever the clocks of the two instances, as both values are compared in the database). So the remove and the keep-alive of a key the instance holds add `AND createdAt < ?`, with the remembered value plus half of `lockMaxAgeMillis + lockKeepAliveIntervalMillis`. The margin leaves room on one side for a keep-alive of the instance itself whose outcome it does not know yet (one that runs while the key is confirmed or removed, or one that failed after the database committed it, when it ran within the margin), and on the other side for the rounding of the column (MySQL `TIMESTAMP` without fractional seconds rounds to the second). It also matches an older `createdAt`, so a lease that expired while nobody took it over is still refreshed and removed by its instance (the CAMEL-27013 case). A null `createdAt` (confirmed) never matche s, which fixes 3. - The margin is added both to the instant and to the local time of the JVM, and the later bound is used. A `createdAt` column without time zone (the table in the docs) holds the local time, which goes back an hour at the end of daylight saving time; with the instant alone, every lease refreshed in the last seconds before that change would look later than its bound, so every instance would mark all its keys lost at that hour, and a failed exchange could not remove its key for about an hour. For a column with time zone, the bound is up to an hour later for a lease written shortly before that change, so the remove and the keep-alive then match as on main. - When the keep-alive matches no row for a key, the lease is marked lost: the key is no longer kept alive or removed, one WARN names the key, and the instance still treats the key as a duplicate until its exchange removes or confirms it. A remove that matches no row returns false and logs a WARN. - A key the instance does not keep alive (a key removed by hand, a key added before a restart of the JVM, or a key inserted earlier in the same transaction) is removed by its key, as before. - The keep-alive sends one `UPDATE` per key in its transaction instead of a batch, as it needs the update count of each key and a batch may return `SUCCESS_NO_INFO`. - **A confirm is not made conditional.** It still confirms the key by its key, also after a takeover: the message was processed, so the key should be a duplicate from now on. The other instance then neither removes the confirmed key (3) nor keeps it alive. Ignoring A's confirm instead would let B's failure delete the key and have the message processed a third time after A processed it. The class Javadoc, the Orphan Lock section of the sql component docs (with its catalog copy) and the 4.24 upgrade guide describe the new behaviour. The upgrade guide says that the repository adds `AND createdAt < ?` to `deleteString` for a key it holds (as it already does to `queryString` and to the update query), so a custom `deleteString` must end with its `WHERE` condition, and that `lockMaxAgeMillis` should exceed `lockKeepAliveIntervalMillis` by a few seconds at least. Model: found with a TLA+ model of the lease (one message id, 2 or 3 instances, each with an exchange thread and a keep-alive thread, discrete time, pauses, crashes and restarts, failing statements including commit-then-throw, a partial partition, redeliveries, stale keep-alive snapshots). On main "two instances process the message at the same time only when one of them missed lockMaxAge" is violated in 14 steps (the same with the file read lock and `readLockRemoveOnCommit=true`). I added the change as a switch: with 2 instances, one crash, one failure of each kind, one failed processing and one partition, that property, "a confirmed key is never acquired again", "an instance never deletes a lease another instance wrote" and "an instance never keeps refreshing a lease another instance wrote" all hold (4,827,584 distinct states, exhaustive; TLC gives the same count), and with `readLockRemoveOnCommit=true` too (2,151,296 states). "An instance never confirms a lease another instance w rote" is violated with the change, as intended (see above); with no instance missing `lockMaxAgeMillis` it holds together with the others, also with 3 instances (8,518,896 states). With a margin larger than `lockMaxAgeMillis` the model violates "never deletes a lease another instance wrote" again, so the margin is what the fix relies on. Tests: new `JdbcOrphanLockAwareIdempotentRepositoryTakeoverTest` (package-private, JUnit assertions as the module). Three repository instances share one H2 database, each with its scheduled keep-alive (`lockMaxAgeMillis=1000`, `lockKeepAliveIntervalMillis=40`); only node A loses the database, and node B takes the key over once A's lease is really older than `lockMaxAgeMillis` (Awaitility, no sleeps): - `testOldHolderDoesNotRemoveTheLeaseOfTheNewHolder` (1): A's remove must return false and leave B's lease, and node C must not acquire the key while B holds it. On main A's remove returns true. - `testOldHolderDoesNotKeepAliveTheLeaseOfTheNewHolder` (2): B stops (as if it crashed); C must take the key over once B's lease expires, while A's keep-alive runs. On main A keeps B's lease alive and C never acquires the key. - `testNewHolderDoesNotRemoveAKeyThatTheOldHolderConfirmed` (3): A confirms, B's remove must return false and leave the confirmed key. On main B's remove deletes it. - `testLeaseWrittenBeforeTheEndOfDaylightSavingTimeStillMatches`: for leases written around the end and the start of daylight saving time in Europe/Berlin (and on a day without change), the bound must be later than the lease plus the margin, as an instant and in local time. With the instant alone it fails for the leases written in the last 520 ms before the end of daylight saving time. The first 3 fail on main. `JdbcOrphanLockAwareIdempotentRepositoryOutageTest` holds a statement by its exact SQL; it now matches the start of the SQL, as the remove of a held key has the extra condition. With the change: camel-sql idempotent, key-value and aggregation repository tests 177 tests, 0 failures; the new class 20 times in one JVM, 80 runs, 0 failures (860 with the other 7 orphan-lock test classes). Formatter and import-sort checks pass. Not changed: - When the keep-alive of an instance failed for longer than half of `lockMaxAgeMillis - lockKeepAliveIntervalMillis` and its next round committed although it reported a failure (the connection was lost during the commit), the instance cannot tell its own lease from one taken over: it marks it lost and stops refreshing it, and another instance may take the key over `lockMaxAgeMillis` later, as after a longer outage. The docs say so. The model does not show this case, as it counts the time an instance missed `lockMaxAgeMillis` from the writes the instance knows of. - Clock skew between instances larger than `lockMaxAgeMillis - lockKeepAliveIntervalMillis` still lets an instance take over the live lease of another (the `createdAt` is written and compared with the clock of each instance). The docs do not mention clock synchronisation; using the database clock would remove it. I will file a follow-up JIRA. - When a confirm runs inside a transaction of the caller, a keep-alive that started just before it can wait for the row lock of that transaction, and no other lease of the instance is refreshed meanwhile. Not reproduced; I will file a follow-up JIRA if wanted. # 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-27436](https://issues.apache.org/jira/browse/CAMEL-27436). # 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, and copied the changed component doc to the catalog. No option 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]
