ohnoitsyou commented on code in PR #11903:
URL: https://github.com/apache/nifi/pull/11903#discussion_r4605124921


##########
nifi-extension-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/ControlRate.java:
##########
@@ -416,28 +421,37 @@ private void clearExpiredThrottles(final ProcessContext 
context) {
         // Periodically clear any Throttle that has not been used in more than 
2 throttling periods
         final long lastClearTime = lastThrottleClearTime.get();
         final long throttleExpirationMillis = getCurrentTimeMillis() - 2 * 
context.getProperty(TIME_PERIOD).asTimePeriod(TimeUnit.MILLISECONDS);
+        // Throttle ourselves to see if we should actually clear old throttles
         if (lastClearTime < throttleExpirationMillis) {
             if (lastThrottleClearTime.compareAndSet(lastClearTime, 
getCurrentTimeMillis())) {
-                final Set<Map.Entry<String, Throttle>> throttleSet = new 
HashSet<>();
                 if (dataThrottleRequired()) {
-                    throttleSet.addAll(dataThrottleMap.entrySet());
+                    removeExpiredThrottles(dataThrottleMap, 
throttleExpirationMillis);
                 }
                 if (countThrottleRequired()) {
-                    throttleSet.addAll(countThrottleMap.entrySet());
+                    removeExpiredThrottles(countThrottleMap, 
throttleExpirationMillis);
                 }
-                final Iterator<Map.Entry<String, Throttle>> itr = 
throttleSet.iterator();
-                while (itr.hasNext()) {
-                    final Map.Entry<String, Throttle> entry = itr.next();
-                    final Throttle throttle = entry.getValue();
-                    if (throttle.tryLock()) {
-                        try {
-                            if (throttle.lastUpdateTime() < lastClearTime) {
-                                itr.remove();
-                            }
-                        } finally {
-                            throttle.unlock();
-                        }
+            }
+        }
+    }
+
+    /**
+     * Loop over the entries in a throttleMap and clear out any that have a 
lastUpdateTime less than expirationCutoffTime
+     * @param throttleMap the throttleMap to be cleaned
+     * @param expirationCutoffTime timestamp after which a throttle is 
considered expired
+     */
+    private void removeExpiredThrottles(final ConcurrentMap<String, Throttle> 
throttleMap, final long expirationCutoffTime) {
+        final Iterator<Map.Entry<String, Throttle>> itr = 
throttleMap.entrySet().iterator();
+        while (itr.hasNext()) {
+            final Map.Entry<String, Throttle> entry = itr.next();
+            final Throttle throttle = entry.getValue();
+            if (throttle.tryLock()) {
+                try {
+                    // if a throttle was last updated after the cutoff, remove 
it

Review Comment:
   You've uncovered the thing that tripped me up for way too long while 
addressing this.
   Depends on how you think about time and things like thresholds. I'll either 
come up with some wording that's less ambiguous or remove the comment 
altogether.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to