Search in sources :

Example 1 with TaskInstance

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

the class LoggerService method getLogBytes.

/**
 * get log size
 *
 * @param taskInstId task instance id
 * @return log byte array
 */
public byte[] getLogBytes(int taskInstId) {
    TaskInstance taskInstance = processService.findTaskInstanceById(taskInstId);
    if (taskInstance == null || StringUtils.isBlank(taskInstance.getHost())) {
        throw new RuntimeException("task instance is null or host is null");
    }
    String host = getHost(taskInstance.getHost());
    return logClient.getLogBytes(host, Constants.RPC_PORT, taskInstance.getLogPath());
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance)

Example 2 with TaskInstance

use of org.apache.dolphinscheduler.dao.entity.TaskInstance 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 3 with TaskInstance

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

the class LoggerServiceTest method testGetLogBytes.

@Test
public void testGetLogBytes() {
    TaskInstance taskInstance = new TaskInstance();
    Mockito.when(processService.findTaskInstanceById(1)).thenReturn(taskInstance);
    // task instance is null
    try {
        loggerService.getLogBytes(2);
    } catch (RuntimeException e) {
        Assert.assertTrue(true);
        logger.error("testGetLogBytes error: {}", "task instance is null");
    }
    // task instance host is null
    try {
        loggerService.getLogBytes(1);
    } catch (RuntimeException e) {
        Assert.assertTrue(true);
        logger.error("testGetLogBytes error: {}", "task instance host is null");
    }
    // success
    taskInstance.setHost("127.0.0.1:8080");
    taskInstance.setLogPath("/temp/log");
    // if use @RunWith(PowerMockRunner.class) mock object,sonarcloud will not calculate the coverage,
    // so no assert will be added here
    loggerService.getLogBytes(1);
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with TaskInstance

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

the class LoggerServiceTest method testQueryDataSourceList.

@Test
public void testQueryDataSourceList() {
    TaskInstance taskInstance = new TaskInstance();
    Mockito.when(processService.findTaskInstanceById(1)).thenReturn(taskInstance);
    Result result = loggerService.queryLog(2, 1, 1);
    // TASK_INSTANCE_NOT_FOUND
    Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(), result.getCode().intValue());
    try {
        // HOST NOT FOUND OR ILLEGAL
        result = loggerService.queryLog(1, 1, 1);
    } catch (RuntimeException e) {
        Assert.assertTrue(true);
        logger.error("testQueryDataSourceList error {}", e.getMessage());
    }
    Assert.assertEquals(Status.TASK_INSTANCE_NOT_FOUND.getCode(), result.getCode().intValue());
    // SUCCESS
    taskInstance.setHost("127.0.0.1:8080");
    taskInstance.setLogPath("/temp/log");
    Mockito.when(processService.findTaskInstanceById(1)).thenReturn(taskInstance);
    result = loggerService.queryLog(1, 1, 1);
    Assert.assertEquals(Status.SUCCESS.getCode(), result.getCode().intValue());
}
Also used : TaskInstance(org.apache.dolphinscheduler.dao.entity.TaskInstance) Result(org.apache.dolphinscheduler.api.utils.Result) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with TaskInstance

use of org.apache.dolphinscheduler.dao.entity.TaskInstance 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)

Aggregations

TaskInstance (org.apache.dolphinscheduler.dao.entity.TaskInstance)65 Test (org.junit.Test)29 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)13 ProcessInstance (org.apache.dolphinscheduler.dao.entity.ProcessInstance)12 ArrayList (java.util.ArrayList)10 ProcessDefinition (org.apache.dolphinscheduler.dao.entity.ProcessDefinition)10 Date (java.util.Date)8 TaskNode (org.apache.dolphinscheduler.common.model.TaskNode)8 HashMap (java.util.HashMap)5 TaskNodeRelation (org.apache.dolphinscheduler.common.model.TaskNodeRelation)5 ExecutionStatus (org.apache.dolphinscheduler.common.enums.ExecutionStatus)4 TaskExecutionContext (org.apache.dolphinscheduler.server.entity.TaskExecutionContext)4 JSONObject (com.alibaba.fastjson.JSONObject)3 Page (com.baomidou.mybatisplus.extension.plugins.pagination.Page)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ExecutionContext (org.apache.dolphinscheduler.server.master.dispatch.context.ExecutionContext)3 IPage (com.baomidou.mybatisplus.core.metadata.IPage)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Future (java.util.concurrent.Future)2