davsclaus commented on code in PR #27341: URL: https://github.com/apache/camel/pull/27341#discussion_r4181088018
########## core/camel-core/src/test/java/org/apache/camel/processor/throttle/requests/TotalRequestsThrottlerCleanTest.java: ########## @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.processor.throttle.requests; + +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import org.apache.camel.CamelExecutionException; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.processor.ThrottlerRejectedExecutionException; +import org.apache.camel.processor.TotalRequestsThrottler; +import org.apache.camel.support.ExpressionAdapter; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * The throttler cleans up its state some time after a permit was last returned. An exchange that looked up the state + * before the clean must not take a permit from the removed state, as the next exchanges get the permits of the state + * that replaces it, and more exchanges than allowed would pass in the time period. + */ +public class TotalRequestsThrottlerCleanTest extends ContextTestSupport { + + private final CapturingExecutor executor = new CapturingExecutor(); + + @AfterEach + public void shutdownExecutor() { + executor.shutdownNow(); + } + + @Test + public void testCleanAfterExchangeLookedUpState() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("first", "race", "second"); + + // takes a permit, which schedules the clean + template.sendBody("direct:start", "first"); + // the clean runs (as if 10 periods had passed) after race looked up the state, but before it takes a permit + template.sendBody("direct:start", "race"); + + // race took the first permit of the state that replaced the cleaned one, so one more permit is left Review Comment: Nit: the clean runs here while `first`'s permit is still delayed (in practice it runs 10 periods later), so `first`, `race` and `second` all pass with a maximum of 2. What the test proves is that the replacement state does not hand out a second full set of permits. Could you reword this comment, and the PR text "only 2 exchanges pass in the period", so they do not suggest the per-period limit holds in this setup? ########## core/camel-core-processor/src/main/java/org/apache/camel/processor/TotalRequestsThrottler.java: ########## @@ -247,8 +262,50 @@ public ThrottlePermit take() throws InterruptedException { return delayQueue.take(); } + /** + * Removes this state if all its permits are returned (no exchange took a permit it has not returned yet). Only + * this state is removed, and not a state that has already replaced it. + */ public void clean() { - states.remove(key); + states.computeIfPresent(key, (k, s) -> s == this && markRemovedIfUnused() ? null : s); + } + + private boolean markRemovedIfUnused() { + removedLock.lock(); + try { + if (delayQueue.size() >= throttleRate) { + removed = true; + } + return removed; + } finally { + removedLock.unlock(); + } + } + + /** + * Whether this state was removed by {@link #clean()} after the exchange looked it up, in which case a permit + * taken from it does not count, and must be taken from the state that replaced it instead. + */ + private boolean isRemoved() { Review Comment: Question: `isRemoved()` now takes a second `ReentrantLock` on every exchange that gets a permit, on top of the one in `calculateAndSetMaxRequestsPerPeriod`. I agree the lock is needed for correctness. Did you measure contention with many threads on one correlation key? -- 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]
