From 5f2acb5597b5e9818c1b2bf0de6bf010b396eb02 Mon Sep 17 00:00:00 2001 From: jimin Date: Wed, 28 Sep 2022 17:13:01 +0800 Subject: [PATCH] optimize: optimize read of storage configuration (#4936) --- changes/en-us/develop.md | 4 + changes/zh-cn/develop.md | 4 + .../java/io/seata/core/lock/LockMode.java | 36 ---- .../java/io/seata/core/store/StoreMode.java | 77 -------- dependencies/pom.xml | 14 +- .../config/test/ApolloPropertiesTest.java | 2 +- .../config/test/ConfigPropertiesTest.java | 2 +- .../config/test/ConsulPropertiesTest.java | 2 +- .../config/test/CustomPropertiesTest.java | 2 +- .../config/test/Etcd3PropertiesTest.java | 2 +- .../config/test/FilePropertiesTest.java | 2 +- .../config/test/NacosPropertiesTest.java | 2 +- .../config/test/ZooKeeperPropertiesTest.java | 2 +- .../java/io/seata/server/ParameterParser.java | 30 +-- .../src/main/java/io/seata/server/Server.java | 7 +- .../server/ServerApplicationListener.java | 12 +- .../server/lock/LockerManagerFactory.java | 19 +- .../seata/server/session/SessionHelper.java | 8 +- .../seata/server/session/SessionHolder.java | 64 +++--- .../io/seata/server/store/StoreConfig.java | 187 +++++++++++++++++- .../io/seata/server/ParameterParserTest.java | 2 +- .../coordinator/DefaultCoordinatorTest.java | 4 +- .../server/coordinator/DefaultCoreTest.java | 3 +- .../event/DefaultCoreForEventBusTest.java | 10 +- .../io/seata/server/lock/LockManagerTest.java | 3 +- .../session/FileSessionManagerTest.java | 3 +- .../server/session/SessionHolderTest.java | 5 +- .../redis/RedisDistributedLockerTest.java | 20 +- .../seata/server/store/SessionStoreTest.java | 25 +-- .../saga/engine/db/AbstractServerTest.java | 6 +- 30 files changed, 309 insertions(+), 250 deletions(-) delete mode 100644 core/src/main/java/io/seata/core/lock/LockMode.java delete mode 100644 core/src/main/java/io/seata/core/store/StoreMode.java diff --git a/changes/en-us/develop.md b/changes/en-us/develop.md index 95cf74db4f6..5b44d9f08d4 100644 --- a/changes/en-us/develop.md +++ b/changes/en-us/develop.md @@ -18,6 +18,7 @@ Add changes here for all PR submitted to the develop branch. - [[#4543](https://github.com/seata/seata/pull/4543)] fix support Oracle nclob types - [[#4915](https://github.com/seata/seata/pull/4915)] fix failed to get server recovery properties - [[#4919](https://github.com/seata/seata/pull/4919)] fix XID port and address null:0 before coordinator.init +- [[#4928](https://github.com/seata/seata/pull/4928)] fix rpcContext.getClientRMHolderMap NPE ### optimize: @@ -37,6 +38,9 @@ Add changes here for all PR submitted to the develop branch. - [[#4917](https://github.com/seata/seata/pull/4917)] upgrade package-lock.json fix vulnerabilities - [[#4924](https://github.com/seata/seata/pull/4924)] optimize pom dependencies - [[#4932](https://github.com/seata/seata/pull/4932)] extract the default values for some properties +- [[#4925](https://github.com/seata/seata/pull/4925)] optimize java doc warning +- [[#4921](https://github.com/seata/seata/pull/4921)] fix some vulnerabilities in console and upgrade skywalking-eyes +- [[#4936](https://github.com/seata/seata/pull/4936)] optimize read of storage configuration - [[#4946](https://github.com/seata/seata/pull/4946)] pass the sqlexception to client when get lock - [[#4962](https://github.com/seata/seata/pull/4962)] optimize build and fix the base image diff --git a/changes/zh-cn/develop.md b/changes/zh-cn/develop.md index df8840619a3..064577df7ce 100644 --- a/changes/zh-cn/develop.md +++ b/changes/zh-cn/develop.md @@ -18,6 +18,7 @@ - [[#4543](https://github.com/seata/seata/pull/4543)] 修复对 Oracle 数据类型nclob的支持 - [[#4915](https://github.com/seata/seata/pull/4915)] 修复获取不到ServerRecoveryProperties属性的问题 - [[#4919](https://github.com/seata/seata/pull/4919)] 修复XID的port和address出现null:0的情况 +- [[#4928](https://github.com/seata/seata/pull/4928)] 修复 rpcContext.getClientRMHolderMap NPE 问题 ### optimize: - [[#4774](https://github.com/seata/seata/pull/4774)] 优化 seataio/seata-server 镜像中的 mysql8 依赖 @@ -36,6 +37,9 @@ - [[#4917](https://github.com/seata/seata/pull/4917)] 升级 package-lock.json 修复漏洞 - [[#4924](https://github.com/seata/seata/pull/4924)] 优化 pom 依赖 - [[#4932](https://github.com/seata/seata/pull/4932)] 抽取部分配置的默认值 +- [[#4925](https://github.com/seata/seata/pull/4925)] 优化 javadoc 注释 +- [[#4921](https://github.com/seata/seata/pull/4921)] 修复控制台模块安全漏洞和升级 skywalking-eyes 版本 +- [[#4936](https://github.com/seata/seata/pull/4936)] 优化存储配置的读取 - [[#4946](https://github.com/seata/seata/pull/4946)] 将获取锁时遇到的sql异常传递给客户端 - [[#4962](https://github.com/seata/seata/pull/4962)] 优化构建配置,并修正docker镜像的基础镜像 diff --git a/core/src/main/java/io/seata/core/lock/LockMode.java b/core/src/main/java/io/seata/core/lock/LockMode.java deleted file mode 100644 index 27e9b12d38d..00000000000 --- a/core/src/main/java/io/seata/core/lock/LockMode.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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.core.lock; - -/** - * lock mode - * - * @author zhangsen - */ -public enum LockMode { - - /** - * store the lock in user's database - */ - LOCAL, - - /** - * store the lock in seata's server - */ - REMOTE; - - -} diff --git a/core/src/main/java/io/seata/core/store/StoreMode.java b/core/src/main/java/io/seata/core/store/StoreMode.java deleted file mode 100644 index be22a72ecf4..00000000000 --- a/core/src/main/java/io/seata/core/store/StoreMode.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * 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.core.store; - -/** - * transaction log store mode - * - * @author zhangsen - */ -public enum StoreMode { - - /** - * file store - */ - FILE("file"), - - /** - * database store - */ - DB("db"), - - /** - * redis store - */ - REDIS("redis"); - - private String name; - - StoreMode(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - /** - * get value of store mode - * @param name the mode name - * @return the store mode - */ - public static StoreMode get(String name) { - for (StoreMode sm : StoreMode.class.getEnumConstants()) { - if (sm.name.equalsIgnoreCase(name)) { - return sm; - } - } - throw new IllegalArgumentException("unknown store mode:" + name); - } - - /** - * whether contains value of store mode - * @param name the mode name - * @return the boolean - */ - public static boolean contains(String name) { - try { - return get(name) != null ? true : false; - } catch (IllegalArgumentException e) { - return false; - } - } - -} diff --git a/dependencies/pom.xml b/dependencies/pom.xml index cb61afd5492..83f2f47da22 100644 --- a/dependencies/pom.xml +++ b/dependencies/pom.xml @@ -48,7 +48,7 @@ 3.5.9 2.9.1 1.0.2 - 1.6.0 + 2.0.1 0.1.16 1.10.17 1.4.2 @@ -63,7 +63,7 @@ 1.72 1.8 1.19 - 1.10.6 + 1.10.12 1.7.1 3.7.1 @@ -295,6 +295,12 @@ com.alipay.sofa registry-client-all ${sofa.registry.version} + + + com.alipay.sofa.lookout + lookout-api + + com.alipay.sofa @@ -353,6 +359,10 @@ javax.servlet servlet-api + + com.github.andrewoma.dexx + dexx-collections + diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ApolloPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ApolloPropertiesTest.java index 7e4cd17cb20..cc8425ad3df 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ApolloPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ApolloPropertiesTest.java @@ -37,7 +37,7 @@ @Import(SpringApplicationContextProvider.class) class ApolloPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigApolloProperties") public ConfigApolloProperties configApolloProperties() { return new ConfigApolloProperties().setApolloMeta(STR_TEST_AAA).setApolloAccessKeySecret(STR_TEST_BBB).setAppId( STR_TEST_CCC).setNamespace(STR_TEST_DDD).setCluster(STR_TEST_EEE).setApolloConfigService(STR_TEST_FFF); diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConfigPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConfigPropertiesTest.java index 25d86bdbab0..f4c7fc1066f 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConfigPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConfigPropertiesTest.java @@ -35,7 +35,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class ConfigPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigProperties") public ConfigProperties configProperties() { return new ConfigProperties().setType(STR_TEST_AAA).setDataType(STR_TEST_BBB); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConsulPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConsulPropertiesTest.java index 482b87f792e..8670ef57579 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConsulPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ConsulPropertiesTest.java @@ -35,7 +35,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class ConsulPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigConsulProperties") public ConfigConsulProperties configConsulProperties() { return new ConfigConsulProperties().setServerAddr(STR_TEST_AAA).setAclToken(STR_TEST_BBB).setKey(STR_TEST_CCC); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/CustomPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/CustomPropertiesTest.java index 6debcc5fd1b..fd362508bf6 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/CustomPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/CustomPropertiesTest.java @@ -36,7 +36,7 @@ @Import(SpringApplicationContextProvider.class) public class CustomPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigCustomProperties") public ConfigCustomProperties configCustomProperties() { return new ConfigCustomProperties().setName(STR_TEST_AAA); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/Etcd3PropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/Etcd3PropertiesTest.java index b166d1bb762..99c5cb6b2fd 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/Etcd3PropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/Etcd3PropertiesTest.java @@ -35,7 +35,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class Etcd3PropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigEtcd3Properties") public ConfigEtcd3Properties configEtcd3Properties() { return new ConfigEtcd3Properties().setServerAddr(STR_TEST_AAA).setKey(STR_TEST_BBB); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/FilePropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/FilePropertiesTest.java index 46e4e496296..2e9bd5c0db5 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/FilePropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/FilePropertiesTest.java @@ -35,7 +35,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class FilePropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigFileProperties") public ConfigFileProperties configFileProperties() { return new ConfigFileProperties().setName(STR_TEST_AAA); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/NacosPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/NacosPropertiesTest.java index 489a96839a1..6cbb2635221 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/NacosPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/NacosPropertiesTest.java @@ -36,7 +36,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class NacosPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigNacosProperties") public ConfigNacosProperties configNacosProperties() { return new ConfigNacosProperties().setServerAddr(STR_TEST_AAA).setDataId(STR_TEST_BBB).setGroup(STR_TEST_CCC).setNamespace(STR_TEST_DDD).setUsername(STR_TEST_EEE).setPassword(STR_TEST_FFF); } diff --git a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ZooKeeperPropertiesTest.java b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ZooKeeperPropertiesTest.java index 0af11467f6b..4a95734e907 100644 --- a/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ZooKeeperPropertiesTest.java +++ b/seata-spring-autoconfigure/seata-spring-autoconfigure-core/src/test/java/io/seata/spring/boot/autoconfigure/properties/config/test/ZooKeeperPropertiesTest.java @@ -35,7 +35,7 @@ @org.springframework.context.annotation.Configuration @Import(SpringApplicationContextProvider.class) public class ZooKeeperPropertiesTest extends BasePropertiesTest { - @Bean + @Bean("testConfigZooKeeperProperties") public ConfigZooKeeperProperties configZooKeeperProperties() { return new ConfigZooKeeperProperties().setNodePath(STR_TEST_AAA).setServerAddr(STR_TEST_BBB).setUsername(STR_TEST_CCC).setPassword(STR_TEST_DDD).setConnectTimeout(LONG_TEST_ONE).setSessionTimeout(LONG_TEST_TWO); } diff --git a/server/src/main/java/io/seata/server/ParameterParser.java b/server/src/main/java/io/seata/server/ParameterParser.java index 1305fa99e00..55690fcaf31 100644 --- a/server/src/main/java/io/seata/server/ParameterParser.java +++ b/server/src/main/java/io/seata/server/ParameterParser.java @@ -21,10 +21,9 @@ import io.seata.common.util.StringUtils; import io.seata.config.Configuration; import io.seata.config.ConfigurationFactory; -import io.seata.core.constants.ConfigurationKeys; import io.seata.server.env.ContainerHelper; +import io.seata.server.store.StoreConfig; -import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE; import static io.seata.config.ConfigurationFactory.ENV_PROPERTY_KEY; /** @@ -67,6 +66,10 @@ public ParameterParser(String... args) { this.init(args); } + /** + * startup args > docker env + * @param args + */ private void init(String[] args) { try { getCommandParameters(args); @@ -74,15 +77,7 @@ private void init(String[] args) { if (StringUtils.isNotBlank(seataEnv)) { System.setProperty(ENV_PROPERTY_KEY, seataEnv); } - if (StringUtils.isBlank(storeMode)) { - storeMode = CONFIG.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE); - } - if (StringUtils.isBlank(sessionStoreMode)) { - sessionStoreMode = CONFIG.getConfig(ConfigurationKeys.STORE_SESSION_MODE, storeMode); - } - if (StringUtils.isBlank(lockStoreMode)) { - lockStoreMode = CONFIG.getConfig(ConfigurationKeys.STORE_LOCK_MODE, storeMode); - } + StoreConfig.setStartupParameter(storeMode, sessionStoreMode, lockStoreMode); } catch (ParameterException e) { printError(e); } @@ -112,15 +107,6 @@ private void getEnvParameters() { if (serverNode == null) { serverNode = ContainerHelper.getServerNode(); } - if (StringUtils.isBlank(storeMode)) { - storeMode = ContainerHelper.getStoreMode(); - } - if (StringUtils.isBlank(sessionStoreMode)) { - sessionStoreMode = ContainerHelper.getSessionStoreMode(); - } - if (StringUtils.isBlank(lockStoreMode)) { - lockStoreMode = ContainerHelper.getLockStoreMode(); - } } private void printError(ParameterException e) { @@ -163,7 +149,7 @@ public String getStoreMode() { * @return the store mode */ public String getLockStoreMode() { - return StringUtils.isNotEmpty(lockStoreMode) ? lockStoreMode : storeMode; + return lockStoreMode; } /** @@ -172,7 +158,7 @@ public String getLockStoreMode() { * @return the store mode */ public String getSessionStoreMode() { - return StringUtils.isNotEmpty(sessionStoreMode) ? sessionStoreMode : storeMode; + return sessionStoreMode; } /** diff --git a/server/src/main/java/io/seata/server/Server.java b/server/src/main/java/io/seata/server/Server.java index 78316e71839..fc6e74f1f30 100644 --- a/server/src/main/java/io/seata/server/Server.java +++ b/server/src/main/java/io/seata/server/Server.java @@ -24,7 +24,6 @@ import io.seata.common.util.NetUtil; import io.seata.common.util.StringUtils; import io.seata.config.ConfigurationFactory; -import io.seata.core.constants.ConfigurationKeys; import io.seata.core.rpc.netty.NettyRemotingServer; import io.seata.core.rpc.netty.NettyServerConfig; import io.seata.server.coordinator.DefaultCoordinator; @@ -60,8 +59,6 @@ public static void start(String[] args) { //initialize the metrics MetricsManager.get().init(); - System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode()); - ThreadPoolExecutor workingThreads = new ThreadPoolExecutor(NettyServerConfig.getMinServerPoolSize(), NettyServerConfig.getMaxServerPoolSize(), NettyServerConfig.getKeepAliveTime(), TimeUnit.SECONDS, new LinkedBlockingQueue<>(NettyServerConfig.getMaxTaskQueueSize()), @@ -83,8 +80,8 @@ public static void start(String[] args) { XID.setPort(nettyRemotingServer.getListenPort()); UUIDGenerator.init(parameterParser.getServerNode()); //log store mode : file, db, redis - SessionHolder.init(parameterParser.getSessionStoreMode()); - LockerManagerFactory.init(parameterParser.getLockStoreMode()); + SessionHolder.init(); + LockerManagerFactory.init(); DefaultCoordinator coordinator = DefaultCoordinator.getInstance(nettyRemotingServer); coordinator.init(); nettyRemotingServer.setHandler(coordinator); diff --git a/server/src/main/java/io/seata/server/ServerApplicationListener.java b/server/src/main/java/io/seata/server/ServerApplicationListener.java index aa8b0fc2c65..c67000fc6fe 100644 --- a/server/src/main/java/io/seata/server/ServerApplicationListener.java +++ b/server/src/main/java/io/seata/server/ServerApplicationListener.java @@ -16,10 +16,12 @@ package io.seata.server; import java.util.Properties; + import io.seata.common.holder.ObjectHolder; import io.seata.common.util.StringUtils; import io.seata.config.Configuration; import io.seata.config.ConfigurationFactory; +import io.seata.server.store.StoreConfig; import io.seata.spring.boot.autoconfigure.SeataCoreEnvironmentPostProcessor; import io.seata.spring.boot.autoconfigure.SeataServerEnvironmentPostProcessor; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; @@ -30,11 +32,7 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.PropertiesPropertySource; -import static io.seata.common.ConfigurationKeys.STORE_LOCK_MODE; -import static io.seata.common.ConfigurationKeys.STORE_MODE; -import static io.seata.common.ConfigurationKeys.STORE_SESSION_MODE; import static io.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT; -import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE; import static io.seata.common.DefaultValues.SERVICE_OFFSET_SPRING_BOOT; import static io.seata.core.constants.ConfigurationKeys.ENV_SEATA_PORT_KEY; import static io.seata.core.constants.ConfigurationKeys.SERVER_SERVICE_PORT_CAMEL; @@ -64,10 +62,8 @@ public void onApplicationEvent(ApplicationEvent event) { SeataServerEnvironmentPostProcessor.init(); Configuration config = ConfigurationFactory.getInstance(); // Load by priority - System.setProperty("sessionMode", - config.getConfig(STORE_SESSION_MODE, config.getConfig(STORE_MODE, SERVER_DEFAULT_STORE_MODE))); - System.setProperty("lockMode", - config.getConfig(STORE_LOCK_MODE, config.getConfig(STORE_MODE, SERVER_DEFAULT_STORE_MODE))); + System.setProperty("sessionMode", StoreConfig.getSessionMode().getName()); + System.setProperty("lockMode", StoreConfig.getLockMode().getName()); String[] args = environmentPreparedEvent.getArgs(); diff --git a/server/src/main/java/io/seata/server/lock/LockerManagerFactory.java b/server/src/main/java/io/seata/server/lock/LockerManagerFactory.java index 65303260b89..cec4f0daf53 100644 --- a/server/src/main/java/io/seata/server/lock/LockerManagerFactory.java +++ b/server/src/main/java/io/seata/server/lock/LockerManagerFactory.java @@ -16,13 +16,10 @@ package io.seata.server.lock; import io.seata.common.loader.EnhancedServiceLoader; -import io.seata.common.util.StringUtils; import io.seata.config.Configuration; import io.seata.config.ConfigurationFactory; -import io.seata.core.constants.ConfigurationKeys; -import io.seata.core.store.StoreMode; - -import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE; +import io.seata.server.store.StoreConfig; +import io.seata.server.store.StoreConfig.LockMode; /** * The type Lock manager factory. @@ -54,16 +51,16 @@ public static void init() { init(null); } - public static void init(String lockMode) { + public static void init(LockMode lockMode) { if (LOCK_MANAGER == null) { synchronized (LockerManagerFactory.class) { if (LOCK_MANAGER == null) { - if (StringUtils.isBlank(lockMode)) { - lockMode = CONFIG.getConfig(ConfigurationKeys.STORE_LOCK_MODE, - CONFIG.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE)); + if (null == lockMode) { + lockMode = StoreConfig.getLockMode(); } - if (StoreMode.contains(lockMode)) { - LOCK_MANAGER = EnhancedServiceLoader.load(LockManager.class, lockMode); + //if not exist the lock mode, throw exception + if (null != StoreConfig.StoreMode.get(lockMode.name())) { + LOCK_MANAGER = EnhancedServiceLoader.load(LockManager.class, lockMode.getName()); } } } diff --git a/server/src/main/java/io/seata/server/session/SessionHelper.java b/server/src/main/java/io/seata/server/session/SessionHelper.java index 919e7817402..9d144445720 100644 --- a/server/src/main/java/io/seata/server/session/SessionHelper.java +++ b/server/src/main/java/io/seata/server/session/SessionHelper.java @@ -20,7 +20,6 @@ import java.util.Objects; import io.seata.common.util.CollectionUtils; -import io.seata.common.util.StringUtils; import io.seata.config.Configuration; import io.seata.config.ConfigurationFactory; import io.seata.core.constants.ConfigurationKeys; @@ -28,11 +27,12 @@ import io.seata.core.exception.TransactionException; import io.seata.core.model.BranchType; import io.seata.core.model.GlobalStatus; -import io.seata.core.store.StoreMode; import io.seata.metrics.IdConstants; import io.seata.server.UUIDGenerator; import io.seata.server.coordinator.DefaultCoordinator; import io.seata.server.metrics.MetricsPublisher; +import io.seata.server.store.StoreConfig; +import io.seata.server.store.StoreConfig.SessionMode; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -60,9 +60,7 @@ public class SessionHelper { */ private static final DefaultCoordinator COORDINATOR = DefaultCoordinator.getInstance(); - private static final boolean DELAY_HANDLE_SESSION = - !StringUtils.equalsIgnoreCase(ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_SESSION_MODE, - ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_MODE)), StoreMode.FILE.getName()); + private static final boolean DELAY_HANDLE_SESSION = StoreConfig.getSessionMode() != SessionMode.FILE; private SessionHelper() { } diff --git a/server/src/main/java/io/seata/server/session/SessionHolder.java b/server/src/main/java/io/seata/server/session/SessionHolder.java index 615864ae016..eeea2551487 100644 --- a/server/src/main/java/io/seata/server/session/SessionHolder.java +++ b/server/src/main/java/io/seata/server/session/SessionHolder.java @@ -21,10 +21,6 @@ import java.util.concurrent.CompletableFuture; import io.seata.common.ConfigurationKeys; -import io.seata.core.model.LockStatus; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import io.seata.common.XID; import io.seata.common.exception.ShouldNeverHappenException; import io.seata.common.exception.StoreException; @@ -35,13 +31,16 @@ import io.seata.config.ConfigurationFactory; import io.seata.core.exception.TransactionException; import io.seata.core.model.GlobalStatus; +import io.seata.core.model.LockStatus; import io.seata.core.store.DistributedLockDO; import io.seata.core.store.DistributedLocker; import io.seata.server.lock.distributed.DistributedLockerFactory; -import io.seata.core.store.StoreMode; +import io.seata.server.store.StoreConfig; +import io.seata.server.store.StoreConfig.SessionMode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import static io.seata.common.DefaultValues.DEFAULT_DISTRIBUTED_LOCK_EXPIRE_TIME; -import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE; /** * The type Session holder. @@ -90,56 +89,57 @@ public class SessionHolder { private static DistributedLocker DISTRIBUTED_LOCKER; + public static void init() { + init(null); + } /** * Init. * - * @param mode the store mode: file, db, redis + * @param sessionMode the store mode: file, db, redis * @throws IOException the io exception */ - public static void init(String mode) { - if (StringUtils.isBlank(mode)) { - mode = CONFIG.getConfig(ConfigurationKeys.STORE_SESSION_MODE, - CONFIG.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE)); + public static void init(SessionMode sessionMode) { + if (null == sessionMode) { + sessionMode = StoreConfig.getSessionMode(); } - StoreMode storeMode = StoreMode.get(mode); - if (StoreMode.DB.equals(storeMode)) { - ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName()); - ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), + if (SessionMode.DB.equals(sessionMode)) { + ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.DB.getName()); + ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.DB.getName(), new Object[]{ASYNC_COMMITTING_SESSION_MANAGER_NAME}); - RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), + RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.DB.getName(), new Object[]{RETRY_COMMITTING_SESSION_MANAGER_NAME}); - RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.DB.getName(), + RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.DB.getName(), new Object[]{RETRY_ROLLBACKING_SESSION_MANAGER_NAME}); - DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(StoreMode.DB.getName()); - } else if (StoreMode.FILE.equals(storeMode)) { + DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(SessionMode.DB.getName()); + } else if (SessionMode.FILE.equals(sessionMode)) { String sessionStorePath = CONFIG.getConfig(ConfigurationKeys.STORE_FILE_DIR, DEFAULT_SESSION_STORE_FILE_DIR); if (StringUtils.isBlank(sessionStorePath)) { throw new StoreException("the {store.file.dir} is empty."); } - ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.FILE.getName(), + ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.FILE.getName(), new Object[]{ROOT_SESSION_MANAGER_NAME, sessionStorePath}); ASYNC_COMMITTING_SESSION_MANAGER = ROOT_SESSION_MANAGER; RETRY_COMMITTING_SESSION_MANAGER = ROOT_SESSION_MANAGER; RETRY_ROLLBACKING_SESSION_MANAGER = ROOT_SESSION_MANAGER; - DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(StoreMode.FILE.getName()); - } else if (StoreMode.REDIS.equals(storeMode)) { - ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, StoreMode.REDIS.getName()); + DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(SessionMode.FILE.getName()); + } else if (SessionMode.REDIS.equals(sessionMode)) { + ROOT_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, SessionMode.REDIS.getName()); ASYNC_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, - StoreMode.REDIS.getName(), new Object[]{ASYNC_COMMITTING_SESSION_MANAGER_NAME}); + SessionMode.REDIS.getName(), new Object[]{ASYNC_COMMITTING_SESSION_MANAGER_NAME}); RETRY_COMMITTING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, - StoreMode.REDIS.getName(), new Object[]{RETRY_COMMITTING_SESSION_MANAGER_NAME}); + SessionMode.REDIS.getName(), new Object[]{RETRY_COMMITTING_SESSION_MANAGER_NAME}); RETRY_ROLLBACKING_SESSION_MANAGER = EnhancedServiceLoader.load(SessionManager.class, - StoreMode.REDIS.getName(), new Object[]{RETRY_ROLLBACKING_SESSION_MANAGER_NAME}); + SessionMode.REDIS.getName(), new Object[]{RETRY_ROLLBACKING_SESSION_MANAGER_NAME}); - DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(StoreMode.REDIS.getName()); + DISTRIBUTED_LOCKER = DistributedLockerFactory.getDistributedLocker(SessionMode.REDIS.getName()); } else { // unknown store - throw new IllegalArgumentException("unknown store mode:" + mode); + throw new IllegalArgumentException("unknown store mode:" + sessionMode.getName()); } - reload(storeMode); + reload(sessionMode); } //region reload @@ -147,15 +147,15 @@ public static void init(String mode) { /** * Reload. * - * @param storeMode the mode of store + * @param sessionMode the mode of store */ - protected static void reload(StoreMode storeMode) { + protected static void reload(SessionMode sessionMode) { if (ROOT_SESSION_MANAGER instanceof Reloadable) { ((Reloadable) ROOT_SESSION_MANAGER).reload(); } - if (storeMode == StoreMode.FILE) { + if (SessionMode.FILE.equals(sessionMode)) { Collection allSessions = ROOT_SESSION_MANAGER.allSessions(); if (CollectionUtils.isNotEmpty(allSessions)) { for (GlobalSession globalSession : allSessions) { diff --git a/server/src/main/java/io/seata/server/store/StoreConfig.java b/server/src/main/java/io/seata/server/store/StoreConfig.java index 3903c4ad76a..362c7992e1d 100644 --- a/server/src/main/java/io/seata/server/store/StoreConfig.java +++ b/server/src/main/java/io/seata/server/store/StoreConfig.java @@ -15,20 +15,44 @@ */ package io.seata.server.store; +import io.seata.common.util.StringUtils; import io.seata.config.Configuration; import io.seata.config.ConfigurationFactory; +import io.seata.core.constants.ConfigurationKeys; +import io.seata.server.env.ContainerHelper; import io.seata.server.storage.file.FlushDiskMode; +import static io.seata.common.DefaultValues.SERVER_DEFAULT_STORE_MODE; import static io.seata.core.constants.ConfigurationKeys.STORE_FILE_PREFIX; - /** * @author lizhao */ public class StoreConfig { private static final Configuration CONFIGURATION = ConfigurationFactory.getInstance(); + private static StoreMode storeMode; + private static SessionMode sessionMode; + private static LockMode lockMode; + /** + * set storeMode sessionMode lockMode from StartupParameter + * + * @param storeMode storeMode + * @param sessionMode sessionMode + * @param lockMode lockMode + */ + public static void setStartupParameter(String storeMode, String sessionMode, String lockMode) { + if (StringUtils.isNotBlank(storeMode)) { + StoreConfig.storeMode = StoreMode.get(storeMode); + } + if (StringUtils.isNotBlank(sessionMode)) { + StoreConfig.sessionMode = SessionMode.get(sessionMode); + } + if (StringUtils.isNotBlank(lockMode)) { + StoreConfig.lockMode = LockMode.get(lockMode); + } + } /** * Default 16kb. @@ -60,4 +84,165 @@ public static int getFileWriteBufferCacheSize() { public static FlushDiskMode getFlushDiskMode() { return FlushDiskMode.findDiskMode(CONFIGURATION.getConfig(STORE_FILE_PREFIX + "flushDiskMode")); } + + /** + * only for inner call + * + * @return + */ + private static StoreMode getStoreMode() { + //startup + if (null != storeMode) { + return storeMode; + } + //env + String storeModeEnv = ContainerHelper.getStoreMode(); + if (StringUtils.isNotBlank(storeModeEnv)) { + return StoreMode.get(storeModeEnv); + } + //config + String storeModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_MODE, SERVER_DEFAULT_STORE_MODE); + return StoreMode.get(storeModeConfig); + } + + public static SessionMode getSessionMode() { + //startup + if (null != sessionMode) { + return sessionMode; + } + //env + String sessionModeEnv = ContainerHelper.getSessionStoreMode(); + if (StringUtils.isNotBlank(sessionModeEnv)) { + return SessionMode.get(sessionModeEnv); + } + //config + String sessionModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_SESSION_MODE); + if (StringUtils.isNotBlank(sessionModeConfig)) { + return SessionMode.get(sessionModeConfig); + } + // complication old config + return SessionMode.get(getStoreMode().name()); + } + + public static LockMode getLockMode() { + //startup + if (null != lockMode) { + return lockMode; + } + //env + String lockModeEnv = ContainerHelper.getLockStoreMode(); + if (StringUtils.isNotBlank(lockModeEnv)) { + return LockMode.get(lockModeEnv); + } + //config + String lockModeConfig = CONFIGURATION.getConfig(ConfigurationKeys.STORE_LOCK_MODE); + if (StringUtils.isNotBlank(lockModeConfig)) { + return LockMode.get(lockModeConfig); + } + // complication old config + return LockMode.get(getStoreMode().name()); + } + + public enum StoreMode { + /** + * The File store mode. + */ + FILE("file"), + /** + * The Db store mode. + */ + DB("db"), + /** + * The Redis store mode. + */ + REDIS("redis"); + + private String name; + + StoreMode(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static StoreMode get(String name) { + for (StoreMode mode : StoreMode.values()) { + if (mode.getName().equalsIgnoreCase(name)) { + return mode; + } + } + throw new IllegalArgumentException("unknown store mode:" + name); + } + } + + public enum SessionMode { + /** + * The File store mode. + */ + FILE("file"), + /** + * The Db store mode. + */ + DB("db"), + /** + * The Redis store mode. + */ + REDIS("redis"); + + private String name; + + SessionMode(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static SessionMode get(String name) { + for (SessionMode mode : SessionMode.values()) { + if (mode.getName().equalsIgnoreCase(name)) { + return mode; + } + } + throw new IllegalArgumentException("unknown session mode:" + name); + } + } + + public enum LockMode { + /** + * The File store mode. + */ + FILE("file"), + /** + * The Db store mode. + */ + DB("db"), + /** + * The Redis store mode. + */ + REDIS("redis"); + + private String name; + + LockMode(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public static LockMode get(String name) { + for (LockMode mode : LockMode.values()) { + if (mode.getName().equalsIgnoreCase(name)) { + return mode; + } + } + throw new IllegalArgumentException("unknown lock mode:" + name); + } + } + } diff --git a/server/src/test/java/io/seata/server/ParameterParserTest.java b/server/src/test/java/io/seata/server/ParameterParserTest.java index ac0a6d2f57a..c953d878940 100644 --- a/server/src/test/java/io/seata/server/ParameterParserTest.java +++ b/server/src/test/java/io/seata/server/ParameterParserTest.java @@ -48,7 +48,7 @@ public void testEmptyMode() { parameterParser.cleanUp(); parameterParser = new ParameterParser(args); //always set store.mode=file in test/resource/file.conf, if not will cause SessionStoreTest's case fail. - Assertions.assertEquals("file", parameterParser.getStoreMode()); + Assertions.assertNull(parameterParser.getStoreMode()); } /** diff --git a/server/src/test/java/io/seata/server/coordinator/DefaultCoordinatorTest.java b/server/src/test/java/io/seata/server/coordinator/DefaultCoordinatorTest.java index 5bdbbb4fdee..5d86fe1250b 100644 --- a/server/src/test/java/io/seata/server/coordinator/DefaultCoordinatorTest.java +++ b/server/src/test/java/io/seata/server/coordinator/DefaultCoordinatorTest.java @@ -42,10 +42,10 @@ import io.seata.core.protocol.transaction.BranchRollbackResponse; import io.seata.core.rpc.RemotingServer; import io.seata.core.rpc.processor.RemotingProcessor; -import io.seata.core.store.StoreMode; import io.seata.server.metrics.MetricsManager; import io.seata.server.session.GlobalSession; import io.seata.server.session.SessionHolder; +import io.seata.server.store.StoreConfig.SessionMode; import io.seata.server.util.StoreUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; @@ -228,7 +228,7 @@ public static void afterClass() throws Exception { private static void deleteAndCreateDataFile() throws IOException { StoreUtil.deleteDataFile(); - SessionHolder.init(StoreMode.FILE.name()); + SessionHolder.init(SessionMode.FILE); } @AfterEach diff --git a/server/src/test/java/io/seata/server/coordinator/DefaultCoreTest.java b/server/src/test/java/io/seata/server/coordinator/DefaultCoreTest.java index 2c83b285cec..4e6c15652c7 100644 --- a/server/src/test/java/io/seata/server/coordinator/DefaultCoreTest.java +++ b/server/src/test/java/io/seata/server/coordinator/DefaultCoreTest.java @@ -27,6 +27,7 @@ import io.seata.server.session.GlobalSession; import io.seata.server.session.SessionHelper; import io.seata.server.session.SessionHolder; +import io.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; @@ -76,7 +77,7 @@ public class DefaultCoreTest { */ @BeforeAll public static void initSessionManager(ApplicationContext context) throws Exception { - SessionHolder.init(null); + SessionHolder.init(SessionMode.FILE); remotingServer = new DefaultCoordinatorTest.MockServerMessageSender(); core = new DefaultCore(remotingServer); } diff --git a/server/src/test/java/io/seata/server/event/DefaultCoreForEventBusTest.java b/server/src/test/java/io/seata/server/event/DefaultCoreForEventBusTest.java index 50573f643da..bb9c4a154ad 100644 --- a/server/src/test/java/io/seata/server/event/DefaultCoreForEventBusTest.java +++ b/server/src/test/java/io/seata/server/event/DefaultCoreForEventBusTest.java @@ -25,20 +25,18 @@ import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.Subscribe; -import io.seata.common.util.StringUtils; -import io.seata.config.ConfigurationFactory; -import io.seata.core.constants.ConfigurationKeys; import io.seata.core.event.GlobalTransactionEvent; import io.seata.core.exception.TransactionException; import io.seata.core.model.GlobalStatus; import io.seata.core.rpc.RemotingServer; -import io.seata.core.store.StoreMode; import io.seata.metrics.registry.Registry; import io.seata.server.coordinator.DefaultCoordinator; import io.seata.server.coordinator.DefaultCoordinatorTest; import io.seata.server.coordinator.DefaultCore; import io.seata.server.metrics.MetricsManager; import io.seata.server.session.SessionHolder; +import io.seata.server.store.StoreConfig; +import io.seata.server.store.StoreConfig.SessionMode; import io.seata.server.util.StoreUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; @@ -55,9 +53,7 @@ @SpringBootTest public class DefaultCoreForEventBusTest { - private static final boolean DELAY_HANDLE_SESSION = - !StringUtils.equalsIgnoreCase(ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_SESSION_MODE, - ConfigurationFactory.getInstance().getConfig(ConfigurationKeys.STORE_MODE)), StoreMode.FILE.getName()); + private static final boolean DELAY_HANDLE_SESSION = StoreConfig.getSessionMode() != SessionMode.FILE; @BeforeAll public static void setUp(ApplicationContext context) throws InterruptedException { diff --git a/server/src/test/java/io/seata/server/lock/LockManagerTest.java b/server/src/test/java/io/seata/server/lock/LockManagerTest.java index 98a16d7d3d0..fea75ba3fcc 100644 --- a/server/src/test/java/io/seata/server/lock/LockManagerTest.java +++ b/server/src/test/java/io/seata/server/lock/LockManagerTest.java @@ -38,6 +38,7 @@ import io.seata.server.session.GlobalSession; import io.seata.server.session.SessionHolder; import io.seata.server.session.SessionManager; +import io.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.params.ParameterizedTest; @@ -216,7 +217,7 @@ public void duplicatePkBranchSessionHolderTest(BranchSession branchSession1, Bra @MethodSource("globalSessionForLockTestProvider") public void lockQueryTest(GlobalSession globalSessions1, GlobalSession globalSessions2) throws TransactionException, ParseException { SessionHolder.getRootSessionManager().destroy(); - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); final SessionManager sessionManager = SessionHolder.getRootSessionManager(); //make sure sessionMaanager is empty Collection sessions = sessionManager.allSessions(); diff --git a/server/src/test/java/io/seata/server/session/FileSessionManagerTest.java b/server/src/test/java/io/seata/server/session/FileSessionManagerTest.java index 6c2a33bc4c6..9b2b74856c8 100644 --- a/server/src/test/java/io/seata/server/session/FileSessionManagerTest.java +++ b/server/src/test/java/io/seata/server/session/FileSessionManagerTest.java @@ -38,6 +38,7 @@ import io.seata.server.console.service.GlobalSessionService; import io.seata.server.console.vo.GlobalSessionVO; import io.seata.server.storage.file.session.FileSessionManager; +import io.seata.server.store.StoreConfig.SessionMode; import io.seata.server.util.StoreUtil; import org.apache.commons.lang.time.DateUtils; import org.junit.jupiter.api.Assertions; @@ -284,7 +285,7 @@ public void findGlobalSessionsTest(List globalSessions) throws Ex @MethodSource("globalSessionsWithPageResultProvider") public void findGlobalSessionsWithPageResultTest(List globalSessions) throws Exception { SessionHolder.getRootSessionManager().destroy(); - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); try { for (GlobalSession globalSession : globalSessions) { diff --git a/server/src/test/java/io/seata/server/session/SessionHolderTest.java b/server/src/test/java/io/seata/server/session/SessionHolderTest.java index 8ec0f953023..aa259cde9fc 100644 --- a/server/src/test/java/io/seata/server/session/SessionHolderTest.java +++ b/server/src/test/java/io/seata/server/session/SessionHolderTest.java @@ -19,7 +19,7 @@ import java.io.IOException; import io.seata.core.constants.ConfigurationKeys; -import io.seata.core.store.StoreMode; +import io.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -61,8 +61,7 @@ public void testInit() throws IOException { if (rootSessionFile.exists()) { rootSessionFile.delete(); } - final String mode = StoreMode.FILE.getName(); - SessionHolder.init(mode); + SessionHolder.init(SessionMode.FILE); try { final File actual = new File(pathname); Assertions.assertTrue(actual.exists()); diff --git a/server/src/test/java/io/seata/server/session/redis/RedisDistributedLockerTest.java b/server/src/test/java/io/seata/server/session/redis/RedisDistributedLockerTest.java index b6594390843..c5e20134296 100644 --- a/server/src/test/java/io/seata/server/session/redis/RedisDistributedLockerTest.java +++ b/server/src/test/java/io/seata/server/session/redis/RedisDistributedLockerTest.java @@ -15,18 +15,18 @@ */ package io.seata.server.session.redis; -import org.junit.jupiter.api.Assertions; -import redis.clients.jedis.JedisPool; -import redis.clients.jedis.JedisPoolAbstract; -import redis.clients.jedis.JedisPoolConfig; import java.io.IOException; import io.seata.core.store.DistributedLockDO; import io.seata.core.store.DistributedLocker; -import io.seata.core.store.StoreMode; import io.seata.server.lock.distributed.DistributedLockerFactory; import io.seata.server.session.SessionHolder; import io.seata.server.storage.redis.JedisPooledFactory; +import io.seata.server.store.StoreConfig.SessionMode; +import org.junit.jupiter.api.Assertions; +import redis.clients.jedis.JedisPool; +import redis.clients.jedis.JedisPoolAbstract; +import redis.clients.jedis.JedisPoolConfig; /** * @description redis distributed lock test @@ -53,7 +53,7 @@ public static void start() throws IOException { poolConfig.setMinIdle(1); poolConfig.setMaxIdle(10); jedisPoolInstance = JedisPooledFactory.getJedisPoolInstance(new JedisPool(poolConfig, "127.0.0.1", 6379, 60000)); - distributedLocker = DistributedLockerFactory.getDistributedLocker(StoreMode.REDIS.getName()); + distributedLocker = DistributedLockerFactory.getDistributedLocker(SessionMode.REDIS.getName()); } // @Test @@ -69,7 +69,7 @@ public void test_acquireScheduledLock_success() { // @Test public void test_acquireScheduledLock_success_() { - SessionHolder.init(StoreMode.REDIS.getName()); + SessionHolder.init(SessionMode.REDIS); boolean accquire = SessionHolder.acquireDistributedLock(retryRollbacking); Assertions.assertEquals(true,accquire); String lockValueExisted = jedisPoolInstance.getResource().get(retryRollbacking); @@ -99,11 +99,11 @@ public void test_acquireLock_concurrent() { Assertions.assertNull(jedisPoolInstance.getResource().get(retryRollbacking)); // other acquire the lock success - boolean c = distributedLocker.acquireLock(new DistributedLockDO(retryRollbacking, lockValue + 1, 60000l)); + boolean c = distributedLocker.acquireLock(new DistributedLockDO(retryRollbacking, lockValue + 1, 60000L)); Assertions.assertEquals(true,c); //other2 acquire the lock failed - boolean d = distributedLocker.acquireLock(new DistributedLockDO(retryRollbacking, lockValue + 2, 60000l)); + boolean d = distributedLocker.acquireLock(new DistributedLockDO(retryRollbacking, lockValue + 2, 60000L)); Assertions.assertEquals(false,d); //sleep 60s @@ -125,7 +125,7 @@ public void test_acquireLock_concurrent() { public void test_acquireLock_false() { String set = jedisPoolInstance.getResource().set(retryCommiting, lockValue); Assertions.assertEquals("OK",set); - boolean acquire = distributedLocker.acquireLock(new DistributedLockDO(retryCommiting, lockValue, 60000l)); + boolean acquire = distributedLocker.acquireLock(new DistributedLockDO(retryCommiting, lockValue, 60000L)); Assertions.assertEquals(false,acquire); } } diff --git a/server/src/test/java/io/seata/server/store/SessionStoreTest.java b/server/src/test/java/io/seata/server/store/SessionStoreTest.java index 61fa9fe80f9..49ade6dce27 100644 --- a/server/src/test/java/io/seata/server/store/SessionStoreTest.java +++ b/server/src/test/java/io/seata/server/store/SessionStoreTest.java @@ -30,6 +30,7 @@ import io.seata.server.session.GlobalSession; import io.seata.server.session.SessionHelper; import io.seata.server.session.SessionHolder; +import io.seata.server.store.StoreConfig.SessionMode; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; @@ -87,7 +88,7 @@ public void clean() throws Exception { @Test public void testRestoredFromFile() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); String xid = XID.generateXID(globalSession.getTransactionId()); globalSession.setXid(xid); @@ -119,7 +120,7 @@ public void testRestoredFromFile() throws Exception { Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "tb:3")); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); long tid = globalSession.getTransactionId(); GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid()); @@ -148,14 +149,14 @@ public void testRestoredFromFile() throws Exception { //@Test public void testRestoredFromFile2() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); globalSession.addSessionLifecycleListener(SessionHolder.getRootSessionManager()); globalSession.begin(); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); } finally { SessionHolder.destroy(); } @@ -169,7 +170,7 @@ public void testRestoredFromFile2() throws Exception { @Test public void testRestoredFromFileAsyncCommitting() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); String xid = XID.generateXID(globalSession.getTransactionId()); @@ -196,7 +197,7 @@ public void testRestoredFromFileAsyncCommitting() throws Exception { Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1")); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); long tid = globalSession.getTransactionId(); GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid()); @@ -225,7 +226,7 @@ public void testRestoredFromFileAsyncCommitting() throws Exception { @Test public void testRestoredFromFileCommitRetry() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); String xid = XID.generateXID(globalSession.getTransactionId()); @@ -254,7 +255,7 @@ public void testRestoredFromFileCommitRetry() throws Exception { Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1")); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); long tid = globalSession.getTransactionId(); GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid()); @@ -285,7 +286,7 @@ public void testRestoredFromFileCommitRetry() throws Exception { @Test public void testRestoredFromFileRollbackRetry() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); @@ -315,7 +316,7 @@ public void testRestoredFromFileRollbackRetry() throws Exception { Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1")); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); long tid = globalSession.getTransactionId(); GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid()); @@ -346,7 +347,7 @@ public void testRestoredFromFileRollbackRetry() throws Exception { @Test public void testRestoredFromFileRollbackFailed() throws Exception { try { - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); GlobalSession globalSession = new GlobalSession("demo-app", DEFAULT_TX_GROUP, "test", 6000); @@ -379,7 +380,7 @@ public void testRestoredFromFileRollbackFailed() throws Exception { Assertions.assertTrue(lockManager.isLockable(otherXID, RESOURCE_ID, "ta:1")); // Re-init SessionHolder: restore sessions from file - SessionHolder.init("file"); + SessionHolder.init(SessionMode.FILE); long tid = globalSession.getTransactionId(); GlobalSession reloadSession = SessionHolder.findGlobalSession(globalSession.getXid()); diff --git a/test/src/test/java/io/seata/saga/engine/db/AbstractServerTest.java b/test/src/test/java/io/seata/saga/engine/db/AbstractServerTest.java index c2bb7adf40d..3a19acf336d 100644 --- a/test/src/test/java/io/seata/saga/engine/db/AbstractServerTest.java +++ b/test/src/test/java/io/seata/saga/engine/db/AbstractServerTest.java @@ -22,7 +22,6 @@ import io.seata.common.XID; import io.seata.common.util.NetUtil; -import io.seata.core.constants.ConfigurationKeys; import io.seata.core.rpc.ShutdownHook; import io.seata.core.rpc.netty.NettyRemotingServer; import io.seata.server.ParameterParser; @@ -45,7 +44,6 @@ public abstract class AbstractServerTest { protected static void startSeataServer() throws InterruptedException { (new Thread(new Runnable() { - @Override public void run() { File file = new File("sessionStore/root.data"); if(file.exists()){ @@ -57,12 +55,10 @@ public void run() { //initialize the metrics MetricsManager.get().init(); - System.setProperty(ConfigurationKeys.STORE_MODE, parameterParser.getStoreMode()); - nettyServer = new NettyRemotingServer(workingThreads); UUIDGenerator.init(parameterParser.getServerNode()); //log store mode : file、db - SessionHolder.init(parameterParser.getStoreMode()); + SessionHolder.init(); DefaultCoordinator coordinator = DefaultCoordinator.getInstance(nettyServer); coordinator.init();