adoroszlai commented on code in PR #8498:
URL: https://github.com/apache/ozone/pull/8498#discussion_r2203198395


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/SlidingWindow.java:
##########
@@ -0,0 +1,94 @@
+/*
+ * 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.hadoop.ozone.container.common.utils;
+
+import java.time.Duration;
+import java.util.ArrayDeque;
+import java.util.Deque;
+import org.apache.hadoop.util.Time;
+
+/**
+ * A time-based sliding window implementation that tracks event timestamps.
+ */
+public class SlidingWindow {
+  private final Object lock = new Object();
+  private final int windowSize;
+  private final Deque<Long> timestamps;
+  private final long expiryDurationMillis;
+
+  /**
+   * @param windowSize     the maximum number of events that are tracked
+   * @param expiryDuration the duration after which an entry in the window 
expires
+   */
+  public SlidingWindow(int windowSize, Duration expiryDuration) {
+    if (windowSize <= 0) {
+      throw new IllegalArgumentException("Window size must be greater than 0");
+    }
+    if (expiryDuration.isNegative() || expiryDuration.isZero()) {
+      throw new IllegalArgumentException("Expiry duration must be greater than 
0");
+    }
+    this.expiryDurationMillis = expiryDuration.toMillis();
+    this.windowSize = windowSize;
+    // We limit the initial queue size to 100 to control the memory usage
+    this.timestamps = new ArrayDeque<>(Math.min(windowSize + 1, 100));
+  }
+
+  public void add() {
+    synchronized (lock) {
+      removeExpired();
+
+      if (isFull()) {

Review Comment:
   Call to `removeExpired()` is not necessary here, `isFull()` also does that.



##########
hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/utils/TestSlidingWindow.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.hadoop.ozone.container.common.utils;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+/**
+ * Tests for {@link SlidingWindow} class.
+ */
+public class TestSlidingWindow {
+
+  private SlidingWindow slidingWindow;
+
+  @BeforeEach
+  public void setup() {
+    slidingWindow = new SlidingWindow(3, Duration.ofSeconds(5));

Review Comment:
   This can be moved to `testAdd()`, its only usage (other test cases define 
their own instance).



##########
hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/utils/TestSlidingWindow.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.hadoop.ozone.container.common.utils;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+/**
+ * Tests for {@link SlidingWindow} class.
+ */
+public class TestSlidingWindow {
+
+  private SlidingWindow slidingWindow;
+
+  @BeforeEach
+  public void setup() {
+    slidingWindow = new SlidingWindow(3, Duration.ofSeconds(5));
+  }
+
+  @Test
+  public void testConstructorValidation() {
+    // Test invalid window size
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(0, Duration.ofMillis(100)));
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(-1, Duration.ofMillis(100)));
+
+    // Test invalid expiry duration
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(1, Duration.ofMillis(0)));
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(1, Duration.ofMillis(-1)));
+  }
+
+  @Test
+  public void testAdd() {
+    for (int i = 0; i < slidingWindow.getWindowSize(); i++) {
+      slidingWindow.add();
+      assertFalse(slidingWindow.isFull());
+    }
+
+    slidingWindow.add();
+    assertTrue(slidingWindow.isFull());
+  }
+
+  @Test
+  public void testEventExpiration() throws InterruptedException {
+    slidingWindow = new SlidingWindow(2, Duration.ofMillis(500));
+
+    // Add events to reach threshold
+    slidingWindow.add();
+    slidingWindow.add();
+    slidingWindow.add();
+    assertTrue(slidingWindow.isFull());
+
+    // Wait for events to expire
+    Thread.sleep(600);
+
+    assertFalse(slidingWindow.isFull());
+
+    // Add one more event - should not be enough to mark as full
+    slidingWindow.add();
+    assertFalse(slidingWindow.isFull());
+  }
+
+  @Test
+  public void testPartialExpiration() throws InterruptedException {
+    slidingWindow = new SlidingWindow(3, Duration.ofSeconds(1));
+
+    slidingWindow.add();
+    slidingWindow.add();
+    slidingWindow.add();
+    slidingWindow.add();
+    assertTrue(slidingWindow.isFull());
+
+    Thread.sleep(600);
+    slidingWindow.add(); // this will remove the oldest event as the window is 
full
+
+    // Wait for the oldest events to expire
+    Thread.sleep(500);
+    assertFalse(slidingWindow.isFull());
+  }
+
+  @Test
+  @Timeout(value = 10)
+  public void testConcurrentAccess() throws InterruptedException {

Review Comment:
   I don't think we need this test case.  `SlidingWindow` is a simple class, 
and we can also rely on static analysis (Spotbugs).



##########
hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/utils/TestSlidingWindow.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * 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.hadoop.ozone.container.common.utils;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.Timeout;
+
+/**
+ * Tests for {@link SlidingWindow} class.
+ */
+public class TestSlidingWindow {
+
+  private SlidingWindow slidingWindow;
+
+  @BeforeEach
+  public void setup() {
+    slidingWindow = new SlidingWindow(3, Duration.ofSeconds(5));
+  }
+
+  @Test
+  public void testConstructorValidation() {
+    // Test invalid window size
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(0, Duration.ofMillis(100)));
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(-1, Duration.ofMillis(100)));
+
+    // Test invalid expiry duration
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(1, Duration.ofMillis(0)));
+    assertThrows(IllegalArgumentException.class, () -> 
+        new SlidingWindow(1, Duration.ofMillis(-1)));
+  }
+
+  @Test
+  public void testAdd() {
+    for (int i = 0; i < slidingWindow.getWindowSize(); i++) {
+      slidingWindow.add();
+      assertFalse(slidingWindow.isFull());
+    }
+
+    slidingWindow.add();
+    assertTrue(slidingWindow.isFull());
+  }
+
+  @Test
+  public void testEventExpiration() throws InterruptedException {
+    slidingWindow = new SlidingWindow(2, Duration.ofMillis(500));
+
+    // Add events to reach threshold
+    slidingWindow.add();
+    slidingWindow.add();
+    slidingWindow.add();
+    assertTrue(slidingWindow.isFull());
+
+    // Wait for events to expire
+    Thread.sleep(600);

Review Comment:
   Please avoid tests based on sleep.  These introduce unnecessary delays and 
flakiness.  Let's use `Clock` in `SlidingWindow`, with a private implementation 
based on `Time.monotonicNow()` for real usage, and `TestClock` for the test.  
Use `TestClock#fastForward` for controlling "time" without delay or flakiness.
   
   
https://github.com/apache/ozone/blob/fb75be7a8962b7ed57d75e0fed3fcd2befae2b6b/hadoop-hdds/test-utils/src/test/java/org/apache/ozone/test/TestClock.java#L26-L31



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to