SubProcessTaskExecThread.java 6.7 KB
Newer Older
L
ligang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
K
Kirs 已提交
17 18

package org.apache.dolphinscheduler.server.worker.runner;
L
ligang 已提交
19

Q
qiaozhanwei 已提交
20 21 22 23 24
import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.ExecutionStatus;
import org.apache.dolphinscheduler.common.thread.Stopper;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.TaskInstance;
L
ligang 已提交
25 26 27 28

import java.util.Date;

/**
K
Kirs 已提交
29
 * subflow task exec thread
L
ligang 已提交
30 31 32
 */
public class SubProcessTaskExecThread extends MasterBaseTaskExecThread {

33 34 35
    /**
     * sub process instance
     */
L
ligang 已提交
36 37
    private ProcessInstance subProcessInstance;

38 39
    /**
     * sub process task exec thread
K
Kirs 已提交
40 41
     *
     * @param taskInstance task instance
42
     */
K
Kirs 已提交
43
    public SubProcessTaskExecThread(TaskInstance taskInstance) {
44
        super(taskInstance);
L
ligang 已提交
45 46 47 48 49 50
    }

    @Override
    public Boolean submitWaitComplete() {

        Boolean result = false;
K
Kirs 已提交
51
        try {
L
ligang 已提交
52 53 54
            // submit task instance
            this.taskInstance = submit();

K
Kirs 已提交
55
            if (taskInstance == null) {
L
ligang 已提交
56 57 58 59 60
                logger.error("sub work flow submit task instance to mysql and queue failed , please check and fix it");
                return result;
            }
            setTaskInstanceState();
            waitTaskQuit();
61
            subProcessInstance = processService.findSubProcessInstance(processInstance.getId(), taskInstance.getId());
L
ligang 已提交
62 63

            // at the end of the subflow , the task state is changed to the subflow state
K
Kirs 已提交
64 65
            if (subProcessInstance != null) {
                if (subProcessInstance.getState() == ExecutionStatus.STOP) {
L
ligang 已提交
66
                    this.taskInstance.setState(ExecutionStatus.KILL);
K
Kirs 已提交
67
                } else {
L
ligang 已提交
68 69 70 71
                    this.taskInstance.setState(subProcessInstance.getState());
                }
            }
            taskInstance.setEndTime(new Date());
72
            processService.updateTaskInstance(taskInstance);
L
ligang 已提交
73
            logger.info("subflow task :{} id:{}, process id:{}, exec thread completed ",
K
Kirs 已提交
74
                    this.taskInstance.getName(), taskInstance.getId(), processInstance.getId());
L
ligang 已提交
75 76
            result = true;

K
Kirs 已提交
77 78 79
        } catch (Exception e) {
            Thread.currentThread().interrupt();
            logger.error("exception: ", e);
80 81 82 83
            if (null != taskInstance) {
                logger.error("wait task quit failed, instance id:{}, task id:{}",
                        processInstance.getId(), taskInstance.getId());
            }
L
ligang 已提交
84 85 86 87 88
        }
        return result;
    }

    /**
K
Kirs 已提交
89
     * set task instance state
L
ligang 已提交
90
     */
K
Kirs 已提交
91
    private boolean setTaskInstanceState() {
92
        subProcessInstance = processService.findSubProcessInstance(processInstance.getId(), taskInstance.getId());
K
Kirs 已提交
93
        if (subProcessInstance == null || taskInstance.getState().typeIsFinished()) {
L
ligang 已提交
94 95 96
            return false;
        }

_和's avatar
_和 已提交
97
        taskInstance.setState(ExecutionStatus.RUNNING_EXECUTION);
L
ligang 已提交
98
        taskInstance.setStartTime(new Date());
99
        processService.updateTaskInstance(taskInstance);
L
ligang 已提交
100 101 102 103
        return true;
    }

    /**
K
Kirs 已提交
104
     * updateProcessInstance parent state
L
ligang 已提交
105
     */
K
Kirs 已提交
106
    private void updateParentProcessState() {
107
        ProcessInstance parentProcessInstance = processService.findProcessInstanceById(this.processInstance.getId());
L
ligang 已提交
108

K
Kirs 已提交
109
        if (parentProcessInstance == null) {
L
ligang 已提交
110 111 112 113 114 115 116
            logger.error("parent work flow instance is null ,  please check it! work flow id {}", processInstance.getId());
            return;
        }
        this.processInstance.setState(parentProcessInstance.getState());
    }

    /**
117
     * wait task quit
L
ligang 已提交
118 119 120 121 122 123 124 125
     */
    private void waitTaskQuit() throws InterruptedException {

        logger.info("wait sub work flow: {} complete", this.taskInstance.getName());

        if (taskInstance.getState().typeIsFinished()) {
            logger.info("sub work flow task {} already complete. task state:{}, parent work flow instance state:{}",
                    this.taskInstance.getName(),
126 127
                    this.taskInstance.getState(),
                    this.processInstance.getState());
L
ligang 已提交
128 129 130 131 132 133
            return;
        }
        while (Stopper.isRunning()) {
            // waiting for subflow process instance establishment
            if (subProcessInstance == null) {
                Thread.sleep(Constants.SLEEP_TIME_MILLIS);
K
Kirs 已提交
134
                if (!setTaskInstanceState()) {
L
ligang 已提交
135 136 137
                    continue;
                }
            }
138
            subProcessInstance = processService.findProcessInstanceById(subProcessInstance.getId());
139 140 141 142
            if (checkTaskTimeout()) {
                this.checkTimeoutFlag = !alertTimeout();
                handleTimeoutFailed();
            }
L
ligang 已提交
143
            updateParentProcessState();
K
Kirs 已提交
144
            if (subProcessInstance.getState().typeIsFinished()) {
L
ligang 已提交
145 146
                break;
            }
K
Kirs 已提交
147
            if (this.processInstance.getState() == ExecutionStatus.READY_PAUSE) {
L
ligang 已提交
148 149
                // parent process "ready to pause" , child process "pause"
                pauseSubProcess();
K
Kirs 已提交
150
            } else if (this.cancel || this.processInstance.getState() == ExecutionStatus.READY_STOP) {
L
ligang 已提交
151 152 153 154 155 156 157 158
                // parent Process "Ready to Cancel" , subflow "Cancel"
                stopSubProcess();
            }
            Thread.sleep(Constants.SLEEP_TIME_MILLIS);
        }
    }

    /**
159
     * stop sub process
L
ligang 已提交
160 161
     */
    private void stopSubProcess() {
K
Kirs 已提交
162 163
        if (subProcessInstance.getState() == ExecutionStatus.STOP
                || subProcessInstance.getState() == ExecutionStatus.READY_STOP) {
L
ligang 已提交
164 165 166
            return;
        }
        subProcessInstance.setState(ExecutionStatus.READY_STOP);
167
        processService.updateProcessInstance(subProcessInstance);
L
ligang 已提交
168 169 170
    }

    /**
171
     * pause sub process
L
ligang 已提交
172 173
     */
    private void pauseSubProcess() {
K
Kirs 已提交
174 175
        if (subProcessInstance.getState() == ExecutionStatus.PAUSE
                || subProcessInstance.getState() == ExecutionStatus.READY_PAUSE) {
L
ligang 已提交
176 177 178
            return;
        }
        subProcessInstance.setState(ExecutionStatus.READY_PAUSE);
179
        processService.updateProcessInstance(subProcessInstance);
L
ligang 已提交
180 181
    }
}