generate 代码优化
This commit is contained in:
@@ -10,6 +10,7 @@ import com.alibaba.fastjson.JSONObject;
|
|||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.rabbitmq.client.Channel;
|
import com.rabbitmq.client.Channel;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.tomcat.jni.Time;
|
||||||
import org.springframework.amqp.core.Message;
|
import org.springframework.amqp.core.Message;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
|
||||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||||
@@ -41,9 +42,6 @@ public class GenerateConsumer {
|
|||||||
@Value("${redis.key.generateExceptionMap}")
|
@Value("${redis.key.generateExceptionMap}")
|
||||||
private String exceptionMapKey;
|
private String exceptionMapKey;
|
||||||
|
|
||||||
@Value("${redis.key.resultMap}")
|
|
||||||
private String resultMapKey;
|
|
||||||
|
|
||||||
@Value("${redis.key.generateResult}")
|
@Value("${redis.key.generateResult}")
|
||||||
private String generateResultKey;
|
private String generateResultKey;
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ public interface GenerateService extends IService<Generate> {
|
|||||||
|
|
||||||
List<GenerateDetail> selectBatchByLibraryId(List<Long> libraryId);
|
List<GenerateDetail> selectBatchByLibraryId(List<Long> libraryId);
|
||||||
|
|
||||||
GenerateCollectionVO getGenerateResult(String uniqueId);
|
// GenerateCollectionVO getGenerateResult(String uniqueId);
|
||||||
|
|
||||||
List<GenerateResultVO> getGenerateResultList(List<String> taskIdList);
|
List<GenerateResultVO> getGenerateResultList(List<String> taskIdList);
|
||||||
|
|
||||||
|
|||||||
@@ -82,9 +82,6 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
@Value("${redis.key.generateExceptionMap}")
|
@Value("${redis.key.generateExceptionMap}")
|
||||||
private String exceptionMapKey;
|
private String exceptionMapKey;
|
||||||
|
|
||||||
@Value("${redis.key.resultMap}")
|
|
||||||
private String resultMapKey;
|
|
||||||
|
|
||||||
@Value("${redis.key.generateResult}")
|
@Value("${redis.key.generateResult}")
|
||||||
private String generateResultKey;
|
private String generateResultKey;
|
||||||
|
|
||||||
@@ -428,27 +425,9 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
|
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
|
||||||
validateGeneraType(new Generate(), text, elementId, generateType);
|
validateGeneraType(new Generate(), text, elementId, generateType);
|
||||||
|
|
||||||
// 2、生成唯一id 使用uuid
|
// 2、生成唯一id 使用uuid,由于uuid重复的几率很小,故取消对uuid重复性的校验
|
||||||
String uuid = UUID.randomUUID().toString();
|
String uuid = UUID.randomUUID().toString();
|
||||||
|
|
||||||
int num = 1;
|
|
||||||
// 判断已经正常生成结果的uuid或正在排队的uuid中是否有相同的id
|
|
||||||
while ((redisUtil.isElementExistsInMap(resultMapKey, uuid) ||
|
|
||||||
redisUtil.isElementExistsInZSet(consumptionOrderKey, uuid))
|
|
||||||
&& num < 10) {
|
|
||||||
uuid = UUID.randomUUID().toString();
|
|
||||||
num++;
|
|
||||||
}
|
|
||||||
// 无依据确定的数字
|
|
||||||
if (num > 10) {
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
uuid = UUID.randomUUID().toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<String> taskIdList = new ArrayList<>();
|
ArrayList<String> taskIdList = new ArrayList<>();
|
||||||
for (int i = 1; i <= 4; i++) {
|
for (int i = 1; i <= 4; i++) {
|
||||||
String temp = uuid;
|
String temp = uuid;
|
||||||
@@ -480,7 +459,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
return redisUtil.getRank(consumptionOrderKey, uniqueId);
|
return redisUtil.getRank(consumptionOrderKey, uniqueId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/*@Override
|
||||||
public GenerateCollectionVO getGenerateResult(String uniqueId) {
|
public GenerateCollectionVO getGenerateResult(String uniqueId) {
|
||||||
// 1、判断该请求是否已经异常
|
// 1、判断该请求是否已经异常
|
||||||
Boolean isMember = redisUtil.isElementExistsInMap(exceptionMapKey, uniqueId);
|
Boolean isMember = redisUtil.isElementExistsInMap(exceptionMapKey, uniqueId);
|
||||||
@@ -529,7 +508,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
});
|
});
|
||||||
|
|
||||||
return new GenerateCollectionVO(generateId, null, generatedCollectionItems);
|
return new GenerateCollectionVO(generateId, null, generatedCollectionItems);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<GenerateResultVO> getGenerateResultList(List<String> taskIdList) {
|
public List<GenerateResultVO> getGenerateResultList(List<String> taskIdList) {
|
||||||
@@ -579,7 +558,10 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
public void cancelGenerate(Long userId, List<String> uniqueIdList, String timeZone) {
|
public void cancelGenerate(Long userId, List<String> uniqueIdList, String timeZone) {
|
||||||
// todo 取消待优化
|
// todo 取消待优化
|
||||||
uniqueIdList.forEach(uniqueId -> {
|
uniqueIdList.forEach(uniqueId -> {
|
||||||
// 1、确认当前消息是否还在排队中
|
// 1、将需要取消的唯一id加入redis,以便及时取消生成
|
||||||
|
redisUtil.addToSet(cancelSetKey, uniqueId);
|
||||||
|
|
||||||
|
/*// 1、确认当前消息是否还在排队中
|
||||||
Boolean exists = redisUtil.isElementExistsInZSet(consumptionOrderKey, uniqueId);
|
Boolean exists = redisUtil.isElementExistsInZSet(consumptionOrderKey, uniqueId);
|
||||||
Boolean flag = Boolean.FALSE;
|
Boolean flag = Boolean.FALSE;
|
||||||
if (exists) flag = redisUtil.getRank(consumptionOrderKey, uniqueId) > 1L ? Boolean.TRUE : Boolean.FALSE;
|
if (exists) flag = redisUtil.getRank(consumptionOrderKey, uniqueId) > 1L ? Boolean.TRUE : Boolean.FALSE;
|
||||||
@@ -600,9 +582,17 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
|
|||||||
// 3、直接发送取消请求到python端
|
// 3、直接发送取消请求到python端
|
||||||
pythonService.cancelGenerateTask(uniqueId);
|
pythonService.cancelGenerateTask(uniqueId);
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
String key = generateResultKey + ":" + uniqueId;
|
String key = generateResultKey + ":" + uniqueId;
|
||||||
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(uniqueId, null, null, "Cancelled")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
|
GenerateResultVO generateResultVO = new Gson().fromJson(redisUtil.getFromString(key), GenerateResultVO.class);
|
||||||
|
// 判断当前task的状态是不是Fail
|
||||||
|
if (!generateResultVO.getStatus().equals("Fail")){
|
||||||
|
// 2、不是,直接发送取消请求到python端
|
||||||
|
pythonService.cancelGenerateTask(uniqueId);
|
||||||
|
// 3、更改result中当前taskId的状态
|
||||||
|
redisUtil.addToString(key, new Gson().toJson(new GenerateResultVO(uniqueId, null, null, "Cancelled")), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
// 3、考虑加一张表,专门用于记录哪些用户在什么时间进行了取消操作,包括已经异常的请求
|
// 3、考虑加一张表,专门用于记录哪些用户在什么时间进行了取消操作,包括已经异常的请求
|
||||||
GenerateCancel generateCancel = new GenerateCancel(userId, uniqueId, DateUtil.getByTimeZone(timeZone));
|
GenerateCancel generateCancel = new GenerateCancel(userId, uniqueId, DateUtil.getByTimeZone(timeZone));
|
||||||
|
|||||||
@@ -57,9 +57,6 @@ public class SuperResolutionServiceImpl extends ServiceImpl<TaskListMapper, Task
|
|||||||
@Value("${redis.key.orderForSR}")
|
@Value("${redis.key.orderForSR}")
|
||||||
private String orderForSR;
|
private String orderForSR;
|
||||||
|
|
||||||
@Value("${redis.key.resultMap}")
|
|
||||||
private String resultMapKey;
|
|
||||||
|
|
||||||
@Value("${minio.bucketName.users}")
|
@Value("${minio.bucketName.users}")
|
||||||
private String usersBucket;
|
private String usersBucket;
|
||||||
|
|
||||||
@@ -84,16 +81,8 @@ public class SuperResolutionServiceImpl extends ServiceImpl<TaskListMapper, Task
|
|||||||
for (SuperResolutionDTO superResolutionDTO : superResolutionDTOList) {
|
for (SuperResolutionDTO superResolutionDTO : superResolutionDTOList) {
|
||||||
// todo 校验倍率是否是2的幂次(前端已做)
|
// todo 校验倍率是否是2的幂次(前端已做)
|
||||||
|
|
||||||
// 2、生成唯一id 使用uuid
|
// 2、生成唯一id 使用uuid 由于uuid重复的几率很小很小,故这里取消验证uuid是否已存在
|
||||||
String uuid = UUID.randomUUID().toString();
|
String uuid = UUID.randomUUID().toString();
|
||||||
int num = 1;
|
|
||||||
// 判断已经正常生成结果的uuid或正在排队的uuid中是否有相同的id
|
|
||||||
while ((redisUtil.isElementExistsInMap(resultMapKey, uuid) ||
|
|
||||||
redisUtil.isElementExistsInZSet(orderForSR, uuid))
|
|
||||||
&& num < 10) {
|
|
||||||
uuid = UUID.randomUUID().toString();
|
|
||||||
num++;
|
|
||||||
}
|
|
||||||
uuid += "-" + accountId;
|
uuid += "-" + accountId;
|
||||||
uuidList.add(uuid);
|
uuidList.add(uuid);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user