RATIS-105. Server should check group id for client requests. Contributed by Tsz Wo Nicholas Sze.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ratis/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ratis/commit/9ce1783d Tree: http://git-wip-us.apache.org/repos/asf/incubator-ratis/tree/9ce1783d Diff: http://git-wip-us.apache.org/repos/asf/incubator-ratis/diff/9ce1783d Branch: refs/heads/master Commit: 9ce1783d30726b940193e1926659fefdbe2a2cdb Parents: 4ed8f72 Author: Jing Zhao <[email protected]> Authored: Mon Sep 4 14:54:42 2017 -0700 Committer: Jing Zhao <[email protected]> Committed: Mon Sep 4 14:54:42 2017 -0700 ---------------------------------------------------------------------- .../ratis/client/impl/ClientProtoUtils.java | 4 +- .../ratis/client/impl/RaftClientImpl.java | 18 +- .../ratis/protocol/NotLeaderException.java | 2 +- .../org/apache/ratis/protocol/RaftPeer.java | 7 +- .../ratis/protocol/StateMachineException.java | 2 +- .../test/java/org/apache/ratis/BaseTest.java | 19 ++ .../ratis/grpc/MiniRaftClusterWithGRpc.java | 7 + .../grpc/TestNotLeaderExceptionWithGrpc.java | 31 --- .../ratis/grpc/TestRaftExceptionWithGrpc.java | 25 +++ .../ratis/hadooprpc/client/HadoopClientRpc.java | 3 +- .../hadooprpc/MiniRaftClusterWithHadoopRpc.java | 7 + .../TestNotLeaderExceptionWithHadoopRpc.java | 31 --- .../TestRaftExceptionWithHadoopRpc.java | 25 +++ .../ratis/netty/MiniRaftClusterWithNetty.java | 7 + .../netty/TestNotLeaderExceptionWithNetty.java | 31 --- .../ratis/netty/TestRaftExceptionWithNetty.java | 25 +++ .../ratis/server/impl/RaftServerImpl.java | 29 ++- .../ratis/server/impl/RaftServerProxy.java | 2 + .../apache/ratis/server/impl/RetryCache.java | 25 ++- .../apache/ratis/server/storage/RaftLog.java | 2 +- .../java/org/apache/ratis/MiniRaftCluster.java | 4 + .../org/apache/ratis/RaftExceptionBaseTest.java | 199 +++++++++++++++++++ .../ratis/RaftNotLeaderExceptionBaseTest.java | 170 ---------------- .../server/impl/ReinitializationBaseTest.java | 4 +- .../MiniRaftClusterWithSimulatedRpc.java | 7 + .../TestNotLeaderExceptionWithSimulation.java | 28 --- .../TestRaftExceptionWithSimulation.java | 25 +++ 27 files changed, 415 insertions(+), 324 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java ---------------------------------------------------------------------- diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java index e6621de..ea8c5ac 100644 --- a/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java +++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/ClientProtoUtils.java @@ -144,7 +144,7 @@ public class ClientProtoUtils { suggestedLeader, peers); } else if (replyProto.getExceptionDetailsCase().equals(STATEMACHINEEXCEPTION)) { StateMachineExceptionProto smeProto = replyProto.getStateMachineException(); - e = wrapStateMachineException(rp.getReplyId().toStringUtf8(), + e = wrapStateMachineException(RaftPeerId.valueOf(rp.getReplyId()), smeProto.getExceptionClassName(), smeProto.getErrorMsg(), smeProto.getStacktrace()); } @@ -156,7 +156,7 @@ public class ClientProtoUtils { } private static StateMachineException wrapStateMachineException( - String serverId, String className, String errorMsg, + RaftPeerId serverId, String className, String errorMsg, ByteString stackTraceBytes) { StateMachineException sme; if (className == null) { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java ---------------------------------------------------------------------- diff --git a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java index 69483ef..c25b9e0 100644 --- a/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java +++ b/ratis-client/src/main/java/org/apache/ratis/client/impl/RaftClientImpl.java @@ -27,10 +27,7 @@ import org.apache.ratis.protocol.*; import java.io.IOException; import java.io.InterruptedIOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; +import java.util.*; import java.util.concurrent.atomic.AtomicLong; import java.util.function.Supplier; import java.util.stream.Collectors; @@ -82,6 +79,8 @@ final class RaftClientImpl implements RaftClient { } private RaftClientReply send(Message message, boolean readOnly) throws IOException { + Objects.requireNonNull(message, "message == null"); + final long callId = nextCallId(); return sendRequestWithRetry(() -> new RaftClientRequest( clientId, leaderId, groupId, callId, message, readOnly)); @@ -90,6 +89,8 @@ final class RaftClientImpl implements RaftClient { @Override public RaftClientReply setConfiguration(RaftPeer[] peersInNewConf) throws IOException { + Objects.requireNonNull(peersInNewConf, "peersInNewConf == null"); + final long callId = nextCallId(); // also refresh the rpc proxies for these peers addServers(Arrays.stream(peersInNewConf)); @@ -100,6 +101,9 @@ final class RaftClientImpl implements RaftClient { @Override public RaftClientReply reinitialize(RaftGroup newGroup, RaftPeerId server) throws IOException { + Objects.requireNonNull(newGroup, "newGroup == null"); + Objects.requireNonNull(server, "server == null"); + final long callId = nextCallId(); addServers(newGroup.getPeers().stream()); return sendRequest(new ReinitializeRequest( @@ -113,7 +117,7 @@ final class RaftClientImpl implements RaftClient { private RaftClientReply sendRequestWithRetry( Supplier<RaftClientRequest> supplier) - throws InterruptedIOException, StateMachineException { + throws InterruptedIOException, StateMachineException, GroupMismatchException { for(;;) { final RaftClientRequest request = supplier.get(); final RaftClientReply reply = sendRequest(request); @@ -133,11 +137,13 @@ final class RaftClientImpl implements RaftClient { } private RaftClientReply sendRequest(RaftClientRequest request) - throws StateMachineException { + throws StateMachineException, GroupMismatchException { LOG.debug("{}: {}", clientId, request); RaftClientReply reply = null; try { reply = clientRpc.sendRequest(request); + } catch (GroupMismatchException gme) { + throw gme; } catch (IOException ioe) { handleIOException(request, ioe, null); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-common/src/main/java/org/apache/ratis/protocol/NotLeaderException.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/main/java/org/apache/ratis/protocol/NotLeaderException.java b/ratis-common/src/main/java/org/apache/ratis/protocol/NotLeaderException.java index 062f5de..453965e 100644 --- a/ratis-common/src/main/java/org/apache/ratis/protocol/NotLeaderException.java +++ b/ratis-common/src/main/java/org/apache/ratis/protocol/NotLeaderException.java @@ -27,7 +27,7 @@ public class NotLeaderException extends RaftException { super("Server " + id + " is not the leader (" + suggestedLeader + "). Request must be sent to leader."); this.suggestedLeader = suggestedLeader; - this.peers = peers == null ? RaftPeer.EMPTY_PEERS : peers; + this.peers = peers == null ? RaftPeer.emptyArray(): peers; } public RaftPeer getSuggestedLeader() { http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeer.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeer.java b/ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeer.java index 26c9c4c..68252f3 100644 --- a/ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeer.java +++ b/ratis-common/src/main/java/org/apache/ratis/protocol/RaftPeer.java @@ -28,7 +28,12 @@ import java.util.Objects; * The objects of this class are immutable. */ public class RaftPeer { - public static final RaftPeer[] EMPTY_PEERS = {}; + private static final RaftPeer[] EMPTY_ARRAY = {}; + + /** @return an empty array. */ + public static RaftPeer[] emptyArray() { + return EMPTY_ARRAY; + } /** The id of the peer. */ private final RaftPeerId id; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-common/src/main/java/org/apache/ratis/protocol/StateMachineException.java ---------------------------------------------------------------------- diff --git a/ratis-common/src/main/java/org/apache/ratis/protocol/StateMachineException.java b/ratis-common/src/main/java/org/apache/ratis/protocol/StateMachineException.java index 68d808b..49a64ef 100644 --- a/ratis-common/src/main/java/org/apache/ratis/protocol/StateMachineException.java +++ b/ratis-common/src/main/java/org/apache/ratis/protocol/StateMachineException.java @@ -18,7 +18,7 @@ package org.apache.ratis.protocol; public class StateMachineException extends RaftException { - public StateMachineException(String serverId, Throwable cause) { + public StateMachineException(RaftPeerId serverId, Throwable cause) { super(cause.getClass().getName() + " from Server " + serverId, cause); } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/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 659da0b..f77cc64 100644 --- a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java +++ b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java @@ -19,8 +19,10 @@ package org.apache.ratis; import org.apache.log4j.Level; import org.apache.ratis.conf.ConfUtils; +import org.apache.ratis.util.CheckedRunnable; import org.apache.ratis.util.JavaUtils; import org.apache.ratis.util.LogUtils; +import org.junit.Assert; import org.junit.Rule; import org.junit.rules.Timeout; import org.slf4j.Logger; @@ -69,4 +71,21 @@ public abstract class BaseTest { public File getTestDir() { return getClassTestDir(getClass()); } + + public static void testFailureCase( + String description, CheckedRunnable<?> testCode, + Class<? extends Throwable> exceptedThrowableClass, Logger log) { + try { + testCode.run(); + Assert.fail("The test \"" + description + "\" does not throw anything."); + } catch (Throwable t) { + log.info("The test \"" + description + "\" throws " + t.getClass().getSimpleName(), t); + Assert.assertEquals(exceptedThrowableClass, t.getClass()); + } + } + + public void testFailureCase( + String description, CheckedRunnable<?> testCode, Class<? extends Throwable> exceptedThrowableClass) { + testFailureCase(description, testCode, exceptedThrowableClass, LOG); + } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-grpc/src/test/java/org/apache/ratis/grpc/MiniRaftClusterWithGRpc.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/MiniRaftClusterWithGRpc.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/MiniRaftClusterWithGRpc.java index a5992b1..3580f22 100644 --- a/ratis-grpc/src/test/java/org/apache/ratis/grpc/MiniRaftClusterWithGRpc.java +++ b/ratis-grpc/src/test/java/org/apache/ratis/grpc/MiniRaftClusterWithGRpc.java @@ -41,6 +41,13 @@ public class MiniRaftClusterWithGRpc extends MiniRaftCluster.RpcBase { } }; + public interface FactoryGet extends Factory.Get<MiniRaftClusterWithGRpc> { + @Override + default Factory<MiniRaftClusterWithGRpc> getFactory() { + return FACTORY; + } + } + public static final DelayLocalExecutionInjection sendServerRequestInjection = new DelayLocalExecutionInjection(RaftGRpcService.GRPC_SEND_SERVER_REQUEST); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestNotLeaderExceptionWithGrpc.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestNotLeaderExceptionWithGrpc.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestNotLeaderExceptionWithGrpc.java deleted file mode 100644 index e9e20b0..0000000 --- a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestNotLeaderExceptionWithGrpc.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.grpc; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.RaftNotLeaderExceptionBaseTest; -import org.apache.ratis.conf.RaftProperties; - -import java.io.IOException; - -public class TestNotLeaderExceptionWithGrpc extends RaftNotLeaderExceptionBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithGRpc.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftExceptionWithGrpc.java ---------------------------------------------------------------------- diff --git a/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftExceptionWithGrpc.java b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftExceptionWithGrpc.java new file mode 100644 index 0000000..fc110ea --- /dev/null +++ b/ratis-grpc/src/test/java/org/apache/ratis/grpc/TestRaftExceptionWithGrpc.java @@ -0,0 +1,25 @@ +/** + * 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.grpc; + +import org.apache.ratis.RaftExceptionBaseTest; + +public class TestRaftExceptionWithGrpc + extends RaftExceptionBaseTest<MiniRaftClusterWithGRpc> + implements MiniRaftClusterWithGRpc.FactoryGet { +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-hadoop/src/main/java/org/apache/ratis/hadooprpc/client/HadoopClientRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/main/java/org/apache/ratis/hadooprpc/client/HadoopClientRpc.java b/ratis-hadoop/src/main/java/org/apache/ratis/hadooprpc/client/HadoopClientRpc.java index 3a2d6fc..c5a6ba9 100644 --- a/ratis-hadoop/src/main/java/org/apache/ratis/hadooprpc/client/HadoopClientRpc.java +++ b/ratis-hadoop/src/main/java/org/apache/ratis/hadooprpc/client/HadoopClientRpc.java @@ -54,7 +54,8 @@ public class HadoopClientRpc implements RaftClientRpc { ReconfigurationTimeoutException.class, ReconfigurationInProgressException.class, RaftException.class, - LeaderNotReadyException.class); + LeaderNotReadyException.class, + GroupMismatchException.class); } } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/MiniRaftClusterWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/MiniRaftClusterWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/MiniRaftClusterWithHadoopRpc.java index bc420f4..51b1d5d 100644 --- a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/MiniRaftClusterWithHadoopRpc.java +++ b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/MiniRaftClusterWithHadoopRpc.java @@ -39,6 +39,13 @@ public class MiniRaftClusterWithHadoopRpc extends MiniRaftCluster.RpcBase { static final Logger LOG = LoggerFactory.getLogger(MiniRaftClusterWithHadoopRpc.class); public static class Factory extends MiniRaftCluster.Factory<MiniRaftClusterWithHadoopRpc> { + public interface Get extends MiniRaftCluster.Factory.Get<MiniRaftClusterWithHadoopRpc> { + @Override + default MiniRaftCluster.Factory<MiniRaftClusterWithHadoopRpc> getFactory() { + return FACTORY; + } + } + @Override public MiniRaftClusterWithHadoopRpc newCluster(String[] ids, RaftProperties prop) { final Configuration conf = new Configuration(); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestNotLeaderExceptionWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestNotLeaderExceptionWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestNotLeaderExceptionWithHadoopRpc.java deleted file mode 100644 index 103a0b3..0000000 --- a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestNotLeaderExceptionWithHadoopRpc.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.hadooprpc; - -import org.apache.ratis.MiniRaftCluster; -import org.apache.ratis.RaftNotLeaderExceptionBaseTest; -import org.apache.ratis.conf.RaftProperties; - -import java.io.IOException; - -public class TestNotLeaderExceptionWithHadoopRpc extends RaftNotLeaderExceptionBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithHadoopRpc.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftExceptionWithHadoopRpc.java ---------------------------------------------------------------------- diff --git a/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftExceptionWithHadoopRpc.java b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftExceptionWithHadoopRpc.java new file mode 100644 index 0000000..c60e183 --- /dev/null +++ b/ratis-hadoop/src/test/java/org/apache/ratis/hadooprpc/TestRaftExceptionWithHadoopRpc.java @@ -0,0 +1,25 @@ +/** + * 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.hadooprpc; + +import org.apache.ratis.RaftExceptionBaseTest; + +public class TestRaftExceptionWithHadoopRpc + extends RaftExceptionBaseTest<MiniRaftClusterWithHadoopRpc> + implements MiniRaftClusterWithHadoopRpc.Factory.Get { +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-netty/src/test/java/org/apache/ratis/netty/MiniRaftClusterWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/MiniRaftClusterWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/MiniRaftClusterWithNetty.java index e6b19f8..3941f33 100644 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/MiniRaftClusterWithNetty.java +++ b/ratis-netty/src/test/java/org/apache/ratis/netty/MiniRaftClusterWithNetty.java @@ -40,6 +40,13 @@ public class MiniRaftClusterWithNetty extends MiniRaftCluster.RpcBase { } }; + public interface FactoryGet extends Factory.Get<MiniRaftClusterWithNetty> { + @Override + default Factory<MiniRaftClusterWithNetty> getFactory() { + return FACTORY; + } + } + public static final DelayLocalExecutionInjection sendServerRequest = new DelayLocalExecutionInjection(NettyRpcService.SEND_SERVER_REQUEST); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-netty/src/test/java/org/apache/ratis/netty/TestNotLeaderExceptionWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestNotLeaderExceptionWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestNotLeaderExceptionWithNetty.java deleted file mode 100644 index 6e4ed12..0000000 --- a/ratis-netty/src/test/java/org/apache/ratis/netty/TestNotLeaderExceptionWithNetty.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.RaftNotLeaderExceptionBaseTest; -import org.apache.ratis.conf.RaftProperties; - -import java.io.IOException; - -public class TestNotLeaderExceptionWithNetty extends RaftNotLeaderExceptionBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithNetty.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftExceptionWithNetty.java ---------------------------------------------------------------------- diff --git a/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftExceptionWithNetty.java b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftExceptionWithNetty.java new file mode 100644 index 0000000..3414a65 --- /dev/null +++ b/ratis-netty/src/test/java/org/apache/ratis/netty/TestRaftExceptionWithNetty.java @@ -0,0 +1,25 @@ +/** + * 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.RaftExceptionBaseTest; + +public class TestRaftExceptionWithNetty + extends RaftExceptionBaseTest<MiniRaftClusterWithNetty> + implements MiniRaftClusterWithNetty.FactoryGet { +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java index 4463914..2d38fe7 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerImpl.java @@ -351,25 +351,19 @@ public class RaftServerImpl implements RaftServerProtocol, */ private CompletableFuture<RaftClientReply> checkLeaderState( RaftClientRequest request, RetryCache.CacheEntry entry) { + try { + assertGroup(request.getRequestorId(), request.getRaftGroupId()); + } catch (GroupMismatchException e) { + return RetryCache.failWithException(e, entry); + } + if (!isLeader()) { NotLeaderException exception = generateNotLeaderException(); final RaftClientReply reply = new RaftClientReply(request, exception); - if (entry != null) { - entry.failWithReply(reply); - } - return entry != null ? - entry.getReplyFuture() : CompletableFuture.completedFuture(reply); + return RetryCache.failWithReply(reply, entry); } else { if (leaderState == null || !leaderState.isReady()) { - final Exception e = new LeaderNotReadyException(); - if (entry != null) { - entry.failWithException(e); - return entry.getReplyFuture(); - } else { - CompletableFuture<RaftClientReply> future = new CompletableFuture<>(); - future.completeExceptionally(e); - return future; - } + return RetryCache.failWithException(new LeaderNotReadyException(), entry); } } return null; @@ -476,7 +470,7 @@ public class RaftServerImpl implements RaftServerProtocol, TransactionContext context = stateMachine.startTransaction(request); if (context.getException() != null) { RaftClientReply exceptionReply = new RaftClientReply(request, - new StateMachineException(getId().toString(), context.getException())); + new StateMachineException(getId(), context.getException())); cacheEntry.failWithReply(exceptionReply); return CompletableFuture.completedFuture(exceptionReply); } @@ -526,6 +520,8 @@ public class RaftServerImpl implements RaftServerProtocol, SetConfigurationRequest request) throws IOException { LOG.debug("{}: receive setConfiguration({})", getId(), request); assertLifeCycleState(RUNNING); + assertGroup(request.getRequestorId(), request.getRaftGroupId()); + CompletableFuture<RaftClientReply> reply = checkLeaderState(request, null); if (reply != null) { return reply; @@ -914,8 +910,7 @@ public class RaftServerImpl implements RaftServerProtocol, } else { // the exception is coming from the state machine. wrap it into the // reply as a StateMachineException - final StateMachineException e = new StateMachineException( - getId().toString(), exception); + final StateMachineException e = new StateMachineException(getId(), exception); r = new RaftClientReply(clientId, serverId, groupId, callId, false, null, e); } // update retry cache http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java index 9be1e9a..554372c 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RaftServerProxy.java @@ -158,9 +158,11 @@ public class RaftServerProxy implements RaftServer { @Override public CompletableFuture<RaftClientReply> reinitializeAsync( ReinitializeRequest request) throws IOException { + getImpl().assertGroup(request.getRequestorId(), request.getRaftGroupId()); if (!reinitializeRequest.compareAndSet(null, request)) { throw new IOException("Another reinitialize is already in progress."); } + return CompletableFuture.supplyAsync(() -> { try { final CompletableFuture<RaftServerImpl> oldImpl = impl; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java b/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java index bf2e94c..4e65124 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/impl/RetryCache.java @@ -80,7 +80,7 @@ public class RetryCache implements Closeable { /** * "failed" means we failed to commit the request into the raft group, or * the request did not get approved by the state machine before the raft - * replication. Not once the request gets committed by the raft group, this + * replication. Note once the request gets committed by the raft group, this * field is never true even if the state machine throws an exception when * applying the transaction. */ @@ -222,4 +222,27 @@ public class RetryCache implements Closeable { cache.invalidateAll(); } } + + static CompletableFuture<RaftClientReply> failWithReply( + RaftClientReply reply, CacheEntry entry) { + if (entry != null) { + entry.failWithReply(reply); + return entry.getReplyFuture(); + } else { + return CompletableFuture.completedFuture(reply); + } + } + + static CompletableFuture<RaftClientReply> failWithException( + Throwable t, CacheEntry entry) { + if (entry != null) { + entry.failWithException(t); + return entry.getReplyFuture(); + } else { + final CompletableFuture<RaftClientReply> future = new CompletableFuture<>(); + future.completeExceptionally(t); + return future; + } + } + } http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java index ee765c1..ac86582 100644 --- a/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java +++ b/ratis-server/src/main/java/org/apache/ratis/server/storage/RaftLog.java @@ -136,7 +136,7 @@ public abstract class RaftLog implements Closeable { try { operation = operation.preAppendTransaction(); } catch (IOException e) { - throw new StateMachineException(selfId.toString(), e); + throw new StateMachineException(selfId, e); } // build the log entry after calling the StateMachine http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/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 79cb9bb..a50c0d7 100644 --- a/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java +++ b/ratis-server/src/test/java/org/apache/ratis/MiniRaftCluster.java @@ -56,6 +56,10 @@ public abstract class MiniRaftCluster { public static final Class<? extends StateMachine> STATEMACHINE_CLASS_DEFAULT = BaseStateMachine.class; public static abstract class Factory<CLUSTER extends MiniRaftCluster> { + public interface Get<CLUSTER extends MiniRaftCluster> { + Factory<CLUSTER> getFactory(); + } + public abstract CLUSTER newCluster( String[] ids, RaftProperties prop); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/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 new file mode 100644 index 0000000..eceaf20 --- /dev/null +++ b/ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java @@ -0,0 +1,199 @@ +/** + * 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.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 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> { + static { + LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftLog.LOG, Level.DEBUG); + LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); + } + + public static final int NUM_PEERS = 5; + + private CLUSTER cluster; + + @Before + public void setup() throws IOException { + cluster = getFactory().newCluster(NUM_PEERS, new RaftProperties()); + cluster.start(); + } + + @After + public void tearDown() { + if (cluster != null) { + cluster.shutdown(); + } + } + + @Test + public void testHandleNotLeaderException() throws Exception { + testHandleNotLeaderException(false); + } + + /** + * Test handle both IOException and NotLeaderException + */ + @Test + public void testHandleNotLeaderAndIOException() throws Exception { + testHandleNotLeaderException(true); + } + + private void testHandleNotLeaderException(boolean killNewLeader) + throws Exception { + RaftTestUtil.waitForLeader(cluster); + final RaftPeerId leaderId = cluster.getLeader().getId(); + final RaftClient client = cluster.createClient(leaderId); + + RaftClientReply reply = client.send(new SimpleMessage("m1")); + Assert.assertTrue(reply.isSuccess()); + + // enforce leader change + RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId); + Assert.assertNotEquals(leaderId, newLeader); + + if (killNewLeader) { + // kill the new leader + cluster.killServer(newLeader); + } + + RaftClientRpc rpc = client.getClientRpc(); + 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"))); + } catch (IOException ignored) { + Thread.sleep(1000); + } + } + Assert.assertNotNull(reply); + Assert.assertFalse(reply.isSuccess()); + Assert.assertTrue(reply.isNotLeader()); + Assert.assertEquals(newLeader, + reply.getNotLeaderException().getSuggestedLeader().getId()); + + reply = client.send(new SimpleMessage("m3")); + Assert.assertTrue(reply.isSuccess()); + client.close(); + } + + @Test + public void testNotLeaderExceptionWithReconf() throws Exception { + Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster)); + + final RaftPeerId leaderId = cluster.getLeader().getId(); + final RaftClient client = cluster.createClient(leaderId); + + // enforce leader change + RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId); + Assert.assertNotEquals(leaderId, newLeader); + + // also add two new peers + // add two more peers + MiniRaftCluster.PeerChanges change = cluster.addNewPeers( + new String[]{"ss1", "ss2"}, true); + // trigger setConfiguration + LOG.info("Start changing the configuration: {}", + Arrays.asList(change.allPeersInNewConf)); + try(final RaftClient c2 = cluster.createClient(newLeader)) { + RaftClientReply reply = c2.setConfiguration(change.allPeersInNewConf); + Assert.assertTrue(reply.isSuccess()); + } + LOG.info(cluster.printServers()); + + RaftClientRpc rpc = client.getClientRpc(); + RaftClientReply reply = null; + // 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"))); + } catch (IOException ignored) { + Thread.sleep(1000); + } + } + Assert.assertNotNull(reply); + Assert.assertFalse(reply.isSuccess()); + Assert.assertTrue(reply.isNotLeader()); + Assert.assertEquals(newLeader, + reply.getNotLeaderException().getSuggestedLeader().getId()); + Collection<RaftPeer> peers = cluster.getPeers(); + RaftPeer[] peersFromReply = reply.getNotLeaderException().getPeers(); + Assert.assertEquals(peers.size(), peersFromReply.length); + for (RaftPeer p : peersFromReply) { + Assert.assertTrue(peers.contains(p)); + } + + reply = client.send(new SimpleMessage("m2")); + Assert.assertTrue(reply.isSuccess()); + client.close(); + } + + @Test + public void testGroupMismatchException() throws Exception { + final RaftGroup clusterGroup = cluster.getGroup(); + Assert.assertEquals(NUM_PEERS, clusterGroup.getPeers().size()); + + final RaftGroup anotherGroup = new RaftGroup(RaftGroupId.randomId(), clusterGroup.getPeers()); + Assert.assertNotEquals(clusterGroup.getGroupId(), anotherGroup.getGroupId()); + + // Create client using another group + try(RaftClient client = cluster.createClient(anotherGroup)) { + testFailureCase("send(..) with client group being different from the server group", + () -> client.send(() -> ByteString.EMPTY), + GroupMismatchException.class); + + testFailureCase("sendReadOnly(..) with client group being different from the server group", + () -> client.sendReadOnly(() -> ByteString.EMPTY), + GroupMismatchException.class); + + testFailureCase("setConfiguration(..) with client group being different from the server group", + () -> client.setConfiguration(RaftPeer.emptyArray()), + GroupMismatchException.class); + + testFailureCase("reinitialize(..) with client group being different from the server group", + () -> client.reinitialize(anotherGroup, clusterGroup.getPeers().get(0).getId()), + GroupMismatchException.class); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java deleted file mode 100644 index 583aac7..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/RaftNotLeaderExceptionBaseTest.java +++ /dev/null @@ -1,170 +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.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.util.LogUtils; -import org.junit.*; - -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 RaftNotLeaderExceptionBaseTest extends BaseTest { - static { - LogUtils.setLogLevel(RaftServerImpl.LOG, Level.DEBUG); - LogUtils.setLogLevel(RaftLog.LOG, Level.DEBUG); - LogUtils.setLogLevel(RaftClient.LOG, Level.DEBUG); - } - - public static final int NUM_PEERS = 5; - - private MiniRaftCluster cluster; - - public abstract MiniRaftCluster.Factory<?> getFactory(); - - @Before - public void setup() throws IOException { - cluster = getFactory().newCluster(NUM_PEERS, new RaftProperties()); - cluster.start(); - } - - @After - public void tearDown() { - if (cluster != null) { - cluster.shutdown(); - } - } - - @Test - public void testHandleNotLeaderException() throws Exception { - testHandleNotLeaderException(false); - } - - /** - * Test handle both IOException and NotLeaderException - */ - @Test - public void testHandleNotLeaderAndIOException() throws Exception { - testHandleNotLeaderException(true); - } - - private void testHandleNotLeaderException(boolean killNewLeader) - throws Exception { - RaftTestUtil.waitForLeader(cluster); - final RaftPeerId leaderId = cluster.getLeader().getId(); - final RaftClient client = cluster.createClient(leaderId); - - RaftClientReply reply = client.send(new SimpleMessage("m1")); - Assert.assertTrue(reply.isSuccess()); - - // enforce leader change - RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId); - Assert.assertNotEquals(leaderId, newLeader); - - if (killNewLeader) { - // kill the new leader - cluster.killServer(newLeader); - } - - RaftClientRpc rpc = client.getClientRpc(); - 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"))); - } catch (IOException ignored) { - Thread.sleep(1000); - } - } - Assert.assertNotNull(reply); - Assert.assertFalse(reply.isSuccess()); - Assert.assertTrue(reply.isNotLeader()); - Assert.assertEquals(newLeader, - reply.getNotLeaderException().getSuggestedLeader().getId()); - - reply = client.send(new SimpleMessage("m3")); - Assert.assertTrue(reply.isSuccess()); - client.close(); - } - - @Test - public void testNotLeaderExceptionWithReconf() throws Exception { - Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster)); - - final RaftPeerId leaderId = cluster.getLeader().getId(); - final RaftClient client = cluster.createClient(leaderId); - - // enforce leader change - RaftPeerId newLeader = RaftTestUtil.changeLeader(cluster, leaderId); - Assert.assertNotEquals(leaderId, newLeader); - - // also add two new peers - // add two more peers - MiniRaftCluster.PeerChanges change = cluster.addNewPeers( - new String[]{"ss1", "ss2"}, true); - // trigger setConfiguration - LOG.info("Start changing the configuration: {}", - Arrays.asList(change.allPeersInNewConf)); - try(final RaftClient c2 = cluster.createClient(newLeader)) { - RaftClientReply reply = c2.setConfiguration(change.allPeersInNewConf); - Assert.assertTrue(reply.isSuccess()); - } - LOG.info(cluster.printServers()); - - RaftClientRpc rpc = client.getClientRpc(); - RaftClientReply reply = null; - // 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"))); - } catch (IOException ignored) { - Thread.sleep(1000); - } - } - Assert.assertNotNull(reply); - Assert.assertFalse(reply.isSuccess()); - Assert.assertTrue(reply.isNotLeader()); - Assert.assertEquals(newLeader, - reply.getNotLeaderException().getSuggestedLeader().getId()); - Collection<RaftPeer> peers = cluster.getPeers(); - RaftPeer[] peersFromReply = reply.getNotLeaderException().getPeers(); - Assert.assertEquals(peers.size(), peersFromReply.length); - for (RaftPeer p : peersFromReply) { - Assert.assertTrue(peers.contains(p)); - } - - reply = client.send(new SimpleMessage("m2")); - Assert.assertTrue(reply.isSuccess()); - client.close(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/test/java/org/apache/ratis/server/impl/ReinitializationBaseTest.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/ReinitializationBaseTest.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/ReinitializationBaseTest.java index d9068d1..251a4d3 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/impl/ReinitializationBaseTest.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/ReinitializationBaseTest.java @@ -149,7 +149,7 @@ public abstract class ReinitializationBaseTest extends BaseTest { for (int i = 0; i < idIndex.length; i++) { final RaftGroupId gid = RaftGroupId.randomId(); final int previous = i == 0 ? 0 : idIndex[i - 1]; - final RaftPeer[] peers = allPeers.subList(previous, idIndex[i]).toArray(RaftPeer.EMPTY_PEERS); + final RaftPeer[] peers = allPeers.subList(previous, idIndex[i]).toArray(RaftPeer.emptyArray()); groups[i] = new RaftGroup(gid, peers); LOG.info(i + ") starting " + groups[i]); @@ -196,7 +196,7 @@ public abstract class ReinitializationBaseTest extends BaseTest { } LOG.info(chosen + ") setConfiguration: " + cluster.printServers(groups[chosen].getGroupId())); try (final RaftClient client = cluster.createClient(groups[chosen])) { - client.setConfiguration(allPeers.toArray(RaftPeer.EMPTY_PEERS)); + client.setConfiguration(allPeers.toArray(RaftPeer.emptyArray())); } Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true)); http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/test/java/org/apache/ratis/server/simulation/MiniRaftClusterWithSimulatedRpc.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/MiniRaftClusterWithSimulatedRpc.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/MiniRaftClusterWithSimulatedRpc.java index b3b679f..c2851d1 100644 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/MiniRaftClusterWithSimulatedRpc.java +++ b/ratis-server/src/test/java/org/apache/ratis/server/simulation/MiniRaftClusterWithSimulatedRpc.java @@ -59,6 +59,13 @@ public class MiniRaftClusterWithSimulatedRpc extends MiniRaftCluster { } }; + public interface FactoryGet extends Factory.Get<MiniRaftClusterWithSimulatedRpc> { + @Override + default Factory<MiniRaftClusterWithSimulatedRpc> getFactory() { + return FACTORY; + } + } + private final SimulatedRequestReply<RaftServerRequest, RaftServerReply> serverRequestReply; private final SimulatedClientRpc client2serverRequestReply; http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestNotLeaderExceptionWithSimulation.java ---------------------------------------------------------------------- diff --git a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestNotLeaderExceptionWithSimulation.java b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestNotLeaderExceptionWithSimulation.java deleted file mode 100644 index c37e2fb..0000000 --- a/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestNotLeaderExceptionWithSimulation.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.RaftNotLeaderExceptionBaseTest; - -public class TestNotLeaderExceptionWithSimulation extends RaftNotLeaderExceptionBaseTest { - @Override - public MiniRaftCluster.Factory<?> getFactory() { - return MiniRaftClusterWithSimulatedRpc.FACTORY; - } -} http://git-wip-us.apache.org/repos/asf/incubator-ratis/blob/9ce1783d/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 new file mode 100644 index 0000000..f328ea3 --- /dev/null +++ b/ratis-server/src/test/java/org/apache/ratis/server/simulation/TestRaftExceptionWithSimulation.java @@ -0,0 +1,25 @@ +/* + * 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 { +}
