From fd3afd84ad3f02fd7a5c51a780c76d843f887b5f Mon Sep 17 00:00:00 2001 From: Wenjun Ruan Date: Wed, 28 Dec 2022 11:40:12 +0800 Subject: [PATCH] Remove alert when delete workflow instance (#13281) --- .../api/controller/BaseController.java | 4 +- .../controller/ProcessInstanceController.java | 18 ++--- .../api/controller/ResourcesController.java | 18 ++--- .../v2/WorkflowInstanceV2Controller.java | 4 +- .../api/service/ProcessInstanceService.java | 18 +---- .../api/service/ResourcesService.java | 6 +- .../impl/ProcessInstanceServiceImpl.java | 60 +++++------------ .../service/impl/ResourcesServiceImpl.java | 12 +++- .../dolphinscheduler/api/utils/Result.java | 2 +- .../ProcessInstanceControllerTest.java | 10 +-- .../v2/WorkflowInstanceV2ControllerTest.java | 2 +- .../service/ProcessInstanceServiceTest.java | 65 +++++++------------ .../common/constants/Constants.java | 2 + .../apache/dolphinscheduler/dao/AlertDao.java | 7 ++ .../dao/mapper/AlertMapper.java | 1 + .../dao/mapper/AlertMapper.xml | 6 ++ .../service/alert/ProcessAlertManager.java | 5 ++ 17 files changed, 105 insertions(+), 135 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java index aed6dbabdf..89205621ad 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/BaseController.java @@ -47,8 +47,8 @@ public class BaseController { * @return check result code */ // todo: directly throw exception - public Result checkPageParams(int pageNo, int pageSize) { - Result result = new Result(); + public Result checkPageParams(int pageNo, int pageSize) { + Result result = new Result<>(); Status resultEnum = Status.SUCCESS; String msg = Status.SUCCESS.getMsg(); if (pageNo <= 0) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java index f75970e466..cf76fe646f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceController.java @@ -267,11 +267,11 @@ public class ProcessInstanceController extends BaseController { @ResponseStatus(HttpStatus.OK) @ApiException(Status.DELETE_PROCESS_INSTANCE_BY_ID_ERROR) @AccessLogAnnotation(ignoreRequestArgs = "loginUser") - public Result deleteProcessInstanceById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, - @PathVariable("id") Integer id) { - Map result = processInstanceService.deleteProcessInstanceById(loginUser, projectCode, id); - return returnDataList(result); + public Result deleteProcessInstanceById(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @Parameter(name = "projectCode", description = "PROJECT_CODE", required = true) @PathVariable long projectCode, + @PathVariable("id") Integer id) { + processInstanceService.deleteProcessInstanceById(loginUser, id); + return Result.success(); } /** @@ -396,13 +396,9 @@ public class ProcessInstanceController extends BaseController { for (String strProcessInstanceId : processInstanceIdArray) { int processInstanceId = Integer.parseInt(strProcessInstanceId); try { - Map deleteResult = - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, processInstanceId); - if (!Status.SUCCESS.equals(deleteResult.get(Constants.STATUS))) { - deleteFailedIdList.add((String) deleteResult.get(Constants.MSG)); - logger.error((String) deleteResult.get(Constants.MSG)); - } + processInstanceService.deleteProcessInstanceById(loginUser, processInstanceId); } catch (Exception e) { + logger.error("Delete workflow instance: {} error", strProcessInstanceId, e); deleteFailedIdList .add(MessageFormat.format(Status.PROCESS_INSTANCE_ERROR.getMsg(), strProcessInstanceId)); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java index c1d0fbae5c..db4cad6822 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/ResourcesController.java @@ -46,11 +46,13 @@ import org.apache.dolphinscheduler.api.dto.resources.DeleteDataTransferResponse; import org.apache.dolphinscheduler.api.exceptions.ApiException; import org.apache.dolphinscheduler.api.service.ResourcesService; import org.apache.dolphinscheduler.api.service.UdfFuncService; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.ProgramType; import org.apache.dolphinscheduler.common.enums.UdfType; import org.apache.dolphinscheduler.dao.entity.User; +import org.apache.dolphinscheduler.plugin.storage.api.StorageEntity; import org.apache.dolphinscheduler.plugin.task.api.utils.ParameterUtils; import org.apache.dolphinscheduler.spi.enums.ResourceType; @@ -233,14 +235,14 @@ public class ResourcesController extends BaseController { @ResponseStatus(HttpStatus.OK) @ApiException(QUERY_RESOURCES_LIST_PAGING) @AccessLogAnnotation(ignoreRequestArgs = "loginUser") - public Result queryResourceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @RequestParam(value = "fullName") String fullName, - @RequestParam(value = "tenantCode") String tenantCode, - @RequestParam(value = "type") ResourceType type, - @RequestParam("pageNo") Integer pageNo, - @RequestParam(value = "searchVal", required = false) String searchVal, - @RequestParam("pageSize") Integer pageSize) { - Result result = checkPageParams(pageNo, pageSize); + public Result> queryResourceListPaging(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @RequestParam(value = "fullName") String fullName, + @RequestParam(value = "tenantCode") String tenantCode, + @RequestParam(value = "type") ResourceType type, + @RequestParam("pageNo") Integer pageNo, + @RequestParam(value = "searchVal", required = false) String searchVal, + @RequestParam("pageSize") Integer pageSize) { + Result> result = checkPageParams(pageNo, pageSize); if (!result.checkResult()) { return result; } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java index d61c63ff7f..92c231f2d7 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2Controller.java @@ -122,8 +122,8 @@ public class WorkflowInstanceV2Controller extends BaseController { @ResponseStatus(HttpStatus.OK) @ApiException(Status.DELETE_PROCESS_DEFINE_BY_CODE_ERROR) @AccessLogAnnotation(ignoreRequestArgs = "loginUser") - public Result deleteWorkflowInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, - @PathVariable("workflowInstanceId") Integer workflowInstanceId) { + public Result deleteWorkflowInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser, + @PathVariable("workflowInstanceId") Integer workflowInstanceId) { processInstanceService.deleteProcessInstanceById(loginUser, workflowInstanceId); return Result.success(); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java index 7ff2aa9091..fc60773839 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java @@ -175,24 +175,12 @@ public interface ProcessInstanceService { /** * delete process instance by id, at the same time,delete task instance and their mapping relation data * - * @param loginUser login user - * @param projectCode project code + * @param loginUser login user * @param processInstanceId process instance id * @return delete result code */ - Map deleteProcessInstanceById(User loginUser, - long projectCode, - Integer processInstanceId); - - /** - * delete process instance by id, at the same time,delete task instance and their mapping relation data - * - * @param loginUser login user - * @param workflowInstanceId work instance id - * @return delete result code - */ - Map deleteProcessInstanceById(User loginUser, - Integer workflowInstanceId); + void deleteProcessInstanceById(User loginUser, + Integer processInstanceId); /** * view process instance variables diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java index 094a60e6db..82c1a4c01b 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ResourcesService.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.service; import org.apache.dolphinscheduler.api.dto.resources.DeleteDataTransferResponse; +import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.enums.ProgramType; import org.apache.dolphinscheduler.dao.entity.User; @@ -97,8 +98,9 @@ public interface ResourcesService { * @param pageSize page size * @return resource list page */ - Result queryResourceListPaging(User loginUser, String fullName, String resTenantCode, - ResourceType type, String searchVal, Integer pageNo, Integer pageSize); + Result> queryResourceListPaging(User loginUser, String fullName, String resTenantCode, + ResourceType type, String searchVal, Integer pageNo, + Integer pageSize); /** * query resource list diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java index 5d0b00f721..6dcbcaa5f6 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java @@ -50,6 +50,7 @@ import org.apache.dolphinscheduler.common.model.TaskNodeRelation; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.common.utils.placeholder.BusinessTimeUtils; +import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessTaskRelationLog; @@ -174,6 +175,9 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce @Autowired private ScheduleMapper scheduleMapper; + @Autowired + private AlertDao alertDao; + @Autowired private CuringParamsService curingGlobalParamsService; @@ -781,23 +785,21 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce * delete process instance by id, at the same time,delete task instance and their mapping relation data * * @param loginUser login user - * @param projectCode project code * @param processInstanceId process instance id * @return delete result code */ @Override @Transactional - public Map deleteProcessInstanceById(User loginUser, long projectCode, Integer processInstanceId) { - Project project = projectMapper.queryByCode(projectCode); - // check user access for project - Map result = - projectService.checkProjectAndAuth(loginUser, project, projectCode, - ApiFuncIdentificationConstant.INSTANCE_DELETE); - if (result.get(Constants.STATUS) != Status.SUCCESS) { - return result; - } + public void deleteProcessInstanceById(User loginUser, Integer processInstanceId) { ProcessInstance processInstance = processService.findProcessInstanceDetailById(processInstanceId) .orElseThrow(() -> new ServiceException(PROCESS_INSTANCE_NOT_EXIST, processInstanceId)); + ProcessDefinition processDefinition = processDefinitionLogMapper.queryByDefinitionCodeAndVersion( + processInstance.getProcessDefinitionCode(), processInstance.getProcessDefinitionVersion()); + + Project project = projectMapper.queryByCode(processDefinition.getProjectCode()); + // check user access for project + projectService.checkProjectAndAuthThrowException(loginUser, project, + ApiFuncIdentificationConstant.INSTANCE_DELETE); // check process instance status if (!processInstance.getState().isFinished()) { logger.warn("Process Instance state is {} so can not delete process instance, processInstanceId:{}.", @@ -806,14 +808,6 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce processInstance.getState(), "delete"); } - ProcessDefinition processDefinition = - processDefineMapper.queryByCode(processInstance.getProcessDefinitionCode()); - if (processDefinition != null && projectCode != processDefinition.getProjectCode()) { - logger.error("Process definition does not exist, projectCode:{}, ProcessDefinitionCode:{}.", - projectCode, processInstance.getProcessDefinitionCode()); - throw new ServiceException(PROCESS_INSTANCE_NOT_EXIST, processInstanceId); - } - // delete database cascade int delete = processService.deleteWorkProcessInstanceById(processInstanceId); @@ -824,38 +818,18 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce // When delete task instance error, the task log file will also be deleted, this may cause data inconsistency. processService.removeTaskLogFile(processInstanceId); taskInstanceDao.deleteByWorkflowInstanceId(processInstanceId); + alertDao.deleteByWorkflowInstanceId(processInstanceId); if (delete > 0) { logger.info( - "Delete process instance complete, projectCode:{}, ProcessDefinitionCode{}, processInstanceId:{}.", - projectCode, processInstance.getProcessDefinitionCode(), processInstanceId); - putMsg(result, Status.SUCCESS); + "Delete process instance complete, ProcessDefinitionCode{}, processInstanceId:{}.", + processInstance.getProcessDefinitionCode(), processInstanceId); } else { logger.error( - "Delete process instance error, projectCode:{}, ProcessDefinitionCode{}, processInstanceId:{}.", - projectCode, processInstance.getProcessDefinitionCode(), processInstanceId); - putMsg(result, Status.DELETE_PROCESS_INSTANCE_BY_ID_ERROR); + "Delete process instance error, ProcessDefinitionCode{}, processInstanceId:{}.", + processInstance.getProcessDefinitionCode(), processInstanceId); throw new ServiceException(Status.DELETE_PROCESS_INSTANCE_BY_ID_ERROR); } - - return result; - } - - /** - * delete workflow instance by id, at the same time,delete task instance and their mapping relation data - * - * @param loginUser login user - * @param workflowInstanceId workflow instance id - * @return delete result code - */ - @Override - public Map deleteProcessInstanceById(User loginUser, Integer workflowInstanceId) { - ProcessInstance processInstance = processService.findProcessInstanceDetailById(workflowInstanceId) - .orElseThrow(() -> new ServiceException(PROCESS_INSTANCE_NOT_EXIST, workflowInstanceId)); - ProcessDefinition processDefinition = - processDefineMapper.queryByCode(processInstance.getProcessDefinitionCode()); - - return deleteProcessInstanceById(loginUser, processDefinition.getProjectCode(), workflowInstanceId); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java index a3d5c16846..3ef1d18b1f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java @@ -679,10 +679,16 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe * @return resource list page */ @Override - public Result queryResourceListPaging(User loginUser, String fullName, String resTenantCode, - ResourceType type, String searchVal, Integer pageNo, Integer pageSize) { - Result result = new Result<>(); + public Result> queryResourceListPaging(User loginUser, String fullName, + String resTenantCode, + ResourceType type, String searchVal, Integer pageNo, + Integer pageSize) { + Result> result = new Result<>(); PageInfo pageInfo = new PageInfo<>(pageNo, pageSize); + if (storageOperate == null) { + logger.warn("The resource storage is not opened."); + return Result.success(pageInfo); + } User user = userMapper.selectById(loginUser.getId()); if (user == null) { diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/Result.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/Result.java index a08f056b90..90273e5bb4 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/Result.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/utils/Result.java @@ -75,7 +75,7 @@ public class Result { return new Result<>(Status.SUCCESS.getCode(), Status.SUCCESS.getMsg(), data); } - public static Result success() { + public static Result success() { return success(null); } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java index 4f9a6fd57c..5fa88ea52a 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/ProcessInstanceControllerTest.java @@ -218,9 +218,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest { public void testDeleteProcessInstanceById() throws Exception { Map mockResult = new HashMap<>(); mockResult.put(Constants.STATUS, Status.SUCCESS); - Mockito.when( - processInstanceService.deleteProcessInstanceById(Mockito.any(), Mockito.anyLong(), Mockito.anyInt())) - .thenReturn(mockResult); + Mockito.doNothing().when(processInstanceService).deleteProcessInstanceById(Mockito.any(), Mockito.anyInt()); MvcResult mvcResult = mockMvc.perform(delete("/projects/{projectCode}/process-instances/{id}", "1113", "123") .header(SESSION_ID, sessionId)) @@ -238,9 +236,7 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest { Map mockResult = new HashMap<>(); mockResult.put(Constants.STATUS, Status.PROCESS_INSTANCE_NOT_EXIST); - Mockito.when( - processInstanceService.deleteProcessInstanceById(Mockito.any(), Mockito.anyLong(), Mockito.anyInt())) - .thenReturn(mockResult); + Mockito.doNothing().when(processInstanceService).deleteProcessInstanceById(Mockito.any(), Mockito.anyInt()); MvcResult mvcResult = mockMvc.perform(post("/projects/{projectCode}/process-instances/batch-delete", "1113") .header(SESSION_ID, sessionId) .param("processInstanceIds", "1205,1206")) @@ -250,6 +246,6 @@ public class ProcessInstanceControllerTest extends AbstractControllerTest { Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); Assertions.assertNotNull(result); - Assertions.assertEquals(Status.DELETE_PROCESS_INSTANCE_BY_ID_ERROR.getCode(), result.getCode().intValue()); + Assertions.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2ControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2ControllerTest.java index 7beabf1a4d..9b72a1c970 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2ControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/v2/WorkflowInstanceV2ControllerTest.java @@ -95,7 +95,7 @@ public class WorkflowInstanceV2ControllerTest extends AbstractControllerTest { public void testDeleteWorkflowInstanceById() { User loginUser = getLoginUser(); - Mockito.when(processInstanceService.deleteProcessInstanceById(any(), eq(1))).thenReturn(null); + Mockito.doNothing().when(processInstanceService).deleteProcessInstanceById(any(), eq(1)); Result result = workflowInstanceV2Controller.deleteWorkflowInstance(loginUser, 1); Assertions.assertTrue(result.isSuccess()); } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java index 2ddefced0c..7c40b6d2c6 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ProcessInstanceServiceTest.java @@ -38,6 +38,7 @@ import org.apache.dolphinscheduler.common.graph.DAG; import org.apache.dolphinscheduler.common.model.TaskNodeRelation; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessDefinitionLog; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; @@ -146,6 +147,9 @@ public class ProcessInstanceServiceTest { @Mock CuringParamsService curingGlobalParamsService; + @Mock + AlertDao alertDao; + private String shellJson = "[{\"name\":\"\",\"preTaskCode\":0,\"preTaskVersion\":0,\"postTaskCode\":123456789," + "\"postTaskVersion\":1,\"conditionType\":0,\"conditionParams\":\"{}\"},{\"name\":\"\",\"preTaskCode\":123456789," + "\"preTaskVersion\":1,\"postTaskCode\":123451234,\"postTaskVersion\":1,\"conditionType\":0,\"conditionParams\":\"{}\"}]"; @@ -522,15 +526,10 @@ public class ProcessInstanceServiceTest { when(projectMapper.queryByCode(projectCode)).thenReturn(project); when(projectService.checkProjectAndAuth(loginUser, project, projectCode, INSTANCE_UPDATE)).thenReturn(result); when(processService.findProcessInstanceDetailById(1)).thenReturn(Optional.empty()); - try { - Map processInstanceNullRes = - processInstanceService.updateProcessInstance(loginUser, projectCode, 1, - shellJson, taskJson, "2020-02-21 00:00:00", true, "", "", 0, ""); - Assertions.fail(); - } catch (ServiceException ex) { - Assertions.assertEquals(Status.PROCESS_INSTANCE_NOT_EXIST.getCode(), ex.getCode()); - } - + Assertions.assertThrows(ServiceException.class, () -> { + processInstanceService.updateProcessInstance(loginUser, projectCode, 1, + shellJson, taskJson, "2020-02-21 00:00:00", true, "", "", 0, ""); + }); // process instance not finish when(processService.findProcessInstanceDetailById(1)).thenReturn(Optional.ofNullable(processInstance)); processInstance.setState(WorkflowExecutionStatus.RUNNING_EXECUTION); @@ -598,13 +597,9 @@ public class ProcessInstanceServiceTest { when(projectMapper.queryByCode(projectCode)).thenReturn(project); when(projectService.checkProjectAndAuth(loginUser, project, projectCode, WORKFLOW_INSTANCE)).thenReturn(result); when(processService.findProcessInstanceDetailById(1)).thenReturn(Optional.empty()); - try { - Map processInstanceNullRes = - processInstanceService.queryParentInstanceBySubId(loginUser, projectCode, 1); - - } catch (ServiceException ex) { - Assertions.assertEquals(Status.PROCESS_INSTANCE_NOT_EXIST.getCode(), ex.getCode()); - } + Assertions.assertThrows(ServiceException.class, () -> { + processInstanceService.queryParentInstanceBySubId(loginUser, projectCode, 1); + }); // not sub process ProcessInstance processInstance = getProcessInstance(); @@ -642,9 +637,9 @@ public class ProcessInstanceServiceTest { // project auth fail when(projectMapper.queryByCode(projectCode)).thenReturn(project); when(projectService.checkProjectAndAuth(loginUser, project, projectCode, INSTANCE_DELETE)).thenReturn(result); - Map projectAuthFailRes = - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, 1); - Assertions.assertEquals(Status.PROJECT_NOT_FOUND, projectAuthFailRes.get(Constants.STATUS)); + + Assertions.assertThrows(ServiceException.class, + () -> processInstanceService.deleteProcessInstanceById(loginUser, 1)); // not sub process ProcessInstance processInstance = getProcessInstance(); @@ -652,12 +647,10 @@ public class ProcessInstanceServiceTest { processInstance.setState(WorkflowExecutionStatus.RUNNING_EXECUTION); putMsg(result, Status.SUCCESS, projectCode); when(processService.findProcessInstanceDetailById(1)).thenReturn(Optional.ofNullable(processInstance)); - try { - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, 1); - Assertions.fail(); - } catch (ServiceException ex) { - Assertions.assertEquals(Status.PROCESS_INSTANCE_STATE_OPERATION_ERROR.getCode(), ex.getCode()); - } + when(processDefinitionLogMapper.queryByDefinitionCodeAndVersion(Mockito.anyLong(), Mockito.anyInt())) + .thenReturn(new ProcessDefinitionLog()); + Assertions.assertThrows(ServiceException.class, + () -> processInstanceService.deleteProcessInstanceById(loginUser, 1)); processInstance.setState(WorkflowExecutionStatus.SUCCESS); processInstance.setState(WorkflowExecutionStatus.SUCCESS); @@ -670,26 +663,18 @@ public class ProcessInstanceServiceTest { processDefinition.setUserId(1); processDefinition.setProjectCode(0L); when(processDefineMapper.queryByCode(46L)).thenReturn(processDefinition); - try { - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, 1); - Assertions.fail(); - } catch (ServiceException ex) { - Assertions.assertEquals(Status.PROCESS_INSTANCE_NOT_EXIST.getCode(), ex.getCode()); - } + when(processService.findProcessInstanceDetailById(Mockito.anyInt())).thenReturn(Optional.empty()); + Assertions.assertThrows(ServiceException.class, + () -> processInstanceService.deleteProcessInstanceById(loginUser, 1)); processDefinition.setProjectCode(projectCode); + when(processService.findProcessInstanceDetailById(Mockito.anyInt())).thenReturn(Optional.of(processInstance)); when(processService.deleteWorkProcessInstanceById(1)).thenReturn(1); - Map successRes = - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, 1); - Assertions.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS)); + processInstanceService.deleteProcessInstanceById(loginUser, 1); when(processService.deleteWorkProcessInstanceById(1)).thenReturn(0); - try { - processInstanceService.deleteProcessInstanceById(loginUser, projectCode, 1); - Assertions.fail(); - } catch (ServiceException ex) { - Assertions.assertEquals(Status.DELETE_PROCESS_INSTANCE_BY_ID_ERROR.getCode(), ex.getCode()); - } + Assertions.assertThrows(ServiceException.class, + () -> processInstanceService.deleteProcessInstanceById(loginUser, 1)); } @Test diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/constants/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/constants/Constants.java index b72520f14e..cd72a843e0 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/constants/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/constants/Constants.java @@ -28,6 +28,8 @@ public final class Constants { throw new UnsupportedOperationException("Construct Constants"); } + public static final String AUTO_CLOSE_ALERT = "alert.auto-close"; + /** * common properties path */ diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java index 470dffb588..58bf333f2e 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/AlertDao.java @@ -333,4 +333,11 @@ public class AlertDao { public void setCrashAlarmSuppression(Integer crashAlarmSuppression) { this.crashAlarmSuppression = crashAlarmSuppression; } + + public void deleteByWorkflowInstanceId(Integer processInstanceId) { + if (processInstanceId == null) { + return; + } + alertMapper.deleteByWorkflowInstanceId(processInstanceId); + } } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertMapper.java index d5563be518..d96101101e 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertMapper.java @@ -45,4 +45,5 @@ public interface AlertMapper extends BaseMapper { void insertAlertWhenServerCrash(@Param("alert") Alert alert, @Param("crashAlarmSuppressionStartTime") Date crashAlarmSuppressionStartTime); + void deleteByWorkflowInstanceId(@Param("workflowInstanceId") Integer processInstanceId); } diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertMapper.xml index 274c88cf02..a9054986d3 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertMapper.xml @@ -52,4 +52,10 @@ where alert_status = #{alertStatus} limit #{limit} + + + delete + from t_ds_alert + where process_instance_id = #{workflowInstanceId} + diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManager.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManager.java index 2604354fbc..c568343931 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManager.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/alert/ProcessAlertManager.java @@ -17,11 +17,13 @@ package org.apache.dolphinscheduler.service.alert; +import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.AlertType; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.common.utils.PropertyUtils; import org.apache.dolphinscheduler.dao.AlertDao; import org.apache.dolphinscheduler.dao.entity.Alert; import org.apache.dolphinscheduler.dao.entity.DqExecuteResult; @@ -275,6 +277,9 @@ public class ProcessAlertManager { * @param processInstance success process instance */ public void closeAlert(ProcessInstance processInstance) { + if (!PropertyUtils.getBoolean(Constants.AUTO_CLOSE_ALERT, false)) { + return; + } List alerts = alertDao.listAlerts(processInstance.getId()); if (CollectionUtils.isEmpty(alerts)) { // no need to close alert