anshumg commented on a change in pull request #1686: URL: https://github.com/apache/lucene-solr/pull/1686#discussion_r464870231
########## File path: solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java ########## @@ -668,4 +694,9 @@ public void close() { public void closeOnDestroy(boolean closeOnDestroy) { this.closeOnDestroy = closeOnDestroy; } + + // Only for testing + void replaceRateLimitManager(RateLimitManager rateLimitManager) { Review comment: @VisibleForTesting ? ########## File path: solr/core/src/java/org/apache/solr/servlet/QueryRateLimiter.java ########## @@ -0,0 +1,52 @@ +/* + * 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 org.apache.solr.client.solrj.SolrRequest; + +import static org.apache.solr.servlet.RateLimitManager.DEFAULT_CONCURRENT_REQUESTS; +import static org.apache.solr.servlet.RateLimitManager.DEFAULT_SLOT_ACQUISITION_TIMEOUT_MS; + +public class QueryRateLimiter extends RequestRateLimiter { Review comment: Can we add some java doc to clarify what does this implementation do? ########## File path: solr/core/src/java/org/apache/solr/servlet/RateLimitManager.java ########## @@ -0,0 +1,148 @@ +/* + * 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.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.solr.client.solrj.SolrRequest; + +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 { + 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> 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 + public boolean handleRequest(HttpServletRequest request) throws InterruptedException { + String requestContext = request.getHeader(SOLR_REQUEST_CONTEXT_PARAM); + String typeOfRequest = request.getHeader(SOLR_REQUEST_TYPE_PARAM); + + if (typeOfRequest == null) { + // Cannot determine if this request should be throttled + return true; + } + + // Do not throttle internal requests + if (requestContext != null && requestContext.equals(SolrRequest.SolrClientContext.SERVER.toString())) { Review comment: This is really clean! ########## File path: solr/core/src/java/org/apache/solr/servlet/QueryRateLimiter.java ########## @@ -0,0 +1,52 @@ +/* + * 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 org.apache.solr.client.solrj.SolrRequest; + +import static org.apache.solr.servlet.RateLimitManager.DEFAULT_CONCURRENT_REQUESTS; +import static org.apache.solr.servlet.RateLimitManager.DEFAULT_SLOT_ACQUISITION_TIMEOUT_MS; + +public class QueryRateLimiter extends RequestRateLimiter { + final static String IS_QUERY_RATE_LIMITER_ENABLED = "isQueryRateLimiterEnabled"; + final static String MAX_QUERY_REQUESTS = "maxQueryRequests"; + final static String QUERY_WAIT_FOR_SLOT_ALLOCATION_INMS = "queryWaitForSlotAllocationInMS"; + final static String QUERY_GUARANTEED_SLOTS = "queryGuaranteedSlots"; + final static String QUERY_ALLOW_WORK_STEALING = "queryAllowWorkStealing"; Review comment: Don't want to nit-pick but 'stealing' sounds like when something is taken undesirably. Considering this is a config setting, I'd suggest using something else here. ########## File path: solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java ########## @@ -377,6 +385,20 @@ public void doFilter(ServletRequest _request, ServletResponse _response, FilterC } } + try { + accepted = rateLimitManager.handleRequest(request); + } catch (InterruptedException e) { + throw new SolrException(ErrorCode.SERVER_ERROR, e.getMessage()); + } + + if (!accepted) { + String errorMessage = "Too many requests for this request type." + + "Please try after some time or increase the quota for this request type"; + + response.sendError(429, errorMessage); + //throw new SolrException(ErrorCode.TOO_MANY_REQUESTS, "FOOFOOOFOO"); Review comment: Forgot to remove this ? ---------------------------------------------------------------- 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