madrob commented on a change in pull request #1686: URL: https://github.com/apache/lucene-solr/pull/1686#discussion_r467165632
########## File path: solr/core/src/java/org/apache/solr/servlet/RequestRateLimiter.java ########## @@ -80,20 +79,12 @@ public RequestRateLimiter(RateLimiterConfig rateLimiterConfig) { * * @lucene.experimental -- Can cause slots to be blocked if a request borrows a slot and is itself long lived. */ - public Pair<Boolean, AcquiredSlotMetadata> allowSlotBorrowing() throws InterruptedException { + public SlotMetadata allowSlotBorrowing() throws InterruptedException { Review comment: javadoc on this is out of date, still refers to booleans ########## File path: solr/core/src/java/org/apache/solr/servlet/RequestRateLimiter.java ########## @@ -55,21 +54,21 @@ public RequestRateLimiter(RateLimiterConfig rateLimiterConfig) { * NOTE: Always check for a null metadata object even if this method returns a true -- this will be the scenario when * rate limiters are not enabled. * */ - public Pair<Boolean, AcquiredSlotMetadata> handleRequest() throws InterruptedException { + public SlotMetadata handleRequest() throws InterruptedException { if (!rateLimiterConfig.isEnabled) { - return new Pair<Boolean, AcquiredSlotMetadata>(true, new AcquiredSlotMetadata(null, null)); + return new SlotMetadata(null); Review comment: I'd prefer to see these as pre declared members so that we're not allocating a new metadata on each request. ########## File path: solr/core/src/java/org/apache/solr/servlet/RequestRateLimiter.java ########## @@ -152,14 +143,22 @@ public RateLimiterConfig(SolrRequest.SolrRequestType requestType, boolean isEnab } } - // Represents the metadata for an acquired slot - static class AcquiredSlotMetadata { - public RequestRateLimiter requestRateLimiter; - public Semaphore usedPool; + // Represents the metadata for a slot + static class SlotMetadata { + private Semaphore usedPool; - public AcquiredSlotMetadata(RequestRateLimiter requestRateLimiter, Semaphore usedPool) { - this.requestRateLimiter = requestRateLimiter; + public SlotMetadata(Semaphore usedPool) { this.usedPool = usedPool; } + + public void decrementRequest() { + if (usedPool != null) { + usedPool.release(); + } + } + + public boolean isUsedPoolNull() { Review comment: This seems like an implementation detail that we don't need to expose? ########## File path: solr/core/src/java/org/apache/solr/servlet/RateLimitManager.java ########## @@ -132,12 +130,12 @@ public boolean handleRequest(HttpServletRequest request) throws InterruptedExcep Thread.currentThread().interrupt(); } - if (result != null && result.first()) { - if (result.second() == null) { - throw new IllegalStateException("AcquiredSlotMetadata object null even when slot is acquired and rate limiters are enabled"); - } + if (result == null) { Review comment: This should never happen, right? ########## File path: solr/core/src/java/org/apache/solr/servlet/RequestRateLimiter.java ########## @@ -152,14 +143,22 @@ public RateLimiterConfig(SolrRequest.SolrRequestType requestType, boolean isEnab } } - // Represents the metadata for an acquired slot - static class AcquiredSlotMetadata { - public RequestRateLimiter requestRateLimiter; - public Semaphore usedPool; + // Represents the metadata for a slot + static class SlotMetadata { Review comment: 👍 ########## File path: solr/core/src/java/org/apache/solr/servlet/RateLimitManager.java ########## @@ -147,19 +145,16 @@ public boolean handleRequest(HttpServletRequest request) throws InterruptedExcep // Decrement the active requests in the rate limiter for the corresponding request type. public void decrementActiveRequests(HttpServletRequest request) { - RequestRateLimiter.AcquiredSlotMetadata acquiredSlotMetadata = activeRequestsMap.get(request); + RequestRateLimiter.SlotMetadata slotMetadata = activeRequestsMap.get(request); - if (acquiredSlotMetadata == null) { + if (slotMetadata == null) { Review comment: I think I would flip this condition so that we only have one return point from the method. ########## File path: solr/core/src/java/org/apache/solr/servlet/RateLimitManager.java ########## @@ -0,0 +1,183 @@ +/* + * 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.solr.servlet; + +import javax.servlet.FilterConfig; +import javax.servlet.http.HttpServletRequest; +import java.lang.invoke.MethodHandles; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.solr.client.solrj.SolrRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.solr.common.params.CommonParams.SOLR_REQUEST_CONTEXT_PARAM; +import static org.apache.solr.common.params.CommonParams.SOLR_REQUEST_TYPE_PARAM; + +/** + * This class is responsible for managing rate limiting per request type. Rate limiters + * can be registered with this class against a corresponding type. There can be only one + * rate limiter associated with a request type. + * + * The actual rate limiting and the limits should be implemented in the corresponding RequestRateLimiter + * implementation. RateLimitManager is responsible for the orchestration but not the specifics of how the + * rate limiting is being done for a specific request type. + */ +public class RateLimitManager { + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + public final static int DEFAULT_CONCURRENT_REQUESTS= (Runtime.getRuntime().availableProcessors()) * 3; + public final static long DEFAULT_SLOT_ACQUISITION_TIMEOUT_MS = -1; + private final Map<String, RequestRateLimiter> requestRateLimiterMap; + + private final Map<HttpServletRequest, RequestRateLimiter.SlotMetadata> activeRequestsMap; + + public RateLimitManager() { + this.requestRateLimiterMap = new HashMap<>(); + this.activeRequestsMap = new ConcurrentHashMap<>(); + } + + // Handles an incoming request. The main orchestration code path, this method will + // identify which (if any) rate limiter can handle this request. Internal requests will not be + // rate limited + // Returns true if request is accepted for processing, false if it should be rejected Review comment: nit: use proper javadoc ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org