aweisberg commented on code in PR #101:
URL: https://github.com/apache/cassandra-accord/pull/101#discussion_r1664643939


##########
accord-core/src/main/java/accord/local/NodeTimeService.java:
##########
@@ -36,18 +37,53 @@ public interface NodeTimeService
     long now();
 
     /**
-     * Return the current time since the Unix epoch in the specified time 
unit. May still be simulated time and not
-     * real time.
+     * Return the current time since some arbitrary epoch in the specified 
time unit. May still be simulated time and not
+     * real time. The time returned by this will be monotonic.
      */
-    long unix(TimeUnit unit);
+    long elapsed(TimeUnit unit);
 
     Timestamp uniqueNow(Timestamp atLeast);
 
-    static ToLongFunction<TimeUnit> unixWrapper(TimeUnit sourceUnit, 
LongSupplier nowSupplier)
+    static ToLongFunction<TimeUnit> elapsedWrapperFromMonotonicSource(TimeUnit 
sourceUnit, LongSupplier monotonicNowSupplier)
+    {
+        return resultUnit ->  
resultUnit.convert(monotonicNowSupplier.getAsLong(), sourceUnit);
+    }
+
+    /**
+     * Allow time progression to be controlled using a potentially 
non-monotonic time source for testing with simulated
+     * time sources. This is not designed to be fast and real usage should be 
with a monotonic time source.
+     */
+    @VisibleForTesting
+    static ToLongFunction<TimeUnit> 
elapsedWrapperFromNonMonotonicSource(TimeUnit sourceUnit, LongSupplier 
nonMonotonicNowSupplier)
     {
-        return resultUnit -> {
-            Invariants.checkArgument(resultUnit != TimeUnit.NANOSECONDS, 
"Nanoseconds since epoch doesn't fit in a long");
-            return resultUnit.convert(nowSupplier.getAsLong(), sourceUnit);
-        };
+        return elapsedWrapperFromMonotonicSource(sourceUnit, new 
MonotonicWrapper(nonMonotonicNowSupplier));
     }
+
+    class MonotonicWrapper implements LongSupplier
+    {
+       private final LongSupplier nowSupplier;
+       private long lastNow = Long.MIN_VALUE;
+       private long delta = 0;
+
+       // Use an arbitrary epoch
+       private long epoch = Long.MAX_VALUE / 4;
+
+       private MonotonicWrapper(LongSupplier nowSupplier)
+       {
+           this.nowSupplier = nowSupplier;
+       }
+
+       @Override
+       public synchronized long getAsLong()

Review Comment:
   It's normal for something that reports time to be able to report the same 
value multiple times and it also fits the definition for monotonic which is 
either non-increasing or non-decreasing. In this case the function is 
non-decreasing.
   
   This isn't a transaction id generation function trying to create new unique 
values where counting makes sense to keep generating unique values when time 
doesn't move forward.



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