Repository: incubator-ratis Updated Branches: refs/heads/master 5d4bb4235 -> 77ffa18ba
RATIS-151. Refactor ratis-server tests to reduce the use DEFAULT_CALLID. Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/77ffa18b Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/77ffa18b Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/77ffa18b Branch: refs/heads/master Commit: 77ffa18baba92bcea22e83533628fdf1020f68e1 Parents: 5d4bb42 Author: Tsz-Wo Nicholas Sze <[email protected]> Authored: Mon Nov 20 17:20:14 2017 -0800 Committer: Tsz-Wo Nicholas Sze <[email protected]> Committed: Tue Nov 21 15:04:17 2017 -0800 ---------------------------------------------------------------------- .../test/java/org/apache/ratis/BaseTest.java | 10 +- .../ratis/examples/ParameterizedBaseTest.java | 10 +- .../examples/arithmetic/TestArithmetic.java | 1 - .../examples/filestore/FileStoreBaseTest.java | 2 +- .../TestRaftStateMachineException.java | 48 ++---- .../ratis/grpc/TestRetryCacheWithGrpc.java | 4 +- .../hadooprpc/TestRetryCacheWithHadoopRpc.java | 4 +- .../ratis/netty/TestRetryCacheWithNettyRpc.java | 4 +- .../java/org/apache/ratis/MiniRaftCluster.java | 44 ++++- .../org/apache/ratis/RaftExceptionBaseTest.java | 20 +-- .../org/apache/ratis/RaftRetryCacheTests.java | 166 ------------------- .../java/org/apache/ratis/RetryCacheTests.java | 161 ++++++++++++++++++ .../impl/RaftReconfigurationBaseTest.java | 32 ++-- .../TestRetryCacheWithSimulatedRpc.java | 4 +- .../statemachine/RaftSnapshotBaseTest.java | 9 +- 15 files changed, 258 insertions(+), 261 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java index f4e622a..54aad77 100644 --- a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java +++ b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java @@ -25,6 +25,7 @@ import org.apache.ratis.util.JavaUtils; import org.apache.ratis.util.LogUtils; import org.junit.Assert; import org.junit.Rule; +import org.junit.rules.TestName; import org.junit.rules.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,6 +46,9 @@ public abstract class BaseTest { @Rule public final Timeout globalTimeout = new Timeout(getGlobalTimeoutSeconds() * 1000); + @Rule + public final TestName testName = new TestName(); + public int getGlobalTimeoutSeconds() { return 100; } @@ -66,12 +70,12 @@ public abstract class BaseTest { return rootTestDir.get(); } - public static File getClassTestDir(Class<?> caller) { - return new File(getRootTestDir(), caller.getSimpleName()); + public File getClassTestDir() { + return new File(getRootTestDir(), getClass().getSimpleName()); } public File getTestDir() { - return getClassTestDir(getClass()); + return new File(getClassTestDir(), testName.getMethodName()); } public static void testFailureCase( http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java b/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java index 9e140ac..de9fa6a 100644 --- a/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java +++ b/ratis-examples/src/test/java/org/apache/ratis/examples/ParameterizedBaseTest.java @@ -28,6 +28,7 @@ import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc; import org.apache.ratis.statemachine.StateMachine; import org.junit.AfterClass; import org.junit.Test; +import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,6 +37,7 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.atomic.AtomicReference; +@RunWith(Parameterized.class) public class ParameterizedBaseTest extends BaseTest { public static final Logger LOG = LoggerFactory.getLogger(ParameterizedBaseTest.class); @@ -93,14 +95,14 @@ public class ParameterizedBaseTest extends BaseTest { if (isAll || classes.contains(MiniRaftClusterWithSimulatedRpc.class)) { add(clusters, MiniRaftClusterWithSimulatedRpc.FACTORY, ids.next(), prop); } - if (isAll || classes.contains(MiniRaftClusterWithHadoopRpc.class)) { - add(clusters, MiniRaftClusterWithHadoopRpc.FACTORY, ids.next(), prop); + if (isAll || classes.contains(MiniRaftClusterWithGRpc.class)) { + add(clusters, MiniRaftClusterWithGRpc.FACTORY, ids.next(), prop); } if (isAll || classes.contains(MiniRaftClusterWithNetty.class)) { add(clusters, MiniRaftClusterWithNetty.FACTORY, ids.next(), prop); } - if (isAll || classes.contains(MiniRaftClusterWithGRpc.class)) { - add(clusters, MiniRaftClusterWithGRpc.FACTORY, ids.next(), prop); + if (isAll || classes.contains(MiniRaftClusterWithHadoopRpc.class)) { + add(clusters, MiniRaftClusterWithHadoopRpc.FACTORY, ids.next(), prop); } for(int i = 0; i < clusters.size(); i++) { LOG.info(i + ": " + clusters.get(i)[0].getClass().getSimpleName()); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java ---------------------------------------------------------------------- diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java index 0694f6d..bd88000 100644 --- a/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java +++ b/ratis-examples/src/test/java/org/apache/ratis/examples/arithmetic/TestArithmetic.java @@ -40,7 +40,6 @@ import static org.apache.ratis.examples.arithmetic.expression.BinaryExpression.O import static org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQRT; import static org.apache.ratis.examples.arithmetic.expression.UnaryExpression.Op.SQUARE; -@RunWith(Parameterized.class) public class TestArithmetic extends ParameterizedBaseTest { { LogUtils.setLogLevel(ArithmeticStateMachine.LOG, Level.DEBUG); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java index 8991b0d..66aeb6a 100644 --- a/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java +++ b/ratis-examples/src/test/java/org/apache/ratis/examples/filestore/FileStoreBaseTest.java @@ -53,7 +53,7 @@ public abstract class FileStoreBaseTest<CLUSTER extends MiniRaftCluster> p.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, FileStoreStateMachine.class, StateMachine.class); ConfUtils.setFile(p::setFile, FileStoreCommon.STATEMACHINE_DIR_KEY, - new File(BaseTest.getRootTestDir(), "filestore")); + new File(getClassTestDir(), "filestore")); } static final int NUM_PEERS = 3; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java ---------------------------------------------------------------------- diff --git a/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java b/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java index 756de13..6eaa3ea 100644 --- a/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java +++ b/ratis-examples/src/test/java/org/apache/ratis/statemachine/TestRaftStateMachineException.java @@ -18,9 +18,8 @@ package org.apache.ratis.statemachine; import org.apache.log4j.Level; -import org.apache.ratis.BaseTest; import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.RaftTestUtil; +import org.apache.ratis.RaftTestUtil.SimpleMessage; import org.apache.ratis.client.RaftClient; import org.apache.ratis.client.RaftClientRpc; import org.apache.ratis.examples.ParameterizedBaseTest; @@ -32,7 +31,6 @@ import org.apache.ratis.server.storage.RaftLog; import org.apache.ratis.util.LogUtils; import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.io.IOException; @@ -41,8 +39,7 @@ import java.util.concurrent.CompletableFuture; import static org.junit.Assert.fail; -@RunWith(Parameterized.class) -public class TestRaftStateMachineException extends BaseTest { +public class TestRaftStateMachineException extends ParameterizedBaseTest { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); LogUtils.setLogLevel(RaftLog.LOG, Level.DEBUG); @@ -72,8 +69,7 @@ public class TestRaftStateMachineException extends BaseTest { @Parameterized.Parameters public static Collection<Object[]> data() throws IOException { - return ParameterizedBaseTest.getMiniRaftClusters( - StateMachineWithException.class, 3); + return getMiniRaftClusters(StateMachineWithException.class, 3); } @Parameterized.Parameter @@ -81,39 +77,31 @@ public class TestRaftStateMachineException extends BaseTest { @Test public void testHandleStateMachineException() throws Exception { - cluster.restart(true); - RaftTestUtil.waitForLeader(cluster); + setAndStart(cluster); final RaftPeerId leaderId = cluster.getLeader().getId(); try(final RaftClient client = cluster.createClient(leaderId)) { - client.send(new RaftTestUtil.SimpleMessage("m")); + client.send(new SimpleMessage("m")); fail("Exception expected"); } catch (StateMachineException e) { e.printStackTrace(); Assert.assertTrue(e.getCause().getMessage().contains("Fake Exception")); } - - cluster.shutdown(); } @Test public void testRetryOnStateMachineException() throws Exception { - cluster.restart(true); - RaftTestUtil.waitForLeader(cluster); - final RaftPeerId leaderId = cluster.getLeader().getId(); + setAndStart(cluster); - RaftClient client = cluster.createClient(leaderId); - try { - client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); - } catch (Exception ignored) { - } + final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).getId(); long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); + final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, - cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); + cluster.getGroupId(), callId, new SimpleMessage("message")); RaftClientReply reply = rpc.sendRequest(r); Assert.assertFalse(reply.isSuccess()); Assert.assertNotNull(reply.getStateMachineException()); @@ -140,27 +128,21 @@ public class TestRaftStateMachineException extends BaseTest { server.getState().getLastAppliedIndex()); } - cluster.shutdown(); + client.close(); } @Test public void testRetryOnExceptionDuringReplication() throws Exception { - cluster.restart(true); - RaftTestUtil.waitForLeader(cluster); - final RaftPeerId leaderId = cluster.getLeader().getId(); - - RaftClient client = cluster.createClient(leaderId); - try { - client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); - } catch (Exception ignored) { - } + setAndStart(cluster); + final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(true).getId(); // turn on the preAppend failure switch failPreAppend = true; + final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, - cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); + cluster.getGroupId(), callId, new SimpleMessage("message")); RaftClientReply reply = rpc.sendRequest(r); Assert.assertTrue(reply.hasStateMachineException()); @@ -180,6 +162,6 @@ public class TestRaftStateMachineException extends BaseTest { Assert.assertNotEquals(oldEntry, currentEntry); failPreAppend = false; - cluster.shutdown(); + client.close(); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 cc0e2cc..956fd66 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 @@ -20,12 +20,12 @@ package org.apache.ratis.grpc; import java.io.IOException; import org.apache.log4j.Level; -import org.apache.ratis.RaftRetryCacheTests; +import org.apache.ratis.RetryCacheTests; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.util.LogUtils; import org.junit.Assert; -public class TestRetryCacheWithGrpc extends RaftRetryCacheTests { +public class TestRetryCacheWithGrpc extends RetryCacheTests { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 6e5ba8c..8c0dc3a 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 @@ -18,14 +18,14 @@ package org.apache.ratis.hadooprpc; import org.apache.log4j.Level; -import org.apache.ratis.RaftRetryCacheTests; +import org.apache.ratis.RetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.util.LogUtils; import java.io.IOException; -public class TestRetryCacheWithHadoopRpc extends RaftRetryCacheTests { +public class TestRetryCacheWithHadoopRpc extends RetryCacheTests { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 cb4c31b..659e426 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 @@ -20,12 +20,12 @@ package org.apache.ratis.netty; import java.io.IOException; import org.apache.log4j.Level; -import org.apache.ratis.RaftRetryCacheTests; +import org.apache.ratis.RetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.util.LogUtils; -public class TestRetryCacheWithNettyRpc extends RaftRetryCacheTests { +public class TestRetryCacheWithNettyRpc extends RetryCacheTests { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 8b6f681..a6d4bdd 100644 --- a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java +++ b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java @@ -20,10 +20,7 @@ package org.apache.ratis; import org.apache.ratis.client.RaftClient; import org.apache.ratis.conf.Parameters; import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.RaftGroup; -import org.apache.ratis.protocol.RaftGroupId; -import org.apache.ratis.protocol.RaftPeer; -import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.protocol.*; import org.apache.ratis.server.RaftServer; import org.apache.ratis.server.RaftServerConfigKeys; import org.apache.ratis.server.impl.BlockRequestHandlingInjection; @@ -48,6 +45,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.stream.StreamSupport; +import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; + public abstract class MiniRaftCluster { public static final Logger LOG = LoggerFactory.getLogger(MiniRaftCluster.class); @@ -370,6 +369,22 @@ public abstract class MiniRaftCluster { return b.toString(); } + public RaftServerImpl getLeaderAndSendFirstMessage() throws IOException { + return getLeaderAndSendFirstMessage(false); + } + + public RaftServerImpl getLeaderAndSendFirstMessage(boolean ignoreException) throws IOException { + final RaftServerImpl leader = getLeader(); + try(RaftClient client = createClient(leader.getId())) { + client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); + } catch (IOException e) { + if (!ignoreException) { + throw e; + } + } + return leader; + } + public RaftServerImpl getLeader() { return getLeader((RaftGroupId)null); } @@ -466,6 +481,27 @@ public abstract class MiniRaftCluster { .build(); } + public RaftClientRequest newRaftClientRequest( + ClientId clientId, RaftPeerId leaderId, Message message) { + return new RaftClientRequest(clientId, leaderId, getGroupId(), + DEFAULT_CALLID, message); + } + + public SetConfigurationRequest newSetConfigurationRequest( + ClientId clientId, RaftPeerId leaderId, + RaftPeer... peers) throws IOException { + return new SetConfigurationRequest(clientId, leaderId, getGroupId(), + DEFAULT_CALLID, peers); + } + + public void setConfiguration(RaftPeer... peers) throws IOException { + final RaftServerImpl leader = getLeader(); + final SetConfigurationRequest r = newSetConfigurationRequest( + ClientId.randomId(), leader.getId(), peers); + LOG.info("Start changing the configuration: {}", r); + leader.setConfiguration(r); + } + public void shutdown() { LOG.info("************************************************************** "); LOG.info("*** "); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java index e5439d6..4672b9d 100644 --- a/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java +++ b/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java @@ -21,20 +21,20 @@ import org.apache.log4j.Level; 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.server.impl.RaftServerImpl; import org.apache.ratis.server.storage.RaftLog; import org.apache.ratis.shaded.com.google.protobuf.ByteString; import org.apache.ratis.util.LogUtils; -import org.junit.*; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import java.io.IOException; import java.util.Arrays; import java.util.Collection; -import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; - public abstract class RaftExceptionBaseTest<CLUSTER extends MiniRaftCluster> extends BaseTest implements MiniRaftCluster.Factory.Get<CLUSTER> { @@ -95,10 +95,8 @@ public abstract class RaftExceptionBaseTest<CLUSTER extends MiniRaftCluster> reply= null; for (int i = 0; reply == null && i < 10; i++) { try { - reply = rpc.sendRequest( - new RaftClientRequest(ClientId.randomId(), leaderId, - cluster.getGroupId(), DEFAULT_CALLID, - new SimpleMessage("m2"))); + reply = rpc.sendRequest(cluster.newRaftClientRequest( + ClientId.randomId(), leaderId, new SimpleMessage("m2"))); } catch (IOException ignored) { Thread.sleep(1000); } @@ -142,10 +140,8 @@ public abstract class RaftExceptionBaseTest<CLUSTER extends MiniRaftCluster> // it is possible that the remote peer's rpc server is not ready. need retry for (int i = 0; reply == null && i < 10; i++) { try { - reply = rpc.sendRequest( - new RaftClientRequest(ClientId.randomId(), leaderId, - cluster.getGroupId(), DEFAULT_CALLID, - new SimpleMessage("m1"))); + reply = rpc.sendRequest(cluster.newRaftClientRequest( + ClientId.randomId(), leaderId, new SimpleMessage("m1"))); } catch (IOException ignored) { Thread.sleep(1000); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-server/src/test/java/org/apache/ratis/RaftRetryCacheTests.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftRetryCacheTests.java b/ratis-server/src/test/java/org/apache/ratis/RaftRetryCacheTests.java deleted file mode 100644 index 3398a0f..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/RaftRetryCacheTests.java +++ /dev/null @@ -1,166 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.ratis; - -import org.apache.ratis.MiniRaftCluster.PeerChanges; -import org.apache.ratis.client.RaftClient; -import org.apache.ratis.client.RaftClientRpc; -import org.apache.ratis.conf.RaftProperties; -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.SetConfigurationRequest; -import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.server.impl.RaftServerTestUtil; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; - -import static java.util.Arrays.asList; -import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; - -public abstract class RaftRetryCacheTests extends BaseTest { - public static final int NUM_SERVERS = 3; - protected static final RaftProperties properties = new RaftProperties(); - - public abstract MiniRaftCluster getCluster(); - - public RaftProperties getProperties() { - return properties; - } - - @Before - public void setup() throws IOException { - Assert.assertNull(getCluster().getLeader()); - getCluster().start(); - } - - @After - public void tearDown() { - final MiniRaftCluster cluster = getCluster(); - if (cluster != null) { - cluster.shutdown(); - } - } - - /** - * make sure the retry cache can correct capture the retry from a client, - * and returns the result from the previous request - */ - @Test - public void testBasicRetry() throws Exception { - final MiniRaftCluster cluster = getCluster(); - RaftTestUtil.waitForLeader(cluster); - - final RaftPeerId leaderId = cluster.getLeader().getId(); - RaftClient client = cluster.createClient(leaderId); - client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); - long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); - - final RaftClientRpc rpc = client.getClientRpc(); - final long callId = 999; - RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, - cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); - RaftClientReply reply = rpc.sendRequest(r); - Assert.assertEquals(callId, reply.getCallId()); - Assert.assertTrue(reply.isSuccess()); - - // retry with the same callId - for (int i = 0; i < 5; i++) { - reply = rpc.sendRequest(r); - Assert.assertEquals(client.getId(), reply.getClientId()); - Assert.assertEquals(callId, reply.getCallId()); - Assert.assertTrue(reply.isSuccess()); - } - - long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex(); - // make sure retry cache has the entry - for (RaftServerImpl server : cluster.iterateServerImpls()) { - LOG.info("check server " + server.getId()); - if (server.getState().getLastAppliedIndex() < leaderApplied) { - Thread.sleep(1000); - } - Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server)); - Assert.assertNotNull( - RaftServerTestUtil.getRetryEntry(server, client.getId(), callId)); - // make sure there is only one log entry committed - Assert.assertEquals(oldLastApplied + 1, - server.getState().getLastAppliedIndex()); - } - } - - /** - * Test retry while the leader changes to another peer - */ - @Test - public void testRetryOnNewLeader() throws Exception { - final MiniRaftCluster cluster = getCluster(); - RaftTestUtil.waitForLeader(cluster); - - final RaftPeerId leaderId = cluster.getLeader().getId(); - RaftClient client = cluster.createClient(leaderId); - client.send(new RaftTestUtil.SimpleMessage("first msg to make leader ready")); - - RaftClientRpc rpc = client.getClientRpc(); - final long callId = 999; - RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, - cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); - RaftClientReply reply = rpc.sendRequest(r); - Assert.assertEquals(callId, reply.getCallId()); - Assert.assertTrue(reply.isSuccess()); - long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); - - // trigger the reconfiguration, make sure the original leader is kicked out - PeerChanges change = cluster.addNewPeers(2, true); - RaftPeer[] allPeers = cluster.removePeers(2, true, - asList(change.newPeers)).allPeersInNewConf; - // trigger setConfiguration - SetConfigurationRequest request = new SetConfigurationRequest( - client.getId(), cluster.getLeader().getId(), cluster.getGroupId(), - DEFAULT_CALLID, allPeers); - LOG.info("Start changing the configuration: {}", request); - cluster.getLeader().setConfiguration(request); - - RaftTestUtil.waitForLeader(cluster); - final RaftPeerId newLeaderId = cluster.getLeader().getId(); - Assert.assertNotEquals(leaderId, newLeaderId); - // same clientId and callId in the request - r = new RaftClientRequest(client.getId(), newLeaderId, cluster.getGroupId(), - callId, new RaftTestUtil.SimpleMessage("message")); - for (int i = 0; i < 10; i++) { - try { - reply = rpc.sendRequest(r); - LOG.info("successfully sent out the retry request_" + i); - Assert.assertEquals(client.getId(), reply.getClientId()); - Assert.assertEquals(callId, reply.getCallId()); - Assert.assertTrue(reply.isSuccess()); - } catch (Exception e) { - LOG.info("hit exception while retrying the same request: " + e); - } - Thread.sleep(100); - } - - // check the new leader and make sure the retry did not get committed - Assert.assertEquals(oldLastApplied + 3, - cluster.getLeader().getState().getLastAppliedIndex()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-server/src/test/java/org/apache/ratis/RetryCacheTests.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RetryCacheTests.java b/ratis-server/src/test/java/org/apache/ratis/RetryCacheTests.java new file mode 100644 index 0000000..91aa58a --- /dev/null +++ b/ratis-server/src/test/java/org/apache/ratis/RetryCacheTests.java @@ -0,0 +1,161 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.ratis; + +import org.apache.ratis.MiniRaftCluster.PeerChanges; +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.RaftClientReply; +import org.apache.ratis.protocol.RaftClientRequest; +import org.apache.ratis.protocol.RaftPeer; +import org.apache.ratis.protocol.RaftPeerId; +import org.apache.ratis.server.impl.RaftServerImpl; +import org.apache.ratis.server.impl.RaftServerTestUtil; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; + +import static java.util.Arrays.asList; + +public abstract class RetryCacheTests extends BaseTest { + public static final int NUM_SERVERS = 3; + protected static final RaftProperties properties = new RaftProperties(); + + public abstract MiniRaftCluster getCluster(); + + public RaftProperties getProperties() { + return properties; + } + + @Before + public void setup() throws IOException { + Assert.assertNull(getCluster().getLeader()); + getCluster().start(); + } + + @After + public void tearDown() { + final MiniRaftCluster cluster = getCluster(); + if (cluster != null) { + cluster.shutdown(); + } + } + + /** + * make sure the retry cache can correct capture the retry from a client, + * and returns the result from the previous request + */ + @Test + public void testBasicRetry() throws Exception { + final MiniRaftCluster cluster = getCluster(); + RaftTestUtil.waitForLeader(cluster); + + + final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId(); + long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); + + final RaftClient client = cluster.createClient(leaderId); + final RaftClientRpc rpc = client.getClientRpc(); + final long callId = 999; + RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, + cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); + RaftClientReply reply = rpc.sendRequest(r); + Assert.assertEquals(callId, reply.getCallId()); + Assert.assertTrue(reply.isSuccess()); + + // retry with the same callId + for (int i = 0; i < 5; i++) { + reply = rpc.sendRequest(r); + Assert.assertEquals(client.getId(), reply.getClientId()); + Assert.assertEquals(callId, reply.getCallId()); + Assert.assertTrue(reply.isSuccess()); + } + + long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex(); + // make sure retry cache has the entry + for (RaftServerImpl server : cluster.iterateServerImpls()) { + LOG.info("check server " + server.getId()); + if (server.getState().getLastAppliedIndex() < leaderApplied) { + Thread.sleep(1000); + } + Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server)); + Assert.assertNotNull( + RaftServerTestUtil.getRetryEntry(server, client.getId(), callId)); + // make sure there is only one log entry committed + Assert.assertEquals(oldLastApplied + 1, + server.getState().getLastAppliedIndex()); + } + client.close(); + } + + /** + * Test retry while the leader changes to another peer + */ + @Test + public void testRetryOnNewLeader() throws Exception { + final MiniRaftCluster cluster = getCluster(); + RaftTestUtil.waitForLeader(cluster); + + final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId(); + final RaftClient client = cluster.createClient(leaderId); + RaftClientRpc rpc = client.getClientRpc(); + final long callId = 999; + RaftClientRequest r = new RaftClientRequest(client.getId(), leaderId, + cluster.getGroupId(), callId, new RaftTestUtil.SimpleMessage("message")); + RaftClientReply reply = rpc.sendRequest(r); + Assert.assertEquals(callId, reply.getCallId()); + Assert.assertTrue(reply.isSuccess()); + long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); + + // trigger the reconfiguration, make sure the original leader is kicked out + PeerChanges change = cluster.addNewPeers(2, true); + RaftPeer[] allPeers = cluster.removePeers(2, true, + asList(change.newPeers)).allPeersInNewConf; + // trigger setConfiguration + cluster.setConfiguration(allPeers); + + RaftTestUtil.waitForLeader(cluster); + final RaftPeerId newLeaderId = cluster.getLeader().getId(); + Assert.assertNotEquals(leaderId, newLeaderId); + // same clientId and callId in the request + r = new RaftClientRequest(client.getId(), newLeaderId, cluster.getGroupId(), + callId, new RaftTestUtil.SimpleMessage("message")); + for (int i = 0; i < 10; i++) { + try { + reply = rpc.sendRequest(r); + LOG.info("successfully sent out the retry request_" + i); + Assert.assertEquals(client.getId(), reply.getClientId()); + Assert.assertEquals(callId, reply.getCallId()); + Assert.assertTrue(reply.isSuccess()); + } catch (Exception e) { + LOG.info("hit exception while retrying the same request: " + e); + } + Thread.sleep(100); + } + + // check the new leader and make sure the retry did not get committed + Assert.assertEquals(oldLastApplied + 3, + cluster.getLeader().getState().getLastAppliedIndex()); + client.close(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 780ce79..1fccfc4 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 @@ -46,7 +46,6 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicReference; import static java.util.Arrays.asList; -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 static org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto.LogEntryBodyCase.NOOP; @@ -94,10 +93,7 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { RaftPeer[] allPeers = cluster.addNewPeers(2, true).allPeersInNewConf; // trigger setConfiguration - SetConfigurationRequest request = new SetConfigurationRequest(clientId, - cluster.getLeader().getId(), cluster.getGroupId(), DEFAULT_CALLID, allPeers); - LOG.info("Start changing the configuration: {}", request); - cluster.getLeader().setConfiguration(request); + cluster.setConfiguration(allPeers); // wait for the new configuration to take effect waitAndCheckNewConf(cluster, allPeers, 0, null); @@ -122,10 +118,7 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { .removePeers(2, false, Collections.emptyList()).allPeersInNewConf; // trigger setConfiguration - SetConfigurationRequest request = new SetConfigurationRequest(clientId, - cluster.getLeader().getId(), cluster.getGroupId(), DEFAULT_CALLID, allPeers); - LOG.info("Start changing the configuration: {}", request); - cluster.getLeader().setConfiguration(request); + cluster.setConfiguration(allPeers); // wait for the new configuration to take effect waitAndCheckNewConf(cluster, allPeers, 2, null); @@ -160,10 +153,7 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { asList(change.newPeers)).allPeersInNewConf; // trigger setConfiguration - SetConfigurationRequest request = new SetConfigurationRequest(clientId, - cluster.getLeader().getId(), cluster.getGroupId(), DEFAULT_CALLID, allPeers); - LOG.info("Start changing the configuration: {}", request); - cluster.getLeader().setConfiguration(request); + cluster.setConfiguration(allPeers); // wait for the new configuration to take effect waitAndCheckNewConf(cluster, allPeers, 2, null); @@ -256,8 +246,8 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { Assert.assertFalse(cluster.getLeader().getRaftConf().isTransitional()); final RaftClientRpc sender = client.getClientRpc(); - final SetConfigurationRequest request = new SetConfigurationRequest( - client.getId(), leaderId, cluster.getGroupId(), DEFAULT_CALLID, c1.allPeersInNewConf); + final SetConfigurationRequest request = cluster.newSetConfigurationRequest( + client.getId(), leaderId, c1.allPeersInNewConf); try { sender.sendRequest(request); Assert.fail("did not get expected exception"); @@ -474,8 +464,8 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { latch.await(); LOG.info("client2 starts to change conf"); final RaftClientRpc sender2 = client2.getClientRpc(); - sender2.sendRequest(new SetConfigurationRequest( - client2.getId(), leaderId, cluster.getGroupId(), DEFAULT_CALLID, peersInRequest2)); + sender2.sendRequest(cluster.newSetConfigurationRequest( + client2.getId(), leaderId, peersInRequest2)); } catch (ReconfigurationInProgressException e) { caughtException.set(true); } catch (Exception e) { @@ -540,8 +530,8 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { try(final RaftClient client = cluster.createClient(leaderId)) { LOG.info("client starts to change conf"); final RaftClientRpc sender = client.getClientRpc(); - RaftClientReply reply = sender.sendRequest(new SetConfigurationRequest( - client.getId(), leaderId, cluster.getGroupId(), DEFAULT_CALLID, change.allPeersInNewConf)); + RaftClientReply reply = sender.sendRequest(cluster.newSetConfigurationRequest( + client.getId(), leaderId, change.allPeersInNewConf)); if (reply.isNotLeader()) { gotNotLeader.set(true); } @@ -608,8 +598,8 @@ public abstract class RaftReconfigurationBaseTest extends BaseTest { final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc sender = client.getClientRpc(); - final RaftClientRequest request = new RaftClientRequest(client.getId(), - leaderId, cluster.getGroupId(), 0, new SimpleMessage("test")); + final RaftClientRequest request = cluster.newRaftClientRequest( + client.getId(), leaderId, new SimpleMessage("test")); while (!success.get()) { try { RaftClientReply reply = sender.sendRequest(request); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/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 64cf0d7..a088578 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 @@ -20,12 +20,12 @@ package org.apache.ratis.server.simulation; import java.io.IOException; import org.apache.log4j.Level; -import org.apache.ratis.RaftRetryCacheTests; +import org.apache.ratis.RetryCacheTests; import org.apache.ratis.client.RaftClient; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.util.LogUtils; -public class TestRetryCacheWithSimulatedRpc extends RaftRetryCacheTests { +public class TestRetryCacheWithSimulatedRpc extends RetryCacheTests { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/77ffa18b/ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java index b51dd7c..387319d 100644 --- a/ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java +++ b/ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java @@ -24,10 +24,8 @@ import org.apache.ratis.RaftTestUtil; import org.apache.ratis.RaftTestUtil.SimpleMessage; import org.apache.ratis.client.RaftClient; import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.ClientId; import org.apache.ratis.protocol.RaftClientReply; import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.protocol.SetConfigurationRequest; import org.apache.ratis.server.RaftServerConfigKeys; import org.apache.ratis.server.impl.RaftServerImpl; import org.apache.ratis.server.impl.RaftServerTestUtil; @@ -47,8 +45,6 @@ import org.slf4j.LoggerFactory; import java.io.File; import java.util.List; -import static org.apache.ratis.server.impl.RaftServerConstants.DEFAULT_CALLID; - public abstract class RaftSnapshotBaseTest extends BaseTest { static { LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); @@ -197,10 +193,7 @@ public abstract class RaftSnapshotBaseTest extends BaseTest { MiniRaftCluster.PeerChanges change = cluster.addNewPeers( new String[]{"s3", "s4"}, true); // trigger setConfiguration - SetConfigurationRequest request = new SetConfigurationRequest(ClientId.randomId(), - cluster.getLeader().getId(), cluster.getGroupId(), DEFAULT_CALLID, change.allPeersInNewConf); - LOG.info("Start changing the configuration: {}", request); - cluster.getLeader().setConfiguration(request); + cluster.setConfiguration(change.allPeersInNewConf); RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null); } finally {
