Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
noblepaul committed Nov 20, 2023
1 parent 89ebb08 commit 0202d69
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 67 deletions.
4 changes: 2 additions & 2 deletions solr/core/src/java/org/apache/solr/servlet/ServletUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
}
Expand Down
130 changes: 65 additions & 65 deletions solr/core/src/test/org/apache/solr/servlet/TestBucketedRateLimit.java
Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
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;
import org.apache.zookeeper.data.Stat;
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());
}
}

0 comments on commit 0202d69

Please sign in to comment.