-
Notifications
You must be signed in to change notification settings - Fork 5k
[Fix-17817][Master]Add workflow timeout event and handle #18063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from 4 commits
b12b82b
2b02024
6c30466
ac5373d
bc39a58
d31aa6e
1a18b36
e4f43df
ed36525
73832b8
65e9b5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkState; | ||
|
|
||
| import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; | ||
| import org.apache.dolphinscheduler.server.master.engine.ILifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.AbstractWorkflowLifecycleLifecycleEvent; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.WorkflowLifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import lombok.AccessLevel; | ||
| import lombok.AllArgsConstructor; | ||
| import lombok.Getter; | ||
|
|
||
| @Getter | ||
| @AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public class WorkflowTimeoutLifecycleEvent extends AbstractWorkflowLifecycleLifecycleEvent { | ||
|
|
||
| private IWorkflowExecutionRunnable workflowExecutionRunnable; | ||
Check noticeCode scanning / CodeQL Missing Override annotation Note
This method overrides
AbstractWorkflowLifecycleLifecycleEvent.getWorkflowExecutionRunnable Error loading related location Loading |
||
|
|
||
| protected WorkflowTimeoutLifecycleEvent(final IWorkflowExecutionRunnable workflowExecutionRunnable, | ||
| final long timeout) { | ||
| super(timeout); | ||
| this.workflowExecutionRunnable = workflowExecutionRunnable; | ||
| } | ||
|
|
||
| public static WorkflowTimeoutLifecycleEvent of(IWorkflowExecutionRunnable workflowExecutionRunnable) { | ||
| final WorkflowInstance workflowInstance = workflowExecutionRunnable.getWorkflowInstance(); | ||
| checkState(workflowInstance != null, "The workflow instance must be initialized before retrying."); | ||
|
||
|
|
||
| final int timeout = workflowInstance.getTimeout(); | ||
| checkState(timeout > 0, "The workflow timeout: %s must > 0 minutes", timeout); | ||
|
|
||
| long delayTime = System.currentTimeMillis() - workflowInstance.getStartTime().getTime() | ||
|
||
| + TimeUnit.MINUTES.toMillis(timeout); | ||
|
||
| return new WorkflowTimeoutLifecycleEvent(workflowExecutionRunnable, delayTime); | ||
| } | ||
|
|
||
| @Override | ||
| public ILifecycleEventType getEventType() { | ||
| return WorkflowLifecycleEventType.TIMEOUT; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "WorkflowTimeoutLifecycleEvent{" + | ||
| "workflow=" + workflowExecutionRunnable.getWorkflowExecuteContext().getWorkflowInstance().getName() + | ||
| '}'; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,11 @@ | |
|
|
||
| package org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.handler; | ||
|
|
||
| import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; | ||
| import org.apache.dolphinscheduler.server.master.engine.ILifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.WorkflowLifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowStartLifecycleEvent; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowTimeoutLifecycleEvent; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.statemachine.IWorkflowStateAction; | ||
|
|
||
|
|
@@ -38,11 +40,24 @@ public void handle(final IWorkflowStateAction workflowStateAction, | |
| final IWorkflowExecutionRunnable workflowExecutionRunnable, | ||
| final WorkflowStartLifecycleEvent workflowStartEvent) { | ||
|
|
||
| workflowTimeoutMonitor(workflowExecutionRunnable); | ||
| workflowStateAction.onStartEvent(workflowExecutionRunnable, workflowStartEvent); | ||
| } | ||
|
|
||
| @Override | ||
| public ILifecycleEventType matchEventType() { | ||
| return WorkflowLifecycleEventType.START; | ||
| } | ||
|
|
||
| private void workflowTimeoutMonitor(final IWorkflowExecutionRunnable workflowExecutionRunnable) { | ||
| final WorkflowInstance workflowInstance = workflowExecutionRunnable.getWorkflowInstance(); | ||
| if (workflowInstance.getTimeout() <= 0) { | ||
| log.debug("The workflow {} timeout {} is invalided, so the timeout monitor will not be started.", | ||
|
||
| workflowInstance.getName(), | ||
| workflowInstance.getTimeout()); | ||
| return; | ||
| } | ||
| workflowExecutionRunnable.getWorkflowEventBus() | ||
| .publish(WorkflowTimeoutLifecycleEvent.of(workflowExecutionRunnable)); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| package org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.handler; | ||
|
|
||
| import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; | ||
| import org.apache.dolphinscheduler.server.master.engine.ILifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.graph.IWorkflowExecutionGraph; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.WorkflowLifecycleEventType; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.lifecycle.event.WorkflowTimeoutLifecycleEvent; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.runnable.IWorkflowExecutionRunnable; | ||
| import org.apache.dolphinscheduler.server.master.engine.workflow.statemachine.IWorkflowStateAction; | ||
| import org.apache.dolphinscheduler.service.alert.WorkflowAlertManager; | ||
|
|
||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class WorkflowTimeoutLifecycleEventHandler | ||
| extends | ||
| AbstractWorkflowLifecycleEventHandler<WorkflowTimeoutLifecycleEvent> { | ||
|
|
||
| private final WorkflowAlertManager workflowAlertManager; | ||
|
|
||
| public WorkflowTimeoutLifecycleEventHandler(final WorkflowAlertManager workflowAlertManager) { | ||
| this.workflowAlertManager = workflowAlertManager; | ||
| } | ||
|
|
||
| @Override | ||
| public void handle(final IWorkflowStateAction workflowStateAction, | ||
| final IWorkflowExecutionRunnable workflowExecutionRunnable, | ||
| final WorkflowTimeoutLifecycleEvent workflowTimeoutLifecycleEvent) { | ||
| final IWorkflowExecutionGraph workflowExecutionGraph = workflowExecutionRunnable.getWorkflowExecutionGraph(); | ||
| if (workflowExecutionGraph.isAllTaskExecutionRunnableChainFinish()) { | ||
| // all the TaskExecutionRunnable chain in the graph is finish, means the workflow is already finished. | ||
| return; | ||
| } | ||
|
|
||
| final WorkflowInstance workflowInstance = workflowExecutionRunnable.getWorkflowInstance(); | ||
| final boolean shouldSendAlert = workflowInstance.getWarningGroupId() != null; | ||
|
|
||
| if (shouldSendAlert) { | ||
| doWorkflowTimeoutAlert(workflowExecutionRunnable); | ||
| } else { | ||
| log.info("Skipped sending timeout alert for workflow {} because warningGroupId is null.", | ||
| workflowInstance.getName()); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public ILifecycleEventType matchEventType() { | ||
| return WorkflowLifecycleEventType.TIMEOUT; | ||
| } | ||
|
|
||
| private void doWorkflowTimeoutAlert(final IWorkflowExecutionRunnable workflowExecutionRunnable) { | ||
| final WorkflowInstance workflowInstance = workflowExecutionRunnable.getWorkflowInstance(); | ||
| workflowAlertManager.sendWorkflowTimeoutAlert(workflowInstance); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't change the unrelated code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok