diff --git a/changes/en-us/2.0.0.md b/changes/en-us/2.0.0.md index 549ef253ec7..0d98463ba17 100644 --- a/changes/en-us/2.0.0.md +++ b/changes/en-us/2.0.0.md @@ -177,6 +177,7 @@ The version is updated as follows: - [[#6001](https://github.com/seata/seata/pull/6001)] add test cases for RaftMsgExecute under branch package - [[#5996](https://github.com/seata/seata/pull/5996)] add test cases for RaftMsgExecute under global package - [[#6003](https://github.com/seata/seata/pull/6003)] add test cases for RaftMsgExecute under lock package +- [[#6009](https://github.com/seata/seata/pull/6009)] add test cases for RaftServerFactory ### Contributors: diff --git a/changes/zh-cn/2.0.0.md b/changes/zh-cn/2.0.0.md index a8fbac39e56..64e6021c24d 100644 --- a/changes/zh-cn/2.0.0.md +++ b/changes/zh-cn/2.0.0.md @@ -178,6 +178,7 @@ Seata 是一款开源的分布式事务解决方案,提供高性能和简单 - [[#6001](https://github.com/seata/seata/pull/6001)] 添加 RaftMsgExecute 模块 branch 包下的单元测试用例 - [[#5996](https://github.com/seata/seata/pull/5996)] 添加 RaftMsgExecute 模块 global 包下的单元测试用例 - [[#6003](https://github.com/seata/seata/pull/6003)] 添加 RaftMsgExecute 模块 lock 包下的单元测试用例 +- [[#6009](https://github.com/seata/seata/pull/6009)] 添加RaftServerFactory的单元测试用例 ### Contributors: diff --git a/server/src/main/java/io/seata/server/cluster/raft/RaftServerFactory.java b/server/src/main/java/io/seata/server/cluster/raft/RaftServerFactory.java index 245dd07d047..a5b74d2518b 100644 --- a/server/src/main/java/io/seata/server/cluster/raft/RaftServerFactory.java +++ b/server/src/main/java/io/seata/server/cluster/raft/RaftServerFactory.java @@ -89,14 +89,7 @@ public static CliClientService getCliClientServiceInstance() { public void init() { String initConfStr = CONFIG.getConfig(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR); StoreConfig.SessionMode storeMode = StoreConfig.getSessionMode(); - if (storeMode.equals(StoreConfig.SessionMode.RAFT)) { - for (RegistryService instance : MultiRegistryFactory.getInstances()) { - if (!(instance instanceof FileRegistryServiceImpl)) { - throw new IllegalArgumentException("Raft store mode not support other Registration Center"); - } - } - raftMode = true; - } + raftMode = storeMode.equals(StoreConfig.SessionMode.RAFT); if (StringUtils.isBlank(initConfStr)) { if (raftMode) { throw new IllegalArgumentException( @@ -104,6 +97,13 @@ public void init() { } return; } else { + if (raftMode) { + for (RegistryService instance : MultiRegistryFactory.getInstances()) { + if (!(instance instanceof FileRegistryServiceImpl)) { + throw new IllegalArgumentException("Raft store mode not support other Registration Center"); + } + } + } LOGGER.warn("raft mode and raft cluster is an experimental feature"); } final Configuration initConf = new Configuration(); @@ -161,7 +161,7 @@ public void start() { public void destroy() { this.close(); rpcServer = null; - RAFT_SERVER_MAP.clear(); + raftMode = false; } @Override @@ -171,6 +171,7 @@ public void close() { LOGGER.info("closed seata server raft cluster, group: {} ", group); }); Optional.ofNullable(rpcServer).ifPresent(RpcServer::shutdown); + RAFT_SERVER_MAP.clear(); } public RaftServer getRaftServer(String group) { diff --git a/server/src/main/java/io/seata/server/cluster/raft/RaftStateMachine.java b/server/src/main/java/io/seata/server/cluster/raft/RaftStateMachine.java index d99c75b2646..6c6114cb073 100644 --- a/server/src/main/java/io/seata/server/cluster/raft/RaftStateMachine.java +++ b/server/src/main/java/io/seata/server/cluster/raft/RaftStateMachine.java @@ -39,8 +39,6 @@ import io.seata.common.metadata.Node; import io.seata.common.store.StoreMode; import io.seata.common.util.StringUtils; -import io.seata.config.ConfigurationFactory; -import io.seata.common.ConfigurationKeys; import io.seata.server.cluster.raft.context.SeataClusterContext; import io.seata.server.cluster.raft.snapshot.metadata.LeaderMetadataSnapshotFile; import io.seata.server.cluster.raft.snapshot.session.SessionSnapshotFile; @@ -114,7 +112,7 @@ public boolean isLeader() { public RaftStateMachine(String group) { this.group = group; - mode = ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_MODE); + mode = StoreConfig.getSessionMode().getName(); EXECUTES.put(REFRESH_CLUSTER_METADATA, syncMsg -> { refreshClusterMetadata(syncMsg); return null; diff --git a/server/src/test/java/io/seata/server/raft/RaftServerTest.java b/server/src/test/java/io/seata/server/raft/RaftServerTest.java new file mode 100644 index 00000000000..0ce2d935980 --- /dev/null +++ b/server/src/test/java/io/seata/server/raft/RaftServerTest.java @@ -0,0 +1,83 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * 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. + */ +package io.seata.server.raft; + +import io.seata.common.ConfigurationKeys; +import io.seata.common.XID; +import io.seata.config.Configuration; +import io.seata.config.ConfigurationCache; +import io.seata.config.ConfigurationFactory; +import io.seata.server.cluster.raft.RaftServerFactory; +import io.seata.server.lock.LockerManagerFactory; +import io.seata.server.session.SessionHolder; +import io.seata.server.store.StoreConfig; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +@SpringBootTest +public class RaftServerTest { + + @BeforeAll + public static void setUp(ApplicationContext context) { + LockerManagerFactory.destroy(); + SessionHolder.destroy(); + } + + @AfterEach + public void destroy() { + System.setProperty("server.raftPort", "0"); + System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR, ""); + ConfigurationCache.clear(); + StoreConfig.setStartupParameter("file", "file", "file"); + LockerManagerFactory.destroy(); + SessionHolder.destroy(); + } + + @Test + public void initRaftServerStart() { + System.setProperty("server.raftPort", "9091"); + System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR, + XID.getIpAddress() + ":9091" + "," + XID.getIpAddress() + ":9092" + "," + XID.getIpAddress() + ":9093"); + StoreConfig.setStartupParameter("raft", "raft", "raft"); + Assertions.assertDoesNotThrow(() -> RaftServerFactory.getInstance().init()); + Assertions.assertNotNull(RaftServerFactory.getInstance().getRaftServer("default")); + Assertions.assertNotNull(RaftServerFactory.groups()); + Assertions.assertNotNull(RaftServerFactory.getCliServiceInstance()); + Assertions.assertNotNull(RaftServerFactory.getCliClientServiceInstance()); + Assertions.assertEquals(RaftServerFactory.getInstance().isLeader("default"), false); + RaftServerFactory.getInstance().start(); + } + + @Test + public void initRaftServerFail() { + StoreConfig.setStartupParameter("raft", "raft", "raft"); + Assertions.assertThrows(IllegalArgumentException.class, () -> RaftServerFactory.getInstance().init()); + } + + @Test + public void initRaftServerFailByRaftPortNull() { + System.setProperty(ConfigurationKeys.SERVER_RAFT_SERVER_ADDR, + XID.getIpAddress() + ":9091" + "," + XID.getIpAddress() + ":9092" + "," + XID.getIpAddress() + ":9093"); + StoreConfig.setStartupParameter("raft", "raft", "raft"); + Assertions.assertThrows(IllegalArgumentException.class, () -> RaftServerFactory.getInstance().init()); + } + +}