feat(sj_1.0.0): 完成组导入导出

This commit is contained in:
opensnail 2024-05-27 22:51:34 +08:00
parent 50483b5c2a
commit 285ead5518
4 changed files with 79 additions and 66 deletions

View File

@ -102,7 +102,7 @@ public class GroupConfigController {
}
@PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@LoginRequired
@LoginRequired(role = RoleEnum.ADMIN)
public void importScene(@RequestPart("file") MultipartFile file) throws IOException {
if (file.isEmpty()) {
throw new SnailJobCommonException("Please select a file to upload");
@ -119,6 +119,8 @@ public class GroupConfigController {
List<GroupConfigRequestVO> requestList = JsonUtil.parseList(JsonUtil.toJsonString(node),
GroupConfigRequestVO.class);
Assert.notEmpty(requestList, () -> new SnailJobServerException("导入数据不能为空"));
// 校验参数是否合法
for (final GroupConfigRequestVO groupConfigRequestVO : requestList) {
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();
@ -133,10 +135,9 @@ public class GroupConfigController {
}
@PostMapping("/export")
@LoginRequired
@LoginRequired(role = RoleEnum.ADMIN)
@OriginalControllerReturnValue
public ResponseEntity<String> exportGroup(@RequestBody Set<Long> groupIds) {
Assert.notEmpty(groupIds, () -> new SnailJobServerException("参数错误"));
String configs = groupConfigService.exportGroup(groupIds);
HttpHeaders headers = new HttpHeaders();

View File

@ -1,9 +1,11 @@
package com.aizuda.snailjob.server.web.controller;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import com.aizuda.snailjob.common.core.annotation.OriginalControllerReturnValue;
import com.aizuda.snailjob.common.core.exception.SnailJobCommonException;
import com.aizuda.snailjob.common.core.util.JsonUtil;
import com.aizuda.snailjob.server.common.exception.SnailJobServerException;
import com.aizuda.snailjob.server.common.util.DateUtils;
import com.aizuda.snailjob.server.web.annotation.LoginRequired;
import com.aizuda.snailjob.server.web.model.base.PageResult;
@ -97,6 +99,8 @@ public class SceneConfigController {
List<SceneConfigRequestVO> requestList = JsonUtil.parseList(JsonUtil.toJsonString(node),
SceneConfigRequestVO.class);
Assert.notEmpty(requestList, () -> new SnailJobServerException("导入数据不能为空"));
// 校验参数是否合法
for (final SceneConfigRequestVO sceneConfigRequestVO : requestList) {
ValidatorFactory vf = Validation.buildDefaultValidatorFactory();

View File

@ -26,6 +26,7 @@ public class GroupConfigRequestVO {
/**
* 令牌
*/
@NotBlank(message = "令牌不能为空")
private String token;
/**
@ -36,17 +37,20 @@ public class GroupConfigRequestVO {
/**
* 分区
*/
@NotNull(message = "分区不能为空")
private Integer groupPartition;
/**
* 唯一id生成模式
* {@link IdGeneratorModeEnum}
*/
@NotNull(message = "id生成模式不能为空")
private Integer idGeneratorMode;
/**
* 是否初始化场景
*/
@NotNull(message = "初始化场不能为空")
private Integer initScene;
}

View File

@ -77,9 +77,9 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
Assert.isTrue(groupConfigAccess.count(new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupConfigRequestVO.getGroupName())) == 0,
() -> new SnailJobServerException("GroupName已经存在 {}", groupConfigRequestVO.getGroupName()));
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupConfigRequestVO.getGroupName())) == 0,
() -> new SnailJobServerException("GroupName已经存在 {}", groupConfigRequestVO.getGroupName()));
// 保存组配置
Boolean isSuccess = doSaveGroupConfig(namespaceId, groupConfigRequestVO);
@ -103,8 +103,8 @@ public class GroupConfigServiceImpl implements GroupConfigService {
sequenceAlloc.setStep(systemProperties.getStep());
sequenceAlloc.setUpdateDt(LocalDateTime.now());
Assert.isTrue(1 == sequenceAllocMapper.insert(sequenceAlloc),
() -> new SnailJobServerException("failed to save sequence generation rule configuration [{}].",
groupConfigRequestVO.getGroupName()));
() -> new SnailJobServerException("failed to save sequence generation rule configuration [{}].",
groupConfigRequestVO.getGroupName()));
}
@Override
@ -121,9 +121,9 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
long count = groupConfigAccess.count(
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupName));
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupName));
if (count <= 0) {
return false;
}
@ -134,19 +134,19 @@ public class GroupConfigServiceImpl implements GroupConfigService {
groupConfig.setVersion(1);
groupConfig.setToken(null);
Assert.isTrue(tablePartitionList.contains(groupConfigRequestVO.getGroupPartition()),
() -> new SnailJobServerException("分区不存在. [{}]", tablePartitionList));
() -> new SnailJobServerException("分区不存在. [{}]", tablePartitionList));
Assert.isTrue(groupConfigRequestVO.getGroupPartition() >= 0,
() -> new SnailJobServerException("分区不能是负数."));
() -> new SnailJobServerException("分区不能是负数."));
// 校验retry_task_x和retry_dead_letter_x是否存在
checkGroupPartition(groupConfig, namespaceId);
Assert.isTrue(1 == groupConfigAccess.update(groupConfig,
new LambdaUpdateWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupName)),
() -> new SnailJobServerException("exception occurred while adding group. groupConfigVO[{}]",
groupConfigRequestVO));
new LambdaUpdateWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.eq(GroupConfig::getGroupName, groupName)),
() -> new SnailJobServerException("exception occurred while adding group. groupConfigVO[{}]",
groupConfigRequestVO));
// 同步版本 版本为0代表需要同步到客户端
boolean add = configVersionSyncHandler.addSyncTask(groupName, namespaceId, 0);
@ -163,9 +163,9 @@ public class GroupConfigServiceImpl implements GroupConfigService {
groupConfig.setGroupStatus(status);
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
return groupConfigAccess.update(groupConfig,
new LambdaUpdateWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(GroupConfig::getGroupName, groupName)) == 1;
new LambdaUpdateWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(GroupConfig::getGroupName, groupName)) == 1;
}
@Override
@ -176,30 +176,30 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
PageDTO<GroupConfig> groupConfigPageDTO = groupConfigAccess.listPage(
new PageDTO<>(queryVO.getPage(), queryVO.getSize()),
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.in(userSessionVO.isUser(), GroupConfig::getGroupName, userSessionVO.getGroupNames())
.likeRight(StrUtil.isNotBlank(queryVO.getGroupName()), GroupConfig::getGroupName,
StrUtil.trim(queryVO.getGroupName()))
.orderByDesc(GroupConfig::getId));
new PageDTO<>(queryVO.getPage(), queryVO.getSize()),
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.in(userSessionVO.isUser(), GroupConfig::getGroupName, userSessionVO.getGroupNames())
.likeRight(StrUtil.isNotBlank(queryVO.getGroupName()), GroupConfig::getGroupName,
StrUtil.trim(queryVO.getGroupName()))
.orderByDesc(GroupConfig::getId));
List<GroupConfig> records = groupConfigPageDTO.getRecords();
if (CollUtil.isEmpty(records)) {
return new PageResult<>(groupConfigPageDTO.getCurrent(), groupConfigPageDTO.getSize(),
groupConfigPageDTO.getTotal());
groupConfigPageDTO.getTotal());
}
PageResult<List<GroupConfigResponseVO>> pageResult = new PageResult<>(groupConfigPageDTO.getCurrent(),
groupConfigPageDTO.getSize(), groupConfigPageDTO.getTotal());
groupConfigPageDTO.getSize(), groupConfigPageDTO.getTotal());
List<GroupConfigResponseVO> responseVOList = GroupConfigResponseVOConverter.INSTANCE.convertList(
records);
records);
for (GroupConfigResponseVO groupConfigResponseVO : responseVOList) {
Optional.ofNullable(IdGeneratorModeEnum.modeOf(groupConfigResponseVO.getIdGeneratorMode()))
.ifPresent(idGeneratorMode -> {
groupConfigResponseVO.setIdGeneratorModeName(idGeneratorMode.getDesc());
});
.ifPresent(idGeneratorMode -> {
groupConfigResponseVO.setIdGeneratorModeName(idGeneratorMode.getDesc());
});
}
pageResult.setData(responseVOList);
@ -222,19 +222,19 @@ public class GroupConfigServiceImpl implements GroupConfigService {
groupConfig.setDescription(Optional.ofNullable(groupConfigRequestVO.getDescription()).orElse(StrUtil.EMPTY));
if (Objects.isNull(groupConfigRequestVO.getGroupPartition())) {
groupConfig.setGroupPartition(
HashUtil.bkdrHash(groupConfigRequestVO.getGroupName()) % tablePartitionList.size());
HashUtil.bkdrHash(groupConfigRequestVO.getGroupName()) % tablePartitionList.size());
} else {
Assert.isTrue(tablePartitionList.contains(groupConfigRequestVO.getGroupPartition()),
() -> new SnailJobServerException("分区不存在. [{}]", tablePartitionList));
() -> new SnailJobServerException("分区不存在. [{}]", tablePartitionList));
Assert.isTrue(groupConfigRequestVO.getGroupPartition() >= 0,
() -> new SnailJobServerException("分区不能是负数."));
() -> new SnailJobServerException("分区不能是负数."));
}
groupConfig.setBucketIndex(
HashUtil.bkdrHash(groupConfigRequestVO.getGroupName()) % systemProperties.getBucketTotal());
HashUtil.bkdrHash(groupConfigRequestVO.getGroupName()) % systemProperties.getBucketTotal());
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
Assert.isTrue(1 == groupConfigAccess.insert(groupConfig),
() -> new SnailJobServerException("新增组异常异常 groupConfigVO[{}]", groupConfigRequestVO));
() -> new SnailJobServerException("新增组异常异常 groupConfigVO[{}]", groupConfigRequestVO));
// 校验retry_task_x和retry_dead_letter_x是否存在
checkGroupPartition(groupConfig, namespaceId);
@ -249,12 +249,12 @@ public class GroupConfigServiceImpl implements GroupConfigService {
try {
TaskAccess<RetryTask> retryTaskAccess = accessTemplate.getRetryTaskAccess();
retryTaskAccess.count(groupConfig.getGroupName(), namespaceId,
new LambdaQueryWrapper<RetryTask>().eq(RetryTask::getId, 1));
new LambdaQueryWrapper<RetryTask>().eq(RetryTask::getId, 1));
} catch (BadSqlGrammarException e) {
Optional.ofNullable(e.getMessage()).ifPresent(s -> {
if (s.contains("retry_task_" + groupConfig.getGroupPartition()) && s.contains("doesn't exist")) {
throw new SnailJobServerException("分区:[{}] '未配置表retry_task_{}', 请联系管理员进行配置",
groupConfig.getGroupPartition(), groupConfig.getGroupPartition());
groupConfig.getGroupPartition(), groupConfig.getGroupPartition());
}
});
}
@ -262,12 +262,12 @@ public class GroupConfigServiceImpl implements GroupConfigService {
try {
TaskAccess<RetryDeadLetter> retryTaskAccess = accessTemplate.getRetryDeadLetterAccess();
retryTaskAccess.one(groupConfig.getGroupName(), namespaceId,
new LambdaQueryWrapper<RetryDeadLetter>().eq(RetryDeadLetter::getId, 1));
new LambdaQueryWrapper<RetryDeadLetter>().eq(RetryDeadLetter::getId, 1));
} catch (BadSqlGrammarException e) {
Optional.ofNullable(e.getMessage()).ifPresent(s -> {
if (s.contains("retry_dead_letter_" + groupConfig.getGroupPartition()) && s.contains("doesn't exist")) {
throw new SnailJobServerException("分区:[{}] '未配置表retry_dead_letter_{}', 请联系管理员进行配置",
groupConfig.getGroupPartition(), groupConfig.getGroupPartition());
groupConfig.getGroupPartition(), groupConfig.getGroupPartition());
}
});
}
@ -278,12 +278,12 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
GroupConfig groupConfig = groupConfigAccess.one(
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(GroupConfig::getGroupName, groupName));
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(GroupConfig::getGroupName, groupName));
GroupConfigResponseVO groupConfigResponseVO = GroupConfigResponseVOConverter.INSTANCE.convert(
groupConfig);
groupConfig);
Optional.ofNullable(IdGeneratorModeEnum.modeOf(groupConfig.getIdGeneratorMode())).ifPresent(idGeneratorMode -> {
groupConfigResponseVO.setIdGeneratorModeName(idGeneratorMode.getDesc());
@ -298,21 +298,21 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
List<GroupConfig> groupConfigs = groupConfigAccess.list(
new LambdaQueryWrapper<GroupConfig>()
.select(GroupConfig::getGroupName, GroupConfig::getNamespaceId)
.in(CollUtil.isNotEmpty(namespaceIds), GroupConfig::getNamespaceId, namespaceIds));
new LambdaQueryWrapper<GroupConfig>()
.select(GroupConfig::getGroupName, GroupConfig::getNamespaceId)
.in(CollUtil.isNotEmpty(namespaceIds), GroupConfig::getNamespaceId, namespaceIds));
if (CollUtil.isEmpty(groupConfigs)) {
return Collections.emptyList();
}
List<Namespace> namespaces = namespaceMapper.selectList(
new LambdaQueryWrapper<Namespace>()
.in(Namespace::getUniqueId, StreamUtils.toSet(groupConfigs, GroupConfig::getNamespaceId)));
new LambdaQueryWrapper<Namespace>()
.in(Namespace::getUniqueId, StreamUtils.toSet(groupConfigs, GroupConfig::getNamespaceId)));
Map<String, String> namespaceMap = StreamUtils.toMap(namespaces, Namespace::getUniqueId, Namespace::getName);
List<GroupConfigResponseVO> groupConfigResponses = GroupConfigResponseVOConverter.INSTANCE.convertList(
groupConfigs);
groupConfigs);
for (final GroupConfigResponseVO groupConfigResponseVO : groupConfigResponses) {
groupConfigResponseVO.setNamespaceName(namespaceMap.get(groupConfigResponseVO.getNamespaceId()));
}
@ -330,8 +330,8 @@ public class GroupConfigServiceImpl implements GroupConfigService {
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
List<GroupConfig> groupConfigs = groupConfigAccess.list(new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, userSessionVO.getNamespaceId())
.select(GroupConfig::getGroupName));
.eq(GroupConfig::getNamespaceId, userSessionVO.getNamespaceId())
.select(GroupConfig::getGroupName));
return StreamUtils.toList(groupConfigs, GroupConfig::getGroupName);
}
@ -339,9 +339,9 @@ public class GroupConfigServiceImpl implements GroupConfigService {
@Override
public List<String> getOnlinePods(String groupName) {
List<ServerNode> serverNodes = serverNodeMapper.selectList(
new LambdaQueryWrapper<ServerNode>()
.eq(ServerNode::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(ServerNode::getGroupName, groupName));
new LambdaQueryWrapper<ServerNode>()
.eq(ServerNode::getNamespaceId, UserSessionUtils.currentUserSession().getNamespaceId())
.eq(ServerNode::getGroupName, groupName));
return StreamUtils.toList(serverNodes, serverNode -> serverNode.getHostIp() + ":" + serverNode.getHostPort());
}
@ -371,8 +371,8 @@ public class GroupConfigServiceImpl implements GroupConfigService {
}
return tableList.stream().map(ReUtil::getFirstNumber).filter(i ->
!Objects.isNull(i)).distinct()
.collect(Collectors.toList());
!Objects.isNull(i)).distinct()
.collect(Collectors.toList());
} catch (SQLException ignored) {
} finally {
if (Objects.nonNull(connection)) {
@ -393,10 +393,14 @@ public class GroupConfigServiceImpl implements GroupConfigService {
Set<String> groupSet = StreamUtils.toSet(requestList, GroupConfigRequestVO::getGroupName);
ConfigAccess<GroupConfig> groupConfigAccess = accessTemplate.getGroupConfigAccess();
Assert.isTrue(groupConfigAccess.count(new LambdaQueryWrapper<GroupConfig>()
List<GroupConfig> configs = groupConfigAccess.list(new LambdaQueryWrapper<GroupConfig>()
.select(GroupConfig::getGroupName)
.eq(GroupConfig::getNamespaceId, namespaceId)
.in(GroupConfig::getGroupName, groupSet)) == 0,
() -> new SnailJobServerException("GroupName已经存在 {}", groupSet));
.in(GroupConfig::getGroupName, groupSet));
Assert.isTrue(CollUtil.isEmpty(configs),
() -> new SnailJobServerException("导入失败. 原因: 组{}已存在", StreamUtils.toSet(configs, GroupConfig::getGroupName)));
for (final GroupConfigRequestVO groupConfigRequestVO : requestList) {
@ -415,9 +419,9 @@ public class GroupConfigServiceImpl implements GroupConfigService {
String namespaceId = UserSessionUtils.currentUserSession().getNamespaceId();
List<GroupConfig> groupConfigs = accessTemplate.getGroupConfigAccess().list(
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.in(CollUtil.isNotEmpty(groupIds), GroupConfig::getId, groupIds)
new LambdaQueryWrapper<GroupConfig>()
.eq(GroupConfig::getNamespaceId, namespaceId)
.in(CollUtil.isNotEmpty(groupIds), GroupConfig::getId, groupIds)
);
return JsonUtil.toJsonString(GroupConfigConverter.INSTANCE.toGroupConfigRequestVOs(groupConfigs));