Pengzna commented on code in PR #16886: URL: https://github.com/apache/iotdb/pull/16886#discussion_r2744308565
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/tools/DelayAnalyzer.java: ########## @@ -0,0 +1,346 @@ +/* + * 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.iotdb.db.tools; + +import org.apache.iotdb.commons.utils.TestOnly; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Arrays; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * DelayAnalyzer: Calculate watermarks for in-order and out-of-order data based on statistics and + * models + * + * <p>This implementation is based on the paper "Separation or Not: On Handling Out-of-Order + * Time-Series Data" (ICDE 2022) proposed method, which dynamically calculates safe watermarks by + * analyzing the Cumulative Distribution Function (CDF) of data arrival delays. + * + * <p>Core concepts: + * + * <ul> + * <li>Record the generation time (Event Time) and arrival time (System Time) of each data point + * <li>Calculate delay: delay = arrivalTime - generationTime + * <li>Maintain a sliding window to store recent delay samples + * <li>Use CDF to calculate delay quantiles (e.g., P99) at specific confidence levels + * <li>Safe watermark = Current system time - P99 delay + * </ul> + * + * <p>Key formulas from the paper: + * + * <ul> + * <li>Delay calculation: p.t_d = p.t_a - p.t_g (Formula 160) + * <li>Use F(x) (CDF) to estimate the probability of out-of-order data (Reference 110) + * <li>Dynamic delay distribution: Use sliding window to maintain recent delay samples + * </ul> + * + * <p>Features: + * + * <ul> + * <li>Thread-safe: Uses ReadWriteLock to support high-concurrency read/write operations + * <li>Memory efficient: Uses circular buffer with fixed memory footprint + * <li>Clock skew handling: Automatically handles negative delays (clock rollback) + * <li>Dynamic adaptation: Sliding window automatically adapts to changes in delay distribution + * </ul> + * + * @see <a href="https://ieeexplore.ieee.org/document/9835234">ICDE 2022 Paper</a> + */ +public class DelayAnalyzer { + + private static final Logger LOGGER = LoggerFactory.getLogger(DelayAnalyzer.class); + + /** Default window size, empirical value: 10000 sample points */ + public static final int DEFAULT_WINDOW_SIZE = 10000; + + /** Recommended window size range */ + public static final int MIN_WINDOW_SIZE = 1000; + + public static final int MAX_WINDOW_SIZE = 100000; + + /** Default confidence level: 99% */ + public static final double DEFAULT_CONFIDENCE_LEVEL = 0.99; + + /** + * Sliding window size for storing recent delay sample points. A larger window provides more + * accurate P99 calculations but increases memory and sorting overhead. The paper mentions using + * dynamic delay distribution to adapt to changes in delay patterns. + */ + private final int windowSize; + + /** Circular buffer storing delay samples (unit: milliseconds) */ + private final long[] delaySamples; + + /** Current write position (cursor of the circular buffer) */ + private int cursor = 0; + + /** Flag indicating whether the buffer is full */ + private boolean isFull = false; + + /** Total number of samples (for statistics) */ + private long totalSamples = 0; + + /** + * ReadWriteLock ensures thread safety in high-concurrency scenarios. Read operations (calculating + * quantiles) use read lock, write operations (updating delays) use write lock. + */ + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + + /** Constructor using default window size */ + public DelayAnalyzer() { + this(DEFAULT_WINDOW_SIZE); + } + + /** + * Constructor + * + * @param windowSize Sliding window size, recommended range: 5000 - 10000. Larger windows provide + * more accurate statistics but increase memory and computational overhead. + * @throws IllegalArgumentException if window size is not within valid range + */ + public DelayAnalyzer(int windowSize) { + if (windowSize < MIN_WINDOW_SIZE || windowSize > MAX_WINDOW_SIZE) { + throw new IllegalArgumentException( + String.format( + "Window size must be between %d and %d, got %d", + MIN_WINDOW_SIZE, MAX_WINDOW_SIZE, windowSize)); + } + this.windowSize = windowSize; + this.delaySamples = new long[windowSize]; + } + + @TestOnly + public DelayAnalyzer(int windowSize, int placeHolder) { + this.windowSize = windowSize; + this.delaySamples = new long[windowSize]; + } Review Comment: fixed -- 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]
