perf(lastleg): batch receiving candidates and trace slow work invalidation

This commit is contained in:
2026-10-17 07:49:56 +08:00
parent af97cd17d0
commit 5fd74e9c8c
9 changed files with 377 additions and 17 deletions
@@ -4,13 +4,15 @@ import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/** Original business commands invalidate the manifest atomically with their business write. */
/** 原业务写入与工作清单失效保持在同一事务,不延后或绕过版本门禁。 */
@Component
public class AppointmentWorkChanges {
private final AppointmentWorkMapper mapper;
public AppointmentWorkChanges(AppointmentWorkMapper mapper) { this.mapper=mapper; }
private final AppointmentWorkStore store;
/** 复用工作事项持久层,统一记录守卫更新耗时。 */
public AppointmentWorkChanges(AppointmentWorkStore store) { this.store=store; }
/** 只标记需求变化,保留调用方原事务和异常语义。 */
@Transactional(propagation=Propagation.MANDATORY)
public void requirementsChanged() {
if(mapper.invalidateRequirements()!=1) throw AppointmentWorkPolicy.unavailable("工作事项同步守卫未初始化");
store.invalidateRequirements();
}
}
@@ -1,6 +1,9 @@
package com.baiyue.lastleg.appointmentwork;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import java.time.*;
import java.util.*;
@@ -9,10 +12,25 @@ import static com.baiyue.lastleg.appointmentwork.AppointmentWorkPolicy.*;
@Component
public class AppointmentWorkStore {
private static final Logger LOG = LoggerFactory.getLogger(AppointmentWorkStore.class);
public static final int MAX_ITEMS=10000;
private final AppointmentWorkMapper mapper;
private final ObjectMapper json;
public AppointmentWorkStore(AppointmentWorkMapper mapper,ObjectMapper json) { this.mapper=mapper; this.json=json; }
/** 只测量守卫更新语句,不改变锁、事务或版本,用于区分慢写是否卡在该语句。 */
public void invalidateRequirements() {
long started = System.nanoTime();
try {
if (mapper.invalidateRequirements() != 1) throw unavailable("工作事项同步守卫未初始化");
} finally {
long elapsedMs = java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - started);
if (elapsedMs >= 250) {
// 语句耗时可能包含等锁或连接等待,不能仅凭日志认定具体锁根因;不记录请求业务载荷。
LOG.warn("Appointment work guard update slow: elapsedMs={}, requestId={}, traceId={}",
elapsedMs, MDC.get("requestId"), MDC.get("traceId"));
}
}
}
public AppointmentWorkMapper.Guard lock() {
var guard=mapper.lockGuard(); if(guard==null) throw unavailable("工作事项DDL尚未集成"); return guard;
}