mjsax commented on code in PR #16162:
URL: https://github.com/apache/kafka/pull/16162#discussion_r1645260947


##########
streams/src/main/java/org/apache/kafka/streams/processor/internals/PunctuateRatioSlidingWindow.java:
##########
@@ -0,0 +1,58 @@
+/*
+ * 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.streams.processor.internals;
+
+import org.apache.kafka.common.utils.Time;
+
+import java.util.LinkedList;
+import java.util.Queue;
+
+public class PunctuateRatioSlidingWindow {
+    private final Queue<RatioTimeStamp> ratioQueue;
+    private final long windowSizeMillis;
+    private final Time time;
+
+    public PunctuateRatioSlidingWindow(long windowSizeMillis, Time time) {
+        this.windowSizeMillis = windowSizeMillis;
+        this.ratioQueue = new LinkedList<>();
+        this.time=time;
+    }
+
+    public void update(double ratio){
+        long currentTimeMillis = time.milliseconds();
+        ratioQueue.offer(new RatioTimeStamp(ratio, currentTimeMillis));;
+        pruneQueue(currentTimeMillis);
+    }
+
+    private void pruneQueue(long currentTimeMillis) {
+        while(!ratioQueue.isEmpty()){
+            RatioTimeStamp oldest = ratioQueue.peek();
+            if(currentTimeMillis - oldest.getTimestamp() > windowSizeMillis) {
+                ratioQueue.poll();
+            } else {
+                break;
+            }
+        }
+    }
+
+    public double getAverageRatio() {
+        return ratioQueue.stream()
+                .mapToDouble(RatioTimeStamp::getRatio)
+                .average()

Review Comment:
   Seems we are computing an average over a ratio. Is this mathematically 
sound? I believe not.
   
   When we record punctuation ratio, the "time frame" over with the ration is 
computed is not guaranteed to be of a fixed size. Thus, the different ratios 
would need to be weighted differently for a correct computation?
   
   Seem, instead of keeping window of ratio samples, we should rather keep the 
raw latency and runtime values, ie, a window of pairs `totalPunctuateLatency` 
and `runOnceLatency `, including a pre-computed `sumTotalPunctuateLatency` and 
`sumRunOnceLatency` and compute this result as `sumTotalPunctuateLatency / 
sumRunOnceLatency` ?
   
   When updating this queue, we can also update both running sums by adding new 
and removing old values.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to