From 5d277e588ac770abb47aa6dd7e6ed721b888c8ea Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 19 Jan 2022 16:19:35 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B9=B6=E8=A1=8C=E6=88=96=E4=B8=B2=E8=A1=8C?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E8=AE=A1=E5=88=92=E6=8A=A5=E5=91=8A=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E6=9C=AA=E7=BB=93=E6=9D=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/ApiCaseParallelExecuteService.java | 1 + .../scenario/ApiScenarioParallelService.java | 1 + .../api/jmeter/APISingleResultListener.java | 5 ++ .../api/jmeter/MsKafkaListener.java | 6 ++- .../api/service/ApiExecutionQueueService.java | 49 ++++++++++++++----- .../mapper/ext/ExtApiExecutionQueueMapper.xml | 2 +- 6 files changed, 51 insertions(+), 13 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java index a7b2e4f482..7fe4b8d6f6 100644 --- a/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java +++ b/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseParallelExecuteService.java @@ -38,6 +38,7 @@ public class ApiCaseParallelExecuteService { } JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(testId, reportId, runMode, hashTree); runRequest.setPool(GenerateHashTreeUtil.isResourcePool(config.getResourcePoolId())); + runRequest.setTestPlanReportId(executionQueue.getReportId()); runRequest.setPoolId(config.getResourcePoolId()); runRequest.setReportType(executionQueue.getReportType()); runRequest.setRunType(RunModeConstants.PARALLEL.toString()); diff --git a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java index ff36ce475e..3ef285c1cb 100644 --- a/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java +++ b/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioParallelService.java @@ -58,6 +58,7 @@ public class ApiScenarioParallelService { runRequest.setTestPlanReportId(request.getTestPlanReportId()); runRequest.setHashTree(executeQueue.get(reportId).getHashTree()); runRequest.setPlatformUrl(executionQueue.getDetailMap().get(reportId)); + runRequest.setRunType(RunModeConstants.PARALLEL.toString()); if (LoggerUtil.getLogger().isDebugEnabled()) { LoggerUtil.debug("Scenario run-开始并发执行:" + JSON.toJSONString(request)); } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java index 5f971d4eeb..3c81743e70 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APISingleResultListener.java @@ -43,6 +43,11 @@ public class APISingleResultListener extends MsExecListener { if (StringUtils.isNotEmpty(dto.getQueueId())) { CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(dto); } + // 更新测试计划报告 + if (StringUtils.isNotEmpty(dto.getTestPlanReportId())) { + LoggerUtil.info("Check Processing Test Plan report status:" + dto.getQueueId() + "," + dto.getTestId()); + CommonBeanFactory.getBean(ApiExecutionQueueService.class).testPlanReportTestEnded(dto.getTestPlanReportId()); + } } catch (Exception e) { LoggerUtil.error(e); } diff --git a/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java b/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java index 0ebbf92243..510f94dc1c 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/MsKafkaListener.java @@ -34,6 +34,11 @@ public class MsKafkaListener { CommonBeanFactory.getBean(ApiExecutionQueueService.class).queueNext(testResult); // 全局并发队列 PoolExecBlockingQueueUtil.offer(testResult.getReportId()); + // 更新测试计划报告 + if (StringUtils.isNotEmpty(testResult.getTestPlanReportId())) { + LoggerUtil.info("Check Processing Test Plan report status:" + testResult.getQueueId() + "," + testResult.getTestId()); + CommonBeanFactory.getBean(ApiExecutionQueueService.class).testPlanReportTestEnded(testResult.getTestPlanReportId()); + } } else { // 更新报告最后接收到请求的时间 if (StringUtils.equalsAny(testResult.getRunMode(), ApiRunMode.SCENARIO.name(), @@ -41,7 +46,6 @@ public class MsKafkaListener { ApiRunMode.SCHEDULE_SCENARIO.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) { CommonBeanFactory.getBean(TestResultService.class).editReportTime(testResult); } - testResultService.saveResults(testResult); } LoggerUtil.info("执行内容存储结束"); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java index e145679d9a..99bfdb92a7 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiExecutionQueueService.java @@ -201,7 +201,45 @@ public class ApiExecutionQueueService { return queue; } + public void testPlanReportTestEnded(String testPlanReportId) { + // 检查测试计划中其他队列是否结束 + ApiExecutionQueueExample apiExecutionQueueExample = new ApiExecutionQueueExample(); + apiExecutionQueueExample.createCriteria().andReportIdEqualTo(testPlanReportId); + List queues = queueMapper.selectByExample(apiExecutionQueueExample); + if (CollectionUtils.isEmpty(queues)) { + LoggerUtil.info("Normal execution completes, update test plan report status:" + testPlanReportId); + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(testPlanReportId, TestPlanReportStatus.COMPLETED.name()); + } else { + List ids = queues.stream().map(ApiExecutionQueue::getId).collect(Collectors.toList()); + ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); + example.createCriteria().andQueueIdIn(ids); + long count = executionQueueDetailMapper.countByExample(example); + if (count == 0) { + LoggerUtil.info("Normal execution completes, update test plan report status:" + testPlanReportId); + CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(testPlanReportId, TestPlanReportStatus.COMPLETED.name()); + + LoggerUtil.info("Clear Queue:" + ids); + ApiExecutionQueueExample queueExample = new ApiExecutionQueueExample(); + queueExample.createCriteria().andIdIn(ids); + queueMapper.deleteByExample(queueExample); + } + } + } + public void queueNext(ResultDTO dto) { + if (StringUtils.equals(dto.getRunType(), RunModeConstants.PARALLEL.toString())) { + ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); + example.createCriteria().andQueueIdEqualTo(dto.getQueueId()).andTestIdEqualTo(dto.getTestId()); + executionQueueDetailMapper.deleteByExample(example); + // 检查队列是否已空 + ApiExecutionQueueDetailExample queueDetailExample = new ApiExecutionQueueDetailExample(); + queueDetailExample.createCriteria().andQueueIdEqualTo(dto.getQueueId()); + long count = executionQueueDetailMapper.countByExample(queueDetailExample); + if (count == 0) { + queueMapper.deleteByPrimaryKey(dto.getQueueId()); + } + return; + } DBTestQueue executionQueue = this.handleQueue(dto.getQueueId(), dto.getTestId()); if (executionQueue != null) { // 串行失败停止 @@ -211,7 +249,6 @@ public class ApiExecutionQueueService { return; } } - LoggerUtil.info("开始处理执行队列:" + executionQueue.getId() + " 当前资源是:" + dto.getTestId()); if (executionQueue.getQueue() != null && StringUtils.isNotEmpty(executionQueue.getQueue().getTestId())) { if (StringUtils.equals(dto.getRunType(), RunModeConstants.SERIAL.toString())) { @@ -222,16 +259,6 @@ public class ApiExecutionQueueService { if (StringUtils.equals(dto.getReportType(), RunModeConstants.SET_REPORT.toString())) { apiScenarioReportService.margeReport(dto.getReportId()); } - // 更新测试计划报告 - if (StringUtils.isNotEmpty(dto.getTestPlanReportId())) { - ApiExecutionQueueDetailExample example = new ApiExecutionQueueDetailExample(); - example.createCriteria().andQueueIdEqualTo(dto.getQueueId()); - long count = executionQueueDetailMapper.countByExample(example); - if (count == 0) { - LoggerUtil.info("Normal execution completes, update test plan report status:" + dto.getTestPlanReportId()); - CommonBeanFactory.getBean(TestPlanReportService.class).finishedTestPlanReport(dto.getTestPlanReportId(), TestPlanReportStatus.COMPLETED.name()); - } - } queueMapper.deleteByPrimaryKey(dto.getQueueId()); LoggerUtil.info("Queue execution ends:" + dto.getQueueId()); } diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiExecutionQueueMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiExecutionQueueMapper.xml index 375e855aad..e44ce476d1 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiExecutionQueueMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiExecutionQueueMapper.xml @@ -9,6 +9,6 @@ \ No newline at end of file