apovzner commented on a change in pull request #8977: URL: https://github.com/apache/kafka/pull/8977#discussion_r451937131
########## 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: I think the following implementation of extension of a SampleStat would work close to TokenBucket. Instead of over-writing `combine()`, we could over-write `record()`. Instead of the current implementation of recording the value to a sample that corresponds to the current time, we backfill the oldest sample -- the one that still did not expire (within the whole window size) and whose value < quota * (sample length in seconds). Backfilling is equivalent to using "tokens" (unused quota for that sample) of samples that are still within the burst size (number of samples * sample length). I think this would entail: * overwriting Sample.isComplete to return true only if current condition AND value >= quota * sample length * perhaps keep track of last incomplete sample; * when backfilling last incomplete sample, only add the portion of the value until the quota for that sample is reached. Spill the remaining part of the value into the next sample. This way, expiring samples will also "expire" the values in those samples that we backfilled because they had unused quota. What do you think about this? ---------------------------------------------------------------- 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]
