You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by sz...@apache.org on 2017/03/14 19:24:51 UTC
[2/2] incubator-ratis git commit: RATIS-39 Avoid using google guava.
RATIS-39 Avoid using google guava.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/f1716ac4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/f1716ac4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/f1716ac4
Branch: refs/heads/master
Commit: f1716ac43e9dcb49ce3be8628cfb0e849bedc8ce
Parents: 6a755e6
Author: Tsz-Wo Nicholas Sze <sz...@hortonworks.com>
Authored: Tue Mar 14 12:24:13 2017 -0700
Committer: Tsz-Wo Nicholas Sze <sz...@hortonworks.com>
Committed: Tue Mar 14 12:24:13 2017 -0700
----------------------------------------------------------------------
ratis-client/pom.xml | 6 ---
.../java/org/apache/ratis/util/RaftUtils.java | 50 ++++++++++++++++++++
ratis-examples/pom.xml | 5 --
.../arithmetic/ArithmeticStateMachine.java | 5 +-
.../arithmetic/expression/BinaryExpression.java | 6 +--
.../arithmetic/expression/DoubleValue.java | 6 +--
.../arithmetic/expression/Expression.java | 9 ++--
.../arithmetic/expression/NullValue.java | 4 +-
.../arithmetic/expression/UnaryExpression.java | 6 +--
.../arithmetic/expression/Variable.java | 7 ++-
ratis-grpc/pom.xml | 5 --
.../org/apache/ratis/grpc/RaftGRpcService.java | 4 +-
.../ratis/grpc/client/AppendStreamer.java | 11 ++---
.../grpc/client/RaftClientProtocolService.java | 9 ++--
.../ratis/grpc/server/GRpcLogAppender.java | 20 ++++----
ratis-hadoop/pom.xml | 5 --
.../hadoop/ipc/ProtobufRpcEngineShaded.java | 11 -----
ratis-netty/pom.xml | 5 --
.../org/apache/ratis/netty/NettyClient.java | 8 ++--
.../ratis/netty/server/NettyRpcService.java | 6 +--
ratis-server/pom.xml | 5 --
.../ratis/server/impl/ConfigurationManager.java | 6 +--
.../ratis/server/impl/LeaderElection.java | 26 ++++------
.../apache/ratis/server/impl/LeaderState.java | 11 ++---
.../apache/ratis/server/impl/LogAppender.java | 7 ++-
.../ratis/server/impl/PeerConfiguration.java | 9 ++--
.../ratis/server/impl/PendingRequest.java | 7 ++-
.../ratis/server/impl/PendingRequests.java | 13 +++--
.../ratis/server/impl/RaftConfiguration.java | 42 ++++++++--------
.../ratis/server/impl/RaftServerImpl.java | 19 ++++----
.../apache/ratis/server/impl/ServerState.java | 8 +---
.../ratis/server/impl/StateMachineUpdater.java | 9 ++--
.../ratis/server/storage/LogInputStream.java | 17 +++----
.../apache/ratis/server/storage/LogReader.java | 4 +-
.../apache/ratis/server/storage/LogSegment.java | 39 ++++++++-------
.../ratis/server/storage/MemoryRaftLog.java | 5 +-
.../apache/ratis/server/storage/MetaFile.java | 7 ++-
.../apache/ratis/server/storage/RaftLog.java | 5 +-
.../ratis/server/storage/RaftLogCache.java | 17 +++----
.../ratis/server/storage/RaftLogWorker.java | 22 ++++-----
.../ratis/server/storage/RaftStorage.java | 4 +-
.../server/storage/RaftStorageDirectory.java | 13 ++---
.../ratis/server/storage/SegmentedRaftLog.java | 8 ++--
.../ratis/server/storage/SnapshotManager.java | 4 +-
.../statemachine/SimpleStateMachineStorage.java | 4 --
.../java/org/apache/ratis/MiniRaftCluster.java | 5 +-
.../java/org/apache/ratis/RaftTestUtil.java | 5 +-
.../server/simulation/RaftServerReply.java | 8 ++--
.../simulation/SimulatedRequestReply.java | 11 ++---
.../server/simulation/SimulatedServerRpc.java | 7 +--
.../server/storage/TestRaftLogSegment.java | 12 ++---
.../SimpleStateMachine4Testing.java | 19 +++-----
.../ratis/statemachine/TermIndexTracker.java | 11 +++--
53 files changed, 257 insertions(+), 320 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-client/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-client/pom.xml b/ratis-client/pom.xml
index 7a070a9..b4913b8 100644
--- a/ratis-client/pom.xml
+++ b/ratis-client/pom.xml
@@ -44,11 +44,5 @@
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
-
- <dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- <scope>compile</scope>
- </dependency>
</dependencies>
</project>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-common/src/main/java/org/apache/ratis/util/RaftUtils.java
----------------------------------------------------------------------
diff --git a/ratis-common/src/main/java/org/apache/ratis/util/RaftUtils.java b/ratis-common/src/main/java/org/apache/ratis/util/RaftUtils.java
index 02b227c..64c7a15 100644
--- a/ratis-common/src/main/java/org/apache/ratis/util/RaftUtils.java
+++ b/ratis-common/src/main/java/org/apache/ratis/util/RaftUtils.java
@@ -330,4 +330,54 @@ public abstract class RaftUtils {
}
};
}
+
+ /**
+ * Assert if the given value is true.
+ * @param value the value to be asserted.
+ * @throws IllegalStateException if the given value is false.
+ */
+ public static void assertTrue(boolean value) {
+ if (!value) {
+ throw new IllegalStateException();
+ }
+ }
+
+ /**
+ * Assert if the given value is true.
+ * @param value the value to be asserted.
+ * @param message The exception message.
+ * @throws IllegalStateException with the given message if the given value is false.
+ */
+ public static void assertTrue(boolean value, Object message) {
+ if (!value) {
+ throw new IllegalStateException(String.valueOf(message));
+ }
+ }
+
+ /**
+ * Assert if the given value is true.
+ * @param value the value to be asserted.
+ * @param format exception message format.
+ * @param args exception message arguments.
+ * @throws IllegalStateException if the given value is false.
+ * The exception message is constructed by {@link String#format(String, Object...)}
+ * with the given format and arguments.
+ */
+ public static void assertTrue(boolean value, String format, Object... args) {
+ if (!value) {
+ throw new IllegalStateException(String.format(format, args));
+ }
+ }
+
+ /**
+ * Assert if the given value is true.
+ * @param value the value to be asserted.
+ * @param message The exception message supplier.
+ * @throws IllegalStateException with the given message if the given value is false.
+ */
+ public static void assertTrue(boolean value, Supplier<Object> message) {
+ if (!value) {
+ throw new IllegalStateException(String.valueOf(message.get()));
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-examples/pom.xml b/ratis-examples/pom.xml
index c5c3805..41ab6ce 100644
--- a/ratis-examples/pom.xml
+++ b/ratis-examples/pom.xml
@@ -112,11 +112,6 @@
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
index e400817..89580df 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/ArithmeticStateMachine.java
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.examples.arithmetic;
-import com.google.common.base.Preconditions;
-
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.examples.arithmetic.expression.Expression;
import org.apache.ratis.protocol.Message;
@@ -31,6 +29,7 @@ import org.apache.ratis.server.storage.RaftStorage;
import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
import org.apache.ratis.statemachine.*;
import org.apache.ratis.util.AutoCloseableLock;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -176,7 +175,7 @@ public class ArithmeticStateMachine extends BaseStateMachine {
final TermIndex newTI = TermIndex.newTermIndex(term, index);
final TermIndex oldTI = latestTermIndex.getAndSet(newTI);
if (oldTI != null) {
- Preconditions.checkArgument(newTI.compareTo(oldTI) >= 0);
+ RaftUtils.assertTrue(newTI.compareTo(oldTI) >= 0);
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
index fceaef9..6486cfd 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/BinaryExpression.java
@@ -17,7 +17,7 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
import java.util.Map;
@@ -43,7 +43,7 @@ public class BinaryExpression implements Expression {
static final Op[] VALUES = Op.values();
static Op valueOf(byte b) {
- Preconditions.checkArgument(b < VALUES.length);
+ RaftUtils.assertTrue(b < VALUES.length);
return VALUES[b];
}
}
@@ -52,7 +52,7 @@ public class BinaryExpression implements Expression {
private final Expression left, right;
BinaryExpression(byte[] buf, final int offset) {
- Preconditions.checkArgument(buf[offset] == Type.BINARY.byteValue());
+ RaftUtils.assertTrue(buf[offset] == Type.BINARY.byteValue());
op = Op.valueOf(buf[offset + 1]);
left = Utils.bytes2Expression(buf, offset + 2);
right = Utils.bytes2Expression(buf, offset + 2 + left.length());
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
index 7de3847..1753186 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/DoubleValue.java
@@ -17,7 +17,7 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
import java.util.Map;
@@ -33,12 +33,12 @@ public class DoubleValue implements Expression {
DoubleValue(byte[] buf, int offset) {
this(Utils.bytes2double(buf, offset + 1));
- Preconditions.checkArgument(buf[offset] == Type.DOUBLE.byteValue());
+ RaftUtils.assertTrue(buf[offset] == Type.DOUBLE.byteValue());
}
@Override
public int toBytes(byte[] buf, int offset) {
- Preconditions.checkArgument(offset + length() <= buf.length);
+ RaftUtils.assertTrue(offset + length() <= buf.length);
buf[offset++] = Type.DOUBLE.byteValue();
Utils.double2bytes(value, buf, offset);
return length();
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Expression.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Expression.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Expression.java
index c90814a..2d5a490 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Expression.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Expression.java
@@ -17,12 +17,11 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
-
import static org.apache.ratis.util.ProtoUtils.toByteString;
import org.apache.ratis.examples.arithmetic.Evaluable;
import org.apache.ratis.protocol.Message;
+import org.apache.ratis.util.RaftUtils;
public interface Expression extends Evaluable {
enum Type {
@@ -35,8 +34,8 @@ public interface Expression extends Evaluable {
private static final Type[] VALUES = Type.values();
static Type valueOf(byte b) {
- Preconditions.checkArgument(b >= 0);
- Preconditions.checkArgument(b < VALUES.length);
+ RaftUtils.assertTrue(b >= 0);
+ RaftUtils.assertTrue(b < VALUES.length);
return VALUES[b];
}
}
@@ -50,7 +49,7 @@ public interface Expression extends Evaluable {
return () -> {
final byte[] buf = new byte[e.length()];
final int length = e.toBytes(buf, 0);
- Preconditions.checkState(length == buf.length);
+ RaftUtils.assertTrue(length == buf.length);
return toByteString(buf);
};
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/NullValue.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/NullValue.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/NullValue.java
index 7f087d0..9167839 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/NullValue.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/NullValue.java
@@ -17,7 +17,7 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
import java.util.Map;
@@ -33,7 +33,7 @@ public class NullValue implements Expression {
@Override
public int toBytes(byte[] buf, int offset) {
- Preconditions.checkArgument(offset + length() <= buf.length);
+ RaftUtils.assertTrue(offset + length() <= buf.length);
buf[offset++] = Type.NULL.byteValue();
return length();
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
index c340a1e..2311064 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/UnaryExpression.java
@@ -17,7 +17,7 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
import java.util.Map;
@@ -43,7 +43,7 @@ public class UnaryExpression implements Expression {
static final Op[] VALUES = Op.values();
static Op valueOf(byte b) {
- Preconditions.checkArgument(b < VALUES.length);
+ RaftUtils.assertTrue(b < VALUES.length);
return VALUES[b];
}
}
@@ -52,7 +52,7 @@ public class UnaryExpression implements Expression {
final Expression expression;
UnaryExpression(byte[] buf, int offset) {
- Preconditions.checkArgument(buf[offset] == Type.UNARY.byteValue());
+ RaftUtils.assertTrue(buf[offset] == Type.UNARY.byteValue());
op = Op.valueOf(buf[offset + 1]);
expression = Utils.bytes2Expression(buf, offset + 2);
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
----------------------------------------------------------------------
diff --git a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
index 4e9a834..4c5608b 100644
--- a/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
+++ b/ratis-examples/src/main/java/org/apache/ratis/examples/arithmetic/expression/Variable.java
@@ -17,12 +17,11 @@
*/
package org.apache.ratis.examples.arithmetic.expression;
-import com.google.common.base.Preconditions;
-
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.ratis.examples.arithmetic.AssignmentMessage;
+import org.apache.ratis.util.RaftUtils;
public class Variable implements Expression {
static final int LENGTH_LIMIT = 32;
@@ -39,7 +38,7 @@ public class Variable implements Expression {
}
static String extractString(byte[] buf, int offset) {
- Preconditions.checkArgument(buf[offset] == Type.VARIABLE.byteValue());
+ RaftUtils.assertTrue(buf[offset] == Type.VARIABLE.byteValue());
final int length = buf[offset + 1];
final byte[] stringBytes = new byte[length];
System.arraycopy(buf, offset + 2, stringBytes, 0, length);
@@ -47,7 +46,7 @@ public class Variable implements Expression {
}
static byte[] copyBytes(byte[] buf, int offset) {
- Preconditions.checkArgument(buf[offset] == Type.VARIABLE.byteValue());
+ RaftUtils.assertTrue(buf[offset] == Type.VARIABLE.byteValue());
final int length = buf[offset + 1];
final byte[] copy = new byte[length + 2];
System.arraycopy(buf, offset, copy, 0, copy.length);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-grpc/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-grpc/pom.xml b/ratis-grpc/pom.xml
index 99af552..c352cdc 100644
--- a/ratis-grpc/pom.xml
+++ b/ratis-grpc/pom.xml
@@ -74,11 +74,6 @@
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-grpc/src/main/java/org/apache/ratis/grpc/RaftGRpcService.java
----------------------------------------------------------------------
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/RaftGRpcService.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/RaftGRpcService.java
index 3185a37..252dc51 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/RaftGRpcService.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/RaftGRpcService.java
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.grpc;
-import com.google.common.base.Preconditions;
import org.apache.ratis.grpc.client.RaftClientProtocolService;
import org.apache.ratis.grpc.server.RaftServerProtocolClient;
import org.apache.ratis.grpc.server.RaftServerProtocolService;
@@ -40,6 +39,7 @@ import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
+import java.util.Objects;
/** A grpc implementation of {@link RaftServerRpc}. */
public class RaftGRpcService implements RaftServerRpc {
@@ -146,7 +146,7 @@ public class RaftGRpcService implements RaftServerRpc {
CodeInjectionForTesting.execute(GRPC_SEND_SERVER_REQUEST, selfId,
null, request);
- RaftServerProtocolClient target = Preconditions.checkNotNull(
+ RaftServerProtocolClient target = Objects.requireNonNull(
peers.get(new RaftPeerId(request.getServerRequest().getReplyId())));
return target.requestVote(request);
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
----------------------------------------------------------------------
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
index 553ec9a..d42f4e1 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/AppendStreamer.java
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.grpc.client;
-import com.google.common.base.Preconditions;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.grpc.GrpcConfigKeys;
@@ -255,10 +254,10 @@ public class AppendStreamer implements Closeable {
return;
}
synchronized (AppendStreamer.this) {
- RaftClientRequestProto pending = Preconditions.checkNotNull(
+ RaftClientRequestProto pending = Objects.requireNonNull(
ackQueue.peek());
if (reply.getRpcReply().getSuccess()) {
- Preconditions.checkState(pending.getRpcRequest().getCallId() ==
+ RaftUtils.assertTrue(pending.getRpcRequest().getCallId() ==
reply.getRpcReply().getCallId());
ackQueue.poll();
LOG.trace("{} received success ack for request {}", this,
@@ -312,7 +311,7 @@ public class AppendStreamer implements Closeable {
private void handleNotLeader(NotLeaderException nle,
RaftPeerId oldLeader) {
- Preconditions.checkState(Thread.holdsLock(AppendStreamer.this));
+ RaftUtils.assertTrue(Thread.holdsLock(AppendStreamer.this));
// handle NotLeaderException: refresh leader and RaftConfiguration
refreshPeers(nle.getPeers());
@@ -320,7 +319,7 @@ public class AppendStreamer implements Closeable {
}
private void handleError(Throwable t, ResponseHandler handler) {
- Preconditions.checkState(Thread.holdsLock(AppendStreamer.this));
+ RaftUtils.assertTrue(Thread.holdsLock(AppendStreamer.this));
final IOException e = RaftGrpcUtil.unwrapIOException(t);
exceptionAndRetry.addException(handler.targetId, e);
@@ -341,7 +340,7 @@ public class AppendStreamer implements Closeable {
refreshLeaderProxy(suggestedLeader, oldLeader);
reQueuePendingRequests(leaderId);
- final RaftClientRequestProto request = Preconditions.checkNotNull(
+ final RaftClientRequestProto request = Objects.requireNonNull(
dataQueue.poll());
ackQueue.offer(request);
try {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/RaftClientProtocolService.java
----------------------------------------------------------------------
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/RaftClientProtocolService.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/RaftClientProtocolService.java
index d550963..7812cb5 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/RaftClientProtocolService.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/client/RaftClientProtocolService.java
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.grpc.client;
-import com.google.common.base.Preconditions;
-
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.shaded.io.grpc.stub.StreamObserver;
import org.apache.ratis.shaded.proto.RaftProtos.RaftClientReplyProto;
@@ -29,6 +27,7 @@ import org.apache.ratis.client.impl.ClientProtoUtils;
import org.apache.ratis.grpc.RaftGrpcUtil;
import org.apache.ratis.protocol.RaftClientAsynchronousProtocol;
import org.apache.ratis.protocol.RaftClientReply;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -127,14 +126,14 @@ public class RaftClientProtocolService extends RaftClientProtocolServiceImplBase
} else {
final long replySeq = reply.getCallId();
synchronized (pendingList) {
- Preconditions.checkState(!pendingList.isEmpty(),
+ RaftUtils.assertTrue(!pendingList.isEmpty(),
"PendingList is empty when handling onNext for callId %s",
replySeq);
final long headSeqNum = pendingList.get(0).callId;
// we assume the callId is consecutive for a stream RPC call
final PendingAppend pendingForReply = pendingList.get(
(int) (replySeq - headSeqNum));
- Preconditions.checkState(pendingForReply != null &&
+ RaftUtils.assertTrue(pendingForReply != null &&
pendingForReply.callId == replySeq,
"pending for reply is: %s, the pending list: %s",
pendingForReply, pendingList);
@@ -163,7 +162,7 @@ public class RaftClientProtocolService extends RaftClientProtocolServiceImplBase
private void sendReadyReplies(Collection<PendingAppend> readySet) {
readySet.forEach(ready -> {
- Preconditions.checkState(ready.isReady());
+ RaftUtils.assertTrue(ready.isReady());
if (ready == COMPLETED) {
responseObserver.onCompleted();
} else {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GRpcLogAppender.java
----------------------------------------------------------------------
diff --git a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GRpcLogAppender.java b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GRpcLogAppender.java
index 91dc02c..e29da00 100644
--- a/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GRpcLogAppender.java
+++ b/ratis-grpc/src/main/java/org/apache/ratis/grpc/server/GRpcLogAppender.java
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.grpc.server;
-import com.google.common.base.Preconditions;
-
import org.apache.ratis.shaded.io.grpc.Status;
import org.apache.ratis.shaded.io.grpc.stub.StreamObserver;
import org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto;
@@ -33,10 +31,12 @@ import org.apache.ratis.server.impl.LogAppender;
import org.apache.ratis.server.impl.RaftServerImpl;
import org.apache.ratis.statemachine.SnapshotInfo;
import org.apache.ratis.util.CodeInjectionForTesting;
+import org.apache.ratis.util.RaftUtils;
import static org.apache.ratis.grpc.RaftGRpcService.GRPC_SEND_SERVER_REQUEST;
import java.util.LinkedList;
+import java.util.Objects;
import java.util.Queue;
import java.util.UUID;
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -130,7 +130,7 @@ public class GRpcLogAppender extends LogAppender {
// together and protected by the lock
pending = createRequest();
if (pending != null) {
- Preconditions.checkState(pendingRequests.offer(pending));
+ RaftUtils.assertTrue(pendingRequests.offer(pending));
updateNextIndex(pending);
}
}
@@ -256,12 +256,12 @@ public class GRpcLogAppender extends LogAppender {
private void onSuccess(AppendEntriesReplyProto reply) {
AppendEntriesRequestProto request = pendingRequests.poll();
final long replyNextIndex = reply.getNextIndex();
- Preconditions.checkNotNull(request,
- "Got reply with next index %s but the pending queue is empty",
- replyNextIndex);
+ Objects.requireNonNull(request,
+ () -> "Got reply with next index " + replyNextIndex
+ + " but the pending queue is empty");
if (request.getEntriesCount() == 0) {
- Preconditions.checkState(!request.hasPreviousLog() ||
+ RaftUtils.assertTrue(!request.hasPreviousLog() ||
replyNextIndex - 1 == request.getPreviousLog().getIndex(),
"reply's next index is %s, request's previous is %s",
replyNextIndex, request.getPreviousLog());
@@ -269,7 +269,7 @@ public class GRpcLogAppender extends LogAppender {
// check if the reply and the pending request is consistent
final long lastEntryIndex = request
.getEntries(request.getEntriesCount() - 1).getIndex();
- Preconditions.checkState(replyNextIndex == lastEntryIndex + 1,
+ RaftUtils.assertTrue(replyNextIndex == lastEntryIndex + 1,
"reply's next index is %s, request's last entry index is %s",
replyNextIndex, lastEntryIndex);
follower.updateMatchIndex(lastEntryIndex);
@@ -284,7 +284,7 @@ public class GRpcLogAppender extends LogAppender {
private synchronized void onInconsistency(AppendEntriesReplyProto reply) {
AppendEntriesRequestProto request = pendingRequests.peek();
- Preconditions.checkState(request.hasPreviousLog());
+ RaftUtils.assertTrue(request.hasPreviousLog());
if (request.getPreviousLog().getIndex() >= reply.getNextIndex()) {
clearPendingRequests(reply.getNextIndex());
}
@@ -305,7 +305,7 @@ public class GRpcLogAppender extends LogAppender {
synchronized void removePending(InstallSnapshotReplyProto reply) {
int index = pending.poll();
- Preconditions.checkState(index == reply.getRequestIndex());
+ RaftUtils.assertTrue(index == reply.getRequestIndex());
}
boolean isDone() {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-hadoop/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-hadoop/pom.xml b/ratis-hadoop/pom.xml
index 98ea84f..f75498c 100644
--- a/ratis-hadoop/pom.xml
+++ b/ratis-hadoop/pom.xml
@@ -79,11 +79,6 @@
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-hadoop/src/main/java/org/apache/hadoop/ipc/ProtobufRpcEngineShaded.java
----------------------------------------------------------------------
diff --git a/ratis-hadoop/src/main/java/org/apache/hadoop/ipc/ProtobufRpcEngineShaded.java b/ratis-hadoop/src/main/java/org/apache/hadoop/ipc/ProtobufRpcEngineShaded.java
index a5a9654..cf9c917 100644
--- a/ratis-hadoop/src/main/java/org/apache/hadoop/ipc/ProtobufRpcEngineShaded.java
+++ b/ratis-hadoop/src/main/java/org/apache/hadoop/ipc/ProtobufRpcEngineShaded.java
@@ -18,7 +18,6 @@
package org.apache.hadoop.ipc;
-import com.google.common.annotations.VisibleForTesting;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience;
@@ -467,16 +466,6 @@ public class ProtobufRpcEngineShaded implements RpcEngine {
}
}
- @VisibleForTesting
- @InterfaceAudience.Private
- @InterfaceStability.Unstable
- static Client getClient(Configuration conf) {
- return CLIENTS.getClient(conf, SocketFactory.getDefault(),
- RpcResponseWrapper.class);
- }
-
-
-
@Override
public RPC.Server getServer(Class<?> protocol, Object protocolImpl,
String bindAddress, int port, int numHandlers, int numReaders,
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-netty/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-netty/pom.xml b/ratis-netty/pom.xml
index fb6566b..e82550e 100644
--- a/ratis-netty/pom.xml
+++ b/ratis-netty/pom.xml
@@ -75,11 +75,6 @@
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-netty/src/main/java/org/apache/ratis/netty/NettyClient.java
----------------------------------------------------------------------
diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyClient.java b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyClient.java
index 1aea87b..1927951 100644
--- a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyClient.java
+++ b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyClient.java
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.netty;
-import com.google.common.base.Preconditions;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
@@ -30,6 +29,7 @@ import io.netty.handler.logging.LoggingHandler;
import java.io.Closeable;
import java.net.InetSocketAddress;
+import java.util.Objects;
import org.apache.ratis.util.LifeCycle;
import org.apache.ratis.util.NetUtils;
@@ -43,9 +43,9 @@ public class NettyClient implements Closeable {
public void connect(String serverAddress, EventLoopGroup group,
ChannelInitializer<SocketChannel> initializer)
throws InterruptedException {
- final InetSocketAddress address = NetUtils.newInetSocketAddress(serverAddress);
- Preconditions.checkNotNull(address,
- "Failed to create InetSocketAddress from %s.", serverAddress);
+ final InetSocketAddress address = Objects.requireNonNull(
+ NetUtils.newInetSocketAddress(serverAddress),
+ () -> "Failed to create InetSocketAddress from " + serverAddress);
lifeCycle.startAndTransition(
() -> channel = new Bootstrap()
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java
----------------------------------------------------------------------
diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java
index 7487219..dcb70e5 100644
--- a/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java
+++ b/ratis-netty/src/main/java/org/apache/ratis/netty/server/NettyRpcService.java
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.netty.server;
-import com.google.common.base.Preconditions;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
@@ -49,6 +48,7 @@ import org.apache.ratis.util.ProtoUtils;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.ClosedChannelException;
+import java.util.Objects;
/**
* A netty server endpoint that acts as the communication layer.
@@ -208,8 +208,8 @@ public final class NettyRpcService implements RaftServerRpc {
+ proto.getRaftNettyServerRequestCase());
}
} catch (IOException ioe) {
- Preconditions.checkNotNull(rpcRequest);
- return toRaftNettyServerReplyProto(rpcRequest, ioe);
+ return toRaftNettyServerReplyProto(
+ Objects.requireNonNull(rpcRequest, "rpcRequest = null"), ioe);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/pom.xml
----------------------------------------------------------------------
diff --git a/ratis-server/pom.xml b/ratis-server/pom.xml
index 78587d2..9138038 100644
--- a/ratis-server/pom.xml
+++ b/ratis-server/pom.xml
@@ -61,11 +61,6 @@
</dependency>
<dependency>
- <groupId>com.google.guava</groupId>
- <artifactId>guava</artifactId>
- </dependency>
-
- <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
index c000238..85b41d0 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/ConfigurationManager.java
@@ -17,8 +17,7 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
import java.util.*;
@@ -45,7 +44,7 @@ public class ConfigurationManager {
public synchronized void addConfiguration(long logIndex,
RaftConfiguration conf) {
- Preconditions.checkArgument(configurations.isEmpty() ||
+ RaftUtils.assertTrue(configurations.isEmpty() ||
configurations.lastEntry().getKey() < logIndex);
configurations.put(logIndex, conf);
this.currentConf = conf;
@@ -73,7 +72,6 @@ public class ConfigurationManager {
return currentConf;
}
- @VisibleForTesting
synchronized int numOfConf() {
return 1 + configurations.size();
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/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 f2010e4..db899f1 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
@@ -17,18 +17,6 @@
*/
package org.apache.ratis.server.impl;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.List;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorCompletionService;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-import java.util.stream.Collectors;
-
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.protocol.TermIndex;
@@ -37,12 +25,17 @@ import org.apache.ratis.shaded.proto.RaftProtos.RequestVoteRequestProto;
import org.apache.ratis.statemachine.SnapshotInfo;
import org.apache.ratis.util.Daemon;
import org.apache.ratis.util.ProtoUtils;
+import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.Timestamp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.*;
+import java.util.stream.Collectors;
class LeaderElection extends Daemon {
public static final Logger LOG = LoggerFactory.getLogger(LeaderElection.class);
@@ -98,9 +91,8 @@ class LeaderElection extends Daemon {
}
private void initExecutor() {
- Preconditions.checkState(!others.isEmpty());
- executor = Executors.newFixedThreadPool(others.size(),
- new ThreadFactoryBuilder().setDaemon(true).build());
+ RaftUtils.assertTrue(!others.isEmpty());
+ executor = Executors.newFixedThreadPool(others.size(), Daemon::new);
service = new ExecutorCompletionService<>(executor);
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/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 6f246d8..c6b15e6 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
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.base.Preconditions;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.protocol.*;
import org.apache.ratis.server.RaftServerConfigKeys;
@@ -182,7 +181,7 @@ public class LeaderState {
* Start bootstrapping new peers
*/
PendingRequest startSetConfiguration(SetConfigurationRequest request) {
- Preconditions.checkState(running && !inStagingState());
+ RaftUtils.assertTrue(running && !inStagingState());
RaftPeer[] peersInNewConf = request.getPeersInNewConf();
Collection<RaftPeer> peersToBootStrap = RaftConfiguration
@@ -248,7 +247,7 @@ public class LeaderState {
* Update the RpcSender list based on the current configuration
*/
private void updateSenders(RaftConfiguration conf) {
- Preconditions.checkState(conf.isStable() && !inStagingState());
+ RaftUtils.assertTrue(conf.isStable() && !inStagingState());
Iterator<LogAppender> iterator = senders.iterator();
while (iterator.hasNext()) {
LogAppender sender = iterator.next();
@@ -314,7 +313,7 @@ public class LeaderState {
LOG.warn("Failed to persist new votedFor/term.", e);
// the failure should happen while changing the state to follower
// thus the in-memory state should have been updated
- Preconditions.checkState(!running);
+ RaftUtils.assertTrue(!running);
}
}
}
@@ -348,7 +347,7 @@ public class LeaderState {
*/
private BootStrapProgress checkProgress(FollowerInfo follower,
long committed) {
- Preconditions.checkArgument(!follower.isAttendingVote());
+ RaftUtils.assertTrue(!follower.isAttendingVote());
final Timestamp progressTime = new Timestamp().addTimeMs(-server.getMaxTimeoutMs());
final Timestamp timeoutTime = new Timestamp().addTimeMs(-3*server.getMaxTimeoutMs());
if (follower.getLastRpcResponseTime().compareTo(timeoutTime) < 0) {
@@ -364,7 +363,7 @@ public class LeaderState {
}
private Collection<BootStrapProgress> checkAllProgress(long committed) {
- Preconditions.checkState(inStagingState());
+ RaftUtils.assertTrue(inStagingState());
return senders.stream()
.filter(sender -> !sender.getFollower().isAttendingVote())
.map(sender -> checkProgress(sender.getFollower(), committed))
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/LogAppender.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/LogAppender.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/LogAppender.java
index 9dbe314..b5bb4b9 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/LogAppender.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/LogAppender.java
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.server.RaftServerConfigKeys;
@@ -31,6 +29,7 @@ import org.apache.ratis.shaded.proto.RaftProtos.*;
import org.apache.ratis.statemachine.SnapshotInfo;
import org.apache.ratis.util.Daemon;
import org.apache.ratis.util.ProtoUtils;
+import org.apache.ratis.util.RaftUtils;
import org.apache.ratis.util.Timestamp;
import org.slf4j.Logger;
@@ -143,7 +142,7 @@ public class LogAppender extends Daemon {
if (previous == null) {
// if previous is null, nextIndex must be equal to the log start
// index (otherwise we will install snapshot).
- Preconditions.checkState(follower.getNextIndex() == raftLog.getStartIndex(),
+ RaftUtils.assertTrue(follower.getNextIndex() == raftLog.getStartIndex(),
"follower's next index %s, local log start index %s",
follower.getNextIndex(), raftLog.getStartIndex());
SnapshotInfo snapshot = server.getState().getLatestSnapshot();
@@ -281,7 +280,7 @@ public class LogAppender extends Daemon {
InstallSnapshotRequestProto request =
server.createInstallSnapshotRequest(follower.getPeer().getId(),
requestId, requestIndex++, snapshot,
- Lists.newArrayList(chunk), done);
+ Arrays.asList(chunk), done);
currentOffset += targetLength;
chunkIndex++;
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java
index 0b5c8af..704a7d2 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/PeerConfiguration.java
@@ -17,12 +17,11 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.base.Preconditions;
-
import java.util.*;
import org.apache.ratis.protocol.RaftPeer;
import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.util.RaftUtils;
/**
* The peer configuration of a raft cluster.
@@ -33,13 +32,13 @@ class PeerConfiguration {
private final Map<RaftPeerId, RaftPeer> peers;
PeerConfiguration(Iterable<RaftPeer> peers) {
- Preconditions.checkNotNull(peers);
+ Objects.requireNonNull(peers);
Map<RaftPeerId, RaftPeer> map = new HashMap<>();
for(RaftPeer p : peers) {
map.put(p.getId(), p);
}
this.peers = Collections.unmodifiableMap(map);
- Preconditions.checkState(!this.peers.isEmpty());
+ RaftUtils.assertTrue(!this.peers.isEmpty());
}
Collection<RaftPeer> getPeers() {
@@ -74,7 +73,7 @@ class PeerConfiguration {
}
boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) {
- Preconditions.checkArgument(!others.contains(selfId));
+ RaftUtils.assertTrue(!others.contains(selfId));
int num = 0;
if (contains(selfId)) {
num++;
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequest.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequest.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequest.java
index bf47cdc..f5581b9 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequest.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequest.java
@@ -17,13 +17,12 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.base.Preconditions;
-
import org.apache.ratis.protocol.Message;
import org.apache.ratis.protocol.RaftClientReply;
import org.apache.ratis.protocol.RaftClientRequest;
import org.apache.ratis.protocol.SetConfigurationRequest;
import org.apache.ratis.statemachine.TransactionContext;
+import org.apache.ratis.util.RaftUtils;
import java.util.concurrent.CompletableFuture;
@@ -62,12 +61,12 @@ public class PendingRequest implements Comparable<PendingRequest> {
}
synchronized void setException(Throwable e) {
- Preconditions.checkArgument(e != null);
+ RaftUtils.assertTrue(e != null);
future.completeExceptionally(e);
}
synchronized void setReply(RaftClientReply r) {
- Preconditions.checkArgument(r != null);
+ RaftUtils.assertTrue(r != null);
future.complete(r);
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequests.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequests.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequests.java
index 6343344..ff407e4 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequests.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/PendingRequests.java
@@ -17,10 +17,9 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.base.Preconditions;
-
import org.apache.ratis.protocol.*;
import org.apache.ratis.statemachine.TransactionContext;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import java.io.IOException;
@@ -45,8 +44,8 @@ class PendingRequests {
PendingRequest addPendingRequest(long index, RaftClientRequest request,
TransactionContext entry) {
// externally synced for now
- Preconditions.checkArgument(!request.isReadOnly());
- Preconditions.checkState(last == null || index == last.getIndex() + 1);
+ RaftUtils.assertTrue(!request.isReadOnly());
+ RaftUtils.assertTrue(last == null || index == last.getIndex() + 1);
return add(index, request, entry);
}
@@ -59,7 +58,7 @@ class PendingRequests {
}
PendingRequest addConfRequest(SetConfigurationRequest request) {
- Preconditions.checkState(pendingSetConf == null);
+ RaftUtils.assertTrue(pendingSetConf == null);
pendingSetConf = new PendingRequest(request);
return pendingSetConf;
}
@@ -77,7 +76,7 @@ class PendingRequests {
}
void failSetConfiguration(RaftException e) {
- Preconditions.checkState(pendingSetConf != null);
+ RaftUtils.assertTrue(pendingSetConf != null);
pendingSetConf.setException(e);
pendingSetConf = null;
}
@@ -92,7 +91,7 @@ class PendingRequests {
void replyPendingRequest(long index, CompletableFuture<Message> messageFuture) {
final PendingRequest pending = pendingRequests.get(index);
if (pending != null) {
- Preconditions.checkState(pending.getIndex() == index);
+ RaftUtils.assertTrue(pending.getIndex() == index);
messageFuture.whenComplete((reply, exception) -> {
if (exception == null) {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
index 50acbaf..a70abf9 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftConfiguration.java
@@ -17,15 +17,13 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.protocol.RaftPeer;
+import org.apache.ratis.protocol.RaftPeerId;
+import org.apache.ratis.util.RaftUtils;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
-import org.apache.ratis.protocol.RaftPeer;
-import org.apache.ratis.protocol.RaftPeerId;
-
/**
* The configuration of the raft cluster.
*
@@ -52,8 +50,8 @@ public class RaftConfiguration {
private Builder() {}
public Builder setConf(PeerConfiguration conf) {
- Preconditions.checkNotNull(conf);
- Preconditions.checkState(this.conf == null, "conf is already set.");
+ Objects.requireNonNull(conf);
+ RaftUtils.assertTrue(this.conf == null, "conf is already set.");
this.conf = conf;
return this;
}
@@ -67,18 +65,18 @@ public class RaftConfiguration {
}
Builder setConf(RaftConfiguration transitionalConf) {
- Preconditions.checkNotNull(transitionalConf);
- Preconditions.checkState(transitionalConf.isTransitional());
+ Objects.requireNonNull(transitionalConf);
+ RaftUtils.assertTrue(transitionalConf.isTransitional());
- Preconditions.checkState(!forceTransitional);
+ RaftUtils.assertTrue(!forceTransitional);
forceStable = true;
return setConf(transitionalConf.conf);
}
public Builder setOldConf(PeerConfiguration oldConf) {
- Preconditions.checkNotNull(oldConf);
- Preconditions.checkState(this.oldConf == null, "oldConf is already set.");
+ Objects.requireNonNull(oldConf);
+ RaftUtils.assertTrue(this.oldConf == null, "oldConf is already set.");
this.oldConf = oldConf;
return this;
}
@@ -92,18 +90,18 @@ public class RaftConfiguration {
}
Builder setOldConf(RaftConfiguration stableConf) {
- Preconditions.checkNotNull(stableConf);
- Preconditions.checkState(stableConf.isStable());
+ Objects.requireNonNull(stableConf);
+ RaftUtils.assertTrue(stableConf.isStable());
- Preconditions.checkState(!forceStable);
+ RaftUtils.assertTrue(!forceStable);
forceTransitional = true;
return setOldConf(stableConf.conf);
}
public Builder setLogEntryIndex(long logEntryIndex) {
- Preconditions.checkArgument(
+ RaftUtils.assertTrue(
logEntryIndex != RaftServerConstants.INVALID_LOG_INDEX);
- Preconditions.checkState(
+ RaftUtils.assertTrue(
this.logEntryIndex == RaftServerConstants.INVALID_LOG_INDEX,
"logEntryIndex is already set.");
this.logEntryIndex = logEntryIndex;
@@ -113,10 +111,10 @@ public class RaftConfiguration {
/** Build a {@link RaftConfiguration}. */
public RaftConfiguration build() {
if (forceTransitional) {
- Preconditions.checkState(oldConf != null);
+ RaftUtils.assertTrue(oldConf != null);
}
if (forceStable) {
- Preconditions.checkState(oldConf == null);
+ RaftUtils.assertTrue(oldConf == null);
}
return new RaftConfiguration(conf, oldConf, logEntryIndex);
}
@@ -135,8 +133,7 @@ public class RaftConfiguration {
private RaftConfiguration(PeerConfiguration conf, PeerConfiguration oldConf,
long logEntryIndex) {
- Preconditions.checkNotNull(conf);
- this.conf = conf;
+ this.conf = Objects.requireNonNull(conf);
this.oldConf = oldConf;
this.logEntryIndex = logEntryIndex;
}
@@ -207,7 +204,7 @@ public class RaftConfiguration {
/** @return true if the self id together with the others are in the majority. */
boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) {
- Preconditions.checkArgument(!others.contains(selfId));
+ RaftUtils.assertTrue(!others.contains(selfId));
return conf.hasMajority(others, selfId) &&
(oldConf == null || oldConf.hasMajority(others, selfId));
}
@@ -217,7 +214,6 @@ public class RaftConfiguration {
return conf + (oldConf != null ? "old:" + oldConf : "");
}
- @VisibleForTesting
boolean hasNoChange(RaftPeer[] newMembers) {
if (!isStable() || conf.size() != newMembers.length) {
return false;
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/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 e13a0ba..897b2a8 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
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
import org.apache.ratis.RaftConfigKeys;
import org.apache.ratis.conf.Parameters;
import org.apache.ratis.rpc.RpcType;
@@ -95,7 +93,7 @@ public class RaftServerImpl implements RaftServer {
this.lifeCycle = new LifeCycle(id);
minTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMin(properties).toInt(TimeUnit.MILLISECONDS);
maxTimeoutMs = RaftServerConfigKeys.Rpc.timeoutMax(properties).toInt(TimeUnit.MILLISECONDS);
- Preconditions.checkArgument(maxTimeoutMs > minTimeoutMs,
+ RaftUtils.assertTrue(maxTimeoutMs > minTimeoutMs,
"max timeout: %s, min timeout: %s", maxTimeoutMs, minTimeoutMs);
this.properties = properties;
this.stateMachine = stateMachine;
@@ -213,7 +211,6 @@ public class RaftServerImpl implements RaftServer {
});
}
- @VisibleForTesting
public boolean isAlive() {
return !lifeCycle.getCurrentState().isOneOf(CLOSING, CLOSED);
}
@@ -287,7 +284,7 @@ public class RaftServerImpl implements RaftServer {
}
synchronized void changeToLeader() {
- Preconditions.checkState(isCandidate());
+ RaftUtils.assertTrue(isCandidate());
shutdownElectionDaemon();
role = Role.LEADER;
state.becomeLeader();
@@ -306,7 +303,7 @@ public class RaftServerImpl implements RaftServer {
}
synchronized void changeToCandidate() {
- Preconditions.checkState(isFollower());
+ RaftUtils.assertTrue(isFollower());
shutdownHeartbeatMonitor();
role = Role.CANDIDATE;
// start election
@@ -574,23 +571,23 @@ public class RaftServerImpl implements RaftServer {
final long index0 = entries[0].getIndex();
if (previous == null || previous.getTerm() == 0) {
- Preconditions.checkArgument(index0 == 0,
+ RaftUtils.assertTrue(index0 == 0,
"Unexpected Index: previous is null but entries[%s].getIndex()=%s",
0, index0);
} else {
- Preconditions.checkArgument(previous.getIndex() == index0 - 1,
+ RaftUtils.assertTrue(previous.getIndex() == index0 - 1,
"Unexpected Index: previous is %s but entries[%s].getIndex()=%s",
previous, 0, index0);
}
for (int i = 0; i < entries.length; i++) {
final long t = entries[i].getTerm();
- Preconditions.checkArgument(expectedTerm >= t,
+ RaftUtils.assertTrue(expectedTerm >= t,
"Unexpected Term: entries[%s].getTerm()=%s but expectedTerm=%s",
i, t, expectedTerm);
final long indexi = entries[i].getIndex();
- Preconditions.checkArgument(indexi == index0 + i,
+ RaftUtils.assertTrue(indexi == index0 + i,
"Unexpected Index: entries[%s].getIndex()=%s but entries[0].getIndex()=%s",
i, indexi, index0);
}
@@ -743,7 +740,7 @@ public class RaftServerImpl implements RaftServer {
// Check and append the snapshot chunk. We simply put this in lock
// considering a follower peer requiring a snapshot installation does not
// have a lot of requests
- Preconditions.checkState(
+ RaftUtils.assertTrue(
state.getLog().getNextIndex() <= lastIncludedIndex,
"%s log's next id is %s, last included index in snapshot is %s",
getId(), state.getLog().getNextIndex(), lastIncludedIndex);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/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 2e0ea71..5cd0ee9 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
@@ -17,8 +17,6 @@
*/
package org.apache.ratis.server.impl;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.protocol.ClientId;
import org.apache.ratis.protocol.RaftPeerId;
@@ -31,6 +29,7 @@ import org.apache.ratis.statemachine.SnapshotInfo;
import org.apache.ratis.statemachine.StateMachine;
import org.apache.ratis.statemachine.TransactionContext;
import org.apache.ratis.util.ProtoUtils;
+import org.apache.ratis.util.RaftUtils;
import java.io.Closeable;
import java.io.IOException;
@@ -138,7 +137,6 @@ public class ServerState implements Closeable {
return configurationManager.getCurrent();
}
- @VisibleForTesting
public RaftPeerId getSelfId() {
return this.selfId;
}
@@ -216,7 +214,7 @@ public class ServerState implements Closeable {
// leader and term later
return true;
}
- Preconditions.checkArgument(this.leaderId.equals(leaderId),
+ RaftUtils.assertTrue(this.leaderId.equals(leaderId),
"selfId:%s, this.leaderId:%s, received leaderId:%s",
selfId, this.leaderId, leaderId);
return true;
@@ -303,7 +301,6 @@ public class ServerState implements Closeable {
storage.close();
}
- @VisibleForTesting
public RaftStorage getStorage() {
return storage;
}
@@ -326,7 +323,6 @@ public class ServerState implements Closeable {
return latestInstalledSnapshot;
}
- @VisibleForTesting
public long getLastAppliedIndex() {
return stateMachineUpdater.getLastAppliedIndex();
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
index 3341549..50aeae8 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/StateMachineUpdater.java
@@ -35,11 +35,10 @@ import org.apache.ratis.statemachine.TransactionContext;
import org.apache.ratis.util.Daemon;
import org.apache.ratis.util.ExitUtils;
import org.apache.ratis.util.LifeCycle;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
-
/**
* This class tracks the log entries that have been committed in a quorum and
* applies them to the state machine. We let a separate thread do this work
@@ -127,15 +126,15 @@ class StateMachineUpdater implements Runnable {
}
final long committedIndex = raftLog.getLastCommittedIndex();
- Preconditions.checkState(lastAppliedIndex < committedIndex);
+ RaftUtils.assertTrue(lastAppliedIndex < committedIndex);
if (state == State.RELOAD) {
- Preconditions.checkState(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED);
+ RaftUtils.assertTrue(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED);
stateMachine.reinitialize(server.getId(), properties, storage);
SnapshotInfo snapshot = stateMachine.getLatestSnapshot();
- Preconditions.checkState(snapshot != null && snapshot.getIndex() > lastAppliedIndex,
+ RaftUtils.assertTrue(snapshot != null && snapshot.getIndex() > lastAppliedIndex,
"Snapshot: %s, lastAppliedIndex: %s", snapshot, lastAppliedIndex);
lastAppliedIndex = snapshot.getIndex();
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/LogInputStream.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogInputStream.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogInputStream.java
index 60572c6..4a36430 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogInputStream.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogInputStream.java
@@ -29,9 +29,6 @@ import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
-import com.google.common.base.Throwables;
-
public class LogInputStream implements Closeable {
static final Logger LOG = LoggerFactory.getLogger(LogInputStream.class);
@@ -75,9 +72,9 @@ public class LogInputStream implements Closeable {
public LogInputStream(File log, long startIndex, long endIndex,
boolean isOpen) {
if (isOpen) {
- Preconditions.checkArgument(endIndex == INVALID_LOG_INDEX);
+ RaftUtils.assertTrue(endIndex == INVALID_LOG_INDEX);
} else {
- Preconditions.checkArgument(endIndex >= startIndex);
+ RaftUtils.assertTrue(endIndex >= startIndex);
}
this.logFile = log;
@@ -87,12 +84,12 @@ public class LogInputStream implements Closeable {
}
private void init() throws IOException {
- Preconditions.checkState(state == State.UNINIT);
+ RaftUtils.assertTrue(state == State.UNINIT);
try {
reader = new LogReader(logFile);
// read the log header
String header = reader.readLogHeader();
- Preconditions.checkState(SegmentedRaftLog.HEADER_STR.equals(header),
+ RaftUtils.assertTrue(SegmentedRaftLog.HEADER_STR.equals(header),
"Corrupted log header: %s", header);
state = State.OPEN;
} finally {
@@ -122,9 +119,9 @@ public class LogInputStream implements Closeable {
init();
} catch (Throwable e) {
LOG.error("caught exception initializing " + this, e);
- Throwables.propagateIfPossible(e, IOException.class);
+ throw RaftUtils.asIOException(e);
}
- Preconditions.checkState(state != State.UNINIT);
+ RaftUtils.assertTrue(state != State.UNINIT);
return nextEntry();
case OPEN:
entry = reader.readEntry();
@@ -152,7 +149,7 @@ public class LogInputStream implements Closeable {
}
long scanNextEntry() throws IOException {
- Preconditions.checkState(state == State.OPEN);
+ RaftUtils.assertTrue(state == State.OPEN);
return reader.scanEntry();
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/LogReader.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogReader.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogReader.java
index 0e5a168..69ac6ba 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogReader.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogReader.java
@@ -38,8 +38,6 @@ import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
import org.apache.ratis.util.PureJavaCrc32C;
import org.apache.ratis.util.RaftUtils;
-import com.google.common.base.Preconditions;
-
public class LogReader implements Closeable {
/**
* InputStream wrapper that keeps track of the current stream position.
@@ -276,7 +274,7 @@ public class LogReader implements Closeable {
}
private void checkBufferSize(int entryLength) {
- Preconditions.checkArgument(entryLength <= maxOpSize);
+ RaftUtils.assertTrue(entryLength <= maxOpSize);
int length = temp.length;
if (length < entryLength) {
while (length < entryLength) {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java
index c8e5306..89c0ff5 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogSegment.java
@@ -17,20 +17,19 @@
*/
package org.apache.ratis.server.storage;
-import static org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase.CONFIGURATIONENTRY;
-
-import java.io.File;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.ratis.server.impl.ConfigurationManager;
import org.apache.ratis.server.impl.ServerProtoUtils;
import org.apache.ratis.shaded.com.google.protobuf.CodedOutputStream;
import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
import org.apache.ratis.util.FileUtils;
+import org.apache.ratis.util.RaftUtils;
-import com.google.common.base.Preconditions;
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase.CONFIGURATIONENTRY;
/**
* In-memory cache for a log segment file. All the updates will be first written
@@ -86,12 +85,12 @@ class LogSegment implements Comparable<Long> {
}
static LogSegment newOpenSegment(long start) {
- Preconditions.checkArgument(start >= 0);
+ RaftUtils.assertTrue(start >= 0);
return new LogSegment(true, start, start - 1);
}
private static LogSegment newCloseSegment(long start, long end) {
- Preconditions.checkArgument(start >= 0 && end >= start);
+ RaftUtils.assertTrue(start >= 0 && end >= start);
return new LogSegment(false, start, end);
}
@@ -105,7 +104,7 @@ class LogSegment implements Comparable<Long> {
LogEntryProto prev = null;
while ((next = in.nextEntry()) != null) {
if (prev != null) {
- Preconditions.checkState(next.getIndex() == prev.getIndex() + 1,
+ RaftUtils.assertTrue(next.getIndex() == prev.getIndex() + 1,
"gap between entry %s and entry %s", prev, next);
}
segment.append(next);
@@ -124,9 +123,9 @@ class LogSegment implements Comparable<Long> {
FileUtils.truncateFile(file, segment.getTotalSize());
}
- Preconditions.checkState(start == segment.records.get(0).entry.getIndex());
+ RaftUtils.assertTrue(start == segment.records.get(0).entry.getIndex());
if (!isOpen) {
- Preconditions.checkState(segment.getEndIndex() == end);
+ RaftUtils.assertTrue(segment.getEndIndex() == end);
}
return segment;
}
@@ -148,26 +147,26 @@ class LogSegment implements Comparable<Long> {
}
void appendToOpenSegment(LogEntryProto... entries) {
- Preconditions.checkState(isOpen(),
+ RaftUtils.assertTrue(isOpen(),
"The log segment %s is not open for append", this.toString());
append(entries);
}
private void append(LogEntryProto... entries) {
- Preconditions.checkArgument(entries != null && entries.length > 0);
+ RaftUtils.assertTrue(entries != null && entries.length > 0);
final long term = entries[0].getTerm();
if (records.isEmpty()) {
- Preconditions.checkArgument(entries[0].getIndex() == startIndex,
+ RaftUtils.assertTrue(entries[0].getIndex() == startIndex,
"gap between start index %s and first entry to append %s",
startIndex, entries[0].getIndex());
}
for (LogEntryProto entry : entries) {
// all these entries should be of the same term
- Preconditions.checkArgument(entry.getTerm() == term,
+ RaftUtils.assertTrue(entry.getTerm() == term,
"expected term:%s, term of the entry:%s", term, entry.getTerm());
final LogRecord currentLast = getLastRecord();
if (currentLast != null) {
- Preconditions.checkArgument(
+ RaftUtils.assertTrue(
entry.getIndex() == currentLast.entry.getIndex() + 1,
"gap between entries %s and %s", entry.getIndex(),
currentLast.entry.getIndex());
@@ -199,7 +198,7 @@ class LogSegment implements Comparable<Long> {
* Remove records from the given index (inclusive)
*/
void truncate(long fromIndex) {
- Preconditions.checkArgument(fromIndex >= startIndex && fromIndex <= endIndex);
+ RaftUtils.assertTrue(fromIndex >= startIndex && fromIndex <= endIndex);
LogRecord record = records.get((int) (fromIndex - startIndex));
for (long index = endIndex; index >= fromIndex; index--) {
records.remove((int)(index - startIndex));
@@ -210,7 +209,7 @@ class LogSegment implements Comparable<Long> {
}
void close() {
- Preconditions.checkState(isOpen());
+ RaftUtils.assertTrue(isOpen());
isOpen = false;
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/MemoryRaftLog.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/MemoryRaftLog.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/MemoryRaftLog.java
index 2ba37e1..17914cd 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/MemoryRaftLog.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/MemoryRaftLog.java
@@ -27,8 +27,7 @@ import org.apache.ratis.server.impl.ServerProtoUtils;
import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
import org.apache.ratis.util.AutoCloseableLock;
import org.apache.ratis.util.CodeInjectionForTesting;
-
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
/**
* A simple RaftLog implementation in memory. Used only for testing.
@@ -66,7 +65,7 @@ public class MemoryRaftLog extends RaftLog {
void truncate(long index) {
checkLogState();
try(AutoCloseableLock writeLock = writeLock()) {
- Preconditions.checkArgument(index >= 0);
+ RaftUtils.assertTrue(index >= 0);
final int truncateIndex = (int) index;
for (int i = entries.size() - 1; i >= truncateIndex; i--) {
entries.remove(i);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/MetaFile.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/MetaFile.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/MetaFile.java
index 8deb7e3..ff010e1 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/MetaFile.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/MetaFile.java
@@ -17,14 +17,13 @@
*/
package org.apache.ratis.server.storage;
-import com.google.common.base.Charsets;
-
import org.apache.ratis.util.AtomicFileOutputStream;
import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
+import java.nio.charset.StandardCharsets;
import java.util.Properties;
/**
@@ -93,7 +92,7 @@ class MetaFile {
properties.setProperty(VOTEDFOR_KEY, votedFor);
try {
properties.store(
- new BufferedWriter(new OutputStreamWriter(fos, Charsets.UTF_8)), "");
+ new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8)), "");
fos.close();
fos = null;
} finally {
@@ -108,7 +107,7 @@ class MetaFile {
votedFor = EMPTY_VOTEFOR;
if (file.exists()) {
BufferedReader br = new BufferedReader(
- new InputStreamReader(new FileInputStream(file), Charsets.UTF_8));
+ new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
try {
Properties properties = new Properties();
properties.load(br);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java
index 32422d3..243da73 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java
@@ -33,11 +33,10 @@ import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
import org.apache.ratis.statemachine.TransactionContext;
import org.apache.ratis.util.AutoCloseableLock;
import org.apache.ratis.util.ProtoUtils;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
-
/**
* Base class of RaftLog. Currently we provide two types of RaftLog
* implementation:
@@ -71,7 +70,7 @@ public abstract class RaftLog implements Closeable {
}
public void checkLogState() {
- Preconditions.checkState(isOpen,
+ RaftUtils.assertTrue(isOpen,
"The RaftLog has not been opened or has been closed");
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogCache.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogCache.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogCache.java
index 90dd7fd..2b9972c 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogCache.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogCache.java
@@ -30,8 +30,7 @@ import org.apache.ratis.server.storage.LogSegment.LogRecord;
import org.apache.ratis.server.storage.LogSegment.SegmentFileInfo;
import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
+import org.apache.ratis.util.RaftUtils;
/**
* In-memory RaftLog Cache. Currently we provide a simple implementation that
@@ -58,10 +57,10 @@ class RaftLogCache {
private void validateAdding(LogSegment segment) {
final LogSegment lastClosed = getLastClosedSegment();
if (!segment.isOpen()) {
- Preconditions.checkState(lastClosed == null ||
+ RaftUtils.assertTrue(lastClosed == null ||
areConsecutiveSegments(lastClosed, segment));
} else {
- Preconditions.checkState(openSegment == null &&
+ RaftUtils.assertTrue(openSegment == null &&
(lastClosed == null || areConsecutiveSegments(lastClosed, segment)));
}
}
@@ -148,7 +147,6 @@ class RaftLogCache {
}
}
- @VisibleForTesting
long getEndIndex() {
return openSegment != null ? openSegment.getEndIndex() :
(closedSegments.isEmpty() ?
@@ -170,7 +168,7 @@ class RaftLogCache {
void appendEntry(LogEntryProto entry) {
// SegmentedRaftLog does the segment creation/rolling work. Here we just
// simply append the entry into the open segment.
- Preconditions.checkState(openSegment != null);
+ RaftUtils.assertTrue(openSegment != null);
openSegment.appendToOpenSegment(entry);
}
@@ -178,7 +176,7 @@ class RaftLogCache {
* finalize the current open segment, and start a new open segment
*/
void rollOpenSegment(boolean createNewOpen) {
- Preconditions.checkState(openSegment != null
+ RaftUtils.assertTrue(openSegment != null
&& openSegment.numOfEntries() > 0);
final long nextIndex = openSegment.getEndIndex() + 1;
openSegment.close();
@@ -213,7 +211,7 @@ class RaftLogCache {
Collections.singletonList(deleteOpenSegment()));
} else {
openSegment.truncate(index);
- Preconditions.checkState(!openSegment.isOpen());
+ RaftUtils.assertTrue(!openSegment.isOpen());
SegmentFileInfo info = new SegmentFileInfo(openSegment.getStartIndex(),
oldEnd, true, openSegment.getTotalSize(),
openSegment.getEndIndex());
@@ -282,7 +280,7 @@ class RaftLogCache {
// the start index is smaller than the first closed segment's start
// index. We no longer keep the log entry (because of the snapshot) or
// the start index is invalid.
- Preconditions.checkState(segmentIndex == 0);
+ RaftUtils.assertTrue(segmentIndex == 0);
throw new IndexOutOfBoundsException();
}
}
@@ -312,7 +310,6 @@ class RaftLogCache {
}
}
- @VisibleForTesting
int getNumOfSegments() {
return closedSegments.size() + (openSegment == null ? 0 : 1);
}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogWorker.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogWorker.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogWorker.java
index 0d724e8..d0fc3ce 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogWorker.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLogWorker.java
@@ -38,8 +38,6 @@ import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.google.common.base.Preconditions;
-
/**
* This class takes the responsibility of all the raft log related I/O ops for a
* raft peer.
@@ -85,7 +83,7 @@ class RaftLogWorker implements Runnable {
lastWrittenIndex = latestIndex;
flushedIndex = latestIndex;
if (openSegmentFile != null) {
- Preconditions.checkArgument(openSegmentFile.exists());
+ RaftUtils.assertTrue(openSegmentFile.exists());
out = new LogOutputStream(openSegmentFile, true, properties);
}
workerThread.start();
@@ -124,7 +122,7 @@ class RaftLogWorker implements Runnable {
LOG.debug("add task {}", task);
try {
if (!queue.offer(task, 1, TimeUnit.SECONDS)) {
- Preconditions.checkState(isAlive(),
+ RaftUtils.assertTrue(isAlive(),
"the worker thread is not alive");
queue.put(task);
}
@@ -227,8 +225,8 @@ class RaftLogWorker implements Runnable {
@Override
public void execute() throws IOException {
- Preconditions.checkState(out != null);
- Preconditions.checkState(lastWrittenIndex + 1 == entry.getIndex(),
+ RaftUtils.assertTrue(out != null);
+ RaftUtils.assertTrue(lastWrittenIndex + 1 == entry.getIndex(),
"lastWrittenIndex == %s, entry == %s", lastWrittenIndex, entry);
out.write(entry);
lastWrittenIndex = entry.getIndex();
@@ -255,17 +253,17 @@ class RaftLogWorker implements Runnable {
public void execute() throws IOException {
RaftUtils.cleanup(null, out);
out = null;
- Preconditions.checkState(segmentToClose != null);
+ RaftUtils.assertTrue(segmentToClose != null);
File openFile = storage.getStorageDir()
.getOpenLogFile(segmentToClose.getStartIndex());
- Preconditions.checkState(openFile.exists(),
+ RaftUtils.assertTrue(openFile.exists(),
"File %s does not exist.", openFile);
if (segmentToClose.numOfEntries() > 0) {
// finalize the current open segment
File dstFile = storage.getStorageDir().getClosedLogFile(
segmentToClose.getStartIndex(), segmentToClose.getEndIndex());
- Preconditions.checkState(!dstFile.exists());
+ RaftUtils.assertTrue(!dstFile.exists());
NativeIO.renameTo(openFile, dstFile);
} else { // delete the file of the empty segment
@@ -290,9 +288,9 @@ class RaftLogWorker implements Runnable {
@Override
void execute() throws IOException {
File openFile = storage.getStorageDir().getOpenLogFile(newStartIndex);
- Preconditions.checkState(!openFile.exists(), "open file %s exists for %s",
+ RaftUtils.assertTrue(!openFile.exists(), "open file %s exists for %s",
openFile.getAbsolutePath(), RaftLogWorker.this.toString());
- Preconditions.checkState(out == null && pendingFlushNum == 0);
+ RaftUtils.assertTrue(out == null && pendingFlushNum == 0);
out = new LogOutputStream(openFile, false, properties);
}
@@ -325,7 +323,7 @@ class RaftLogWorker implements Runnable {
// rename the file
File dstFile = storage.getStorageDir().getClosedLogFile(
segments.toTruncate.startIndex, segments.toTruncate.newEndIndex);
- Preconditions.checkState(!dstFile.exists());
+ RaftUtils.assertTrue(!dstFile.exists());
NativeIO.renameTo(fileToTruncate, dstFile);
// update lastWrittenIndex
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorage.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorage.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorage.java
index c86357d..a7bc47c 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorage.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorage.java
@@ -17,7 +17,6 @@
*/
package org.apache.ratis.server.storage;
-import com.google.common.base.Preconditions;
import org.apache.ratis.conf.RaftProperties;
import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.server.impl.RaftServerConstants;
@@ -25,6 +24,7 @@ import org.apache.ratis.server.storage.RaftStorageDirectory.StorageState;
import org.apache.ratis.statemachine.SnapshotInfo;
import org.apache.ratis.statemachine.StateMachineStorage;
import org.apache.ratis.util.FileUtils;
+import org.apache.ratis.util.RaftUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,7 +54,7 @@ public class RaftStorage implements Closeable {
storageDir.lock();
format();
state = storageDir.analyzeStorage(false);
- Preconditions.checkState(state == StorageState.NORMAL);
+ RaftUtils.assertTrue(state == StorageState.NORMAL);
} else {
state = analyzeAndRecoverStorage(true); // metaFile is initialized here
if (state != StorageState.NORMAL) {
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/f1716ac4/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java
----------------------------------------------------------------------
diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java
index bfa691d..7f87e65 100644
--- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java
+++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftStorageDirectory.java
@@ -17,11 +17,6 @@
*/
package org.apache.ratis.server.storage;
-import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Charsets;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-
import org.apache.ratis.util.AtomicFileOutputStream;
import org.apache.ratis.util.FileUtils;
import org.slf4j.Logger;
@@ -33,6 +28,7 @@ import java.io.RandomAccessFile;
import java.lang.management.ManagementFactory;
import java.nio.channels.FileLock;
import java.nio.channels.OverlappingFileLockException;
+import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -57,7 +53,7 @@ public class RaftStorageDirectory {
static final Pattern OPEN_SEGMENT_REGEX = Pattern.compile("log_inprogress_(\\d+)(?:\\..*)?");
private static final List<Pattern> LOGSEGMENTS_REGEXES =
- ImmutableList.of(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX);
+ Arrays.asList(CLOSED_SEGMENT_REGEX, OPEN_SEGMENT_REGEX);
enum StorageState {
NON_EXISTENT,
@@ -184,7 +180,6 @@ public class RaftStorageDirectory {
/**
* @return log segment files sorted based on their index.
*/
- @VisibleForTesting
public List<LogPathAndIndex> getLogSegmentFiles() throws IOException {
List<LogPathAndIndex> list = new ArrayList<>();
try (DirectoryStream<Path> stream =
@@ -231,7 +226,7 @@ public class RaftStorageDirectory {
* @return state {@link StorageState} of the storage directory
*/
StorageState analyzeStorage(boolean toLock) throws IOException {
- Preconditions.checkState(root != null, "root directory is null");
+ Objects.requireNonNull(root, "root directory is null");
String rootPath = root.getCanonicalPath();
try { // check that storage exists
@@ -319,7 +314,7 @@ public class RaftStorageDirectory {
LOG.error("Unable to acquire file lock on path " + lockF.toString());
throw new OverlappingFileLockException();
}
- file.write(jvmName.getBytes(Charsets.UTF_8));
+ file.write(jvmName.getBytes(StandardCharsets.UTF_8));
LOG.info("Lock on " + lockF + " acquired by nodename " + jvmName);
} catch (OverlappingFileLockException oe) {
// Cannot read from the locked file on Windows.