http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftStream.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftStream.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftStream.java index 4bae5a9..16bc221 100644 --- a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftStream.java +++ b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftStream.java @@ -20,6 +20,7 @@ package org.apache.ratis.grpc; import org.apache.log4j.Level; import org.apache.ratis.RaftTestUtil; import org.apache.ratis.conf.RaftProperties; +import org.apache.ratis.util.LogUtils; import org.apache.ratis.util.SizeInBytes; import org.apache.ratis.grpc.client.AppendStreamer; import org.apache.ratis.grpc.client.RaftOutputStream; @@ -27,7 +28,6 @@ import org.apache.ratis.protocol.ClientId; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.server.storage.RaftLog; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; -import org.apache.ratis.util.RaftUtils; import org.junit.After; import org.junit.Assert; import org.junit.Test; @@ -47,7 +47,7 @@ import static org.junit.Assert.fail; public class TestRaftStream { static { - RaftUtils.setLogLevel(AppendStreamer.LOG, Level.ALL); + LogUtils.setLogLevel(AppendStreamer.LOG, Level.ALL); } static final Logger LOG = LoggerFactory.getLogger(TestRaftStream.class);
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java index bfaf819..d7b72c2 100644 --- a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java +++ b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftWithGrpc.java @@ -21,7 +21,7 @@ import org.apache.log4j.Level; import org.apache.ratis.RaftBasicTests; import org.apache.ratis.server.impl.BlockRequestHandlingInjection; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.Assert; import org.junit.Test; @@ -29,7 +29,7 @@ import java.io.IOException; public class TestRaftWithGrpc extends RaftBasicTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); } private final MiniRaftClusterWithGRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRetryCacheWithGrpc.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRetryCacheWithGrpc.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRetryCacheWithGrpc.java index d709c1c..cc0e2cc 100644 --- a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRetryCacheWithGrpc.java +++ b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRetryCacheWithGrpc.java @@ -17,17 +17,17 @@ */ package org.apache.ratis.grpc; +import java.io.IOException; + import org.apache.log4j.Level; import org.apache.ratis.RaftRetryCacheTests; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.Assert; -import java.io.IOException; - public class TestRetryCacheWithGrpc extends RaftRetryCacheTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); } private final MiniRaftClusterWithGRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftReconfigurationWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftReconfigurationWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftReconfigurationWithHadoopRpc.java index 7a36fa1..df432fc 100644 --- a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftReconfigurationWithHadoopRpc.java +++ b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftReconfigurationWithHadoopRpc.java @@ -22,9 +22,7 @@ import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.ipc.Client; import org.apache.log4j.Level; import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftReconfigurationBaseTest; -import org.apache.ratis.util.RaftUtils; import java.io.IOException; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftWithHadoopRpc.java index 5c03129..124e7ee 100644 --- a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftWithHadoopRpc.java +++ b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftWithHadoopRpc.java @@ -22,7 +22,7 @@ import org.apache.ratis.RaftBasicTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.BlockRequestHandlingInjection; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.Test; import java.io.IOException; @@ -31,9 +31,9 @@ import static org.apache.ratis.hadooprpc.MiniRaftClusterWithHadoopRpc.sendServer public class TestRaftWithHadoopRpc extends RaftBasicTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - RaftUtils.setLogLevel(MiniRaftClusterWithHadoopRpc.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(MiniRaftClusterWithHadoopRpc.LOG, Level.DEBUG); } private final MiniRaftClusterWithHadoopRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRetryCacheWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRetryCacheWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRetryCacheWithHadoopRpc.java index fade34b..6e5ba8c 100644 --- a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRetryCacheWithHadoopRpc.java +++ b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRetryCacheWithHadoopRpc.java @@ -21,15 +21,15 @@ import org.apache.log4j.Level; import org.apache.ratis.RaftRetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import java.io.IOException; public class TestRetryCacheWithHadoopRpc extends RaftRetryCacheTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - RaftUtils.setLogLevel(MiniRaftClusterWithHadoopRpc.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(MiniRaftClusterWithHadoopRpc.LOG, Level.DEBUG); } private final MiniRaftClusterWithHadoopRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-netty/src/main/java/org/apache/ratis/netty/NettyRpcProxy.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyRpcProxy.java b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyRpcProxy.java index 8b64784..5b3492b 100644 --- a/ratis-netty/src/main/java/org/apache/ratis/netty/NettyRpcProxy.java +++ b/ratis-netty/src/main/java/org/apache/ratis/netty/NettyRpcProxy.java @@ -28,9 +28,9 @@ import org.apache.ratis.shaded.io.netty.handler.codec.protobuf.ProtobufVarint32L import org.apache.ratis.shaded.proto.RaftProtos.RaftRpcRequestProto; import org.apache.ratis.shaded.proto.netty.NettyProtos.RaftNettyServerReplyProto; import org.apache.ratis.shaded.proto.netty.NettyProtos.RaftNettyServerRequestProto; +import org.apache.ratis.util.IOUtils; import org.apache.ratis.util.PeerProxyMap; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import java.io.Closeable; import java.io.IOException; @@ -51,7 +51,7 @@ public class NettyRpcProxy implements Closeable { try { return new NettyRpcProxy(peer, group); } catch (InterruptedException e) { - throw RaftUtils.toInterruptedIOException("Failed connecting to " + peer, e); + throw IOUtils.toInterruptedIOException("Failed connecting to " + peer, e); } } @@ -170,10 +170,10 @@ public class NettyRpcProxy implements Closeable { channelFuture.sync(); return reply.get(); } catch (InterruptedException e) { - throw RaftUtils.toInterruptedIOException(ProtoUtils.toString(request) + throw IOUtils.toInterruptedIOException(ProtoUtils.toString(request) + " sending from " + peer + " is interrupted.", e); } catch (ExecutionException e) { - throw RaftUtils.toIOException(e); + throw IOUtils.toIOException(e); } } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java index a48e608..afb7c2b 100644 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java +++ b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java @@ -22,15 +22,15 @@ import org.apache.ratis.RaftBasicTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.BlockRequestHandlingInjection; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.Test; import java.io.IOException; public class TestRaftWithNetty extends RaftBasicTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } private final MiniRaftClusterWithNetty cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java index 540cc16..cb4c31b 100644 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java +++ b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java @@ -17,18 +17,18 @@ */ package org.apache.ratis.netty; +import java.io.IOException; + import org.apache.log4j.Level; import org.apache.ratis.RaftRetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; - -import java.io.IOException; +import org.apache.ratis.util.LogUtils; public class TestRetryCacheWithNettyRpc extends RaftRetryCacheTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } private final MiniRaftClusterWithNetty cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 85b41d0..6aed1d7 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,7 +17,7 @@ */ package org.apache.ratis.server.impl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import java.util.*; @@ -44,7 +44,7 @@ public class ConfigurationManager { public synchronized void addConfiguration(long logIndex, RaftConfiguration conf) { - RaftUtils.assertTrue(configurations.isEmpty() || + Preconditions.assertTrue(configurations.isEmpty() || configurations.lastEntry().getKey() < logIndex); configurations.put(logIndex, conf); this.currentConf = conf; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 db899f1..d026db6 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 @@ -24,8 +24,8 @@ import org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto; 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.Preconditions; 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; @@ -91,7 +91,7 @@ class LeaderElection extends Daemon { } private void initExecutor() { - RaftUtils.assertTrue(!others.isEmpty()); + Preconditions.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/43bc6ebf/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 e7704fc..2d1ab52 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 @@ -180,7 +180,7 @@ public class LeaderState { * Start bootstrapping new peers */ PendingRequest startSetConfiguration(SetConfigurationRequest request) { - RaftUtils.assertTrue(running && !inStagingState()); + Preconditions.assertTrue(running && !inStagingState()); RaftPeer[] peersInNewConf = request.getPeersInNewConf(); Collection<RaftPeer> peersToBootStrap = RaftConfiguration @@ -246,7 +246,7 @@ public class LeaderState { * Update the RpcSender list based on the current configuration */ private void updateSenders(RaftConfiguration conf) { - RaftUtils.assertTrue(conf.isStable() && !inStagingState()); + Preconditions.assertTrue(conf.isStable() && !inStagingState()); Iterator<LogAppender> iterator = senders.iterator(); while (iterator.hasNext()) { LogAppender sender = iterator.next(); @@ -312,7 +312,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 - RaftUtils.assertTrue(!running); + Preconditions.assertTrue(!running); } } } @@ -346,7 +346,7 @@ public class LeaderState { */ private BootStrapProgress checkProgress(FollowerInfo follower, long committed) { - RaftUtils.assertTrue(!follower.isAttendingVote()); + Preconditions.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) { @@ -362,7 +362,7 @@ public class LeaderState { } private Collection<BootStrapProgress> checkAllProgress(long committed) { - RaftUtils.assertTrue(inStagingState()); + Preconditions.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/43bc6ebf/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 4ae4726..5417e92 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 @@ -27,8 +27,9 @@ import org.apache.ratis.shaded.com.google.protobuf.ByteString; import org.apache.ratis.shaded.proto.RaftProtos.*; import org.apache.ratis.statemachine.SnapshotInfo; import org.apache.ratis.util.Daemon; +import org.apache.ratis.util.IOUtils; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import org.apache.ratis.util.Timestamp; import org.slf4j.Logger; @@ -141,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). - RaftUtils.assertTrue(follower.getNextIndex() == raftLog.getStartIndex(), + Preconditions.assertTrue(follower.getNextIndex() == raftLog.getStartIndex(), "follower's next index %s, local log start index %s", follower.getNextIndex(), raftLog.getStartIndex()); SnapshotInfo snapshot = server.getState().getLatestSnapshot(); @@ -312,7 +313,7 @@ public class LogAppender extends Daemon { throws IOException { FileChunkProto.Builder builder = FileChunkProto.newBuilder() .setOffset(offset).setChunkIndex(chunkIndex); - RaftUtils.readFully(in, buf, 0, length); + IOUtils.readFully(in, buf, 0, length); Path relativePath = server.getState().getStorage().getStorageDir() .relativizeToRoot(fileInfo.getPath()); builder.setFilename(relativePath.toString()); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 704a7d2..82f546b 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 @@ -21,7 +21,7 @@ import java.util.*; import org.apache.ratis.protocol.RaftPeer; import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; /** * The peer configuration of a raft cluster. @@ -38,7 +38,7 @@ class PeerConfiguration { map.put(p.getId(), p); } this.peers = Collections.unmodifiableMap(map); - RaftUtils.assertTrue(!this.peers.isEmpty()); + Preconditions.assertTrue(!this.peers.isEmpty()); } Collection<RaftPeer> getPeers() { @@ -73,7 +73,7 @@ class PeerConfiguration { } boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) { - RaftUtils.assertTrue(!others.contains(selfId)); + Preconditions.assertTrue(!others.contains(selfId)); int num = 0; if (contains(selfId)) { num++; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 1d51e54..59f870e 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 @@ -22,7 +22,7 @@ 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 org.apache.ratis.util.Preconditions; import java.util.concurrent.CompletableFuture; @@ -64,12 +64,12 @@ public class PendingRequest implements Comparable<PendingRequest> { * This is only used when setting new raft configuration. */ synchronized void setException(Throwable e) { - RaftUtils.assertTrue(e != null); + Preconditions.assertTrue(e != null); future.completeExceptionally(e); } synchronized void setReply(RaftClientReply r) { - RaftUtils.assertTrue(r != null); + Preconditions.assertTrue(r != null); future.complete(r); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 c56d7a8..98bc0a7 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 @@ -19,7 +19,7 @@ package org.apache.ratis.server.impl; import org.apache.ratis.protocol.*; import org.apache.ratis.statemachine.TransactionContext; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import org.slf4j.Logger; import java.io.IOException; @@ -44,8 +44,8 @@ class PendingRequests { PendingRequest addPendingRequest(long index, RaftClientRequest request, TransactionContext entry) { // externally synced for now - RaftUtils.assertTrue(!request.isReadOnly()); - RaftUtils.assertTrue(last == null || index == last.getIndex() + 1); + Preconditions.assertTrue(!request.isReadOnly()); + Preconditions.assertTrue(last == null || index == last.getIndex() + 1); return add(index, request, entry); } @@ -58,7 +58,7 @@ class PendingRequests { } PendingRequest addConfRequest(SetConfigurationRequest request) { - RaftUtils.assertTrue(pendingSetConf == null); + Preconditions.assertTrue(pendingSetConf == null); pendingSetConf = new PendingRequest(request); return pendingSetConf; } @@ -76,7 +76,7 @@ class PendingRequests { } void failSetConfiguration(RaftException e) { - RaftUtils.assertTrue(pendingSetConf != null); + Preconditions.assertTrue(pendingSetConf != null); pendingSetConf.setException(e); pendingSetConf = null; } @@ -91,7 +91,7 @@ class PendingRequests { void replyPendingRequest(long index, RaftClientReply reply) { final PendingRequest pending = pendingRequests.get(index); if (pending != null) { - RaftUtils.assertTrue(pending.getIndex() == index); + Preconditions.assertTrue(pending.getIndex() == index); pending.setReply(reply); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 a70abf9..6ce7ecd 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 @@ -19,7 +19,7 @@ package org.apache.ratis.server.impl; import org.apache.ratis.protocol.RaftPeer; import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import java.util.*; import java.util.concurrent.ThreadLocalRandom; @@ -51,7 +51,7 @@ public class RaftConfiguration { public Builder setConf(PeerConfiguration conf) { Objects.requireNonNull(conf); - RaftUtils.assertTrue(this.conf == null, "conf is already set."); + Preconditions.assertTrue(this.conf == null, "conf is already set."); this.conf = conf; return this; } @@ -66,9 +66,9 @@ public class RaftConfiguration { Builder setConf(RaftConfiguration transitionalConf) { Objects.requireNonNull(transitionalConf); - RaftUtils.assertTrue(transitionalConf.isTransitional()); + Preconditions.assertTrue(transitionalConf.isTransitional()); - RaftUtils.assertTrue(!forceTransitional); + Preconditions.assertTrue(!forceTransitional); forceStable = true; return setConf(transitionalConf.conf); } @@ -76,7 +76,7 @@ public class RaftConfiguration { public Builder setOldConf(PeerConfiguration oldConf) { Objects.requireNonNull(oldConf); - RaftUtils.assertTrue(this.oldConf == null, "oldConf is already set."); + Preconditions.assertTrue(this.oldConf == null, "oldConf is already set."); this.oldConf = oldConf; return this; } @@ -91,17 +91,17 @@ public class RaftConfiguration { Builder setOldConf(RaftConfiguration stableConf) { Objects.requireNonNull(stableConf); - RaftUtils.assertTrue(stableConf.isStable()); + Preconditions.assertTrue(stableConf.isStable()); - RaftUtils.assertTrue(!forceStable); + Preconditions.assertTrue(!forceStable); forceTransitional = true; return setOldConf(stableConf.conf); } public Builder setLogEntryIndex(long logEntryIndex) { - RaftUtils.assertTrue( + Preconditions.assertTrue( logEntryIndex != RaftServerConstants.INVALID_LOG_INDEX); - RaftUtils.assertTrue( + Preconditions.assertTrue( this.logEntryIndex == RaftServerConstants.INVALID_LOG_INDEX, "logEntryIndex is already set."); this.logEntryIndex = logEntryIndex; @@ -111,10 +111,10 @@ public class RaftConfiguration { /** Build a {@link RaftConfiguration}. */ public RaftConfiguration build() { if (forceTransitional) { - RaftUtils.assertTrue(oldConf != null); + Preconditions.assertTrue(oldConf != null); } if (forceStable) { - RaftUtils.assertTrue(oldConf == null); + Preconditions.assertTrue(oldConf == null); } return new RaftConfiguration(conf, oldConf, logEntryIndex); } @@ -204,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) { - RaftUtils.assertTrue(!others.contains(selfId)); + Preconditions.assertTrue(!others.contains(selfId)); return conf.hasMajority(others, selfId) && (oldConf == null || oldConf.hasMajority(others, selfId)); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 6a1fbd4..0a6a68d 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,43 +17,68 @@ */ package org.apache.ratis.server.impl; +import static org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.INCONSISTENCY; +import static org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.NOT_LEADER; +import static org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.SUCCESS; +import static org.apache.ratis.util.LifeCycle.State.CLOSED; +import static org.apache.ratis.util.LifeCycle.State.CLOSING; +import static org.apache.ratis.util.LifeCycle.State.RUNNING; +import static org.apache.ratis.util.LifeCycle.State.STARTING; + +import java.io.IOException; +import java.io.InterruptedIOException; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.OptionalLong; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.TimeUnit; + import org.apache.ratis.RaftConfigKeys; import org.apache.ratis.conf.Parameters; -import org.apache.ratis.rpc.RpcType; import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.*; +import org.apache.ratis.protocol.ClientId; +import org.apache.ratis.protocol.LeaderNotReadyException; +import org.apache.ratis.protocol.Message; +import org.apache.ratis.protocol.NotLeaderException; +import org.apache.ratis.protocol.RaftClientReply; +import org.apache.ratis.protocol.RaftClientRequest; +import org.apache.ratis.protocol.RaftException; +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.protocol.ReconfigurationInProgressException; +import org.apache.ratis.protocol.SetConfigurationRequest; +import org.apache.ratis.protocol.StateMachineException; +import org.apache.ratis.rpc.RpcType; import org.apache.ratis.server.RaftServer; import org.apache.ratis.server.RaftServerConfigKeys; import org.apache.ratis.server.RaftServerRpc; import org.apache.ratis.server.protocol.TermIndex; import org.apache.ratis.server.storage.FileInfo; import org.apache.ratis.shaded.com.google.common.annotations.VisibleForTesting; -import org.apache.ratis.shaded.proto.RaftProtos.*; +import org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto; +import org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesRequestProto; +import org.apache.ratis.shaded.proto.RaftProtos.FileChunkProto; +import org.apache.ratis.shaded.proto.RaftProtos.InstallSnapshotReplyProto; +import org.apache.ratis.shaded.proto.RaftProtos.InstallSnapshotRequestProto; +import org.apache.ratis.shaded.proto.RaftProtos.InstallSnapshotResult; +import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; +import org.apache.ratis.shaded.proto.RaftProtos.RequestVoteReplyProto; +import org.apache.ratis.shaded.proto.RaftProtos.RequestVoteRequestProto; import org.apache.ratis.statemachine.SnapshotInfo; import org.apache.ratis.statemachine.StateMachine; import org.apache.ratis.statemachine.TransactionContext; import org.apache.ratis.util.CodeInjectionForTesting; +import org.apache.ratis.util.IOUtils; import org.apache.ratis.util.LifeCycle; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import org.apache.ratis.util.TimeDuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.io.InterruptedIOException; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.OptionalLong; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ThreadLocalRandom; -import java.util.concurrent.TimeUnit; - -import static org.apache.ratis.shaded.proto.RaftProtos.AppendEntriesReplyProto.AppendResult.*; -import static org.apache.ratis.util.LifeCycle.State.*; - public class RaftServerImpl implements RaftServer { public static final Logger LOG = LoggerFactory.getLogger(RaftServerImpl.class); @@ -98,7 +123,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); - RaftUtils.assertTrue(maxTimeoutMs > minTimeoutMs, + Preconditions.assertTrue(maxTimeoutMs > minTimeoutMs, "max timeout: %s, min timeout: %s", maxTimeoutMs, minTimeoutMs); this.properties = properties; this.stateMachine = stateMachine; @@ -302,7 +327,7 @@ public class RaftServerImpl implements RaftServer { } synchronized void changeToLeader() { - RaftUtils.assertTrue(isCandidate()); + Preconditions.assertTrue(isCandidate()); shutdownElectionDaemon(); role = Role.LEADER; state.becomeLeader(); @@ -321,7 +346,7 @@ public class RaftServerImpl implements RaftServer { } synchronized void changeToCandidate() { - RaftUtils.assertTrue(isFollower()); + Preconditions.assertTrue(isFollower()); shutdownHeartbeatMonitor(); role = Role.CANDIDATE; // start election @@ -455,7 +480,6 @@ public class RaftServerImpl implements RaftServer { cacheEntry.failWithReply(exceptionReply); return CompletableFuture.completedFuture(exceptionReply); } - return appendTransaction(request, context, cacheEntry); } @@ -473,7 +497,7 @@ public class RaftServerImpl implements RaftServer { } catch (InterruptedException e) { final String s = id + ": Interrupted when waiting for reply, request=" + request; LOG.info(s, e); - throw RaftUtils.toInterruptedIOException(s, e); + throw IOUtils.toInterruptedIOException(s, e); } catch (ExecutionException e) { final Throwable cause = e.getCause(); if (cause == null) { @@ -483,7 +507,7 @@ public class RaftServerImpl implements RaftServer { cause instanceof StateMachineException) { return new RaftClientReply(request, (RaftException) cause); } else { - throw RaftUtils.asIOException(cause); + throw IOUtils.asIOException(cause); } } } @@ -617,23 +641,23 @@ public class RaftServerImpl implements RaftServer { final long index0 = entries[0].getIndex(); if (previous == null || previous.getTerm() == 0) { - RaftUtils.assertTrue(index0 == 0, + Preconditions.assertTrue(index0 == 0, "Unexpected Index: previous is null but entries[%s].getIndex()=%s", 0, index0); } else { - RaftUtils.assertTrue(previous.getIndex() == index0 - 1, + Preconditions.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(); - RaftUtils.assertTrue(expectedTerm >= t, + Preconditions.assertTrue(expectedTerm >= t, "Unexpected Term: entries[%s].getTerm()=%s but expectedTerm=%s", i, t, expectedTerm); final long indexi = entries[i].getIndex(); - RaftUtils.assertTrue(indexi == index0 + i, + Preconditions.assertTrue(indexi == index0 + i, "Unexpected Index: entries[%s].getIndex()=%s but entries[0].getIndex()=%s", i, indexi, index0); } @@ -786,7 +810,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 - RaftUtils.assertTrue( + Preconditions.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/43bc6ebf/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java index 0372558..bf2e94c 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java @@ -17,21 +17,21 @@ */ package org.apache.ratis.server.impl; +import java.io.Closeable; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + import org.apache.ratis.protocol.ClientId; import org.apache.ratis.protocol.RaftClientReply; import org.apache.ratis.shaded.com.google.common.annotations.VisibleForTesting; import org.apache.ratis.shaded.com.google.common.cache.Cache; import org.apache.ratis.shaded.com.google.common.cache.CacheBuilder; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.TimeDuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.Closeable; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; - public class RetryCache implements Closeable { static final Logger LOG = LoggerFactory.getLogger(RetryCache.class); private static final int MIN_CAPACITY = 128; @@ -162,7 +162,7 @@ public class RetryCache implements Closeable { } catch (ExecutionException e) { throw new IllegalStateException(e); } - RaftUtils.assertTrue(entry != null && !entry.isDone(), + Preconditions.assertTrue(entry != null && !entry.isDone(), "retry cache entry should be pending: %s", entry); return entry; } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 ff75237..e9d8ef8 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 @@ -29,8 +29,8 @@ import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; import org.apache.ratis.statemachine.SnapshotInfo; import org.apache.ratis.statemachine.StateMachine; import org.apache.ratis.statemachine.TransactionContext; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import java.io.Closeable; import java.io.IOException; @@ -215,7 +215,7 @@ public class ServerState implements Closeable { // leader and term later return true; } - RaftUtils.assertTrue(this.leaderId.equals(leaderId), + Preconditions.assertTrue(this.leaderId.equals(leaderId), "selfId:%s, this.leaderId:%s, received leaderId:%s", selfId, this.leaderId, leaderId); return true; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 38436a2..95af956 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,7 +35,7 @@ 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.apache.ratis.util.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -126,15 +126,15 @@ class StateMachineUpdater implements Runnable { } final long committedIndex = raftLog.getLastCommittedIndex(); - RaftUtils.assertTrue(lastAppliedIndex < committedIndex); + Preconditions.assertTrue(lastAppliedIndex < committedIndex); if (state == State.RELOAD) { - RaftUtils.assertTrue(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED); + Preconditions.assertTrue(stateMachine.getLifeCycleState() == LifeCycle.State.PAUSED); stateMachine.reinitialize(server.getId(), properties, storage); SnapshotInfo snapshot = stateMachine.getLatestSnapshot(); - RaftUtils.assertTrue(snapshot != null && snapshot.getIndex() > lastAppliedIndex, + Preconditions.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/43bc6ebf/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 4a36430..0edea13 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 @@ -25,7 +25,8 @@ import java.io.File; import java.io.IOException; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.IOUtils; +import org.apache.ratis.util.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,9 +73,9 @@ public class LogInputStream implements Closeable { public LogInputStream(File log, long startIndex, long endIndex, boolean isOpen) { if (isOpen) { - RaftUtils.assertTrue(endIndex == INVALID_LOG_INDEX); + Preconditions.assertTrue(endIndex == INVALID_LOG_INDEX); } else { - RaftUtils.assertTrue(endIndex >= startIndex); + Preconditions.assertTrue(endIndex >= startIndex); } this.logFile = log; @@ -84,12 +85,12 @@ public class LogInputStream implements Closeable { } private void init() throws IOException { - RaftUtils.assertTrue(state == State.UNINIT); + Preconditions.assertTrue(state == State.UNINIT); try { reader = new LogReader(logFile); // read the log header String header = reader.readLogHeader(); - RaftUtils.assertTrue(SegmentedRaftLog.HEADER_STR.equals(header), + Preconditions.assertTrue(SegmentedRaftLog.HEADER_STR.equals(header), "Corrupted log header: %s", header); state = State.OPEN; } finally { @@ -119,9 +120,9 @@ public class LogInputStream implements Closeable { init(); } catch (Throwable e) { LOG.error("caught exception initializing " + this, e); - throw RaftUtils.asIOException(e); + throw IOUtils.asIOException(e); } - RaftUtils.assertTrue(state != State.UNINIT); + Preconditions.assertTrue(state != State.UNINIT); return nextEntry(); case OPEN: entry = reader.readEntry(); @@ -149,7 +150,7 @@ public class LogInputStream implements Closeable { } long scanNextEntry() throws IOException { - RaftUtils.assertTrue(state == State.OPEN); + Preconditions.assertTrue(state == State.OPEN); return reader.scanEntry(); } @@ -202,7 +203,7 @@ public class LogInputStream implements Closeable { try { return scanEditLog(in, maxTxIdToScan); } finally { - RaftUtils.cleanup(LOG, in); + IOUtils.cleanup(LOG, in); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/main/java/org/apache/ratis/server/storage/LogOutputStream.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogOutputStream.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogOutputStream.java index 8563549..db0789e 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/LogOutputStream.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/LogOutputStream.java @@ -22,8 +22,8 @@ import org.apache.ratis.server.RaftServerConfigKeys; import org.apache.ratis.server.impl.RaftServerConstants; import org.apache.ratis.shaded.com.google.protobuf.CodedOutputStream; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; +import org.apache.ratis.util.IOUtils; import org.apache.ratis.util.PureJavaCrc32C; -import org.apache.ratis.util.RaftUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -126,7 +126,7 @@ public class LogOutputStream implements Closeable { fc.truncate(fc.position()); } } finally { - RaftUtils.cleanup(LOG, fc, out); + IOUtils.cleanup(LOG, fc, out); fc = null; out = null; } @@ -151,7 +151,7 @@ public class LogOutputStream implements Closeable { int size = (int) Math.min(BUFFER_SIZE, targetSize - allocated); ByteBuffer buffer = fill.slice(); buffer.limit(size); - RaftUtils.writeFully(fc, buffer, preallocatedPos); + IOUtils.writeFully(fc, buffer, preallocatedPos); preallocatedPos += size; allocated += size; } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 829bd3a..48a00e1 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 @@ -22,8 +22,9 @@ import org.apache.ratis.server.impl.RaftServerConstants; import org.apache.ratis.shaded.com.google.protobuf.CodedInputStream; import org.apache.ratis.shaded.com.google.protobuf.CodedOutputStream; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; +import org.apache.ratis.util.IOUtils; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.PureJavaCrc32C; -import org.apache.ratis.util.RaftUtils; import java.io.*; import java.nio.charset.StandardCharsets; @@ -196,9 +197,9 @@ public class LogReader implements Closeable { // want to reposition the mark one byte before the error if (numRead != -1) { in.reset(); - RaftUtils.skipFully(in, idx); + IOUtils.skipFully(in, idx); in.mark(temp.length + 1); - RaftUtils.skipFully(in, 1); + IOUtils.skipFully(in, 1); } } } @@ -246,7 +247,7 @@ public class LogReader implements Closeable { checkBufferSize(totalLength); in.reset(); in.mark(maxOpSize); - RaftUtils.readFully(in, temp, 0, totalLength); + IOUtils.readFully(in, temp, 0, totalLength); // verify checksum checksum.reset(); @@ -265,7 +266,7 @@ public class LogReader implements Closeable { } private void checkBufferSize(int entryLength) { - RaftUtils.assertTrue(entryLength <= maxOpSize); + Preconditions.assertTrue(entryLength <= maxOpSize); int length = temp.length; if (length < entryLength) { while (length < entryLength) { @@ -281,11 +282,11 @@ public class LogReader implements Closeable { void skipFully(long length) throws IOException { limiter.clearLimit(); - RaftUtils.skipFully(limiter, length); + IOUtils.skipFully(limiter, length); } @Override public void close() throws IOException { - RaftUtils.cleanup(null, in); + IOUtils.cleanup(null, in); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 89c0ff5..46f9f4f 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 @@ -22,7 +22,7 @@ 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 org.apache.ratis.util.Preconditions; import java.io.File; import java.io.IOException; @@ -85,12 +85,12 @@ class LogSegment implements Comparable<Long> { } static LogSegment newOpenSegment(long start) { - RaftUtils.assertTrue(start >= 0); + Preconditions.assertTrue(start >= 0); return new LogSegment(true, start, start - 1); } private static LogSegment newCloseSegment(long start, long end) { - RaftUtils.assertTrue(start >= 0 && end >= start); + Preconditions.assertTrue(start >= 0 && end >= start); return new LogSegment(false, start, end); } @@ -104,7 +104,7 @@ class LogSegment implements Comparable<Long> { LogEntryProto prev = null; while ((next = in.nextEntry()) != null) { if (prev != null) { - RaftUtils.assertTrue(next.getIndex() == prev.getIndex() + 1, + Preconditions.assertTrue(next.getIndex() == prev.getIndex() + 1, "gap between entry %s and entry %s", prev, next); } segment.append(next); @@ -123,9 +123,9 @@ class LogSegment implements Comparable<Long> { FileUtils.truncateFile(file, segment.getTotalSize()); } - RaftUtils.assertTrue(start == segment.records.get(0).entry.getIndex()); + Preconditions.assertTrue(start == segment.records.get(0).entry.getIndex()); if (!isOpen) { - RaftUtils.assertTrue(segment.getEndIndex() == end); + Preconditions.assertTrue(segment.getEndIndex() == end); } return segment; } @@ -147,26 +147,26 @@ class LogSegment implements Comparable<Long> { } void appendToOpenSegment(LogEntryProto... entries) { - RaftUtils.assertTrue(isOpen(), + Preconditions.assertTrue(isOpen(), "The log segment %s is not open for append", this.toString()); append(entries); } private void append(LogEntryProto... entries) { - RaftUtils.assertTrue(entries != null && entries.length > 0); + Preconditions.assertTrue(entries != null && entries.length > 0); final long term = entries[0].getTerm(); if (records.isEmpty()) { - RaftUtils.assertTrue(entries[0].getIndex() == startIndex, + Preconditions.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 - RaftUtils.assertTrue(entry.getTerm() == term, + Preconditions.assertTrue(entry.getTerm() == term, "expected term:%s, term of the entry:%s", term, entry.getTerm()); final LogRecord currentLast = getLastRecord(); if (currentLast != null) { - RaftUtils.assertTrue( + Preconditions.assertTrue( entry.getIndex() == currentLast.entry.getIndex() + 1, "gap between entries %s and %s", entry.getIndex(), currentLast.entry.getIndex()); @@ -198,7 +198,7 @@ class LogSegment implements Comparable<Long> { * Remove records from the given index (inclusive) */ void truncate(long fromIndex) { - RaftUtils.assertTrue(fromIndex >= startIndex && fromIndex <= endIndex); + Preconditions.assertTrue(fromIndex >= startIndex && fromIndex <= endIndex); LogRecord record = records.get((int) (fromIndex - startIndex)); for (long index = endIndex; index >= fromIndex; index--) { records.remove((int)(index - startIndex)); @@ -209,7 +209,7 @@ class LogSegment implements Comparable<Long> { } void close() { - RaftUtils.assertTrue(isOpen()); + Preconditions.assertTrue(isOpen()); isOpen = false; } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 17914cd..a49db9a 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,7 +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 org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; /** * A simple RaftLog implementation in memory. Used only for testing. @@ -65,7 +65,7 @@ public class MemoryRaftLog extends RaftLog { void truncate(long index) { checkLogState(); try(AutoCloseableLock writeLock = writeLock()) { - RaftUtils.assertTrue(index >= 0); + Preconditions.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/43bc6ebf/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 ff010e1..0e4ccbd 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 @@ -18,7 +18,7 @@ package org.apache.ratis.server.storage; import org.apache.ratis.util.AtomicFileOutputStream; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,7 +123,7 @@ class MetaFile { LOG.warn("Cannot load term/votedFor properties from {}", file, e); throw e; } finally { - RaftUtils.cleanup(LOG, br); + IOUtils.cleanup(LOG, br); } } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 77e554b..e72249a 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,8 +33,8 @@ import org.apache.ratis.server.protocol.TermIndex; 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.Preconditions; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -71,7 +71,7 @@ public abstract class RaftLog implements Closeable { } public void checkLogState() { - RaftUtils.assertTrue(isOpen, + Preconditions.assertTrue(isOpen, "The RaftLog has not been opened or has been closed"); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 2b9972c..0a21846 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,7 +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 org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; /** * In-memory RaftLog Cache. Currently we provide a simple implementation that @@ -57,10 +57,10 @@ class RaftLogCache { private void validateAdding(LogSegment segment) { final LogSegment lastClosed = getLastClosedSegment(); if (!segment.isOpen()) { - RaftUtils.assertTrue(lastClosed == null || + Preconditions.assertTrue(lastClosed == null || areConsecutiveSegments(lastClosed, segment)); } else { - RaftUtils.assertTrue(openSegment == null && + Preconditions.assertTrue(openSegment == null && (lastClosed == null || areConsecutiveSegments(lastClosed, segment))); } } @@ -168,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. - RaftUtils.assertTrue(openSegment != null); + Preconditions.assertTrue(openSegment != null); openSegment.appendToOpenSegment(entry); } @@ -176,7 +176,7 @@ class RaftLogCache { * finalize the current open segment, and start a new open segment */ void rollOpenSegment(boolean createNewOpen) { - RaftUtils.assertTrue(openSegment != null + Preconditions.assertTrue(openSegment != null && openSegment.numOfEntries() > 0); final long nextIndex = openSegment.getEndIndex() + 1; openSegment.close(); @@ -211,7 +211,7 @@ class RaftLogCache { Collections.singletonList(deleteOpenSegment())); } else { openSegment.truncate(index); - RaftUtils.assertTrue(!openSegment.isOpen()); + Preconditions.assertTrue(!openSegment.isOpen()); SegmentFileInfo info = new SegmentFileInfo(openSegment.getStartIndex(), oldEnd, true, openSegment.getTotalSize(), openSegment.getEndIndex()); @@ -280,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. - RaftUtils.assertTrue(segmentIndex == 0); + Preconditions.assertTrue(segmentIndex == 0); throw new IndexOutOfBoundsException(); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 d0fc3ce..5add8ae 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 @@ -34,7 +34,8 @@ import org.apache.ratis.server.storage.SegmentedRaftLog.Task; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; import org.apache.ratis.util.ExitUtils; import org.apache.ratis.util.FileUtils; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.IOUtils; +import org.apache.ratis.util.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -83,7 +84,7 @@ class RaftLogWorker implements Runnable { lastWrittenIndex = latestIndex; flushedIndex = latestIndex; if (openSegmentFile != null) { - RaftUtils.assertTrue(openSegmentFile.exists()); + Preconditions.assertTrue(openSegmentFile.exists()); out = new LogOutputStream(openSegmentFile, true, properties); } workerThread.start(); @@ -122,7 +123,7 @@ class RaftLogWorker implements Runnable { LOG.debug("add task {}", task); try { if (!queue.offer(task, 1, TimeUnit.SECONDS)) { - RaftUtils.assertTrue(isAlive(), + Preconditions.assertTrue(isAlive(), "the worker thread is not alive"); queue.put(task); } @@ -225,8 +226,8 @@ class RaftLogWorker implements Runnable { @Override public void execute() throws IOException { - RaftUtils.assertTrue(out != null); - RaftUtils.assertTrue(lastWrittenIndex + 1 == entry.getIndex(), + Preconditions.assertTrue(out != null); + Preconditions.assertTrue(lastWrittenIndex + 1 == entry.getIndex(), "lastWrittenIndex == %s, entry == %s", lastWrittenIndex, entry); out.write(entry); lastWrittenIndex = entry.getIndex(); @@ -251,19 +252,19 @@ class RaftLogWorker implements Runnable { @Override public void execute() throws IOException { - RaftUtils.cleanup(null, out); + IOUtils.cleanup(null, out); out = null; - RaftUtils.assertTrue(segmentToClose != null); + Preconditions.assertTrue(segmentToClose != null); File openFile = storage.getStorageDir() .getOpenLogFile(segmentToClose.getStartIndex()); - RaftUtils.assertTrue(openFile.exists(), + Preconditions.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()); - RaftUtils.assertTrue(!dstFile.exists()); + Preconditions.assertTrue(!dstFile.exists()); NativeIO.renameTo(openFile, dstFile); } else { // delete the file of the empty segment @@ -288,9 +289,9 @@ class RaftLogWorker implements Runnable { @Override void execute() throws IOException { File openFile = storage.getStorageDir().getOpenLogFile(newStartIndex); - RaftUtils.assertTrue(!openFile.exists(), "open file %s exists for %s", + Preconditions.assertTrue(!openFile.exists(), "open file %s exists for %s", openFile.getAbsolutePath(), RaftLogWorker.this.toString()); - RaftUtils.assertTrue(out == null && pendingFlushNum == 0); + Preconditions.assertTrue(out == null && pendingFlushNum == 0); out = new LogOutputStream(openFile, false, properties); } @@ -309,7 +310,7 @@ class RaftLogWorker implements Runnable { @Override void execute() throws IOException { - RaftUtils.cleanup(null, out); + IOUtils.cleanup(null, out); out = null; if (segments.toTruncate != null) { File fileToTruncate = segments.toTruncate.isOpen ? @@ -323,7 +324,7 @@ class RaftLogWorker implements Runnable { // rename the file File dstFile = storage.getStorageDir().getClosedLogFile( segments.toTruncate.startIndex, segments.toTruncate.newEndIndex); - RaftUtils.assertTrue(!dstFile.exists()); + Preconditions.assertTrue(!dstFile.exists()); NativeIO.renameTo(fileToTruncate, dstFile); // update lastWrittenIndex http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 a7bc47c..db8a196 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 @@ -24,7 +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.apache.ratis.util.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -54,7 +54,7 @@ public class RaftStorage implements Closeable { storageDir.lock(); format(); state = storageDir.analyzeStorage(false); - RaftUtils.assertTrue(state == StorageState.NORMAL); + Preconditions.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/43bc6ebf/ratis-server/src/main/java/org/apache/ratis/server/storage/SegmentedRaftLog.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/SegmentedRaftLog.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/SegmentedRaftLog.java index fc35535..2fd4dd2 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/SegmentedRaftLog.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/SegmentedRaftLog.java @@ -27,7 +27,7 @@ import org.apache.ratis.server.storage.RaftStorageDirectory.LogPathAndIndex; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; import org.apache.ratis.util.AutoCloseableLock; import org.apache.ratis.util.CodeInjectionForTesting; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import java.io.File; import java.io.IOException; @@ -213,7 +213,7 @@ public class SegmentedRaftLog extends RaftLog { // the term changes final long currentTerm = currentOpenSegment.getLastRecord().entry .getTerm(); - RaftUtils.assertTrue(currentTerm < entry.getTerm(), + Preconditions.assertTrue(currentTerm < entry.getTerm(), "open segment's term %s is larger than the new entry's term %s", currentTerm, entry.getTerm()); cache.rollOpenSegment(true); @@ -249,7 +249,7 @@ public class SegmentedRaftLog extends RaftLog { long truncateIndex = -1; for (; iter.hasNext() && index < entries.length; index++) { LogEntryProto storedEntry = iter.next(); - RaftUtils.assertTrue( + Preconditions.assertTrue( storedEntry.getIndex() == entries[index].getIndex(), "The stored entry's index %s is not consistent with" + " the received entries[%s]'s index %s", storedEntry.getIndex(), http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java index 0b4e8fa..bd25d07 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/SnapshotManager.java @@ -29,8 +29,9 @@ import org.apache.ratis.shaded.proto.RaftProtos.InstallSnapshotRequestProto; import org.apache.ratis.statemachine.SnapshotInfo; import org.apache.ratis.statemachine.StateMachine; import org.apache.ratis.util.FileUtils; +import org.apache.ratis.util.IOUtils; import org.apache.ratis.util.MD5FileUtil; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -89,7 +90,7 @@ public class SnapshotManager { // create the temp snapshot file and put padding inside out = new FileOutputStream(tmpSnapshotFile); } else { - RaftUtils.assertTrue(tmpSnapshotFile.exists()); + Preconditions.assertTrue(tmpSnapshotFile.exists()); out = new FileOutputStream(tmpSnapshotFile, true); FileChannel fc = out.getChannel(); fc.position(chunk.getOffset()); @@ -98,7 +99,7 @@ public class SnapshotManager { // write data to the file out.write(chunk.getData().toByteArray()); } finally { - RaftUtils.cleanup(null, out); + IOUtils.cleanup(null, out); } // rename the temp snapshot file if this is the last chunk. also verify http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 2a5f314..8c4a7c6 100644 --- a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java +++ b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java @@ -17,6 +17,19 @@ */ package org.apache.ratis; +import java.io.File; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + import org.apache.ratis.client.ClientFactory; import org.apache.ratis.client.RaftClient; import org.apache.ratis.conf.Parameters; @@ -25,28 +38,24 @@ import org.apache.ratis.protocol.RaftPeer; import org.apache.ratis.protocol.RaftPeerId; import org.apache.ratis.rpc.RpcType; import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.server.impl.*; +import org.apache.ratis.server.impl.DelayLocalExecutionInjection; +import org.apache.ratis.server.impl.LeaderState; +import org.apache.ratis.server.impl.RaftConfiguration; +import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.server.storage.MemoryRaftLog; import org.apache.ratis.server.storage.RaftLog; import org.apache.ratis.statemachine.BaseStateMachine; import org.apache.ratis.statemachine.StateMachine; import org.apache.ratis.util.ExitUtils; import org.apache.ratis.util.FileUtils; +import org.apache.ratis.util.CollectionUtils; import org.apache.ratis.util.NetUtils; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.Preconditions; +import org.apache.ratis.util.ReflectionUtils; import org.junit.Assert; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.util.*; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.TimeUnit; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; - public abstract class MiniRaftCluster { public static final Logger LOG = LoggerFactory.getLogger(MiniRaftCluster.class); public static final DelayLocalExecutionInjection logSyncDelay = @@ -117,7 +126,7 @@ public abstract class MiniRaftCluster { private static void formatDir(String dirStr) { final File serverDir = new File(dirStr); - RaftUtils.assertTrue(FileUtils.fullyDelete(serverDir), + Preconditions.assertTrue(FileUtils.fullyDelete(serverDir), "Failed to format directory %s", dirStr); LOG.info("Formatted directory {}", dirStr); } @@ -152,14 +161,14 @@ public abstract class MiniRaftCluster { public MiniRaftCluster initServers() { if (servers.isEmpty()) { - putNewServers(RaftUtils.as(conf.getPeers(), RaftPeer::getId), true); + putNewServers(CollectionUtils.as(conf.getPeers(), RaftPeer::getId), true); } return this; } private RaftServerImpl putNewServer(RaftPeerId id, boolean format) { final RaftServerImpl s = newRaftServer(id, format); - RaftUtils.assertTrue(servers.put(id, s) == null); + Preconditions.assertTrue(servers.put(id, s) == null); return s; } @@ -228,7 +237,7 @@ public abstract class MiniRaftCluster { STATEMACHINE_CLASS_KEY, STATEMACHINE_CLASS_DEFAULT, StateMachine.class); - return RaftUtils.newInstance(smClass); + return ReflectionUtils.newInstance(smClass); } public static Collection<RaftPeer> toRaftPeers( @@ -253,7 +262,7 @@ public abstract class MiniRaftCluster { // create and add new RaftServers final Collection<RaftServerImpl> newServers = putNewServers( - RaftUtils.as(Arrays.asList(ids), RaftPeerId::new), true); + CollectionUtils.as(Arrays.asList(ids), RaftPeerId::new), true); newServers.forEach(s -> startServer(s, startNewPeer)); final Collection<RaftPeer> newPeers = toRaftPeers(newServers); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java index 54cfa4d..d60f8eb 100644 --- a/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java +++ b/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java @@ -30,7 +30,7 @@ import org.apache.ratis.protocol.RaftPeerId; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.server.simulation.RequestHandler; import org.apache.ratis.server.storage.RaftLog; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.*; import org.junit.rules.Timeout; import org.slf4j.Logger; @@ -44,10 +44,10 @@ import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; public abstract class RaftNotLeaderExceptionBaseTest { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftLog.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RequestHandler.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftLog.LOG, Level.DEBUG); + LogUtils.setLogLevel(RequestHandler.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } public static final Logger LOG = http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java index bf7de9f..6884e40 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/RaftReconfigurationBaseTest.java @@ -17,6 +17,21 @@ */ package org.apache.ratis.server.impl; +import static java.util.Arrays.asList; +import static org.apache.ratis.MiniRaftCluster.leaderPlaceHolderDelay; +import static org.apache.ratis.MiniRaftCluster.logSyncDelay; +import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; +import static org.apache.ratis.server.impl.RaftServerTestUtil.waitAndCheckNewConf; +import static org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase.CONFIGURATIONENTRY; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; + import org.apache.log4j.Level; import org.apache.ratis.MiniRaftCluster; import org.apache.ratis.MiniRaftCluster.PeerChanges; @@ -25,11 +40,19 @@ import org.apache.ratis.RaftTestUtil.SimpleMessage; import org.apache.ratis.client.RaftClient; import org.apache.ratis.client.RaftClientRpc; import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.*; +import org.apache.ratis.protocol.ClientId; +import org.apache.ratis.protocol.LeaderNotReadyException; +import org.apache.ratis.protocol.RaftClientReply; +import org.apache.ratis.protocol.RaftClientRequest; +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.protocol.ReconfigurationInProgressException; +import org.apache.ratis.protocol.ReconfigurationTimeoutException; +import org.apache.ratis.protocol.SetConfigurationRequest; import org.apache.ratis.server.RaftServerConfigKeys; import org.apache.ratis.server.simulation.RequestHandler; import org.apache.ratis.server.storage.RaftLog; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -37,26 +60,11 @@ import org.mockito.internal.util.reflection.Whitebox; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; - -import static java.util.Arrays.asList; -import static org.apache.ratis.MiniRaftCluster.leaderPlaceHolderDelay; -import static org.apache.ratis.MiniRaftCluster.logSyncDelay; -import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; -import static org.apache.ratis.server.impl.RaftServerTestUtil.waitAndCheckNewConf; -import static org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase.CONFIGURATIONENTRY; - public abstract class RaftReconfigurationBaseTest { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RequestHandler.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RequestHandler.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } static final Logger LOG = LoggerFactory.getLogger(RaftReconfigurationBaseTest.class); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/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 48c9d55..528cc1a 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 @@ -21,7 +21,8 @@ import org.apache.ratis.RaftTestUtil; import org.apache.ratis.protocol.RaftPeerId; import org.apache.ratis.protocol.RaftRpcMessage; import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.IOUtils; +import org.apache.ratis.util.Preconditions; import org.apache.ratis.util.Timestamp; import java.io.IOException; @@ -43,7 +44,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, private final IOException ioe; ReplyOrException(REPLY reply, IOException ioe) { - RaftUtils.assertTrue(reply == null ^ ioe == null); + Preconditions.assertTrue(reply == null ^ ioe == null); this.reply = reply; this.ioe = ioe; } @@ -120,7 +121,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, RaftTestUtil.block(q.blockSendRequestTo::get); return q.request(request); } catch (InterruptedException e) { - throw RaftUtils.toInterruptedIOException("", e); + throw IOUtils.toInterruptedIOException("", e); } } @@ -136,7 +137,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, RaftTestUtil.delay(q.delayTakeRequestTo::get); request = q.takeRequest(); - RaftUtils.assertTrue(qid.equals(request.getReplierId())); + Preconditions.assertTrue(qid.equals(request.getReplierId())); // block request for testing final EventQueue<REQUEST, REPLY> reqQ = queues.get(request.getRequestorId()); @@ -145,7 +146,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, RaftTestUtil.block(reqQ.blockTakeRequestFrom::get); } } catch (InterruptedException e) { - throw RaftUtils.toInterruptedIOException("", e); + throw IOUtils.toInterruptedIOException("", e); } return request; } @@ -153,9 +154,9 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, public void sendReply(REQUEST request, REPLY reply, IOException ioe) throws IOException { if (reply != null) { - RaftUtils.assertTrue( + Preconditions.assertTrue( request.getRequestorId().equals(reply.getRequestorId())); - RaftUtils.assertTrue( + Preconditions.assertTrue( request.getReplierId().equals(reply.getReplierId())); } simulateLatency(); @@ -187,7 +188,7 @@ class SimulatedRequestReply<REQUEST extends RaftRpcMessage, try { Thread.sleep(randomSleepMs); } catch (InterruptedException ie) { - throw RaftUtils.toInterruptedIOException("", ie); + throw IOUtils.toInterruptedIOException("", ie); } } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java index 5332064..c1136b7 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java @@ -20,17 +20,15 @@ package org.apache.ratis.server.simulation; import org.apache.log4j.Level; import org.apache.ratis.RaftBasicTests; import org.apache.ratis.client.RaftClient; -import org.apache.ratis.conf.RaftProperties; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; +import org.apache.ratis.util.LogUtils; import java.io.IOException; -import java.util.concurrent.ThreadLocalRandom; public class TestRaftWithSimulatedRpc extends RaftBasicTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } private final MiniRaftClusterWithSimulatedRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java index 5e896e2..64cf0d7 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java @@ -17,18 +17,18 @@ */ package org.apache.ratis.server.simulation; +import java.io.IOException; + import org.apache.log4j.Level; import org.apache.ratis.RaftRetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.util.RaftUtils; - -import java.io.IOException; +import org.apache.ratis.util.LogUtils; public class TestRetryCacheWithSimulatedRpc extends RaftRetryCacheTests { static { - RaftUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - RaftUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); } private final MiniRaftClusterWithSimulatedRpc cluster; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/43bc6ebf/ratis-server/src/test/java/org/apache/ratis/server/storage/TestSegmentedRaftLog.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestSegmentedRaftLog.java b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestSegmentedRaftLog.java index 1fcb54b..ccf7690 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestSegmentedRaftLog.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestSegmentedRaftLog.java @@ -22,6 +22,7 @@ import org.apache.ratis.MiniRaftCluster; import org.apache.ratis.RaftTestUtil; import org.apache.ratis.RaftTestUtil.SimpleOperation; import org.apache.ratis.conf.RaftProperties; +import org.apache.ratis.util.LogUtils; import org.apache.ratis.util.SizeInBytes; import org.apache.ratis.protocol.ClientId; import org.apache.ratis.protocol.RaftPeerId; @@ -32,7 +33,6 @@ import org.apache.ratis.server.impl.RaftServerTestUtil; import org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto; import org.apache.ratis.util.FileUtils; import org.apache.ratis.util.ProtoUtils; -import org.apache.ratis.util.RaftUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -48,7 +48,7 @@ import java.util.function.Supplier; public class TestSegmentedRaftLog { static { - RaftUtils.setLogLevel(RaftLogWorker.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftLogWorker.LOG, Level.DEBUG); } private static final RaftPeerId peerId = new RaftPeerId("s0");
