fuchanghai
2 years ago
committed by
GitHub
33 changed files with 587 additions and 25 deletions
@ -0,0 +1,103 @@
|
||||
/* |
||||
* 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.event; |
||||
|
||||
import org.apache.dolphinscheduler.common.enums.TaskEventType; |
||||
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
||||
import org.apache.dolphinscheduler.dao.repository.TaskInstanceDao; |
||||
import org.apache.dolphinscheduler.dao.utils.TaskInstanceUtils; |
||||
import org.apache.dolphinscheduler.remote.command.TaskUpdatePidAckMessage; |
||||
import org.apache.dolphinscheduler.server.master.cache.ProcessInstanceExecCacheManager; |
||||
import org.apache.dolphinscheduler.server.master.processor.queue.TaskEvent; |
||||
import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteRunnable; |
||||
import org.apache.dolphinscheduler.server.master.runner.WorkflowExecuteThreadPool; |
||||
|
||||
import java.util.Optional; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class TaskUpdatePidEventHandler implements TaskEventHandler { |
||||
|
||||
@Autowired |
||||
private ProcessInstanceExecCacheManager processInstanceExecCacheManager; |
||||
|
||||
@Autowired |
||||
private WorkflowExecuteThreadPool workflowExecuteThreadPool; |
||||
|
||||
@Autowired |
||||
private TaskInstanceDao taskInstanceDao; |
||||
|
||||
@Override |
||||
public void handleTaskEvent(TaskEvent taskEvent) throws TaskEventHandleError { |
||||
int taskInstanceId = taskEvent.getTaskInstanceId(); |
||||
int processInstanceId = taskEvent.getProcessInstanceId(); |
||||
|
||||
WorkflowExecuteRunnable workflowExecuteRunnable = |
||||
this.processInstanceExecCacheManager.getByProcessInstanceId(processInstanceId); |
||||
if (workflowExecuteRunnable == null) { |
||||
sendAckToWorker(taskEvent); |
||||
throw new TaskEventHandleError( |
||||
"Handle task running event error, cannot find related workflow instance from cache, will discard this event"); |
||||
} |
||||
Optional<TaskInstance> taskInstanceOptional = workflowExecuteRunnable.getTaskInstance(taskInstanceId); |
||||
if (!taskInstanceOptional.isPresent()) { |
||||
sendAckToWorker(taskEvent); |
||||
throw new TaskEventHandleError( |
||||
"Handle running event error, cannot find the taskInstance from cache, will discord this event"); |
||||
} |
||||
TaskInstance taskInstance = taskInstanceOptional.get(); |
||||
if (taskInstance.getState().isFinished()) { |
||||
sendAckToWorker(taskEvent); |
||||
throw new TaskEventHandleError( |
||||
"Handle task running event error, this task instance is already finished, this event is delay, will discard this event"); |
||||
} |
||||
|
||||
TaskInstance oldTaskInstance = new TaskInstance(); |
||||
TaskInstanceUtils.copyTaskInstance(taskInstance, oldTaskInstance); |
||||
try { |
||||
taskInstance.setStartTime(taskEvent.getStartTime()); |
||||
taskInstance.setHost(taskEvent.getWorkerAddress()); |
||||
taskInstance.setPid(taskEvent.getProcessId()); |
||||
if (!taskInstanceDao.updateTaskInstance(taskInstance)) { |
||||
throw new TaskEventHandleError("Handle task running event error, update taskInstance to db failed"); |
||||
} |
||||
sendAckToWorker(taskEvent); |
||||
} catch (Exception ex) { |
||||
TaskInstanceUtils.copyTaskInstance(oldTaskInstance, taskInstance); |
||||
if (ex instanceof TaskEventHandleError) { |
||||
throw ex; |
||||
} |
||||
throw new TaskEventHandleError("Handle task update pid event error, update taskInstance to db failed", ex); |
||||
} |
||||
|
||||
} |
||||
|
||||
private void sendAckToWorker(TaskEvent taskEvent) { |
||||
// If event handle success, send ack to worker to otherwise the worker will retry this event
|
||||
TaskUpdatePidAckMessage taskUpdatePidAckMessage = |
||||
new TaskUpdatePidAckMessage(true, taskEvent.getTaskInstanceId()); |
||||
taskEvent.getChannel().writeAndFlush(taskUpdatePidAckMessage.convert2Command()); |
||||
} |
||||
|
||||
@Override |
||||
public TaskEventType getHandleEventType() { |
||||
return TaskEventType.UPDATE_PID; |
||||
} |
||||
} |
@ -0,0 +1,66 @@
|
||||
/* |
||||
* 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.processor; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
||||
import org.apache.dolphinscheduler.remote.command.Command; |
||||
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||
import org.apache.dolphinscheduler.remote.command.TaskUpdatePidCommand; |
||||
import org.apache.dolphinscheduler.remote.processor.NettyRequestProcessor; |
||||
import org.apache.dolphinscheduler.server.master.processor.queue.TaskEvent; |
||||
import org.apache.dolphinscheduler.server.master.processor.queue.TaskEventService; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import com.google.common.base.Preconditions; |
||||
import io.netty.channel.Channel; |
||||
|
||||
/** |
||||
* task execute running processor |
||||
*/ |
||||
@Component |
||||
@Slf4j |
||||
public class TaskUpdatePidProcessor implements NettyRequestProcessor { |
||||
|
||||
@Autowired |
||||
private TaskEventService taskEventService; |
||||
|
||||
/** |
||||
* task ack process |
||||
* |
||||
* @param channel channel channel |
||||
* @param command command TaskExecuteAckCommand |
||||
*/ |
||||
@Override |
||||
public void process(Channel channel, Command command) { |
||||
Preconditions.checkArgument(CommandType.TASK_UPDATE_PID == command.getType(), |
||||
String.format("invalid command type : %s", command.getType())); |
||||
TaskUpdatePidCommand taskUpdatePidCommand = |
||||
JSONUtils.parseObject(command.getBody(), TaskUpdatePidCommand.class); |
||||
log.info("taskUpdatePidCommand: {}", taskUpdatePidCommand); |
||||
|
||||
TaskEvent taskEvent = TaskEvent.newUpdatePidEvent(taskUpdatePidCommand, |
||||
channel, |
||||
taskUpdatePidCommand.getMessageSenderAddress()); |
||||
taskEventService.addEvent(taskEvent); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,55 @@
|
||||
/* |
||||
* 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.remote.command; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Builder; |
||||
import lombok.Data; |
||||
import lombok.NoArgsConstructor; |
||||
|
||||
/** |
||||
* task execute running ack command |
||||
* from master to worker |
||||
*/ |
||||
@Data |
||||
@Builder |
||||
@AllArgsConstructor |
||||
@NoArgsConstructor |
||||
public class TaskUpdatePidAckMessage implements Serializable { |
||||
|
||||
private boolean success; |
||||
private int taskInstanceId; |
||||
|
||||
/** |
||||
* package response command |
||||
* |
||||
* @return command |
||||
*/ |
||||
public Command convert2Command() { |
||||
Command command = new Command(); |
||||
command.setType(CommandType.TASK_UPDATE_PID_ACK); |
||||
byte[] body = JSONUtils.toJsonByteArray(this); |
||||
command.setBody(body); |
||||
return command; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,83 @@
|
||||
/* |
||||
* 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.remote.command; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
||||
|
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import lombok.NoArgsConstructor; |
||||
import lombok.ToString; |
||||
|
||||
/** |
||||
* Task running message, means the task is running in worker. |
||||
*/ |
||||
@Data |
||||
@NoArgsConstructor |
||||
@ToString(callSuper = true) |
||||
@EqualsAndHashCode(callSuper = true) |
||||
public class TaskUpdatePidCommand extends BaseCommand { |
||||
|
||||
/** |
||||
* taskInstanceId |
||||
*/ |
||||
private int taskInstanceId; |
||||
|
||||
/** |
||||
* process instance id |
||||
*/ |
||||
private int processInstanceId; |
||||
|
||||
/** |
||||
* startTime |
||||
*/ |
||||
private long startTime; |
||||
|
||||
/** |
||||
* host |
||||
*/ |
||||
private String host; |
||||
|
||||
/** |
||||
* logPath |
||||
*/ |
||||
private String logPath; |
||||
|
||||
/** |
||||
* processId |
||||
*/ |
||||
private int processId; |
||||
|
||||
public TaskUpdatePidCommand(String messageSenderAddress, String messageReceiverAddress, long messageSendTime) { |
||||
super(messageSenderAddress, messageReceiverAddress, messageSendTime); |
||||
} |
||||
|
||||
/** |
||||
* package request command |
||||
* |
||||
* @return command |
||||
*/ |
||||
public Command convert2Command() { |
||||
Command command = new Command(); |
||||
command.setType(CommandType.TASK_UPDATE_PID); |
||||
byte[] body = JSONUtils.toJsonByteArray(this); |
||||
command.setBody(body); |
||||
return command; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,65 @@
|
||||
/* |
||||
* 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.worker.message; |
||||
|
||||
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext; |
||||
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||
import org.apache.dolphinscheduler.remote.command.TaskUpdatePidCommand; |
||||
import org.apache.dolphinscheduler.remote.exceptions.RemotingException; |
||||
import org.apache.dolphinscheduler.remote.utils.Host; |
||||
import org.apache.dolphinscheduler.server.worker.config.WorkerConfig; |
||||
import org.apache.dolphinscheduler.server.worker.rpc.WorkerRpcClient; |
||||
|
||||
import lombok.NonNull; |
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
@Component |
||||
public class TaskUpdatePidMessageSender implements MessageSender<TaskUpdatePidCommand> { |
||||
|
||||
@Autowired |
||||
private WorkerRpcClient workerRpcClient; |
||||
|
||||
@Autowired |
||||
private WorkerConfig workerConfig; |
||||
|
||||
@Override |
||||
public void sendMessage(TaskUpdatePidCommand message) throws RemotingException { |
||||
workerRpcClient.send(Host.of(message.getMessageReceiverAddress()), message.convert2Command()); |
||||
} |
||||
|
||||
@Override |
||||
public TaskUpdatePidCommand buildMessage(@NonNull TaskExecutionContext taskExecutionContext, |
||||
@NonNull String messageReceiverAddress) { |
||||
TaskUpdatePidCommand taskUpdatePidCommand = |
||||
new TaskUpdatePidCommand(workerConfig.getWorkerAddress(), |
||||
messageReceiverAddress, |
||||
System.currentTimeMillis()); |
||||
taskUpdatePidCommand.setTaskInstanceId(taskExecutionContext.getTaskInstanceId()); |
||||
taskUpdatePidCommand.setProcessInstanceId(taskExecutionContext.getProcessInstanceId()); |
||||
taskUpdatePidCommand.setHost(taskExecutionContext.getHost()); |
||||
taskUpdatePidCommand.setStartTime(taskExecutionContext.getStartTime()); |
||||
return taskUpdatePidCommand; |
||||
} |
||||
|
||||
@Override |
||||
public CommandType getMessageType() { |
||||
return CommandType.TASK_UPDATE_PID; |
||||
} |
||||
} |
@ -0,0 +1,71 @@
|
||||
/* |
||||
* 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.worker.processor; |
||||
|
||||
import org.apache.dolphinscheduler.common.utils.JSONUtils; |
||||
import org.apache.dolphinscheduler.plugin.task.api.utils.LogUtils; |
||||
import org.apache.dolphinscheduler.remote.command.Command; |
||||
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||
import org.apache.dolphinscheduler.remote.command.TaskUpdatePidAckMessage; |
||||
import org.apache.dolphinscheduler.remote.processor.NettyRequestProcessor; |
||||
import org.apache.dolphinscheduler.server.worker.message.MessageRetryRunner; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
import lombok.extern.slf4j.Slf4j; |
||||
|
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import com.google.common.base.Preconditions; |
||||
import io.netty.channel.Channel; |
||||
|
||||
/** |
||||
* task execute running ack processor |
||||
*/ |
||||
@Component |
||||
@Slf4j |
||||
public class TaskUpdatePidAckProcessor implements NettyRequestProcessor { |
||||
|
||||
@Resource |
||||
private MessageRetryRunner messageRetryRunner; |
||||
|
||||
@Override |
||||
public void process(Channel channel, Command command) { |
||||
Preconditions.checkArgument(CommandType.TASK_UPDATE_PID_ACK == command.getType(), |
||||
String.format("invalid command type : %s", command.getType())); |
||||
|
||||
TaskUpdatePidAckMessage updatePidAckCommand = JSONUtils.parseObject(command.getBody(), |
||||
TaskUpdatePidAckMessage.class); |
||||
if (updatePidAckCommand == null) { |
||||
log.error("task execute update pid ack command is null"); |
||||
return; |
||||
} |
||||
try { |
||||
LogUtils.setTaskInstanceIdMDC(updatePidAckCommand.getTaskInstanceId()); |
||||
log.info("task execute update pid ack command : {}", updatePidAckCommand); |
||||
|
||||
if (updatePidAckCommand.isSuccess()) { |
||||
messageRetryRunner.removeRetryMessage(updatePidAckCommand.getTaskInstanceId(), |
||||
CommandType.TASK_UPDATE_PID); |
||||
} |
||||
} finally { |
||||
LogUtils.removeTaskInstanceIdMDC(); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue