From 086e71644163a0bb44e4bd1c5f7f16abc383ab1a Mon Sep 17 00:00:00 2001 From: wenjun <861923274@qq.com> Date: Thu, 18 Mar 2021 22:49:12 +0800 Subject: [PATCH] [Improvement][Dao] SQL optimization - check exist #5069 (#5070) --- .../service/impl/AlertGroupServiceImpl.java | 5 ++- .../impl/AlertPluginInstanceServiceImpl.java | 5 +-- .../api/service/impl/QueueServiceImpl.java | 8 ++--- .../service/impl/ResourcesServiceImpl.java | 5 +-- .../api/service/impl/TenantServiceImpl.java | 7 ++-- .../impl/WorkFlowLineageServiceImpl.java | 3 +- .../api/service/AlertGroupServiceTest.java | 2 +- .../AlertPluginInstanceServiceTest.java | 2 +- .../api/service/QueueServiceTest.java | 31 ++++++++--------- .../api/service/ResourcesServiceTest.java | 6 ++-- .../api/service/TenantServiceTest.java | 6 ++-- .../common/utils/BooleanUtils.java | 33 +++++++++++++++++++ .../dolphinscheduler/dao/PluginDao.java | 3 +- .../dao/mapper/AlertGroupMapper.java | 7 ++++ .../dao/mapper/AlertPluginInstanceMapper.java | 7 ++++ .../dao/mapper/QueueMapper.java | 7 ++++ .../dao/mapper/ResourceMapper.java | 11 +++++++ .../dao/mapper/TenantMapper.java | 7 ++++ .../dao/mapper/UserMapper.java | 9 ++++- .../dao/mapper/AlertGroupMapper.xml | 7 ++++ .../dao/mapper/AlertPluginInstanceMapper.xml | 6 ++++ .../dao/mapper/QueueMapper.xml | 12 ++++++- .../dao/mapper/ResourceMapper.xml | 8 +++++ .../dao/mapper/TenantMapper.xml | 5 +++ .../dao/mapper/UserMapper.xml | 7 +++- .../dao/mapper/AlertGroupMapperTest.java | 9 +++++ .../mapper/AlertPluginInstanceMapperTest.java | 8 +++++ .../dao/mapper/QueueMapperTest.java | 25 ++++++++++---- .../dao/mapper/ResourceMapperTest.java | 10 ++++++ .../dao/mapper/TenantMapperTest.java | 12 ++++++- .../dao/mapper/UserMapperTest.java | 10 ++++++ 31 files changed, 234 insertions(+), 49 deletions(-) create mode 100644 dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java index 90df9a09a9..3831ad0892 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertGroupServiceImpl.java @@ -21,7 +21,7 @@ import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.AlertGroupService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.common.Constants; -import org.apache.dolphinscheduler.common.utils.CollectionUtils; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.dao.entity.AlertGroup; import org.apache.dolphinscheduler.dao.entity.User; @@ -207,7 +207,6 @@ public class AlertGroupServiceImpl extends BaseServiceImpl implements AlertGroup */ @Override public boolean existGroupName(String groupName) { - List alertGroup = alertGroupMapper.queryByGroupName(groupName); - return CollectionUtils.isNotEmpty(alertGroup); + return BooleanUtils.isTrue(alertGroupMapper.existGroupName(groupName)); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java index 95171edf06..ea4664e9a5 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/AlertPluginInstanceServiceImpl.java @@ -22,6 +22,7 @@ import org.apache.dolphinscheduler.api.service.AlertPluginInstanceService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.vo.AlertPluginInstanceVO; import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance; @@ -82,7 +83,7 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A Map result = new HashMap<>(); - if (CollectionUtils.isNotEmpty(alertPluginInstanceMapper.queryByInstanceName(alertPluginInstance.getInstanceName()))) { + if (BooleanUtils.isTrue(alertPluginInstanceMapper.existInstanceName(alertPluginInstance.getInstanceName()))) { putMsg(result, Status.PLUGIN_INSTANCE_ALREADY_EXIT); return result; } @@ -183,7 +184,7 @@ public class AlertPluginInstanceServiceImpl extends BaseServiceImpl implements A @Override public boolean checkExistPluginInstanceName(String pluginInstanceName) { - return CollectionUtils.isNotEmpty(alertPluginInstanceMapper.queryByInstanceName(pluginInstanceName)); + return BooleanUtils.isTrue(alertPluginInstanceMapper.existInstanceName(pluginInstanceName)); } @Override diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java index 002794dc56..8042a364e9 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/QueueServiceImpl.java @@ -22,7 +22,7 @@ import org.apache.dolphinscheduler.api.service.QueueService; import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; -import org.apache.dolphinscheduler.common.utils.CollectionUtils; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.common.utils.StringUtils; import org.apache.dolphinscheduler.dao.entity.Queue; import org.apache.dolphinscheduler.dao.entity.User; @@ -270,7 +270,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService { * @return true if the queue not exists, otherwise return false */ private boolean checkQueueExist(String queue) { - return CollectionUtils.isNotEmpty(queueMapper.queryAllQueueList(queue, null)); + return BooleanUtils.isTrue(queueMapper.existQueue(queue, null)); } /** @@ -281,7 +281,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService { * @return true if the queue name not exists, otherwise return false */ private boolean checkQueueNameExist(String queueName) { - return CollectionUtils.isNotEmpty(queueMapper.queryAllQueueList(null, queueName)); + return BooleanUtils.isTrue(queueMapper.existQueue(null, queueName)); } /** @@ -293,7 +293,7 @@ public class QueueServiceImpl extends BaseServiceImpl implements QueueService { * @return true if need to update user */ private boolean checkIfQueueIsInUsing (String oldQueue, String newQueue) { - return !oldQueue.equals(newQueue) && CollectionUtils.isNotEmpty(userMapper.queryUserListByQueue(oldQueue)); + return !oldQueue.equals(newQueue) && BooleanUtils.isTrue(userMapper.existUser(oldQueue)); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java index f61996223e..7337b332f9 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ResourcesServiceImpl.java @@ -34,6 +34,7 @@ import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.ProgramType; import org.apache.dolphinscheduler.common.enums.ResourceType; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.FileUtils; import org.apache.dolphinscheduler.common.utils.HadoopUtils; @@ -251,8 +252,8 @@ public class ResourcesServiceImpl extends BaseServiceImpl implements ResourcesSe * @return true if resource exists */ private boolean checkResourceExists(String fullName, int userId, int type) { - List resources = resourcesMapper.queryResourceList(fullName, userId, type); - return resources != null && !resources.isEmpty(); + Boolean existResource = resourcesMapper.existResource(fullName, userId, type); + return BooleanUtils.isTrue(existResource); } /** diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java index 9deef7d035..8a3a945d10 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TenantServiceImpl.java @@ -23,6 +23,7 @@ import org.apache.dolphinscheduler.api.utils.PageInfo; import org.apache.dolphinscheduler.api.utils.RegexUtils; import org.apache.dolphinscheduler.api.utils.Result; import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.HadoopUtils; import org.apache.dolphinscheduler.common.utils.PropertyUtils; @@ -95,7 +96,7 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService } if (checkTenantExists(tenantCode)) { - putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, tenantCode); + putMsg(result, Status.OS_TENANT_CODE_EXIST, tenantCode); return result; } @@ -344,7 +345,7 @@ public class TenantServiceImpl extends BaseServiceImpl implements TenantService * @return ture if the tenant code exists, otherwise return false */ private boolean checkTenantExists(String tenantCode) { - List tenants = tenantMapper.queryByTenantCode(tenantCode); - return CollectionUtils.isNotEmpty(tenants); + Boolean existTenant = tenantMapper.existTenant(tenantCode); + return BooleanUtils.isTrue(existTenant); } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java index 22c7622ab8..8cf16cdc7f 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkFlowLineageServiceImpl.java @@ -20,6 +20,7 @@ package org.apache.dolphinscheduler.api.service.impl; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.service.WorkFlowLineageService; import org.apache.dolphinscheduler.common.Constants; +import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.dao.entity.WorkFlowLineage; import org.apache.dolphinscheduler.dao.entity.WorkFlowRelation; import org.apache.dolphinscheduler.dao.mapper.WorkFlowLineageMapper; @@ -56,7 +57,7 @@ public class WorkFlowLineageServiceImpl extends BaseServiceImpl implements WorkF for (int id : ids) { sourceIds.addAll(ids); List workFlowRelationsTmp = workFlowLineageMapper.querySourceTarget(id); - if (workFlowRelationsTmp != null && !workFlowRelationsTmp.isEmpty()) { + if (CollectionUtils.isNotEmpty(workFlowRelationsTmp)) { Set idsTmp = new HashSet<>(); for (WorkFlowRelation workFlowRelation:workFlowRelationsTmp) { if (!sourceIds.contains(workFlowRelation.getTargetWorkFlowId())) { diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java index 85115bbc28..0f5843bca0 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertGroupServiceTest.java @@ -156,7 +156,7 @@ public class AlertGroupServiceTest { //group name not exist boolean result = alertGroupService.existGroupName(groupName); Assert.assertFalse(result); - Mockito.when(alertGroupMapper.queryByGroupName(groupName)).thenReturn(getList()); + Mockito.when(alertGroupMapper.existGroupName(groupName)).thenReturn(true); //group name exist result = alertGroupService.existGroupName(groupName); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java index ae3896218c..071d59bfe8 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/AlertPluginInstanceServiceTest.java @@ -157,7 +157,7 @@ public class AlertPluginInstanceServiceTest { @Test public void testCreate() { - Mockito.when(alertPluginInstanceMapper.queryByInstanceName("test")).thenReturn(alertPluginInstances); + Mockito.when(alertPluginInstanceMapper.existInstanceName("test")).thenReturn(true); Map result = alertPluginInstanceService.create(user, 1, "test", uiParams); Assert.assertEquals(Status.PLUGIN_INSTANCE_ALREADY_EXIT, result.get(Constants.STATUS)); Mockito.when(alertPluginInstanceMapper.insert(Mockito.any())).thenReturn(1); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java index 10c6d486e7..6d48da87b9 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/QueueServiceTest.java @@ -76,7 +76,7 @@ public class QueueServiceTest { } @Test - public void testQueryList(){ + public void testQueryList() { Mockito.when(queueMapper.selectList(null)).thenReturn(getQueueList()); Map result = queueService.queryList(getLoginUser()); @@ -86,7 +86,7 @@ public class QueueServiceTest { } @Test - public void testQueryListPage(){ + public void testQueryListPage() { IPage page = new Page<>(1,10); page.setTotal(1L); @@ -98,7 +98,7 @@ public class QueueServiceTest { Assert.assertTrue(CollectionUtils.isNotEmpty(pageInfo.getLists())); } @Test - public void testCreateQueue(){ + public void testCreateQueue() { // queue is null Map result = queueService.createQueue(getLoginUser(),null,queueName); @@ -114,13 +114,13 @@ public class QueueServiceTest { Assert.assertEquals(Status.SUCCESS,result.get(Constants.STATUS)); } + @Test - public void testUpdateQueue(){ + public void testUpdateQueue() { Mockito.when(queueMapper.selectById(1)).thenReturn(getQueue()); - Mockito.when(queueMapper.queryAllQueueList("test", null)).thenReturn(getQueueList()); - Mockito.when(queueMapper.queryAllQueueList(null, "test")).thenReturn(getQueueList()); - Mockito.when(userMapper.queryUserListByQueue(queueName)).thenReturn(getUserList()); + Mockito.when(queueMapper.existQueue("test", null)).thenReturn(true); + Mockito.when(queueMapper.existQueue(null, "test")).thenReturn(true); // not exist Map result = queueService.updateQueue(getLoginUser(),0,"queue",queueName); @@ -144,11 +144,12 @@ public class QueueServiceTest { Assert.assertEquals(Status.SUCCESS.getCode(),((Status)result.get(Constants.STATUS)).getCode()); } + @Test - public void testVerifyQueue(){ + public void testVerifyQueue() { - Mockito.when(queueMapper.queryAllQueueList(queueName, null)).thenReturn(getQueueList()); - Mockito.when(queueMapper.queryAllQueueList(null, queueName)).thenReturn(getQueueList()); + Mockito.when(queueMapper.existQueue(queueName, null)).thenReturn(true); + Mockito.when(queueMapper.existQueue(null, queueName)).thenReturn(true); //queue null Result result = queueService.verifyQueue(null,queueName); @@ -175,13 +176,13 @@ public class QueueServiceTest { logger.info(result.toString()); Assert.assertEquals(result.getCode().intValue(), Status.SUCCESS.getCode()); - } + /** * create admin user * @return */ - private User getLoginUser(){ + private User getLoginUser() { User loginUser = new User(); loginUser.setUserType(UserType.ADMIN_USER); @@ -189,7 +190,7 @@ public class QueueServiceTest { return loginUser; } - private List getUserList(){ + private List getUserList() { List list = new ArrayList<>(); list.add(getLoginUser()); return list; @@ -200,7 +201,7 @@ public class QueueServiceTest { * get queue * @return */ - private Queue getQueue(){ + private Queue getQueue() { Queue queue = new Queue(); queue.setId(1); queue.setQueue(queueName); @@ -208,7 +209,7 @@ public class QueueServiceTest { return queue; } - private List getQueueList(){ + private List getQueueList() { List queueList = new ArrayList<>(); queueList.add(getQueue()); return queueList; diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java index 8478b1b2bc..89bd3df243 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/ResourcesServiceTest.java @@ -169,7 +169,7 @@ public class ResourcesServiceTest { Assert.assertEquals(Status.PARENT_RESOURCE_NOT_EXIST.getMsg(), result.getMsg()); //RESOURCE_EXIST PowerMockito.when(PropertyUtils.getResUploadStartupState()).thenReturn(true); - Mockito.when(resourcesMapper.queryResourceList("/directoryTest", 0, 0)).thenReturn(getResourceList()); + Mockito.when(resourcesMapper.existResource("/directoryTest", 0, 0)).thenReturn(true); result = resourcesService.createDirectory(user, "directoryTest", "directory test", ResourceType.FILE, -1, "/"); logger.info(result.toString()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); @@ -227,7 +227,7 @@ public class ResourcesServiceTest { Assert.assertEquals(Status.SUCCESS.getMsg(), result.getMsg()); //RESOURCE_EXIST - Mockito.when(resourcesMapper.queryResourceList("/ResourcesServiceTest1.jar", 0, 0)).thenReturn(getResourceList()); + Mockito.when(resourcesMapper.existResource("/ResourcesServiceTest1.jar", 0, 0)).thenReturn(true); result = resourcesService.updateResource(user, 1, "ResourcesServiceTest1.jar", "ResourcesServiceTest", ResourceType.FILE, null); logger.info(result.toString()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); @@ -344,7 +344,7 @@ public class ResourcesServiceTest { User user = new User(); user.setId(1); - Mockito.when(resourcesMapper.queryResourceList("/ResourcesServiceTest.jar", 0, 0)).thenReturn(getResourceList()); + Mockito.when(resourcesMapper.existResource("/ResourcesServiceTest.jar", 0, 0)).thenReturn(true); Result result = resourcesService.verifyResourceName("/ResourcesServiceTest.jar", ResourceType.FILE, user); logger.info(result.toString()); Assert.assertEquals(Status.RESOURCE_EXIST.getMsg(), result.getMsg()); diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java index bdd38df421..ee85d260cc 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/TenantServiceTest.java @@ -79,7 +79,7 @@ public class TenantServiceTest { public void testCreateTenant() { User loginUser = getLoginUser(); - Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getList()); + Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); try { //check tenantCode Map result = @@ -90,7 +90,7 @@ public class TenantServiceTest { //check exist result = tenantService.createTenant(loginUser, tenantCode, 1, "TenantServiceTest"); logger.info(result.toString()); - Assert.assertEquals(Status.REQUEST_PARAMS_NOT_VALID_ERROR, result.get(Constants.STATUS)); + Assert.assertEquals(Status.OS_TENANT_CODE_EXIST, result.get(Constants.STATUS)); // success result = tenantService.createTenant(loginUser, "test", 1, "TenantServiceTest"); @@ -186,7 +186,7 @@ public class TenantServiceTest { @Test public void testVerifyTenantCode() { - Mockito.when(tenantMapper.queryByTenantCode(tenantCode)).thenReturn(getList()); + Mockito.when(tenantMapper.existTenant(tenantCode)).thenReturn(true); // tenantCode not exist Result result = tenantService.verifyTenantCode("s00000000000l887888885554444sfjdskfjslakslkdf"); logger.info(result.toString()); diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java new file mode 100644 index 0000000000..5873556494 --- /dev/null +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/BooleanUtils.java @@ -0,0 +1,33 @@ +/* + * 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.common.utils; + +public class BooleanUtils { + + public static boolean isTrue(Boolean bool) { + if (bool == null) { + return false; + } else { + return bool; + } + } + + public static boolean isNotTrue(Boolean bool) { + return !isTrue(bool); + } +} diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java index 93df36aadc..646307496b 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/PluginDao.java @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.dao; import static java.util.Objects.requireNonNull; +import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.dao.datasource.ConnectionFactory; import org.apache.dolphinscheduler.dao.entity.PluginDefine; import org.apache.dolphinscheduler.dao.mapper.PluginDefineMapper; @@ -64,7 +65,7 @@ public class PluginDao extends AbstractBaseDao { requireNonNull(pluginDefine.getPluginType(), "pluginType is null"); List pluginDefineList = pluginDefineMapper.queryByNameAndType(pluginDefine.getPluginName(), pluginDefine.getPluginType()); - if (pluginDefineList == null || pluginDefineList.size() == 0) { + if (CollectionUtils.isEmpty(pluginDefineList)) { return pluginDefineMapper.insert(pluginDefine); } PluginDefine currPluginDefine = pluginDefineList.get(0); diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java index ebfa5b42ec..8026b9a89f 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.java @@ -48,6 +48,13 @@ public interface AlertGroupMapper extends BaseMapper { */ List queryByGroupName(@Param("groupName") String groupName); + /** + * Judge whether the alert group exist + * @param groupName groupName + * @return if exist return true else return null + */ + Boolean existGroupName(@Param("groupName") String groupName); + /** * query by userId * @param userId userId diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java index 618a8774c7..12da99c3a0 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapper.java @@ -44,4 +44,11 @@ public interface AlertPluginInstanceMapper extends BaseMapper queryByInstanceName(@Param("instanceName")String instanceName); + /** + * + * @param instanceName instanceName + * @return if exist return true else return null + */ + Boolean existInstanceName(@Param("instanceName") String instanceName); + } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java index 3be65b86d9..027bfd2b52 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/QueueMapper.java @@ -46,4 +46,11 @@ public interface QueueMapper extends BaseMapper { List queryAllQueueList(@Param("queue") String queue, @Param("queueName") String queueName); + /** + * check the target queue exist + * @param queue queue + * @param queueName queueName + * @return true if exist else return null + */ + Boolean existQueue(@Param("queue") String queue, @Param("queueName") String queueName); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java index f58cc7d496..96b203963b 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.java @@ -144,4 +144,15 @@ public interface ResourceMapper extends BaseMapper { * @return update num */ int batchUpdateResource(@Param("resourceList") List resourceList); + + /** + * check resource exist + * @param fullName full name + * @param userId userId + * @param type type + * @return true if exist else return null + */ + Boolean existResource(@Param("fullName") String fullName, + @Param("userId") int userId, + @Param("type") int type); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java index e201805e79..45c824ce89 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/TenantMapper.java @@ -50,4 +50,11 @@ public interface TenantMapper extends BaseMapper { */ IPage queryTenantPaging(IPage page, @Param("searchVal") String searchVal); + + /** + * check tenant exist + * @param tenantCode tenantCode + * @return true if exist else return null + */ + Boolean existTenant(@Param("tenantCode") String tenantCode); } diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java index 1468f10b90..3e766389da 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java @@ -100,7 +100,14 @@ public interface UserMapper extends BaseMapper { * @param queueName queue name * @return user list */ - List queryUserListByQueue(@Param("queueName") String queueName); + List queryUserListByQueue(@Param("queue") String queueName); + + /** + * check the user exist + * @param queueName queue name + * @return true if exist else return null + */ + Boolean existUser(@Param("queue") String queue); /** * update user with old queue diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml index 90fd8a72a7..8a7d3a57e8 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapper.xml @@ -38,6 +38,13 @@ from t_ds_alertgroup where group_name=#{groupName} + + + + + \ No newline at end of file diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml index 564dd0354f..0d75c6e364 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/QueueMapper.xml @@ -43,5 +43,15 @@ and queue_name =#{queueName} - + diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml index 880e3260a5..c67fd7291c 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/ResourceMapper.xml @@ -166,4 +166,12 @@ #{i} + + diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml index db3a282846..5c1575e706 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/TenantMapper.xml @@ -54,4 +54,9 @@ order by t.update_time desc + diff --git a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml index f4263eb54b..32768a8639 100644 --- a/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml +++ b/dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml @@ -103,7 +103,12 @@ select from t_ds_user - where queue = #{queueName} + where queue = #{queue} + + update t_ds_user diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java index db42a2f57b..9db5d2b9f2 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertGroupMapperTest.java @@ -31,6 +31,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -161,6 +162,14 @@ public class AlertGroupMapperTest { compareAlertGroups(alertGroupMap, alertGroupList); } + @Test + public void testExistGroupName() { + String groupName = "testGroup"; + createAlertGroups(1, groupName); + + Assert.assertTrue(alertGroupMapper.existGroupName(groupName)); + } + /** * test query all group list */ diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java index 6ebcde39f9..1688e0ec41 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/AlertPluginInstanceMapperTest.java @@ -67,6 +67,14 @@ public class AlertPluginInstanceMapperTest { AlertGroup alertGroup = testAlertGroupList.get(0); } + @Test + public void testExistInstanceName() { + String instanceName = "test_instance"; + Assert.assertNull(alertPluginInstanceMapper.existInstanceName(instanceName)); + createAlertPluginInstance(); + Assert.assertTrue(alertPluginInstanceMapper.existInstanceName(instanceName)); + } + /** * insert * diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java index a1e1fdaf7a..8d44f8878e 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/QueueMapperTest.java @@ -14,12 +14,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.dolphinscheduler.dao.mapper; +package org.apache.dolphinscheduler.dao.mapper; +import org.apache.dolphinscheduler.common.utils.BooleanUtils; import org.apache.dolphinscheduler.dao.entity.Queue; + +import java.util.Date; +import java.util.List; + import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,8 +35,6 @@ import org.springframework.test.annotation.Rollback; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.transaction.annotation.Transactional; -import java.util.Date; -import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @@ -47,7 +51,7 @@ public class QueueMapperTest { * insert * @return Queue */ - private Queue insertOne(){ + private Queue insertOne() { //insertOne Queue queue = new Queue(); queue.setQueueName("queue"); @@ -62,7 +66,7 @@ public class QueueMapperTest { * test update */ @Test - public void testUpdate(){ + public void testUpdate() { //insertOne Queue queue = insertOne(); queue.setCreateTime(new Date()); @@ -75,7 +79,7 @@ public class QueueMapperTest { * test delete */ @Test - public void testDelete(){ + public void testDelete() { Queue queue = insertOne(); int delete = queueMapper.deleteById(queue.getId()); Assert.assertEquals(1, delete); @@ -123,4 +127,13 @@ public class QueueMapperTest { queues = queueMapper.queryAllQueueList(null, queue.getQueueName()); Assert.assertNotEquals(queues.size(), 0); } + + @Test + public void existQueue() { + Assert.assertNull(queueMapper.existQueue("queue", null)); + Assert.assertNull(queueMapper.existQueue(null, "queue")); + Queue queue = insertOne(); + Assert.assertTrue(BooleanUtils.isTrue(queueMapper.existQueue(queue.getQueue(), null))); + Assert.assertTrue(BooleanUtils.isTrue(queueMapper.existQueue(null, queue.getQueueName()))); + } } \ No newline at end of file diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java index d36a26ffd3..f06e4fcd9b 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/ResourceMapperTest.java @@ -412,4 +412,14 @@ public class ResourceMapperTest { Assert.fail("batch update resource data error"); } } + + @Test + public void existResourceTest() { + String fullName = "/ut-resource"; + int userId = 111; + int type = ResourceType.FILE.getCode(); + Assert.assertNull(resourceMapper.existResource(fullName, userId, type)); + insertOne(); + Assert.assertTrue(resourceMapper.existResource(fullName, userId, type)); + } } \ No newline at end of file diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java index a1860709bc..b8a97e15c6 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TenantMapperTest.java @@ -54,6 +54,7 @@ public class TenantMapperTest { Tenant tenant = new Tenant(); tenant.setCreateTime(new Date()); tenant.setUpdateTime(new Date()); + tenant.setTenantCode("test_code"); tenantMapper.insert(tenant); return tenant; } @@ -122,6 +123,8 @@ public class TenantMapperTest { Tenant tenant = insertOne(); tenant.setTenantCode("ut code"); tenantMapper.updateById(tenant); + List tenantList = tenantMapper.queryByTenantCode("ut code"); + Assert.assertEquals(1, tenantList.size()); } /** @@ -139,11 +142,18 @@ public class TenantMapperTest { tenant.setTenantCode("ut code"); tenant.setQueueId(queue.getId()); tenantMapper.updateById(tenant); - Page page = new Page(1,3); + Page page = new Page(1, 3); //tenant.getTenantCode() used instead of tenant.getTenantName() IPage tenantIPage = tenantMapper.queryTenantPaging(page, tenant.getTenantCode()); Assert.assertNotEquals(tenantIPage.getTotal(), 0); } + + public void testExistTenant() { + String tenantCode = "test_code"; + Assert.assertNull(tenantMapper.existTenant(tenantCode)); + insertOne(); + Assert.assertTrue(tenantMapper.existTenant(tenantCode)); + } } \ No newline at end of file diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java index 7f7ceb480a..e573ad77ee 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java @@ -74,6 +74,8 @@ public class UserMapperTest { user.setCreateTime(new Date()); user.setTenantId(1); user.setUpdateTime(new Date()); + user.setQueueName("test_queue"); + user.setQueue("queue"); userMapper.insert(user); return user; } @@ -312,4 +314,12 @@ public class UserMapperTest { Assert.assertEquals(userToken, user); } + + @Test + public void testExistUser() { + String queueName = "queue"; + Assert.assertNull(userMapper.existUser(queueName)); + insertOne(); + Assert.assertTrue(userMapper.existUser(queueName)); + } }