adoroszlai commented on code in PR #8498: URL: https://github.com/apache/ozone/pull/8498#discussion_r2141783083
########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/SlidingWindow.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.util.ArrayDeque; +import java.util.Deque; +import java.util.concurrent.TimeUnit; + +/** + * A time-based sliding window implementation that tracks only failed test results within a specified time duration. + * It determines failure based on a configured tolerance threshold. + * + * The queue saves one failure more than the configured tolerance threshold, + * so that the window can be considered failed. + */ +public class SlidingWindow { + private final long windowDuration; + private final TimeUnit timeUnit; + private final int failureTolerance; + private final Deque<Long> failureTimestamps; + + /** + * @param failureTolerance the number of failures that can be tolerated before the window is considered failed + * @param windowDuration the duration of the sliding window + * @param timeUnit the time unit of the window duration + */ + public SlidingWindow(int failureTolerance, long windowDuration, TimeUnit timeUnit) { Review Comment: Please consider using `Duration` instead of separate parameters `windowDuration` and `timeUnit`. ########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/SlidingWindow.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.util.ArrayDeque; +import java.util.Deque; +import java.util.concurrent.TimeUnit; + +/** + * A time-based sliding window implementation that tracks only failed test results within a specified time duration. + * It determines failure based on a configured tolerance threshold. + * + * The queue saves one failure more than the configured tolerance threshold, + * so that the window can be considered failed. + */ +public class SlidingWindow { + private final long windowDuration; + private final TimeUnit timeUnit; + private final int failureTolerance; + private final Deque<Long> failureTimestamps; + + /** + * @param failureTolerance the number of failures that can be tolerated before the window is considered failed + * @param windowDuration the duration of the sliding window + * @param timeUnit the time unit of the window duration + */ + public SlidingWindow(int failureTolerance, long windowDuration, TimeUnit timeUnit) { + this.windowDuration = windowDuration; + this.timeUnit = timeUnit; + this.failureTolerance = failureTolerance; + // If the failure tolerance is high, we limit the queue size to 100 as we want to control the memory usage + this.failureTimestamps = new ArrayDeque<>(Math.min(failureTolerance + 1, 100)); + } + + public synchronized void add(boolean result) { + if (!result) { + if (failureTolerance > 0 && failureTimestamps.size() > failureTolerance) { + failureTimestamps.remove(); + } + long currentTime = System.currentTimeMillis(); Review Comment: Can use `Time.monotonicNow()`, since the timestamp is not persisted or sent over the network. Add a private method to ensure it is consistently used in both add and remove. ########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/SlidingWindow.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.util.ArrayDeque; +import java.util.Deque; +import java.util.concurrent.TimeUnit; + +/** + * A time-based sliding window implementation that tracks only failed test results within a specified time duration. + * It determines failure based on a configured tolerance threshold. + * + * The queue saves one failure more than the configured tolerance threshold, + * so that the window can be considered failed. + */ +public class SlidingWindow { + private final long windowDuration; + private final TimeUnit timeUnit; + private final int failureTolerance; + private final Deque<Long> failureTimestamps; + + /** + * @param failureTolerance the number of failures that can be tolerated before the window is considered failed + * @param windowDuration the duration of the sliding window + * @param timeUnit the time unit of the window duration + */ + public SlidingWindow(int failureTolerance, long windowDuration, TimeUnit timeUnit) { + this.windowDuration = windowDuration; + this.timeUnit = timeUnit; + this.failureTolerance = failureTolerance; + // If the failure tolerance is high, we limit the queue size to 100 as we want to control the memory usage + this.failureTimestamps = new ArrayDeque<>(Math.min(failureTolerance + 1, 100)); + } + + public synchronized void add(boolean result) { + if (!result) { + if (failureTolerance > 0 && failureTimestamps.size() > failureTolerance) { + failureTimestamps.remove(); + } + long currentTime = System.currentTimeMillis(); + failureTimestamps.addLast(currentTime); + } + + removeExpiredFailures(); + } + + public synchronized boolean isFailed() { + removeExpiredFailures(); + return failureTimestamps.size() > failureTolerance; + } + + private void removeExpiredFailures() { + long currentTime = System.currentTimeMillis(); + long expirationThreshold = currentTime - timeUnit.toMillis(windowDuration); Review Comment: Convert `toMillis` in the constructor. The result will not change over time. ########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/utils/SlidingWindow.java: ########## @@ -0,0 +1,75 @@ +/* + * 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.util.ArrayDeque; +import java.util.Deque; +import java.util.concurrent.TimeUnit; + +/** + * A time-based sliding window implementation that tracks only failed test results within a specified time duration. + * It determines failure based on a configured tolerance threshold. + * + * The queue saves one failure more than the configured tolerance threshold, + * so that the window can be considered failed. + */ +public class SlidingWindow { + private final long windowDuration; + private final TimeUnit timeUnit; + private final int failureTolerance; + private final Deque<Long> failureTimestamps; + + /** + * @param failureTolerance the number of failures that can be tolerated before the window is considered failed + * @param windowDuration the duration of the sliding window + * @param timeUnit the time unit of the window duration + */ + public SlidingWindow(int failureTolerance, long windowDuration, TimeUnit timeUnit) { + this.windowDuration = windowDuration; + this.timeUnit = timeUnit; + this.failureTolerance = failureTolerance; + // If the failure tolerance is high, we limit the queue size to 100 as we want to control the memory usage + this.failureTimestamps = new ArrayDeque<>(Math.min(failureTolerance + 1, 100)); + } + + public synchronized void add(boolean result) { + if (!result) { + if (failureTolerance > 0 && failureTimestamps.size() > failureTolerance) { + failureTimestamps.remove(); + } + long currentTime = System.currentTimeMillis(); + failureTimestamps.addLast(currentTime); + } + + removeExpiredFailures(); + } + + public synchronized boolean isFailed() { Review Comment: Consider synchronizing on a `private final Object lock` for [safety](https://wiki.sei.cmu.edu/confluence/display/java/LCK00-J.+Use+private+final+lock+objects+to+synchronize+classes+that+may+interact+with+untrusted+code). -- 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]
