From 05904f3df390e6d83e7360603f1f880d3c284e9c Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Fri, 31 Mar 2023 10:43:37 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E4=BC=98=E5=8C=96=E5=89=8D=E5=90=8E=E7=BD=AESQL=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E9=80=89=E6=8B=A9=E6=95=B0=E6=8D=AE=E6=BA=90=E7=9A=84?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化前后置SQL脚本选择数据源的逻辑 --- .../dto/definition/request/ElementUtil.java | 41 +++++++++++++++++++ .../processors/post/MsJDBCPostProcessor.java | 33 +-------------- .../processors/pre/MsJDBCPreProcessor.java | 34 +-------------- .../request/sampler/MsJDBCSampler.java | 5 ++- .../api/exec/api/ApiCaseSerialService.java | 2 + 5 files changed, 48 insertions(+), 67 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 80450d7948..6c82eb9491 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -43,6 +43,7 @@ import io.metersphere.metadata.service.FileMetadataService; import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; import io.metersphere.request.BodyFile; +import io.metersphere.utils.LoggerUtil; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; @@ -1030,4 +1031,44 @@ public class ElementUtil { loopController.setEnable(true); return loopController.controller(tree, name); } + + public static DatabaseConfig selectDataSourceFromJDBCProcessor(String processorName, String environmentId, String dataSourceId, String projectId, ParameterConfig config) { + if (config == null) { + return null; + } + DatabaseConfig dataSource = null; + // 自选了数据源 + if (config.isEffective(projectId) && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getDatabaseConfigs()) + && isDataSource(dataSourceId, config.getConfig().get(projectId).getDatabaseConfigs())) { + EnvironmentConfig environmentConfig = config.getConfig().get(projectId); + if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) { + environmentId = environmentConfig.getEnvironmentId(); + } + dataSource = ElementUtil.initDataSource(environmentId, dataSourceId); + if (dataSource == null && CollectionUtils.isNotEmpty(environmentConfig.getDatabaseConfigs())) { + dataSource = environmentConfig.getDatabaseConfigs().get(0); + } + } else { + // 取当前环境下默认的一个数据源 + if (config.isEffective(projectId) && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getDatabaseConfigs())) { + LoggerUtil.info(processorName + ":开始获取当前环境下默认数据源"); + DatabaseConfig dataSourceOrg = ElementUtil.dataSource(projectId, dataSourceId, config.getConfig().get(projectId)); + if (dataSourceOrg != null) { + dataSource = dataSourceOrg; + } else { + LoggerUtil.info(processorName + ":获取当前环境下默认数据源结束!未查找到默认数据源"); + dataSource = config.getConfig().get(projectId).getDatabaseConfigs().get(0); + } + } + } + return dataSource; + } + + private static boolean isDataSource(String dataSourceId, List databaseConfigs) { + List ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList()); + if (StringUtils.isNotEmpty(dataSourceId) && ids.contains(dataSourceId)) { + return true; + } + return false; + } } diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java index 4b35392022..8f2d4a03d0 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java @@ -5,7 +5,6 @@ import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; -import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.BeanUtils; @@ -22,7 +21,6 @@ import org.apache.jmeter.protocol.jdbc.processor.JDBCPostProcessor; import org.apache.jorphan.collections.HashTree; import java.util.List; -import java.util.stream.Collectors; /** * @author song.tianyang @@ -61,28 +59,7 @@ public class MsJDBCPostProcessor extends MsTestElement { config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId())); } - // 自选了数据源 - if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs()) - && isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId()); - if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) { - this.environmentId = environmentConfig.getEnvironmentId(); - } - this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); - LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null)); - } else { - // 取当前环境下默认的一个数据源 - if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - LoggerUtil.info(this.getName() + ":开始获取当前环境下默认数据源"); - DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId())); - if (dataSourceOrg != null) { - this.dataSource = dataSourceOrg; - } else { - LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源"); - this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); - } - } - } + this.dataSource = ElementUtil.selectDataSourceFromJDBCProcessor(this.getName(), this.environmentId, this.dataSourceId, this.getProjectId(), config); if (this.dataSource == null) { // 用自身的数据 if (StringUtils.isNotEmpty(dataSourceId)) { @@ -106,14 +83,6 @@ public class MsJDBCPostProcessor extends MsTestElement { } } - private boolean isDataSource(List databaseConfigs) { - List ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList()); - if (StringUtils.isNotEmpty(this.dataSourceId) && ids.contains(this.dataSourceId)) { - return true; - } - return false; - } - private JDBCPostProcessor jdbcPostProcessor(ParameterConfig config) { JDBCPostProcessor jdbcPostProcessor = new JDBCPostProcessor(); JDBCProcessorVO vo = new JDBCProcessorVO(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java index 403ecaee17..17deb34bd7 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java @@ -5,7 +5,6 @@ import io.metersphere.api.dto.definition.request.ElementUtil; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.scenario.DatabaseConfig; import io.metersphere.api.dto.scenario.KeyValue; -import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.commons.constants.ElementConstants; import io.metersphere.commons.constants.RequestTypeConstants; import io.metersphere.commons.exception.MSException; @@ -23,7 +22,6 @@ import org.apache.jmeter.protocol.jdbc.processor.JDBCPreProcessor; import org.apache.jorphan.collections.HashTree; import java.util.List; -import java.util.stream.Collectors; /** * @author song.tianyang @@ -60,29 +58,7 @@ public class MsJDBCPreProcessor extends MsTestElement { this.setProjectId(config.getProjectId()); config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId())); } - - // 自选了数据源 - if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs()) - && isDataSource(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - EnvironmentConfig environmentConfig = config.getConfig().get(this.getProjectId()); - if (environmentConfig.getDatabaseConfigs() != null && StringUtils.isNotEmpty(environmentConfig.getEnvironmentId())) { - this.environmentId = environmentConfig.getEnvironmentId(); - } - this.dataSource = ElementUtil.initDataSource(this.environmentId, this.dataSourceId); - LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null)); - } else { - // 取当前环境下默认的一个数据源 - if (config.isEffective(this.getProjectId()) && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getDatabaseConfigs())) { - LoggerUtil.info(this.getName() + ":开始获取当前环境下默认数据源"); - DatabaseConfig dataSourceOrg = ElementUtil.dataSource(getProjectId(), dataSourceId, config.getConfig().get(this.getProjectId())); - if (dataSourceOrg != null) { - this.dataSource = dataSourceOrg; - } else { - LoggerUtil.info(this.getName() + ":获取当前环境下默认数据源结束!未查找到默认数据源"); - this.dataSource = config.getConfig().get(this.getProjectId()).getDatabaseConfigs().get(0); - } - } - } + this.dataSource = ElementUtil.selectDataSourceFromJDBCProcessor(this.getName(), this.environmentId, this.dataSourceId, this.getProjectId(), config); if (this.dataSource == null) { // 用自身的数据 if (StringUtils.isNotEmpty(dataSourceId)) { @@ -108,14 +84,6 @@ public class MsJDBCPreProcessor extends MsTestElement { } } - private boolean isDataSource(List databaseConfigs) { - List ids = databaseConfigs.stream().map(DatabaseConfig::getId).collect(Collectors.toList()); - if (StringUtils.isNotEmpty(this.dataSourceId) && ids.contains(this.dataSourceId)) { - return true; - } - return false; - } - private JDBCPreProcessor jdbcPreProcessor(ParameterConfig config) { JDBCPreProcessor jdbcPreProcessor = new JDBCPreProcessor(); JDBCProcessorVO vo = new JDBCProcessorVO(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index 15466724f5..ef914da87a 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -92,8 +92,9 @@ public class MsJDBCSampler extends MsTestElement { } this.dataSource = null; envConfig = this.initDataSource(); - - LoggerUtil.info(this.getName() + ":自选数据源结束 查找结果:" + (this.dataSource == null)); + if (dataSource == null && CollectionUtils.isNotEmpty(environmentConfig.getDatabaseConfigs())) { + dataSource = environmentConfig.getDatabaseConfigs().get(0); + } } else { // 取当前环境下默认的一个数据源 if (config.isEffective(this.getProjectId())) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java index fab89919d1..ebaa0a0d46 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java @@ -34,6 +34,7 @@ import org.apache.jorphan.collections.HashTree; import org.json.JSONObject; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.util.*; @@ -86,6 +87,7 @@ public class ApiCaseSerialService { } } + @Transactional(propagation = Propagation.NOT_SUPPORTED) protected void updateDefinitionExecResultToRunning(ApiExecutionQueueDetail queue, JmeterRunRequestDTO runRequest) { ApiDefinitionExecResultWithBLOBs execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId()); if (execResult != null) {