You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@accumulo.apache.org by "DomGarguilo (via GitHub)" <gi...@apache.org> on 2024/08/01 19:49:35 UTC

[PR] Replace OpTimer with new Timer class [accumulo]

DomGarguilo opened a new pull request, #4784:
URL: https://github.com/apache/accumulo/pull/4784

   Fixes #4777 
   
   This PR:
   * Creates a new class that is a combination of the OpTimer class and NanoTime class (which only exists in 3.1 and up)
   * removes the OpTimer class and replace its usages with the new Timer class
   * adds tests for the new class
   
   Once this is merged into 2.1 it will be merged up to replace NanoTime and OpTimer in 3.1 and elasticity.


-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "keith-turner (via GitHub)" <gi...@apache.org>.
keith-turner commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1700916369


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {

Review Comment:
   Seems like this stop method could be dropped.  It is not really used, or elapsed is called immediately after calling it.



##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {
+    if (!isStarted) {
+      throw new IllegalStateException("Timer is not running");
+    }
+    accumulatedNanos += System.nanoTime() - startNanos;
+    isStarted = false;
+  }
+
+  /**
+   * Resets the timer, stopping it if necessary and clearing accumulated time.
+   */
+  public void reset() {
+    accumulatedNanos = 0;
+    isStarted = false;
+  }
+
+  private long getElapsedNanos() {
+    return isStarted ? System.nanoTime() - startNanos + accumulatedNanos : accumulatedNanos;
+  }
+
+  /**
+   * Checks if the specified duration has elapsed since the timer was started.
+   *
+   * @param duration the duration to check.
+   * @return true if the specified duration has elapsed, false otherwise.
+   */
+  public boolean hasElapsed(Duration duration) {

Review Comment:
   This will be really nice for efficiency. 



##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {
+    if (!isStarted) {
+      throw new IllegalStateException("Timer is not running");
+    }
+    accumulatedNanos += System.nanoTime() - startNanos;
+    isStarted = false;
+  }
+
+  /**
+   * Resets the timer, stopping it if necessary and clearing accumulated time.
+   */
+  public void reset() {

Review Comment:
   Looking at use of this its only called in conjunction with the start method currently.   Could rename it restart() and make it also start the timer.
   
   Not sure about this, wondering if we could drop the start() method and only have startNew() and restart().  If only have those methods then would not need the isStarted boolean



##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {
+    if (!isStarted) {
+      throw new IllegalStateException("Timer is not running");
+    }
+    accumulatedNanos += System.nanoTime() - startNanos;
+    isStarted = false;
+  }
+
+  /**
+   * Resets the timer, stopping it if necessary and clearing accumulated time.
+   */
+  public void reset() {
+    accumulatedNanos = 0;
+    isStarted = false;
+  }
+
+  private long getElapsedNanos() {
+    return isStarted ? System.nanoTime() - startNanos + accumulatedNanos : accumulatedNanos;
+  }
+
+  /**
+   * Checks if the specified duration has elapsed since the timer was started.
+   *
+   * @param duration the duration to check.
+   * @return true if the specified duration has elapsed, false otherwise.
+   */
+  public boolean hasElapsed(Duration duration) {
+    return getElapsedNanos() >= duration.toNanos();
+  }
+
+  /**
+   * Calculates the elapsed time as a Duration.
+   *
+   * @return the elapsed time as a Duration.
+   */
+  public Duration elapsed() {

Review Comment:
   We could additionally have an `long elapsed(TimeUnit unit)` that avoids creating a Duration object.



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "keith-turner (via GitHub)" <gi...@apache.org>.
keith-turner commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1702008318


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {

Review Comment:
   Yeah, if its not being used currently and the potential use case you mentioned could be satisfied w/ multiple timers then it seems dropping it would be good for now.  Also its internal code, so we can always adapt it easily to future use 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.

To unsubscribe, e-mail: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#issuecomment-2265750660

   The past commits (cb58770 and 0b644db) made the following changes:
   * remove the `start()` and `stop()` methods
       * This allows us to remove some of the member variables and just use the static `startNew()` method for object creation
   * replace the `reset()` method with `restart()`
       * this combines the reset and start functionality for similicity
   * remove `toString()`
       * this was removed because without the `stop()` method, the time will keep running. This might be an issue if we want to use the timers value and also print it because they would differ slightly in time elapsed since the timer keeps running between calls. The user can just print the numeric value after calling `elapsed()`


-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1701920499


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {

Review Comment:
   I had the same idea but then thought it could be helpful in a situation where we want to start and stop a single timer multiple times to measure different parts of a process.
   
   I guess this isn't being done (with this object) at the moment so it might not be a case that we would use very often. We could also just use multiple `Timer`s in this case or some other pattern to measure time.
   
   So I am fine with dropping it if you think its a good idea.



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "dlmarion (via GitHub)" <gi...@apache.org>.
dlmarion commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1704009688


##########
core/src/test/java/org/apache/accumulo/core/util/TimerTest.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.jupiter.api.Test;
+
+public class TimerTest {
+
+  @Test
+  public void testRestart() throws InterruptedException {
+    Timer timer = Timer.startNew();
+
+    // Perform a longer sleep initially
+    Thread.sleep(100);
+
+    Duration firstElapsed = timer.elapsed();
+
+    assertTrue(timer.hasElapsed(Duration.ofMillis(100)),
+        "Should see at least the sleep time has elapsed.");
+
+    timer.restart();
+
+    // Perform a shorter sleep
+    Thread.sleep(50);
+
+    Duration secondElapsed = timer.elapsed();
+
+    // Assert that the elapsed time after restart is greater than 0
+    assertFalse(secondElapsed.isNegative(),
+        "Elapsed time should be greater than 0 after restarting the timer.");
+    assertTrue(secondElapsed.compareTo(firstElapsed) < 0,
+        "Elapsed time after restart should be less than the initial elapsed time.");
+
+  }
+
+  @Test
+  public void testHasElapsed() throws InterruptedException {
+    Timer timer = Timer.startNew();
+
+    Thread.sleep(50);
+
+    assertTrue(timer.hasElapsed(Duration.ofMillis(50)),
+        "The timer should indicate that 50 milliseconds have elapsed.");
+    assertFalse(timer.hasElapsed(Duration.ofMillis(100)),
+        "The timer should not indicate that 100 milliseconds have elapsed.");
+  }
+
+  @Test
+  public void testHasElapsedWithTimeUnit() throws InterruptedException {
+    Timer timer = Timer.startNew();
+
+    Thread.sleep(50);
+
+    assertTrue(timer.hasElapsed(50, MILLISECONDS),
+        "The timer should indicate that 50 milliseconds have elapsed.");
+    assertFalse(timer.hasElapsed(100, MILLISECONDS),
+        "The timer should not indicate that 100 milliseconds have elapsed.");
+  }
+
+  @Test
+  public void testElapsedPrecision() throws InterruptedException {
+    Timer timer = Timer.startNew();
+
+    final int sleepMillis = 50;
+    Thread.sleep(sleepMillis);
+
+    long elapsedMillis = timer.elapsed(MILLISECONDS);
+    assertEquals(sleepMillis, elapsedMillis, 5, "Elapsed time in milliseconds is not accurate.");

Review Comment:
   @DomGarguilo  - this test failed in the most recent elasticity full IT build. I'm not sure that you want to perform an `assertEquals` check on the duration of a `Thread.sleep` call. The [javadoc](https://docs.oracle.com/en%2Fjava%2Fjavase%2F11%2Fdocs%2Fapi%2F%2F/java.base/java/lang/Thread.html#sleep(long)) says that it might not be accurate and I tend to think of the Thread.sleep parameter as a lower bound. I'm fairly certain that when you call `Thread.sleep` the OS pauses the execution of that Thread, executing other tasks that need the CPU, and does not consider it for scheduling until the time has elapsed. On a lightly loaded system this likely means that the Thread will resume at or close to the time parameter that you specify. On a busy system, there is no guarantee when the Thread will resume. I think you want to change this check from `elapsedMillis == sleepMillis` to `elapsedMillis >= sleepMillis`. There may be other places in this test where that needs to happen as well.



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1702078367


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+
+  private Timer() {
+    this.startNanos = System.nanoTime();
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    return new Timer();
+  }
+
+  /**
+   * Resets the start point for this timer.
+   */
+  public void restart() {
+    this.startNanos = System.nanoTime();
+  }
+
+  private long getElapsedNanos() {
+    return System.nanoTime() - startNanos;
+  }
+
+  /**
+   * Checks if the specified duration has elapsed since the timer was started.
+   *
+   * @param duration the duration to check.
+   * @return true if the specified duration has elapsed, false otherwise.
+   */
+  public boolean hasElapsed(Duration duration) {
+    return getElapsedNanos() >= duration.toNanos();

Review Comment:
   Nice!



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo merged PR #4784:
URL: https://github.com/apache/accumulo/pull/4784


-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "keith-turner (via GitHub)" <gi...@apache.org>.
keith-turner commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1702072759


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+
+  private Timer() {
+    this.startNanos = System.nanoTime();
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {

Review Comment:
   Could rename this to `start()` since the other `start()` method was deleted. 



##########
core/src/main/java/org/apache/accumulo/core/clientImpl/TabletServerBatchReaderIterator.java:
##########
@@ -857,11 +857,10 @@ static void doLookup(ClientContext context, String server, Map<KeyExtent,List<Ra
         MultiScanResult scanResult = imsr.result;
 
         if (timer != null) {
-          timer.stop();
           log.trace("tid={} Got 1st multi scan results, #results={} {} in {}",
               Thread.currentThread().getId(), scanResult.results.size(),
               (scanResult.more ? "scanID=" + imsr.scanID : ""),
-              String.format("%.3f secs", timer.scale(SECONDS)));
+              String.format("%.3f secs", timer.elapsed().toMillis() / 1000.0));

Review Comment:
   A lot of these immediately call toMillis().  Those could call the method with the time unti
   
   ```suggestion
                 String.format("%.3f secs", timer.elapsed(TimeUnit.MILLIS) / 1000.0));
   ```



##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+
+  private Timer() {
+    this.startNanos = System.nanoTime();
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    return new Timer();
+  }
+
+  /**
+   * Resets the start point for this timer.
+   */
+  public void restart() {
+    this.startNanos = System.nanoTime();
+  }
+
+  private long getElapsedNanos() {
+    return System.nanoTime() - startNanos;
+  }
+
+  /**
+   * Checks if the specified duration has elapsed since the timer was started.
+   *
+   * @param duration the duration to check.
+   * @return true if the specified duration has elapsed, false otherwise.
+   */
+  public boolean hasElapsed(Duration duration) {
+    return getElapsedNanos() >= duration.toNanos();

Review Comment:
   The Duration object can hold durations that are larger than can fit in a 64 bit integer for nanos. Looked at the javadoc and if that happens then `toNanos()` will throw an exception which is good.



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "keith-turner (via GitHub)" <gi...@apache.org>.
keith-turner commented on PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#issuecomment-2265808825

   > I am trying to think of the best course of action for merging this up and making usage more consistent across branches.
   
   IMO it would be best to minimize refactoring in 2.1.  Could merge these changes up to elasticity, then circle back to main and start doing a wider to refactor to use this new class more broadly.  If any bugs are found during that refactor then could fix those in 2.1.


-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1701927141


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,112 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+  private long accumulatedNanos;
+  private boolean isStarted;
+
+  private Timer() {
+    this.accumulatedNanos = 0;
+    this.isStarted = false;
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {
+    Timer timer = new Timer();
+    timer.start();
+    return timer;
+  }
+
+  /**
+   * Starts the timer if it is not already running.
+   *
+   * @throws IllegalStateException if the timer is already running.
+   */
+  public void start() throws IllegalStateException {
+    if (isStarted) {
+      throw new IllegalStateException("Timer is already running");
+    }
+    startNanos = System.nanoTime();
+    isStarted = true;
+  }
+
+  /**
+   * Stops the timer if it is running and accumulates the elapsed time.
+   *
+   * @throws IllegalStateException if the timer is not running.
+   */
+  public void stop() throws IllegalStateException {
+    if (!isStarted) {
+      throw new IllegalStateException("Timer is not running");
+    }
+    accumulatedNanos += System.nanoTime() - startNanos;
+    isStarted = false;
+  }
+
+  /**
+   * Resets the timer, stopping it if necessary and clearing accumulated time.
+   */
+  public void reset() {
+    accumulatedNanos = 0;
+    isStarted = false;
+  }
+
+  private long getElapsedNanos() {
+    return isStarted ? System.nanoTime() - startNanos + accumulatedNanos : accumulatedNanos;
+  }
+
+  /**
+   * Checks if the specified duration has elapsed since the timer was started.
+   *
+   * @param duration the duration to check.
+   * @return true if the specified duration has elapsed, false otherwise.
+   */
+  public boolean hasElapsed(Duration duration) {
+    return getElapsedNanos() >= duration.toNanos();
+  }
+
+  /**
+   * Calculates the elapsed time as a Duration.
+   *
+   * @return the elapsed time as a Duration.
+   */
+  public Duration elapsed() {

Review Comment:
   good idea



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on code in PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#discussion_r1702078066


##########
core/src/main/java/org/apache/accumulo/core/util/Timer.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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
+ *
+ *   https://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.accumulo.core.util;
+
+import java.time.Duration;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * This class provides a timer for measuring elapsed time.
+ */
+public final class Timer {
+
+  private long startNanos;
+
+  private Timer() {
+    this.startNanos = System.nanoTime();
+  }
+
+  /**
+   * Creates and starts a new Timer instance.
+   *
+   * @return a new Timer instance that is already started.
+   */
+  public static Timer startNew() {

Review Comment:
   I kind of like `startNew()` as it implies a new object is being created and started



-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Replace OpTimer with new Timer class [accumulo]

Posted by "DomGarguilo (via GitHub)" <gi...@apache.org>.
DomGarguilo commented on PR #4784:
URL: https://github.com/apache/accumulo/pull/4784#issuecomment-2265769401

   I am trying to think of the best course of action for merging this up and making usage more consistent across branches.
   
   Should we look for places that, currently NanoTime, is being used in main and make use of this new Timer object in those spots here in 2.1?
   
   It seems like this would make things easier to merge up and also make things more consistent but I am not sure if its worth the disruption to 2.1 code or not.


-- 
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: notifications-unsubscribe@accumulo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org