cshannon commented on code in PR #4494:
URL: https://github.com/apache/accumulo/pull/4494#discussion_r1596638898


##########
server/manager/src/test/java/org/apache/accumulo/manager/ManagerTimeTest.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.manager;
+
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+
+import org.apache.accumulo.core.util.time.SteadyTime;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class ManagerTimeTest {
+
+  @Test
+  public void testSteadyTime() {
+    long time = 20_000;
+    var steadyTime = SteadyTime.from(time);
+
+    // make sure calling serialize on instance matches static helper
+    byte[] serialized = ManagerTime.serialize(steadyTime);
+    assertArrayEquals(serialized, ManagerTime.serialize(steadyTime));
+
+    // Verify deserialization matches original object
+    var deserialized = ManagerTime.deserialize(serialized);
+    assertEquals(steadyTime, deserialized);
+    assertEquals(0, steadyTime.compareTo(deserialized));
+  }
+
+  @ParameterizedTest
+  // Test with both a 0 and positive previous value. This simulates the value
+  // read out of zookeeper for the time and should never be negative as it is
+  // based on elapsed time from previous manager runs
+  @ValueSource(longs = {0, 50_000})
+  public void testSteadyTimeFromSkew(long previousTime) throws 
InterruptedException {
+    List<Long> times = List.of(-100_000L, -100L, 0L, 20_000L, 
System.nanoTime());
+
+    for (Long time : times) {
+      // ManagerTime builds the skew amount by subtracting the current nanotime
+      // from the previous persisted time in ZK. The skew can be negative or 
positive because
+      // it will depend on if the current nanotime is negative or positive as
+      // nanotime is allowed to be negative
+      var skewAmount = ManagerTime.updateSkew(SteadyTime.from(previousTime), 
time);
+
+      // Build a SteadyTime using the skewAmount
+      // SteadyTime should never be negative
+      var original = ManagerTime.fromSkew(time, skewAmount);
+
+      // Simulate a future time and create another SteadyTime from the skew 
which should
+      // now be after the original
+      time = time + 10000;
+      var futureSkew = ManagerTime.fromSkew(time, skewAmount);
+
+      // future should be after the original
+      assertTrue(futureSkew.compareTo(original) > 0);
+    }
+  }
+
+  @ParameterizedTest
+  // Test with both a 0 and positive previous value. This simulates the value
+  // read out of zookeeper for the time and should never be negative as it is
+  // based on elapsed time from previous manager runs
+  @ValueSource(longs = {0, 50_000})
+  public void testSteadyTimeFromSkewCurrent(long previousTime) throws 
InterruptedException {
+    // Also test fromSkew(skewAmount) method which only uses System.nanoTime()
+    var skewAmount = ManagerTime.updateSkew(SteadyTime.from(previousTime));
+
+    // Build a SteadyTime using the skewAmount and current time
+    var original = ManagerTime.fromSkew(skewAmount);

Review Comment:
   Yes, a skew can be negative but the SteadyTime duration should never be 
negative. I added comments about the possible negative 
[here](https://github.com/apache/accumulo/pull/4494/files#diff-fa00d4556410517fab86a5a4a0ba34405582e7fb30e4e320b19974e87212a6f7R54-R56)
 and added a check for non-negative for SteadyTime 
[here](https://github.com/apache/accumulo/pull/4494/files#diff-702cd267f71e8c3f2d08ab609159181c76b07d88bb8fe02b8a46c91ab51e4305R35).
   
   The reason for the negative skew, as I mentioned in the comments, is that 
nano time may be positive or negative so you end up with a negative skew 
because of it. The 
[javadocs](https://docs.oracle.com/en%2Fjava%2Fjavase%2F17%2Fdocs%2Fapi%2F%2F/java.base/java/lang/System.html#nanoTime())
 for System.nanotime() state:
   
   > The value returned represents nanoseconds since some fixed but arbitrary 
origin time (perhaps in the future, so values may be negative). 
   
   The skew is computed by 
[subtracting](https://github.com/apache/accumulo/pull/4494/files#diff-fa00d4556410517fab86a5a4a0ba34405582e7fb30e4e320b19974e87212a6f7R142)
 the current nano time from the previously persisted duration. This skew is 
then saved and 
[added](https://github.com/apache/accumulo/pull/4494/files#diff-fa00d4556410517fab86a5a4a0ba34405582e7fb30e4e320b19974e87212a6f7R165)
 to the current time when computing the current SteadyTime 
   
   Examples of a negative skew is as follows:
   
   1. There's an existing persisted SteadyTime duration from the total previous 
manager runs of `1,000,000`.
   2. Manager starts up and the current nano time is `2,000,000`
   3. The skew gets computed as the previous steady time duration minus the 
current time so that becomes: `1,000,000 - 2,000,000 = -1,000,000 `so the skew 
value is negative 1 million in this case.
   4. When the code wants to get the current steady time, it calls the API and 
a new steady time is computed by adding the current nano time plus the skew. So 
let's say `100,000` ns have elapsed since start so the current time is now 
`2,100,000`. This results in:` (-1,000,000) + 2,100,000 = 1,100,000 `so you get 
a SteadyTime value that is the current elapsed time of 100,000 for the current 
manager run plus the previous SteadyTime of 1 million that was loaded on start 
up.
   
   
   
   
   



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