This is an automated email from the ASF dual-hosted git repository. apupier pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit c8e5646703a7512a0b4c2c642e965b9c9d12f7b5 Author: Aurélien Pupier <[email protected]> AuthorDate: Wed Nov 25 11:02:01 2026 +0100 CAMEL-26486: camel-cassandraql - delete(key) retries when the row was removed by another client A failed lightweight transaction on a missing row only returns the [applied] column, so reading the value column of that result threw an IllegalArgumentException instead of retrying. Also, add tests for concurrent deletes of the same key and fix the upgrade guide entry. Co-authored-by: IBM Bob <[email protected]> --- .../cassandra/CassandraKeyValueRepository.java | 5 +- .../cassandra/CassandraKeyValueRepositoryIT.java | 62 ++++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 16 ++++-- 3 files changed, 77 insertions(+), 6 deletions(-) diff --git a/components/camel-cassandraql/src/main/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepository.java b/components/camel-cassandraql/src/main/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepository.java index 0971f28c5b9a..c56f3e6c6479 100644 --- a/components/camel-cassandraql/src/main/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepository.java +++ b/components/camel-cassandraql/src/main/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepository.java @@ -309,8 +309,9 @@ public class CassandraKeyValueRepository extends ServiceSupport implements KeyVa if (row == null || row.getBoolean("[applied]")) { return deserialize(previous); } - // another client changed the entry in between, try again with the value it has now - previous = row.getByteBuffer(VALUE_COLUMN); + // another client changed the entry in between, try again with the value it has now. When the row does not + // exist anymore the result has the [applied] column only, and the next attempt then matches the missing row + previous = row.getColumnDefinitions().contains(VALUE_COLUMN) ? row.getByteBuffer(VALUE_COLUMN) : null; } throw new IllegalStateException("Unable to delete the key " + key + " after " + MAX_ATTEMPTS + " attempts"); } diff --git a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepositoryIT.java b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepositoryIT.java index 0f5fb18fb9d7..184ef143071c 100644 --- a/components/camel-cassandraql/src/test/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepositoryIT.java +++ b/components/camel-cassandraql/src/test/java/org/apache/camel/processor/keyvalue/cassandra/CassandraKeyValueRepositoryIT.java @@ -22,10 +22,13 @@ import java.util.List; import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.cassandra.integration.BaseCassandra; @@ -404,6 +407,65 @@ public class CassandraKeyValueRepositoryIT extends BaseCassandra { } } + @Test + public void testConcurrentDeleteOnSameKeyRemovesTheValueOnce() throws Exception { + int threads = 8; + for (int round = 0; round < 10; round++) { + String key = key("deleted" + round); + repository.put(key, "value1", null); + + // a delete that finds the row removed by another one must not fail + List<Object> previousValues = runConcurrently(threads, () -> repository.delete(key)); + + assertEquals(1, previousValues.stream().filter(Objects::nonNull).count(), key); + assertNull(repository.get(key), key); + } + } + + @Test + public void testConcurrentDeleteWithExpectedValueAndDeleteOnSameKey() throws Exception { + for (int round = 0; round < 10; round++) { + String key = key("deletedboth" + round); + repository.put(key, "value1", null); + + List<Object> results = runConcurrently(6, new Callable<Object>() { + private final AtomicInteger counter = new AtomicInteger(); + + @Override + public Object call() { + return counter.getAndIncrement() % 2 == 0 ? repository.delete(key) : repository.delete(key, "value1"); + } + }); + + // the value is removed by a single call, which returns it or true + long removed = results.stream().filter(r -> "value1".equals(r) || Boolean.TRUE.equals(r)).count(); + assertEquals(1, removed, key); + assertNull(repository.get(key), key); + } + } + + private static List<Object> runConcurrently(int threads, Callable<Object> task) throws Exception { + ExecutorService executor = Executors.newFixedThreadPool(threads); + try { + CountDownLatch start = new CountDownLatch(1); + List<Future<Object>> futures = new ArrayList<>(); + for (int t = 0; t < threads; t++) { + futures.add(executor.submit(() -> { + start.await(); + return task.call(); + })); + } + start.countDown(); + List<Object> results = new ArrayList<>(); + for (Future<Object> future : futures) { + results.add(future.get(60, TimeUnit.SECONDS)); + } + return results; + } finally { + executor.shutdownNow(); + } + } + @Override protected RouteBuilder createRouteBuilder() { return null; diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc index dfede46e80fa..c0a32601f4a3 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc @@ -933,13 +933,21 @@ again. The application closes a session it gives to the endpoint, such as with a === camel-cassandraql - the key value repository writes with lightweight transactions only -`CassandraKeyValueRepository` (and its `Named` variant) now stores and removes entries with Cassandra lightweight -transactions only. Before, `put` and `delete(key)` used a plain `INSERT` and `DELETE` while `putIfAbsent`, `replace` and +`CassandraKeyValueRepository` now stores and removes entries with Cassandra lightweight transactions only. Before, +`put` and `delete(key)` used a plain `INSERT` and `DELETE` while `putIfAbsent`, `replace` and `delete(key, expectedValue)` used lightweight transactions. The timestamp of a plain write is generated by the client and the one of a lightweight transaction by the server, so a plain write could hide a `replace` or a `delete(key, expectedValue)` that had returned `true`. Now `put` and `delete(key)` return the value they have actually replaced or -removed, also when another client writes the same key at the same time. As a lightweight transaction costs more than a -plain write, `put` and `delete(key)` are slower, and Cassandra's `SERIAL` consistency applies to them. +removed, also when another client writes the same key at the same time. + +As a lightweight transaction costs more than a plain write, `put` and `delete(key)` are slower, and the serial +consistency applies to them. A multi-datacenter deployment that writes with `LOCAL_ONE` or `LOCAL_QUORUM` pays a +cross-datacenter Paxos round for each of them, unless the driver uses a local serial consistency with +`basic.request.serial-consistency = LOCAL_SERIAL` in its configuration. Under contention on a key, a lightweight +transaction can also fail with a `WriteTimeoutException` of write type `CAS`, which a plain write never did. + +The protected methods `initInsertStatement`, `initInsertWithTtlStatement` and `initDeleteStatement` are removed, as the +repository no longer uses these statements. Also, when the value of an entry expires (TTL) Cassandra can leave the row behind without a value, which happened for an entry stored with a TTL that replaces an existing entry or that is stored by `replace`. Such a row is now considered
