Browse Source

[Improvement][Api] Optimize query processInstanceList #5134 (#5135)

* Batch query user rather than several query
pull/3/MERGE
wenjun 4 years ago committed by GitHub
parent
commit
5d264c9f20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java
  2. 4
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java
  3. 8
      dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java
  4. 13
      dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java
  5. 34
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CollectionUtils.java
  6. 16
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/CollectionUtilsTest.java
  7. 8
      dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java
  8. 8
      dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml
  9. 11
      dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java

8
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/UsersService.java

@ -70,6 +70,14 @@ public interface UsersService {
*/
User queryUser(int id);
/**
* query user by ids
*
* @param ids id list
* @return user list
*/
List<User> queryUser(List<Integer> ids);
/**
* query user
*

4
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java

@ -252,10 +252,12 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce
project.getId(), processDefineId, searchVal, executorId, statusArray, host, start, end);
List<ProcessInstance> processInstances = processInstanceList.getRecords();
List<Integer> userIds = CollectionUtils.transformToList(processInstances, ProcessInstance::getExecutorId);
Map<Integer, User> idToUserMap = CollectionUtils.collectionToMap(usersService.queryUser(userIds), User::getId);
for (ProcessInstance processInstance : processInstances) {
processInstance.setDuration(DateUtils.format2Duration(processInstance.getStartTime(), processInstance.getEndTime()));
User executor = usersService.queryUser(processInstance.getExecutorId());
User executor = idToUserMap.get(processInstance.getExecutorId());
if (null != executor) {
processInstance.setExecutorName(executor.getUserName());
}

8
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/UsersServiceImpl.java

@ -245,6 +245,14 @@ public class UsersServiceImpl extends BaseServiceImpl implements UsersService {
return userMapper.selectById(id);
}
@Override
public List<User> queryUser(List<Integer> ids) {
if (CollectionUtils.isEmpty(ids)) {
return new ArrayList<>();
}
return userMapper.selectByIds(ids);
}
/**
* query user
*

13
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/UsersServiceTest.java

@ -180,6 +180,19 @@ public class UsersServiceTest {
Assert.assertTrue(queryUser != null);
}
@Test
public void testSelectByIds() {
List<Integer> ids = new ArrayList<>();
List<User> users = usersService.queryUser(ids);
Assert.assertTrue(users.isEmpty());
ids.add(1);
List<User> userList = new ArrayList<>();
userList.add(new User());
when(userMapper.selectByIds(ids)).thenReturn(userList);
List<User> userList1 = usersService.queryUser(ids);
Assert.assertFalse(userList1.isEmpty());
}
@Test
public void testGetUserIdByName() {
User user = new User();

34
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/CollectionUtils.java

@ -27,6 +27,8 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Provides utility methods and decorators for {@link Collection} instances.
@ -126,6 +128,38 @@ public class CollectionUtils {
return map;
}
/**
* Transform item in collection
*
* @param collection origin collection
* @param transformFunc transform function
* @param <R> origin item type
* @param <T> target type
* @return transform list
*/
public static <R, T> List<T> transformToList(Collection<R> collection, Function<R, T> transformFunc) {
if (isEmpty(collection)) {
return new ArrayList<>();
}
return collection.stream().map(transformFunc).collect(Collectors.toList());
}
/**
* Collect collection to map
*
* @param collection origin collection
* @param keyTransformFunction key transform function
* @param <K> target k type
* @param <V> value
* @return map
*/
public static <K, V> Map<K, V> collectionToMap(Collection<V> collection, Function<V, K> keyTransformFunction) {
if (isEmpty(collection)) {
return new HashMap<>();
}
return collection.stream().collect(Collectors.toMap(keyTransformFunction, Function.identity()));
}
/**
* Helper class to easily access cardinality properties of two collections.
*

16
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/CollectionUtilsTest.java

@ -142,4 +142,20 @@ public class CollectionUtilsTest {
Assert.assertEquals(2, cardinalityMap.get(2).intValue());
Assert.assertEquals(3, cardinalityMap.get(3).intValue());
}
@Test
public void transformToList() {
List<String> stringList = new ArrayList<>();
stringList.add("1");
List<Integer> integers = CollectionUtils.transformToList(stringList, String::length);
Assert.assertFalse(integers.isEmpty());
}
@Test
public void collectionToMap() {
List<String> stringList = new ArrayList<>();
stringList.add("1");
Map<Integer, String> lengthStringMap = CollectionUtils.collectionToMap(stringList, String::length);
Assert.assertFalse(lengthStringMap.isEmpty());
}
}

8
dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/mapper/UserMapper.java

@ -116,4 +116,12 @@ public interface UserMapper extends BaseMapper<User> {
* @return update rows
*/
Integer updateUserQueue(@Param("oldQueue") String oldQueue, @Param("newQueue") String newQueue);
/**
* query user by ids
*
* @param ids id list
* @return user list
*/
List<User> selectByIds(@Param("ids") List<Integer> ids);
}

8
dolphinscheduler-dao/src/main/resources/org/apache/dolphinscheduler/dao/mapper/UserMapper.xml

@ -115,4 +115,12 @@
set queue = #{newQueue}
where queue = #{oldQueue}
</update>
<select id="selectByIds" resultType="org.apache.dolphinscheduler.dao.entity.User">
select *
from t_ds_user
where id in
<foreach item="id" index="index" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>
</mapper>

11
dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/UserMapperTest.java

@ -25,6 +25,7 @@ import org.apache.dolphinscheduler.dao.entity.Queue;
import org.apache.dolphinscheduler.dao.entity.Tenant;
import org.apache.dolphinscheduler.dao.entity.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -315,6 +316,16 @@ public class UserMapperTest {
}
@Test
public void selectByIds() {
//insertOne
User user = insertOne();
List<Integer> userIds = new ArrayList<>();
userIds.add(user.getId());
List<User> users = userMapper.selectByIds(userIds);
Assert.assertFalse(users.isEmpty());
}
@Test
public void testExistUser() {
String queueName = "queue";

Loading…
Cancel
Save