dajac commented on a change in pull request #8977: URL: https://github.com/apache/kafka/pull/8977#discussion_r454366138
########## 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 Review comment: We are on the same page regarding the token bucket algorithm. My interpretation of backfilling was indeed wrong from a Token Bucket perspective. Thanks for clarifying this. I think that I understand the mechanics now but I am not sure to completely follow the last paragraph. This condition only applies if we are at the last sample already, isn't it? Also, you are basically saying that we must maintain the sum in oder to be able to take it into account, right? Instead, couldn't we carry on the amount that is above the quota of the previous sample into the new sample? I am currently trying to implement this to compare with my current implementation. Let's see how it goes. ---------------------------------------------------------------- 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]
