dajac commented on a change in pull request #8977: URL: https://github.com/apache/kafka/pull/8977#discussion_r452166182
########## File path: clients/src/main/java/org/apache/kafka/common/metrics/stats/TokenBucket.java ########## @@ -0,0 +1,127 @@ +/* + * 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 java.util.concurrent.TimeUnit; +import org.apache.kafka.common.metrics.MetricConfig; + +import java.util.List; + +/** + * A {@link SampledStat} that mimics the behavior of a Token Bucket and is meant to + * be used in conjunction with a {@link Rate} and a {@link org.apache.kafka.common.metrics.Quota}. + * + * The {@link TokenBucket} considers each sample as the amount of credits spent during the sample's + * window while giving back credits based on the defined quota. + * + * At time T, it computes the total O as follow: + * - O(T) = max(0, O(T-1) - Q * (W(T) - W(T-1)) + S(T) + * Where: + * - Q is the defined Quota or 0 if undefined + * - W is the time of the sample or now if undefined + * - S is the value of the sample or 0 if undefined + * + * Example with 3 samples with a Quota = 2: + * - S1 at T+0s => 4 + * - S2 at T+2s => 2 + * - S3 at T+4s => 6 + * + * The total at T+6s is computed as follow: + * - T0 => Total at T+0s => S1 = 4 + * - T1 => Total at T+2s => max(0, T0 - Q * dT) + S2 = (4 - 2 * 2) + 2 = 2 + * - T2 => Total at T+4s => max(0, T1 - Q * dT) + S3 = (2 - 2 * 2) + 6 = 6 + * - T3 => Total at T+6s => max(0, T2 - Q * dT) = (6 - 2 * 2) = 2 + */ +public class TokenBucket extends SampledStat { + + private final TimeUnit unit; + + public TokenBucket() { + this(TimeUnit.SECONDS); + } + + public TokenBucket(TimeUnit unit) { + super(0); + this.unit = unit; + } + + @Override + protected void update(Sample sample, MetricConfig config, double value, long now) { + sample.value += value; + } + + @Override + public double combine(List<Sample> samples, MetricConfig config, long now) { + if (this.samples.isEmpty()) + return 0; + + final double quota = config.quota() != null ? config.quota().bound() : 0; + final int startIndex = (this.current + 1) % this.samples.size(); + + long lastWindowMs = Long.MAX_VALUE; + double total = 0.0; + + for (int i = 0; i < this.samples.size(); i++) { Review comment: 1. It does not cause a problem here. As you said, `purgeObsoleteSamples` sets the value to zero and the lastWindowMs to now when it resets a sample. When the past `lastWindowMs` is higher that the current sample `lastWindowMs` or `now`, it has no effect: `Math.max(0, nowMs - lastWindowMs)`. 2. Right. I take current sample's lastWindowMs - previous sample's lastWindowMs to take this into account. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
