You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by "andrew4699 (via GitHub)" <gi...@apache.org> on 2024/09/10 19:08:07 UTC

[PR] Rate limiter [polaris]

andrew4699 opened a new pull request, #278:
URL: https://github.com/apache/polaris/pull/278

   # Description
   
   Adds a rate limiter.
   
   There are 3 implementations to choose from:
   - DefaultRateLimiter which uses the OpenTelemetry token bucket implementation
   - NoOpRateLimiter which does no rate limiting
   - MockRateLimiter which comes with a MockClock that can be used in tests
   Implementations are dynamically discoverable through META-INF and there are interfaces so you can plug your own.
   
   ## Type of change
   
   - [X] New feature (non-breaking change which adds functionality)
   
   # How Has This Been Tested?
   
   - [X] Existing integration tests work with a rate limiter configured
   - [X] New rate limiter integration tests
   - [X] Manual testing
   
   # Checklist:
   
   - [X] I have performed a self-review of my code
   - [X] I have commented my code, particularly in hard-to-understand areas
   - [X] My changes generate no new warnings
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769374983


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.JsonProperty;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.
+   */
+  private boolean allowRequestOnConstructionTimeout;
+
+  @JsonProperty("factory")
+  public void setRateLimiterFactory(RateLimiterFactory rateLimiterFactory) {
+    this.rateLimiterFactory = rateLimiterFactory;

Review Comment:
   Added.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1762023996


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ * The opentelemetry limiter uses a credits/balance system. We treat 1 request as 1 credit.
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  private final io.opentelemetry.sdk.internal.RateLimiter rateLimiter;

Review Comment:
   It seemed to be the only rate limiter we already had available that allowed bursts. The Google one only has per-second limits. I went ahead and added our own simple implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770865475


##########
polaris-server.yml:
##########
@@ -166,3 +166,9 @@ logging:
 
 # Limits the size of request bodies sent to Polaris. -1 means no limit.
 maxRequestBodyBytes: -1
+
+rateLimiter:

Review Comment:
   I don't actually think this should be optional. Currently, if you don't specify a `callContextResolver` for example, the config parses fine but eventually throws an NPE. Let's be consistent with existing configs here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771817948


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Clock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import org.apache.polaris.core.context.RealmContext;
+
+/**
+ * Simple rate limiter factory that just constructs a TokenBucketRateLimiter and is indiscriminate
+ * of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {
+  @JsonProperty("requestsPerSecond")
+  private long requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  private long windowSeconds;
+
+  @Override
+  public Future<RateLimiter> createRateLimiter(RealmContext realmContext) {
+    return CompletableFuture.completedFuture(
+        new TokenBucketRateLimiter(
+            requestsPerSecond, requestsPerSecond * windowSeconds, Clock.systemUTC()));

Review Comment:
   Added `Math.multiplyExact` which should throw an exception rather than wrapping around



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771874021


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.JsonProperty;
+import org.apache.hadoop.util.Preconditions;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.

Review Comment:
   Added



##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiterTest.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+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;
+import org.threeten.extra.MutableClock;
+
+/** Main unit test class for TokenBucketRateLimiter */
+public class TokenBucketRateLimiterTest {
+  @Test
+  void testBasic() {
+    MutableClock clock = MutableClock.of(Instant.now(), ZoneOffset.UTC);
+    //    MockClock clock = new MockClock();

Review Comment:
   Removed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771731459


##########
polaris-server.yml:
##########
@@ -166,3 +166,9 @@ logging:
 
 # Limits the size of request bodies sent to Polaris. -1 means no limit.
 maxRequestBodyBytes: -1
+
+rateLimiter:

Review Comment:
   I think this logic make it optional. NO?
   ```
       if (configuration.getRateLimiterConfig() != null) {
         environment
             .servlets()
             .addFilter("ratelimiter", new RateLimiterFilter(configuration.getRateLimiterConfig()))
             .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1765404876


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(realm, (key) -> config.getRateLimiterFactory().createRateLimiter(key))

Review Comment:
   I updated this to store the `Future` in my latest commit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771811983


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" costs 1 token.

Review Comment:
   Updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757690100


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();

Review Comment:
   Oops I usually write everything with var while I'm experimenting and then change it. Will update it to Clock. I'm a fan of using it ~everywhere, especially since IDEs and now even Github can tell you the type, but I've been trying to respect that the rest of the project doesn't use it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771745184


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;
+    tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli));

Review Comment:
   +1 to use existing lib if possible. Guava provides an impl. https://guava.dev/releases/23.0/api/docs/com/google/common/util/concurrent/RateLimiter.html. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770741803


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiterTest.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+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;
+import org.threeten.extra.MutableClock;
+
+/** Main unit test class for TokenBucketRateLimiter */
+public class TokenBucketRateLimiterTest {
+  @Test
+  void testBasic() {
+    MutableClock clock = MutableClock.of(Instant.now(), ZoneOffset.UTC);
+    //    MockClock clock = new MockClock();

Review Comment:
   Remove unused code?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Clock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import org.apache.polaris.core.context.RealmContext;
+
+/**
+ * Simple rate limiter factory that just constructs a TokenBucketRateLimiter and is indiscriminate
+ * of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {

Review Comment:
   Nit: Is a name like `TokenBucketRateLimiterFactory` more descriptive? Plus, we don't really have a `default` behavior here, as the config `factory` couldn't be null. 



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Clock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import org.apache.polaris.core.context.RealmContext;
+
+/**
+ * Simple rate limiter factory that just constructs a TokenBucketRateLimiter and is indiscriminate
+ * of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {
+  @JsonProperty("requestsPerSecond")
+  private long requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  private long windowSeconds;
+
+  @Override
+  public Future<RateLimiter> createRateLimiter(RealmContext realmContext) {
+    return CompletableFuture.completedFuture(
+        new TokenBucketRateLimiter(
+            requestsPerSecond, requestsPerSecond * windowSeconds, Clock.systemUTC()));

Review Comment:
   Can we check if `requestsPerSecond * windowSeconds` overflow here?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {

Review Comment:
   I guess it's safer to do this. WDYT?
   ```
     if (response instanceof HttpServletResponse) {
      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
    }
   ```



##########
polaris-server.yml:
##########
@@ -166,3 +166,9 @@ logging:
 
 # Limits the size of request bodies sent to Polaris. -1 means no limit.
 maxRequestBodyBytes: -1
+
+rateLimiter:

Review Comment:
   Nit: Can we comment that this is optional? Like this
   ```
   # Optional, missing the config means no rate limiter
   rateLimiter:
      ...
   ```
   This is an minor thing, but explicitly tells users that if they don't have to config it if they don't care about the rate limiter.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.JsonProperty;
+import org.apache.hadoop.util.Preconditions;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.

Review Comment:
   Can we add a comment to explain what is the default behavior? Something like this:
   ```
   The default value is false, which denies all requests in case of rate limiter construction timeout.
   ```



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(
+              realmContext.getRealmIdentifier(),
+              (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext))
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);

Review Comment:
   Do we need an async constructor for a rate limiter, given that there are not so many realms? Or am I missing something?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters

Review Comment:
   Are we going to introduce finer granularity limiters later? For example, rate limiters based on principal/Ip address?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   Curious if we want to apply the rate limiter filter before the `authentication` and after `TracingFilter`. Should we even authenticate the request if there are too many requests?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772124082


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(
+              realmContext.getRealmIdentifier(),
+              (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext))
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);

Review Comment:
   I'm not 100% convinced. We can prefetch and cache in case of additional API calls, but not a blocker for this PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "mrcnc (via GitHub)" <gi...@apache.org>.
mrcnc commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1767294822


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  /**
+   * 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();

Review Comment:
   So the idea is to update this interface to accept the CallContext and then update the CallContext to include the ServletRequest (via contextVariables)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771739405


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;

Review Comment:
   This is for granting tokens over time which should always happen.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772381344


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterKey.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Context about the type of entity that will be rate limited.
+ *
+ * <p>Note that this holds the realmIdentifier directly rather than the RealmContext because
+ * RealmContexts don't necessarily have to follow the guarantees of being a Map Key: hashCode,
+ * equals, immutability.
+ */
+public record RateLimiterKey(String realmIdentifier) {}

Review Comment:
   I would prefer we don't use `Context` since that term is already quite overloaded within Polaris although it was my first thought. I suggested `...Info` above, any other favorites?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752920744


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -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;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  CompletableFuture<RateLimiter> createRateLimiter(String key, Clock clock);

Review Comment:
   Instead of a `CompleteableFuture` maybe we can just return a `Future` / `Promise` -- if the caller needs to check when it's complete, it could also be a `CompletionStage`



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -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;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  CompletableFuture<RateLimiter> createRateLimiter(String key, Clock clock);

Review Comment:
   Also, missing javadoc



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752922649


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  io.opentelemetry.sdk.internal.RateLimiter rateLimiter;

Review Comment:
   can be `private final`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1765451183


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/NoOpRateLimiterFactory.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * 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;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+
+/** Rate limiter factory that constructs a no-op rate limiter */
+@JsonTypeName("no-op")
+public class NoOpRateLimiterFactory implements RateLimiterFactory {
+  @Override
+  public Future<RateLimiter> createRateLimiter(String key) {
+    return CompletableFuture.supplyAsync(NoOpRateLimiter::new);

Review Comment:
   Updated!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761719680


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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;
+import java.util.concurrent.Future;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  /**
+   * Constructs a rate limiter asynchronously. Callers may choose to set a timeout on construction.
+   *
+   * @param key The rate limiting key. Rate limiters may optionally choose to discriminate their
+   *     behavior by the key.
+   * @param clock The clock which tells you the current time
+   * @return a Future with the constructed RateLimiter
+   */
+  Future<RateLimiter> createRateLimiter(String key, Clock clock);

Review Comment:
   Updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757702686


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  io.opentelemetry.sdk.internal.RateLimiter rateLimiter;
+
+  public OpenTelemetryRateLimiter(double creditsPerSecond, double maxBalance, Clock clock) {
+    rateLimiter =
+        new io.opentelemetry.sdk.internal.RateLimiter(
+            creditsPerSecond, maxBalance, new OpenTelemetryClock(clock));
+  }
+
+  @Override
+  public boolean trySpend(double credits) {
+    return rateLimiter.trySpend(credits);

Review Comment:
   I updated the interface to just have `tryAcquire()` to decouple from this. Do you see this conflicting with other rate limiter semantics? I just picked the opentelemetry RateLimiter because it was really easy to understand the code and confirm that it was a reasonable-enough token bucket implementation.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757685774


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryClock.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 io.opentelemetry.sdk.common.Clock;
+
+/** Implementation of the opentelemetry Clock interface using the Polaris Clock interface */
+public class OpenTelemetryClock implements Clock {
+  org.apache.polaris.service.ratelimiter.Clock clock;
+
+  public OpenTelemetryClock(org.apache.polaris.service.ratelimiter.Clock clock) {
+    this.clock = clock;
+  }
+
+  @Override

Review Comment:
   It implements the opentelemetry Clock interface using the Polaris Clock interface. I updated it so when we just write `Clock` we always refer to a Polaris Clock and for the opentelemetry Clock we write out the full path, `io.opentelemetry.sdk.common.Clock`.
   
   I got myself into this situation because after doing my initial implementation using the opentelemetry limiter, I figured we shouldn't be stuck to their interface, so the idea now is that we own our own Clock/RateLimiter interface but that means we need:
   - a Polaris Clock implementation using an OpenTelemetry Clock
   - an OpenTelemetry Clock implementation using a Polaris Clock
   - an OpenTelemetry RateLimiter implementation, which normally takes an OpenTelemetry Clock, using a Polaris Clock
   
   A lot of complexity would go away if we just committed to OpenTelemetry's interfaces so I'm open to discussion on that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1767318386


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  /**
+   * 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();

Review Comment:
   The request can either be put into contextVariables or added as a separate field. And CallContext doesn't have to be passed around, you can call the static getter `CallContext.getCurrentContext()`: https://github.com/apache/polaris/blob/main/polaris-core/src/main/java/org/apache/polaris/core/context/CallContext.java#L58



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1767222064


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  /**
+   * 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();

Review Comment:
   Yes, those would be useful features that I hadn't considered.
   
   I wonder if it makes more sense long term to expose those through the [CallContext](https://github.com/apache/polaris/blob/main/polaris-core/src/main/java/org/apache/polaris/core/context/CallContext.java), which is essentially a context corresponding to the current request which is fetchable from anywhere. It doesn't have that information today, but it easily could, and then you could re-use that information everywhere rather than each component having to pass it through.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769375035


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/ClockImpl.java:
##########
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+/** An implementation of our Clock interface that just uses System.nanoTime() */
+public class ClockImpl implements Clock {

Review Comment:
   Updated the whole thing to use InstantSource/Clock.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770867206


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(
+              realmContext.getRealmIdentifier(),
+              (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext))
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);

Review Comment:
   Realm resolution may require an API call, and it's best not to block on that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771936853


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   Yea I think there's room for both types of limiters. It should be straight forward to extend this work to add a pre-auth limiter, but I'm a bit worried about the scope of this initial PR growing too big. This PR should just aim to be extensible and provide guidance on how one might extend this. I think it does that by creating generic-enough interfaces and providing a full implementation of them.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   Yea I think there's room for both types of limiters. It should be straight forward to extend this work to add a pre-auth limiter, but I'm a bit worried about the scope of this initial PR growing too big. This PR should just aim to be extensible and provide guidance on how one might extend this. I think it does that by creating generic-enough interfaces and providing one full implementation of them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757710428


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();
+    perRealmLimiters =
+        Caffeine.newBuilder()
+            .buildAsync(
+                (key, executor) -> config.getRateLimiterFactory().createRateLimiter(key, clock));
+  }
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.trySpend(1)) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .get(realm)
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
+    } catch (InterruptedException | ExecutionException | TimeoutException e) {
+      LOGGER.error("Failed to fetch rate limiter", e);

Review Comment:
   I think at the very least we should have the option to fail here.
   
   I have a hard time imagining a scenario where the construction times out or fails but we would still want the server to keep running normally. And just allow users to bypass the rate limiter for that realm?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771815627


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters

Review Comment:
   Added a layer above the key called `RateLimiterKey`. To answer the original question, it should be straight forward to rate limit on different properties and having a custom key class makes it so others don't have to refactor their code when we do so.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1765157871


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/NoOpRateLimiterFactory.java:
##########
@@ -0,0 +1,32 @@
+/*
+ * 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;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+
+/** Rate limiter factory that constructs a no-op rate limiter */
+@JsonTypeName("no-op")
+public class NoOpRateLimiterFactory implements RateLimiterFactory {
+  @Override
+  public Future<RateLimiter> createRateLimiter(String key) {
+    return CompletableFuture.supplyAsync(NoOpRateLimiter::new);

Review Comment:
   completedFuture?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(realm, (key) -> config.getRateLimiterFactory().createRateLimiter(key))

Review Comment:
   Better store the `CompletableFuture` in the map to eliminate the deadlock-probably with the current blocking behavior (CHM deadlocks are extremely hard to detect).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769373077


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;
+  private final long maxTokens;
+  private final Clock clock;
+
+  private double tokens;

Review Comment:
   Token acquisition happens during tryAcquire as opposed to happening on a timer, so you gain tokens in increments. Alternatively this could be modeled as `long milliTokens` but I think that would be less legible. I suspect precision isn't an issue because the minimum increment is `1/1000` with the latest change to milliseconds.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761719017


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/AsyncFallbackTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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 io.dropwizard.testing.ConfigOverride;
+import io.dropwizard.testing.ResourceHelpers;
+import io.dropwizard.testing.junit5.DropwizardAppExtension;
+import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
+import jakarta.ws.rs.core.Response;
+import java.util.function.Consumer;
+import org.apache.polaris.service.PolarisApplication;
+import org.apache.polaris.service.PolarisApplicationIntegrationTest;
+import org.apache.polaris.service.config.PolarisApplicationConfig;
+import org.apache.polaris.service.test.PolarisConnectionExtension;
+import org.apache.polaris.service.test.SnowmanCredentialsExtension;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration test that verifies the timeout behavior for fetching async rate limiters. This is in
+ * its own test class because the Dropwizard app is per test class and there isn't a great way to
+ * allow tests to clear the rate limiter cache.
+ */
+@ExtendWith({
+  DropwizardExtensionsSupport.class,
+  PolarisConnectionExtension.class,
+  SnowmanCredentialsExtension.class
+})
+public class AsyncFallbackTest {

Review Comment:
   This test is a failure scenario where the async construction times out.
   
   I made construction async to be able to explicitly set a timeout and hopefully _prevent_ people from accidentally making their construction blocking (i.e. if they need to fetch account information). I'm happy to change it to be synchronous and leave it for a future PR. I'm also happy to accept suggestions on how the async implementation can be improved as this is my first time touching async functionality in Java.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761731302


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/ClockImpl.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+/** An implementation of our Clock interface using opentelemetry's Clock implementation */
+public class ClockImpl implements Clock {
+  private final io.opentelemetry.sdk.common.Clock openTelemetryClock;
+
+  public ClockImpl() {
+    openTelemetryClock = io.opentelemetry.sdk.common.Clock.getDefault();
+  }
+
+  @Override
+  public long nanoTime() {
+    return openTelemetryClock.nanoTime();

Review Comment:
   Updated to not depend on the OT clock.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752925482


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();
+    perRealmLimiters =
+        Caffeine.newBuilder()
+            .buildAsync(
+                (key, executor) -> config.getRateLimiterFactory().createRateLimiter(key, clock));
+  }
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.trySpend(1)) {

Review Comment:
   If we are always hardcoding `1` here, I'm curious if `trySpend` should even take an int



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757696229


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();
+    perRealmLimiters =
+        Caffeine.newBuilder()
+            .buildAsync(
+                (key, executor) -> config.getRateLimiterFactory().createRateLimiter(key, clock));
+  }
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.trySpend(1)) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .get(realm)
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
+    } catch (InterruptedException | ExecutionException | TimeoutException e) {
+      LOGGER.error("Failed to fetch rate limiter", e);

Review Comment:
   I could see an argument for both crashing and letting it continue. I'm happy to make it a config option but at a minimum I think there should be the ability to fail open.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752915123


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/ClockImpl.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+/** An implementation of our Clock interface using opentelemetry's Clock implementation */
+public class ClockImpl implements Clock {
+  io.opentelemetry.sdk.common.Clock openTelemetryClock;

Review Comment:
   Can be `private final`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757685774


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryClock.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 io.opentelemetry.sdk.common.Clock;
+
+/** Implementation of the opentelemetry Clock interface using the Polaris Clock interface */
+public class OpenTelemetryClock implements Clock {
+  org.apache.polaris.service.ratelimiter.Clock clock;
+
+  public OpenTelemetryClock(org.apache.polaris.service.ratelimiter.Clock clock) {
+    this.clock = clock;
+  }
+
+  @Override

Review Comment:
   It implements the opentelemetry Clock interface using the Polaris Clock interface. I updated it so when we just write `Clock` we always refer to a Polaris Clock and for the opentelemetry Clock we write out the full path, `io.opentelemetry.sdk.common.Clock`.
   
   I got myself into this situation because after doing my initial implementation using the opentelemetry limiter, I figured we shouldn't be stuck to their interface, so the idea now is that we own our own Clock/RateLimiter interface but that means we need:
   - an Polaris Clock implementation using an OpenTelemetry Clock
   - an OpenTelemetry Clock implementation using a Polaris Clock
   - an OpenTelemetry RateLimiter implementation, which normally takes an OpenTelemetry Clock, using a Polaris Clock
   
   A lot of complexity would go away if we just committed to OpenTelemetry's interfaces so I'm open to discussion on that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "sfc-gh-emaynard (via GitHub)" <gi...@apache.org>.
sfc-gh-emaynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771834675


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;

Review Comment:
   I see. So this isn't `lastAcquireMillis` but something like `lastTimeTokensWereGenerated`? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771926671


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;

Review Comment:
   Yea I suppose that could be a clearer name. Updated.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772035754


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<RateLimiterKey, Future<RateLimiter>> perRealmLimiters =
+      new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    // Build the rate limiting key
+    String realmIdentifier =
+        Optional.ofNullable(CallContext.getCurrentContext())
+            .map(CallContext::getRealmContext)
+            .map(RealmContext::getRealmIdentifier)
+            .orElse("");
+    RateLimiterKey key = new RateLimiterKey(realmIdentifier);
+
+    // Maybe enforce the rate limit
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(key);
+    if (response instanceof HttpServletResponse servletResponse && !rateLimiter.tryAcquire()) {
+      servletResponse.setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RateLimiterKey key) {
+    try {
+      System.out.println("ANDREW keys = " + perRealmLimiters.keySet());

Review Comment:
   Remove this line?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on PR #278:
URL: https://github.com/apache/polaris/pull/278#issuecomment-2369802143

   @flyrain In order to make this PR as extensible as possible while still fulfilling its purpose, I did a bit of refactoring. Primarily I got rid of RateLimiterFactory and delegated the work of bucketing to the RateLimiter itself. I also removed mentions of Futures as subclasses of RateLimiter can just handle their async parts independently and/or in the future we can add some generic async extension. Since the refactor was large, I tested this end-to-end again to verify that it all still works.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772382706


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiterTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+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;
+import org.threeten.extra.MutableClock;
+
+/** Main unit test class for TokenBucketRateLimiter */
+public class TokenBucketRateLimiterTest {
+  @Test
+  void testBasic() {
+    MutableClock clock = MutableClock.of(Instant.now(), ZoneOffset.UTC);
+    clock.add(Duration.ofSeconds(5));
+
+    RateLimitResultAsserter asserter =
+        new RateLimitResultAsserter(new TokenBucketRateLimiter(10, 100, clock));
+
+    asserter.canAcquire(100);
+    asserter.cantAcquire();
+
+    clock.add(Duration.ofSeconds(1));
+    asserter.canAcquire(10);
+    asserter.cantAcquire();
+
+    clock.add(Duration.ofSeconds(10));
+    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, Clock.fixed(Instant.now(), ZoneOffset.UTC));
+    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)));

Review Comment:
   I would still really prefer if we keep `sleep` out of the tests. The idea of using a countdown latch is that you can mock the passage of time by counting down on the latch and unblocking various threads at different "times".



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769375079


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/Clock.java:
##########
@@ -0,0 +1,27 @@
+/*
+ * 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;
+
+/** An interface to specify how to retrieve the current wall clock time */
+public interface Clock {
+  /**
+   * @return the current time in nanoseconds
+   */
+  long nanoTime();

Review Comment:
   It was just because I didn't know that existed :)
   
   Updated to use InstantSource/Clock.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770861376


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,46 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Clock;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+import org.apache.polaris.core.context.RealmContext;
+
+/**
+ * Simple rate limiter factory that just constructs a TokenBucketRateLimiter and is indiscriminate
+ * of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {

Review Comment:
   This is consistent with existing classes like `DefaultContextResolver`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "mrcnc (via GitHub)" <gi...@apache.org>.
mrcnc commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1765455401


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  /**
+   * 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();

Review Comment:
   What do you think about  adding the `ServletRequest` as a parameter to add flexibility for RateLimiter implementations?  For example, if I wanted to set different limits for each endpoint, I could use the information from the ServletRequest if it were available.  Another example would be to bypass the rate limiting from certain IP addresses



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769373077


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;
+  private final long maxTokens;
+  private final Clock clock;
+
+  private double tokens;

Review Comment:
   Token acquisition happens during tryAcquire as opposed to happening on a timer, so you acquire partial tokens. Alternatively this could be modeled as `long milliTokens` but I think that would be less legible. I suspect precision isn't an issue because the minimum increment is `1/1000` with the latest change to milliseconds.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771796721


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;
+    tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli));

Review Comment:
   I think overflowing to infinity is natural/expected if the values are high enough to cause that. I do think we should check for `long` overflows as those wrap around, so I've added some checks there.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771824783


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {

Review Comment:
   Added a check



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772037649


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<RateLimiterKey, Future<RateLimiter>> perRealmLimiters =
+      new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    // Build the rate limiting key
+    String realmIdentifier =
+        Optional.ofNullable(CallContext.getCurrentContext())
+            .map(CallContext::getRealmContext)
+            .map(RealmContext::getRealmIdentifier)
+            .orElse("");
+    RateLimiterKey key = new RateLimiterKey(realmIdentifier);
+
+    // Maybe enforce the rate limit
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(key);
+    if (response instanceof HttpServletResponse servletResponse && !rateLimiter.tryAcquire()) {
+      servletResponse.setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RateLimiterKey key) {
+    try {
+      System.out.println("ANDREW keys = " + perRealmLimiters.keySet());

Review Comment:
   Oops, removed!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on PR #278:
URL: https://github.com/apache/polaris/pull/278#issuecomment-2371728877

   This is an essential feature for production deployment! Thanks @andrew4699 for adding this. Thanks @mrcnc @snazy @eric-maynard @collado-mike for the review!


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752917536


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -0,0 +1,24 @@
+/*
+ * 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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  boolean trySpend(double itemCost);

Review Comment:
   missing javadoc on this and its implementations



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752918676


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryClock.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 io.opentelemetry.sdk.common.Clock;
+
+/** Implementation of the opentelemetry Clock interface using the Polaris Clock interface */
+public class OpenTelemetryClock implements Clock {
+  org.apache.polaris.service.ratelimiter.Clock clock;
+
+  public OpenTelemetryClock(org.apache.polaris.service.ratelimiter.Clock clock) {
+    this.clock = clock;
+  }
+
+  @Override

Review Comment:
   What is this overriding?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757708493


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  io.opentelemetry.sdk.internal.RateLimiter rateLimiter;
+
+  public OpenTelemetryRateLimiter(double creditsPerSecond, double maxBalance, Clock clock) {
+    rateLimiter =
+        new io.opentelemetry.sdk.internal.RateLimiter(
+            creditsPerSecond, maxBalance, new OpenTelemetryClock(clock));
+  }
+
+  @Override
+  public boolean trySpend(double credits) {
+    return rateLimiter.trySpend(credits);

Review Comment:
   For now I think it's okay to have `tryAcquire()`. We can always add a `tryAcquire(int tokens)` or something down the road if we ever need richer semantics like those offered by the OpenTelemetry implementation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761750086


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();
+    perRealmLimiters =
+        Caffeine.newBuilder()
+            .buildAsync(
+                (key, executor) -> config.getRateLimiterFactory().createRateLimiter(key, clock));
+  }
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.trySpend(1)) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .get(realm)
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
+    } catch (InterruptedException | ExecutionException | TimeoutException e) {
+      LOGGER.error("Failed to fetch rate limiter", e);

Review Comment:
   Added a config option for this.
   
   I could see an async-constructed rate limiter that has to fetch account information to decide the limit. It times out due to a transient network issue. Then you have to pick whether to allow the request or not.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761726323


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new OpenTelemetryRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, RateLimiter> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);

Review Comment:
   I agree that the rate limiter shouldn't be an attack vector for DoS. However, apart from async nuances like context switching, the implementation is what determines how long this blocks. The filter just makes this explicit and sets a timeout.
   
   See https://github.com/apache/polaris/pull/278#discussion_r1761719017 for discussion on sync vs async.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761746784


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new OpenTelemetryRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, RateLimiter> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    return perRealmLimiters.computeIfAbsent(realm, this::createRateLimiterBlocking);

Review Comment:
   Updated to make the Map value a Future itself.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1766460259


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;

Review Comment:
   Those `double`s could all be `long`s.
   
   I wonder though whether `com.google.common.util.concurrent.RateLimiter` wouldn't satisfy the need as well. Using the Guava one could save some testing pain ;) OTOH it uses Guava's clock/watch stuff.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,89 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(realm, (key) -> config.getRateLimiterFactory().createRateLimiter(key))

Review Comment:
   Much better!
   Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1767157988


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;

Review Comment:
   Updated most of the doubles to longs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771813868


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;
+    tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli));

Review Comment:
   I was looking at the libraries we already pull in and all of the ones available (including the one you linked above) either don't allow specifying window sizes or are internal APIs that the library doesn't want others to use.
   
   Luckily the amount of code is relatively small and I added some tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770879386


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" costs 1 token.

Review Comment:
   nit: comment says `acquire` but the method is `tryAcquire`. You might also want to specify that this only affects successful acquires.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752924107


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,37 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  io.opentelemetry.sdk.internal.RateLimiter rateLimiter;
+
+  public OpenTelemetryRateLimiter(double creditsPerSecond, double maxBalance, Clock clock) {
+    rateLimiter =
+        new io.opentelemetry.sdk.internal.RateLimiter(
+            creditsPerSecond, maxBalance, new OpenTelemetryClock(clock));
+  }
+
+  @Override
+  public boolean trySpend(double credits) {
+    return rateLimiter.trySpend(credits);

Review Comment:
   The javadoc in OpenTelemetry's `trySpend` says:
   ```
     /**
      * Check to see if the provided cost can be spent within the current limits. Will deduct the cost
      * from the current balance if it can be spent.
      */
   ```
   
   It seems like we inherit those semantics, but the other RL implementations don't seem to have a concept of "credits" or "balance". In light of that the concept of "spending" is a little unclear. Should those also have a "budget" and "income" (?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772087257


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterKey.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Context about the type of entity that will be rate limited.
+ *
+ * <p>Note that this holds the realmIdentifier directly rather than the RealmContext because
+ * RealmContexts don't necessarily have to follow the guarantees of being a Map Key: hashCode,
+ * equals, immutability.
+ */
+public record RateLimiterKey(String realmIdentifier) {}

Review Comment:
   I love the idea to introduce this class, which decouples the rate limiter from realm or realm context. It's the right thing to do. With that in mind, we can clean it up and make it more generic by doing followings
   1. Avoid any name related to realm, for example, we can rename from `realmIdentifier` to `key`
   2. Make the `RateLimiterKey` more extensible
       - Rename it to something like `RateLimiterContext`
       - Introduce an interface `RateLimiterContext`, and make this record class inherit from it.
   
   Potential use cases:
   1. A user don't want to use realm as the key, he/she can switch to others like principal or Ip address
   2. A user need more information than a key string in the `RateLimiterContext`, he/she can extend the class to support pass it to Rate Limiter.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772108083


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   Sure, can we file an issue to track it?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "collado-mike (via GitHub)" <gi...@apache.org>.
collado-mike commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769323642


##########
polaris-service/src/main/java/org/apache/polaris/service/PolarisApplication.java:
##########
@@ -252,6 +253,12 @@ public void run(PolarisApplicationConfig configuration, Environment environment)
         .servlets()
         .addFilter("tracing", new TracingFilter(openTelemetry))
         .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
+
+    environment
+        .servlets()
+        .addFilter("ratelimiter", new RateLimiterFilter(configuration.getRateLimiterConfig()))
+        .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");

Review Comment:
   We should wrap this in a null check. If the user doesn't configure the `RateLimiterConfig`, we'll throw a NPE



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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;
+import java.util.concurrent.Future;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  /**
+   * Constructs a rate limiter asynchronously. Callers may choose to set a timeout on construction.
+   *
+   * @param key The rate limiting key. Rate limiters may optionally choose to discriminate their
+   *     behavior by the key.
+   * @return a Future with the constructed RateLimiter
+   */
+  Future<RateLimiter> createRateLimiter(String key);

Review Comment:
   Why is `key` as String? The javadoc is very vague about what the value is here, but it is pretty clearly a realm. I'd either make it very explicit, by passing `RealmContext` or generalize it. Maybe parameterize `RateLimiterFactory` so it accepts a type of `T` - this may be a `RealmContext` or something else supplied by the filter.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.JsonProperty;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.
+   */
+  private boolean allowRequestOnConstructionTimeout;
+
+  @JsonProperty("factory")
+  public void setRateLimiterFactory(RateLimiterFactory rateLimiterFactory) {
+    this.rateLimiterFactory = rateLimiterFactory;
+  }
+
+  @JsonProperty("factory")
+  public RateLimiterFactory getRateLimiterFactory() {
+    return rateLimiterFactory;
+  }
+
+  @JsonProperty("constructionTimeoutMillis")
+  public void setConstructionTimeoutMillis(long constructionTimeoutMillis) {

Review Comment:
   You don't need to define the property annotation for every getter/setter. It's really only needed if the getter/setter method doesn't match the property in the JSON. E.g., above your `factory` property doesn't match the setRateLimiterFactory method, so it's needed there, but not here.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/ClockImpl.java:
##########
@@ -0,0 +1,29 @@
+/*
+ * 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;
+
+/** An implementation of our Clock interface that just uses System.nanoTime() */
+public class ClockImpl implements Clock {

Review Comment:
   This could just be a method reference to `System::nanoTime`



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;
+  private final long maxTokens;
+  private final Clock clock;
+
+  private double tokens;

Review Comment:
   why is `tokens` a double?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {

Review Comment:
   This seems like it could be a `Semaphore`



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.JsonProperty;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.
+   */
+  private boolean allowRequestOnConstructionTimeout;
+
+  @JsonProperty("factory")
+  public void setRateLimiterFactory(RateLimiterFactory rateLimiterFactory) {
+    this.rateLimiterFactory = rateLimiterFactory;

Review Comment:
   We should add null checks and/or `@Nullable` annotations



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/Clock.java:
##########
@@ -0,0 +1,27 @@
+/*
+ * 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;
+
+/** An interface to specify how to retrieve the current wall clock time */
+public interface Clock {
+  /**
+   * @return the current time in nanoseconds
+   */
+  long nanoTime();

Review Comment:
   Why don't we use the `java.time.Clock` interface? Is it because the `nanoTime` method rather than `millis`? Do we really nanosecond precision? Does it make a difference if the nanosecond time is not exact? The javadocs for the SDK say it's not entirely accurate.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772447596


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiterTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * 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.Clock;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.ZoneOffset;
+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;
+import org.threeten.extra.MutableClock;
+
+/** Main unit test class for TokenBucketRateLimiter */
+public class TokenBucketRateLimiterTest {
+  @Test
+  void testBasic() {
+    MutableClock clock = MutableClock.of(Instant.now(), ZoneOffset.UTC);
+    clock.add(Duration.ofSeconds(5));
+
+    RateLimitResultAsserter asserter =
+        new RateLimitResultAsserter(new TokenBucketRateLimiter(10, 100, clock));
+
+    asserter.canAcquire(100);
+    asserter.cantAcquire();
+
+    clock.add(Duration.ofSeconds(1));
+    asserter.canAcquire(10);
+    asserter.cantAcquire();
+
+    clock.add(Duration.ofSeconds(10));
+    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, Clock.fixed(Instant.now(), ZoneOffset.UTC));
+    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)));

Review Comment:
   Removed the sleep. I don't think we even need it anymore now that it's using virtual threads + startLatch.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757693729


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;

Review Comment:
   No eviction, it's basically just a `Map<String, Future>` here. I figured I'd use it since it didn't pull in a new dependency and because it has a clean interface for specifying the construction of new entries, but I don't feel strongly either way in terms of keeping it or removing it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1761735929


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+
+/**
+ * Simple rate limiter factory that just constructs an OpenTelemetryRateLimiter and is
+ * indiscriminate of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {
+  @JsonProperty("requestsPerSecond")
+  private double requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  private double windowSeconds;
+
+  @Override
+  public Future<RateLimiter> createRateLimiter(String key, Clock clock) {
+    return CompletableFuture.supplyAsync(

Review Comment:
   This is my first time working with futures/async in Java. Is `CompletableFuture.completedFuture()` the preferred alternative that just returns an already-completed Future?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757707827


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryClock.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 io.opentelemetry.sdk.common.Clock;
+
+/** Implementation of the opentelemetry Clock interface using the Polaris Clock interface */
+public class OpenTelemetryClock implements Clock {
+  org.apache.polaris.service.ratelimiter.Clock clock;
+
+  public OpenTelemetryClock(org.apache.polaris.service.ratelimiter.Clock clock) {
+    this.clock = clock;
+  }
+
+  @Override

Review Comment:
   I see. I think what I missed is that OpenTelemetry has **two** `Clock`s -- `io.opentelemetry.sdk.common.Clock` and `org.apache.polaris.service.ratelimiter.Clock`. This file LGTM to me given that.
   
   I actually like the idea of having our own interfaces in front of the OpenTelemetry ones, if only for testing. It also fixes this odd two-Clocks situation



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1760713229


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/OpenTelemetryRateLimiter.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+/**
+ * Wrapper around the opentelemetry RateLimiter that implements the Polaris RateLimiter interface
+ * The opentelemetry limiter uses a credits/balance system. We treat 1 request as 1 credit.
+ */
+public class OpenTelemetryRateLimiter implements RateLimiter {
+  private final io.opentelemetry.sdk.internal.RateLimiter rateLimiter;

Review Comment:
   Why import an _internal_ OT type?
   I'd really like to avoid relying on any internals of any library.



##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/AsyncFallbackTest.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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 io.dropwizard.testing.ConfigOverride;
+import io.dropwizard.testing.ResourceHelpers;
+import io.dropwizard.testing.junit5.DropwizardAppExtension;
+import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
+import jakarta.ws.rs.core.Response;
+import java.util.function.Consumer;
+import org.apache.polaris.service.PolarisApplication;
+import org.apache.polaris.service.PolarisApplicationIntegrationTest;
+import org.apache.polaris.service.config.PolarisApplicationConfig;
+import org.apache.polaris.service.test.PolarisConnectionExtension;
+import org.apache.polaris.service.test.SnowmanCredentialsExtension;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Integration test that verifies the timeout behavior for fetching async rate limiters. This is in
+ * its own test class because the Dropwizard app is per test class and there isn't a great way to
+ * allow tests to clear the rate limiter cache.
+ */
+@ExtendWith({
+  DropwizardExtensionsSupport.class,
+  PolarisConnectionExtension.class,
+  SnowmanCredentialsExtension.class
+})
+public class AsyncFallbackTest {

Review Comment:
   This PR introduces async construction behaviors, which deserve a bunch of more testing, especially failure scenarios, but this test asserts only a simple "good case".
   
   I suggest to simplify the whole implementation and remove the async behavior entirely.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * 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;
+import java.util.concurrent.Future;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  /**
+   * Constructs a rate limiter asynchronously. Callers may choose to set a timeout on construction.
+   *
+   * @param key The rate limiting key. Rate limiters may optionally choose to discriminate their
+   *     behavior by the key.
+   * @param clock The clock which tells you the current time
+   * @return a Future with the constructed RateLimiter
+   */
+  Future<RateLimiter> createRateLimiter(String key, Clock clock);

Review Comment:
   The clock is rather an implementation concern and shouldn't be provided by the caller.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new OpenTelemetryRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, RateLimiter> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    return perRealmLimiters.computeIfAbsent(realm, this::createRateLimiterBlocking);

Review Comment:
   Blocking from inside a CHM is really not great, because it can (and eventually will) easily cause deadlocks.
   See Javadoc of the CHM's `computeIfAbsent` function: `... the computation should be short and simple.`



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/DefaultRateLimiterFactory.java:
##########
@@ -0,0 +1,45 @@
+/*
+ * 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.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
+
+/**
+ * Simple rate limiter factory that just constructs an OpenTelemetryRateLimiter and is
+ * indiscriminate of the key
+ */
+@JsonTypeName("default")
+public class DefaultRateLimiterFactory implements RateLimiterFactory {
+  @JsonProperty("requestsPerSecond")
+  private double requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  private double windowSeconds;
+
+  @Override
+  public Future<RateLimiter> createRateLimiter(String key, Clock clock) {
+    return CompletableFuture.supplyAsync(

Review Comment:
   This relies on the fork-join common pool for potentially long-running operations, likely causing other tasks against that pool to either stall and potentially cause deadlocks.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/ClockImpl.java:
##########
@@ -0,0 +1,33 @@
+/*
+ * 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;
+
+/** An implementation of our Clock interface using opentelemetry's Clock implementation */
+public class ClockImpl implements Clock {
+  private final io.opentelemetry.sdk.common.Clock openTelemetryClock;
+
+  public ClockImpl() {
+    openTelemetryClock = io.opentelemetry.sdk.common.Clock.getDefault();
+  }
+
+  @Override
+  public long nanoTime() {
+    return openTelemetryClock.nanoTime();

Review Comment:
   The only thing OT's clock impl does is calling `System.nanoTime()`.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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 java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new OpenTelemetryRateLimiter(0, 0, new ClockImpl());
+  private static final Clock CLOCK = new ClockImpl();
+
+  private final RateLimiterConfig config;
+  private final Map<String, RateLimiter> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);

Review Comment:
   IMHO nothing must block here at all for a rate-limiting decision. The goal is to prevent the service from being overloaded, but a blocking implementation holds system resources which can then be used to overload the system. In other words: the rate-limiter implementation itself can be used for a (D)DOS.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1762024332


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/MockRateLimiterFactory.java:
##########
@@ -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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Duration;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Factory that constructs a standard OpenTelemetryRateLimiter but with a mock Clock and an optional
+ * construction delay
+ */
+@JsonTypeName("mock")
+public class MockRateLimiterFactory implements RateLimiterFactory {
+  public static MockClock clock = new MockClock();
+
+  @JsonProperty("requestsPerSecond")
+  public double requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  public double windowSeconds;
+
+  @JsonProperty("delaySeconds")
+  public long delaySeconds;
+
+  @Override
+  public CompletableFuture<RateLimiter> createRateLimiter(String key, Clock clock) {
+    return CompletableFuture.supplyAsync(
+        () -> {
+          try {
+            Thread.sleep(Duration.ofSeconds(delaySeconds));

Review Comment:
   Changed this to a Future that never completes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1762024491


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -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;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  CompletableFuture<RateLimiter> createRateLimiter(String key, Clock clock);

Review Comment:
   Updated



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771715223


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {

Review Comment:
   Ah I see, then it would work if the intent was a concurrent request limit. I think there's room for debate on whether or not there should be a concurrent request limiter but it doesn't feel like that has to be answered in this PR.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771817363


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(
+              realmContext.getRealmIdentifier(),
+              (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext))
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);

Review Comment:
   Async construction isn't used by our implementation but making the interfaces async lets people implement either version and adds a safeguard in the form of a timeout in case they choose to implement an async one. An example of where you'd use async construction would be if you need to fetch some account information during construction.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771813868


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;
+    tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli));

Review Comment:
   I was looking at the libraries we already pull in and all of the ones available (including the one you linked above) either don't allow specifying window sizes or are internal APIs that the library doesn't want others to use.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771936853


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   Yea I think there's room for both types of limiters. It should be straight forward to extend this work to add a pre-auth limiter, but I'm a bit worried about the scope of this initial PR growing too big. This PR should just aim to be extensible and provide guidance on how one might extend this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1767126203


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {
+  private final double tokensPerNano;

Review Comment:
   I was looking into the different rate limiters that are already pulled in by our dependencies and did see that one, however it doesn't seem to support a custom window size. I had originally pulled in the OpenTelemetry one because it was basically equivalent to what I wanted, but didn't realize that it was an internal package. I figured since the implementation is small enough, it was better to just implement it than to try finding a new library for it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770863274


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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)

Review Comment:
   The authentication layer can additionally have its own rate limiting, but it seems reasonable to rate limit authenticated requests like we have here.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769373363


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {

Review Comment:
   I think a Semaphore would work as a limit on the number of concurrent requests that blocks instead of rejecting the request. This is a limit on request rate that rejects requests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752922311


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();

Review Comment:
   I'm all for `var`, but in a case like this where the type is really simple (Clock) maybe it's better to be explicit



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "mrcnc (via GitHub)" <gi...@apache.org>.
mrcnc commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1765455401


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiter.java:
##########
@@ -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;
+
+/** Interface for rate limiting requests */
+public interface RateLimiter {
+  /**
+   * 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();

Review Comment:
   What do you think about  adding the `ServletRequest` as a parameter to add flexibility for RateLimiter implementations?  For example, if I wanted to set different limits for each endpoint, I could use the information from the ServletRequest if it were available.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769375158


##########
polaris-service/src/main/java/org/apache/polaris/service/PolarisApplication.java:
##########
@@ -252,6 +253,12 @@ public void run(PolarisApplicationConfig configuration, Environment environment)
         .servlets()
         .addFilter("tracing", new TracingFilter(openTelemetry))
         .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
+
+    environment
+        .servlets()
+        .addFilter("ratelimiter", new RateLimiterFilter(configuration.getRateLimiterConfig()))
+        .addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");

Review Comment:
   Added



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterConfig.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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.JsonProperty;
+
+/** Configuration for the rate limiter */
+public class RateLimiterConfig {
+  private RateLimiterFactory rateLimiterFactory;
+
+  /**
+   * Rate limiters can be constructed asynchronously, so this config determines the construction
+   * timeout before we default to a NoOpRateLimiter.
+   */
+  private long constructionTimeoutMillis;
+
+  /**
+   * Since rate limiter construction is asynchronous and has a timeout, construction may fail. If
+   * this option is enabled, the request will still be allowed when construction fails.
+   */
+  private boolean allowRequestOnConstructionTimeout;
+
+  @JsonProperty("factory")
+  public void setRateLimiterFactory(RateLimiterFactory rateLimiterFactory) {
+    this.rateLimiterFactory = rateLimiterFactory;
+  }
+
+  @JsonProperty("factory")
+  public RateLimiterFactory getRateLimiterFactory() {
+    return rateLimiterFactory;
+  }
+
+  @JsonProperty("constructionTimeoutMillis")
+  public void setConstructionTimeoutMillis(long constructionTimeoutMillis) {

Review Comment:
   I cleaned this up in a few places.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1769374940


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFactory.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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;
+import java.util.concurrent.Future;
+
+/**
+ * Interface for constructing a rate limiter given the rate limiting key and clock. Notably, rate
+ * limiter construction may be asynchronous. This allows fetching information related to the key.
+ * For example, implementors may wish to fetch per-account rate limit values, which require lookup
+ * in an external database.
+ */
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
+public interface RateLimiterFactory extends Discoverable {
+  /**
+   * Constructs a rate limiter asynchronously. Callers may choose to set a timeout on construction.
+   *
+   * @param key The rate limiting key. Rate limiters may optionally choose to discriminate their
+   *     behavior by the key.
+   * @return a Future with the constructed RateLimiter
+   */
+  Future<RateLimiter> createRateLimiter(String key);

Review Comment:
   Updated it to explicitly be a RealmContext.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1770877189


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;

Review Comment:
   Shouldn't we only be updating this if the acquire is successful?



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -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 "acquire" 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 lastAcquireMillis;
+
+  public TokenBucketRateLimiter(long tokensPerSecond, long maxTokens, InstantSource instantSource) {
+    this.tokensPerMilli = tokensPerSecond / 1000D;
+    this.maxTokens = maxTokens;
+    this.instantSource = instantSource;
+
+    tokens = maxTokens;
+    lastAcquireMillis = 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 = t - lastAcquireMillis;
+    lastAcquireMillis = t;
+    tokens = Math.min(maxTokens, tokens + (millisPassed * tokensPerMilli));

Review Comment:
   We should check for overflow here too. All of this is kind of scary. I would rather us not write this ourselves if we can use something existing.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/TokenBucketRateLimiter.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * 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. Acquires tokens at a fixed rate and has a
+ * maximum amount of tokens. Each "acquire" costs 1 token.
+ */
+public class TokenBucketRateLimiter implements RateLimiter {

Review Comment:
   Java's Semaphore has a [tryAcquire](https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Semaphore.html#tryAcquire()), and you could reject the request if that returns `false`.



##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters

Review Comment:
   +1 to this, maybe we can introduce a thin type to cover our bases here. 
   
   So...
   ```
   public record RateLimiterInfo(RealmContext realmContext) {}
   
   . . .
   
   private RateLimiter maybeBlockToGetRateLimiter(RateLimiterInfo rateLimiterInfo) { ... }
   private final Map<RateLimiterInfo, Future<RateLimiter>> rateLimiters = new ConcurrentHashMap<>();
   // etc.
   
   ...
   ```
   
   This might let us add more fine grained rate limiters later on without having to hack up the APIs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771748722


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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 java.time.Clock;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+import org.apache.polaris.core.context.RealmContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+  private static final RateLimiter ALWAYS_REJECT_LIMITER =
+      new TokenBucketRateLimiter(0, 0, Clock.systemUTC());
+
+  private final RateLimiterConfig config;
+  private final Map<String, Future<RateLimiter>> perRealmLimiters = new ConcurrentHashMap<>();
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+  }
+
+  /** 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 {
+    RealmContext realmContext = CallContext.getCurrentContext().getRealmContext();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realmContext);
+    if (!rateLimiter.tryAcquire()) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  private RateLimiter maybeBlockToGetRateLimiter(RealmContext realmContext) {
+    try {
+      return perRealmLimiters
+          .computeIfAbsent(
+              realmContext.getRealmIdentifier(),
+              (key) -> config.getRateLimiterFactory().createRateLimiter(realmContext))
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);

Review Comment:
   `RealmContext` isn't used in TokenBucketRateLimiter's constructor, or method `createRateLimiter()`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1771869253


##########
polaris-server.yml:
##########
@@ -166,3 +166,9 @@ logging:
 
 # Limits the size of request bodies sent to Polaris. -1 means no limit.
 maxRequestBodyBytes: -1
+
+rateLimiter:

Review Comment:
   Yea currently if you don't have the config then there's no rate limiter, which might be better for people who are already using Polaris so we don't surprise them by failing all requests when they update to the latest version. I added the comment in the YAML config.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain merged PR #278:
URL: https://github.com/apache/polaris/pull/278


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "flyrain (via GitHub)" <gi...@apache.org>.
flyrain commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1772087257


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterKey.java:
##########
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/**
+ * Context about the type of entity that will be rate limited.
+ *
+ * <p>Note that this holds the realmIdentifier directly rather than the RealmContext because
+ * RealmContexts don't necessarily have to follow the guarantees of being a Map Key: hashCode,
+ * equals, immutability.
+ */
+public record RateLimiterKey(String realmIdentifier) {}

Review Comment:
   I love the idea to introduce this class, which decouples the rate limiter from realm or realm context. It's the right thing to do. With that in mind, we can clean it up and make it more generic by doing followings
   1. Avoid any name related to realm, for example, we can rename from `realmIdentifier` to `key`
   2. Make the `RateLimiterKey` more extensible
       - Rename it to something like `RateLimiterContext`
       - Introduce an interface `RateLimiterContext`, and make this record class inherit from it.
   
   Potential use cases:
   1. A user don't want to use realm as the key, he/she can switch to others like principal or Ip address
   2. A user need more information than a key string in the `RateLimiterContext`, he/she can extend the class to support pass it to Rate Limiter constructor.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757690100


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();

Review Comment:
   Oops I usually write everything with var while I'm experimenting and then change it. Will update it to Clock. I'm a fan of using it ~everywhere but I've been trying to respect that the rest of the project doesn't use it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1757712010


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;

Review Comment:
   If we don't need anything fancy, a `Map<String, Future>` might be better. For one thing, it gives us more control over the exceptions that might come from trying to retrieve a value.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752924907


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;

Review Comment:
   Does this do eviction? Or are we basically using it as a `Map<String, Future>`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752925124


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;
+
+  public RateLimiterFilter(RateLimiterConfig config) {
+    this.config = config;
+
+    var clock = new ClockImpl();
+    perRealmLimiters =
+        Caffeine.newBuilder()
+            .buildAsync(
+                (key, executor) -> config.getRateLimiterFactory().createRateLimiter(key, clock));
+  }
+
+  @Override
+  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
+      throws IOException, ServletException {
+    String realm = CallContext.getCurrentContext().getRealmContext().getRealmIdentifier();
+    RateLimiter rateLimiter = maybeBlockToGetRateLimiter(realm);
+    if (!rateLimiter.trySpend(1)) {
+      ((HttpServletResponse) response).setStatus(Response.Status.TOO_MANY_REQUESTS.getStatusCode());
+      return;
+    }
+    chain.doFilter(request, response);
+  }
+
+  RateLimiter maybeBlockToGetRateLimiter(String realm) {
+    try {
+      return perRealmLimiters
+          .get(realm)
+          .get(config.getConstructionTimeoutMillis(), TimeUnit.MILLISECONDS);
+    } catch (InterruptedException | ExecutionException | TimeoutException e) {
+      LOGGER.error("Failed to fetch rate limiter", e);

Review Comment:
   Should we crash here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "eric-maynard (via GitHub)" <gi...@apache.org>.
eric-maynard commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1752921963


##########
polaris-service/src/test/java/org/apache/polaris/service/ratelimiter/MockRateLimiterFactory.java:
##########
@@ -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 com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonTypeName;
+import java.time.Duration;
+import java.util.concurrent.CompletableFuture;
+
+/**
+ * Factory that constructs a standard OpenTelemetryRateLimiter but with a mock Clock and an optional
+ * construction delay
+ */
+@JsonTypeName("mock")
+public class MockRateLimiterFactory implements RateLimiterFactory {
+  public static MockClock clock = new MockClock();
+
+  @JsonProperty("requestsPerSecond")
+  public double requestsPerSecond;
+
+  @JsonProperty("windowSeconds")
+  public double windowSeconds;
+
+  @JsonProperty("delaySeconds")
+  public long delaySeconds;
+
+  @Override
+  public CompletableFuture<RateLimiter> createRateLimiter(String key, Clock clock) {
+    return CompletableFuture.supplyAsync(
+        () -> {
+          try {
+            Thread.sleep(Duration.ofSeconds(delaySeconds));

Review Comment:
   Let's not have an explicit sleep if we can help it. Maybe simulate this with a `CountDownLatch`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rate limiter [polaris]

Posted by "andrew4699 (via GitHub)" <gi...@apache.org>.
andrew4699 commented on code in PR #278:
URL: https://github.com/apache/polaris/pull/278#discussion_r1759425312


##########
polaris-service/src/main/java/org/apache/polaris/service/ratelimiter/RateLimiterFilter.java:
##########
@@ -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.github.benmanes.caffeine.cache.AsyncLoadingCache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+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 java.util.concurrent.ExecutionException;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+import org.apache.polaris.core.context.CallContext;
+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 static final RateLimiter NO_OP_LIMITER = new NoOpRateLimiter();
+
+  private final RateLimiterConfig config;
+  private final AsyncLoadingCache<String, RateLimiter> perRealmLimiters;

Review Comment:
   Updated!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org