-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7512182
commit d8093fc
Showing
9 changed files
with
186 additions
and
51 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
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
38 changes: 0 additions & 38 deletions
38
...ervice/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java
This file was deleted.
Oops, something went wrong.
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
54 changes: 54 additions & 0 deletions
54
...-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.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,54 @@ | ||
/* | ||
* 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.polaris.service.ratelimiter; | ||
|
||
/** Token bucket implementation of a Polaris RateLimiter. */ | ||
public class TokenBucketRateLimiter implements RateLimiter { | ||
private final double tokensPerNano; | ||
private final double maxTokens; | ||
private final Clock clock; | ||
|
||
private double tokens; | ||
private long lastAcquireNanos; | ||
|
||
public TokenBucketRateLimiter(double tokensPerSecond, double maxTokens, Clock clock) { | ||
this.tokensPerNano = tokensPerSecond / 1e9; | ||
this.maxTokens = maxTokens; | ||
this.clock = clock; | ||
|
||
tokens = maxTokens; | ||
lastAcquireNanos = clock.nanoTime(); | ||
} | ||
|
||
@Override | ||
public synchronized boolean tryAcquire() { | ||
// Grant tokens for the time that has passed since our last tryAcquire() | ||
long t = clock.nanoTime(); | ||
long nanosPassed = t - lastAcquireNanos; | ||
lastAcquireNanos = t; | ||
tokens = Math.min(maxTokens, tokens + (nanosPassed * tokensPerNano)); | ||
|
||
// Take a token if they have one available | ||
if (tokens >= 1) { | ||
tokens--; | ||
return true; | ||
} | ||
return false; | ||
} | ||
} |
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
117 changes: 117 additions & 0 deletions
117
...vice/src/test/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiterTest.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,117 @@ | ||
/* | ||
* 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.polaris.service.ratelimiter; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** Main unit test class for TokenBucketRateLimiter */ | ||
public class TokenBucketRateLimiterTest { | ||
@Test | ||
void testBasic() { | ||
MockClock clock = new MockClock(); | ||
clock.setSeconds(5); | ||
|
||
RateLimitResultAsserter asserter = | ||
new RateLimitResultAsserter(new TokenBucketRateLimiter(10, 100, clock)); | ||
|
||
asserter.canAcquire(100); | ||
asserter.cantAcquire(); | ||
|
||
clock.setSeconds(6); | ||
asserter.canAcquire(10); | ||
asserter.cantAcquire(); | ||
|
||
clock.setSeconds(16); | ||
asserter.canAcquire(100); | ||
asserter.cantAcquire(); | ||
} | ||
|
||
/** | ||
* Starts several threads that try to query the rate limiter at the same time, ensuring that we | ||
* only allow "maxTokens" requests | ||
*/ | ||
@Test | ||
void testConcurrent() throws InterruptedException { | ||
int maxTokens = 100; | ||
int numTasks = 50000; | ||
int tokensPerSecond = 10; // Can be anything above 0 | ||
int sleepPerNThreads = 100; // Making this too low will result in the test taking a long time | ||
int maxSleepMillis = 5; | ||
|
||
TokenBucketRateLimiter rl = | ||
new TokenBucketRateLimiter(tokensPerSecond, maxTokens, new MockClock()); | ||
AtomicInteger numAcquired = new AtomicInteger(); | ||
CountDownLatch startLatch = new CountDownLatch(numTasks); | ||
CountDownLatch endLatch = new CountDownLatch(numTasks); | ||
|
||
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) { | ||
for (int i = 0; i < numTasks; i++) { | ||
int i_ = i; | ||
executor.submit( | ||
() -> { | ||
try { | ||
// Enforce that tasks pause until all tasks are submitted | ||
startLatch.countDown(); | ||
startLatch.await(); | ||
|
||
// Make some threads sleep | ||
if (i_ % sleepPerNThreads == 0) { | ||
Thread.sleep((int) (Math.random() * (maxSleepMillis + 1))); | ||
} | ||
|
||
if (rl.tryAcquire()) { | ||
numAcquired.incrementAndGet(); | ||
} | ||
endLatch.countDown(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
endLatch.await(); | ||
Assertions.assertEquals(maxTokens, numAcquired.get()); | ||
} | ||
|
||
static class RateLimitResultAsserter { | ||
private final RateLimiter rateLimiter; | ||
|
||
RateLimitResultAsserter(RateLimiter rateLimiter) { | ||
this.rateLimiter = rateLimiter; | ||
} | ||
|
||
private void canAcquire(int times) { | ||
for (int i = 0; i < times; i++) { | ||
Assertions.assertTrue(rateLimiter.tryAcquire()); | ||
} | ||
} | ||
|
||
private void cantAcquire() { | ||
for (int i = 0; i < 5; i++) { | ||
Assertions.assertFalse(rateLimiter.tryAcquire()); | ||
} | ||
} | ||
} | ||
} |