diff --git a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java index 487b51222e8..0340260edc7 100644 --- a/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java +++ b/solr/core/src/java/org/apache/solr/servlet/ServletUtils.java @@ -209,7 +209,7 @@ static void rateLimitRequest( RateLimitManager rateLimitManager = getRateLimitManager(request); try { try { - if(rateLimitManager != null) { + if (rateLimitManager != null) { accepted = rateLimitManager.handleRequest(request); } else { accepted = true; @@ -231,7 +231,7 @@ static void rateLimitRequest( traceHttpRequestExecution2(request, response, limitedExecution, trace); } finally { if (accepted) { - if(rateLimitManager!= null) rateLimitManager.decrementActiveRequests(request); + if (rateLimitManager != null) rateLimitManager.decrementActiveRequests(request); } } } diff --git a/solr/core/src/test/org/apache/solr/servlet/TestBucketedRateLimit.java b/solr/core/src/test/org/apache/solr/servlet/TestBucketedRateLimit.java index ee93ff79f51..eccbedfe3e8 100644 --- a/solr/core/src/test/org/apache/solr/servlet/TestBucketedRateLimit.java +++ b/solr/core/src/test/org/apache/solr/servlet/TestBucketedRateLimit.java @@ -1,7 +1,6 @@ package org.apache.solr.servlet; import java.nio.charset.StandardCharsets; - import org.apache.solr.client.solrj.SolrRequest; import org.apache.solr.cloud.SolrCloudTestCase; import org.apache.solr.common.cloud.SolrZkClient; @@ -9,75 +8,76 @@ import org.junit.BeforeClass; public class TestBucketedRateLimit extends SolrCloudTestCase { - private static final String FIRST_COLLECTION = "c1"; + private static final String FIRST_COLLECTION = "c1"; - @BeforeClass - public static void setupCluster() throws Exception { - configureCluster(1).addConfig(FIRST_COLLECTION, configset("cloud-minimal")).configure(); - } + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(1).addConfig(FIRST_COLLECTION, configset("cloud-minimal")).configure(); + } - public void testConfig() throws Exception { - String config = - "{\n" - + " \"rate-limiters\": {\n" - + " \"readBuckets\": [\n" - + " {\n" - + " \"name\": \"expensive\",\n" - + " \"conditions\": [{\n" - + " \"queryParamPattern\": {\n" - + " \"q\": \".*multijoin.*\"\n" - + " }\n" - + " }],\n" - + " \"allowedRequests\": 5,\n" - + " \"slotAcquisitionTimeoutInMS\": 100\n" - + " },\n" - + " {\n" - + " \"name\": \"low\",\n" - + " \"conditions\": [{\n" - + " \"headerPattern\": {\n" - + " \"solr_req_priority\": \"20\"\n" - + " }\n" - + " }],\n" - + " \"allowedRequests\": 20,\n" - + " \"slotAcquisitionTimeoutInMS\": 100\n" - + " },\n" - + " {\n" - + " \"name\": \"global\",\n" - + " \"conditions\": [],\n" - + " \"allowedRequests\": 50,\n" - + " \"slotAcquisitionTimeoutInMS\": 100\n" - + " }\n" - + " ]\n" - + " }\n" - + "}\n" - + "\n"; - RateLimitManager mgr = - new RateLimitManager.Builder( - () -> - new SolrZkClient.NodeData(new Stat(), config.getBytes(StandardCharsets.UTF_8))) - .build(); - RequestRateLimiter rl = mgr.getRequestRateLimiter(SolrRequest.SolrRequestType.QUERY); - assertTrue(rl instanceof BucketedQueryRateLimiter); - BucketedQueryRateLimiter brl = (BucketedQueryRateLimiter) rl; - assertEquals(3, brl.buckets.size()); + public void testConfig() throws Exception { + String config = + "{\n" + + " \"rate-limiters\": {\n" + + " \"readBuckets\": [\n" + + " {\n" + + " \"name\": \"expensive\",\n" + + " \"conditions\": [{\n" + + " \"queryParamPattern\": {\n" + + " \"q\": \".*multijoin.*\"\n" + + " }\n" + + " }],\n" + + " \"allowedRequests\": 5,\n" + + " \"slotAcquisitionTimeoutInMS\": 100\n" + + " },\n" + + " {\n" + + " \"name\": \"low\",\n" + + " \"conditions\": [{\n" + + " \"headerPattern\": {\n" + + " \"solr_req_priority\": \"20\"\n" + + " }\n" + + " }],\n" + + " \"allowedRequests\": 20,\n" + + " \"slotAcquisitionTimeoutInMS\": 100\n" + + " },\n" + + " {\n" + + " \"name\": \"global\",\n" + + " \"conditions\": [],\n" + + " \"allowedRequests\": 50,\n" + + " \"slotAcquisitionTimeoutInMS\": 100\n" + + " }\n" + + " ]\n" + + " }\n" + + "}\n" + + "\n"; + RateLimitManager mgr = + new RateLimitManager.Builder( + () -> + new SolrZkClient.NodeData(new Stat(), config.getBytes(StandardCharsets.UTF_8))) + .build(); + RequestRateLimiter rl = mgr.getRequestRateLimiter(SolrRequest.SolrRequestType.QUERY); + assertTrue(rl instanceof BucketedQueryRateLimiter); + BucketedQueryRateLimiter brl = (BucketedQueryRateLimiter) rl; + assertEquals(3, brl.buckets.size()); - RequestRateLimiter.SlotMetadata smd = rl.handleRequest(new RequestRateLimiter.RequestWrapper() { - @Override - public String getParameter(String name) { + RequestRateLimiter.SlotMetadata smd = + rl.handleRequest( + new RequestRateLimiter.RequestWrapper() { + @Override + public String getParameter(String name) { return null; - } + } - @Override - public String getHeader(String name) { - if (name.equals("solr_req_priority")) - return "20"; + @Override + public String getHeader(String name) { + if (name.equals("solr_req_priority")) return "20"; else return null; - } - }); + } + }); - //star - assertEquals(19, smd.usedPool.availablePermits()); - smd.decrementRequest(); - assertEquals(20, smd.usedPool.availablePermits()); - } + // star + assertEquals(19, smd.usedPool.availablePermits()); + smd.decrementRequest(); + assertEquals(20, smd.usedPool.availablePermits()); + } }