refactor: 2.5.0

1. 优化获取重试分区的问题
This commit is contained in:
byteblogs168 2023-12-11 14:56:46 +08:00
parent 6c8170b880
commit bc60963f7c
8 changed files with 177 additions and 31 deletions

View File

@ -77,6 +77,12 @@
ifnull(SUM(success_num + fail_num + stop_num + cancel_num), 0) AS total
FROM job_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != null and groupName != '' ">
AND group_name = #{groupName}
</if>
@ -94,6 +100,12 @@
ifnull(sum(success_num + fail_num + stop_num + cancel_num), 0) AS totalNum
FROM job_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
</select>
<select id="dashboardRank"
@ -102,6 +114,12 @@
CONCAT(group_name, '/', (SELECT job_name FROM job WHERE id=job_id)) `name`,
SUM(fail_num) AS total FROM job_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != '' and groupName != null">
AND group_name = #{groupName}
</if>
@ -120,6 +138,12 @@
count(*) AS total
FROM job
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY namespace_id, group_name
</select>
</mapper>

View File

@ -48,6 +48,12 @@
ifnull(sum(running_num + finish_num + max_count_num + suspend_num), 0) AS totalNum
FROM retry_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
</select>
<select id="retryTaskBarList"
@ -69,6 +75,12 @@
LEFT JOIN (SELECT DATE (trigger_at) AS triggerAt,
SUM(running_num + finish_num + max_count_num + suspend_num) AS taskTotal FROM retry_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY triggerAt
) b
ON tmp.date = b.triggerAt
@ -102,6 +114,12 @@
ifnull(SUM(finish_num + running_num + max_count_num + suspend_num), 0) AS total
FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != null and groupName != '' ">
AND group_name = #{groupName}
</if>
@ -116,6 +134,12 @@
CONCAT(group_name, '/', scene_name) `name`,
SUM(running_num + finish_num + max_count_num + suspend_num) AS total FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != '' and groupName != null">
AND group_name = #{groupName}
</if>
@ -134,6 +158,12 @@
count(*) AS total
FROM scene_config
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY namespace_id, group_name
</select>
</mapper>

View File

@ -114,17 +114,17 @@
ifnull(SUM(finish_num + running_num + max_count_num + suspend_num), 0) AS total
FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != null and groupName != '' ">
AND group_name = #{groupName}
</if>
AND namespace_id = #{namespaceId} AND trigger_at BETWEEN #{from} AND #{to}
</where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY createDt
</select>
@ -134,18 +134,18 @@
CONCAT(group_name, '/', scene_name) `name`,
SUM(running_num + finish_num + max_count_num + suspend_num) AS total FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != '' and groupName != null">
AND group_name = #{groupName}
</if>
AND namespace_id = #{namespaceId}
AND trigger_at >= #{startTime} AND trigger_at &lt;= #{endTime}
</where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY namespace_id, group_name, scene_name
HAVING total > 0
ORDER BY total DESC LIMIT 10

View File

@ -77,6 +77,12 @@
COALESCE(SUM(success_num + fail_num + stop_num + cancel_num), 0) AS total
FROM job_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != null and groupName != '' ">
AND group_name = #{groupName}
</if>
@ -94,6 +100,12 @@
COALESCE(sum(success_num + fail_num + stop_num + cancel_num), 0) AS totalNum
FROM job_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
</select>
<select id="dashboardRank"
@ -102,6 +114,12 @@
CONCAT(group_name, '/', (SELECT job_name FROM job WHERE id=job_id)) name,
SUM(fail_num) AS total FROM job_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != '' and groupName != null">
AND group_name = #{groupName}
</if>
@ -120,6 +138,12 @@
count(*) AS total
FROM job
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY namespace_id, group_name
</select>
</mapper>

View File

@ -48,6 +48,12 @@
COALESCE(sum(running_num + finish_num + max_count_num + suspend_num), 0) AS totalNum
FROM retry_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
</select>
<select id="retryTaskBarList"
@ -69,6 +75,12 @@
LEFT JOIN (SELECT DATE (trigger_at) AS triggerAt,
SUM(running_num + finish_num + max_count_num + suspend_num) AS taskTotal FROM retry_summary
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY triggerAt
) b
ON tmp.date = b.triggerAt
@ -102,6 +114,12 @@
COALESCE(SUM(finish_num + running_num + max_count_num + suspend_num), 0) AS total
FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != null and groupName != '' ">
AND group_name = #{groupName}
</if>
@ -116,6 +134,12 @@
CONCAT(group_name, '/', scene_name) name,
SUM(running_num + finish_num + max_count_num + suspend_num) AS total FROM retry_summary
<where>
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
<if test="groupName != '' and groupName != null">
AND group_name = #{groupName}
</if>
@ -134,6 +158,12 @@
count(*) AS total
FROM scene_config
WHERE namespace_id = #{namespaceId}
<if test="groupNames != null and groupNames.size > 0">
AND group_name IN
<foreach collection="groupNames" item="groupName" open="(" separator="," close=")">
#{groupName}
</foreach>
</if>
GROUP BY namespace_id, group_name
</select>
</mapper>

View File

@ -12,11 +12,19 @@ import com.aizuda.easy.retry.server.web.annotation.LoginRequired;
import com.aizuda.easy.retry.server.web.annotation.RoleEnum;
import com.aizuda.easy.retry.server.web.service.GroupConfigService;
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
import com.google.common.collect.Lists;
import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.NotNull;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@ -33,10 +41,6 @@ public class GroupConfigController {
@Autowired
private GroupConfigService groupConfigService;
@Autowired
private SystemProperties systemProperties;
@Autowired
private JdbcTemplate jdbcTemplate;
@LoginRequired(role = RoleEnum.ADMIN)
@PostMapping("")
@ -91,20 +95,7 @@ public class GroupConfigController {
@LoginRequired(role = RoleEnum.ADMIN)
@GetMapping("/partition-table/list")
public List<Integer> getTablePartitionList() {
// https://gitee.com/aizuda/easy-retry/issues/I8DAMH
String sql;
if (systemProperties.getDbType().getDb().equals(DbTypeEnum.POSTGRES.getDb())) {
sql = "SELECT table_name\n"
+ "FROM information_schema.tables\n"
+ "WHERE table_name LIKE 'retry_task_%' AND table_schema = 'public'";
} else {
sql = "SHOW TABLES LIKE 'retry_task_%'";
}
List<String> tableList = jdbcTemplate.queryForList(sql, String.class);
return tableList.stream().map(ReUtil::getFirstNumber).filter(i ->
!Objects.isNull(i) && i <= systemProperties.getTotalPartition()).distinct().collect(Collectors.toList());
return groupConfigService.getTablePartitionList();
}
}

View File

@ -29,4 +29,6 @@ public interface GroupConfigService {
List<String> getAllGroupNameList();
List<String> getOnlinePods(String groupName);
List<Integer> getTablePartitionList();
}

View File

@ -2,6 +2,7 @@ package com.aizuda.easy.retry.server.web.service.impl;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.HashUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.aizuda.easy.retry.server.common.config.SystemProperties;
import com.aizuda.easy.retry.server.common.enums.IdGeneratorMode;
@ -26,13 +27,20 @@ import com.aizuda.easy.retry.template.datasource.persistence.po.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.NotNull;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ -59,9 +67,10 @@ public class GroupConfigServiceImpl implements GroupConfigService {
@Autowired
@Lazy
private ConfigVersionSyncHandler configVersionSyncHandler;
@Autowired
private SystemProperties systemProperties;
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
@Transactional
@ -321,4 +330,40 @@ public class GroupConfigServiceImpl implements GroupConfigService {
.collect(Collectors.toList());
}
@Override
public List<Integer> getTablePartitionList() {
DataSource dataSource = jdbcTemplate.getDataSource();
Connection connection = null;
try {
connection = dataSource.getConnection();
String catalog = connection.getCatalog();
String schema = connection.getSchema();
// https://gitee.com/aizuda/easy-retry/issues/I8DAMH
String sql = MessageFormatter.arrayFormat("SELECT table_name\n"
+ "FROM information_schema.tables\n"
+ "WHERE table_name LIKE 'retry_task_%' AND table_schema = '{}' and table_catalog = '{}'", new Object[]{schema, catalog}).getMessage();
List<String> tableList = jdbcTemplate.queryForList(sql, String.class);
return tableList.stream().map(ReUtil::getFirstNumber).filter(i ->
!Objects.isNull(i) && i <= systemProperties.getTotalPartition()).distinct()
.collect(Collectors.toList());
} catch (SQLException ignored) {
} finally {
if (Objects.nonNull(connection)) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
// 兜底
List<Integer> tableList = Lists.newArrayList();
for (int i = 0; i < systemProperties.getTotalPartition(); i++) {
tableList.add(i);
}
return tableList;
}
}