-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split up rate limiter classes to simplify LogContext code and reduce …
…the size of memory allocations needed in the common case (almost no log statements use both types of rate limiting). This change should have no impact other than reducing memory footprint. RELNOTES=Refactored rate limiter classes. PiperOrigin-RevId: 511823509
- Loading branch information
Showing
9 changed files
with
302 additions
and
143 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
api/src/main/java/com/google/common/flogger/CountingRateLimiter.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,57 @@ | ||
/* | ||
* Copyright (C) 2014 The Flogger Authors. | ||
* | ||
* Licensed 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 com.google.common.flogger; | ||
|
||
import static com.google.common.flogger.LogContext.Key.LOG_EVERY_N; | ||
|
||
import com.google.common.flogger.backend.Metadata; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* Rate limiter to support {@code every(N)} functionality. This class is mutable, but thread safe. | ||
*/ | ||
final class CountingRateLimiter { | ||
private static final LogSiteMap<CountingRateLimiter> map = | ||
new LogSiteMap<CountingRateLimiter>() { | ||
@Override | ||
protected CountingRateLimiter initialValue() { | ||
return new CountingRateLimiter(); | ||
} | ||
}; | ||
|
||
static boolean shouldLog(Metadata metadata, LogSiteKey logSiteKey) { | ||
// Fast path is "there's no metadata so return true" and this must not allocate. | ||
Integer rateLimitCount = metadata.findValue(LOG_EVERY_N); | ||
if (rateLimitCount == null) { | ||
return true; | ||
} | ||
return map.get(logSiteKey, metadata).incrementAndCheckInvocationCount(rateLimitCount); | ||
} | ||
|
||
private final AtomicLong invocationCount = new AtomicLong(); | ||
|
||
/** | ||
* Increments the invocation count and returns true if it was a multiple of the specified rate | ||
* limit count; implying that the log statement should be emitted. This is invoked during | ||
* post-processing if a rate limiting count was set via {@link LoggingApi#every(int)}. | ||
*/ | ||
// Visible for testing. | ||
boolean incrementAndCheckInvocationCount(int rateLimitCount) { | ||
// Assume overflow cannot happen for a Long counter. | ||
return (invocationCount.getAndIncrement() % rateLimitCount) == 0; | ||
} | ||
} |
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
82 changes: 82 additions & 0 deletions
82
api/src/test/java/com/google/common/flogger/CountingRateLimiterTest.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,82 @@ | ||
/* | ||
* Copyright (C) 2014 The Flogger Authors. | ||
* | ||
* Licensed 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 com.google.common.flogger; | ||
|
||
import static com.google.common.flogger.LogContext.Key.LOG_EVERY_N; | ||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.flogger.testing.FakeLogSite; | ||
import com.google.common.flogger.testing.FakeMetadata; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class CountingRateLimiterTest { | ||
@Test | ||
public void testMetadataKey() { | ||
FakeMetadata metadata = new FakeMetadata().add(LOG_EVERY_N, 3); | ||
LogSite logSite = FakeLogSite.unique(); | ||
|
||
// The first log is always emitted. | ||
assertThat(CountingRateLimiter.shouldLog(metadata, logSite)).isTrue(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, logSite)).isFalse(); | ||
// Not supplying the metadata disables rate limiting. | ||
assertThat(CountingRateLimiter.shouldLog(new FakeMetadata(), logSite)).isTrue(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, logSite)).isFalse(); | ||
// The fourth log is emitted (ignoring the case when there was no metadata). | ||
assertThat(CountingRateLimiter.shouldLog(metadata, logSite)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testDistinctLogSites() { | ||
FakeMetadata metadata = new FakeMetadata().add(LOG_EVERY_N, 3); | ||
LogSite fooLog = FakeLogSite.unique(); | ||
LogSite barLog = FakeLogSite.unique(); | ||
|
||
// The first log is always emitted. | ||
assertThat(CountingRateLimiter.shouldLog(metadata, fooLog)).isTrue(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, barLog)).isTrue(); | ||
|
||
assertThat(CountingRateLimiter.shouldLog(metadata, fooLog)).isFalse(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, fooLog)).isFalse(); | ||
|
||
assertThat(CountingRateLimiter.shouldLog(metadata, barLog)).isFalse(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, barLog)).isFalse(); | ||
|
||
assertThat(CountingRateLimiter.shouldLog(metadata, fooLog)).isTrue(); | ||
assertThat(CountingRateLimiter.shouldLog(metadata, barLog)).isTrue(); | ||
} | ||
|
||
@Test | ||
public void testIncrementAndCheckInvocationCount() { | ||
CountingRateLimiter limiter = new CountingRateLimiter(); | ||
|
||
// Always log for the first call. | ||
assertThat(limiter.incrementAndCheckInvocationCount(2)).isTrue(); | ||
|
||
// Alternating for a rate limit count of 2 | ||
assertThat(limiter.incrementAndCheckInvocationCount(2)).isFalse(); | ||
assertThat(limiter.incrementAndCheckInvocationCount(2)).isTrue(); | ||
|
||
// Every third for a rate limit count of 3 (counter starts at 3, so returns true immediately). | ||
assertThat(limiter.incrementAndCheckInvocationCount(3)).isTrue(); | ||
assertThat(limiter.incrementAndCheckInvocationCount(3)).isFalse(); | ||
assertThat(limiter.incrementAndCheckInvocationCount(3)).isFalse(); | ||
assertThat(limiter.incrementAndCheckInvocationCount(3)).isTrue(); | ||
} | ||
} |
Oops, something went wrong.