mirror of
https://github.com/google/nomulus.git
synced 2025-05-13 07:57:13 +02:00
Make our Clock util Serializable
It doesn't entirely make semantic sense, since the actual state of the SystemClock isn't being preserved, but it makes injection into serializable classes (e.g. mapreduces) much simpler, so it's worth doing. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=201755949
This commit is contained in:
parent
481790bc91
commit
7d3cb3d426
4 changed files with 18 additions and 19 deletions
|
@ -27,8 +27,6 @@ import google.registry.model.server.Lock;
|
||||||
import google.registry.util.AppEngineTimeLimiter;
|
import google.registry.util.AppEngineTimeLimiter;
|
||||||
import google.registry.util.Clock;
|
import google.registry.util.Clock;
|
||||||
import google.registry.util.RequestStatusChecker;
|
import google.registry.util.RequestStatusChecker;
|
||||||
import google.registry.util.SystemClock;
|
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -42,16 +40,16 @@ import org.joda.time.DateTime;
|
||||||
import org.joda.time.Duration;
|
import org.joda.time.Duration;
|
||||||
|
|
||||||
/** Implementation of {@link LockHandler} that uses the datastore lock. */
|
/** Implementation of {@link LockHandler} that uses the datastore lock. */
|
||||||
public class LockHandlerImpl implements LockHandler, Serializable {
|
public class LockHandlerImpl implements LockHandler {
|
||||||
|
|
||||||
private static final long serialVersionUID = 5162259753801400985L;
|
private static final long serialVersionUID = 5746905970040002524L;
|
||||||
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
|
||||||
|
|
||||||
/** Fudge factor to make sure we kill threads before a lock actually expires. */
|
/** Fudge factor to make sure we kill threads before a lock actually expires. */
|
||||||
private static final Duration LOCK_TIMEOUT_FUDGE = Duration.standardSeconds(5);
|
private static final Duration LOCK_TIMEOUT_FUDGE = Duration.standardSeconds(5);
|
||||||
|
|
||||||
private final RequestStatusChecker requestStatusChecker;
|
private final RequestStatusChecker requestStatusChecker;
|
||||||
@Nullable private transient Clock clock;
|
private final Clock clock;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public LockHandlerImpl(RequestStatusChecker requestStatusChecker, Clock clock) {
|
public LockHandlerImpl(RequestStatusChecker requestStatusChecker, Clock clock) {
|
||||||
|
@ -59,14 +57,6 @@ public class LockHandlerImpl implements LockHandler, Serializable {
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
|
|
||||||
private synchronized Clock getClock() {
|
|
||||||
// Re-set the clock on first use after de-serialization
|
|
||||||
if (clock == null) {
|
|
||||||
clock = new SystemClock();
|
|
||||||
}
|
|
||||||
return clock;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Acquire one or more locks and execute a Void {@link Callable}.
|
* Acquire one or more locks and execute a Void {@link Callable}.
|
||||||
*
|
*
|
||||||
|
@ -83,7 +73,7 @@ public class LockHandlerImpl implements LockHandler, Serializable {
|
||||||
@Nullable String tld,
|
@Nullable String tld,
|
||||||
Duration leaseLength,
|
Duration leaseLength,
|
||||||
String... lockNames) {
|
String... lockNames) {
|
||||||
DateTime startTime = getClock().nowUtc();
|
DateTime startTime = clock.nowUtc();
|
||||||
String sanitizedTld = Strings.emptyToNull(tld);
|
String sanitizedTld = Strings.emptyToNull(tld);
|
||||||
try {
|
try {
|
||||||
return AppEngineTimeLimiter.create()
|
return AppEngineTimeLimiter.create()
|
||||||
|
@ -100,7 +90,7 @@ public class LockHandlerImpl implements LockHandler, Serializable {
|
||||||
"Execution on locks '%s' for TLD '%s' timed out after %s; started at %s",
|
"Execution on locks '%s' for TLD '%s' timed out after %s; started at %s",
|
||||||
Joiner.on(", ").join(lockNames),
|
Joiner.on(", ").join(lockNames),
|
||||||
Optional.ofNullable(sanitizedTld).orElse("(null)"),
|
Optional.ofNullable(sanitizedTld).orElse("(null)"),
|
||||||
new Duration(startTime, getClock().nowUtc()),
|
new Duration(startTime, clock.nowUtc()),
|
||||||
startTime),
|
startTime),
|
||||||
cause);
|
cause);
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,12 +14,20 @@
|
||||||
|
|
||||||
package google.registry.util;
|
package google.registry.util;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
|
||||||
/** A clock that tells the current time in milliseconds or nanoseconds. */
|
/**
|
||||||
|
* A clock that tells the current time in milliseconds or nanoseconds.
|
||||||
|
*
|
||||||
|
* <p>Clocks are technically serializable because they are either a stateless wrapper around the
|
||||||
|
* system clock, or for testing, are just a wrapper around a DateTime. This means that if you
|
||||||
|
* serialize a clock and deserialize it elsewhere, you won't necessarily get the same time or time
|
||||||
|
* zone -- what you will get is a functioning clock.
|
||||||
|
*/
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public interface Clock {
|
public interface Clock extends Serializable {
|
||||||
|
|
||||||
/** Returns current time in UTC timezone. */
|
/** Returns current time in UTC timezone. */
|
||||||
DateTime nowUtc();
|
DateTime nowUtc();
|
||||||
|
|
|
@ -25,6 +25,8 @@ import org.joda.time.DateTime;
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public class SystemClock implements Clock {
|
public class SystemClock implements Clock {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 5165372013848947515L;
|
||||||
|
|
||||||
/** Returns the current time. */
|
/** Returns the current time. */
|
||||||
@Override
|
@Override
|
||||||
public DateTime nowUtc() {
|
public DateTime nowUtc() {
|
||||||
|
|
|
@ -19,7 +19,6 @@ import static org.joda.time.DateTimeZone.UTC;
|
||||||
import static org.joda.time.Duration.millis;
|
import static org.joda.time.Duration.millis;
|
||||||
|
|
||||||
import google.registry.util.Clock;
|
import google.registry.util.Clock;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import javax.annotation.concurrent.ThreadSafe;
|
import javax.annotation.concurrent.ThreadSafe;
|
||||||
import org.joda.time.DateTime;
|
import org.joda.time.DateTime;
|
||||||
|
@ -28,7 +27,7 @@ import org.joda.time.ReadableInstant;
|
||||||
|
|
||||||
/** A mock clock for testing purposes that supports telling, setting, and advancing the time. */
|
/** A mock clock for testing purposes that supports telling, setting, and advancing the time. */
|
||||||
@ThreadSafe
|
@ThreadSafe
|
||||||
public final class FakeClock implements Clock, Serializable {
|
public final class FakeClock implements Clock {
|
||||||
|
|
||||||
private static final long serialVersionUID = 675054721685304599L;
|
private static final long serialVersionUID = 675054721685304599L;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue