-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Refactors rate limiter within LoggingHandler to be singleton (#6693
- Loading branch information
1 parent
14465a2
commit 72bc27e
Showing
6 changed files
with
163 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
ksqldb-rest-app/src/main/java/io/confluent/ksql/api/server/LoggingRateLimiter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2020 Confluent Inc. | ||
* | ||
* Licensed under the Confluent Community License (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.confluent.io/confluent-community-license | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package io.confluent.ksql.api.server; | ||
|
||
import static io.confluent.ksql.rest.server.KsqlRestConfig.KSQL_LOGGING_SERVER_RATE_LIMITED_REQUEST_PATHS_CONFIG; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.util.concurrent.RateLimiter; | ||
import io.confluent.ksql.rest.server.KsqlRestConfig; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.Function; | ||
|
||
class LoggingRateLimiter { | ||
|
||
private final Map<String, Double> rateLimitedPaths; | ||
private final Function<Double, RateLimiter> rateLimiterFactory; | ||
|
||
private final Map<String, RateLimiter> rateLimiters = new ConcurrentHashMap<>(); | ||
|
||
LoggingRateLimiter(final KsqlRestConfig ksqlRestConfig) { | ||
this(ksqlRestConfig, RateLimiter::create); | ||
} | ||
|
||
@VisibleForTesting | ||
LoggingRateLimiter( | ||
final KsqlRestConfig ksqlRestConfig, | ||
final Function<Double, RateLimiter> rateLimiterFactory) { | ||
requireNonNull(ksqlRestConfig); | ||
this.rateLimiterFactory = requireNonNull(rateLimiterFactory); | ||
this.rateLimitedPaths = getRateLimitedRequestPaths(ksqlRestConfig); | ||
} | ||
|
||
public boolean shouldLog(final String path) { | ||
if (rateLimitedPaths.containsKey(path)) { | ||
final double rateLimit = rateLimitedPaths.get(path); | ||
rateLimiters.computeIfAbsent(path, (k) -> rateLimiterFactory.apply(rateLimit)); | ||
return rateLimiters.get(path).tryAcquire(); | ||
} | ||
return true; | ||
} | ||
|
||
private static Map<String, Double> getRateLimitedRequestPaths(final KsqlRestConfig config) { | ||
// Already validated as having double values | ||
return config.getStringAsMap(KSQL_LOGGING_SERVER_RATE_LIMITED_REQUEST_PATHS_CONFIG) | ||
.entrySet().stream() | ||
.collect(ImmutableMap.toImmutableMap(Entry::getKey, | ||
entry -> Double.parseDouble(entry.getValue()))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
ksqldb-rest-app/src/test/java/io/confluent/ksql/api/server/LoggingRateLimiterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package io.confluent.ksql.api.server; | ||
|
||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import com.google.common.util.concurrent.RateLimiter; | ||
import io.confluent.ksql.rest.server.KsqlRestConfig; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class LoggingRateLimiterTest { | ||
|
||
private static final String PATH = "/query"; | ||
|
||
@Mock | ||
private RateLimiter rateLimiter; | ||
@Mock | ||
private KsqlRestConfig ksqlRestConfig; | ||
|
||
private LoggingRateLimiter loggingRateLimiter; | ||
|
||
|
||
@Before | ||
public void setUp() { | ||
when(ksqlRestConfig.getStringAsMap(any())).thenReturn(ImmutableMap.of(PATH, "2")); | ||
when(rateLimiter.tryAcquire()).thenReturn(true); | ||
loggingRateLimiter = new LoggingRateLimiter(ksqlRestConfig, (rateLimit) -> rateLimiter); | ||
} | ||
|
||
@Test | ||
public void shouldLog() { | ||
// When: | ||
assertThat(loggingRateLimiter.shouldLog(PATH), is(true)); | ||
|
||
// Then: | ||
verify(rateLimiter).tryAcquire(); | ||
} | ||
|
||
@Test | ||
public void shouldSkipRateLimited() { | ||
// Given: | ||
when(rateLimiter.tryAcquire()).thenReturn(true, true, false, false); | ||
|
||
// When: | ||
assertThat(loggingRateLimiter.shouldLog(PATH), is(true)); | ||
assertThat(loggingRateLimiter.shouldLog(PATH), is(true)); | ||
assertThat(loggingRateLimiter.shouldLog(PATH), is(false)); | ||
assertThat(loggingRateLimiter.shouldLog(PATH), is(false)); | ||
|
||
// Then: | ||
verify(rateLimiter, times(4)).tryAcquire(); | ||
} | ||
|
||
@Test | ||
public void shouldLog_notRateLimited() { | ||
// When: | ||
assertThat(loggingRateLimiter.shouldLog("/foo"), is(true)); | ||
|
||
// Then: | ||
verify(rateLimiter, never()).tryAcquire(); | ||
} | ||
} |