use of com.dtstack.taier.common.exception.RdosDefineException in project Taier by DTStack.
the class BatchTaskService method buildScheduleTaskShadeDTO.
/**
* 构建一个要发布到engine的任务DTO {@link ScheduleTaskShadeDTO}
* @param batchTask 要发布的任务集合
* @param parentTaskIds 父任务的id
* @return 调度任务DTO
*/
private ScheduleTaskShadeDTO buildScheduleTaskShadeDTO(final BatchTask batchTask, List<Long> parentTaskIds) {
if (batchTask.getId() <= 0) {
// 只有异常情况才会走到该逻辑
throw new RdosDefineException("batchTask id can't be 0", ErrorCode.SERVER_EXCEPTION);
}
final long taskId = batchTask.getId();
// 清空任务关联的batch_task_param, task_resource, task_task 表信息
this.batchTaskParamShadeService.clearDataByTaskId(taskId);
this.batchTaskResourceShadeService.clearDataByTaskId(taskId);
final List<BatchTaskParam> batchTaskParamList = this.batchTaskParamService.getTaskParam(batchTask.getId());
// 查询出任务所有的关联的资源(运行主体资源和依赖引用资源)
final List<BatchTaskResource> batchTaskResourceList = this.batchTaskResourceService.getTaskResources(batchTask.getId(), null);
List<Long> parentTaskList = this.batchTaskTaskService.getAllParentTaskId(batchTask.getId());
parentTaskIds.addAll(parentTaskList);
if (!CollectionUtils.isEmpty(batchTaskResourceList)) {
this.batchTaskResourceShadeService.saveTaskResource(batchTaskResourceList);
}
// 保存batch_task_shade
final ScheduleTaskShadeDTO scheduleTaskShadeDTO = new ScheduleTaskShadeDTO();
BeanUtils.copyProperties(batchTask, scheduleTaskShadeDTO);
scheduleTaskShadeDTO.setTaskId(batchTask.getId());
scheduleTaskShadeDTO.setScheduleStatus(EScheduleStatus.NORMAL.getVal());
if (!CollectionUtils.isEmpty(batchTaskParamList)) {
this.batchTaskParamShadeService.saveTaskParam(batchTaskParamList);
} else {
scheduleTaskShadeDTO.setTaskParams("");
}
return scheduleTaskShadeDTO;
}
use of com.dtstack.taier.common.exception.RdosDefineException in project Taier by DTStack.
the class BatchTaskService method initTaskInfo.
/**
*初始化 task的一些基本属性
*
* @param task
* @return
*/
private boolean initTaskInfo(BatchTaskBatchVO task) {
if (StringUtils.isBlank(task.getTaskDesc())) {
task.setTaskDesc("");
}
if (StringUtils.isBlank(task.getTaskParams())) {
task.setTaskParams("");
}
if (StringUtils.isBlank(task.getMainClass())) {
task.setMainClass("");
}
if (StringUtils.isBlank(task.getScheduleConf())) {
task.setScheduleConf(DEFAULT_SCHEDULE_CONF);
} else {
final JSONObject scheduleConf = JSON.parseObject(task.getScheduleConf());
final String beginDate = scheduleConf.getString("beginDate");
if (StringUtils.isBlank(beginDate) || "null".equalsIgnoreCase(beginDate)) {
throw new RdosDefineException("生效日期起至时间不能为空");
}
final String endDate = scheduleConf.getString("endDate");
if (StringUtils.isBlank(endDate) || "null".equalsIgnoreCase(endDate)) {
throw new RdosDefineException("生效日期结束时间不能为空");
}
}
if (task.getVersion() == null) {
task.setVersion(0);
}
if (task.getCreateUserId() == null) {
task.setCreateUserId(task.getUserId());
}
task.setGmtCreate(task.getGmtModified());
// 增加注释
task.setSqlText(this.createAnnotationText(task));
task.setSubmitStatus(ESubmitStatus.UNSUBMIT.getStatus());
task.setTaskParams(getDefaultTaskParam(task.getTenantId(), task.getTaskType()));
task.setScheduleStatus(EScheduleStatus.NORMAL.getVal());
task.setPeriodType(DEFAULT_SCHEDULE_PERIOD);
String scConf = DEFAULT_SCHEDULE_CONF;
int period = DEFAULT_SCHEDULE_PERIOD;
if (task.getFlowId() != null && task.getFlowId() > 0) {
final BatchTask flow = this.developTaskDao.getOne(task.getFlowId());
if (flow != null) {
scConf = flow.getScheduleConf();
final ScheduleCron scheduleCron;
try {
scheduleCron = ScheduleFactory.parseFromJson(scConf);
} catch (Exception e) {
throw new RdosDefineException(e.getMessage(), e);
}
period = scheduleCron.getPeriodType();
}
task.setScheduleConf(scConf);
}
task.setPeriodType(period);
if (Objects.isNull(task.getFlowId())) {
task.setFlowId(0L);
}
return true;
}
use of com.dtstack.taier.common.exception.RdosDefineException 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;
}
use of com.dtstack.taier.common.exception.RdosDefineException in project Taier by DTStack.
the class JobService method displayPeriods.
/**
* 查询上一个周期或者下一个周期实例
*
* @param isAfter 是否是上个周期
* @param jobId 实例id
* @param limit 查询个数
*/
public List<ReturnDisplayPeriodVO> displayPeriods(Boolean isAfter, String jobId, Integer limit) {
ScheduleJob scheduleJob = this.lambdaQuery().eq(ScheduleJob::getJobId, jobId).eq(ScheduleJob::getIsDeleted, Deleted.NORMAL.getStatus()).one();
if (scheduleJob == null) {
throw new RdosDefineException(ErrorCode.CAN_NOT_FIND_JOB);
}
// 需要根据查询的job的类型来
List<ScheduleJob> scheduleJobList = this.baseMapper.listAfterOrBeforeJobs(scheduleJob.getTaskId(), isAfter, scheduleJob.getCycTime(), scheduleJob.getType());
scheduleJobList.sort((o1, o2) -> {
if (!NumberUtils.isNumber(o1.getCycTime())) {
return 1;
}
if (!NumberUtils.isNumber(o2.getCycTime())) {
return -1;
}
return Long.compare(Long.parseLong(o2.getCycTime()), Long.parseLong(o1.getCycTime()));
});
if (scheduleJobList.size() > limit) {
scheduleJobList = scheduleJobList.subList(0, limit);
}
List<ReturnDisplayPeriodVO> vos = new ArrayList<>(scheduleJobList.size());
scheduleJobList.forEach(nextScheduleJob -> {
ReturnDisplayPeriodVO vo = new ReturnDisplayPeriodVO();
vo.setJobId(nextScheduleJob.getJobId());
vo.setCycTime(DateUtil.addTimeSplit(nextScheduleJob.getCycTime()));
vo.setStatus(nextScheduleJob.getStatus());
vos.add(vo);
});
return vos;
}
use of com.dtstack.taier.common.exception.RdosDefineException in project Taier by DTStack.
the class BatchSparkHiveSqlExeService method checkSingleSqlSyntax.
protected void checkSingleSqlSyntax(Long tenantId, String sql, String db, String taskParam, EScheduleJobType eScheduleJobType) {
try {
if (sql.trim().matches(EXPLAIN_REDEX)) {
return;
}
List<String> variables = Lists.newArrayList();
if (EScheduleJobType.SPARK_SQL == eScheduleJobType) {
// 添加set spark.sql.crossJoin.enabled=true
variables.add("set spark.sql.crossJoin.enabled=true");
}
// 建表语句中 有 lifecycle字段 需要去除
sql = SqlFormatUtil.init(sql).removeCatalogue().removeLifecycle().getSql();
String explainSql = "explain " + sql;
// 处理自定义函数逻辑
List<String> functionVariables = batchFunctionService.buildContainFunctions(sql, tenantId, eScheduleJobType.getType());
variables.addAll(functionVariables);
List<List<Object>> result = jdbcServiceImpl.executeQueryWithVariables(tenantId, null, eScheduleJobType, db, explainSql, variables, taskParam);
if (CollectionUtils.isNotEmpty(result)) {
String plan = result.get(1).get(0).toString();
if (plan.matches(SQL_EXCEPTION_REDEX)) {
throw new RdosDefineException(plan);
}
}
} catch (Exception e) {
throw new RdosDefineException(e.getMessage(), e);
}
}
Aggregations