apovzner commented on a change in pull request #8977:
URL: https://github.com/apache/kafka/pull/8977#discussion_r451932855



##########
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:
       I was not able to prove to myself that this algorithm is equivalent to a 
token bucket. It does seem to smooth out a large sample, but it is hard to 
understand its behavior and to reason about it.
   
   There are couple of places where I think assumptions need to be checked:
   1) `SampledStat.purgeObsoleteSamples` implementation sets current time (as 
of the method call) to every sample it resets. Would this cause a problem here? 
It seems like this code assumes that each next sample will have a timestamp 
that gets advanced correctly to <previous sample time> + <time length of the 
sample>.
   2) Also, in situations where a gap between recorded values is longer than an 
interval of one sample, the `samples` list may have "holes" where a next sample 
in the list is two samples ahead if we consider continuous time. So because 
`samples` only contains samples with values, and skips the ones without 
recorded values, would the logic with tracking `lastWindowMs` work in all cases?




----------------------------------------------------------------
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]


Reply via email to