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

   # Description
   
   `CassandraKeyValueRepositoryIT` is flaky (`testReplaceWithTtl`, 
`testReplaceMatchingOldValue`, `testDeleteWithExpectedValueMatching`, ...: 
`expected: <value2> but was: <value1>`). The previous attempt (#28851) isolated 
the tests from each other with unique keys, but the failures remained: the 
cause is in `CassandraKeyValueRepository`, not in the test.
   
   ## Root cause
   
   `put()` and `delete(key)` used plain `INSERT`/`DELETE`, while 
`putIfAbsent()`, `replace()` and `delete(key, expectedValue)` use lightweight 
transactions (LWT). The timestamp of a plain write is generated by the 
**client** (microseconds), the one of an LWT by the **server** (Paxos ballot). 
A plain write that immediately precedes an LWT can therefore carry a timestamp 
equal or higher than the LWT, which returns `[applied]=true` but is hidden by 
the plain write: the next `get()` returns the old value. This is a product bug, 
not only a test one.
   
   Measured against a real Cassandra 5.0.9 (300-500 operations each):
   
   | Setup | Applied but lost |
   |---|---|
   | plain `put` then `replace` / `delete(key, expected)` | 6-38% |
   | plain write with a server-side timestamp | 10-29% (no fix) |
   | `SERIAL`/`LOCAL_SERIAL` read, `QUORUM`/`ALL` write | no fix |
   | `put` implemented with LWT | 0 |
   
   ## Changes
   
   - `put()` and `delete(key)` use lightweight transactions (`INSERT ... IF NOT 
EXISTS` then `UPDATE ... IF value = <observed>` / `DELETE ... IF value = <read 
value>`, retried up to 50 times when another client wins a race). The previous 
value they return is now the one actually replaced or removed (it could be 
stale before).
   - A row whose value expired (TTL) can survive without a value (`UPDATE ... 
USING TTL` only expires the column), so `contains()` returned `true` for an 
expired entry. Such a row is now considered absent by `contains()`, `keys()`, 
`size()` and `putIfAbsent()`.
   - 8 new tests: put then replace/delete in a loop, delete then put, expiry 
through `put` and `replace`, `keys`/`size` ignoring expired entries, 
`putIfAbsent` after expiry, 8 threads x 20 concurrent puts on one key. 5 of 
them fail with the previous implementation.
   - Entry in the 4.24 upgrade guide: `put` and `delete(key)` cost an LWT 
(about 2x a plain write locally, 0.8 ms to 1.4-1.9 ms) and `SERIAL` consistency 
applies to them.
   
   `clear()` still uses `TRUNCATE`, which is not an LWT; it is documented in 
the class javadoc.
   
   ## Validation
   
   `mvn verify` of `camel-cassandraql`: 15 unit tests and 92 ITs pass; 
`CassandraKeyValueRepositoryIT` (33 tests) was run several times in a row 
without failure.
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 
uses the `main` branch)
   
   # Tracking
   
   - [x] JIRA issue: https://issues.apache.org/jira/browse/CAMEL-26486
   
   # 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 have run the formatter and import sorter locally and committed all 
auto-generated changes.
   
   # AI-assisted contributions
   
   - [x] This PR includes AI-generated code; the commit has a `Co-authored-by` 
trailer and the AI tool is identified below.
   
   _IBM Bob on behalf of @apupier_
   


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