Repository: incubator-ratis Updated Branches: refs/heads/master 087652d47 -> 74a3a7ce2
RATIS-450. Make Timestamp and TimeDuration value-based classes. Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/74a3a7ce Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/74a3a7ce Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/74a3a7ce Branch: refs/heads/master Commit: 74a3a7ce2db6b50bf8d1c6d57dee38a44461795f Parents: 087652d Author: Tsz Wo Nicholas Sze <[email protected]> Authored: Thu Dec 6 11:44:35 2018 -0800 Committer: Tsz Wo Nicholas Sze <[email protected]> Committed: Thu Dec 6 11:50:38 2018 -0800 ---------------------------------------------------------------------- .../org/apache/ratis/util/TimeDuration.java | 55 ++++++++++++++++---- .../java/org/apache/ratis/util/Timestamp.java | 28 +++++++--- .../apache/ratis/server/impl/FollowerInfo.java | 6 +-- .../apache/ratis/server/impl/FollowerState.java | 4 +- .../ratis/server/impl/LeaderElection.java | 4 +- .../apache/ratis/server/impl/LeaderState.java | 8 +-- .../ratis/server/impl/RaftServerImpl.java | 6 +-- .../org/apache/ratis/server/impl/RoleInfo.java | 6 +-- .../apache/ratis/server/impl/ServerState.java | 8 +-- .../apache/ratis/server/impl/WatchRequests.java | 2 +- .../java/org/apache/ratis/MiniRaftCluster.java | 4 +- .../java/org/apache/ratis/RaftBasicTests.java | 2 +- .../simulation/SimulatedRequestReply.java | 15 +++--- .../TestRaftServerLeaderElectionTimeout.java | 2 +- .../ratis/TestRaftServerSlownessDetection.java | 2 +- .../org/apache/ratis/util/TestTimeDuration.java | 18 +++---- 16 files changed, 107 insertions(+), 63 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-common/src/main/java/org/apache/ratis/util/TimeDuration.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/main/java/org/apache/ratis/util/TimeDuration.java b/ratis-common/src/main/java/org/apache/ratis/util/TimeDuration.java index 41ba1c6..a5ebd55 100644 --- a/ratis-common/src/main/java/org/apache/ratis/util/TimeDuration.java +++ b/ratis-common/src/main/java/org/apache/ratis/util/TimeDuration.java @@ -26,12 +26,13 @@ import java.util.concurrent.TimeUnit; import java.util.function.LongUnaryOperator; /** - * Time duration is represented together with a {@link TimeUnit}. + * Time duration is represented by a long together with a {@link TimeUnit}. * - * This class is immutable. + * This is a value-based class. */ -public class TimeDuration implements Comparable<TimeDuration> { +public final class TimeDuration implements Comparable<TimeDuration> { + /** Abbreviations of {@link TimeUnit}. */ public enum Abbreviation { NANOSECONDS("ns", "nanos"), MICROSECONDS("us", "μs", "micros"), @@ -56,30 +57,37 @@ public class TimeDuration implements Comparable<TimeDuration> { this.symbols = Collections.unmodifiableList(all); } + /** @return the corresponding {@link TimeUnit}. */ public TimeUnit unit() { return unit; } + /** @return the default abbreviation. */ String getDefault() { return symbols.get(0); } + /** @return the entire abbreviation list for this unit. */ public List<String> getSymbols() { return symbols; } + /** @return the corresponding {@link Abbreviation}. */ public static Abbreviation valueOf(TimeUnit unit) { return valueOf(unit.name()); } } + /** The same as valueOf(timeString, targetUnit).toLong(targetUnit). */ public static long parse(String timeString, TimeUnit targetUnit) { return valueOf(timeString, targetUnit).toLong(targetUnit); } /** * Parse the given time duration string. - * If there is no unit specified, use the default unit. + * If no unit is specified, use the default unit. + * + * @return a {@link TimeDuration} in the target unit. */ public static TimeDuration valueOf(String timeString, TimeUnit defaultUnit) { final String lower = Objects.requireNonNull(timeString, "timeString = null").trim(); @@ -98,6 +106,7 @@ public class TimeDuration implements Comparable<TimeDuration> { return valueOf(Long.parseLong(lower), defaultUnit); } + /** @return a {@link TimeDuration} representing the given duration and unit. */ public static TimeDuration valueOf(long duration, TimeUnit unit) { return new TimeDuration(duration, unit); } @@ -110,10 +119,12 @@ public class TimeDuration implements Comparable<TimeDuration> { this.unit = Objects.requireNonNull(unit, "unit = null"); } + /** @return the duration value. */ public long getDuration() { return duration; } + /** @return the {@link TimeUnit}. */ public TimeUnit getUnit() { return unit; } @@ -136,7 +147,7 @@ public class TimeDuration implements Comparable<TimeDuration> { * @return the value in the target unit. * @throws ArithmeticException if it overflows. */ - public int toInt(TimeUnit targetUnit) { + public int toIntExact(TimeUnit targetUnit) { return Math.toIntExact(toLong(targetUnit)); } @@ -153,13 +164,13 @@ public class TimeDuration implements Comparable<TimeDuration> { } /** Round up to the given nanos to nearest multiple (in nanoseconds) of this {@link TimeDuration}. */ - public long roundUp(long nanos) { + public long roundUpNanos(long nanos) { if (duration <= 0) { throw new ArithmeticException( "Rounding up to a non-positive " + getClass().getSimpleName() + " (=" + this + ")"); } - final long divisor = unit.toNanos(duration); + final long divisor = toLong(TimeUnit.NANOSECONDS); if (nanos == 0 || divisor == 1) { return nanos; } @@ -180,10 +191,12 @@ public class TimeDuration implements Comparable<TimeDuration> { return valueOf(operator.applyAsLong(duration), unit); } + /** @return Is this {@link TimeDuration} negative? */ public boolean isNegative() { return duration < 0; } + /** Performs a {@link TimeUnit#sleep(long)} using this {@link TimeDuration}. */ public void sleep() throws InterruptedException { unit.sleep(duration); } @@ -193,10 +206,14 @@ public class TimeDuration implements Comparable<TimeDuration> { if (this.unit.compareTo(that.unit) > 0) { return that.compareTo(this); } - // this.unit <= that.unit - final long thisDurationInThatUnit = that.unit.convert(this.duration, this.unit); + if (this.unit == that.unit) { + return Long.compare(this.duration, that.duration); + } + // this.unit < that.unit + final long thisDurationInThatUnit = this.toLong(that.unit); if (thisDurationInThatUnit == that.duration) { - final long thatDurationInThisUnit = this.unit.convert(that.duration, that.unit); + // check for overflow + final long thatDurationInThisUnit = that.toLong(this.unit); return Long.compare(this.duration, thatDurationInThisUnit); } else { return Long.compare(thisDurationInThatUnit, that.duration); @@ -204,7 +221,23 @@ public class TimeDuration implements Comparable<TimeDuration> { } @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (!(obj instanceof TimeDuration)) { + return false; + } + final TimeDuration that = (TimeDuration)obj; + return this.compareTo(that) == 0; + } + + @Override + public int hashCode() { + return Long.hashCode(toLong(TimeUnit.NANOSECONDS)); + } + + @Override public String toString() { - return duration + " " + Abbreviation.valueOf(unit).getDefault(); + return duration + Abbreviation.valueOf(unit).getDefault(); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-common/src/main/java/org/apache/ratis/util/Timestamp.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/main/java/org/apache/ratis/util/Timestamp.java b/ratis-common/src/main/java/org/apache/ratis/util/Timestamp.java index c33a864..8ce45ae 100644 --- a/ratis-common/src/main/java/org/apache/ratis/util/Timestamp.java +++ b/ratis-common/src/main/java/org/apache/ratis/util/Timestamp.java @@ -24,9 +24,9 @@ import java.util.concurrent.TimeUnit; * * This class takes care the possibility of numerical overflow. * - * The objects of this class are immutable. + * This is a value-based class. */ -public class Timestamp implements Comparable<Timestamp> { +public final class Timestamp implements Comparable<Timestamp> { private static final long NANOSECONDS_PER_MILLISECOND = 1000000; private static final long START_TIME = System.nanoTime(); @@ -57,11 +57,6 @@ public class Timestamp implements Comparable<Timestamp> { this.nanos = nanos; } - /** Construct a timestamp with the current time. */ - public Timestamp() { - this(System.nanoTime()); - } - /** * @param milliseconds the time period to be added. * @return a new {@link Timestamp} whose value is calculated @@ -103,7 +98,24 @@ public class Timestamp implements Comparable<Timestamp> { } @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } else if (!(obj instanceof Timestamp)) { + return false; + } + final Timestamp that = (Timestamp)obj; + return this.nanos == that.nanos; + } + + @Override + public int hashCode() { + return Long.hashCode(nanos); + } + + @Override public String toString() { - return (nanos - START_TIME)/NANOSECONDS_PER_MILLISECOND + "ms"; + final long ms = (nanos - START_TIME)/NANOSECONDS_PER_MILLISECOND; + return (ms/1000) + "." + (ms%1000) + TimeDuration.Abbreviation.SECONDS.getDefault(); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerInfo.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerInfo.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerInfo.java index 94bd7c4..cad9620 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerInfo.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerInfo.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -114,7 +114,7 @@ public class FollowerInfo { /** Update lastRpcResponseTime to the current time. */ public void updateLastRpcResponseTime() { - lastRpcResponseTime.set(new Timestamp()); + lastRpcResponseTime.set(Timestamp.currentTime()); } public Timestamp getLastRpcResponseTime() { @@ -123,7 +123,7 @@ public class FollowerInfo { /** Update lastRpcSendTime to the current time. */ public void updateLastRpcSendTime() { - lastRpcSendTime.set(new Timestamp()); + lastRpcSendTime.set(Timestamp.currentTime()); } public Timestamp getLastRpcTime() { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java index 903ab5e..4e5fbbf 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/FollowerState.java @@ -51,7 +51,7 @@ class FollowerState extends Daemon { private final RaftServerImpl server; - private volatile Timestamp lastRpcTime = new Timestamp(); + private volatile Timestamp lastRpcTime = Timestamp.currentTime(); private volatile boolean monitorRunning = true; private final AtomicInteger outstandingOp = new AtomicInteger(); @@ -60,7 +60,7 @@ class FollowerState extends Daemon { } void updateLastRpcTime(UpdateType type) { - lastRpcTime = new Timestamp(); + lastRpcTime = Timestamp.currentTime(); final int n = type.update(outstandingOp); if (LOG.isTraceEnabled()) { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java index 5cdc8a9..fd02632 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderElection.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -193,7 +193,7 @@ class LeaderElection extends Daemon { private ResultAndTerm waitForResults(final long electionTerm, final int submitted) throws InterruptedException { - final Timestamp timeout = new Timestamp().addTimeMs(server.getRandomTimeoutMs()); + final Timestamp timeout = Timestamp.currentTime().addTimeMs(server.getRandomTimeoutMs()); final List<RequestVoteReplyProto> responses = new ArrayList<>(); final List<Exception> exceptions = new ArrayList<>(); int waitForNum = submitted; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java index 924a7d0..5e9dbc6 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LeaderState.java @@ -204,7 +204,7 @@ public class LeaderState { final RaftConfiguration conf = server.getRaftConf(); Collection<RaftPeer> others = conf.getOtherPeers(state.getSelfId()); - final Timestamp t = new Timestamp().addTimeMs(-server.getMaxTimeoutMs()); + final Timestamp t = Timestamp.currentTime().addTimeMs(-server.getMaxTimeoutMs()); placeHolderIndex = raftLog.getNextIndex(); senders = new SenderList(others.stream().map( @@ -362,7 +362,7 @@ public class LeaderState { * RpcSender list. */ void addSenders(Collection<RaftPeer> newMembers) { - final Timestamp t = new Timestamp().addTimeMs(-server.getMaxTimeoutMs()); + final Timestamp t = Timestamp.currentTime().addTimeMs(-server.getMaxTimeoutMs()); final long nextIndex = raftLog.getNextIndex(); senders.addAll(newMembers.stream().map(peer -> { @@ -459,8 +459,8 @@ public class LeaderState { private BootStrapProgress checkProgress(FollowerInfo follower, long committed) { Preconditions.assertTrue(!follower.isAttendingVote()); - final Timestamp progressTime = new Timestamp().addTimeMs(-server.getMaxTimeoutMs()); - final Timestamp timeoutTime = new Timestamp().addTimeMs(-3*server.getMaxTimeoutMs()); + final Timestamp progressTime = Timestamp.currentTime().addTimeMs(-server.getMaxTimeoutMs()); + final Timestamp timeoutTime = Timestamp.currentTime().addTimeMs(-3*server.getMaxTimeoutMs()); if (follower.getLastRpcResponseTime().compareTo(timeoutTime) < 0) { LOG.debug("{} detects a follower {} timeout for bootstrapping," + " timeoutTime: {}", server.getId(), follower, timeoutTime); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java index ed74866..7ec0600 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java @@ -93,9 +93,9 @@ public class RaftServerImpl implements RaftServerProtocol, RaftServerAsynchronou this.role = new RoleInfo(id); final RaftProperties properties = proxy.getProperties(); - minTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMin(properties).toInt(TimeUnit.MILLISECONDS); - maxTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMax(properties).toInt(TimeUnit.MILLISECONDS); - rpcSlownessTimeoutMs = RaftServerConfigKeys.Rpc.slownessTimeout(properties).toInt(TimeUnit.MILLISECONDS); + minTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMin(properties).toIntExact(TimeUnit.MILLISECONDS); + maxTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMax(properties).toIntExact(TimeUnit.MILLISECONDS); + rpcSlownessTimeoutMs = RaftServerConfigKeys.Rpc.slownessTimeout(properties).toIntExact(TimeUnit.MILLISECONDS); Preconditions.assertTrue(maxTimeoutMs > minTimeoutMs, "max timeout: %s, min timeout: %s", maxTimeoutMs, minTimeoutMs); this.proxy = proxy; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java index 42635f9..88d525c 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RoleInfo.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -50,12 +50,12 @@ class RoleInfo { RoleInfo(RaftPeerId id) { this.id = id; - this.transitionTime = new AtomicReference<>(new Timestamp()); + this.transitionTime = new AtomicReference<>(Timestamp.currentTime()); } void transitionRole(RaftPeerRole newRole) { this.role = newRole; - this.transitionTime.set(new Timestamp()); + this.transitionTime.set(Timestamp.currentTime()); } long getRoleElapsedTimeMs() { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java index 7dfc331..1fd8a20 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ServerState.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -105,9 +105,9 @@ public class ServerState implements Closeable { // On start the leader is null, start the clock now leaderId = null; - this.lastNoLeaderTime = new Timestamp(); + this.lastNoLeaderTime = Timestamp.currentTime(); this.leaderElectionTimeoutMs = - RaftServerConfigKeys.leaderElectionTimeout(prop).toInt(TimeUnit.MILLISECONDS); + RaftServerConfigKeys.leaderElectionTimeout(prop).toIntExact(TimeUnit.MILLISECONDS); // we cannot apply log entries to the state machine in this step, since we // do not know whether the local log entries have been committed. @@ -248,7 +248,7 @@ public class ServerState implements Closeable { String suffix; if (newLeaderId == null) { // reset the time stamp when a null leader is assigned - lastNoLeaderTime = new Timestamp(); + lastNoLeaderTime = Timestamp.currentTime(); suffix = ""; } else { Timestamp previous = lastNoLeaderTime; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/main/java/org/apache/ratis/server/impl/WatchRequests.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/WatchRequests.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/WatchRequests.java index 912d12d..391b1ea 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/WatchRequests.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/WatchRequests.java @@ -85,7 +85,7 @@ class WatchRequests { PendingWatch add(RaftClientRequest request) { final long currentTime = Timestamp.currentTimeNanos(); - final long roundUp = watchTimeoutDenominationNanos.roundUp(currentTime); + final long roundUp = watchTimeoutDenominationNanos.roundUpNanos(currentTime); final PendingWatch pending = new PendingWatch(request.getType().getWatch(), Timestamp.valueOf(roundUp)); synchronized (this) { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java index ea5730e..f1f33e1 100644 --- a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java +++ b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java @@ -276,7 +276,7 @@ public abstract class MiniRaftCluster implements Closeable { /** @deprecated use {@link #getTimeoutMax()}. */ @Deprecated public int getMaxTimeout() { - return RaftServerConfigKeys.Rpc.timeoutMax(properties).toInt(TimeUnit.MILLISECONDS); + return RaftServerConfigKeys.Rpc.timeoutMax(properties).toIntExact(TimeUnit.MILLISECONDS); } public TimeDuration getTimeoutMax() { @@ -718,7 +718,7 @@ public abstract class MiniRaftCluster implements Closeable { // vote, all non-leader servers can grant the vote. // Disable the target leader server RPC so that it can request a vote. blockQueueAndSetDelay(leaderId, - RaftServerConfigKeys.Rpc.TIMEOUT_MIN_DEFAULT.toInt(TimeUnit.MILLISECONDS)); + RaftServerConfigKeys.Rpc.TIMEOUT_MIN_DEFAULT.toIntExact(TimeUnit.MILLISECONDS)); // Reopen queues so that the vote can make progress. blockQueueAndSetDelay(leaderId, 0); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/test/java/org/apache/ratis/RaftBasicTests.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftBasicTests.java b/ratis-server/src/test/java/org/apache/ratis/RaftBasicTests.java index a21796f..f182132 100644 --- a/ratis-server/src/test/java/org/apache/ratis/RaftBasicTests.java +++ b/ratis-server/src/test/java/org/apache/ratis/RaftBasicTests.java @@ -146,7 +146,7 @@ public abstract class RaftBasicTests<CLUSTER extends MiniRaftCluster> Assert.assertEquals(messages.length, asyncReplyCount.get()); } } - Thread.sleep(cluster.getTimeoutMax().toInt(TimeUnit.MILLISECONDS) + 100); + Thread.sleep(cluster.getTimeoutMax().toIntExact(TimeUnit.MILLISECONDS) + 100); LOG.info(cluster.printAllLogs()); killAndRestartFollower.join(); killAndRestartLeader.join(); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRequestReply.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRequestReply.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRequestReply.java index 528cc1a..7ab58e0 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRequestReply.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/simulation/SimulatedRequestReply.java @@ -1,4 +1,4 @@ -/** +/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information @@ -31,13 +31,12 @@ import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; -class SimulatedRequestReply<REQUEST extends RaftRpcMessage, - REPLY extends RaftRpcMessage> { - public static final String SIMULATE_LATENCY_KEY +class SimulatedRequestReply<REQUEST extends RaftRpcMessage, REPLY extends RaftRpcMessage> { + static final String SIMULATE_LATENCY_KEY = SimulatedRequestReply.class.getName() + ".simulateLatencyMs"; - public static final int SIMULATE_LATENCY_DEFAULT - = RaftServerConfigKeys.Rpc.TIMEOUT_MIN_DEFAULT.toInt(TimeUnit.MILLISECONDS); - public static final long TIMEOUT = 3000L; + static final int SIMULATE_LATENCY_DEFAULT + = RaftServerConfigKeys.Rpc.TIMEOUT_MIN_DEFAULT.toIntExact(TimeUnit.MILLISECONDS); + static final long TIMEOUT = 3000L; private static class ReplyOrException<REPLY> { private final REPLY reply; @@ -68,7 +67,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, REPLY request(REQUEST request) throws InterruptedException, IOException { requestQueue.put(request); synchronized (this) { - final Timestamp startTime = new Timestamp(); + final Timestamp startTime = Timestamp.currentTime(); while (startTime.elapsedTimeMs() < TIMEOUT && !replyMap.containsKey(request)) { this.wait(TIMEOUT); // no need to be precise here http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-test/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java ---------------------------------------------------------------------- diff --git a/ratis-test/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java b/ratis-test/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java index 55bcdfc..afe7f32 100644 --- a/ratis-test/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java +++ b/ratis-test/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java @@ -77,7 +77,7 @@ public class TestRaftServerLeaderElectionTimeout extends BaseTest { public void testLeaderElectionDetection() throws Exception { RaftTestUtil.waitForLeader(cluster); long leaderElectionTimeout = RaftServerConfigKeys. - leaderElectionTimeout(cluster.getProperties()).toInt(TimeUnit.MILLISECONDS); + leaderElectionTimeout(cluster.getProperties()).toIntExact(TimeUnit.MILLISECONDS); RaftServerImpl healthyFollower = cluster.getFollowers().get(1); RaftServerImpl failedFollower = cluster.getFollowers().get(0); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-test/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java ---------------------------------------------------------------------- diff --git a/ratis-test/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java b/ratis-test/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java index 96a164e..eb6d562 100644 --- a/ratis-test/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java +++ b/ratis-test/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java @@ -81,7 +81,7 @@ public class TestRaftServerSlownessDetection extends BaseTest { public void testSlownessDetection() throws Exception { RaftTestUtil.waitForLeader(cluster); long slownessTimeout = RaftServerConfigKeys.Rpc - .slownessTimeout(cluster.getProperties()).toInt(TimeUnit.MILLISECONDS); + .slownessTimeout(cluster.getProperties()).toIntExact(TimeUnit.MILLISECONDS); RaftServerImpl failedFollower = cluster.getFollowers().get(0); // fail the node and wait for the callback to be triggered http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/74a3a7ce/ratis-test/src/test/java/org/apache/ratis/util/TestTimeDuration.java ---------------------------------------------------------------------- diff --git a/ratis-test/src/test/java/org/apache/ratis/util/TestTimeDuration.java b/ratis-test/src/test/java/org/apache/ratis/util/TestTimeDuration.java index feb7b6c..20c4b8f 100644 --- a/ratis-test/src/test/java/org/apache/ratis/util/TestTimeDuration.java +++ b/ratis-test/src/test/java/org/apache/ratis/util/TestTimeDuration.java @@ -89,15 +89,15 @@ public class TestTimeDuration { public void testRoundUp() { final long nanosPerSecond = 1_000_000_000L; final TimeDuration oneSecond = TimeDuration.valueOf(1, TimeUnit.SECONDS); - assertEquals(-nanosPerSecond, oneSecond.roundUp(-nanosPerSecond - 1)); - assertEquals(-nanosPerSecond, oneSecond.roundUp(-nanosPerSecond)); - assertEquals(0, oneSecond.roundUp(-nanosPerSecond + 1)); - assertEquals(0, oneSecond.roundUp(-1)); - assertEquals(0, oneSecond.roundUp(0)); - assertEquals(nanosPerSecond, oneSecond.roundUp(1)); - assertEquals(nanosPerSecond, oneSecond.roundUp(nanosPerSecond - 1)); - assertEquals(nanosPerSecond, oneSecond.roundUp(nanosPerSecond)); - assertEquals(2*nanosPerSecond, oneSecond.roundUp(nanosPerSecond + 1)); + assertEquals(-nanosPerSecond, oneSecond.roundUpNanos(-nanosPerSecond - 1)); + assertEquals(-nanosPerSecond, oneSecond.roundUpNanos(-nanosPerSecond)); + assertEquals(0, oneSecond.roundUpNanos(-nanosPerSecond + 1)); + assertEquals(0, oneSecond.roundUpNanos(-1)); + assertEquals(0, oneSecond.roundUpNanos(0)); + assertEquals(nanosPerSecond, oneSecond.roundUpNanos(1)); + assertEquals(nanosPerSecond, oneSecond.roundUpNanos(nanosPerSecond - 1)); + assertEquals(nanosPerSecond, oneSecond.roundUpNanos(nanosPerSecond)); + assertEquals(2*nanosPerSecond, oneSecond.roundUpNanos(nanosPerSecond + 1)); } @Test(timeout = 1000)
