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


##########
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:
   this is not monotinc
   
   ```
   call 1:
   lastNow = MIN_VALUE
   now = 100
   delta = 0
   return (100 + 0) = 100
   
   call 2:
   lastNow = 100
   now = 99
   delta = 1
   
   lastNow = 99
   return (99 + 1) = 100
   ```
   
   If you want something like this its far simpler just to fallback to a 
counter when `now` rolls backwards
   
   ```
   private long lastNow = Long.MIN_VALUE;
   
   long getAsLong() {
     long now = nowSupplier.getAsLong();
     if (now < lastNow)
       now = lastNow + 1;
     lastNow = now;
     return now;
   }
   ```



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