This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 118d2c8b86f7 CAMEL-28002: camel-jcache - JCacheAggregationRepository
keeps a group created concurrently by another node (#31224)
118d2c8b86f7 is described below
commit 118d2c8b86f738276bf1082a62fc14c5a1175e34
Author: Adriano Machado <[email protected]>
AuthorDate: Wed Dec 2 03:34:24 2026 -0500
CAMEL-28002: camel-jcache - JCacheAggregationRepository keeps a group
created concurrently by another node (#31224)
- CAMEL-28002: camel-jcache - JCacheAggregationRepository keeps a group
created concurrently by another node
- CAMEL-28002: camel-jcache - address review of the optimistic paths of
JCacheAggregationRepository
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../aggregate/JCacheAggregationRepository.java | 26 ++++++++--------
.../aggregate/JCacheAggregationRepositoryTest.java | 35 ++++++++++++++++++++++
2 files changed, 47 insertions(+), 14 deletions(-)
diff --git
a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
index 0d8062e6a32d..9130fdd9483a 100644
---
a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
+++
b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepository.java
@@ -138,23 +138,21 @@ public class JCacheAggregationRepository extends
ServiceSupport
LOG.trace("Adding an Exchange with ID {} for key {} in an optimistic
manner.", newExchange.getExchangeId(), key);
if (oldExchange == null) {
DefaultExchangeHolder newHolder =
DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
- DefaultExchangeHolder oldHolder = cache.getAndPut(key, newHolder);
- if (oldHolder != null) {
- Exchange exchange = unmarshallExchange(camelContext,
oldHolder);
- LOG.error(
- "Optimistic locking failed for exchange with key {}:
IMap#putIfAbsend returned Exchange with ID {}, while it's expected no exchanges
to be returned",
- key,
- exchange != null ? exchange.getExchangeId() :
"<null>");
-
+ // putIfAbsent, not getAndPut: a group created concurrently (by
another thread or node) must be kept, not
+ // overwritten
+ if (!cache.putIfAbsent(key, newHolder)) {
+ LOG.debug(
+ "Optimistic locking failed for exchange with ID {} and
key {}: Cache#putIfAbsent found an existing Exchange, while none was expected",
+ newExchange.getExchangeId(), key);
throw new OptimisticLockingException();
}
} else {
DefaultExchangeHolder oldHolder =
DefaultExchangeHolder.marshal(oldExchange, true, allowSerializedHeaders);
DefaultExchangeHolder newHolder =
DefaultExchangeHolder.marshal(newExchange, true, allowSerializedHeaders);
if (!cache.replace(key, oldHolder, newHolder)) {
- LOG.error(
- "Optimistic locking failed for exchange with key {}:
IMap#replace returned no Exchanges, while it's expected to replace one",
- key);
+ LOG.debug(
+ "Optimistic locking failed for exchange with ID {} and
key {}: Cache#replace replaced no Exchange, while it's expected to replace one",
+ newExchange.getExchangeId(), key);
throw new OptimisticLockingException();
}
}
@@ -184,9 +182,9 @@ public class JCacheAggregationRepository extends
ServiceSupport
if (optimistic) {
LOG.trace("Removing an exchange with ID {} for key {} in an
optimistic manner.", exchange.getExchangeId(), key);
if (!cache.remove(key, holder)) {
- LOG.error(
- "Optimistic locking failed for exchange with key {}:
IMap#remove removed no Exchanges, while it's expected to remove one.",
- key);
+ LOG.debug(
+ "Optimistic locking failed for exchange with ID {} and
key {}: Cache#remove removed no Exchange, while it's expected to remove one",
+ exchange.getExchangeId(), key);
throw new OptimisticLockingException();
}
LOG.trace("Removed an exchange with ID {} for key {} in an
optimistic manner.", exchange.getExchangeId(), key);
diff --git
a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTest.java
b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTest.java
index e215d030942b..a89c51f25080 100644
---
a/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTest.java
+++
b/components/camel-jcache/src/test/java/org/apache/camel/component/jcache/processor/aggregate/JCacheAggregationRepositoryTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.jcache.processor.aggregate;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
+import
org.apache.camel.spi.OptimisticLockingAggregationRepository.OptimisticLockingException;
import org.apache.camel.support.DefaultExchange;
import org.junit.jupiter.api.Test;
@@ -85,6 +86,40 @@ public class JCacheAggregationRepositoryTest extends
JCacheAggregationRepository
}
}
+ @Test
+ public void
checkOptimisticAddOfNewExchangeKeepsGroupCreatedByAnotherNode() throws
Exception {
+ JCacheAggregationRepository repoOne = createRepository(true);
+ JCacheAggregationRepository repoTwo = createRepository(true);
+
+ repoOne.start();
+ repoTwo.start();
+
+ try {
+ final String key = "concurrentKey";
+ final String firstBody = "The group created first, by node one.";
+ Exchange firstEx = createExchangeWithBody(firstBody);
+ assertNull(repoOne.add(context(), key, null, firstEx), "No group
should exist before node one adds it.");
+
+ // node two also believes the group does not exist yet: it must
fail, and must not replace the group of
+ // node one, or the exchange of node one is lost when node two
retries
+ Exchange secondEx = createExchangeWithBody("The group created
concurrently, by node two.");
+ assertThrows(OptimisticLockingException.class,
+ () -> repoTwo.add(context(), key, null, secondEx));
+
+ assertEquals(firstBody, repoOne.get(context(),
key).getIn().getBody());
+
+ // the retry of node two reads the group again and aggregates on
top of the group of node one
+ Exchange group = repoTwo.get(context(), key);
+ final String aggregatedBody = "The group of node one, with the
exchange of node two aggregated.";
+ Exchange previous = repoTwo.add(context(), key, group,
createExchangeWithBody(aggregatedBody));
+ assertEquals(firstBody, previous.getIn().getBody());
+ assertEquals(aggregatedBody, repoOne.get(context(),
key).getIn().getBody());
+ } finally {
+ repoOne.stop();
+ repoTwo.stop();
+ }
+ }
+
@Test
public void checkThreadSafeAddOfNewExchange() throws Exception {
JCacheAggregationRepository repoOne = createRepository(false);