-
Notifications
You must be signed in to change notification settings - Fork 142
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
711060d
commit 81ec424
Showing
18 changed files
with
723 additions
and
0 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
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
30 changes: 30 additions & 0 deletions
30
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/NoOpRateLimiter.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,30 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.annotation.JsonTypeName; | ||
|
||
/** Rate limiter that always allows the request */ | ||
@JsonTypeName("no-op") | ||
public class NoOpRateLimiter implements RateLimiter { | ||
@Override | ||
public boolean tryAcquire() { | ||
return true; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.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,34 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import io.dropwizard.jackson.Discoverable; | ||
|
||
/** Interface for rate limiting requests */ | ||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") | ||
public interface RateLimiter extends Discoverable { | ||
/** | ||
* This signifies that a request is being made. That is, the rate limiter should count the request | ||
* at this point. | ||
* | ||
* @return Whether the request is allowed to proceed by the rate limiter | ||
*/ | ||
boolean tryAcquire(); | ||
} |
56 changes: 56 additions & 0 deletions
56
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.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,56 @@ | ||
/* | ||
* 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 jakarta.annotation.Priority; | ||
import jakarta.servlet.Filter; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.ServletRequest; | ||
import jakarta.servlet.ServletResponse; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.ws.rs.Priorities; | ||
import jakarta.ws.rs.core.Response; | ||
import java.io.IOException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** Request filter that returns a 429 Too Many Requests if the rate limiter says so */ | ||
@Priority(Priorities.AUTHORIZATION + 1) | ||
public class RateLimiterFilter implements Filter { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(RateLimiterFilter.class); | ||
|
||
private final RateLimiter rateLimiter; | ||
|
||
public RateLimiterFilter(RateLimiter rateLimiter) { | ||
this.rateLimiter = rateLimiter; | ||
} | ||
|
||
/** Returns a 429 if the rate limiter says so. Otherwise, forwards the request along. */ | ||
@Override | ||
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) | ||
throws IOException, ServletException { | ||
if (response instanceof HttpServletResponse servletResponse && !rateLimiter.tryAcquire()) { | ||
servletResponse.setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode()); | ||
LOGGER.atDebug().log("Rate limiting request"); | ||
return; | ||
} | ||
chain.doFilter(request, response); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...ice/src/main/java/org/apache/polaris/service/ratelimiter/RealmTokenBucketRateLimiter.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,81 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.annotation.JsonTypeName; | ||
import java.time.Clock; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import org.apache.polaris.core.context.CallContext; | ||
import org.apache.polaris.core.context.RealmContext; | ||
import org.jetbrains.annotations.VisibleForTesting; | ||
|
||
/** | ||
* Rate limiter that maps the request's realm identifier to its own TokenBucketRateLimiter, with its | ||
* own capacity. | ||
*/ | ||
@JsonTypeName("realm-token-bucket") | ||
public class RealmTokenBucketRateLimiter implements RateLimiter { | ||
private final long requestsPerSecond; | ||
private final long windowSeconds; | ||
private final Map<String, RateLimiter> perRealmLimiters; | ||
|
||
@VisibleForTesting | ||
@JsonCreator | ||
public RealmTokenBucketRateLimiter( | ||
@JsonProperty("requestsPerSecond") final long requestsPerSecond, | ||
@JsonProperty("windowSeconds") final long windowSeconds) { | ||
this.requestsPerSecond = requestsPerSecond; | ||
this.windowSeconds = windowSeconds; | ||
this.perRealmLimiters = new ConcurrentHashMap<>(); | ||
} | ||
|
||
/** | ||
* This signifies that a request is being made. That is, the rate limiter should count the request | ||
* at this point. | ||
* | ||
* @return Whether the request is allowed to proceed by the rate limiter | ||
*/ | ||
@Override | ||
public boolean tryAcquire() { | ||
String key = | ||
Optional.ofNullable(CallContext.getCurrentContext()) | ||
.map(CallContext::getRealmContext) | ||
.map(RealmContext::getRealmIdentifier) | ||
.orElse(""); | ||
|
||
return perRealmLimiters | ||
.computeIfAbsent( | ||
key, | ||
(k) -> | ||
new TokenBucketRateLimiter( | ||
requestsPerSecond, | ||
Math.multiplyExact(requestsPerSecond, windowSeconds), | ||
getClock())) | ||
.tryAcquire(); | ||
} | ||
|
||
@VisibleForTesting | ||
protected Clock getClock() { | ||
return Clock.systemUTC(); | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...-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,64 @@ | ||
/* | ||
* 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.time.InstantSource; | ||
|
||
/** | ||
* Token bucket implementation of a Polaris RateLimiter. Acquires tokens at a fixed rate and has a | ||
* maximum amount of tokens. Each successful "tryAcquire" costs 1 token. | ||
*/ | ||
public class TokenBucketRateLimiter implements RateLimiter { | ||
private final double tokensPerMilli; | ||
private final long maxTokens; | ||
private final InstantSource instantSource; | ||
|
||
private double tokens; | ||
private long lastTokenGenerationMillis; | ||
|
||
public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) { | ||
this.tokensPerMilli = tokensPerSecond / 1000D; | ||
this.maxTokens = maxTokens; | ||
this.instantSource = instantSource; | ||
|
||
tokens = maxTokens; | ||
lastTokenGenerationMillis = instantSource.millis(); | ||
} | ||
|
||
/** | ||
* Tries to acquire and spend 1 token. Doesn't block if a token isn't available. | ||
* | ||
* @return whether a token was successfully acquired & spent | ||
*/ | ||
@Override | ||
public synchronized boolean tryAcquire() { | ||
// Grant tokens for the time that has passed since our last tryAcquire() | ||
long t = instantSource.millis(); | ||
long millisPassed = Math.subtractExact(t, lastTokenGenerationMillis); | ||
lastTokenGenerationMillis = t; | ||
tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli)); | ||
|
||
// 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
21 changes: 21 additions & 0 deletions
21
...e/src/main/resources/META-INF/services/org.apache.polaris.service.ratelimiter.RateLimiter
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,21 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
org.apache.polaris.service.ratelimiter.RealmTokenBucketRateLimiter | ||
org.apache.polaris.service.ratelimiter.NoOpRateLimiter |
Oops, something went wrong.