Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
Expand All @@ -31,4 +32,11 @@ public interface SerialCommandMapper extends BaseMapper<SerialCommand> {

int deleteByWorkflowInstanceId(@Param("workflowInstanceId") int workflowInstanceId);

int launchSerialCommand(@Param("id") Integer id,
@Param("originState") int originState,
@Param("targetState") int targetState,
@Param("updateTime") Date updateTime);

int deleteByIdAndState(@Param("id") Integer id, @Param("state") int state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dolphinscheduler.dao.entity.SerialCommand;
import org.apache.dolphinscheduler.dao.model.SerialCommandDto;

import java.util.Date;
import java.util.List;

public interface SerialCommandDao extends IDao<SerialCommand> {
Expand All @@ -28,4 +29,8 @@ public interface SerialCommandDao extends IDao<SerialCommand> {

int deleteByWorkflowInstanceId(Integer workflowInstanceId);

boolean launchSerialCommand(Integer id, Date updateTime);

boolean deleteByIdAndState(Integer id, SerialCommandDto.State state);

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.dolphinscheduler.dao.repository.BaseDao;
import org.apache.dolphinscheduler.dao.repository.SerialCommandDao;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -47,4 +48,16 @@ public List<SerialCommandDto> fetchSerialCommands(int fetchSize) {
public int deleteByWorkflowInstanceId(Integer workflowInstanceId) {
return mybatisMapper.deleteByWorkflowInstanceId(workflowInstanceId);
}
@Override
public boolean launchSerialCommand(Integer id, Date updateTime) {
return mybatisMapper.launchSerialCommand(id,
SerialCommandDto.State.WAITING.getValue(),
SerialCommandDto.State.LAUNCHED.getValue(),
updateTime) > 0;
}

@Override
public boolean deleteByIdAndState(Integer id, SerialCommandDto.State state) {
return mybatisMapper.deleteByIdAndState(id, state.getValue()) > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,13 @@
delete from t_ds_serial_command where workflow_instance_id = #{workflowInstanceId}
</delete>

<update id="launchSerialCommand">
update t_ds_serial_command
set state = #{targetState}, update_time = #{updateTime}
where id = #{id} and state = #{originState}
</update>

<delete id="deleteByIdAndState">
delete from t_ds_serial_command where id = #{id} and state = #{state}
</delete>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.dao.repository.impl;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.dolphinscheduler.dao.BaseDaoTest;
import org.apache.dolphinscheduler.dao.entity.SerialCommand;
import org.apache.dolphinscheduler.dao.model.SerialCommandDto;
import org.apache.dolphinscheduler.dao.repository.SerialCommandDao;

import java.util.Date;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

class SerialCommandDaoImplTest extends BaseDaoTest {

@Autowired
private SerialCommandDao serialCommandDao;

@Test
void testWaitingCommandCanOnlyBeAcquiredOnce() {
SerialCommand command = insertWaitingCommand();
Date updateTime = new Date(System.currentTimeMillis() + 1000);
assertTrue(serialCommandDao.launchSerialCommand(command.getId(), updateTime));
assertFalse(serialCommandDao.launchSerialCommand(command.getId(), new Date()));
SerialCommand acquired = serialCommandDao.queryById(command.getId());
assertEquals(SerialCommandDto.State.LAUNCHED.getValue(), acquired.getState());
assertEquals(updateTime, acquired.getUpdateTime());
}

@Test
void testStaleWaitingSnapshotCannotDeleteLaunchedCommand() {
SerialCommand command = insertWaitingCommand();
assertTrue(serialCommandDao.launchSerialCommand(command.getId(), new Date()));
assertFalse(serialCommandDao.deleteByIdAndState(command.getId(), SerialCommandDto.State.WAITING));
assertNotNull(serialCommandDao.queryById(command.getId()));
}

@Test
void testWaitingCommandCanOnlyBeDiscardedOnce() {
SerialCommand command = insertWaitingCommand();
assertTrue(serialCommandDao.deleteByIdAndState(command.getId(), SerialCommandDto.State.WAITING));
assertFalse(serialCommandDao.deleteByIdAndState(command.getId(), SerialCommandDto.State.WAITING));
assertNull(serialCommandDao.queryById(command.getId()));
}

@Test
void testDiscardedCommandCannotBeAcquired() {
SerialCommand command = insertWaitingCommand();
assertTrue(serialCommandDao.deleteByIdAndState(command.getId(), SerialCommandDto.State.WAITING));
assertFalse(serialCommandDao.launchSerialCommand(command.getId(), new Date()));
}

private SerialCommand insertWaitingCommand() {
SerialCommand command = SerialCommand.builder()
.workflowInstanceId(1)
.workflowDefinitionCode(1L)
.workflowDefinitionVersion(1)
.command("{}")
.state(SerialCommandDto.State.WAITING.getValue())
.createTime(new Date())
.updateTime(new Date())
.build();
assertEquals(1, serialCommandDao.insert(command));
return command;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import org.apache.dolphinscheduler.common.enums.WorkflowExecutionStatus;
import org.apache.dolphinscheduler.common.utils.DateUtils;
import org.apache.dolphinscheduler.dao.entity.Command;
import org.apache.dolphinscheduler.dao.entity.WorkflowInstance;
import org.apache.dolphinscheduler.dao.model.SerialCommandDto;
import org.apache.dolphinscheduler.dao.repository.CommandDao;
Expand All @@ -34,8 +33,6 @@
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

@Slf4j
Expand All @@ -59,40 +56,46 @@ public abstract class AbstractSerialCommandHandler implements ISerialCommandHand
@Autowired
protected SerialCommandDao serialCommandDao;

protected void launchSerialCommand(SerialCommandDto serialCommand) {
transactionTemplate.execute(new TransactionCallback<Object>() {

@Override
public Void doInTransaction(TransactionStatus status) {
final Command command = serialCommand.getCommand();
commandDao.insert(command);

serialCommand.setState(SerialCommandDto.State.LAUNCHED);
serialCommandDao.updateById(serialCommand.toEntity());
return null;
protected boolean launchSerialCommand(SerialCommandDto serialCommand) {
final boolean launched = Boolean.TRUE.equals(transactionTemplate.execute(status -> {
// Claim before inserting so two WAITING snapshots cannot create two Commands.
if (!serialCommandDao.launchSerialCommand(serialCommand.getId(), DateUtils.getCurrentDate())) {
log.info("Skip launching SerialCommand: {}, it is no longer in WAITING state", serialCommand.getId());
return false;
}
if (commandDao.insert(serialCommand.getCommand()) != 1) {
throw new IllegalStateException("Failed to insert Command for SerialCommand: " + serialCommand.getId());
}
});
return true;
}));
if (launched) {
// Keep the snapshot unchanged if the transaction rolls back.
serialCommand.setState(SerialCommandDto.State.LAUNCHED);
}
return launched;
}

protected void discardSerialCommandAndStopWorkflowInstanceInDB(SerialCommandDto serialCommand) {
transactionTemplate.execute(new TransactionCallback<Object>() {

@Override
public Void doInTransaction(TransactionStatus status) {
serialCommandDao.deleteById(serialCommand.getId());

// todo: call api to stop the workflow instance
final Integer workflowInstanceId = serialCommand.getWorkflowInstanceId();
final WorkflowInstance workflowInstance = workflowInstanceDao.queryById(workflowInstanceId);
workflowInstance.setEndTime(DateUtils.getCurrentDate());
workflowInstance.setState(WorkflowExecutionStatus.STOP);
workflowInstanceDao.upsertWorkflowInstance(workflowInstance);
WorkflowInstanceMetrics.recordWorkflowInstanceFinish(
WorkflowExecutionStatus.STOP,
workflowInstance.getWorkflowDefinitionCode());
return null;
protected boolean discardSerialCommandAndStopWorkflowInstanceInDB(SerialCommandDto serialCommand) {
return Boolean.TRUE.equals(transactionTemplate.execute(status -> {
// A stale WAITING snapshot must not discard a Command another coordinator has launched.
if (!serialCommandDao.deleteByIdAndState(serialCommand.getId(), SerialCommandDto.State.WAITING)) {
log.info("Skip discarding SerialCommand: {}, it is no longer in WAITING state", serialCommand.getId());
return false;
}

// todo: call api to stop the workflow instance
final Integer workflowInstanceId = serialCommand.getWorkflowInstanceId();
final WorkflowInstance workflowInstance = workflowInstanceDao.queryById(workflowInstanceId);
workflowInstance.setEndTime(DateUtils.getCurrentDate());
workflowInstance.setState(WorkflowExecutionStatus.STOP);
if (!workflowInstanceDao.updateById(workflowInstance)) {
throw new IllegalStateException("Failed to stop WorkflowInstance: " + workflowInstanceId);
}
});
WorkflowInstanceMetrics.recordWorkflowInstanceFinish(
WorkflowExecutionStatus.STOP,
workflowInstance.getWorkflowDefinitionCode());
return true;
}));
}

protected void stopWorkflowInstanceInMaster(SerialCommandDto serialCommand) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ public void handle(SerialCommandsGroup serialCommandsGroup) {
SerialCommandDto serialCommand = serialCommands.get(i);
if (i == 0) {
if (serialCommand.getState() == SerialCommandDto.State.WAITING) {
launchSerialCommand(serialCommand);
if (!launchSerialCommand(serialCommand)) {
// Retry this group from a fresh snapshot in the next coordinator round.
return;
}
log.info("Launched SerialCommand: {}", serialCommand);
}
continue;
Expand All @@ -51,7 +54,9 @@ public void handle(SerialCommandsGroup serialCommandsGroup) {
throw new IllegalStateException(
"The post SerialCommand except WAITING state but -> " + serialCommand.getState());
}
discardSerialCommandAndStopWorkflowInstanceInDB(serialCommand);
if (!discardSerialCommandAndStopWorkflowInstanceInDB(serialCommand)) {
return;
}
log.info("Discard SerialCommand: {}", serialCommand);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,17 @@ public void handle(SerialCommandsGroup serialCommandsGroup) {
for (int i = 0; i < serialCommands.size(); i++) {
final SerialCommandDto serialCommand = serialCommands.get(i);
if (i == serialCommands.size() - 1) {
if (serialCommand.getState() == SerialCommandDto.State.WAITING) {
launchSerialCommand(serialCommand);
if (serialCommand.getState() == SerialCommandDto.State.WAITING && launchSerialCommand(serialCommand)) {
Comment thread
michaellx1057 marked this conversation as resolved.
Resolved
log.info("Launched SerialCommand: {}", serialCommand);
}
continue;
}

if (serialCommand.getState() == SerialCommandDto.State.WAITING) {
discardSerialCommandAndStopWorkflowInstanceInDB(serialCommand);
if (!discardSerialCommandAndStopWorkflowInstanceInDB(serialCommand)) {
Comment thread
michaellx1057 marked this conversation as resolved.
Resolved
// Re-read the group before deciding whether a launched predecessor must be stopped.
return;
}
log.info("Discard SerialCommand: {}", serialCommand);
} else {
stopWorkflowInstanceInMaster(serialCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public void handle(SerialCommandsGroup serialCommandsGroup) {
return;
}
final SerialCommandDto serialCommand = serialCommands.get(0);
if (serialCommand.getState() == SerialCommandDto.State.WAITING) {
launchSerialCommand(serialCommand);
if (serialCommand.getState() == SerialCommandDto.State.WAITING && launchSerialCommand(serialCommand)) {
Comment thread
michaellx1057 marked this conversation as resolved.
Resolved
log.info("Launched SerialCommand: {}", serialCommand);
}
}
Expand Down
Loading
Loading