Skip to content
Open
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 @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -107,7 +106,7 @@ public class SchedulerServiceImpl extends BaseServiceImpl implements SchedulerSe
private EnvironmentMapper environmentMapper;

@Autowired
private TenantMapper tenantMapper;
private TenantExistValidator tenantExistValidator;

/**
* save schedule
Expand Down Expand Up @@ -166,7 +165,7 @@ public Map<String, Object> insertSchedule(User loginUser,
Schedule scheduleObj = new Schedule();
Date now = new Date();

checkValidTenant(tenantCode);
tenantExistValidator.validate(tenantCode);

scheduleObj.setTenantCode(tenantCode);
scheduleObj.setProjectName(project.getName());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -759,7 +758,7 @@ private void updateSchedule(Map<String, Object> result, Schedule schedule, Workf

Date now = new Date();

checkValidTenant(tenantCode);
tenantExistValidator.validate(tenantCode);
schedule.setTenantCode(tenantCode);

// updateWorkflowInstance param
Expand Down Expand Up @@ -818,17 +817,4 @@ private void updateSchedule(Map<String, Object> 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);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
* <p> If the tenant does not exist, an {@link IllegalArgumentException} will be thrown. </p>
*/
@Slf4j
@Component
public class TenantExistValidator implements IValidator<String> {

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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,6 +32,12 @@
@Component
public class BackfillWorkflowDTOValidator implements IValidator<BackfillWorkflowDTO> {

private final TenantExistValidator tenantExistValidator;

public BackfillWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
this.tenantExistValidator = tenantExistValidator;
}

@Override
public void validate(final BackfillWorkflowDTO backfillWorkflowDTO) {
final BackfillWorkflowDTO.BackfillParamsDTO backfillParams = backfillWorkflowDTO.getBackfillParams();
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,6 +30,12 @@
@Component
public class TriggerWorkflowDTOValidator implements IValidator<TriggerWorkflowDTO> {

private final TenantExistValidator tenantExistValidator;

public TriggerWorkflowDTOValidator(TenantExistValidator tenantExistValidator) {
this.tenantExistValidator = tenantExistValidator;
}

@Override
public void validate(final TriggerWorkflowDTO triggerWorkflowDTO) {
if (triggerWorkflowDTO.getExecType() != CommandType.START_PROCESS) {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import org.apache.dolphinscheduler.dao.entity.Tenant;

import java.util.Optional;

public interface TenantDao extends IDao<Tenant> {

Optional<Tenant> queryByCode(String tenantCode);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,4 +35,8 @@ public TenantDaoImpl(@NonNull TenantMapper tenantMapper) {
super(tenantMapper);
}

@Override
public Optional<Tenant> queryByCode(String tenantCode) {
return Optional.ofNullable(mybatisMapper.queryByTenantCode(tenantCode));
}
}
Loading