forked from apache/solr
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial implementation of priority based rate limiter
- Loading branch information
Showing
8 changed files
with
271 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
solr/core/src/java/org/apache/solr/servlet/PriorityBasedRateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package org.apache.solr.servlet; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.LinkedBlockingQueue; | ||
import java.util.concurrent.Semaphore; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.apache.solr.client.solrj.SolrRequest; | ||
import org.apache.solr.core.RateLimiterConfig; | ||
|
||
public class PriorityBasedRateLimiter extends RequestRateLimiter { | ||
private final AtomicInteger priorityOneRequests = new AtomicInteger(); | ||
private final String[] priorities; | ||
private final Semaphore numRequestsAllowed; | ||
|
||
private final int totalAllowedRequests; | ||
|
||
private final LinkedBlockingQueue<CountDownLatch> waitingList = new LinkedBlockingQueue<>(); | ||
|
||
public PriorityBasedRateLimiter(RateLimiterConfig rateLimiterConfig) { | ||
super(rateLimiterConfig); | ||
this.priorities = | ||
new String[] { | ||
SolrRequest.RequestPriorities.FOREGROUND.toString(), | ||
SolrRequest.RequestPriorities.BACKGROUND.toString() | ||
}; | ||
this.numRequestsAllowed = new Semaphore(rateLimiterConfig.priorityMaxRequests, true); | ||
this.totalAllowedRequests = rateLimiterConfig.priorityMaxRequests; | ||
} | ||
|
||
/* public PriorityBasedRequestLimiter(String[] priorities, int numRequestsAllowed) { | ||
this.priorities = priorities; | ||
this.numRequestsAllowed = new Semaphore(numRequestsAllowed, true); | ||
this.totalAllowedRequests = numRequestsAllowed; | ||
}*/ | ||
|
||
@Override | ||
public SlotReservation handleRequest(String requestPriority) throws InterruptedException { | ||
acquire(requestPriority); | ||
return () -> PriorityBasedRateLimiter.this.release(requestPriority); | ||
} | ||
|
||
public void acquire(String priority) throws InterruptedException { | ||
if (priority.equals(this.priorities[0])) { | ||
nextInQueue(); | ||
} else if (priority.equals(this.priorities[1])) { | ||
if (this.priorityOneRequests.get() < this.totalAllowedRequests) { | ||
nextInQueue(); | ||
} else { | ||
CountDownLatch wait = new CountDownLatch(1); | ||
this.waitingList.put(wait); | ||
wait.await(); | ||
nextInQueue(); | ||
} | ||
} | ||
} | ||
|
||
private void nextInQueue() throws InterruptedException { | ||
this.priorityOneRequests.addAndGet(1); | ||
this.numRequestsAllowed.acquire(1); | ||
} | ||
|
||
private void exitFromQueue() { | ||
this.priorityOneRequests.addAndGet(-1); | ||
this.numRequestsAllowed.release(1); | ||
} | ||
|
||
public void release(String priority) { | ||
if (this.priorities[0].equals(priority) || this.priorities[1].equals(priority)) { | ||
if (this.priorityOneRequests.get() > this.totalAllowedRequests) { | ||
// priority one request is waiting, let's inform it | ||
this.exitFromQueue(); | ||
} else { | ||
// next priority | ||
CountDownLatch waiter = this.waitingList.poll(); | ||
if (waiter != null) { | ||
waiter.countDown(); | ||
} | ||
this.exitFromQueue(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public SlotReservation allowSlotBorrowing() throws InterruptedException { | ||
throw new RuntimeException( | ||
"PriorityBasedRateLimiter.allowSlotBorrowing method is not implemented"); | ||
} | ||
|
||
public int getRequestsAllowed() { | ||
return this.priorityOneRequests.get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.