github-advanced-security[bot] commented on code in PR #18000: URL: https://github.com/apache/dolphinscheduler/pull/18000#discussion_r2853897907
########## dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/command/handler/ExecuteTaskCommandHandler.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.command.handler; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.dolphinscheduler.common.constants.CommandKeyConstants.CMD_PARAM_START_NODES; + +import org.apache.dolphinscheduler.common.enums.CommandType; +import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; +import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.TaskDefinition; +import org.apache.dolphinscheduler.dao.entity.TaskInstance; +import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; +import org.apache.dolphinscheduler.dao.repository.TaskInstanceDao; +import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao; +import org.apache.dolphinscheduler.server.master.config.MasterConfig; +import org.apache.dolphinscheduler.server.master.engine.graph.IWorkflowGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowExecutionGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowGraphTopologyLogicalVisitor; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnable; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnableBuilder; +import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteContext.WorkflowExecuteContextBuilder; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +import com.google.common.base.Splitter; + +/** + * This handler is used to handle {@link CommandType#EXECUTE_TASK}. + * <p> It will rerun the given start task and all its downstream tasks in the same workflow instance. + */ +@Component +public class ExecuteTaskCommandHandler extends AbstractCommandHandler { + + @Autowired + private WorkflowInstanceDao workflowInstanceDao; + + @Autowired + private TaskInstanceDao taskInstanceDao; + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private MasterConfig masterConfig; + + @Override + protected void assembleWorkflowInstance(final WorkflowExecuteContextBuilder workflowExecuteContextBuilder) { + final Command command = workflowExecuteContextBuilder.getCommand(); + final int workflowInstanceId = command.getWorkflowInstanceId(); + final WorkflowInstance workflowInstance = workflowInstanceDao.queryOptionalById(workflowInstanceId) + .orElseThrow(() -> new IllegalArgumentException("Cannot find WorkflowInstance:" + workflowInstanceId)); + workflowInstance.setStateWithDesc(WorkflowExecutionStatus.RUNNING_EXECUTION, command.getCommandType().name()); + workflowInstance.setCommandType(command.getCommandType()); + if (command.getTaskDependType() != null) { + workflowInstance.setTaskDependType(command.getTaskDependType()); Review Comment: ## Deprecated method or constructor invocation Invoking [Command.getTaskDependType](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/5619) ########## dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/command/handler/ExecuteTaskCommandHandler.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.command.handler; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.dolphinscheduler.common.constants.CommandKeyConstants.CMD_PARAM_START_NODES; + +import org.apache.dolphinscheduler.common.enums.CommandType; +import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; +import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.TaskDefinition; +import org.apache.dolphinscheduler.dao.entity.TaskInstance; +import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; +import org.apache.dolphinscheduler.dao.repository.TaskInstanceDao; +import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao; +import org.apache.dolphinscheduler.server.master.config.MasterConfig; +import org.apache.dolphinscheduler.server.master.engine.graph.IWorkflowGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowExecutionGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowGraphTopologyLogicalVisitor; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnable; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnableBuilder; +import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteContext.WorkflowExecuteContextBuilder; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +import com.google.common.base.Splitter; + +/** + * This handler is used to handle {@link CommandType#EXECUTE_TASK}. + * <p> It will rerun the given start task and all its downstream tasks in the same workflow instance. + */ +@Component +public class ExecuteTaskCommandHandler extends AbstractCommandHandler { + + @Autowired + private WorkflowInstanceDao workflowInstanceDao; + + @Autowired + private TaskInstanceDao taskInstanceDao; + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private MasterConfig masterConfig; + + @Override + protected void assembleWorkflowInstance(final WorkflowExecuteContextBuilder workflowExecuteContextBuilder) { + final Command command = workflowExecuteContextBuilder.getCommand(); + final int workflowInstanceId = command.getWorkflowInstanceId(); + final WorkflowInstance workflowInstance = workflowInstanceDao.queryOptionalById(workflowInstanceId) + .orElseThrow(() -> new IllegalArgumentException("Cannot find WorkflowInstance:" + workflowInstanceId)); + workflowInstance.setStateWithDesc(WorkflowExecutionStatus.RUNNING_EXECUTION, command.getCommandType().name()); + workflowInstance.setCommandType(command.getCommandType()); + if (command.getTaskDependType() != null) { + workflowInstance.setTaskDependType(command.getTaskDependType()); + } + workflowInstance.setHost(masterConfig.getMasterAddress()); + workflowInstanceDao.updateById(workflowInstance); + workflowExecuteContextBuilder.setWorkflowInstance(workflowInstance); + } + + @Override + protected void assembleWorkflowExecutionGraph(final WorkflowExecuteContextBuilder workflowExecuteContextBuilder) { + final IWorkflowGraph workflowGraph = workflowExecuteContextBuilder.getWorkflowGraph(); + final List<String> startNodes = parseStartNodesFromCommand(workflowExecuteContextBuilder, workflowGraph); + final Map<String, TaskInstance> taskInstanceMap = getValidTaskInstance(workflowExecuteContextBuilder + .getWorkflowInstance()) + .stream() + .collect(Collectors.toMap(TaskInstance::getName, Function.identity())); + + // Mark the selected task and all its downstream task instances as invalid, then trigger them again. + final Set<String> taskNamesNeedRerun = new HashSet<>(); + final WorkflowGraphTopologyLogicalVisitor markInvalidTaskVisitor = + WorkflowGraphTopologyLogicalVisitor.builder() + .onWorkflowGraph(workflowGraph) + .taskDependType(workflowExecuteContextBuilder.getWorkflowInstance().getTaskDependType()) + .fromTask(startNodes) + .doVisitFunction((task, successors) -> taskNamesNeedRerun.add(task)) + .build(); + markInvalidTaskVisitor.visit(); + + final List<TaskInstance> taskInstancesNeedRerun = taskNamesNeedRerun.stream() + .map(taskInstanceMap::remove) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(taskInstancesNeedRerun)) { + taskInstanceDao.markTaskInstanceInvalid(taskInstancesNeedRerun); + } + + final WorkflowExecutionGraph workflowExecutionGraph = new WorkflowExecutionGraph(); + final BiConsumer<String, Set<String>> taskExecutionRunnableCreator = (task, successors) -> { + final TaskExecutionRunnableBuilder taskExecutionRunnableBuilder = + TaskExecutionRunnableBuilder + .builder() + .workflowExecutionGraph(workflowExecutionGraph) + .workflowDefinition(workflowExecuteContextBuilder.getWorkflowDefinition()) + .project(workflowExecuteContextBuilder.getProject()) + .workflowInstance(workflowExecuteContextBuilder.getWorkflowInstance()) + .taskDefinition(workflowGraph.getTaskNodeByName(task)) + .taskInstance(taskInstanceMap.get(task)) + .workflowEventBus(workflowExecuteContextBuilder.getWorkflowEventBus()) + .applicationContext(applicationContext) + .build(); + workflowExecutionGraph.addNode(new TaskExecutionRunnable(taskExecutionRunnableBuilder)); + workflowExecutionGraph.addEdge(task, successors); + }; + + final WorkflowGraphTopologyLogicalVisitor workflowGraphTopologyLogicalVisitor = + WorkflowGraphTopologyLogicalVisitor.builder() + .taskDependType(workflowExecuteContextBuilder.getWorkflowInstance().getTaskDependType()) + .onWorkflowGraph(workflowGraph) + .fromTask(startNodes) + .doVisitFunction(taskExecutionRunnableCreator) + .build(); + workflowGraphTopologyLogicalVisitor.visit(); + workflowExecutionGraph.removeUnReachableEdge(); + workflowExecuteContextBuilder.setWorkflowExecutionGraph(workflowExecutionGraph); + } + + private List<String> parseStartNodesFromCommand(final WorkflowExecuteContextBuilder workflowExecuteContextBuilder, + final IWorkflowGraph workflowGraph) { + final Command command = workflowExecuteContextBuilder.getCommand(); + final String startNodes = JSONUtils.getNodeString(command.getCommandParam(), CMD_PARAM_START_NODES); + checkArgument(StringUtils.isNotBlank(startNodes), + "Invalid command param, the start nodes is empty: " + command.getCommandParam()); + final List<Long> startNodeCodes = Splitter.on(',') + .trimResults() + .omitEmptyStrings() + .splitToStream(startNodes) + .map(Long::parseLong) Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/5617) ########## dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/engine/command/handler/ExecuteTaskCommandHandler.java: ########## @@ -0,0 +1,176 @@ +/* + * 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.command.handler; + +import static com.google.common.base.Preconditions.checkArgument; +import static org.apache.dolphinscheduler.common.constants.CommandKeyConstants.CMD_PARAM_START_NODES; + +import org.apache.dolphinscheduler.common.enums.CommandType; +import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus; +import org.apache.dolphinscheduler.common.utils.JSONUtils; +import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.TaskDefinition; +import org.apache.dolphinscheduler.dao.entity.TaskInstance; +import org.apache.dolphinscheduler.dao.entity.WorkflowInstance; +import org.apache.dolphinscheduler.dao.repository.TaskInstanceDao; +import org.apache.dolphinscheduler.dao.repository.WorkflowInstanceDao; +import org.apache.dolphinscheduler.server.master.config.MasterConfig; +import org.apache.dolphinscheduler.server.master.engine.graph.IWorkflowGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowExecutionGraph; +import org.apache.dolphinscheduler.server.master.engine.graph.WorkflowGraphTopologyLogicalVisitor; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnable; +import org.apache.dolphinscheduler.server.master.engine.task.runnable.TaskExecutionRunnableBuilder; +import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteContext.WorkflowExecuteContextBuilder; + +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.BiConsumer; +import java.util.function.Function; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.stereotype.Component; + +import com.google.common.base.Splitter; + +/** + * This handler is used to handle {@link CommandType#EXECUTE_TASK}. + * <p> It will rerun the given start task and all its downstream tasks in the same workflow instance. + */ +@Component +public class ExecuteTaskCommandHandler extends AbstractCommandHandler { + + @Autowired + private WorkflowInstanceDao workflowInstanceDao; + + @Autowired + private TaskInstanceDao taskInstanceDao; + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private MasterConfig masterConfig; + + @Override + protected void assembleWorkflowInstance(final WorkflowExecuteContextBuilder workflowExecuteContextBuilder) { + final Command command = workflowExecuteContextBuilder.getCommand(); + final int workflowInstanceId = command.getWorkflowInstanceId(); + final WorkflowInstance workflowInstance = workflowInstanceDao.queryOptionalById(workflowInstanceId) + .orElseThrow(() -> new IllegalArgumentException("Cannot find WorkflowInstance:" + workflowInstanceId)); + workflowInstance.setStateWithDesc(WorkflowExecutionStatus.RUNNING_EXECUTION, command.getCommandType().name()); + workflowInstance.setCommandType(command.getCommandType()); + if (command.getTaskDependType() != null) { Review Comment: ## Deprecated method or constructor invocation Invoking [Command.getTaskDependType](1) should be avoided because it has been deprecated. [Show more details](https://github.com/apache/dolphinscheduler/security/code-scanning/5618) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
