camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aka...@apache.org
Subject svn commit: r1027242 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/main/java/org/apache/camel/model/loadbalancer/ camel-core/src/main/java/org/apache/camel/processor/loadbalancer/ camel-core/src/test/java/org/apache...
Date Mon, 25 Oct 2010 19:30:44 GMT
Author: akarpe
Date: Mon Oct 25 19:30:43 2010
New Revision: 1027242

URL: http://svn.apache.org/viewvc?rev=1027242&view=rev
Log:
CAMEL-3197 - Modified Distribution Ratio from a list to delimited String and modiied behavior
throwing soft warning regarding best effort distribution to throw a CamelException.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/loadbalancer/WeightedLoadBalancerDefinition.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRandomLoadBalancer.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRoundRobinLoadBalancer.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRandomLoadBalanceTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRoundRobinLoadBalanceTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRandomLoadBalance.xml
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRoundRobinLoadBalance.xml

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/LoadBalanceDefinition.java
Mon Oct 25 19:30:43 2010
@@ -190,12 +190,45 @@ public class LoadBalanceDefinition exten
     /**
      * Uses weighted load balancer
      *
-     * @param roundRobin               used to set the processor selection algorithm.
-     * @param distributionRatioList    ArrayList<Long> of weighted ratios for distribution
of messages.
+     * @param roundRobin                   used to set the processor selection algorithm.
+     * @param distributionRatio            String of weighted ratios for distribution of
messages.
      * @return the builder
      */
-    public LoadBalanceDefinition weighted(boolean roundRobin, ArrayList<Integer> distributionRatioList)
{
+    public LoadBalanceDefinition weighted(boolean roundRobin, String distributionRatio) {
         WeightedLoadBalancer weighted;
+        List<Integer> distributionRatioList = new ArrayList<Integer>();
+        
+        String[] ratios = distributionRatio.split(":");
+        for (String ratio : ratios) {
+            distributionRatioList.add(new Integer(ratio));
+        }
+        
+        if (!roundRobin) {
+            weighted = new WeightedRandomLoadBalancer(distributionRatioList);
+        } else {
+            weighted = new WeightedRoundRobinLoadBalancer(distributionRatioList);
+        }
+        loadBalancerType = new LoadBalancerDefinition(weighted);
+        return this;
+    }
+    
+    /**
+     * Uses weighted load balancer
+     *
+     * @param roundRobin                   used to set the processor selection algorithm.
+     * @param distributionRatio            String of weighted ratios for distribution of
messages.
+     * @param distributionRatioDelimiter   String containing delimiter to be used for ratios
+     * @return the builder
+     */
+    public LoadBalanceDefinition weighted(boolean roundRobin, String distributionRatio, String
distributionRatioDelimiter) {
+        WeightedLoadBalancer weighted;
+        List<Integer> distributionRatioList = new ArrayList<Integer>();
+        
+        String[] ratios = distributionRatio.split(distributionRatioDelimiter);
+        for (String ratio : ratios) {
+            distributionRatioList.add(new Integer(ratio.trim()));
+        }
+        
         if (!roundRobin) {
             weighted = new WeightedRandomLoadBalancer(distributionRatioList);
         } else {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/loadbalancer/WeightedLoadBalancerDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/loadbalancer/WeightedLoadBalancerDefinition.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/loadbalancer/WeightedLoadBalancerDefinition.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/loadbalancer/WeightedLoadBalancerDefinition.java
Mon Oct 25 19:30:43 2010
@@ -44,13 +44,26 @@ public class WeightedLoadBalancerDefinit
     private Boolean roundRobin = Boolean.FALSE;
     
     @XmlAttribute(name = "distributionRatio", required = true)
-    private List<Integer> distributionRatioList = new ArrayList<Integer>();
+    private String distributionRatio;
+    
+    @XmlAttribute(name = "distributionRatioDelimiter", required = false)
+    private String distributionRatioDelimiter;
     
     @Override
     protected LoadBalancer createLoadBalancer(RouteContext routeContext) {
         WeightedLoadBalancer loadBalancer = null;
+        List<Integer> distributionRatioList = new ArrayList<Integer>();
         
         try {
+            if (distributionRatioDelimiter == null) {
+                distributionRatioDelimiter = ":";
+            }
+            
+            String[] ratios = distributionRatio.split(distributionRatioDelimiter);
+            for (String ratio : ratios) {
+                distributionRatioList.add(new Integer(ratio.trim()));
+            }
+            
             if (!roundRobin) {
                 loadBalancer = new WeightedRandomLoadBalancer(distributionRatioList);
             } else {
@@ -70,20 +83,20 @@ public class WeightedLoadBalancerDefinit
         this.roundRobin = roundRobin;
     }
 
-    public List<Integer> getDistributionRatioList() {
-        return distributionRatioList;
+    public String getDistributionRatio() {
+        return distributionRatio;
     }
 
-    public void setDistributionRatioList(List<Integer> distributionRatioList) {
-        this.distributionRatioList = distributionRatioList;
+    public void setDistributionRatioList(String distributionRatio) {
+        this.distributionRatio = distributionRatio;
     }
 
     @Override
     public String toString() {
         if (!roundRobin) { 
-            return "WeightedRandomLoadBalancer[" + distributionRatioList + "]";
+            return "WeightedRandomLoadBalancer[" + distributionRatio + "]";
         } else {
-            return "WeightedRoundRobinLoadBalancer[" + distributionRatioList + "]";
+            return "WeightedRoundRobinLoadBalancer[" + distributionRatio + "]";
         }
     }
 }

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedLoadBalancer.java
Mon Oct 25 19:30:43 2010
@@ -19,6 +19,7 @@ package org.apache.camel.processor.loadb
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.Processor;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -39,6 +40,19 @@ public abstract class WeightedLoadBalanc
         }
     }
     
+    
+    /* (non-Javadoc)
+     * @see org.apache.camel.processor.loadbalancer.LoadBalancerSupport#doStart()
+     */
+    @Override
+    protected void doStart() throws Exception {
+        
+        super.doStart();
+        if (getProcessors().size() != getDistributionRatioList().size()) {
+            throw new CamelException("Listed Load Balance Processors do not match Distribution
Ratio.");
+        }
+    }
+
     protected void loadRuntimeRatios(List<Integer> distributionRatios) {
         int position = 0;
         
@@ -46,24 +60,6 @@ public abstract class WeightedLoadBalanc
             runtimeRatios.add(new DistributionRatio(position++, value.intValue()));
         }
     }
-
-    protected void normalizeDistributionListAgainstProcessors(List<Processor> processors)
{
-        if (processors.size() > getDistributionRatioList().size()) {
-            if (LOG.isWarnEnabled()) {
-                LOG.warn("Listed Load Balance Processors do not match distributionRatio.
Best Effort distribution will be attempted");
-                LOG.warn("Number of Processors: " + processors.size() + ". Number of DistibutionRatioList
elements: " + getDistributionRatioList().size());
-            }
-        } else if (processors.size() < getDistributionRatioList().size()) {
-            if (LOG.isWarnEnabled()) {
-                LOG.warn("Listed Load Balance Processors do not match distributionRatio.
Best Effort distribution will be attempted");
-                LOG.warn("Number of Processors: " + processors.size() + ". Number of DistibutionRatioList
elements: " + getDistributionRatioList().size());
-            }
-            for (int i = processors.size(); i < getDistributionRatioList().size(); i++)
{
-                getDistributionRatioList().set(i, 0);
-                getRuntimeRatios().remove(i);
-            }
-        }        
-    }
     
     protected boolean isRuntimeRatiosZeroed() {
         boolean cleared = true;

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRandomLoadBalancer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRandomLoadBalancer.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRandomLoadBalancer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRandomLoadBalancer.java
Mon Oct 25 19:30:43 2010
@@ -36,8 +36,6 @@ public class WeightedRandomLoadBalancer 
     protected Processor chooseProcessor(List<Processor> processors,
             Exchange exchange) {
         
-        normalizeDistributionListAgainstProcessors(processors);
-        
         boolean found = false;
         
         while (!found) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRoundRobinLoadBalancer.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRoundRobinLoadBalancer.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRoundRobinLoadBalancer.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/loadbalancer/WeightedRoundRobinLoadBalancer.java
Mon Oct 25 19:30:43 2010
@@ -34,8 +34,6 @@ public class WeightedRoundRobinLoadBalan
     @Override
     protected Processor chooseProcessor(List<Processor> processors,
             Exchange exchange) {
-        
-        normalizeDistributionListAgainstProcessors(processors);
             
         if (isRuntimeRatiosZeroed())  {
             resetRuntimeRatios();

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRandomLoadBalanceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRandomLoadBalanceTest.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRandomLoadBalanceTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRandomLoadBalanceTest.java
Mon Oct 25 19:30:43 2010
@@ -19,6 +19,7 @@ package org.apache.camel.processor;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -54,14 +55,10 @@ public class WeightedRandomLoadBalanceTe
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(4);
-                distributionRatio.add(2);
-                distributionRatio.add(1);
                 
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(false, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(false, "4:2:1").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -80,14 +77,10 @@ public class WeightedRandomLoadBalanceTe
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(2);
-                distributionRatio.add(1);
-                distributionRatio.add(3);
                 
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(false, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(false, "2, 1, 3", ",").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -106,14 +99,10 @@ public class WeightedRandomLoadBalanceTe
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(2);
-                distributionRatio.add(3);
-                distributionRatio.add(5);
                 
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(false, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(false, "2-3-5", "-").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -124,6 +113,28 @@ public class WeightedRandomLoadBalanceTe
         assertMockEndpointsSatisfied();
     }
     
+    public void testUnmatchedRatiosToProcessors() throws Exception {
+        boolean error = false;
+        
+        try {
+            context.addRoutes(new RouteBuilder() {
+                public void configure() {
+                    
+                    // START SNIPPET: example
+                    from("direct:start").loadBalance().
+                    weighted(false, "2:3").to("mock:x", "mock:y", "mock:z");
+                    // END SNIPPET: example
+                }
+            });
+            context.start();
+        } catch (CamelException e) {
+            assertEquals("Listed Load Balance Processors do not match Distribution Ratio.",
e.getMessage());
+            error = true;
+        }
+
+        assertTrue(error);
+    }
+    
     protected void sendBulkMessages(int number) {
         for (int i = 0; i < number; i++) {
             template.sendBodyAndHeader("direct:start", createTestMessage(i), "counter", i);

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRoundRobinLoadBalanceTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRoundRobinLoadBalanceTest.java?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRoundRobinLoadBalanceTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/WeightedRoundRobinLoadBalanceTest.java
Mon Oct 25 19:30:43 2010
@@ -19,6 +19,7 @@ package org.apache.camel.processor;
 import java.util.ArrayList;
 import java.util.List;
 
+import org.apache.camel.CamelException;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
@@ -55,14 +56,9 @@ public class WeightedRoundRobinLoadBalan
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(4);
-                distributionRatio.add(2);
-                distributionRatio.add(1);
-                
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(true, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(true, "4:2:1").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -84,14 +80,10 @@ public class WeightedRoundRobinLoadBalan
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(2);
-                distributionRatio.add(1);
-                distributionRatio.add(3);
                 
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(true, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(true, "2, 1, 3", ",").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -113,14 +105,9 @@ public class WeightedRoundRobinLoadBalan
 
         context.addRoutes(new RouteBuilder() {
             public void configure() {
-                ArrayList<Integer> distributionRatio = new ArrayList<Integer>();
-                distributionRatio.add(2);
-                distributionRatio.add(3);
-                distributionRatio.add(5);
-                
                 // START SNIPPET: example
                 from("direct:start").loadBalance().
-                weighted(true, distributionRatio).to("mock:x", "mock:y", "mock:z");
+                weighted(true, "2-3-5", "-").to("mock:x", "mock:y", "mock:z");
                 // END SNIPPET: example
             }
         });
@@ -131,6 +118,28 @@ public class WeightedRoundRobinLoadBalan
         assertMockEndpointsSatisfied();
     }
     
+    public void testUnmatchedRatiosToProcessors() throws Exception {
+        boolean error = false;
+        
+        try {
+            context.addRoutes(new RouteBuilder() {
+                public void configure() {
+                    
+                    // START SNIPPET: example
+                    from("direct:start").loadBalance().
+                    weighted(true, "2:3").to("mock:x", "mock:y", "mock:z");
+                    // END SNIPPET: example
+                }
+            });
+            context.start();
+        } catch (CamelException e) {
+            assertEquals("Listed Load Balance Processors do not match Distribution Ratio.",
e.getMessage());
+            error = true;
+        }
+
+        assertTrue(error);
+    }
+    
     protected void sendBulkMessages(int number) {
         for (int i = 0; i < number; i++) {
             template.sendBodyAndHeader("direct:start", createTestMessage(i), "counter", i);

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRandomLoadBalance.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRandomLoadBalance.xml?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRandomLoadBalance.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRandomLoadBalance.xml
Mon Oct 25 19:30:43 2010
@@ -29,7 +29,7 @@
     <route>
       <from uri="direct:start"/>
       <loadBalance>
-        <weighted roundRobin="false" distributionRatio="4 2 1"/>
+        <weighted roundRobin="false" distributionRatio="4, 2, 1" distributionRatioDelimiter=","
/>
           <to uri="mock:x"/>
           <to uri="mock:y"/>
           <to uri="mock:z"/>

Modified: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRoundRobinLoadBalance.xml
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRoundRobinLoadBalance.xml?rev=1027242&r1=1027241&r2=1027242&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRoundRobinLoadBalance.xml
(original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/weightedRoundRobinLoadBalance.xml
Mon Oct 25 19:30:43 2010
@@ -29,7 +29,7 @@
     <route>
       <from uri="direct:start"/>
       <loadBalance>
-        <weighted roundRobin="true" distributionRatio="4 2 1"/>
+        <weighted roundRobin="true" distributionRatio="4:2:1"/>
           <to uri="mock:x"/>
           <to uri="mock:y"/>
           <to uri="mock:z"/>



Mime
View raw message