@ -24,6 +24,7 @@ import org.apache.dolphinscheduler.common.enums.TimeoutFlag;
import org.apache.dolphinscheduler.common.thread.BaseDaemonThread ;
import org.apache.dolphinscheduler.common.thread.BaseDaemonThread ;
import org.apache.dolphinscheduler.common.thread.Stopper ;
import org.apache.dolphinscheduler.common.thread.Stopper ;
import org.apache.dolphinscheduler.common.utils.DateUtils ;
import org.apache.dolphinscheduler.common.utils.DateUtils ;
import org.apache.dolphinscheduler.common.utils.LoggerUtils ;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance ;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskDefinition ;
import org.apache.dolphinscheduler.dao.entity.TaskInstance ;
import org.apache.dolphinscheduler.dao.entity.TaskInstance ;
@ -43,6 +44,8 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import lombok.NonNull ;
/ * *
/ * *
* Check thread
* Check thread
* 1 . timeout task check
* 1 . timeout task check
@ -110,10 +113,16 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
public void addProcess4TimeoutCheck ( ProcessInstance processInstance ) {
public void addProcess4TimeoutCheck ( ProcessInstance processInstance ) {
processInstanceTimeoutCheckList . add ( processInstance . getId ( ) ) ;
processInstanceTimeoutCheckList . add ( processInstance . getId ( ) ) ;
logger . info ( "Success add workflow instance into timeout check list" ) ;
}
}
public void removeProcess4TimeoutCheck ( ProcessInstance processInstance ) {
public void removeProcess4TimeoutCheck ( ProcessInstance processInstance ) {
processInstanceTimeoutCheckList . remove ( processInstance . getId ( ) ) ;
boolean removeFlag = processInstanceTimeoutCheckList . remove ( processInstance . getId ( ) ) ;
if ( removeFlag ) {
logger . info ( "Success remove workflow instance from timeout check list" ) ;
} else {
logger . warn ( "Failed to remove workflow instance from timeout check list" ) ;
}
}
}
private void checkProcess4Timeout ( ) {
private void checkProcess4Timeout ( ) {
@ -121,106 +130,95 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
return ;
return ;
}
}
for ( Integer processInstanceId : processInstanceTimeoutCheckList ) {
for ( Integer processInstanceId : processInstanceTimeoutCheckList ) {
if ( processInstanceId = = null ) {
continue ;
}
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
if ( workflowExecuteThread = = null ) {
if ( workflowExecuteThread = = null ) {
logger . warn ( "can not find workflowExecuteThread, this check event will remove, processInstanceId:{}" , processInstanceId ) ;
logger . warn ( "Check workflow timeout failed, can not find workflowExecuteThread from cache manager, will remove this workflowInstance from check list" ) ;
processInstanceTimeoutCheckList . remove ( processInstanceId ) ;
processInstanceTimeoutCheckList . remove ( processInstanceId ) ;
continue ;
continue ;
}
}
ProcessInstance processInstance = workflowExecuteThread . getProcessInstance ( ) ;
ProcessInstance processInstance = workflowExecuteThread . getProcessInstance ( ) ;
if ( processInstance = = null ) {
if ( processInstance = = null ) {
logger . warn ( "Check workflow timeout failed, the workflowInstance is null" ) ;
continue ;
continue ;
}
}
long timeRemain = DateUtils . getRemainTime ( processInstance . getStartTime ( ) , ( long ) processInstance . getTimeout ( ) * Constants . SEC_2_MINUTES_TIME_UNIT ) ;
long timeRemain = DateUtils . getRemainTime ( processInstance . getStartTime ( ) , ( long ) processInstance . getTimeout ( ) * Constants . SEC_2_MINUTES_TIME_UNIT ) ;
if ( timeRemain < 0 ) {
if ( timeRemain < 0 ) {
logger . info ( "Workflow instance timeout, adding timeout event" ) ;
addProcessTimeoutEvent ( processInstance ) ;
addProcessTimeoutEvent ( processInstance ) ;
processInstanceTimeoutCheckList . remove ( processInstance . getId ( ) ) ;
processInstanceTimeoutCheckList . remove ( processInstance . getId ( ) ) ;
logger . info ( "Workflow instance timeout, added timeout event" ) ;
}
}
}
}
}
}
public void addTask4TimeoutCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void addTask4TimeoutCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . info ( "Adding task instance into timeout check list" ) ;
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
if ( taskInstanceTimeoutCheckList . contains ( taskInstanceKey ) ) {
if ( taskInstanceTimeoutCheckList . contains ( taskInstanceKey ) ) {
logger . warn ( "Task instance is already in timeout check list" ) ;
return ;
return ;
}
}
TaskDefinition taskDefinition = taskInstance . getTaskDefine ( ) ;
TaskDefinition taskDefinition = taskInstance . getTaskDefine ( ) ;
if ( taskDefinition = = null ) {
if ( taskDefinition = = null ) {
logger . error ( "taskDefinition is null, taskId:{}" , taskInstance . getId ( ) ) ;
logger . error ( "Failed to add task instance into timeout check list, taskDefinition is null" ) ;
return ;
return ;
}
}
if ( TimeoutFlag . OPEN = = taskDefinition . getTimeoutFlag ( ) ) {
if ( TimeoutFlag . OPEN = = taskDefinition . getTimeoutFlag ( ) ) {
taskInstanceTimeoutCheckList . add ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . add ( taskInstanceKey ) ;
logger . info ( "Timeout flag is open, added task instance into timeout check list" ) ;
}
}
if ( taskInstance . isDependTask ( ) | | taskInstance . isSubProcess ( ) ) {
if ( taskInstance . isDependTask ( ) | | taskInstance . isSubProcess ( ) ) {
taskInstanceTimeoutCheckList . add ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . add ( taskInstanceKey ) ;
logger . info ( "task instance is dependTask orSubProcess, added task instance into timeout check list" ) ;
}
}
}
}
public void removeTask4TimeoutCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void removeTask4TimeoutCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
logger . info ( "remove task instance from timeout check list" ) ;
}
}
public void addTask4RetryCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void addTask4RetryCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
logger . info ( "Adding task instance into retry check list" ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
if ( taskInstanceRetryCheckList . contains ( taskInstanceKey ) ) {
if ( taskInstanceRetryCheckList . contains ( taskInstanceKey ) ) {
logger . warn ( "Task instance is already in retry check list" ) ;
return ;
return ;
}
}
TaskDefinition taskDefinition = taskInstance . getTaskDefine ( ) ;
TaskDefinition taskDefinition = taskInstance . getTaskDefine ( ) ;
if ( taskDefinition = = null ) {
if ( taskDefinition = = null ) {
logger . error ( "taskDefinition is null, taskId:{}" , taskInstance . getId ( ) ) ;
logger . error ( "Add task instance into retry check list error, taskDefinition is null" ) ;
return ;
return ;
}
}
logger . debug ( "addTask4RetryCheck, taskCode:{}, processInstanceId:{}" , taskInstance . getTaskCode ( ) , taskInstance . getProcessInstanceId ( ) ) ;
taskInstanceRetryCheckList . add ( taskInstanceKey ) ;
taskInstanceRetryCheckList . add ( taskInstanceKey ) ;
logger . info ( "[WorkflowInstance-{}][TaskInstance-{}] Added task instance into retry check list" ,
processInstance . getId ( ) , taskInstance . getId ( ) ) ;
}
}
public void removeTask4RetryCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void removeTask4RetryCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
logger . info ( "remove task instance from retry check list" ) ;
}
}
public void addTask4StateCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void addTask4StateCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
logger . info ( "Adding task instance into state check list" ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
if ( taskInstanceStateCheckList . contains ( taskInstanceKey ) ) {
if ( taskInstanceStateCheckList . contains ( taskInstanceKey ) ) {
logger . warn ( "Task instance is already in state check list" ) ;
return ;
return ;
}
}
if ( taskInstance . isDependTask ( ) | | taskInstance . isSubProcess ( ) ) {
if ( taskInstance . isDependTask ( ) | | taskInstance . isSubProcess ( ) ) {
taskInstanceStateCheckList . add ( taskInstanceKey ) ;
taskInstanceStateCheckList . add ( taskInstanceKey ) ;
logger . info ( "Added task instance into state check list" ) ;
}
}
}
}
public void removeTask4StateCheck ( ProcessInstance processInstance , TaskInstance taskInstance ) {
public void removeTask4StateCheck ( @NonNull ProcessInstance processInstance , @NonNull TaskInstance taskInstance ) {
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
TaskInstanceKey taskInstanceKey = TaskInstanceKey . getTaskInstanceKey ( processInstance , taskInstance ) ;
if ( taskInstanceKey = = null ) {
logger . error ( "taskInstanceKey is null" ) ;
return ;
}
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
logger . info ( "Removed task instance from state check list" ) ;
}
}
private void checkTask4Timeout ( ) {
private void checkTask4Timeout ( ) {
@ -228,20 +226,21 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
return ;
return ;
}
}
for ( TaskInstanceKey taskInstanceKey : taskInstanceTimeoutCheckList ) {
for ( TaskInstanceKey taskInstanceKey : taskInstanceTimeoutCheckList ) {
try {
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
LoggerUtils . setWorkflowInstanceIdMDC ( processInstanceId ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
if ( workflowExecuteThread = = null ) {
if ( workflowExecuteThread = = null ) {
logger . warn ( "can not find workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn ( "Check task instance timeout failed, can not find workflowExecuteThread from cache manager, will remove this check task" ) ;
processInstanceId , taskCode ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
Optional < TaskInstance > taskInstanceOptional = workflowExecuteThread . getActiveTaskInstanceByTaskCode ( taskCode ) ;
Optional < TaskInstance > taskInstanceOptional = workflowExecuteThread . getActiveTaskInstanceByTaskCode ( taskCode ) ;
if ( ! taskInstanceOptional . isPresent ( ) ) {
if ( ! taskInstanceOptional . isPresent ( ) ) {
logger . warn ( "can not find taskInstance from workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn ( "Check task instance timeout failed, can not get taskInstance from workflowExecuteThread, taskCode: {}"
processInstanceId , taskCode ) ;
+ "will remove this check task" , taskCode ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
@ -249,10 +248,14 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
if ( TimeoutFlag . OPEN = = taskInstance . getTaskDefine ( ) . getTimeoutFlag ( ) ) {
if ( TimeoutFlag . OPEN = = taskInstance . getTaskDefine ( ) . getTimeoutFlag ( ) ) {
long timeRemain = DateUtils . getRemainTime ( taskInstance . getStartTime ( ) , ( long ) taskInstance . getTaskDefine ( ) . getTimeout ( ) * Constants . SEC_2_MINUTES_TIME_UNIT ) ;
long timeRemain = DateUtils . getRemainTime ( taskInstance . getStartTime ( ) , ( long ) taskInstance . getTaskDefine ( ) . getTimeout ( ) * Constants . SEC_2_MINUTES_TIME_UNIT ) ;
if ( timeRemain < 0 ) {
if ( timeRemain < 0 ) {
logger . info ( "Task instance is timeout, adding task timeout event and remove the check" ) ;
addTaskTimeoutEvent ( taskInstance ) ;
addTaskTimeoutEvent ( taskInstance ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
taskInstanceTimeoutCheckList . remove ( taskInstanceKey ) ;
}
}
}
}
} finally {
LoggerUtils . removeWorkflowInstanceIdMDC ( ) ;
}
}
}
}
}
@ -264,12 +267,14 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
for ( TaskInstanceKey taskInstanceKey : taskInstanceRetryCheckList ) {
for ( TaskInstanceKey taskInstanceKey : taskInstanceRetryCheckList ) {
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
try {
LoggerUtils . setWorkflowInstanceIdMDC ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
if ( workflowExecuteThread = = null ) {
if ( workflowExecuteThread = = null ) {
logger . warn ( "can not find workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn ( "Task instance retry check failed, can not find workflowExecuteThread from cache manager, "
processInstanceId , taskCode ) ;
+ "will remove this check task" ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
@ -278,14 +283,14 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
ProcessInstance processInstance = workflowExecuteThread . getProcessInstance ( ) ;
ProcessInstance processInstance = workflowExecuteThread . getProcessInstance ( ) ;
if ( processInstance . getState ( ) = = ExecutionStatus . READY_STOP ) {
if ( processInstance . getState ( ) = = ExecutionStatus . READY_STOP ) {
logger . warn ( "The process instance is ready to stop, will send process stop event and remove the check task" ) ;
addProcessStopEvent ( processInstance ) ;
addProcessStopEvent ( processInstance ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
break ;
break ;
}
}
if ( ! taskInstanceOptional . isPresent ( ) ) {
if ( ! taskInstanceOptional . isPresent ( ) ) {
logger . warn ( "can not find taskInstance from workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn ( "Task instance retry check failed, can not find taskInstance from workflowExecuteThread, will remove this check" ) ;
processInstanceId , taskCode ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
@ -300,6 +305,9 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
addTaskRetryEvent ( taskInstance ) ;
addTaskRetryEvent ( taskInstance ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
taskInstanceRetryCheckList . remove ( taskInstanceKey ) ;
}
}
} finally {
LoggerUtils . removeWorkflowInstanceIdMDC ( ) ;
}
}
}
}
}
@ -311,17 +319,18 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
int processInstanceId = taskInstanceKey . getProcessInstanceId ( ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
long taskCode = taskInstanceKey . getTaskCode ( ) ;
try {
LoggerUtils . setTaskInstanceIdMDC ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
WorkflowExecuteRunnable workflowExecuteThread = processInstanceExecCacheManager . getByProcessInstanceId ( processInstanceId ) ;
if ( workflowExecuteThread = = null ) {
if ( workflowExecuteThread = = null ) {
logger . warn ( "can not find workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn ( "Task instance state check failed, can not find workflowExecuteThread from cache manager, will remove this check task" ) ;
processInstanceId , taskCode ) ;
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
Optional < TaskInstance > taskInstanceOptional = workflowExecuteThread . getActiveTaskInstanceByTaskCode ( taskCode ) ;
Optional < TaskInstance > taskInstanceOptional = workflowExecuteThread . getActiveTaskInstanceByTaskCode ( taskCode ) ;
if ( ! taskInstanceOptional . isPresent ( ) ) {
if ( ! taskInstanceOptional . isPresent ( ) ) {
logger . warn ( "can not find taskInstance from workflowExecuteThread, this check event will remove, processInstanceId:{}, taskCode:{}" ,
logger . warn (
processInstanceId , taskCode ) ;
"Task instance state check failed, can not find taskInstance from workflowExecuteThread, will remove this check event" ) ;
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
taskInstanceStateCheckList . remove ( taskInstanceKey ) ;
continue ;
continue ;
}
}
@ -330,6 +339,9 @@ public class StateWheelExecuteThread extends BaseDaemonThread {
continue ;
continue ;
}
}
addTaskStateChangeEvent ( taskInstance ) ;
addTaskStateChangeEvent ( taskInstance ) ;
} finally {
LoggerUtils . removeWorkflowInstanceIdMDC ( ) ;
}
}
}
}
}