Browse Source
MonotonicClock can be implemented to provide more certainity about time than the standard System.currentTimeMillis() can provide. This can be used by classes such as PersonIdent and Ketch to rely on more certainity about time moving in a strictly ascending order. Gerrit Code Review can also leverage this interface through its embedding of JGit and use MonotonicClock and ProposedTimestamp to provide stronger assurance that NoteDb time is moving forward. Change-Id: I1a3cbd49a39b150a0d49b36d572da113ca83a786stable-4.6
Shawn Pearce
8 years ago
20 changed files with 780 additions and 41 deletions
@ -0,0 +1,90 @@
|
||||
/* |
||||
* Copyright (C) 2016, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.junit.time; |
||||
|
||||
import java.time.Duration; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
import org.eclipse.jgit.util.time.MonotonicClock; |
||||
import org.eclipse.jgit.util.time.ProposedTimestamp; |
||||
|
||||
/** |
||||
* Fake {@link MonotonicClock} for testing code that uses Clock. |
||||
* |
||||
* @since 4.6 |
||||
*/ |
||||
public class MonotonicFakeClock implements MonotonicClock { |
||||
private long now = TimeUnit.SECONDS.toMicros(42); |
||||
|
||||
/** |
||||
* Advance the time returned by future calls to {@link #propose()}. |
||||
* |
||||
* @param add |
||||
* amount of time to add; must be {@code > 0}. |
||||
* @param unit |
||||
* unit of {@code add}. |
||||
*/ |
||||
public void tick(long add, TimeUnit unit) { |
||||
if (add <= 0) { |
||||
throw new IllegalArgumentException(); |
||||
} |
||||
now += unit.toMillis(add); |
||||
} |
||||
|
||||
@Override |
||||
public ProposedTimestamp propose() { |
||||
long t = now++; |
||||
return new ProposedTimestamp() { |
||||
@Override |
||||
public long read(TimeUnit unit) { |
||||
return unit.convert(t, TimeUnit.MILLISECONDS); |
||||
} |
||||
|
||||
@Override |
||||
public void blockUntil(Duration maxWait) { |
||||
// Nothing to do, since fake time does not go backwards.
|
||||
} |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,60 @@
|
||||
/* |
||||
* Copyright (C) 2016, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.internal.ketch; |
||||
|
||||
import java.io.IOException; |
||||
|
||||
import org.eclipse.jgit.internal.JGitText; |
||||
|
||||
class TimeIsUncertainException extends IOException { |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
TimeIsUncertainException() { |
||||
super(JGitText.get().timeIsUncertain); |
||||
} |
||||
|
||||
TimeIsUncertainException(Exception e) { |
||||
super(JGitText.get().timeIsUncertain, e); |
||||
} |
||||
} |
@ -0,0 +1,96 @@
|
||||
/* |
||||
* Copyright (C) 2016, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.util.time; |
||||
|
||||
import java.time.Duration; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* A provider of time. |
||||
* <p> |
||||
* Clocks should provide wall clock time, obtained from a reasonable clock |
||||
* source, such as the local system clock. |
||||
* <p> |
||||
* MonotonicClocks provide the following behavior, with the assertion always |
||||
* being true if {@link ProposedTimestamp#blockUntil(Duration)} is used: |
||||
* |
||||
* <pre> |
||||
* MonotonicClock clk = ...; |
||||
* long r1; |
||||
* try (ProposedTimestamp t1 = clk.propose()) { |
||||
* r1 = t1.millis(); |
||||
* t1.blockUntil(...); |
||||
* } |
||||
* |
||||
* try (ProposedTimestamp t2 = clk.propose()) { |
||||
* assert t2.millis() > r1; |
||||
* } |
||||
* </pre> |
||||
* |
||||
* @since 4.6 |
||||
*/ |
||||
public interface MonotonicClock { |
||||
/** |
||||
* Obtain a timestamp close to "now". |
||||
* <p> |
||||
* Proposed times are close to "now", but may not yet be certainly in the |
||||
* past. This allows the calling thread to interleave other useful work |
||||
* while waiting for the clock instance to create an assurance it will never |
||||
* in the future propose a time earlier than the returned time. |
||||
* <p> |
||||
* A hypothetical implementation could read the local system clock (managed |
||||
* by NTP) and return that proposal, concurrently sending network messages |
||||
* to closely collaborating peers in the same cluster to also ensure their |
||||
* system clocks are ahead of this time. In such an implementation the |
||||
* {@link ProposedTimestamp#blockUntil(Duration)} method would wait for |
||||
* replies from the peers indicating their own system clocks have moved past |
||||
* the proposed time. |
||||
* |
||||
* @return "now". The value can be immediately accessed by |
||||
* {@link ProposedTimestamp#read(TimeUnit)} and friends, but the |
||||
* caller must use {@link ProposedTimestamp#blockUntil(Duration)} to |
||||
* ensure ordering holds. |
||||
*/ |
||||
ProposedTimestamp propose(); |
||||
} |
@ -0,0 +1,87 @@
|
||||
/* |
||||
* Copyright (C) 2016, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.util.time; |
||||
|
||||
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
||||
|
||||
import java.time.Duration; |
||||
import java.util.concurrent.TimeUnit; |
||||
import java.util.concurrent.atomic.AtomicLong; |
||||
|
||||
/** |
||||
* A {@link MonotonicClock} based on {@code System.currentTimeMillis}. |
||||
* |
||||
* @since 4.6 |
||||
*/ |
||||
public class MonotonicSystemClock implements MonotonicClock { |
||||
private static final AtomicLong before = new AtomicLong(); |
||||
|
||||
private static long nowMicros() { |
||||
long now = MILLISECONDS.toMicros(System.currentTimeMillis()); |
||||
for (;;) { |
||||
long o = before.get(); |
||||
long n = Math.max(o + 1, now); |
||||
if (before.compareAndSet(o, n)) { |
||||
return n; |
||||
} |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public ProposedTimestamp propose() { |
||||
final long u = nowMicros(); |
||||
return new ProposedTimestamp() { |
||||
@Override |
||||
public long read(TimeUnit unit) { |
||||
return unit.convert(u, MICROSECONDS); |
||||
} |
||||
|
||||
@Override |
||||
public void blockUntil(Duration maxWait) { |
||||
// Assume system clock never goes backwards.
|
||||
} |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,176 @@
|
||||
/* |
||||
* Copyright (C) 2016, Google Inc. |
||||
* and other copyright owners as documented in the project's IP log. |
||||
* |
||||
* This program and the accompanying materials are made available |
||||
* under the terms of the Eclipse Distribution License v1.0 which |
||||
* accompanies this distribution, is reproduced below, and is |
||||
* available at http://www.eclipse.org/org/documents/edl-v10.php
|
||||
* |
||||
* All rights reserved. |
||||
* |
||||
* Redistribution and use in source and binary forms, with or |
||||
* without modification, are permitted provided that the following |
||||
* conditions are met: |
||||
* |
||||
* - Redistributions of source code must retain the above copyright |
||||
* notice, this list of conditions and the following disclaimer. |
||||
* |
||||
* - Redistributions in binary form must reproduce the above |
||||
* copyright notice, this list of conditions and the following |
||||
* disclaimer in the documentation and/or other materials provided |
||||
* with the distribution. |
||||
* |
||||
* - Neither the name of the Eclipse Foundation, Inc. nor the |
||||
* names of its contributors may be used to endorse or promote |
||||
* products derived from this software without specific prior |
||||
* written permission. |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
||||
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
||||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
*/ |
||||
|
||||
package org.eclipse.jgit.util.time; |
||||
|
||||
import static java.util.concurrent.TimeUnit.MICROSECONDS; |
||||
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
||||
|
||||
import java.sql.Timestamp; |
||||
import java.time.Duration; |
||||
import java.time.Instant; |
||||
import java.util.Date; |
||||
import java.util.Iterator; |
||||
import java.util.concurrent.TimeUnit; |
||||
import java.util.concurrent.TimeoutException; |
||||
|
||||
/** |
||||
* A timestamp generated by {@link MonotonicClock#propose()}. |
||||
* <p> |
||||
* ProposedTimestamp implements AutoCloseable so that implementations can |
||||
* release resources associated with obtaining certainty about time elapsing. |
||||
* For example the constructing MonotonicClock may start network IO with peers |
||||
* when creating the ProposedTimestamp, and {@link #close()} can ensure those |
||||
* network resources are released in a timely fashion. |
||||
* |
||||
* @since 4.6 |
||||
*/ |
||||
public abstract class ProposedTimestamp implements AutoCloseable { |
||||
/** |
||||
* Wait for several timestamps. |
||||
* |
||||
* @param times |
||||
* timestamps to wait on. |
||||
* @param maxWait |
||||
* how long to wait for the timestamps. |
||||
* @throws InterruptedException |
||||
* current thread was interrupted before the waiting process |
||||
* completed normally. |
||||
* @throws TimeoutException |
||||
* the timeout was reached without the proposed timestamp become |
||||
* certainly in the past. |
||||
*/ |
||||
public static void blockUntil(Iterable<ProposedTimestamp> times, |
||||
Duration maxWait) throws TimeoutException, InterruptedException { |
||||
Iterator<ProposedTimestamp> itr = times.iterator(); |
||||
if (!itr.hasNext()) { |
||||
return; |
||||
} |
||||
|
||||
long now = System.currentTimeMillis(); |
||||
long deadline = now + maxWait.toMillis(); |
||||
for (;;) { |
||||
long w = deadline - now; |
||||
if (w < 0) { |
||||
throw new TimeoutException(); |
||||
} |
||||
itr.next().blockUntil(Duration.ofMillis(w)); |
||||
if (itr.hasNext()) { |
||||
now = System.currentTimeMillis(); |
||||
} else { |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Read the timestamp as {@code unit} since the epoch. |
||||
* <p> |
||||
* The timestamp value for a specific {@code ProposedTimestamp} object never |
||||
* changes, and can be read before {@link #blockUntil(Duration)}. |
||||
* |
||||
* @param unit |
||||
* what unit to return the timestamp in. The timestamp will be |
||||
* rounded if the unit is bigger than the clock's granularity. |
||||
* @return {@code unit} since the epoch. |
||||
*/ |
||||
public abstract long read(TimeUnit unit); |
||||
|
||||
/** |
||||
* Wait for this proposed timestamp to be certainly in the recent past. |
||||
* <p> |
||||
* This method forces the caller to wait up to {@code timeout} for |
||||
* {@code this} to pass sufficiently into the past such that the creating |
||||
* {@link MonotonicClock} instance will not create an earlier timestamp. |
||||
* |
||||
* @param maxWait |
||||
* how long the implementation may block the caller. |
||||
* @throws InterruptedException |
||||
* current thread was interrupted before the waiting process |
||||
* completed normally. |
||||
* @throws TimeoutException |
||||
* the timeout was reached without the proposed timestamp |
||||
* becoming certainly in the past. |
||||
*/ |
||||
public abstract void blockUntil(Duration maxWait) |
||||
throws InterruptedException, TimeoutException; |
||||
|
||||
/** @return milliseconds since epoch; {@code read(MILLISECONDS}). */ |
||||
public long millis() { |
||||
return read(MILLISECONDS); |
||||
} |
||||
|
||||
/** @return microseconds since epoch; {@code read(MICROSECONDS}). */ |
||||
public long micros() { |
||||
return read(MICROSECONDS); |
||||
} |
||||
|
||||
/** @return time since epoch, with up to microsecond resolution. */ |
||||
public Instant instant() { |
||||
long usec = micros(); |
||||
long secs = usec / 1000000L; |
||||
long nanos = (usec % 1000000L) * 1000L; |
||||
return Instant.ofEpochSecond(secs, nanos); |
||||
} |
||||
|
||||
/** @return time since epoch, with up to microsecond resolution. */ |
||||
public Timestamp timestamp() { |
||||
return Timestamp.from(instant()); |
||||
} |
||||
|
||||
/** @return time since epoch, with up to millisecond resolution. */ |
||||
public Date date() { |
||||
return new Date(millis()); |
||||
} |
||||
|
||||
/** Release resources allocated by this timestamp. */ |
||||
@Override |
||||
public void close() { |
||||
// Do nothing by default.
|
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return instant().toString(); |
||||
} |
||||
} |
Loading…
Reference in new issue