This is an automated email from the ASF dual-hosted git repository.

nfilotto pushed a commit to branch camel-4.8.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-4.8.x by this push:
     new 5eb45a6efd9 CAMEL-21432: camel-core - Prevent 0 as cache size for 
multicast EIP (#16240)
5eb45a6efd9 is described below

commit 5eb45a6efd9bef05cf0f7dd16ebfcaf0622e6cc3
Author: Nicolas Filotto <essob...@users.noreply.github.com>
AuthorDate: Tue Nov 12 14:22:11 2024 +0100

    CAMEL-21432: camel-core - Prevent 0 as cache size for multicast EIP (#16240)
    
    ## Motivation
    
    The splitter and multicast EIPs set the cache size to 0 by default which 
causes endless evictions
    
    ## Modifications:
    
    1. Prevent to set 0 as cache size at the cache level
    2. Use the value of `CamelContextHelper.getMaximumCachePoolSize` as the 
default cache size for the splitter and multicast EIPs
---
 .../java/org/apache/camel/processor/MulticastProcessor.java   | 11 +++++------
 .../org/apache/camel/support/cache/SimpleLRUCacheTest.java    |  7 +++++++
 .../java/org/apache/camel/support/cache/SimpleLRUCache.java   |  3 +++
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
index ea91c04a4e3..f9514de06a1 100644
--- 
a/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
+++ 
b/core/camel-core-processor/src/main/java/org/apache/camel/processor/MulticastProcessor.java
@@ -24,7 +24,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.Executor;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.RejectedExecutionException;
@@ -185,7 +184,7 @@ public class MulticastProcessor extends 
AsyncProcessorSupport
     private boolean shutdownAggregateExecutorService;
     private final long timeout;
     private final int cacheSize;
-    private final ConcurrentMap<Processor, Processor> errorHandlers;
+    private final Map<Processor, Processor> errorHandlers;
     private final boolean shareUnitOfWork;
 
     public MulticastProcessor(CamelContext camelContext, Route route, 
Collection<Processor> processors) {
@@ -196,7 +195,7 @@ public class MulticastProcessor extends 
AsyncProcessorSupport
                               AggregationStrategy aggregationStrategy) {
         this(camelContext, route, processors, aggregationStrategy, false, null,
              false, false, false, 0, null,
-             false, false, 
CamelContextHelper.getMaximumCachePoolSize(camelContext));
+             false, false, 0);
     }
 
     public MulticastProcessor(CamelContext camelContext, Route route, 
Collection<Processor> processors,
@@ -225,9 +224,9 @@ public class MulticastProcessor extends 
AsyncProcessorSupport
         this.parallelAggregate = parallelAggregate;
         this.processorExchangeFactory = camelContext.getCamelContextExtension()
                 
.getProcessorExchangeFactory().newProcessorExchangeFactory(this);
-        this.cacheSize = cacheSize;
-        if (cacheSize >= 0) {
-            this.errorHandlers = (ConcurrentMap) 
LRUCacheFactory.newLRUCache(cacheSize);
+        this.cacheSize = cacheSize == 0 ? 
CamelContextHelper.getMaximumCachePoolSize(camelContext) : cacheSize;
+        if (this.cacheSize > 0) {
+            this.errorHandlers = LRUCacheFactory.newLRUCache(this.cacheSize);
         } else {
             // no cache
             this.errorHandlers = null;
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java
index 959fee7f4ec..6c38fbd069a 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/support/cache/SimpleLRUCacheTest.java
@@ -326,6 +326,13 @@ class SimpleLRUCacheTest {
         assertTrue(consumed.contains("Two"));
     }
 
+    @ParameterizedTest
+    @ValueSource(ints = { 0, -1 })
+    void validateCacheSize(int maximumCacheSize) {
+        assertThrows(IllegalArgumentException.class, () -> new 
SimpleLRUCache<>(16, maximumCacheSize, x -> {
+        }));
+    }
+
     @ParameterizedTest
     @ValueSource(ints = { 1, 2, 5, 10, 20, 50, 100, 1_000 })
     void concurrentPut(int maximumCacheSize) throws Exception {
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
index c1fcb77d41b..ea7e614c805 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/cache/SimpleLRUCache.java
@@ -68,6 +68,9 @@ public class SimpleLRUCache<K, V> extends 
ConcurrentHashMap<K, V> {
 
     public SimpleLRUCache(int initialCapacity, int maximumCacheSize, 
Consumer<V> evicted) {
         super(initialCapacity, DEFAULT_LOAD_FACTOR);
+        if (maximumCacheSize <= 0) {
+            throw new IllegalArgumentException("The maximum cache size must be 
greater than 0");
+        }
         this.maximumCacheSize = maximumCacheSize;
         this.evict = Objects.requireNonNull(evicted);
     }

Reply via email to