You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProposedTimestamp.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (C) 2016, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.util.time;
  44. import static java.util.concurrent.TimeUnit.MICROSECONDS;
  45. import static java.util.concurrent.TimeUnit.MILLISECONDS;
  46. import java.sql.Timestamp;
  47. import java.time.Duration;
  48. import java.time.Instant;
  49. import java.util.Date;
  50. import java.util.Iterator;
  51. import java.util.concurrent.TimeUnit;
  52. import java.util.concurrent.TimeoutException;
  53. /**
  54. * A timestamp generated by {@link MonotonicClock#propose()}.
  55. * <p>
  56. * ProposedTimestamp implements AutoCloseable so that implementations can
  57. * release resources associated with obtaining certainty about time elapsing.
  58. * For example the constructing MonotonicClock may start network IO with peers
  59. * when creating the ProposedTimestamp, and {@link #close()} can ensure those
  60. * network resources are released in a timely fashion.
  61. *
  62. * @since 4.6
  63. */
  64. public abstract class ProposedTimestamp implements AutoCloseable {
  65. /**
  66. * Wait for several timestamps.
  67. *
  68. * @param times
  69. * timestamps to wait on.
  70. * @param maxWait
  71. * how long to wait for the timestamps.
  72. * @throws InterruptedException
  73. * current thread was interrupted before the waiting process
  74. * completed normally.
  75. * @throws TimeoutException
  76. * the timeout was reached without the proposed timestamp become
  77. * certainly in the past.
  78. */
  79. public static void blockUntil(Iterable<ProposedTimestamp> times,
  80. Duration maxWait) throws TimeoutException, InterruptedException {
  81. Iterator<ProposedTimestamp> itr = times.iterator();
  82. if (!itr.hasNext()) {
  83. return;
  84. }
  85. long now = System.currentTimeMillis();
  86. long deadline = now + maxWait.toMillis();
  87. for (;;) {
  88. long w = deadline - now;
  89. if (w < 0) {
  90. throw new TimeoutException();
  91. }
  92. itr.next().blockUntil(Duration.ofMillis(w));
  93. if (itr.hasNext()) {
  94. now = System.currentTimeMillis();
  95. } else {
  96. break;
  97. }
  98. }
  99. }
  100. /**
  101. * Read the timestamp as {@code unit} since the epoch.
  102. * <p>
  103. * The timestamp value for a specific {@code ProposedTimestamp} object never
  104. * changes, and can be read before {@link #blockUntil(Duration)}.
  105. *
  106. * @param unit
  107. * what unit to return the timestamp in. The timestamp will be
  108. * rounded if the unit is bigger than the clock's granularity.
  109. * @return {@code unit} since the epoch.
  110. */
  111. public abstract long read(TimeUnit unit);
  112. /**
  113. * Wait for this proposed timestamp to be certainly in the recent past.
  114. * <p>
  115. * This method forces the caller to wait up to {@code timeout} for
  116. * {@code this} to pass sufficiently into the past such that the creating
  117. * {@link MonotonicClock} instance will not create an earlier timestamp.
  118. *
  119. * @param maxWait
  120. * how long the implementation may block the caller.
  121. * @throws InterruptedException
  122. * current thread was interrupted before the waiting process
  123. * completed normally.
  124. * @throws TimeoutException
  125. * the timeout was reached without the proposed timestamp
  126. * becoming certainly in the past.
  127. */
  128. public abstract void blockUntil(Duration maxWait)
  129. throws InterruptedException, TimeoutException;
  130. /** @return milliseconds since epoch; {@code read(MILLISECONDS}). */
  131. public long millis() {
  132. return read(MILLISECONDS);
  133. }
  134. /** @return microseconds since epoch; {@code read(MICROSECONDS}). */
  135. public long micros() {
  136. return read(MICROSECONDS);
  137. }
  138. /** @return time since epoch, with up to microsecond resolution. */
  139. public Instant instant() {
  140. long usec = micros();
  141. long secs = usec / 1000000L;
  142. long nanos = (usec % 1000000L) * 1000L;
  143. return Instant.ofEpochSecond(secs, nanos);
  144. }
  145. /** @return time since epoch, with up to microsecond resolution. */
  146. public Timestamp timestamp() {
  147. return Timestamp.from(instant());
  148. }
  149. /** @return time since epoch, with up to millisecond resolution. */
  150. public Date date() {
  151. return new Date(millis());
  152. }
  153. /** Release resources allocated by this timestamp. */
  154. @Override
  155. public void close() {
  156. // Do nothing by default.
  157. }
  158. @Override
  159. public String toString() {
  160. return instant().toString();
  161. }
  162. }