You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by on...@apache.org on 2018/07/05 22:36:47 UTC

[camel] branch master updated: CAMEL-6840 - split up as-is and grouped throttling implementation to have all existing tests pass, and for grouped throttling more tests need to be added in further progress

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

onders pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new af79e3e  CAMEL-6840 - split up as-is and grouped throttling implementation to have all existing tests pass, and for grouped throttling more tests need to be added in further progress
af79e3e is described below

commit af79e3e350f472793274c33ef24f0b680494be5f
Author: onders86 <on...@gmail.com>
AuthorDate: Fri Jul 6 01:29:46 2018 +0300

    CAMEL-6840 - split up as-is and grouped throttling implementation to have all existing tests pass, and for grouped throttling more tests need to be added in further progress
---
 .../java/org/apache/camel/processor/Throttler.java | 84 ++++++++++++++++------
 1 file changed, 61 insertions(+), 23 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/processor/Throttler.java b/camel-core/src/main/java/org/apache/camel/processor/Throttler.java
index a1b10c8..963330f 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/Throttler.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/Throttler.java
@@ -130,11 +130,12 @@ public class Throttler extends DelegateAsyncProcessor implements Traceable, IdAw
             if (correlationExpression != null) {
                 key = correlationExpression.evaluate(exchange, Integer.class);
                 delayQ = locateDelayQueue(key, doneSync);
+                calculateAndSetMaxRequestsPerPeriod(delayQ, exchange, key);
             } else {
                 delayQ = delayQueue;
+                calculateAndSetMaxRequestsPerPeriod(exchange);
             }
-            
-            calculateAndSetMaxRequestsPerPeriod(delayQ, exchange, key);
+
             ThrottlePermit permit = delayQ.poll();
 
             if (permit == null) {
@@ -295,31 +296,72 @@ public class Throttler extends DelegateAsyncProcessor implements Traceable, IdAw
     /**
      * Evaluates the maxRequestsPerPeriodExpression and adjusts the throttle rate up or down.
      */
-    protected void calculateAndSetMaxRequestsPerPeriod(DelayQueue<ThrottlePermit> delayQueue, final Exchange exchange, final Integer key) throws Exception {
+    protected void calculateAndSetMaxRequestsPerPeriod(final Exchange exchange) throws Exception {
         Integer newThrottle = maxRequestsPerPeriodExpression.evaluate(exchange, Integer.class);
 
         if (newThrottle != null && newThrottle < 0) {
             throw new IllegalStateException("The maximumRequestsPerPeriod must be a positive number, was: " + newThrottle);
         }
 
-        Object lockOnSync = this;
-        Integer currentThrottleRate = throttleRate;
-        if (correlationExpression != null) {
-            currentThrottleRate = throttleRatesMap.get(key);
-            lockOnSync = key;
+        synchronized (this) {
+            if (newThrottle == null && throttleRate == 0) {
+                throw new RuntimeExchangeException("The maxRequestsPerPeriodExpression was evaluated as null: " + maxRequestsPerPeriodExpression, exchange);
+            }
+
+            if (newThrottle != null) {
+                if (newThrottle != throttleRate) {
+                    // get the queue from the cache
+                    // decrease
+                    if (throttleRate > newThrottle) {
+                        int delta = throttleRate - newThrottle;
+
+                        // discard any permits that are needed to decrease throttling
+                        while (delta > 0) {
+                            delayQueue.take();
+                            delta--;
+                            log.trace("Permit discarded due to throttling rate decrease, triggered by ExchangeId: {}", exchange.getExchangeId());
+                        }
+                        log.debug("Throttle rate decreased from {} to {}, triggered by ExchangeId: {}", throttleRate, newThrottle, exchange.getExchangeId());
+
+                    // increase
+                    } else if (newThrottle > throttleRate) {
+                        int delta = newThrottle - throttleRate;
+                        for (int i = 0; i < delta; i++) {
+                            delayQueue.put(new ThrottlePermit(-1));
+                        }
+                        if (throttleRate == 0) {
+                            log.debug("Initial throttle rate set to {}, triggered by ExchangeId: {}", newThrottle, exchange.getExchangeId());
+                        } else {
+                            log.debug("Throttle rate increase from {} to {}, triggered by ExchangeId: {}", throttleRate, newThrottle, exchange.getExchangeId());
+                        }
+                    }
+                    throttleRate = newThrottle;
+                }
+            }
         }
+    }
+    
+    /**
+     * Evaluates the maxRequestsPerPeriodExpression and adjusts the throttle rate up or down.
+     */
+    protected void calculateAndSetMaxRequestsPerPeriod(DelayQueue<ThrottlePermit> delayQueue, final Exchange exchange, final Integer key) throws Exception {
+        Integer newThrottle = maxRequestsPerPeriodExpression.evaluate(exchange, Integer.class);
 
-        synchronized (lockOnSync) {
-            if (newThrottle == null && currentThrottleRate == 0) {
+        if (newThrottle != null && newThrottle < 0) {
+            throw new IllegalStateException("The maximumRequestsPerPeriod must be a positive number, was: " + newThrottle);
+        }
+
+        synchronized (key) {
+            if (newThrottle == null && throttleRatesMap.get(key) == 0) {
                 throw new RuntimeExchangeException("The maxRequestsPerPeriodExpression was evaluated as null: " + maxRequestsPerPeriodExpression, exchange);
             }
 
             if (newThrottle != null) {
-                if (newThrottle != currentThrottleRate) {
+                if (newThrottle != throttleRatesMap.get(key)) {
                     // get the queue from the cache
                     // decrease
-                    if (currentThrottleRate > newThrottle) {
-                        int delta = currentThrottleRate - newThrottle;
+                    if (throttleRatesMap.get(key) > newThrottle) {
+                        int delta = throttleRatesMap.get(key) - newThrottle;
 
                         // discard any permits that are needed to decrease throttling
                         while (delta > 0) {
@@ -327,25 +369,21 @@ public class Throttler extends DelegateAsyncProcessor implements Traceable, IdAw
                             delta--;
                             log.trace("Permit discarded due to throttling rate decrease, triggered by ExchangeId: {}", exchange.getExchangeId());
                         }
-                        log.debug("Throttle rate decreased from {} to {}, triggered by ExchangeId: {}", currentThrottleRate, newThrottle, exchange.getExchangeId());
+                        log.debug("Throttle rate decreased from {} to {}, triggered by ExchangeId: {}", throttleRatesMap.get(key), newThrottle, exchange.getExchangeId());
 
                     // increase
-                    } else if (newThrottle > currentThrottleRate) {
-                        int delta = newThrottle - currentThrottleRate;
+                    } else if (newThrottle > throttleRatesMap.get(key)) {
+                        int delta = newThrottle - throttleRatesMap.get(key);
                         for (int i = 0; i < delta; i++) {
                             delayQueue.put(new ThrottlePermit(-1));
                         }
-                        if (currentThrottleRate == 0) {
+                        if (throttleRatesMap.get(key) == 0) {
                             log.debug("Initial throttle rate set to {}, triggered by ExchangeId: {}", newThrottle, exchange.getExchangeId());
                         } else {
-                            log.debug("Throttle rate increase from {} to {}, triggered by ExchangeId: {}", currentThrottleRate, newThrottle, exchange.getExchangeId());
+                            log.debug("Throttle rate increase from {} to {}, triggered by ExchangeId: {}", throttleRatesMap.get(key), newThrottle, exchange.getExchangeId());
                         }
                     }
-                    if (correlationExpression != null) {
-                        throttleRatesMap.put(key, newThrottle);
-                    } else {
-                        throttleRate = newThrottle;
-                    }
+                    throttleRatesMap.put(key, newThrottle);
                 }
             }
         }