cherry-pick [Bug] [API] Before deleting a worker group, check whether there is environment that reference the worker group. #12534

This commit is contained in:
houshitao 2022-10-27 16:34:08 +08:00 committed by zhuangchong
parent eb449cdd44
commit c3a7af2d6e
3 changed files with 18 additions and 2 deletions

View File

@ -440,8 +440,8 @@ public enum Status {
FUNCTION_DISABLED(1400002, "The current feature is disabled.", "当前功能已被禁用"),
SCHEDULE_TIME_NUMBER(1400003, "The number of complement dates exceed 100.", "补数日期个数超过100"),
DESCRIPTION_TOO_LONG_ERROR(1400004, "description is too long error", "描述过长"),
;
DELETE_WORKER_GROUP_BY_ID_FAIL_ENV(1400005,
"delete worker group fail, for there are [{0}] enviroments using:{1}", "删除工作组失败,有 [{0}] 个环境正在使用:{1}");
private final int code;
private final String enMsg;
private final String zhMsg;

View File

@ -28,9 +28,11 @@ import org.apache.dolphinscheduler.common.Constants;
import org.apache.dolphinscheduler.common.enums.AuthorizationType;
import org.apache.dolphinscheduler.common.enums.NodeType;
import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.EnvironmentWorkerGroupRelation;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.WorkerGroup;
import org.apache.dolphinscheduler.dao.mapper.EnvironmentWorkerGroupRelationMapper;
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper;
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper;
import org.apache.dolphinscheduler.service.registry.RegistryClient;
@ -74,6 +76,9 @@ public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGro
@Autowired
private RegistryClient registryClient;
@Autowired
private EnvironmentWorkerGroupRelationMapper environmentWorkerGroupRelationMapper;
/**
* create or update a worker group
*
@ -328,6 +333,13 @@ public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGro
putMsg(result, Status.DELETE_WORKER_GROUP_BY_ID_FAIL, processInstances.size());
return result;
}
List<EnvironmentWorkerGroupRelation> environmentWorkerGroupRelationList =
environmentWorkerGroupRelationMapper.queryByWorkerGroupName(workerGroup.getName());
if (CollectionUtils.isNotEmpty(environmentWorkerGroupRelationList)) {
putMsg(result, Status.DELETE_WORKER_GROUP_BY_ID_FAIL_ENV, environmentWorkerGroupRelationList.size(),
workerGroup.getName());
return result;
}
workerGroupMapper.deleteById(id);
processInstanceMapper.updateProcessInstanceByWorkerGroupName(workerGroup.getName(), "");
putMsg(result, Status.SUCCESS);

View File

@ -26,6 +26,7 @@ import org.apache.dolphinscheduler.common.enums.UserType;
import org.apache.dolphinscheduler.dao.entity.ProcessInstance;
import org.apache.dolphinscheduler.dao.entity.User;
import org.apache.dolphinscheduler.dao.entity.WorkerGroup;
import org.apache.dolphinscheduler.dao.mapper.EnvironmentWorkerGroupRelationMapper;
import org.apache.dolphinscheduler.dao.mapper.ProcessInstanceMapper;
import org.apache.dolphinscheduler.dao.mapper.WorkerGroupMapper;
import org.apache.dolphinscheduler.service.registry.RegistryClient;
@ -38,6 +39,7 @@ import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@ -65,6 +67,8 @@ public class WorkerGroupServiceTest {
private String groupName = "groupName000001";
private User loginUSer;
@Mock
private EnvironmentWorkerGroupRelationMapper environmentWorkerGroupRelationMapper;
@Before
public void init(){