fix(wave): 波次闭环判空二次确认,避免瞬时空表误终结波次

- wave_batch_closure.js 新增判空二次确认:同一波次需连续两轮(间隔不小于
  confirmMinIntervalMs,默认 30s)都被判定为空才允许终结,确认状态写 Redis
  (TTL 1h);状态缓存不可用或读取异常时本轮不终结任何波次(宁可漏关、不可误关)
- 规避批量解波/订单迁移进行中造成的瞬时空表误判(2026-09-12 生产事故)
- closed_count 改为 UPDATE 实际影响行数,新增 empty_count / confirmed_count /
  pending_confirm_count 等统计与日志(波次列表超长截断)
- sync_finished_wave_batch.js 调度日志同步输出 empty_count / pending_confirm_count
- systemMethods/order/wave.js 解波 UPDATE 增加 wave_status < 3 守卫,避免覆盖
  自动终结(5)及已进入拣货及后续环节的波次状态
This commit is contained in:
liufumingren 2026-09-15 16:18:58 +08:00
parent 933a1ae341
commit df641257cc
3 changed files with 206 additions and 13 deletions

View File

@ -104,7 +104,9 @@ module.exports = {
status,
error_message: errorMessage,
candidate_count: result ? result.candidate_count : 0,
empty_count: result ? result.empty_count : 0,
closed_count: result ? result.closed_count : 0,
pending_confirm_count: result ? result.pending_confirm_count : 0,
warehouse_ids: result ? result.warehouse_ids : [],
closed_waves: result ? result.closed_waves : [],
};
@ -112,7 +114,8 @@ module.exports = {
await appendExecutionHistory(ctx, logEntry);
ctx.logger.info(
`[sync-finished-wave-batch] 本轮完成 closed=${result ? result.closed_count : 0} candidate=${result ? result.candidate_count : 0} warehouses=${result ? result.warehouse_ids.length : 0}`
`[sync-finished-wave-batch] 本轮完成 closed=${result ? result.closed_count : 0} candidate=${result ? result.candidate_count : 0}`
+ ` pendingConfirm=${result ? result.pending_confirm_count : 0} warehouses=${result ? result.warehouse_ids.length : 0}`
);
},
};

View File

@ -9168,12 +9168,15 @@ class WaveService extends OrderBaseService {
Array.isArray(orderUpdateResult) ? orderUpdateResult[0] : orderUpdateResult || 0
);
// wave_status < 3 守卫:解波只作用于待拣货口径的波次,避免覆盖并发流程
// (自动终结写入的 5、已进入拣货及后续环节的状态)导致状态互相打架
const cancelSql = `
UPDATE wms_wave_batch
SET wave_status = -1,
update_time = NOW()
WHERE warehouse_id = :warehouseId
AND wave_id = :waveId
AND wave_status < 3
`;
const cancelResult = await this.ctx.model.query(cancelSql, {
replacements: {

View File

@ -11,6 +11,50 @@ const CLOSED_WAVE_STATUS = 5;
const WAVE_MIN_AGE_MINUTES = 5;
// 单次 SQL IN 列表的波次数量上限
const WAVE_ID_BATCH_SIZE = 500;
// 判空二次确认:同一波次需连续两轮(间隔不小于该毫秒数)都判定为空,才允许终结。
// 用于规避"批量解波/订单迁移进行中"造成的瞬时空表误判(2026-09-12 生产事故)。
const EMPTY_CONFIRM_MIN_INTERVAL_MS = 30 * 1000;
// 判空二次确认状态缓存键(记录上一轮判空结果与首次判定时间)
const EMPTY_CONFIRM_CACHE_KEY = 'schedule:sync_finished_wave_batch:empty_confirm';
// 判空二次确认状态缓存 TTL(秒)
const EMPTY_CONFIRM_CACHE_TTL_SECONDS = 3600;
// 判空二次确认状态最多保留的波次数量(防御性上限)
const EMPTY_CONFIRM_MAX_WAVE_COUNT = 10000;
// 日志中最多打印的波次数量(避免日志爆量)
const LOG_WAVE_ID_LIMIT = 200;
/**
* 提取 UPDATE 语句实际影响行数。
*
* @param {*} updateResult Sequelize query 返回值(UPDATE 通常为 [undefined, metadata])。
* @return {number} 影响行数,无法识别时返回 0。
*/
function extractAffectedRows(updateResult) {
const metadata = Array.isArray(updateResult) ? updateResult[1] : updateResult;
const rawCount = metadata
&& (metadata.affectedRows !== undefined
? metadata.affectedRows
: metadata.affectedCount !== undefined
? metadata.affectedCount
: metadata.rowCount);
const count = Number(rawCount);
return Number.isFinite(count) && count > 0 ? count : 0;
}
/**
* 格式化波次ID列表用于日志输出(超长时截断)。
*
* @param {Array<number>} waveIds 波次ID列表。
* @return {string} 日志用字符串。
*/
function formatWaveIdListForLog(waveIds) {
const list = Array.isArray(waveIds) ? waveIds : [];
if (list.length <= LOG_WAVE_ID_LIMIT) {
return list.join(',');
}
return `${list.slice(0, LOG_WAVE_ID_LIMIT).join(',')},...(共${list.length}个)`;
}
/**
* 波次批次自动终结服务。
@ -26,14 +70,22 @@ class WaveBatchClosureService extends MainService {
/**
* 扫描并关闭"订单已全部离表"的活动波次。
*
* 判空需通过"二次确认":同一波次连续两轮(间隔不小于 confirmMinIntervalMs)
* 都被判定为空才允许终结。这样即使某一瞬间待处理表查不到该波次订单
* (批量解波、出库迁移、外部 DDL 等并发场景),也不会被误判为 0 单波次。
*
* @param {Object} [options={}] 执行选项。
* @param {boolean} [options.dryRun=false] 仅盘点不更新(用于验证命中范围)。
* @param {number} [options.confirmMinIntervalMs] 判空二次确认最小间隔(毫秒),传 0 表示跳过确认、首轮即终结。
* @return {Promise<Object>} 执行结果汇总。
*/
async closeFinishedWaves(options = {}) {
const { ctx } = this;
const startedAt = Date.now();
const dryRun = Boolean(options && options.dryRun);
const normalizedOptions = this.isPlainObject(options) ? options : {};
const dryRun = Boolean(normalizedOptions.dryRun);
const confirmMinIntervalMs = this.normalizeConfirmIntervalMs(normalizedOptions.confirmMinIntervalMs);
const confirmEnabled = !dryRun && confirmMinIntervalMs > 0;
const candidateRows = await ctx.model.query(
`SELECT wave_id, wave_no, warehouse_id, wave_status, total_orders
@ -68,7 +120,7 @@ class WaveBatchClosureService extends MainService {
}
const warehouseIds = Array.from(warehouseWaveMap.keys());
const closedWaves = [];
const emptyWaves = [];
for (const warehouseId of warehouseIds) {
const waves = warehouseWaveMap.get(warehouseId);
@ -120,17 +172,50 @@ class WaveBatchClosureService extends MainService {
const finishedWaves = waves.filter(item => !existingWaveIds.has(item.waveId));
if (finishedWaves.length > 0) {
closedWaves.push(...finishedWaves);
emptyWaves.push(...finishedWaves);
}
}
const closedWaveIds = closedWaves.map(item => item.waveId);
const candidateCount = Array.from(warehouseWaveMap.values()).reduce((sum, waves) => sum + waves.length, 0);
if (!dryRun && closedWaveIds.length > 0) {
for (let offset = 0; offset < closedWaveIds.length; offset += WAVE_ID_BATCH_SIZE) {
const batchWaveIds = closedWaveIds.slice(offset, offset + WAVE_ID_BATCH_SIZE);
await ctx.model.query(
// 二次确认:只有"上一轮也判空"且距首次判空已满 confirmMinIntervalMs 的波次才允许终结
const now = Date.now();
const previousState = confirmEnabled ? await this.readEmptyConfirmState() : null;
const previousWaveIds = Array.isArray(previousState && previousState.waveIds)
? previousState.waveIds
: [];
const previousWaveIdSet = new Set(previousWaveIds);
const previousRoundAt = Number(previousState && previousState.roundAt || 0);
const previousStateUsable = Number.isFinite(previousRoundAt)
&& previousRoundAt > 0
&& (now - previousRoundAt) >= confirmMinIntervalMs;
const confirmedWaves = [];
const pendingConfirmWaves = [];
for (const item of emptyWaves) {
if (!confirmEnabled || (previousStateUsable && previousWaveIdSet.has(item.waveId))) {
confirmedWaves.push(item);
} else {
pendingConfirmWaves.push(item);
}
}
if (confirmEnabled) {
// 状态仍在确认窗口内时保留首次判定时间,避免调度频率高于确认间隔时永远无法确认
const nextRoundAt = previousRoundAt > 0 && !previousStateUsable ? previousRoundAt : now;
await this.writeEmptyConfirmState({
roundAt: nextRoundAt,
waveIds: emptyWaves.map(item => item.waveId),
});
}
const confirmedWaveIds = confirmedWaves.map(item => item.waveId);
let closedRowCount = 0;
if (!dryRun && confirmedWaveIds.length > 0) {
for (let offset = 0; offset < confirmedWaveIds.length; offset += WAVE_ID_BATCH_SIZE) {
const batchWaveIds = confirmedWaveIds.slice(offset, offset + WAVE_ID_BATCH_SIZE);
const updateResult = await ctx.model.query(
`UPDATE wms_wave_batch
SET wave_status = :closedStatus,
processed_orders = total_orders,
@ -148,26 +233,128 @@ class WaveBatchClosureService extends MainService {
type: ctx.model.QueryTypes.UPDATE,
}
);
closedRowCount += extractAffectedRows(updateResult);
}
}
const result = {
dryRun,
confirm_min_interval_ms: confirmMinIntervalMs,
candidate_count: candidateCount,
closed_count: closedWaves.length,
empty_count: emptyWaves.length,
confirmed_count: confirmedWaves.length,
closed_count: dryRun ? 0 : closedRowCount,
pending_confirm_count: pendingConfirmWaves.length,
pending_confirm_wave_ids: pendingConfirmWaves.map(item => item.waveId),
empty_wave_ids: emptyWaves.map(item => item.waveId),
warehouse_ids: warehouseIds,
closed_waves: closedWaves,
closed_waves: confirmedWaves,
duration_ms: Date.now() - startedAt,
};
if (closedWaves.length > 0) {
if (confirmedWaves.length > 0 || pendingConfirmWaves.length > 0) {
const pendingSuffix = pendingConfirmWaves.length > 0
? ` pendingWaves=${formatWaveIdListForLog(result.pending_confirm_wave_ids)}`
: '';
const confirmedSuffix = confirmedWaveIds.length > 0
? ` waves=${formatWaveIdListForLog(confirmedWaveIds)}`
: '';
ctx.logger.info(
`[wave-batch-closure] 波次终结结果 dryRun=${dryRun} closed=${closedWaves.length}/${candidateCount} waves=${closedWaveIds.join(',')}`
`[wave-batch-closure] 波次终结结果 dryRun=${dryRun} candidate=${candidateCount}`
+ ` confirmed=${confirmedWaves.length} closedRows=${result.closed_count}`
+ ` pendingConfirm=${pendingConfirmWaves.length}${pendingSuffix}${confirmedSuffix}`
);
}
return result;
}
/**
* 归一化判空二次确认最小间隔。
*
* @param {*} rawValue 原始入参。
* @return {number} 毫秒数;未传/非法/负数时回落默认值,0 表示跳过确认。
*/
normalizeConfirmIntervalMs(rawValue) {
if (rawValue === undefined || rawValue === null || rawValue === '') {
return EMPTY_CONFIRM_MIN_INTERVAL_MS;
}
const numberValue = Number(rawValue);
if (!Number.isFinite(numberValue) || numberValue < 0) {
return EMPTY_CONFIRM_MIN_INTERVAL_MS;
}
return Math.floor(numberValue);
}
/**
* 读取上一轮判空二次确认状态。
*
* 缓存不可用或读取异常时返回 null,调用方据此跳过本轮终结(宁可漏关,不可误关)。
*
* @return {Promise<{ roundAt: number, waveIds: Array<number> }|null>} 上一轮状态。
*/
async readEmptyConfirmState() {
const { ctx } = this;
const app = ctx && ctx.app;
if (!app || typeof app.getCacheAsync !== 'function') {
ctx.logger.warn('[wave-batch-closure] 判空二次确认状态缓存不可用,本轮不终结任何波次');
return null;
}
try {
const cached = await app.getCacheAsync(EMPTY_CONFIRM_CACHE_KEY);
if (cached === undefined || cached === null) {
return null;
}
const state = typeof cached === 'string' ? JSON.parse(cached) : cached;
if (!state || typeof state !== 'object') {
return null;
}
return {
roundAt: Number(state.roundAt || 0),
waveIds: Array.isArray(state.waveIds)
? state.waveIds.map(waveId => Number(waveId)).filter(waveId => Number.isInteger(waveId) && waveId > 0)
: [],
};
} catch (error) {
ctx.logger.warn(`[wave-batch-closure] 读取判空二次确认状态失败,本轮不终结任何波次: ${error.message}`);
return null;
}
}
/**
* 写入本轮判空二次确认状态(供下一轮确认)。
*
* @param {{ roundAt: number, waveIds: Array<number> }} state 状态。
* @return {Promise<void>} 无返回值。
*/
async writeEmptyConfirmState(state) {
const { ctx } = this;
const app = ctx && ctx.app;
if (!app || typeof app.setCacheAsync !== 'function') {
return;
}
try {
const waveIds = Array.isArray(state && state.waveIds)
? state.waveIds.slice(0, EMPTY_CONFIRM_MAX_WAVE_COUNT)
: [];
await app.setCacheAsync(
EMPTY_CONFIRM_CACHE_KEY,
{
roundAt: Number(state && state.roundAt || Date.now()),
waveIds,
},
EMPTY_CONFIRM_CACHE_TTL_SECONDS
);
} catch (error) {
ctx.logger.warn(`[wave-batch-closure] 写入判空二次确认状态失败: ${error.message}`);
}
}
}
module.exports = WaveBatchClosureService;