-
Notifications
You must be signed in to change notification settings - Fork 15.1k
KAFKA-2443 Expose windowSize on Rate; KAFKA-2567 - Throttle time should not return NaN #213
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9aa992c
Fixing K-2443
auradkar 7412734
Addressing Ismael and Edwards comments.
auradkar d39f561
Fixing KAFKA-2567
auradkar 2f27026
Fixing 2567
auradkar 4345f0d
addressing ismaels comments
auradkar 458ab8f
addressing comments
auradkar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -322,6 +322,34 @@ public void testPercentiles() { | |
| assertEquals(0.0, p75.value(), 1.0); | ||
| } | ||
|
|
||
| @Test | ||
| public void testRateWindowing() throws Exception { | ||
| // Use the default time window. Set 3 samples | ||
| MetricConfig cfg = new MetricConfig().samples(3); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also minor comment - third sample will be unused. |
||
| Sensor s = metrics.sensor("test.sensor", cfg); | ||
| s.add(new MetricName("test.rate", "grp1"), new Rate(TimeUnit.SECONDS)); | ||
|
|
||
| int sum = 0; | ||
| int count = cfg.samples() - 1; | ||
| // Advance 1 window after every record | ||
| for (int i = 0; i < count; i++) { | ||
| s.record(100); | ||
| sum += 100; | ||
| time.sleep(cfg.timeWindowMs()); | ||
| } | ||
|
|
||
| // Sleep for half the window. | ||
| time.sleep(cfg.timeWindowMs() / 2); | ||
|
|
||
| // prior to any time passing | ||
| double elapsedSecs = (cfg.timeWindowMs() * (cfg.samples() - 1) + cfg.timeWindowMs() / 2) / 1000.0; | ||
|
|
||
| KafkaMetric km = metrics.metrics().get(new MetricName("test.rate", "grp1")); | ||
| assertEquals("Rate(0...2) = 2.666", sum / elapsedSecs, km.value(), EPS); | ||
| assertEquals("Elapsed Time = 75 seconds", elapsedSecs, | ||
| ((Rate) km.measurable()).windowSize(cfg, time.milliseconds()) / 1000, EPS); | ||
| } | ||
|
|
||
| public static class ConstantMeasurable implements Measurable { | ||
| public double value = 0.0; | ||
|
|
||
|
|
||
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 |
|---|---|---|
|
|
@@ -121,13 +121,14 @@ class ClientQuotaManager(private val config: ClientQuotaManagerConfig, | |
| case qve: QuotaViolationException => | ||
| // Compute the delay | ||
| val clientMetric = metrics.metrics().get(clientRateMetricName(clientId)) | ||
| throttleTimeMs = throttleTime(clientMetric.value(), getQuotaMetricConfig(quota(clientId))) | ||
| throttleTimeMs = throttleTime(clientMetric, getQuotaMetricConfig(quota(clientId))) | ||
| delayQueue.add(new ThrottledResponse(time, throttleTimeMs, callback)) | ||
| delayQueueSensor.record() | ||
| clientSensors.throttleTimeSensor.record(throttleTimeMs) | ||
| // If delayed, add the element to the delayQueue | ||
| logger.debug("Quota violated for sensor (%s). Delay time: (%d)".format(clientSensors.quotaSensor.name(), throttleTimeMs)) | ||
| } | ||
| // If the request is not throttled, a throttleTime of 0 ms is recorded | ||
| clientSensors.throttleTimeSensor.record(throttleTimeMs) | ||
| throttleTimeMs | ||
| } | ||
|
|
||
|
|
@@ -139,11 +140,21 @@ class ClientQuotaManager(private val config: ClientQuotaManagerConfig, | |
| * we need to add a delay of X to W such that O * W / (W + X) = T. | ||
| * Solving for X, we get X = (O - T)/T * W. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the comment in the RB - can we document the discussion on the right vs conservative formula here? |
||
| */ | ||
| private def throttleTime(metricValue: Double, config: MetricConfig): Int = { | ||
| private def throttleTime(clientMetric: KafkaMetric, config: MetricConfig): Int = { | ||
| val rateMetric: Rate = measurableAsRate(clientMetric.metricName(), clientMetric.measurable()) | ||
| val quota = config.quota() | ||
| val difference = metricValue - quota.bound | ||
| val time = difference / quota.bound * config.timeWindowMs() * config.samples() | ||
| time.round.toInt | ||
| val difference = clientMetric.value() - quota.bound | ||
| // Use the precise window used by the rate calculation | ||
| val throttleTimeMs = difference / quota.bound * rateMetric.windowSize(config, time.milliseconds()) | ||
| throttleTimeMs.round.toInt | ||
| } | ||
|
|
||
| // Casting to Rate because we only use Rate in Quota computation | ||
| private def measurableAsRate(name: MetricName, measurable: Measurable): Rate = { | ||
| measurable match { | ||
| case r: Rate => r | ||
| case _ => throw new IllegalArgumentException(s"Metric $name is not a Rate metric, value $measurable") | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the comment and the fix. Minor typo: "gives". I'm going to check-in with this though.