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