@@ -355,8 +355,8 @@ private void runSynchronizers() {
355355 // Fallback and recovery apply only when another synchronizer can take over.
356356 boolean canFallBack = sourceManager .getAvailableSynchronizerCount () > 1 ;
357357 boolean canRecover = canFallBack && !sourceManager .isPrimeSynchronizer ();
358- Instant recoveryTime = canRecover ? Instant .now ().plus (recoveryTimeout ) : null ;
359- Instant fallbackTime = null ;
358+ MonotonicTime recoveryTime = canRecover ? MonotonicTime .now ().plus (recoveryTimeout ) : null ;
359+ MonotonicTime fallbackTime = null ;
360360 // True once the loop has seen the deadline pass.
361361 boolean recoveryMet = false ;
362362 boolean fallbackMet = false ;
@@ -371,7 +371,7 @@ private void runSynchronizers() {
371371 }
372372
373373 // Wait for the next result, or for the next deadline that the loop has not seen pass.
374- Instant wakeTime = earliest (
374+ MonotonicTime wakeTime = earliest (
375375 recoveryMet ? null : recoveryTime ,
376376 fallbackMet ? null : fallbackTime ,
377377 sourceManager .getEarliestUnparkTime (),
@@ -407,7 +407,7 @@ private void runSynchronizers() {
407407 status .getState ());
408408 // The fallback timeout runs from the first interruption.
409409 if (canFallBack && fallbackTime == null ) {
410- fallbackTime = Instant .now ().plus (fallbackTimeout );
410+ fallbackTime = MonotonicTime .now ().plus (fallbackTimeout );
411411 }
412412 dataSourceUpdates .updateStatus (
413413 DataSourceStatusProvider .State .INTERRUPTED ,
@@ -451,7 +451,7 @@ private void runSynchronizers() {
451451 if (sourceManager .isCurrentSynchronizerFDv1Fallback ()) {
452452 // A directive during the FDv1 fallback replaces its TTL, and FDv1 keeps running.
453453 Duration fallbackTTL = jittered (defaultFDv1FallbackTTL );
454- sourceManager .startFDv1Fallback (Instant .now ().plus (fallbackTTL ));
454+ sourceManager .startFDv1Fallback (MonotonicTime .now ().plus (fallbackTTL ));
455455 logger .info ("FDv2 retry scheduled in {}s." , fallbackTTL .getSeconds ());
456456 } else {
457457 // A directive to fall back to FDv1 ends the run of an FDv2 synchronizer.
@@ -470,7 +470,7 @@ private void runSynchronizers() {
470470 }
471471
472472 // Check every deadline against the current time.
473- Instant now = Instant .now ();
473+ MonotonicTime now = MonotonicTime .now ();
474474 sourceManager .unparkExpiredSynchronizers ();
475475 if (sourceManager .endFDv1FallbackIfExpired ()) {
476476 logger .info ("Fallback TTL elapsed, restarting FDv2 data sources." );
@@ -579,14 +579,13 @@ private boolean waitUnlessClosed(Duration duration) {
579579 * @return true if the future completed
580580 * @throws InterruptedException if the thread is interrupted while waiting
581581 */
582- private boolean awaitUntil (CompletableFuture <?> future , Instant time ) throws InterruptedException {
582+ private boolean awaitUntil (CompletableFuture <?> future , MonotonicTime time ) throws InterruptedException {
583583 CompletableFuture <Object > futureOrClosed = CompletableFuture .anyOf (future , closeLatch .newFuture ());
584584 try {
585585 if (time == null ) {
586586 futureOrClosed .get ();
587587 } else {
588- futureOrClosed .get (
589- Math .max (0 , Duration .between (Instant .now (), time ).toNanos ()), TimeUnit .NANOSECONDS );
588+ futureOrClosed .get (Math .max (0 , MonotonicTime .now ().until (time ).toNanos ()), TimeUnit .NANOSECONDS );
590589 }
591590 } catch (TimeoutException e ) {
592591 return false ;
@@ -602,9 +601,9 @@ private boolean awaitUntil(CompletableFuture<?> future, Instant time) throws Int
602601 * @param times the times, any of which may be null
603602 * @return the earliest time, or null if every time is null
604603 */
605- private static Instant earliest (Instant ... times ) {
606- Instant earliest = null ;
607- for (Instant time : times ) {
604+ private static MonotonicTime earliest (MonotonicTime ... times ) {
605+ MonotonicTime earliest = null ;
606+ for (MonotonicTime time : times ) {
608607 if (time != null && (earliest == null || time .isBefore (earliest ))) {
609608 earliest = time ;
610609 }
@@ -643,7 +642,7 @@ private Duration parkDelay(int parkCount) {
643642 */
644643 private void parkActiveSynchronizer (String synchronizerName , DataSourceStatusProvider .ErrorInfo errorInfo ) {
645644 Duration parkDelay = parkDelay (sourceManager .incrementCurrentSynchronizerParkCount ());
646- sourceManager .parkCurrentSynchronizer (Instant .now ().plus (parkDelay ));
645+ sourceManager .parkCurrentSynchronizer (MonotonicTime .now ().plus (parkDelay ));
647646 logger .warn (
648647 "{} is backing off for {}s after an unexpected error ({})." ,
649648 synchronizerName != null ? "Synchronizer '" + synchronizerName + "'"
@@ -660,7 +659,7 @@ private void parkActiveSynchronizer(String synchronizerName, DataSourceStatusPro
660659 * @param errorInfo the error that came with the directive, or null
661660 */
662661 private void fallBackToFDv1 (Duration fallbackTTL , DataSourceStatusProvider .ErrorInfo errorInfo ) {
663- sourceManager .startFDv1Fallback (Instant .now ().plus (fallbackTTL ));
662+ sourceManager .startFDv1Fallback (MonotonicTime .now ().plus (fallbackTTL ));
664663 if (sourceManager .hasFDv1Fallback ()) {
665664 logger .info ("Falling back to FDv1 fallback synchronizer." );
666665 } else {
@@ -703,23 +702,24 @@ private Synchronizer buildNextAvailableSynchronizer() {
703702 return synchronizer ;
704703 }
705704
706- Instant earliestUnparkTime = sourceManager .getEarliestUnparkTime ();
707- Instant fallbackEndTime = sourceManager .getFDv1FallbackEndTime ();
705+ MonotonicTime earliestUnparkTime = sourceManager .getEarliestUnparkTime ();
706+ MonotonicTime fallbackEndTime = sourceManager .getFDv1FallbackEndTime ();
708707 Duration wait ;
709708 if (earliestUnparkTime == null && fallbackEndTime == null ) {
710709 // This should never happen.
711710 wait = parkInitialDelay ;
712711 logger .error ("No synchronizer can be started; checking again in {}s." , wait .getSeconds ());
713712 } else if (earliestUnparkTime == null ) {
714- wait = Duration . between ( Instant . now (), fallbackEndTime );
713+ wait = MonotonicTime . now (). until ( fallbackEndTime );
715714 logger .warn ("No synchronizer is available until the FDv1 fallback ends in {}s." ,
716715 Math .max (0 , wait .getSeconds ()));
717716 } else if (fallbackEndTime == null ) {
718- wait = Duration . between ( Instant . now (), earliestUnparkTime );
717+ wait = MonotonicTime . now (). until ( earliestUnparkTime );
719718 logger .warn ("All synchronizers are backing off for at least {}s." , Math .max (0 , wait .getSeconds ()));
720719 } else {
721- Instant earliest = fallbackEndTime .isBefore (earliestUnparkTime ) ? fallbackEndTime : earliestUnparkTime ;
722- wait = Duration .between (Instant .now (), earliest );
720+ MonotonicTime earliest =
721+ fallbackEndTime .isBefore (earliestUnparkTime ) ? fallbackEndTime : earliestUnparkTime ;
722+ wait = MonotonicTime .now ().until (earliest );
723723 logger .warn ("All synchronizers are backing off for at least {}s." , Math .max (0 , wait .getSeconds ()));
724724 }
725725 // No synchronizer is running until the wait ends.
0 commit comments