Search in sources :

Example 1 with ProcessInstance

use of org.apache.dolphinscheduler.dao.entity.ProcessInstance in project dolphinscheduler by apache.

the class ProcessDefinitionService method viewTree.

/**
 * Encapsulates the TreeView structure
 *
 * @param processId process definition id
 * @param limit limit
 * @return tree view json data
 * @throws Exception exception
 */
public Map<String, Object> viewTree(Integer processId, Integer limit) throws Exception {
    Map<String, Object> result = new HashMap<>();
    ProcessDefinition processDefinition = processDefineMapper.selectById(processId);
    if (null == processDefinition) {
        logger.info("process define not exists");
        putMsg(result, Status.PROCESS_DEFINE_NOT_EXIST, processDefinition);
        return result;
    }
    DAG<String, TaskNode, TaskNodeRelation> dag = genDagGraph(processDefinition);
    /**
     * nodes that is running
     */
    Map<String, List<TreeViewDto>> runningNodeMap = new ConcurrentHashMap<>();
    /**
     * nodes that is waiting torun
     */
    Map<String, List<TreeViewDto>> waitingRunningNodeMap = new ConcurrentHashMap<>();
    /**
     * List of process instances
     */
    List<ProcessInstance> processInstanceList = processInstanceMapper.queryByProcessDefineId(processId, limit);
    for (ProcessInstance processInstance : processInstanceList) {
        processInstance.setDuration(DateUtils.differSec(processInstance.getStartTime(), processInstance.getEndTime()));
    }
    if (limit > processInstanceList.size()) {
        limit = processInstanceList.size();
    }
    TreeViewDto parentTreeViewDto = new TreeViewDto();
    parentTreeViewDto.setName("DAG");
    parentTreeViewDto.setType("");
    for (int i = limit - 1; i >= 0; i--) {
        ProcessInstance processInstance = processInstanceList.get(i);
        Date endTime = processInstance.getEndTime() == null ? new Date() : processInstance.getEndTime();
        parentTreeViewDto.getInstances().add(new Instance(processInstance.getId(), processInstance.getName(), "", processInstance.getState().toString(), processInstance.getStartTime(), endTime, processInstance.getHost(), DateUtils.format2Readable(endTime.getTime() - processInstance.getStartTime().getTime())));
    }
    List<TreeViewDto> parentTreeViewDtoList = new ArrayList<>();
    parentTreeViewDtoList.add(parentTreeViewDto);
    // Here is the encapsulation task instance
    for (String startNode : dag.getBeginNode()) {
        runningNodeMap.put(startNode, parentTreeViewDtoList);
    }
    while (Stopper.isRunning()) {
        Set<String> postNodeList = null;
        Iterator<Map.Entry<String, List<TreeViewDto>>> iter = runningNodeMap.entrySet().iterator();
        while (iter.hasNext()) {
            Map.Entry<String, List<TreeViewDto>> en = iter.next();
            String nodeName = en.getKey();
            parentTreeViewDtoList = en.getValue();
            TreeViewDto treeViewDto = new TreeViewDto();
            treeViewDto.setName(nodeName);
            TaskNode taskNode = dag.getNode(nodeName);
            treeViewDto.setType(taskNode.getType());
            // set treeViewDto instances
            for (int i = limit - 1; i >= 0; i--) {
                ProcessInstance processInstance = processInstanceList.get(i);
                TaskInstance taskInstance = taskInstanceMapper.queryByInstanceIdAndName(processInstance.getId(), nodeName);
                if (taskInstance == null) {
                    treeViewDto.getInstances().add(new Instance(-1, "not running", null));
                } else {
                    Date startTime = taskInstance.getStartTime() == null ? new Date() : taskInstance.getStartTime();
                    Date endTime = taskInstance.getEndTime() == null ? new Date() : taskInstance.getEndTime();
                    int subProcessId = 0;
                    /**
                     * if process is sub process, the return sub id, or sub id=0
                     */
                    if (taskInstance.getTaskType().equals(TaskType.SUB_PROCESS.name())) {
                        String taskJson = taskInstance.getTaskJson();
                        taskNode = JSON.parseObject(taskJson, TaskNode.class);
                        subProcessId = Integer.parseInt(JSON.parseObject(taskNode.getParams()).getString(CMDPARAM_SUB_PROCESS_DEFINE_ID));
                    }
                    treeViewDto.getInstances().add(new Instance(taskInstance.getId(), taskInstance.getName(), taskInstance.getTaskType(), taskInstance.getState().toString(), taskInstance.getStartTime(), taskInstance.getEndTime(), taskInstance.getHost(), DateUtils.format2Readable(endTime.getTime() - startTime.getTime()), subProcessId));
                }
            }
            for (TreeViewDto pTreeViewDto : parentTreeViewDtoList) {
                pTreeViewDto.getChildren().add(treeViewDto);
            }
            postNodeList = dag.getSubsequentNodes(nodeName);
            if (CollectionUtils.isNotEmpty(postNodeList)) {
                for (String nextNodeName : postNodeList) {
                    List<TreeViewDto> treeViewDtoList = waitingRunningNodeMap.get(nextNodeName);
                    if (CollectionUtils.isNotEmpty(treeViewDtoList)) {
                        treeViewDtoList.add(treeViewDto);
                        waitingRunningNodeMap.put(nextNodeName, treeViewDtoList);
                    } else {
                        treeViewDtoList = new ArrayList<>();
                        treeViewDtoList.add(treeViewDto);
                        waitingRunningNodeMap.put(nextNodeName, treeViewDtoList);
                    }
                }
            }
            runningNodeMap.remove(nodeName);
        }
        if (waitingRunningNodeMap == null || waitingRunningNodeMap.size() == 0) {
            break;
        } else {
            runningNodeMap.putAll(waitingRunningNodeMap);
            waitingRunningNodeMap.clear();
        }
    }
    result.put(Constants.DATA_LIST, parentTreeViewDto);
    result.put(Constants.STATUS, Status.SUCCESS);
    result.put(Constants.MSG, Status.SUCCESS.getMsg());
    return result;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Instance(org.apache.dolphinscheduler.api.dto.treeview.Instance) TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) ArrayList(java.util.ArrayList) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) TaskNodeRelation(org.apache.dolphinscheduler.common.model.TaskNodeRelation) List(java.util.List) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) TaskNode(org.apache.dolphinscheduler.common.model.TaskNode) Date(java.util.Date) JSONObject(com.alibaba.fastjson.JSONObject) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) TreeViewDto(org.apache.dolphinscheduler.api.dto.treeview.TreeViewDto) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 2 with ProcessInstance

use of org.apache.dolphinscheduler.dao.entity.ProcessInstance in project dolphinscheduler by apache.

the class ProcessDefinitionServiceTest method testViewTree.

@Test
public void testViewTree() throws Exception {
    // process definition not exist
    ProcessDefinition processDefinition = getProcessDefinition();
    processDefinition.setProcessDefinitionJson(shellJson);
    Mockito.when(processDefineMapper.selectById(46)).thenReturn(null);
    Map<String, Object> processDefinitionNullRes = processDefinitionService.viewTree(46, 10);
    Assert.assertEquals(Status.PROCESS_DEFINE_NOT_EXIST, processDefinitionNullRes.get(Constants.STATUS));
    List<ProcessInstance> processInstanceList = new ArrayList<>();
    ProcessInstance processInstance = new ProcessInstance();
    processInstance.setId(1);
    processInstance.setName("test_instance");
    processInstance.setState(ExecutionStatus.RUNNING_EXEUTION);
    processInstance.setHost("192.168.xx.xx");
    processInstance.setStartTime(new Date());
    processInstance.setEndTime(new Date());
    processInstanceList.add(processInstance);
    TaskInstance taskInstance = new TaskInstance();
    taskInstance.setStartTime(new Date());
    taskInstance.setEndTime(new Date());
    taskInstance.setTaskType("SHELL");
    taskInstance.setId(1);
    taskInstance.setName("test_task_instance");
    taskInstance.setState(ExecutionStatus.RUNNING_EXEUTION);
    taskInstance.setHost("192.168.xx.xx");
    // task instance not exist
    Mockito.when(processDefineMapper.selectById(46)).thenReturn(processDefinition);
    Mockito.when(processInstanceMapper.queryByProcessDefineId(46, 10)).thenReturn(processInstanceList);
    Mockito.when(taskInstanceMapper.queryByInstanceIdAndName(processInstance.getId(), "shell-1")).thenReturn(null);
    Map<String, Object> taskNullRes = processDefinitionService.viewTree(46, 10);
    Assert.assertEquals(Status.SUCCESS, taskNullRes.get(Constants.STATUS));
    // task instance exist
    Mockito.when(taskInstanceMapper.queryByInstanceIdAndName(processInstance.getId(), "shell-1")).thenReturn(taskInstance);
    Map<String, Object> taskNotNuLLRes = processDefinitionService.viewTree(46, 10);
    Assert.assertEquals(Status.SUCCESS, taskNotNuLLRes.get(Constants.STATUS));
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) ArrayList(java.util.ArrayList) ProcessDefinition(org.apache.dolphinscheduler.dao.entity.ProcessDefinition) JSONObject(com.alibaba.fastjson.JSONObject) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Date(java.util.Date) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 3 with ProcessInstance

use of org.apache.dolphinscheduler.dao.entity.ProcessInstance in project dolphinscheduler by apache.

the class TaskInstanceServiceTest method queryTaskListPaging.

@Test
public void queryTaskListPaging() {
    String projectName = "project_test1";
    User loginUser = getAdminUser();
    Map<String, Object> result = new HashMap<>(5);
    putMsg(result, Status.PROJECT_NOT_FOUNT, projectName);
    // project auth fail
    when(projectMapper.queryByName(projectName)).thenReturn(null);
    when(projectService.checkProjectAndAuth(loginUser, null, projectName)).thenReturn(result);
    Map<String, Object> proejctAuthFailRes = taskInstanceService.queryTaskListPaging(loginUser, "project_test1", 0, "", "test_user", "2019-02-26 19:48:00", "2019-02-26 19:48:22", "", null, "", 1, 20);
    Assert.assertEquals(Status.PROJECT_NOT_FOUNT, proejctAuthFailRes.get(Constants.STATUS));
    // project
    putMsg(result, Status.SUCCESS, projectName);
    Project project = getProject(projectName);
    Date start = DateUtils.getScheduleDate("2020-01-01 00:00:00");
    Date end = DateUtils.getScheduleDate("2020-01-02 00:00:00");
    ProcessInstance processInstance = getProcessInstance();
    TaskInstance taskInstance = getTaskInstance();
    List<TaskInstance> taskInstanceList = new ArrayList<>();
    Page<TaskInstance> pageReturn = new Page<>(1, 10);
    taskInstanceList.add(taskInstance);
    pageReturn.setRecords(taskInstanceList);
    when(projectMapper.queryByName(Mockito.anyString())).thenReturn(project);
    when(projectService.checkProjectAndAuth(loginUser, project, projectName)).thenReturn(result);
    when(usersService.queryUser(loginUser.getId())).thenReturn(loginUser);
    when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(loginUser.getId());
    when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn);
    when(usersService.queryUser(processInstance.getExecutorId())).thenReturn(loginUser);
    when(processService.findProcessInstanceDetailById(taskInstance.getProcessInstanceId())).thenReturn(processInstance);
    Map<String, Object> successRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "test_user", "2020-01-01 00:00:00", "2020-01-02 00:00:00", "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20);
    Assert.assertEquals(Status.SUCCESS, successRes.get(Constants.STATUS));
    // executor name empty
    when(taskInstanceMapper.queryTaskInstanceListPaging(Mockito.any(Page.class), eq(project.getId()), eq(1), eq(""), eq(""), eq(0), Mockito.any(), eq("192.168.xx.xx"), eq(start), eq(end))).thenReturn(pageReturn);
    Map<String, Object> executorEmptyRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "", "2020-01-01 00:00:00", "2020-01-02 00:00:00", "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20);
    Assert.assertEquals(Status.SUCCESS, executorEmptyRes.get(Constants.STATUS));
    // executor null
    when(usersService.queryUser(loginUser.getId())).thenReturn(null);
    when(usersService.getUserIdByName(loginUser.getUserName())).thenReturn(-1);
    Map<String, Object> executorNullRes = taskInstanceService.queryTaskListPaging(loginUser, projectName, 1, "", "test_user", "2020-01-01 00:00:00", "2020-01-02 00:00:00", "", ExecutionStatus.SUCCESS, "192.168.xx.xx", 1, 20);
    Assert.assertEquals(Status.SUCCESS, executorNullRes.get(Constants.STATUS));
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) User(org.apache.dolphinscheduler.dao.entity.User) Page(com.baomidou.mybatisplus.extension.plugins.pagination.Page) Project(org.apache.dolphinscheduler.dao.entity.Project) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ProcessInstance

use of org.apache.dolphinscheduler.dao.entity.ProcessInstance in project dolphinscheduler by apache.

the class TaskInstanceServiceTest method getProcessInstance.

/**
 * get Mock process instance
 * @return process instance
 */
private ProcessInstance getProcessInstance() {
    ProcessInstance processInstance = new ProcessInstance();
    processInstance.setId(1);
    processInstance.setName("test_process_instance");
    processInstance.setStartTime(new Date());
    processInstance.setEndTime(new Date());
    processInstance.setExecutorId(-1);
    return processInstance;
}
Also used : ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance)

Example 5 with ProcessInstance

use of org.apache.dolphinscheduler.dao.entity.ProcessInstance in project dolphinscheduler by apache.

the class ProcessServiceTest method testCreateSubCommand.

@Test
public void testCreateSubCommand() {
    ProcessService processService = new ProcessService();
    ProcessInstance parentInstance = new ProcessInstance();
    parentInstance.setProcessDefinitionId(1);
    parentInstance.setWarningType(WarningType.SUCCESS);
    parentInstance.setWarningGroupId(0);
    TaskInstance task = new TaskInstance();
    task.setTaskJson("{params:{processDefinitionId:100}}");
    task.setId(10);
    ProcessInstance childInstance = null;
    ProcessInstanceMap instanceMap = new ProcessInstanceMap();
    instanceMap.setParentProcessInstanceId(1);
    instanceMap.setParentTaskInstanceId(10);
    Command command = null;
    // father history: start; child null == command type: start
    parentInstance.setHistoryCmd("START_PROCESS");
    parentInstance.setCommandType(CommandType.START_PROCESS);
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_PROCESS, command.getCommandType());
    // father history: start,start failure; child null == command type: start
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("START_PROCESS,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_PROCESS, command.getCommandType());
    // father history: scheduler,start failure; child null == command type: scheduler
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("SCHEDULER,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.SCHEDULER, command.getCommandType());
    // father history: complement,start failure; child null == command type: complement
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("COMPLEMENT_DATA,START_FAILURE_TASK_PROCESS");
    parentInstance.setCommandParam("{complementStartDate:'2020-01-01',complementEndDate:'2020-01-10'}");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.COMPLEMENT_DATA, command.getCommandType());
    JSONObject complementDate = JSONUtils.parseObject(command.getCommandParam());
    Assert.assertEquals("2020-01-01", String.valueOf(complementDate.get(Constants.CMDPARAM_COMPLEMENT_DATA_START_DATE)));
    Assert.assertEquals("2020-01-10", String.valueOf(complementDate.get(Constants.CMDPARAM_COMPLEMENT_DATA_END_DATE)));
    // father history: start,failure,start failure; child not null == command type: start failure
    childInstance = new ProcessInstance();
    parentInstance.setCommandType(CommandType.START_FAILURE_TASK_PROCESS);
    parentInstance.setHistoryCmd("START_PROCESS,START_FAILURE_TASK_PROCESS");
    command = processService.createSubProcessCommand(parentInstance, childInstance, instanceMap, task);
    Assert.assertEquals(CommandType.START_FAILURE_TASK_PROCESS, command.getCommandType());
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) JSONObject(com.alibaba.fastjson.JSONObject) Command(org.apache.dolphinscheduler.dao.entity.Command) ProcessInstanceMap(org.apache.dolphinscheduler.dao.entity.ProcessInstanceMap) ProcessInstance(org.apache.dolphinscheduler.dao.entity.ProcessInstance) Test(org.junit.Test)

Aggregations

ProcessInstance (org.apache.dolphinscheduler.dao.entity.ProcessInstance)29 TaskInstance (org.apache.dolphinscheduler.dao.entity.TaskInstance)12 ProcessDefinition (org.apache.dolphinscheduler.dao.entity.ProcessDefinition)10 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)6 Date (java.util.Date)5 TaskExecutionContext (org.apache.dolphinscheduler.server.entity.TaskExecutionContext)4 JSONObject (com.alibaba.fastjson.JSONObject)3 ExecutionContext (org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)2 HashMap (java.util.HashMap)2 TaskNode (org.apache.dolphinscheduler.common.model.TaskNode)2 TaskNodeRelation (org.apache.dolphinscheduler.common.model.TaskNodeRelation)2 Command (org.apache.dolphinscheduler.dao.entity.Command)2 User (org.apache.dolphinscheduler.dao.entity.User)2 IPage (com.baomidou.mybatisplus.core.metadata.IPage)1 Field (java.lang.reflect.Field)1 List (java.util.List)1 Map (java.util.Map)1