http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftSnapshotWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftSnapshotWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftSnapshotWithNetty.java deleted file mode 100644 index da861d5..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftSnapshotWithNetty.java +++ /dev/null @@ -1,31 +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.netty; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.statemachine.RaftSnapshotBaseTest; - -import java.io.IOException; - -public class TestRaftSnapshotWithNetty extends RaftSnapshotBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithNetty.FACTORY; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftStateMachineExceptionWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftStateMachineExceptionWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftStateMachineExceptionWithNetty.java deleted file mode 100644 index 34fddc9..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftStateMachineExceptionWithNetty.java +++ /dev/null @@ -1,25 +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.netty; - -import org.apache.ratis.server.impl.RaftStateMachineExceptionTests; - -public class TestRaftStateMachineExceptionWithNetty - extends RaftStateMachineExceptionTests<MiniRaftClusterWithNetty> - implements MiniRaftClusterWithNetty.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/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 deleted file mode 100644 index 28815d7..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftWithNetty.java +++ /dev/null @@ -1,34 +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.netty; - -import org.apache.ratis.RaftBasicTests; -import org.apache.ratis.server.impl.BlockRequestHandlingInjection; -import org.junit.Test; - -public class TestRaftWithNetty - extends RaftBasicTests<MiniRaftClusterWithNetty> - implements MiniRaftClusterWithNetty.FactoryGet { - - @Override - @Test - public void testWithLoad() throws Exception { - super.testWithLoad(); - BlockRequestHandlingInjection.getInstance().unblockAll(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/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 deleted file mode 100644 index 659e426..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRetryCacheWithNettyRpc.java +++ /dev/null @@ -1,45 +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.netty; - -import java.io.IOException; - -import org.apache.log4j.Level; -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 RetryCacheTests { - static { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - } - - private final MiniRaftClusterWithNetty cluster; - - public TestRetryCacheWithNettyRpc() throws IOException { - cluster = MiniRaftClusterWithNetty.FACTORY.newCluster( - NUM_SERVERS, getProperties()); - } - - @Override - public MiniRaftClusterWithNetty getCluster() { - return cluster; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerInformationWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerInformationWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerInformationWithNetty.java deleted file mode 100644 index d1bcae4..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerInformationWithNetty.java +++ /dev/null @@ -1,25 +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.netty; - -import org.apache.ratis.server.impl.ServerInformationBaseTest; - -public class TestServerInformationWithNetty - extends ServerInformationBaseTest<MiniRaftClusterWithNetty> - implements MiniRaftClusterWithNetty.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerRestartWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerRestartWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerRestartWithNetty.java deleted file mode 100644 index 15dc688..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestServerRestartWithNetty.java +++ /dev/null @@ -1,25 +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.netty; - -import org.apache.ratis.server.ServerRestartTests; - -public class TestServerRestartWithNetty - extends ServerRestartTests<MiniRaftClusterWithNetty> - implements MiniRaftClusterWithNetty.FactoryGet { -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-netty/src/test/resources/log4j.properties ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/resources/log4j.properties b/ratis-netty/src/test/resources/log4j.properties deleted file mode 100644 index ced0687..0000000 --- a/ratis-netty/src/test/resources/log4j.properties +++ /dev/null @@ -1,18 +0,0 @@ -# Licensed 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. -# log4j configuration used during build and unit tests - -log4j.rootLogger=info,stdout -log4j.threshold=ALL -log4j.appender.stdout=org.apache.log4j.ConsoleAppender -log4j.appender.stdout.layout=org.apache.log4j.PatternLayout -log4j.appender.stdout.layout.ConversionPattern=%d{ISO8601} %-5p %c{2} (%F:%M(%L)) - %m%n http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java b/ratis-server/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java deleted file mode 100644 index 55bcdfc..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/TestRaftServerLeaderElectionTimeout.java +++ /dev/null @@ -1,99 +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.log4j.Level; -import org.apache.ratis.client.RaftClient; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc; -import org.apache.ratis.proto.RaftProtos; -import org.apache.ratis.statemachine.SimpleStateMachine4Testing; -import org.apache.ratis.statemachine.StateMachine; -import org.apache.ratis.util.LogUtils; -import org.apache.ratis.util.TimeDuration; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -/** - * Test Raft Server Leader election timeout detection and notification to state machine. - */ -public class TestRaftServerLeaderElectionTimeout extends BaseTest { - static { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - } - - public static final int NUM_SERVERS = 3; - - protected static final RaftProperties properties = new RaftProperties(); - - private final MiniRaftClusterWithSimulatedRpc cluster = MiniRaftClusterWithSimulatedRpc - .FACTORY.newCluster(NUM_SERVERS, getProperties()); - - public RaftProperties getProperties() { - RaftServerConfigKeys - .setLeaderElectionTimeout(properties, TimeDuration.valueOf(1, TimeUnit.SECONDS)); - properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, - SimpleStateMachine4Testing.class, StateMachine.class); - return properties; - } - - @Before - public void setup() throws IOException { - Assert.assertNull(cluster.getLeader()); - cluster.start(); - } - - @After - public void tearDown() { - if (cluster != null) { - cluster.shutdown(); - } - } - - @Test - public void testLeaderElectionDetection() throws Exception { - RaftTestUtil.waitForLeader(cluster); - long leaderElectionTimeout = RaftServerConfigKeys. - leaderElectionTimeout(cluster.getProperties()).toInt(TimeUnit.MILLISECONDS); - - RaftServerImpl healthyFollower = cluster.getFollowers().get(1); - RaftServerImpl failedFollower = cluster.getFollowers().get(0); - // fail the leader and one of the followers to that quorum is not present - // for next leader election to succeed. - cluster.killServer(failedFollower.getId()); - cluster.killServer(cluster.getLeader().getId()); - - // Wait to ensure that leader election is triggered and also state machine callback is triggered - Thread.sleep( leaderElectionTimeout * 2); - - RaftProtos.RoleInfoProto roleInfoProto = - SimpleStateMachine4Testing.get(healthyFollower).getLeaderElectionTimeoutInfo(); - Assert.assertNotNull(roleInfoProto); - - Assert.assertEquals(roleInfoProto.getRole(), RaftProtos.RaftPeerRole.CANDIDATE); - Assert.assertTrue(roleInfoProto.getCandidateInfo().getLastLeaderElapsedTimeMs() > leaderElectionTimeout); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java b/ratis-server/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java deleted file mode 100644 index 96a164e..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/TestRaftServerSlownessDetection.java +++ /dev/null @@ -1,109 +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.log4j.Level; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc; -import org.apache.ratis.proto.RaftProtos; -import org.apache.ratis.statemachine.SimpleStateMachine4Testing; -import org.apache.ratis.statemachine.StateMachine; -import org.apache.ratis.util.LogUtils; -import org.apache.ratis.util.TimeDuration; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; - - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.TimeUnit; - -/** - * Test Raft Server Slowness detection and notification to Leader's statemachine. - */ -//TODO: fix StateMachine.notifySlowness(..); see RATIS-370 -@Ignore -public class TestRaftServerSlownessDetection extends BaseTest { - static { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - } - - public static final int NUM_SERVERS = 3; - - protected static final RaftProperties properties = new RaftProperties(); - - private final MiniRaftClusterWithSimulatedRpc cluster = MiniRaftClusterWithSimulatedRpc - .FACTORY.newCluster(NUM_SERVERS, getProperties()); - - public RaftProperties getProperties() { - RaftServerConfigKeys.Rpc - .setSlownessTimeout(properties, TimeDuration.valueOf(1, TimeUnit.SECONDS)); - properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, - SimpleStateMachine4Testing.class, StateMachine.class); - return properties; - } - - @Before - public void setup() throws IOException { - Assert.assertNull(cluster.getLeader()); - cluster.start(); - } - - @After - public void tearDown() { - if (cluster != null) { - cluster.shutdown(); - } - } - - @Test - public void testSlownessDetection() throws Exception { - RaftTestUtil.waitForLeader(cluster); - long slownessTimeout = RaftServerConfigKeys.Rpc - .slownessTimeout(cluster.getProperties()).toInt(TimeUnit.MILLISECONDS); - RaftServerImpl failedFollower = cluster.getFollowers().get(0); - - // fail the node and wait for the callback to be triggered - cluster.killServer(failedFollower.getId()); - Thread.sleep( slownessTimeout * 2); - - // Followers should not get any failed not notification - for (RaftServerImpl followerServer : cluster.getFollowers()) { - Assert.assertNull(SimpleStateMachine4Testing.get(followerServer).getSlownessInfo()); - } - // the leader should get notification that the follower has failed now - RaftProtos.RoleInfoProto roleInfoProto = - SimpleStateMachine4Testing.get(cluster.getLeader()).getSlownessInfo(); - Assert.assertNotNull(roleInfoProto); - - List<RaftProtos.ServerRpcProto> followers = - roleInfoProto.getLeaderInfo().getFollowerInfoList(); - //Assert that the node shutdown is lagging behind - for (RaftProtos.ServerRpcProto serverProto : followers) { - if (RaftPeerId.valueOf(serverProto.getId().getId()).equals(failedFollower.getId())) { - Assert.assertTrue(serverProto.getLastRpcElapsedTimeMs() > slownessTimeout); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/protocol/TestRaftId.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/protocol/TestRaftId.java b/ratis-server/src/test/java/org/apache/ratis/protocol/TestRaftId.java deleted file mode 100644 index 30b7ed5..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/protocol/TestRaftId.java +++ /dev/null @@ -1,54 +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.protocol; - -import org.apache.ratis.BaseTest; -import org.apache.ratis.thirdparty.com.google.protobuf.ByteString; -import org.junit.Assert; -import org.junit.Test; - -public class TestRaftId extends BaseTest { - @Override - public int getGlobalTimeoutSeconds() { - return 1; - } - - @Test - public void testClientId() { - final ClientId id = ClientId.randomId(); - final ByteString bytes = id.toByteString(); - Assert.assertEquals(bytes, id.toByteString()); - Assert.assertEquals(id, ClientId.valueOf(bytes)); - } - - @Test - public void testRaftGroupId() { - final RaftGroupId id = RaftGroupId.randomId(); - final ByteString bytes = id.toByteString(); - Assert.assertEquals(bytes, id.toByteString()); - Assert.assertEquals(id, RaftGroupId.valueOf(bytes)); - } - - @Test - public void testRaftPeerId() { - final RaftPeerId id = RaftPeerId.valueOf("abc"); - final ByteString bytes = id.toByteString(); - Assert.assertEquals(bytes, id.toByteString()); - Assert.assertEquals(id, RaftPeerId.valueOf(bytes)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/TestRaftLogMetrics.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/TestRaftLogMetrics.java b/ratis-server/src/test/java/org/apache/ratis/server/TestRaftLogMetrics.java deleted file mode 100644 index 58e319d..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/TestRaftLogMetrics.java +++ /dev/null @@ -1,119 +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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * 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.server; - -import com.codahale.metrics.Timer; -import org.apache.log4j.Level; -import org.apache.ratis.BaseTest; -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.RaftTestUtil; -import org.apache.ratis.client.RaftClient; -import org.apache.ratis.metrics.RatisMetricsRegistry; -import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc; -import org.apache.ratis.server.storage.RaftStorageTestUtils; -import org.apache.ratis.statemachine.StateMachine; -import org.apache.ratis.statemachine.impl.BaseStateMachine; -import org.apache.ratis.util.JavaUtils; -import org.apache.ratis.util.LogUtils; -import org.junit.Assert; -import org.junit.Test; - -import javax.management.ObjectName; -import java.lang.management.ManagementFactory; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.atomic.AtomicInteger; - -public class TestRaftLogMetrics extends BaseTest - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { - - { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - } - - public static final int NUM_SERVERS = 3; - - { - getProperties().setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, - MetricsStateMachine.class, StateMachine.class); - } - - static class MetricsStateMachine extends BaseStateMachine { - static MetricsStateMachine get(RaftServerImpl s) { - return (MetricsStateMachine)s.getStateMachine(); - } - - private final AtomicInteger flushCount = new AtomicInteger(); - - int getFlushCount() { - return flushCount.get(); - } - - @Override - public CompletableFuture<Void> flushStateMachineData(long index) { - flushCount.incrementAndGet(); - return super.flushStateMachineData(index); - } - } - - @Test - public void testFlushMetric() throws Exception { - try(final MiniRaftCluster cluster = newCluster(NUM_SERVERS)) { - cluster.start(); - runTestFlushMetric(cluster); - } - } - - static void runTestFlushMetric(MiniRaftCluster cluster) throws Exception { - int numMsg = 2; - final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMsg); - - try (final RaftClient client = cluster.createClient()) { - for (RaftTestUtil.SimpleMessage message : messages) { - client.send(message); - } - } - - // For leader, flush must happen before client can get replies. - assertFlushCount(cluster.getLeader()); - - // For followers, flush can be lagged behind. Attempt multiple times. - for(RaftServerImpl f : cluster.getFollowers()) { - JavaUtils.attempt(() -> assertFlushCount(f), 10, 100, f.getId() + "-assertFlushCount", null); - } - } - - static void assertFlushCount(RaftServerImpl server) throws Exception { - final String flushTimeMetric = RaftStorageTestUtils.getLogFlushTimeMetric(server.getId()); - Timer tm = RatisMetricsRegistry.getRegistry().getTimers().get(flushTimeMetric); - Assert.assertNotNull(tm); - - final MetricsStateMachine stateMachine = MetricsStateMachine.get(server); - final int expectedFlush = stateMachine.getFlushCount(); - - Assert.assertEquals(expectedFlush, tm.getCount()); - Assert.assertTrue(tm.getMeanRate() > 0); - - // Test jmx - ObjectName oname = new ObjectName("metrics", "name", flushTimeMetric); - Assert.assertEquals(expectedFlush, - ((Long) ManagementFactory.getPlatformMBeanServer().getAttribute(oname, "Count")) - .intValue()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/TestRaftServerConfigKeys.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/TestRaftServerConfigKeys.java b/ratis-server/src/test/java/org/apache/ratis/server/TestRaftServerConfigKeys.java deleted file mode 100644 index 53c8871..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/TestRaftServerConfigKeys.java +++ /dev/null @@ -1,98 +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.server; - -import org.apache.ratis.BaseTest; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.util.FileUtils; -import org.apache.ratis.util.JavaUtils; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; -import java.util.function.Supplier; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -/** - * Test cases to verify RaftServerConfigKeys. - */ -public class TestRaftServerConfigKeys { - - private static final Supplier<File> rootTestDir = JavaUtils.memoize( - () -> new File(BaseTest.getRootTestDir(), - TestRaftServerConfigKeys.class.getSimpleName() + - Integer.toHexString(ThreadLocalRandom.current().nextInt()))); - - @AfterClass - public static void tearDown() throws IOException { - FileUtils.deleteFully(rootTestDir.get()); - } - - /** - * Sets the value to <code>raft.server.storage.dir</code> via - * RaftServerConfigKeys and verifies it by reading directly from property. - */ - @Test - public void testStorageDirsProperty() { - final File testDir = new File( - rootTestDir.get(), UUID.randomUUID().toString()); - final List<File> directories = new ArrayList<>(); - final RaftProperties properties = new RaftProperties(); - - IntStream.range(0, 10).mapToObj((i) -> new File(testDir, - Integer.toString(i))).forEach(directories::add); - RaftServerConfigKeys.setStorageDirs(properties, directories); - - final String expected = directories.stream().map(File::getAbsolutePath) - .collect(Collectors.joining(",")); - final String actual = properties.get(RaftServerConfigKeys.STORAGE_DIR_KEY); - Assert.assertEquals(expected, actual); - } - - /** - * Sets the value to <code>raft.server.storage.dir</code> via - * RaftServerConfigKeys and also verifies the same via RaftServerConfigKeys. - */ - @Test - public void testStorageDirs() { - final File testDir = new File( - rootTestDir.get(), UUID.randomUUID().toString()); - final List<File> directories = new ArrayList<>(); - IntStream.range(0, 10).mapToObj((i) -> new File(testDir, - Integer.toString(i))).forEach(directories::add); - RaftProperties properties = new RaftProperties(); - RaftServerConfigKeys.setStorageDirs(properties, directories); - - final List<File> storageDirs = RaftServerConfigKeys.storageDirs(properties); - final List<String> expectedDirs = directories.stream() - .map(File::getAbsolutePath).collect(Collectors.toList()); - final List<String> actualDirs = storageDirs.stream() - .map(File::getAbsolutePath).collect(Collectors.toList()); - actualDirs.removeAll(expectedDirs); - Assert.assertEquals(directories.size(), storageDirs.size()); - Assert.assertEquals(0, actualDirs.size()); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java deleted file mode 100644 index f060645..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRaftServerJmx.java +++ /dev/null @@ -1,109 +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 - * <p> - * http://www.apache.org/licenses/LICENSE-2.0 - * <p> - * 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.server.impl; - -import org.apache.ratis.BaseTest; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.RaftGroupId; -import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.server.RaftServerMXBean; -import org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc; -import org.apache.ratis.util.JmxRegister; -import org.junit.Assert; -import org.junit.Test; - -import javax.management.JMException; -import javax.management.MBeanServer; -import javax.management.ObjectInstance; -import javax.management.ObjectName; -import java.lang.management.ManagementFactory; -import java.util.List; -import java.util.Set; - -import static org.apache.ratis.RaftTestUtil.waitForLeader; - -public class TestRaftServerJmx extends BaseTest { - @Test(timeout = 30000) - public void testJmxBeans() throws Exception { - final int NUM_SERVERS = 3; - final MiniRaftClusterWithSimulatedRpc cluster - = MiniRaftClusterWithSimulatedRpc.FACTORY.newCluster(3, new RaftProperties()); - cluster.start(); - waitForLeader(cluster); - - MBeanServer platformMBeanServer = ManagementFactory.getPlatformMBeanServer(); - Set<ObjectInstance> objectInstances = platformMBeanServer.queryMBeans(new ObjectName("Ratis:*"), null); - Assert.assertEquals(NUM_SERVERS, objectInstances.size()); - - for (ObjectInstance instance : objectInstances) { - Object groupId = platformMBeanServer.getAttribute(instance.getObjectName(), "GroupId"); - Assert.assertEquals(cluster.getGroupId().toString(), groupId); - } - cluster.shutdown(); - } - - @Test(timeout = 30000) - public void testRegister() throws JMException { - { - final JmxRegister jmx = new JmxRegister(); - runUnregister(false, jmx); - - runRegister(true, "abc", jmx); - runRegister(false, "abc", jmx); - runUnregister(true, jmx); - runUnregister(false, jmx); - - runRegister(true, "abc", jmx); - runUnregister(true, jmx); - runUnregister(false, jmx); - } - - { - final JmxRegister jmx = new JmxRegister(); - runRegister(true, "host:1234", jmx); - runUnregister(true, jmx); - runUnregister(false, jmx); - } - } - - static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) { - final RaftServerMXBean mBean = new RaftServerMXBean() { - @Override - public String getId() { return null; } - @Override - public String getLeaderId() { return null; } - @Override - public long getCurrentTerm() { return 0; } - @Override - public String getGroupId() { return null; } - @Override - public String getRole() { return null; } - @Override - public List<String> getFollowers() { return null; } - }; - final RaftPeerId id = RaftPeerId.valueOf(name); - final RaftGroupId groupId = RaftGroupId.randomId(); - final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx); - Assert.assertEquals(expectToSucceed, succeeded); - } - - static void runUnregister(boolean expectToSucceed, JmxRegister jmx) throws JMException { - final boolean succeeded = jmx.unregister(); - Assert.assertEquals(expectToSucceed, succeeded); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/impl/TestServerState.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestServerState.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestServerState.java deleted file mode 100644 index 5801d2b..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestServerState.java +++ /dev/null @@ -1,127 +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.server.impl; - -import org.apache.ratis.BaseTest; -import org.apache.ratis.util.FileUtils; -import org.apache.ratis.util.JavaUtils; -import org.junit.AfterClass; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.UUID; -import java.util.concurrent.ThreadLocalRandom; -import java.util.function.Supplier; -import java.util.stream.IntStream; - -/** - * Test cases to verify ServerState. - */ -public class TestServerState { - - private static final Supplier<File> rootTestDir = JavaUtils.memoize( - () -> new File(BaseTest.getRootTestDir(), - TestServerState.class.getSimpleName() + - Integer.toHexString(ThreadLocalRandom.current().nextInt()))); - - @AfterClass - public static void tearDown() throws IOException { - FileUtils.deleteFully(rootTestDir.get()); - } - - /** - * Tests choosing of storage directory when only one volume is configured. - * - * @throws IOException in case of exception. - */ - @Test - public void testChooseStorageDirWithOneVolume() throws IOException { - File testDir = new File(rootTestDir.get(), UUID.randomUUID().toString()); - List<File> directories = Collections.singletonList(testDir); - String subDirOne = UUID.randomUUID().toString(); - String subDirTwo = UUID.randomUUID().toString(); - File storageDirOne = ServerState.chooseStorageDir(directories, subDirOne); - File storageDirTwo = ServerState.chooseStorageDir(directories, subDirTwo); - File expectedOne = new File(testDir, subDirOne); - File expectedTwo = new File(testDir, subDirTwo); - Assert.assertEquals(expectedOne.getCanonicalPath(), - storageDirOne.getCanonicalPath()); - Assert.assertEquals(expectedTwo.getCanonicalPath(), - storageDirTwo.getCanonicalPath()); - } - - /** - * Tests choosing of storage directory when multiple volumes are configured. - * - * @throws IOException in case of exception. - */ - @Test - public void testChooseStorageDirWithMultipleVolumes() throws IOException { - File testDir = new File(rootTestDir.get(), UUID.randomUUID().toString()); - List<File> directories = new ArrayList<>(); - IntStream.range(0, 10).mapToObj((i) -> new File(testDir, - Integer.toString(i))).forEach((dir) -> { - try { - FileUtils.createDirectories(dir); - directories.add(dir); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - - directories.stream().filter((dir) -> Integer.parseInt(dir.getName()) != 6) - .forEach( - (dir) -> { - try { - FileUtils.createDirectories( - new File(dir, UUID.randomUUID().toString())); - } catch (IOException e) { - throw new RuntimeException(e); - } - }); - String subDir = UUID.randomUUID().toString(); - File storageDirectory = ServerState.chooseStorageDir(directories, subDir); - File expected = new File(directories.get(6), subDir); - Assert.assertEquals(expected.getCanonicalPath(), - storageDirectory.getCanonicalPath()); - } - - /** - * Tests choosing of storage directory when only no volume is configured. - * - * @throws IOException in case of exception. - */ - @Test - public void testChooseStorageDirWithNoVolume() { - try { - ServerState.chooseStorageDir( - Collections.emptyList(), UUID.randomUUID().toString()); - Assert.fail(); - } catch (IOException ex) { - String expectedErrMsg = "No storage directory found."; - Assert.assertEquals(expectedErrMsg, ex.getMessage()); - } - } - -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestGroupManagementWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestGroupManagementWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestGroupManagementWithSimulatedRpc.java deleted file mode 100644 index 73fbae9..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestGroupManagementWithSimulatedRpc.java +++ /dev/null @@ -1,28 +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.server.simulation; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.server.impl.GroupManagementBaseTest; - -public class TestGroupManagementWithSimulatedRpc extends GroupManagementBaseTest { - @Override - public MiniRaftCluster.Factory<? extends MiniRaftCluster> getClusterFactory() { - return MiniRaftClusterWithSimulatedRpc.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestLeaderElectionWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestLeaderElectionWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestLeaderElectionWithSimulatedRpc.java deleted file mode 100644 index 7ea273b..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestLeaderElectionWithSimulatedRpc.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.server.impl.LeaderElectionTests; - -public class TestLeaderElectionWithSimulatedRpc - extends LeaderElectionTests<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftExceptionWithSimulation.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftExceptionWithSimulation.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftExceptionWithSimulation.java deleted file mode 100644 index f328ea3..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftExceptionWithSimulation.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.RaftExceptionBaseTest; - -public class TestRaftExceptionWithSimulation - extends RaftExceptionBaseTest<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftReconfigurationWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftReconfigurationWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftReconfigurationWithSimulatedRpc.java deleted file mode 100644 index b1bb26a..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftReconfigurationWithSimulatedRpc.java +++ /dev/null @@ -1,31 +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.server.simulation; - -import java.io.IOException; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.server.impl.RaftReconfigurationBaseTest; - -public class TestRaftReconfigurationWithSimulatedRpc - extends RaftReconfigurationBaseTest { - @Override - public MiniRaftCluster getCluster(int peerNum) throws IOException { - return MiniRaftClusterWithSimulatedRpc.FACTORY.newCluster(peerNum, prop); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftSnapshotWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftSnapshotWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftSnapshotWithSimulatedRpc.java deleted file mode 100644 index 8345872..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftSnapshotWithSimulatedRpc.java +++ /dev/null @@ -1,31 +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.server.simulation; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.statemachine.RaftSnapshotBaseTest; - -import java.io.IOException; - -public class TestRaftSnapshotWithSimulatedRpc extends RaftSnapshotBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithSimulatedRpc.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftStateMachineExceptionWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftStateMachineExceptionWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftStateMachineExceptionWithSimulatedRpc.java deleted file mode 100644 index d8ca86f..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftStateMachineExceptionWithSimulatedRpc.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.server.impl.RaftStateMachineExceptionTests; - -public class TestRaftStateMachineExceptionWithSimulatedRpc extends - RaftStateMachineExceptionTests<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/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 deleted file mode 100644 index 391a6fa..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftWithSimulatedRpc.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.RaftBasicTests; - -public class TestRaftWithSimulatedRpc - extends RaftBasicTests<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/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 deleted file mode 100644 index a088578..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRetryCacheWithSimulatedRpc.java +++ /dev/null @@ -1,45 +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.server.simulation; - -import java.io.IOException; - -import org.apache.log4j.Level; -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 RetryCacheTests { - static { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - } - - private final MiniRaftClusterWithSimulatedRpc cluster; - - public TestRetryCacheWithSimulatedRpc() throws IOException { - cluster = MiniRaftClusterWithSimulatedRpc.FACTORY.newCluster( - NUM_SERVERS, getProperties()); - } - - @Override - public MiniRaftClusterWithSimulatedRpc getCluster() { - return cluster; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerInformationWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerInformationWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerInformationWithSimulatedRpc.java deleted file mode 100644 index f7025a5..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerInformationWithSimulatedRpc.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.server.impl.ServerInformationBaseTest; - -public class TestServerInformationWithSimulatedRpc - extends ServerInformationBaseTest<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerRestartWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerRestartWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerRestartWithSimulatedRpc.java deleted file mode 100644 index 306e5e7..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestServerRestartWithSimulatedRpc.java +++ /dev/null @@ -1,25 +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.server.simulation; - -import org.apache.ratis.server.ServerRestartTests; - -public class TestServerRestartWithSimulatedRpc - extends ServerRestartTests<MiniRaftClusterWithSimulatedRpc> - implements MiniRaftClusterWithSimulatedRpc.FactoryGet { -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/storage/TestCacheEviction.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestCacheEviction.java b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestCacheEviction.java deleted file mode 100644 index 1cd41a5..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestCacheEviction.java +++ /dev/null @@ -1,198 +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.server.storage; - -import org.apache.ratis.BaseTest; -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.RaftTestUtil.SimpleOperation; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.protocol.RaftPeerId; -import org.apache.ratis.server.RaftServerConfigKeys; -import org.apache.ratis.server.impl.RaftServerConstants; -import org.apache.ratis.server.impl.RaftServerImpl; -import org.apache.ratis.server.impl.ServerProtoUtils; -import org.apache.ratis.server.impl.ServerState; -import org.apache.ratis.server.storage.CacheInvalidationPolicy.CacheInvalidationPolicyDefault; -import org.apache.ratis.server.storage.TestSegmentedRaftLog.SegmentRange; -import org.apache.ratis.proto.RaftProtos.LogEntryProto; -import org.apache.ratis.statemachine.SimpleStateMachine4Testing; -import org.apache.ratis.statemachine.StateMachine; -import org.apache.ratis.util.SizeInBytes; -import org.junit.Assert; -import org.junit.Test; -import org.mockito.Mockito; - -import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.CompletableFuture; - -public class TestCacheEviction extends BaseTest { - private static final CacheInvalidationPolicy policy = new CacheInvalidationPolicyDefault(); - - private List<LogSegment> prepareSegments(int numSegments, boolean[] cached, long start, long size) { - Assert.assertEquals(numSegments, cached.length); - List<LogSegment> segments = new ArrayList<>(numSegments); - for (int i = 0; i < numSegments; i++) { - LogSegment s = LogSegment.newCloseSegment(null, start, start + size - 1); - if (cached[i]) { - s = Mockito.spy(s); - Mockito.when(s.hasCache()).thenReturn(true); - } - segments.add(s); - start += size; - } - return segments; - } - - @Test - public void testBasicEviction() throws Exception { - final int maxCached = 5; - List<LogSegment> segments = prepareSegments(5, - new boolean[]{true, true, true, true, true}, 0, 10); - - // case 1, make sure we do not evict cache for segments behind local flushed index - List<LogSegment> evicted = policy.evict(null, 5, 15, segments, maxCached); - Assert.assertEquals(0, evicted.size()); - - // case 2, suppose the local flushed index is in the 3rd segment, then we - // can evict the first two segment - evicted = policy.evict(null, 25, 30, segments, maxCached); - Assert.assertEquals(2, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(0)); - Assert.assertSame(evicted.get(1), segments.get(1)); - - // case 3, similar with case 2, but the local applied index is less than - // the local flushed index. - evicted = policy.evict(null, 25, 15, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(0)); - - // case 4, the local applied index is very small, then evict cache behind it - // first and let the state machine load the segments later - evicted = policy.evict(null, 35, 5, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(2)); - - Mockito.when(segments.get(2).hasCache()).thenReturn(false); - evicted = policy.evict(null, 35, 5, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(1)); - - Mockito.when(segments.get(1).hasCache()).thenReturn(false); - evicted = policy.evict(null, 35, 5, segments, maxCached); - Assert.assertEquals(0, evicted.size()); - } - - @Test - public void testEvictionWithFollowerIndices() throws Exception { - final int maxCached = 6; - List<LogSegment> segments = prepareSegments(6, - new boolean[]{true, true, true, true, true, true}, 0, 10); - - // case 1, no matter where the followers are, we do not evict segments behind local - // flushed index - List<LogSegment> evicted = policy.evict(new long[]{20, 40, 40}, 5, 15, segments, - maxCached); - Assert.assertEquals(0, evicted.size()); - - // case 2, the follower indices are behind the local flushed index - evicted = policy.evict(new long[]{30, 40, 45}, 25, 30, segments, maxCached); - Assert.assertEquals(2, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(0)); - Assert.assertSame(evicted.get(1), segments.get(1)); - - // case 3, similar with case 3 in basic eviction test - evicted = policy.evict(new long[]{30, 40, 45}, 25, 15, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(0)); - - // case 4, the followers are slower than local flush - evicted = policy.evict(new long[]{15, 45, 45}, 55, 50, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(0)); - - Mockito.when(segments.get(0).hasCache()).thenReturn(false); - evicted = policy.evict(new long[]{15, 45, 45}, 55, 50, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(2)); - - Mockito.when(segments.get(2).hasCache()).thenReturn(false); - evicted = policy.evict(new long[]{15, 45, 45}, 55, 50, segments, maxCached); - Assert.assertEquals(1, evicted.size()); - Assert.assertSame(evicted.get(0), segments.get(3)); - - Mockito.when(segments.get(3).hasCache()).thenReturn(false); - evicted = policy.evict(new long[]{15, 45, 45}, 55, 50, segments, maxCached); - Assert.assertEquals(0, evicted.size()); - } - - @Test - public void testEvictionInSegmentedLog() throws Exception { - final RaftProperties prop = new RaftProperties(); - prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, - SimpleStateMachine4Testing.class, StateMachine.class); - RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB")); - RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB")); - final RaftPeerId peerId = RaftPeerId.valueOf("s0"); - final int maxCachedNum = RaftServerConfigKeys.Log.maxCachedSegmentNum(prop); - - File storageDir = getTestDir(); - RaftServerConfigKeys.setStorageDirs(prop, Collections.singletonList(storageDir)); - RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR); - - RaftServerImpl server = Mockito.mock(RaftServerImpl.class); - ServerState state = Mockito.mock(ServerState.class); - Mockito.when(server.getState()).thenReturn(state); - Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{}); - Mockito.when(state.getLastAppliedIndex()).thenReturn(0L); - - SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, prop); - raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null); - List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0); - LogEntryProto[] entries = generateEntries(slist); - raftLog.append(entries).forEach(CompletableFuture::join); - - // check the current cached segment number: the last segment is still open - Assert.assertEquals(maxCachedNum - 1, - raftLog.getRaftLogCache().getCachedSegmentNum()); - - Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[]{21, 40, 40}); - Mockito.when(state.getLastAppliedIndex()).thenReturn(35L); - slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum); - entries = generateEntries(slist); - raftLog.append(entries).forEach(CompletableFuture::join); - - // check the cached segment number again. since the slowest follower is on - // index 21, the eviction should happen and evict 3 segments - Assert.assertEquals(maxCachedNum + 1 - 3, - raftLog.getRaftLogCache().getCachedSegmentNum()); - } - - private LogEntryProto[] generateEntries(List<SegmentRange> slist) { - List<LogEntryProto> eList = new ArrayList<>(); - for (SegmentRange range : slist) { - for (long index = range.start; index <= range.end; index++) { - SimpleOperation m = new SimpleOperation(new String(new byte[1024])); - eList.add(ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), range.term, index)); - } - } - return eList.toArray(new LogEntryProto[eList.size()]); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/5c37675f/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogCache.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogCache.java b/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogCache.java deleted file mode 100644 index d3c216d..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/storage/TestRaftLogCache.java +++ /dev/null @@ -1,257 +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.server.storage; - -import java.io.IOException; -import java.util.Iterator; - -import org.apache.ratis.RaftTestUtil.SimpleOperation; -import org.apache.ratis.conf.RaftProperties; -import org.apache.ratis.server.impl.ServerProtoUtils; -import org.apache.ratis.server.protocol.TermIndex; -import org.apache.ratis.server.storage.RaftLogCache.TruncationSegments; -import org.apache.ratis.proto.RaftProtos.LogEntryProto; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -public class TestRaftLogCache { - private static final RaftProperties prop = new RaftProperties(); - - private RaftLogCache cache; - - @Before - public void setup() { - cache = new RaftLogCache(null, null, prop); - } - - private LogSegment prepareLogSegment(long start, long end, boolean isOpen) { - LogSegment s = LogSegment.newOpenSegment(null, start); - for (long i = start; i <= end; i++) { - SimpleOperation m = new SimpleOperation("m" + i); - LogEntryProto entry = ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, i); - s.appendToOpenSegment(entry); - } - if (!isOpen) { - s.close(); - } - return s; - } - - private void checkCache(long start, long end, int segmentSize) throws IOException { - Assert.assertEquals(start, cache.getStartIndex()); - Assert.assertEquals(end, cache.getEndIndex()); - - for (long index = start; index <= end; index++) { - LogEntryProto entry = cache.getSegment(index).getEntryWithoutLoading(index).getEntry(); - Assert.assertEquals(index, entry.getIndex()); - } - - long[] offsets = new long[]{start, start + 1, start + (end - start) / 2, - end - 1, end}; - for (long offset : offsets) { - checkCacheEntries(offset, (int) (end - offset + 1), end); - checkCacheEntries(offset, 1, end); - checkCacheEntries(offset, 20, end); - checkCacheEntries(offset, segmentSize, end); - checkCacheEntries(offset, segmentSize - 1, end); - } - } - - private void checkCacheEntries(long offset, int size, long end) { - TermIndex[] entries = cache.getTermIndices(offset, offset + size); - long realEnd = offset + size > end + 1 ? end + 1 : offset + size; - Assert.assertEquals(realEnd - offset, entries.length); - for (long i = offset; i < realEnd; i++) { - Assert.assertEquals(i, entries[(int) (i - offset)].getIndex()); - } - } - - @Test - public void testAddSegments() throws Exception { - LogSegment s1 = prepareLogSegment(1, 100, false); - cache.addSegment(s1); - checkCache(1, 100, 100); - - try { - LogSegment s = prepareLogSegment(102, 103, true); - cache.addSegment(s); - Assert.fail("should fail since there is gap between two segments"); - } catch (IllegalStateException ignored) { - } - - LogSegment s2 = prepareLogSegment(101, 200, true); - cache.addSegment(s2); - checkCache(1, 200, 100); - - try { - LogSegment s = prepareLogSegment(201, 202, true); - cache.addSegment(s); - Assert.fail("should fail since there is still an open segment in cache"); - } catch (IllegalStateException ignored) { - } - - cache.rollOpenSegment(false); - checkCache(1, 200, 100); - - try { - LogSegment s = prepareLogSegment(202, 203, true); - cache.addSegment(s); - Assert.fail("should fail since there is gap between two segments"); - } catch (IllegalStateException ignored) { - } - - LogSegment s3 = prepareLogSegment(201, 300, true); - cache.addSegment(s3); - Assert.assertNotNull(cache.getOpenSegment()); - checkCache(1, 300, 100); - - cache.rollOpenSegment(true); - Assert.assertNotNull(cache.getOpenSegment()); - checkCache(1, 300, 100); - } - - @Test - public void testAppendEntry() throws Exception { - LogSegment closedSegment = prepareLogSegment(0, 99, false); - cache.addSegment(closedSegment); - - final SimpleOperation m = new SimpleOperation("m"); - try { - LogEntryProto entry = ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, 0); - cache.appendEntry(entry); - Assert.fail("the open segment is null"); - } catch (IllegalStateException ignored) { - } - - LogSegment openSegment = prepareLogSegment(100, 100, true); - cache.addSegment(openSegment); - for (long index = 101; index < 200; index++) { - LogEntryProto entry = ServerProtoUtils.toLogEntryProto(m.getLogEntryContent(), 0, index); - cache.appendEntry(entry); - } - - Assert.assertNotNull(cache.getOpenSegment()); - checkCache(0, 199, 100); - } - - @Test - public void testTruncate() throws Exception { - long start = 0; - for (int i = 0; i < 5; i++) { // 5 closed segments - LogSegment s = prepareLogSegment(start, start + 99, false); - cache.addSegment(s); - start += 100; - } - // add another open segment - LogSegment s = prepareLogSegment(start, start + 99, true); - cache.addSegment(s); - - long end = cache.getEndIndex(); - Assert.assertEquals(599, end); - int numOfSegments = 6; - // start truncation - for (int i = 0; i < 10; i++) { // truncate 10 times - // each time truncate 37 entries - end -= 37; - TruncationSegments ts = cache.truncate(end + 1); - checkCache(0, end, 100); - - // check TruncationSegments - int currentNum= (int) (end / 100 + 1); - if (currentNum < numOfSegments) { - Assert.assertEquals(1, ts.toDelete.length); - numOfSegments = currentNum; - } else { - Assert.assertEquals(0, ts.toDelete.length); - } - } - - // 230 entries remaining. truncate at the segment boundary - TruncationSegments ts = cache.truncate(200); - checkCache(0, 199, 100); - Assert.assertEquals(1, ts.toDelete.length); - Assert.assertEquals(200, ts.toDelete[0].startIndex); - Assert.assertEquals(229, ts.toDelete[0].endIndex); - Assert.assertEquals(0, ts.toDelete[0].targetLength); - Assert.assertFalse(ts.toDelete[0].isOpen); - Assert.assertNull(ts.toTruncate); - - // add another open segment and truncate it as a whole - LogSegment newOpen = prepareLogSegment(200, 249, true); - cache.addSegment(newOpen); - ts = cache.truncate(200); - checkCache(0, 199, 100); - Assert.assertEquals(1, ts.toDelete.length); - Assert.assertEquals(200, ts.toDelete[0].startIndex); - Assert.assertEquals(249, ts.toDelete[0].endIndex); - Assert.assertEquals(0, ts.toDelete[0].targetLength); - Assert.assertTrue(ts.toDelete[0].isOpen); - Assert.assertNull(ts.toTruncate); - - // add another open segment and truncate part of it - newOpen = prepareLogSegment(200, 249, true); - cache.addSegment(newOpen); - ts = cache.truncate(220); - checkCache(0, 219, 100); - Assert.assertNull(cache.getOpenSegment()); - Assert.assertEquals(0, ts.toDelete.length); - Assert.assertTrue(ts.toTruncate.isOpen); - Assert.assertEquals(219, ts.toTruncate.newEndIndex); - Assert.assertEquals(200, ts.toTruncate.startIndex); - Assert.assertEquals(249, ts.toTruncate.endIndex); - } - - private void testIterator(long startIndex) throws IOException { - Iterator<TermIndex> iterator = cache.iterator(startIndex); - TermIndex prev = null; - while (iterator.hasNext()) { - TermIndex termIndex = iterator.next(); - Assert.assertEquals(cache.getLogRecord(termIndex.getIndex()).getTermIndex(), termIndex); - if (prev != null) { - Assert.assertEquals(prev.getIndex() + 1, termIndex.getIndex()); - } - prev = termIndex; - } - if (startIndex <= cache.getEndIndex()) { - Assert.assertNotNull(prev); - Assert.assertEquals(cache.getEndIndex(), prev.getIndex()); - } - } - - @Test - public void testIterator() throws Exception { - long start = 0; - for (int i = 0; i < 2; i++) { // 2 closed segments - LogSegment s = prepareLogSegment(start, start + 99, false); - cache.addSegment(s); - start += 100; - } - // add another open segment - LogSegment s = prepareLogSegment(start, start + 99, true); - cache.addSegment(s); - - for (long startIndex = 0; startIndex < 300; startIndex += 50) { - testIterator(startIndex); - } - testIterator(299); - - Iterator<TermIndex> iterator = cache.iterator(300); - Assert.assertFalse(iterator.hasNext()); - } -}
