Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-5112

Represent timeouts internally as deadlines

    • Type: Icon: Improvement Improvement
    • Resolution: Done
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      https://github.com/mongodb/mongo-java-driver/pull/1164 added TimePoint1, which implements Comparable<TimePoint>, as a tool to measure elapsed time:

      TimePoint start = TimePoint.now();
      ...
      Duration elapsed1 = start.elapsed();
      Duration elapsed2 = TimePoint.now().durationSince(start);
      

      maxim.katcharov@mongodb.com suggested in that PR that the driver can use deadlines internally instead of timeouts. If we do this, we won't need to have two APIs, Timeout and TimePoint, we will only need TimePoint. I like this idea.

      The following table shows how usage may change as a result (the examples assume that the operation we are doing is acquiring a Lock):

      timeouts deadlines
      // results in calling `tryLock(...)`
      Timeout timeout1 = Timeout.startNow(1, SECONDS);
      Timeout timeout2 = Timeout.started(1, SECONDS, t);
      // results in calling `tryLock(0, ...)`/`tryLock()`
      Timeout expiredTimeout = Timeout.expired();
      // results in calling `lock()`
      Timeout noTimeout = Timeout.infinite();
      
      // results in calling `tryLock(...)`
      TimePoint deadline1 = TimePoint.now().add(Duration.ofSeconds(1));
      TimePoint deadline2 = t.add(Duration.ofSeconds(1));
      // results in calling `tryLock(0, ...)`/`tryLock()`
      TimePoint missedDeadline = TimePoint.now();
      // results in calling `lock()`
      TimePoint noDeadline = TimePoint.infinity();
      
      tryLock(
              timeout.remaining(NANOSECONDS),
              NANOSECONDS);
      
      tryLock(
              deadline.saturatingDurationSince(TimePoint.now()).toNanos(),
              NANOSECONDS);
      
      timeout.expired();
      
      deadline.isNotAfter(TimePoint.now());
      deadline.compareTo(TimePoint.now()) <= 0;
      
      timeout.isInfinite();
      
      deadline.isInfinity();
      deadline.equals(TimePoint.infinite());
      deadline.compareTo(TimePoint.infinity()) == 0;
      TimePoint.infinity().isNotAfter(deadline);
      

      1 TimePoint is, of course, implemented via System.nanoTime, but it is better to have a tool that

      • exposes types less generic than long
      • and values that are meaningful on their own (a value returned by System.nanoTime is meaningless on its own).

            Assignee:
            maxim.katcharov@mongodb.com Maxim Katcharov
            Reporter:
            valentin.kovalenko@mongodb.com Valentin Kavalenka
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: