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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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
  55. * {@link org.eclipse.jgit.util.time.MonotonicClock#propose()}.
  56. * <p>
  57. * ProposedTimestamp implements AutoCloseable so that implementations can
  58. * release resources associated with obtaining certainty about time elapsing.
  59. * For example the constructing MonotonicClock may start network IO with peers
  60. * when creating the ProposedTimestamp, and {@link #close()} can ensure those
  61. * network resources are released in a timely fashion.
  62. *
  63. * @since 4.6
  64. */
  65. public abstract class ProposedTimestamp implements AutoCloseable {
  66. /**
  67. * Wait for several timestamps.
  68. *
  69. * @param times
  70. * timestamps to wait on.
  71. * @param maxWait
  72. * how long to wait for the timestamps.
  73. * @throws java.lang.InterruptedException
  74. * current thread was interrupted before the waiting process
  75. * completed normally.
  76. * @throws java.util.concurrent.TimeoutException
  77. * the timeout was reached without the proposed timestamp become
  78. * certainly in the past.
  79. */
  80. public static void blockUntil(Iterable<ProposedTimestamp> times,
  81. Duration maxWait) throws TimeoutException, InterruptedException {
  82. Iterator<ProposedTimestamp> itr = times.iterator();
  83. if (!itr.hasNext()) {
  84. return;
  85. }
  86. long now = System.currentTimeMillis();
  87. long deadline = now + maxWait.toMillis();
  88. for (;;) {
  89. long w = deadline - now;
  90. if (w < 0) {
  91. throw new TimeoutException();
  92. }
  93. itr.next().blockUntil(Duration.ofMillis(w));
  94. if (itr.hasNext()) {
  95. now = System.currentTimeMillis();
  96. } else {
  97. break;
  98. }
  99. }
  100. }
  101. /**
  102. * Read the timestamp as {@code unit} since the epoch.
  103. * <p>
  104. * The timestamp value for a specific {@code ProposedTimestamp} object never
  105. * changes, and can be read before {@link #blockUntil(Duration)}.
  106. *
  107. * @param unit
  108. * what unit to return the timestamp in. The timestamp will be
  109. * rounded if the unit is bigger than the clock's granularity.
  110. * @return {@code unit} since the epoch.
  111. */
  112. public abstract long read(TimeUnit unit);
  113. /**
  114. * Wait for this proposed timestamp to be certainly in the recent past.
  115. * <p>
  116. * This method forces the caller to wait up to {@code timeout} for
  117. * {@code this} to pass sufficiently into the past such that the creating
  118. * {@link org.eclipse.jgit.util.time.MonotonicClock} instance will not
  119. * create an earlier timestamp.
  120. *
  121. * @param maxWait
  122. * how long the implementation may block the caller.
  123. * @throws java.lang.InterruptedException
  124. * current thread was interrupted before the waiting process
  125. * completed normally.
  126. * @throws java.util.concurrent.TimeoutException
  127. * the timeout was reached without the proposed timestamp
  128. * becoming certainly in the past.
  129. */
  130. public abstract void blockUntil(Duration maxWait)
  131. throws InterruptedException, TimeoutException;
  132. /**
  133. * Get milliseconds since epoch; {@code read(MILLISECONDS}).
  134. *
  135. * @return milliseconds since epoch; {@code read(MILLISECONDS}).
  136. */
  137. public long millis() {
  138. return read(MILLISECONDS);
  139. }
  140. /**
  141. * Get microseconds since epoch; {@code read(MICROSECONDS}).
  142. *
  143. * @return microseconds since epoch; {@code read(MICROSECONDS}).
  144. */
  145. public long micros() {
  146. return read(MICROSECONDS);
  147. }
  148. /**
  149. * Get time since epoch, with up to microsecond resolution.
  150. *
  151. * @return time since epoch, with up to microsecond resolution.
  152. */
  153. public Instant instant() {
  154. long usec = micros();
  155. long secs = usec / 1000000L;
  156. long nanos = (usec % 1000000L) * 1000L;
  157. return Instant.ofEpochSecond(secs, nanos);
  158. }
  159. /**
  160. * Get time since epoch, with up to microsecond resolution.
  161. *
  162. * @return time since epoch, with up to microsecond resolution.
  163. */
  164. public Timestamp timestamp() {
  165. return Timestamp.from(instant());
  166. }
  167. /**
  168. * Get time since epoch, with up to millisecond resolution.
  169. *
  170. * @return time since epoch, with up to millisecond resolution.
  171. */
  172. public Date date() {
  173. return new Date(millis());
  174. }
  175. /**
  176. * {@inheritDoc}
  177. * <p>
  178. * Release resources allocated by this timestamp.
  179. */
  180. @Override
  181. public void close() {
  182. // Do nothing by default.
  183. }
  184. /** {@inheritDoc} */
  185. @Override
  186. public String toString() {
  187. return instant().toString();
  188. }
  189. }