diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java index c93c23791518..c308c7f420d3 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ExecutorServiceImpl.java @@ -163,6 +163,7 @@ public class ExecutorServiceImpl extends BaseServiceImpl implements ExecutorServ @Transactional public Integer triggerWorkflowDefinition(final WorkflowTriggerRequest triggerRequest) { final TriggerWorkflowDTO triggerWorkflowDTO = triggerWorkflowRequestTransformer.transform(triggerRequest); + // todo: use validator chain triggerWorkflowDTOValidator.validate(triggerWorkflowDTO); return executorClient.triggerWorkflowDefinition().execute(triggerWorkflowDTO); } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java index a87d36c7af60..4d97ff771ac2 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/SchedulerServiceImpl.java @@ -31,6 +31,7 @@ import org.apache.dolphinscheduler.api.service.SchedulerService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.api.validator.TenantExistValidator; import org.apache.dolphinscheduler.api.vo.ScheduleVO; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.FailureStrategy; @@ -43,13 +44,11 @@ import org.apache.dolphinscheduler.dao.entity.Environment; import org.apache.dolphinscheduler.dao.entity.Project; import org.apache.dolphinscheduler.dao.entity.Schedule; -import org.apache.dolphinscheduler.dao.entity.Tenant; import org.apache.dolphinscheduler.dao.entity.User; import org.apache.dolphinscheduler.dao.entity.WorkflowDefinition; import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper; -import org.apache.dolphinscheduler.dao.mapper.TenantMapper; import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionMapper; import org.apache.dolphinscheduler.scheduler.api.SchedulerApi; import org.apache.dolphinscheduler.service.cron.CronUtils; @@ -107,7 +106,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe private EnvironmentMapper environmentMapper; @Autowired - private TenantMapper tenantMapper; + private TenantExistValidator tenantExistValidator; /** * save schedule @@ -166,7 +165,7 @@ public Map insertSchedule(User loginUser, Schedule scheduleObj = new Schedule(); Date now = new Date(); - checkValidTenant(tenantCode); + tenantExistValidator.validate(tenantCode); scheduleObj.setTenantCode(tenantCode); scheduleObj.setProjectName(project.getName()); @@ -276,7 +275,7 @@ public Schedule createSchedulesV2(User loginUser, scheduleExists.getId()); } - checkValidTenant(scheduleCreateRequest.getTenantCode()); + tenantExistValidator.validate(scheduleCreateRequest.getTenantCode()); Schedule schedule = scheduleCreateRequest.convert2Schedule(); Environment environment = environmentMapper.queryByEnvironmentCode(schedule.getEnvironmentCode()); @@ -759,7 +758,7 @@ private void updateSchedule(Map result, Schedule schedule, Workf Date now = new Date(); - checkValidTenant(tenantCode); + tenantExistValidator.validate(tenantCode); schedule.setTenantCode(tenantCode); // updateWorkflowInstance param @@ -818,17 +817,4 @@ private void updateSchedule(Map result, Schedule schedule, Workf putMsg(result, Status.SUCCESS); } - /** - * check valid tenant - * - * @param tenantCode - */ - private void checkValidTenant(String tenantCode) { - if (!Constants.DEFAULT.equals(tenantCode)) { - Tenant tenant = tenantMapper.queryByTenantCode(tenantCode); - if (tenant == null) { - throw new ServiceException(Status.TENANT_NOT_EXIST, tenantCode); - } - } - } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/TenantExistValidator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/TenantExistValidator.java new file mode 100644 index 000000000000..d37257d22a76 --- /dev/null +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/TenantExistValidator.java @@ -0,0 +1,46 @@ +/* + * 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.api.validator; + +import org.apache.dolphinscheduler.dao.repository.TenantDao; + +import lombok.extern.slf4j.Slf4j; + +import org.springframework.stereotype.Component; + +/** + * This validator is used to validate whether the tenant exists. + *

If the tenant does not exist, an {@link IllegalArgumentException} will be thrown.

+ */ +@Slf4j +@Component +public class TenantExistValidator implements IValidator { + + private final TenantDao tenantDao; + + public TenantExistValidator(TenantDao tenantDao) { + this.tenantDao = tenantDao; + } + + @Override + public void validate(String tenantCode) { + if (!tenantDao.queryByCode(tenantCode).isPresent()) { + throw new IllegalArgumentException(String.format("Tenant: [%s] not exists", tenantCode)); + } + } +} diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java index 634d964a122d..0b5da7e85a3f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/BackfillWorkflowDTOValidator.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.validator.workflow; import org.apache.dolphinscheduler.api.validator.IValidator; +import org.apache.dolphinscheduler.api.validator.TenantExistValidator; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.ReleaseState; @@ -31,6 +32,12 @@ @Component public class BackfillWorkflowDTOValidator implements IValidator { + private final TenantExistValidator tenantExistValidator; + + public BackfillWorkflowDTOValidator(TenantExistValidator tenantExistValidator) { + this.tenantExistValidator = tenantExistValidator; + } + @Override public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) { final BackfillWorkflowDTO.BackfillParamsDTO backfillParams = backfillWorkflowDTO.getBackfillParams(); @@ -52,5 +59,6 @@ public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) { if (backfillWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) { throw new IllegalStateException("The workflowDefinition should be online"); } + tenantExistValidator.validate(backfillWorkflowDTO.getTenantCode()); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java index e994120e9d07..b753714d9777 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/validator/workflow/TriggerWorkflowDTOValidator.java @@ -18,6 +18,7 @@ package org.apache.dolphinscheduler.api.validator.workflow; import org.apache.dolphinscheduler.api.validator.IValidator; +import org.apache.dolphinscheduler.api.validator.TenantExistValidator; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.ReleaseState; @@ -29,6 +30,12 @@ @Component public class TriggerWorkflowDTOValidator implements IValidator { + private final TenantExistValidator tenantExistValidator; + + public TriggerWorkflowDTOValidator(TenantExistValidator tenantExistValidator) { + this.tenantExistValidator = tenantExistValidator; + } + @Override public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) { if (triggerWorkflowDTO.getExecType() != CommandType.START_PROCESS) { @@ -40,5 +47,6 @@ public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) { if (triggerWorkflowDTO.getWorkflowDefinition().getReleaseState() != ReleaseState.ONLINE) { throw new IllegalStateException("The workflowDefinition should be online"); } + tenantExistValidator.validate(triggerWorkflowDTO.getTenantCode()); } } diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java index bb49fa4e7a61..9323efaa1794 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/SchedulerServiceTest.java @@ -26,6 +26,7 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.exceptions.ServiceException; import org.apache.dolphinscheduler.api.service.impl.SchedulerServiceImpl; +import org.apache.dolphinscheduler.api.validator.TenantExistValidator; import org.apache.dolphinscheduler.common.constants.Constants; import org.apache.dolphinscheduler.common.enums.ReleaseState; import org.apache.dolphinscheduler.dao.entity.Environment; @@ -36,11 +37,8 @@ import org.apache.dolphinscheduler.dao.mapper.EnvironmentMapper; import org.apache.dolphinscheduler.dao.mapper.ProjectMapper; import org.apache.dolphinscheduler.dao.mapper.ScheduleMapper; -import org.apache.dolphinscheduler.dao.mapper.TenantMapper; import org.apache.dolphinscheduler.dao.mapper.WorkflowDefinitionMapper; -import org.apache.dolphinscheduler.dao.mapper.WorkflowTaskRelationMapper; import org.apache.dolphinscheduler.scheduler.api.SchedulerApi; -import org.apache.dolphinscheduler.service.process.ProcessService; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -63,15 +61,6 @@ public class SchedulerServiceTest extends BaseServiceTestTool { @InjectMocks private SchedulerServiceImpl schedulerService; - @Mock - private WorkflowTaskRelationMapper workflowTaskRelationMapper; - - @Mock - private MonitorService monitorService; - - @Mock - private ProcessService processService; - @Mock private ScheduleMapper scheduleMapper; @@ -94,7 +83,7 @@ public class SchedulerServiceTest extends BaseServiceTestTool { private EnvironmentMapper environmentMapper; @Mock - private TenantMapper tenantMapper; + private TenantExistValidator tenantExistValidator; protected static User user; protected Exception exception; @@ -128,6 +117,7 @@ public void testCreateSchedulesV2() { scheduleCreateRequest.setWorkflowDefinitionCode(processDefinitionCode); scheduleCreateRequest.setEnvironmentCode(environmentCode); scheduleCreateRequest.setTenantCode(Constants.DEFAULT); + scheduleCreateRequest.setStartTime(startTime); // error process definition not exists exception = Assertions.assertThrows(ServiceException.class, diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/TenantDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/TenantDao.java index 9f48f372da91..731a852819a4 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/TenantDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/TenantDao.java @@ -19,6 +19,10 @@ import org.apache.dolphinscheduler.dao.entity.Tenant; +import java.util.Optional; + public interface TenantDao extends IDao { + Optional queryByCode(String tenantCode); + } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TenantDaoImpl.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TenantDaoImpl.java index 45f9a5fcba69..25cca1dab8a1 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TenantDaoImpl.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TenantDaoImpl.java @@ -22,6 +22,8 @@ import org.apache.dolphinscheduler.dao.repository.BaseDao; import org.apache.dolphinscheduler.dao.repository.TenantDao; +import java.util.Optional; + import lombok.NonNull; import org.springframework.stereotype.Repository; @@ -33,4 +35,8 @@ public TenantDaoImpl(@NonNull TenantMapper tenantMapper) { super(tenantMapper); } + @Override + public Optional queryByCode(String tenantCode) { + return Optional.ofNullable(mybatisMapper.queryByTenantCode(tenantCode)); + } }