-
Notifications
You must be signed in to change notification settings - Fork 15.4k
MINOR: Fix rate metric spikes #15889
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e21bcea
Fix Rate window size
emitskevich-blp 92cd46b
Rollback import order
emitskevich-blp b0d393e
Fix SampledStat doc
emitskevich-blp 6b92808
Fix import style
emitskevich-blp 056b6cf
Merge branch 'refs/heads/trunk' into fix-rate-spikes
emitskevich-blp f95acf5
Merge branch 'apache:trunk' into fix-rate-spikes
emitskevich-blp efd670e
Merge remote-tracking branch 'origin/fix-rate-spikes' into fix-rate-s…
emitskevich-blp 19ddb93
Purge based on lastEventMs
emitskevich-blp 4a64a10
Cleanup SampledStat and test
emitskevich-blp 2c569ee
Address PR comments
emitskevich-blp 7315b75
Add SampledStat test
emitskevich-blp 0051920
Adapt SampledStat
emitskevich-blp 07ab7f9
Add copyright
emitskevich-blp d42d1cd
Add Rate test assertion
emitskevich-blp b5f4401
Clean up SampledStatTest
emitskevich-blp 9a775d2
Address PR feedback
emitskevich-blp 1b3975c
Fix style typo
emitskevich-blp 7657a38
Merge branch 'apache:trunk' into fix-rate-spikes
emitskevich-blp f9ac20f
Fix FrequenciesTest
emitskevich-blp b44f199
Fix MetricsTest
emitskevich-blp 621d0f0
Address PR feedback
emitskevich-blp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
110 changes: 110 additions & 0 deletions
110
clients/src/test/java/org/apache/kafka/common/metrics/stats/SampledStatTest.java
This file contains hidden or 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,110 @@ | ||
| /* | ||
| * 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.kafka.common.metrics.stats; | ||
|
|
||
| import org.apache.kafka.common.metrics.MetricConfig; | ||
| import org.apache.kafka.common.utils.MockTime; | ||
| import org.apache.kafka.common.utils.Time; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.SECONDS; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
|
||
| class SampledStatTest { | ||
|
|
||
| private SampledStat stat; | ||
| private Time time; | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| stat = new SampleCount(0); | ||
| time = new MockTime(); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Sample should be purged if doesn't overlap the window") | ||
| public void testSampleIsPurgedIfDoesntOverlap() { | ||
| MetricConfig config = new MetricConfig().timeWindow(1, SECONDS).samples(2); | ||
|
|
||
| // Monitored window: 2s. Complete a sample and wait 2.5s after. | ||
| completeSample(config); | ||
| time.sleep(2500); | ||
|
|
||
| double numSamples = stat.measure(config, time.milliseconds()); | ||
| assertEquals(0, numSamples); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Sample should be kept if overlaps the window") | ||
| public void testSampleIsKeptIfOverlaps() { | ||
| MetricConfig config = new MetricConfig().timeWindow(1, SECONDS).samples(2); | ||
|
|
||
| // Monitored window: 2s. Complete a sample and wait 1.5s after. | ||
| completeSample(config); | ||
| time.sleep(1500); | ||
|
|
||
| double numSamples = stat.measure(config, time.milliseconds()); | ||
| assertEquals(1, numSamples); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Sample should be kept if overlaps the window and is n+1") | ||
| public void testSampleIsKeptIfOverlapsAndExtra() { | ||
| MetricConfig config = new MetricConfig().timeWindow(1, SECONDS).samples(2); | ||
|
|
||
| // Monitored window: 2s. Create 2 samples with gaps in between and | ||
| // take a measurement at 2.2s from the start. | ||
| completeSample(config); | ||
| time.sleep(100); | ||
| completeSample(config); | ||
| time.sleep(100); | ||
| stat.record(config, 1, time.milliseconds()); | ||
|
|
||
| double numSamples = stat.measure(config, time.milliseconds()); | ||
| assertEquals(3, numSamples); | ||
| } | ||
|
|
||
| // Creates a sample with events at the start and at the end. Positions clock at the end. | ||
| private void completeSample(MetricConfig config) { | ||
| stat.record(config, 1, time.milliseconds()); | ||
| time.sleep(config.timeWindowMs() - 1); | ||
| stat.record(config, 1, time.milliseconds()); | ||
| time.sleep(1); | ||
| } | ||
|
|
||
| // measure() of this impl returns the number of samples | ||
| static class SampleCount extends SampledStat { | ||
|
|
||
| SampleCount(double initialValue) { | ||
|
chia7712 marked this conversation as resolved.
Outdated
|
||
| super(initialValue); | ||
| } | ||
|
|
||
| @Override | ||
| protected void update(Sample sample, MetricConfig config, double value, long timeMs) { | ||
| sample.value = 1; | ||
| } | ||
|
|
||
| @Override | ||
| public double combine(List<Sample> samples, MetricConfig config, long now) { | ||
| return samples.stream().mapToDouble(s -> s.value).sum(); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.