Search in sources :

Example 1 with BatchReadWriteLock

use of com.dtstack.taier.dao.domain.BatchReadWriteLock in project Taier by DTStack.

the class BatchTaskService method updateTask.

/**
 * 新增/更新任务
 *
 * @param task
 * @param isEditBaseInfo 如果是右键编辑的情况则不更新任务参数
 * @return
 */
@Transactional
public BatchTaskBatchVO updateTask(final BatchTaskBatchVO task, final Boolean isEditBaseInfo) {
    if (task.getName() == null) {
        throw new RdosDefineException("任务名称不能为空.", ErrorCode.INVALID_PARAMETERS);
    }
    if (!PublicUtil.matcher(task.getName(), TASK_PATTERN)) {
        throw new RdosDefineException("名称只能由字母、数据、中文、下划线组成", ErrorCode.INVALID_PARAMETERS);
    }
    task.setGmtModified(Timestamp.valueOf(LocalDateTime.now()));
    BatchTask batchTask = this.developTaskDao.getByName(task.getName(), task.getTenantId());
    boolean isAdd = false;
    if (task.getId() > 0) {
        // update
        BatchTask specialTask = getOneWithError(task.getId());
        if (task.getTaskType() == null) {
            task.setTaskType(specialTask.getTaskType());
        }
        String oriTaskName = specialTask.getName();
        if (batchTask != null && !batchTask.getId().equals(task.getId())) {
            throw new RdosDefineException(ErrorCode.NAME_ALREADY_EXIST);
        }
        BatchTask specialTask1 = new BatchTask();
        ReadWriteLockVO readWriteLockVO = this.readWriteLockService.dealWithLock(task.getTenantId(), task.getId(), ReadWriteLockType.BATCH_TASK, task.getUserId(), res -> {
            task.setCreateUser(null);
            task.setIsDeleted(Deleted.NORMAL.getStatus());
            if (task.getVersion() == null) {
                task.setVersion(0);
            }
            PublicUtil.copyPropertiesIgnoreNull(task, specialTask1);
            final Integer updateResult = this.developTaskDao.update(specialTask1);
            if (updateResult == 1) {
                task.setVersion(task.getVersion() + 1);
            }
            return updateResult;
        });
        task.setReadWriteLockVO(readWriteLockVO);
        // 如果是工作流任务 更新父节点调度类型时,需要同样更新子节点
        if (EScheduleJobType.WORK_FLOW.getVal().equals(task.getTaskType()) && task.getFlowId() == 0 && StringUtils.isNotEmpty(task.getScheduleConf())) {
            updateSonTaskPeriodType(task.getId(), task.getPeriodType(), task.getScheduleConf());
        }
        if (!oriTaskName.equals(task.getName())) {
            // 修改名字需要同步到taskShade
            this.taskService.updateTaskName(task.getId(), task.getName());
        }
        LOGGER.info("success update batchTask, taskId:{}", task.getId());
    } else {
        if (batchTask != null) {
            throw new RdosDefineException(ErrorCode.NAME_ALREADY_EXIST);
        }
        // 初始化task的一些属性
        isAdd = initTaskInfo(task);
        BatchTask insertTask = new BatchTask();
        BeanUtils.copyProperties(task, insertTask);
        // 如果是工作流获取父任务的锁 用来保证父任务一定会更新成功 这里有并发问题 如果同时对一个工作流添加子任务 会丢失
        if (task.getFlowId() > 0) {
            BatchTask parentTask = developTaskDao.getOne(task.getFlowId());
            BatchReadWriteLock readWriteLock = developReadWriteLockDao.getByTenantIdAndRelationIdAndType(0L, parentTask.getId(), ReadWriteLockType.BATCH_TASK.name());
            if (readWriteLock == null) {
                throw new RdosDefineException("父任务锁不存在");
            }
            if (!readWriteLock.getVersion().equals(task.getParentReadWriteLockVersion())) {
                throw new RdosDefineException("当前任务已被修改,请重新打开任务后再次提交");
            }
        }
        developTaskDao.insert(insertTask);
        task.setTaskId(insertTask.getId());
        task.setId(insertTask.getId());
        // parseCreateTaskExeArgs(task);
        // 新增锁
        ReadWriteLockVO readWriteLockVO = this.readWriteLockService.getLock(task.getTenantId(), task.getUserId(), ReadWriteLockType.BATCH_TASK.name(), task.getId(), null);
        task.setReadWriteLockVO(readWriteLockVO);
        LOGGER.info("success insert batchTask, taskId:{}", task.getId());
    }
    // 右键编辑时会调用另一个接口
    if (BooleanUtils.isNotTrue(isEditBaseInfo)) {
        if (!EScheduleJobType.WORK_FLOW.getVal().equals(task.getTaskType()) && !EScheduleJobType.VIRTUAL.getVal().equals(task.getTaskType())) {
            // 新增加不校验自定义参数
            if (!isAdd) {
                this.batchTaskParamService.checkParams(task.getSqlText(), task.getTaskVariables());
            }
        }
        this.batchTaskParamService.addOrUpdateTaskParam(task);
    }
    final BatchTaskBatchVO batchTaskBatchVO = new BatchTaskBatchVO(task);
    batchTaskBatchVO.setReadWriteLockVO(task.getReadWriteLockVO());
    batchTaskBatchVO.setVersion(task.getVersion());
    return batchTaskBatchVO;
}
Also used : BatchTask(com.dtstack.taier.dao.domain.BatchTask) BatchTaskBatchVO(com.dtstack.taier.develop.dto.devlop.BatchTaskBatchVO) RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ReadWriteLockVO(com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO) BatchReadWriteLock(com.dtstack.taier.dao.domain.BatchReadWriteLock) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BatchReadWriteLock

use of com.dtstack.taier.dao.domain.BatchReadWriteLock in project Taier by DTStack.

the class ReadWriteLockService method checkLock.

/**
 * 锁检查,判断是否有其他人也在修改相同的文件
 * @param userId
 * @param tenantId
 * @param relationId
 * @param type
 * @param lockVersion
 * @param relationLocalVersion
 * @param relationVersion
 * @return
 */
private ReadWriteLockVO checkLock(Long userId, Long tenantId, Long relationId, ReadWriteLockType type, Integer lockVersion, Integer relationLocalVersion, Integer relationVersion) {
    BatchReadWriteLock readWriteLock = developReadWriteLockDao.getByTenantIdAndRelationIdAndType(tenantId, relationId, type.name());
    if (readWriteLock == null) {
        throw new RdosDefineException(ErrorCode.LOCK_IS_NOT_EXISTS);
    }
    Long modifyUserId = readWriteLock.getModifyUserId();
    int version = readWriteLock.getVersion();
    // 初始化返回对象
    ReadWriteLockVO readWriteLockVO = new ReadWriteLockVO();
    readWriteLockVO.setGmtModified(readWriteLock.getGmtModified());
    readWriteLockVO.setLastKeepLockUserName(userService.getUserName(modifyUserId));
    // 任务版本是否保持一致
    if (!relationLocalVersion.equals(relationVersion)) {
        // 表示已经被提交了
        // 提示任务版本已经更新不能在提交,是否保存到本地,或取消修改
        readWriteLockVO.setResult(TaskLockStatus.UPDATE_COMPLETED.getVal());
        return readWriteLockVO;
    } else if (modifyUserId.equals(userId) && version == lockVersion) {
        readWriteLockVO.setResult(TaskLockStatus.TO_UPDATE.getVal());
        readWriteLockVO.setVersion(lockVersion);
        return readWriteLockVO;
    } else {
        // 提示其他人也正在编辑,是否取消修改
        readWriteLockVO.setResult(TaskLockStatus.TO_CONFIRM.getVal());
        return readWriteLockVO;
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ReadWriteLockVO(com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO) BatchReadWriteLock(com.dtstack.taier.dao.domain.BatchReadWriteLock)

Example 3 with BatchReadWriteLock

use of com.dtstack.taier.dao.domain.BatchReadWriteLock in project Taier by DTStack.

the class ReadWriteLockService method forceUpdateLock.

/**
 * @param userId
 * @param type
 * @param relationId
 * @param tenantId
 * @return
 */
private ReadWriteLockVO forceUpdateLock(Long userId, ReadWriteLockType type, Long relationId, Long tenantId) {
    BatchReadWriteLock readWriteLock = developReadWriteLockDao.getByTenantIdAndRelationIdAndType(tenantId, relationId, type.name());
    if (readWriteLock != null) {
        developReadWriteLockDao.updateVersionAndModifyUserIdDefinitized(readWriteLock.getId(), userId);
        readWriteLock = developReadWriteLockDao.getOne(readWriteLock.getId());
        ReadWriteLockVO readWriteLockVO = ReadWriteLockVO.toVO(readWriteLock);
        String userName = userService.getUserName(readWriteLock.getModifyUserId());
        readWriteLockVO.setLastKeepLockUserName(userName);
        return readWriteLockVO;
    } else {
        throw new RdosDefineException(ErrorCode.LOCK_IS_NOT_EXISTS);
    }
}
Also used : RdosDefineException(com.dtstack.taier.common.exception.RdosDefineException) ReadWriteLockVO(com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO) BatchReadWriteLock(com.dtstack.taier.dao.domain.BatchReadWriteLock)

Example 4 with BatchReadWriteLock

use of com.dtstack.taier.dao.domain.BatchReadWriteLock in project Taier by DTStack.

the class ReadWriteLockService method insert.

/**
 * 添加锁对象
 * @param tenantId
 * @param fileId
 * @param type
 * @param userId
 * @return
 */
private ReadWriteLockVO insert(Long tenantId, Long fileId, String type, Long userId) {
    BatchReadWriteLock readWriteLock = new BatchReadWriteLock();
    readWriteLock.setTenantId(tenantId);
    readWriteLock.setLockName(uniteName(fileId, tenantId, type));
    readWriteLock.setCreateUserId(userId);
    readWriteLock.setModifyUserId(userId);
    readWriteLock.setRelationId(fileId);
    readWriteLock.setType(type);
    developReadWriteLockDao.insert(readWriteLock);
    ReadWriteLockVO readWriteLockVO = ReadWriteLockVO.toVO(readWriteLock);
    readWriteLockVO.setVersion(INIT_VERSION);
    readWriteLockVO.setGmtModified(Timestamp.valueOf(LocalDateTime.now()));
    readWriteLockVO.setLastKeepLockUserName(userService.getUserName(userId));
    return readWriteLockVO;
}
Also used : ReadWriteLockVO(com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO) BatchReadWriteLock(com.dtstack.taier.dao.domain.BatchReadWriteLock)

Example 5 with BatchReadWriteLock

use of com.dtstack.taier.dao.domain.BatchReadWriteLock in project Taier by DTStack.

the class ReadWriteLockService method getDetail.

/**
 * 获取锁VO对象
 * @param tenantId
 * @param relationId
 * @param type
 * @param userId
 * @param modifyUserId
 * @param gmtModified
 * @return
 */
public ReadWriteLockVO getDetail(Long tenantId, Long relationId, ReadWriteLockType type, Long userId, Long modifyUserId, Timestamp gmtModified) {
    BatchReadWriteLock readWriteLock = developReadWriteLockDao.getByTenantIdAndRelationIdAndType(tenantId, relationId, type.name());
    if (readWriteLock == null) {
        ReadWriteLockVO readWriteLockVO = new ReadWriteLockVO();
        readWriteLockVO.setLastKeepLockUserName(userService.getUserName(modifyUserId));
        readWriteLockVO.setGmtModified(gmtModified);
        return readWriteLockVO;
    }
    ReadWriteLockVO readWriteLockVO = ReadWriteLockVO.toVO(readWriteLock);
    readWriteLockVO.setLastKeepLockUserName(userService.getUserName(readWriteLockVO.getModifyUserId()));
    return readWriteLockVO;
}
Also used : ReadWriteLockVO(com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO) BatchReadWriteLock(com.dtstack.taier.dao.domain.BatchReadWriteLock)

Aggregations

BatchReadWriteLock (com.dtstack.taier.dao.domain.BatchReadWriteLock)8 ReadWriteLockVO (com.dtstack.taier.develop.dto.devlop.ReadWriteLockVO)8 RdosDefineException (com.dtstack.taier.common.exception.RdosDefineException)5 Transactional (org.springframework.transaction.annotation.Transactional)3 Callback (com.dtstack.taier.common.Callback)1 ReadWriteLockType (com.dtstack.taier.common.enums.ReadWriteLockType)1 TaskLockStatus (com.dtstack.taier.common.enums.TaskLockStatus)1 ErrorCode (com.dtstack.taier.common.exception.ErrorCode)1 BatchTask (com.dtstack.taier.dao.domain.BatchTask)1 DevelopReadWriteLockDao (com.dtstack.taier.dao.mapper.DevelopReadWriteLockDao)1 BatchTaskBatchVO (com.dtstack.taier.develop.dto.devlop.BatchTaskBatchVO)1 UserService (com.dtstack.taier.develop.service.user.UserService)1 Maps (com.google.common.collect.Maps)1 Timestamp (java.sql.Timestamp)1 LocalDateTime (java.time.LocalDateTime)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 CollectionUtils (org.apache.commons.collections.CollectionUtils)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1