task-122: N+1 批量化修复(claim/stuck-reset/stale-check 逐行 selectById 改 selectBatchIds 批量回读 + Map 装配,行为等价)
This commit is contained in:
+50
-9
@@ -17,6 +17,7 @@ import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@@ -142,15 +143,22 @@ public class TaskFileJobService {
|
||||
if (candidates == null || candidates.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
List<TaskFileJobEntity> claimed = new ArrayList<>();
|
||||
List<Long> claimedIds = new ArrayList<>();
|
||||
for (TaskFileJobEntity candidate : candidates) {
|
||||
if (candidate == null || candidate.getId() == null) {
|
||||
continue;
|
||||
}
|
||||
if (!markRunning(candidate.getId())) {
|
||||
continue;
|
||||
if (markRunning(candidate.getId())) {
|
||||
claimedIds.add(candidate.getId());
|
||||
}
|
||||
TaskFileJobEntity claim = taskFileJobMapper.selectById(candidate.getId());
|
||||
}
|
||||
if (claimedIds.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
Map<Long, TaskFileJobEntity> claimedById = refreshJobsByIds(claimedIds);
|
||||
List<TaskFileJobEntity> claimed = new ArrayList<>();
|
||||
for (Long id : claimedIds) {
|
||||
TaskFileJobEntity claim = claimedById.get(id);
|
||||
if (claim != null && "RUNNING".equals(claim.getStatus())) {
|
||||
claimed.add(claim);
|
||||
}
|
||||
@@ -259,10 +267,16 @@ public class TaskFileJobService {
|
||||
.last("limit " + Math.max(1, Math.min(limit, 200))));
|
||||
int reset = 0;
|
||||
List<TaskFileJobEntity> exhaustedJobs = new ArrayList<>();
|
||||
List<Long> eventJobIds = new ArrayList<>();
|
||||
List<Long> exhaustedJobIds = new ArrayList<>();
|
||||
Map<Long, TaskFileJobEntity> originalExhaustedById = new LinkedHashMap<>();
|
||||
Map<Long, TaskFileJobEntity> originalJobsById = jobs.stream()
|
||||
.collect(Collectors.toMap(TaskFileJobEntity::getId, job -> job, (a, b) -> a));
|
||||
for (TaskFileJobEntity job : jobs) {
|
||||
if ("FAILED".equals(job.getStatus())
|
||||
&& job.getRetryCount() != null && job.getRetryCount() >= MAX_RETRY_COUNT) {
|
||||
exhaustedJobs.add(job);
|
||||
exhaustedJobIds.add(job.getId());
|
||||
originalExhaustedById.put(job.getId(), job);
|
||||
continue;
|
||||
}
|
||||
if ("PENDING".equals(job.getStatus())) {
|
||||
@@ -279,7 +293,7 @@ public class TaskFileJobService {
|
||||
.set(TaskFileJobEntity::getRetryCount, nextRetryCount)
|
||||
.set(TaskFileJobEntity::getUpdatedAt, LocalDateTime.now()));
|
||||
if (updated > 0) {
|
||||
publishDispatchEvent(taskFileJobMapper.selectById(job.getId()));
|
||||
eventJobIds.add(job.getId());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -300,8 +314,7 @@ public class TaskFileJobService {
|
||||
.set(TaskFileJobEntity::getFinishedAt, now)
|
||||
.set(TaskFileJobEntity::getTerminalCallbackAt, null));
|
||||
if (updated > 0) {
|
||||
TaskFileJobEntity exhausted = taskFileJobMapper.selectById(job.getId());
|
||||
exhaustedJobs.add(exhausted == null ? job : exhausted);
|
||||
exhaustedJobIds.add(job.getId());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
@@ -312,12 +325,40 @@ public class TaskFileJobService {
|
||||
.set(TaskFileJobEntity::getTerminalCallbackAt, null));
|
||||
if (updated > 0) {
|
||||
reset++;
|
||||
publishDispatchEvent(taskFileJobMapper.selectById(job.getId()));
|
||||
eventJobIds.add(job.getId());
|
||||
}
|
||||
}
|
||||
if (!eventJobIds.isEmpty()) {
|
||||
Map<Long, TaskFileJobEntity> refreshedById = refreshJobsByIds(eventJobIds);
|
||||
for (Long jobId : eventJobIds) {
|
||||
publishDispatchEvent(refreshedById.get(jobId));
|
||||
}
|
||||
}
|
||||
if (!exhaustedJobIds.isEmpty()) {
|
||||
List<Long> refreshIds = exhaustedJobIds.stream()
|
||||
.filter(id -> !originalExhaustedById.containsKey(id))
|
||||
.toList();
|
||||
Map<Long, TaskFileJobEntity> refreshedById = refreshIds.isEmpty()
|
||||
? Map.of()
|
||||
: refreshJobsByIds(refreshIds);
|
||||
for (Long jobId : exhaustedJobIds) {
|
||||
TaskFileJobEntity original = originalExhaustedById.get(jobId);
|
||||
if (original != null) {
|
||||
exhaustedJobs.add(original);
|
||||
continue;
|
||||
}
|
||||
TaskFileJobEntity refreshed = refreshedById.get(jobId);
|
||||
exhaustedJobs.add(refreshed == null ? originalJobsById.get(jobId) : refreshed);
|
||||
}
|
||||
}
|
||||
return new StuckJobResetResult(reset, List.copyOf(exhaustedJobs));
|
||||
}
|
||||
|
||||
private Map<Long, TaskFileJobEntity> refreshJobsByIds(List<Long> jobIds) {
|
||||
return taskFileJobMapper.selectBatchIds(jobIds).stream()
|
||||
.collect(Collectors.toMap(TaskFileJobEntity::getId, job -> job, (a, b) -> a));
|
||||
}
|
||||
|
||||
public record StuckJobResetResult(int resetCount, List<TaskFileJobEntity> exhaustedJobs) {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user