mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
feat: 优化bi层,提高并发处理能力
This commit is contained in:
+9
-3
@@ -9,12 +9,18 @@ import org.springframework.stereotype.Component;
|
||||
@ConfigurationProperties(prefix = "ecom.bi.stat")
|
||||
public class BiStatProperties {
|
||||
|
||||
/** 全量计算锁 TTL(秒) */
|
||||
/** 日计算锁 TTL(秒) */
|
||||
private long lockTtlSeconds = 600;
|
||||
|
||||
/** 增量遇全量进行中时延迟毫秒 */
|
||||
/** 遇同日锁忙时延迟毫秒 */
|
||||
private long incDelayMs = 5000;
|
||||
|
||||
/** 增量延迟最大重试次数 */
|
||||
/** 延迟最大重试次数 */
|
||||
private int incMaxRetry = 20;
|
||||
|
||||
/** BI 全量队列消费者并发(支持 "n" 或 "m-n") */
|
||||
private String fullListenerConcurrency = "2-4";
|
||||
|
||||
/** BI 增量队列消费者并发 */
|
||||
private String incListenerConcurrency = "2-4";
|
||||
}
|
||||
|
||||
+26
-14
@@ -7,15 +7,16 @@ import top.xyquan.ecomdmbbackend.modules.bi.infra.config.BiStatProperties;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* BI 同店串行锁(全量/增量共用)。
|
||||
* 全量抢锁时登记会话 snapshotTime(事务快照开启时刻),供进行中的增量跳过/延后;
|
||||
* 持久化的「最新全量快照时间」以 Mongo 文档 dbSnapshotTime 为准。
|
||||
* BI 店+日计算锁(同日 FULL/INC 互斥;不同日可并行)。
|
||||
* 全量抢锁时登记会话 snapshotTime,供同日进行中的增量跳过/延后;
|
||||
* 持久化的「最新全量快照时间」以该日 Mongo 文档 dbSnapshotTime 为准。
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
@@ -26,9 +27,17 @@ public class BiStatComputeLock {
|
||||
private final StringRedisTemplate stringRedisTemplate;
|
||||
private final BiStatProperties biStatProperties;
|
||||
|
||||
/** 同店串行:key = companyId:shopId */
|
||||
public Optional<Session> tryLock(Long companyId, Long shopId, Instant snapshotTimeOrNull) {
|
||||
String key = lockKey(companyId, shopId);
|
||||
/**
|
||||
* 抢占 (companyId, shopId, statDate) 日锁。
|
||||
*
|
||||
* @param snapshotTimeOrNull 全量传入会话快照时刻;增量传 null
|
||||
*/
|
||||
public Optional<Session> tryLock(Long companyId, Long shopId, LocalDate statDate,
|
||||
Instant snapshotTimeOrNull) {
|
||||
if (companyId == null || shopId == null || statDate == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String key = lockKey(companyId, shopId, statDate);
|
||||
String token = UUID.randomUUID().toString();
|
||||
long ttl = Math.max(biStatProperties.getLockTtlSeconds(), 60);
|
||||
Boolean ok = stringRedisTemplate.opsForValue()
|
||||
@@ -43,16 +52,18 @@ public class BiStatComputeLock {
|
||||
}
|
||||
stringRedisTemplate.opsForHash().putAll(key, meta);
|
||||
stringRedisTemplate.expire(key, Duration.ofSeconds(ttl));
|
||||
return Optional.of(new Session(key, token, snapshotTimeOrNull));
|
||||
return Optional.of(new Session(key, token, snapshotTimeOrNull, companyId, shopId, statDate));
|
||||
}
|
||||
|
||||
public Optional<Instant> peekSessionSnapshot(Long companyId, Long shopId) {
|
||||
Object v = stringRedisTemplate.opsForHash().get(lockKey(companyId, shopId), "snapshotTime");
|
||||
public Optional<Instant> peekSessionSnapshot(Long companyId, Long shopId, LocalDate statDate) {
|
||||
Object v = stringRedisTemplate.opsForHash()
|
||||
.get(lockKey(companyId, shopId, statDate), "snapshotTime");
|
||||
return parseInstant(v);
|
||||
}
|
||||
|
||||
public boolean isLocked(Long companyId, Long shopId) {
|
||||
return Boolean.TRUE.equals(stringRedisTemplate.hasKey(lockKey(companyId, shopId) + ":hold"));
|
||||
public boolean isLocked(Long companyId, Long shopId, LocalDate statDate) {
|
||||
return Boolean.TRUE.equals(stringRedisTemplate.hasKey(
|
||||
lockKey(companyId, shopId, statDate) + ":hold"));
|
||||
}
|
||||
|
||||
public void unlock(Session session) {
|
||||
@@ -67,8 +78,8 @@ public class BiStatComputeLock {
|
||||
}
|
||||
}
|
||||
|
||||
private static String lockKey(Long companyId, Long shopId) {
|
||||
return LOCK_PREFIX + companyId + ":" + shopId;
|
||||
private static String lockKey(Long companyId, Long shopId, LocalDate statDate) {
|
||||
return LOCK_PREFIX + companyId + ":" + shopId + ":" + statDate;
|
||||
}
|
||||
|
||||
private static Optional<Instant> parseInstant(Object v) {
|
||||
@@ -82,6 +93,7 @@ public class BiStatComputeLock {
|
||||
}
|
||||
}
|
||||
|
||||
public record Session(String key, String token, Instant snapshotTime) {
|
||||
public record Session(String key, String token, Instant snapshotTime,
|
||||
Long companyId, Long shopId, LocalDate statDate) {
|
||||
}
|
||||
}
|
||||
|
||||
+5
-3
@@ -22,7 +22,9 @@ public class BiStatFullConsumer {
|
||||
private final BiStatProperties biStatProperties;
|
||||
private final DelayedMqRepublisher delayedMqRepublisher;
|
||||
|
||||
@RabbitListener(queues = OrderMqConstants.QUEUE_BI_STAT_FULL)
|
||||
@RabbitListener(
|
||||
queues = OrderMqConstants.QUEUE_BI_STAT_FULL,
|
||||
concurrency = "${ecom.bi.stat.full-listener-concurrency:2-4}")
|
||||
public void onMessage(BiFullCalcMessage message) {
|
||||
if (message == null || message.getCompanyId() == null || message.getShopId() == null
|
||||
|| message.getStatDate() == null) {
|
||||
@@ -54,7 +56,7 @@ public class BiStatFullConsumer {
|
||||
OrderMqConstants.RK_BI_STAT_FULL,
|
||||
message,
|
||||
biStatProperties.getIncDelayMs());
|
||||
log.info("BI full 店锁占用,已安排延迟重投 messageId={} shopId={} retry={}",
|
||||
message.getMessageId(), message.getShopId(), message.getRetryCount());
|
||||
log.info("BI full 日锁占用,已安排延迟重投 messageId={} shopId={} statDate={} retry={}",
|
||||
message.getMessageId(), message.getShopId(), message.getStatDate(), message.getRetryCount());
|
||||
}
|
||||
}
|
||||
|
||||
+14
-12
@@ -18,9 +18,9 @@ import java.time.OffsetDateTime;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* BI 增量:与同店全量串行。
|
||||
* 比较消息中的内部订单更新时间 vs Mongo 文档最新全量快照时间(dbSnapshotTime);
|
||||
* 在快照内(≤)跳过,在外(>)才 $inc。全量进行中则按会话快照同样规则跳过或延后。
|
||||
* BI 增量:与同店同日全量互斥;不同日可并行。
|
||||
* 比较消息中的内部订单更新时间 vs 该日 Mongo 文档最新全量快照时间(dbSnapshotTime);
|
||||
* 在快照内(≤)跳过,在外(>)才 $inc。同日全量进行中则按会话快照同样规则跳过或延后。
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -32,7 +32,9 @@ public class BiStatIncConsumer {
|
||||
private final BiStatProperties biStatProperties;
|
||||
private final DelayedMqRepublisher delayedMqRepublisher;
|
||||
|
||||
@RabbitListener(queues = OrderMqConstants.QUEUE_BI_STAT_INC)
|
||||
@RabbitListener(
|
||||
queues = OrderMqConstants.QUEUE_BI_STAT_INC,
|
||||
concurrency = "${ecom.bi.stat.inc-listener-concurrency:2-4}")
|
||||
public void onMessage(BiIncCalcMessage message) {
|
||||
if (message == null || message.getCompanyId() == null || message.getShopId() == null
|
||||
|| message.getStatDate() == null || message.getInternalOrderUpdateTime() == null) {
|
||||
@@ -42,12 +44,12 @@ public class BiStatIncConsumer {
|
||||
LocalDate statDate = LocalDate.parse(message.getStatDate());
|
||||
Instant internalUpdated = message.getInternalOrderUpdateTime().toInstant();
|
||||
|
||||
// 全量进行中:同店串行;内部更新时间已在会话快照内则跳过,否则等全量结束后再比文档
|
||||
if (computeLock.isLocked(message.getCompanyId(), message.getShopId())) {
|
||||
Optional<Instant> sessionSnapshot =
|
||||
computeLock.peekSessionSnapshot(message.getCompanyId(), message.getShopId());
|
||||
// 同日全量进行中:内部更新时间已在会话快照内则跳过,否则等全量结束后再比文档
|
||||
if (computeLock.isLocked(message.getCompanyId(), message.getShopId(), statDate)) {
|
||||
Optional<Instant> sessionSnapshot = computeLock.peekSessionSnapshot(
|
||||
message.getCompanyId(), message.getShopId(), statDate);
|
||||
if (sessionSnapshot.isPresent() && !internalUpdated.isAfter(sessionSnapshot.get())) {
|
||||
log.info("BI inc 跳过(全量会话快照内) messageId={} shopId={} statDate={}",
|
||||
log.info("BI inc 跳过(同日全量会话快照内) messageId={} shopId={} statDate={}",
|
||||
message.getMessageId(), message.getShopId(), statDate);
|
||||
return;
|
||||
}
|
||||
@@ -64,7 +66,7 @@ public class BiStatIncConsumer {
|
||||
}
|
||||
|
||||
Optional<BiStatComputeLock.Session> session =
|
||||
computeLock.tryLock(message.getCompanyId(), message.getShopId(), null);
|
||||
computeLock.tryLock(message.getCompanyId(), message.getShopId(), statDate, null);
|
||||
if (session.isEmpty()) {
|
||||
delayAndRepublish(message);
|
||||
return;
|
||||
@@ -111,7 +113,7 @@ public class BiStatIncConsumer {
|
||||
OrderMqConstants.RK_BI_STAT_INC,
|
||||
message,
|
||||
biStatProperties.getIncDelayMs());
|
||||
log.info("BI inc 店锁占用,已安排延迟重投 messageId={} shopId={} retry={}",
|
||||
message.getMessageId(), message.getShopId(), message.getRetryCount());
|
||||
log.info("BI inc 日锁占用,已安排延迟重投 messageId={} shopId={} statDate={} retry={}",
|
||||
message.getMessageId(), message.getShopId(), message.getStatDate(), message.getRetryCount());
|
||||
}
|
||||
}
|
||||
|
||||
+9
-7
@@ -50,14 +50,14 @@ public class OrderDailyStatComputeService {
|
||||
private final PlatformTransactionManager transactionManager;
|
||||
|
||||
/**
|
||||
* 全量:同店串行锁 + MySQL 事务一致读重算;
|
||||
* 将事务快照开启时间写入 Mongo 文档 dbSnapshotTime(最新全量计算快照时间)。
|
||||
* 全量:店+日锁 + MySQL 事务一致读重算;
|
||||
* 将事务快照开启时间写入 Mongo 文档 dbSnapshotTime(该日最新全量计算快照时间)。
|
||||
*/
|
||||
public boolean tryComputeFull(Long companyId, Long shopId, LocalDate statDate, String platformCode) {
|
||||
// 快照时刻在抢锁时登记,供进行中的增量与会话快照比较
|
||||
// 快照时刻在抢锁时登记,供同日进行中的增量与会话快照比较
|
||||
Instant snapshotTime = Instant.now();
|
||||
Optional<BiStatComputeLock.Session> sessionOpt =
|
||||
computeLock.tryLock(companyId, shopId, snapshotTime);
|
||||
computeLock.tryLock(companyId, shopId, statDate, snapshotTime);
|
||||
if (sessionOpt.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@@ -82,7 +82,7 @@ public class OrderDailyStatComputeService {
|
||||
*/
|
||||
public boolean tryComputeInc(Long companyId, Long shopId, LocalDate statDate, String platformCode) {
|
||||
Optional<BiStatComputeLock.Session> sessionOpt =
|
||||
computeLock.tryLock(companyId, shopId, null);
|
||||
computeLock.tryLock(companyId, shopId, statDate, null);
|
||||
if (sessionOpt.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@@ -99,14 +99,16 @@ public class OrderDailyStatComputeService {
|
||||
/** @deprecated 使用 {@link #tryComputeFull} */
|
||||
public void computeFull(Long companyId, Long shopId, LocalDate statDate, String platformCode) {
|
||||
if (!tryComputeFull(companyId, shopId, statDate, platformCode)) {
|
||||
throw new IllegalStateException("BI 店铺计算锁占用中: " + companyId + "/" + shopId);
|
||||
throw new IllegalStateException(
|
||||
"BI 店日计算锁占用中: " + companyId + "/" + shopId + "/" + statDate);
|
||||
}
|
||||
}
|
||||
|
||||
/** @deprecated 使用 {@link #tryComputeInc} */
|
||||
public void computeInc(Long companyId, Long shopId, LocalDate statDate, String platformCode) {
|
||||
if (!tryComputeInc(companyId, shopId, statDate, platformCode)) {
|
||||
throw new IllegalStateException("BI 店铺计算锁占用中: " + companyId + "/" + shopId);
|
||||
throw new IllegalStateException(
|
||||
"BI 店日计算锁占用中: " + companyId + "/" + shopId + "/" + statDate);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
@@ -61,11 +61,23 @@ public class PddOrderFullSyncTaskService {
|
||||
private final CurrentUserContext currentUserContext;
|
||||
private final PddOrderFullSyncProgressPublisher progressPublisher;
|
||||
|
||||
/** 全量任务可选日历天数(含起止日,对齐 POP「近三个月」) */
|
||||
public static final int FULL_SYNC_MAX_CALENDAR_DAYS = 90;
|
||||
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public PddOrderFullSyncTaskVO createTask(PddOrderFullSyncTaskCreateDTO dto) {
|
||||
if (dto.getDateTo().isBefore(dto.getDateFrom())) {
|
||||
throw new BusinessException("结束日期不能早于开始日期");
|
||||
}
|
||||
LocalDate today = LocalDate.now(java.time.ZoneId.of("Asia/Shanghai"));
|
||||
if (dto.getDateTo().isAfter(today)) {
|
||||
throw new BusinessException("结束日期不能晚于今天");
|
||||
}
|
||||
LocalDate earliest = today.minusDays(FULL_SYNC_MAX_CALENDAR_DAYS - 1L);
|
||||
if (dto.getDateFrom().isBefore(earliest)) {
|
||||
throw new BusinessException("全量同步仅支持近 " + FULL_SYNC_MAX_CALENDAR_DAYS
|
||||
+ " 天(含今天):最早可选 " + earliest);
|
||||
}
|
||||
PddOrderSyncShopContext ctx = contextResolver.resolveForCurrentCompany(dto.getShopId());
|
||||
|
||||
PddOrderFullSyncTaskPO active = taskMapper.selectOne(new LambdaQueryWrapper<PddOrderFullSyncTaskPO>()
|
||||
|
||||
+11
@@ -195,8 +195,19 @@ public class PddOrderSyncService {
|
||||
List<ChannelOrderRef> dayRefs = new ArrayList<>();
|
||||
LocalDateTime dayStart = day.atStartOfDay();
|
||||
LocalDateTime dayEnd = day.plusDays(1).atStartOfDay().minusSeconds(1);
|
||||
// 当日:接口只能拉到当前时刻,避免把 end 开到当日 23:59:59
|
||||
LocalDate today = LocalDate.now(ZONE);
|
||||
if (day.equals(today)) {
|
||||
LocalDateTime now = LocalDateTime.now(ZONE);
|
||||
if (now.isBefore(dayEnd)) {
|
||||
dayEnd = now;
|
||||
}
|
||||
}
|
||||
long startTs = dayStart.atZone(ZONE).toEpochSecond();
|
||||
long endTs = dayEnd.atZone(ZONE).toEpochSecond();
|
||||
if (endTs < startTs) {
|
||||
continue;
|
||||
}
|
||||
int page = 1;
|
||||
boolean hasNext = true;
|
||||
while (hasNext) {
|
||||
|
||||
Reference in New Issue
Block a user