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 88a7e13e9f741fc68ab4a0d401e0bb08cbccbb18 Author: smjain <[email protected]> AuthorDate: Wed Sep 23 19:15:13 2026 +0530 CAMEL-24923: camel-core - Validate the distribution ratios in the weighted load balancer constructor The ratios are validated when the load balancer is created, so distributionRatioSum is never computed with an int overflow, and a weighted load balancer never holds an invalid sum. Co-Authored-By: Claude Opus 5.5 <[email protected]> --- .../loadbalancer/WeightedLoadBalancer.java | 31 ++++++++++++---------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java index 7b37670e403f..3b0685689d12 100644 --- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java +++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java @@ -29,23 +29,11 @@ public abstract class WeightedLoadBalancer extends QueueLoadBalancer { this.ratios = distributionRatios.stream() .map(DistributionRatio::new) .toList(); - this.distributionRatioSum = ratios.stream() - .mapToInt(DistributionRatio::getDistributionWeight).sum(); + this.distributionRatioSum = validateDistributionRatios(ratios); this.runtimeRatioSum = distributionRatioSum; } - public int getLastChosenProcessorIndex() { - return lastIndex; - } - - @Override - protected void doStart() throws Exception { - super.doStart(); - if (getProcessors().size() != ratios.size()) { - throw new IllegalArgumentException( - "Loadbalacing with " + getProcessors().size() - + " should match number of distributions " + ratios.size()); - } + private static int validateDistributionRatios(List<DistributionRatio> ratios) { // a ratio that is negative, or ratios that are all zero or add up to more than an int can hold, // would make the processor selection loop forever or fail on every exchange long sum = 0; @@ -64,6 +52,21 @@ public abstract class WeightedLoadBalancer extends QueueLoadBalancer { throw new IllegalArgumentException( "The sum of the distribution ratios must not be greater than " + Integer.MAX_VALUE + ", was: " + sum); } + return (int) sum; + } + + public int getLastChosenProcessorIndex() { + return lastIndex; + } + + @Override + protected void doStart() throws Exception { + super.doStart(); + if (getProcessors().size() != ratios.size()) { + throw new IllegalArgumentException( + "Loadbalacing with " + getProcessors().size() + + " should match number of distributions " + ratios.size()); + } } protected void decrementSum() {
