refactor(flight-state): 按 flight-state.md 审计定稿全量重构脚手架与 SQL (ACM2-31)
- SQL 基线 V1__flight_state_baseline.sql 整体取代 V1.0.0–V1.4.0: PIPELINE_LOCK/PROC_STATE/MSG_EVENT/REQ_TRACK/BACKFILL_TODO/FLIGHT_SCHD + 8 张资源明细表 + FLIGHT_ROUTE_POINT + SCHD_SNAP_LOG 留痕层 - 废除 FDAY 日代/SCHD_GEN/名单差删:OPERATION_DAY 不可变(应用层校验 + 条件更新强化 §7.4),STATE 仅 ACTIVE/DELETED,物理清除只在历史归档后 - 处理器化:applyScheduleRecords(§5.1 七步同一事务,重放判定/整包 DEAD(PROTOCOL)/归属冲突不落地)+ FLOP/FDEL/ADFT(tombstone 仅 ACTIVE→DELETED,重复 FDEL 幂等不推进版本) - 投递:KAFKA_SCHD 同 FLID 按最新 STATE_VERSION 合并,被压掉事件关闭, TOMBSTONE 发 null 值消息(键缺失=删除旧值 §7.3) - 回填待办改为业务事务内预登记,消除提交后写待办的崩溃窗口(§7.2/§10) - XML 解码改为 jackson-dataformat-xml 数据类直接映射(SIS 信封强类型, FLTR 开放标签泛型承载) - 历史归档/物理清除顺序不可颠倒:归档确认成功集才物理删除,未接通删 0 条 - 移除 PUMP_JOB 队列/ReferenceService/FlightStoreDiffTool 等旧机制与测试, 新增运营日/引擎/快照/FDEL/归档顺序不变性回归测试
This commit is contained in:
@@ -0,0 +1,214 @@
|
||||
package com.gzzn.omms.msgexchange.processing
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.gzzn.omms.msgexchange.codec.FlopPayload
|
||||
import com.gzzn.omms.msgexchange.config.OperationDayProps
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.EventType
|
||||
import com.gzzn.omms.msgexchange.domain.MsgEvent
|
||||
import com.gzzn.omms.msgexchange.domain.OperationDayCalculator
|
||||
import com.gzzn.omms.msgexchange.domain.ProcState
|
||||
import com.gzzn.omms.msgexchange.domain.Targets
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightSnapshot
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine
|
||||
import com.gzzn.omms.msgexchange.domain.flight.MergeChange
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.BackfillTodoRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightStateRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineLockRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineTransactionManager
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Instant
|
||||
import java.time.ZoneId
|
||||
|
||||
/**
|
||||
* FLOP(§6.1):读取完整当前态 → 合并变化 → 保留运营日 → STATE_VERSION+1 →
|
||||
* 同事务登记 KAFKA_MSG / KAFKA_SCHD 与处理终态。
|
||||
*/
|
||||
@Singleton
|
||||
class FlopProcessor(
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val lock: PipelineLockRepository,
|
||||
private val flightState: FlightStateRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val backfillTodo: BackfillTodoRepository?,
|
||||
private val mapper: ObjectMapper,
|
||||
) {
|
||||
fun apply(head: ProcState, msg: DecodedMessage, payload: FlopPayload): ApplyResult = txManager.inTransaction {
|
||||
lock.lock()
|
||||
val current = flightState.loadFullSnapshot(payload.flid)
|
||||
?: return@inTransaction idempotentAbsent(head, msg) // 迟到/未知航班:幂等成功,不创建
|
||||
|
||||
val change = MergeChange(flid = payload.flid, scalars = payload.scalars, collections = payload.collections)
|
||||
val next = FlightStateEngine.mergedState(current, change)
|
||||
flightState.persistFullState(next, msgId = head.msgId, now = Instant.now())
|
||||
msgEvents.insertAll(eventsFor(next, mapper))
|
||||
preRegisterBackfill(head, msg, backfillTodo)
|
||||
ApplyResult.Succeeded
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* FDEL(§6.2):ACTIVE → 置 DELETED、推进版本、明细保留、发布 tombstone;
|
||||
* 已 DELETED / 不存在 → 幂等成功,不推进版本、不重复发布。
|
||||
*/
|
||||
@Singleton
|
||||
class FdelProcessor(
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val lock: PipelineLockRepository,
|
||||
private val flightState: FlightStateRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val backfillTodo: BackfillTodoRepository?,
|
||||
private val mapper: ObjectMapper,
|
||||
) {
|
||||
fun apply(head: ProcState, msg: DecodedMessage, payload: FlopPayload): ApplyResult = txManager.inTransaction {
|
||||
lock.lock()
|
||||
val deleted = flightState.markDeleted(payload.flid, msgId = head.msgId, now = Instant.now())
|
||||
if (deleted) {
|
||||
val current = flightState.loadFullSnapshot(payload.flid)
|
||||
// tombstone 仅在 ACTIVE→DELETED 时登记(§7.3),与删除同事务
|
||||
msgEvents.insertAll(
|
||||
listOf(
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_SCHD,
|
||||
partitionKey = payload.flid,
|
||||
eventType = EventType.TOMBSTONE,
|
||||
stateVersion = current?.stateVersion ?: 0L,
|
||||
payloadJson = mapper.writeValueAsString(
|
||||
mapOf(
|
||||
"flid" to payload.flid,
|
||||
"stateVersion" to (current?.stateVersion ?: 0L),
|
||||
"deleted" to true,
|
||||
),
|
||||
),
|
||||
),
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_MSG,
|
||||
partitionKey = payload.flid,
|
||||
stateVersion = current?.stateVersion ?: 0L,
|
||||
payloadJson = mapper.writeValueAsString(
|
||||
mapOf("flid" to payload.flid, "stateVersion" to (current?.stateVersion ?: 0L), "deleted" to true),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
preRegisterBackfill(head, msg, backfillTodo)
|
||||
}
|
||||
ApplyResult.Succeeded // 未命中 = 迟到/重复,幂等成功(§6.2 步骤 3/4)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ADFT(§6.3 + §2.1):字段缺失语义待确认——确认前按保守 Set-only 处理
|
||||
* (出现字段覆盖,缺失不 Clear,不沿用 FLOP 全量合并规则)。
|
||||
* FLID 已存在且 DELETED → 生命周期重激活;不存在 → 新实例建立(含运营日计算 §3.5)。
|
||||
*/
|
||||
@Singleton
|
||||
class AdftProcessor(
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val lock: PipelineLockRepository,
|
||||
private val flightState: FlightStateRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val backfillTodo: BackfillTodoRepository?,
|
||||
operationDayProps: OperationDayProps,
|
||||
private val mapper: ObjectMapper,
|
||||
) {
|
||||
private val opDay = OperationDayCalculator(
|
||||
zone = runCatching { ZoneId.of(operationDayProps.zone) }.getOrElse { ZoneId.of("Asia/Shanghai") },
|
||||
cutoffHour = operationDayProps.cutoffHour,
|
||||
)
|
||||
|
||||
fun apply(head: ProcState, msg: DecodedMessage, record: com.gzzn.omms.msgexchange.domain.flight.ScheduleRecord): ApplyResult =
|
||||
txManager.inTransaction {
|
||||
lock.lock()
|
||||
val main = flightState.findMainRow(record.flid)
|
||||
if (main != null && main.state == com.gzzn.omms.msgexchange.domain.flight.FlightState.DELETED) {
|
||||
// §6.3 重激活:DELETED → ACTIVE,推进版本,登记状态事件
|
||||
if (flightState.revive(record.flid, msgId = head.msgId, now = Instant.now())) {
|
||||
val current = flightState.loadFullSnapshot(record.flid)
|
||||
if (current != null) {
|
||||
val next = FlightStateEngine.mergedState(current, setOnly(record))
|
||||
flightState.persistFullState(next, msgId = head.msgId, now = Instant.now())
|
||||
msgEvents.insertAll(eventsFor(next, mapper))
|
||||
}
|
||||
}
|
||||
preRegisterBackfill(head, msg, backfillTodo)
|
||||
return@inTransaction ApplyResult.Succeeded
|
||||
}
|
||||
|
||||
val current = flightState.loadFullSnapshot(record.flid)
|
||||
val next: FlightSnapshot = if (current == null) {
|
||||
// 新实例建立:ADFT 含 SODT 时直接计算运营日(§2.1),不可算则置 null 待快照收录
|
||||
val day = opDay.compute(record.scalars["SODT"])
|
||||
FlightSnapshot(
|
||||
flid = record.flid,
|
||||
operationDay = day, // 待确认项 §2.1:不可算时不得默认写接收日
|
||||
state = com.gzzn.omms.msgexchange.domain.flight.FlightState.ACTIVE,
|
||||
stateVersion = 1L,
|
||||
scalars = record.scalars,
|
||||
collections = com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine.COLLECTION_KEYS.associateWith { key ->
|
||||
record.collections[key] ?: emptyList()
|
||||
},
|
||||
)
|
||||
} else {
|
||||
FlightStateEngine.mergedState(current, setOnly(record))
|
||||
}
|
||||
val outcome = flightState.persistFullState(next, msgId = head.msgId, now = Instant.now())
|
||||
check(outcome != com.gzzn.omms.msgexchange.infra.persistence.PersistOutcome.DAY_GUARD_VIOLATION) {
|
||||
"operation-day guard violated flid=${record.flid}"
|
||||
}
|
||||
msgEvents.insertAll(eventsFor(next, mapper))
|
||||
preRegisterBackfill(head, msg, backfillTodo)
|
||||
ApplyResult.Succeeded
|
||||
}
|
||||
|
||||
/** §2.1 保守语义:仅出现字段 Set;集合出现 Replace、缺失保留。 */
|
||||
private fun setOnly(record: com.gzzn.omms.msgexchange.domain.flight.ScheduleRecord) = MergeChange(
|
||||
flid = record.flid,
|
||||
scalars = record.scalars,
|
||||
collections = record.collections,
|
||||
)
|
||||
}
|
||||
|
||||
// =====================================================================
|
||||
// 共享小工具(处理器层私有约定)
|
||||
// =====================================================================
|
||||
|
||||
/** 航班不存在/迟到:幂等成功(§9 队头不阻塞;不创建实例——创建入口只有 SCHD/ADFT)。 */
|
||||
private fun idempotentAbsent(head: ProcState, msg: DecodedMessage): ApplyResult = ApplyResult.Succeeded
|
||||
|
||||
/** KAFKA_SCHD 整态 + KAFKA_MSG 变化通知(§7.3)。 */
|
||||
internal fun eventsFor(next: FlightSnapshot, mapper: ObjectMapper): List<MsgEvent> {
|
||||
val payload = linkedMapOf<String, Any>(
|
||||
"flid" to next.flid,
|
||||
"stateVersion" to next.stateVersion,
|
||||
"scalars" to next.scalars,
|
||||
"collections" to next.collections,
|
||||
)
|
||||
return listOf(
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_SCHD,
|
||||
partitionKey = next.flid,
|
||||
stateVersion = next.stateVersion,
|
||||
payloadJson = mapper.writeValueAsString(payload),
|
||||
),
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_MSG,
|
||||
partitionKey = next.flid,
|
||||
stateVersion = next.stateVersion,
|
||||
payloadJson = mapper.writeValueAsString(mapOf("flid" to next.flid, "stateVersion" to next.stateVersion)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/** §7.2 业务事务内预登记回填待办(与终态同事务)。 */
|
||||
internal fun preRegisterBackfill(head: ProcState, msg: DecodedMessage, backfillTodo: BackfillTodoRepository?) {
|
||||
backfillTodo?.record(
|
||||
BackfillTodoRepository.BackfillTask(
|
||||
msgId = head.msgId, sndr = msg.meta.sndr, type = msg.meta.type,
|
||||
styp = msg.meta.styp, seqn = msg.meta.seqn,
|
||||
),
|
||||
null,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
package com.gzzn.omms.msgexchange.processing
|
||||
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.Decision
|
||||
import com.gzzn.omms.msgexchange.domain.MsgKind
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 2:Handler = 纯函数(状态与报文进,Decision 出,不碰 Redis/Kafka)。
|
||||
* 32 个 Handler(flop 29 + schd 3)翻译属阶段 2/3,逐条对照 ACMA-4 基线与兼容矩阵 KEEP/FIX。
|
||||
*/
|
||||
interface Handler {
|
||||
val kind: MsgKind
|
||||
|
||||
/**
|
||||
* 在线状态以只读视图传入:FLID → 字段集(field → value,与 legacy flightInfo hash /
|
||||
* hgetAllFlightInfo 同构;阶段 A 由调用方点查自有库 FLIGHT_SCHD 宽表装配,ACM2-28 定案)。
|
||||
*/
|
||||
fun decide(flightView: Map<String, Map<String, String>>, msg: DecodedMessage): Decision
|
||||
}
|
||||
|
||||
/**
|
||||
* sealed 穷尽分派(ACMA-6 选型:取代 legacy 反射 get{TYPE}())。
|
||||
*/
|
||||
class HandlerRegistry(handlers: List<Handler>) {
|
||||
private val byKind: Map<String, Handler> = handlers.associateBy { keyOf(it.kind) }
|
||||
|
||||
fun dispatcherFor(msg: DecodedMessage): Handler? = byKind[keyOf(msg.kind)]
|
||||
|
||||
companion object {
|
||||
fun keyOf(kind: MsgKind): String = when (kind) {
|
||||
is MsgKind.Schd -> "SCHD-${kind.subtype.name}"
|
||||
is MsgKind.Flop -> "FLOP-${kind.subtype}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(阶段2/3): 注册 3+29 Handler;本阶段仅含骨架(阶段 2 最小纵向链路先做 1 SCHD(DNLD) + 1 FLOP)。
|
||||
@@ -1,37 +1,32 @@
|
||||
package com.gzzn.omms.msgexchange.processing
|
||||
|
||||
import com.gzzn.omms.msgexchange.codec.FlopPayload
|
||||
import com.gzzn.omms.msgexchange.codec.JacksonXmlCodec
|
||||
import com.gzzn.omms.msgexchange.codec.ScheduleBody
|
||||
import com.gzzn.omms.msgexchange.config.PipelineProps
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine
|
||||
import com.gzzn.omms.msgexchange.domain.ErrorClass
|
||||
import com.gzzn.omms.msgexchange.domain.MsgEvent
|
||||
import com.gzzn.omms.msgexchange.domain.MsgKind
|
||||
import com.gzzn.omms.msgexchange.domain.ProcState
|
||||
import com.gzzn.omms.msgexchange.domain.ProcStatus
|
||||
import com.gzzn.omms.msgexchange.domain.Targets
|
||||
import com.gzzn.omms.msgexchange.infra.log.TraceLog
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.BackfillTodoRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.CminmsgInboxRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.ProcStateRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PumpJobRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightSchdRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineTransactionManager
|
||||
import com.gzzn.omms.msgexchange.infra.retry.ProcFailure
|
||||
import com.gzzn.omms.msgexchange.jobs.JobExecutor
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 2:处理主泵——严格 FIFO + HOL + 毒丸 DEAD 升级(I1);
|
||||
* 阶段 A 运营航班 FLIGHT_SCHD 与待发事件、终态同事务原子提交(I2/I5,ACM2-28 定案)。
|
||||
* 处理主泵(docs/flight-state.md §1 目标 2):单活动主泵严格 FIFO + HOL + 毒丸 DEAD 升级;
|
||||
* 航班状态、事件、处理终态在处理器事务内原子提交(§1 目标 3)。
|
||||
*/
|
||||
@Singleton
|
||||
class Pump(
|
||||
private val procState: ProcStateRepository,
|
||||
private val pumpJobs: PumpJobRepository,
|
||||
private val inbox: CminmsgInboxRepository,
|
||||
private val processor: MessageProcessor,
|
||||
private val jobExecutor: JobExecutor,
|
||||
private val props: PipelineProps,
|
||||
) {
|
||||
private val log = org.slf4j.LoggerFactory.getLogger(Pump::class.java)
|
||||
@@ -39,7 +34,7 @@ class Pump(
|
||||
@Volatile
|
||||
private var running = true
|
||||
|
||||
/** 优雅停机:loop 在当前 tick 收尾后退出;线程中断由 Runner 负责。 */
|
||||
/** 优雅停机:loop 在当前 tick 收尾后退出;线程中断由 PipelineLifecycle 负责。 */
|
||||
fun stop() {
|
||||
running = false
|
||||
}
|
||||
@@ -52,27 +47,23 @@ class Pump(
|
||||
Thread.currentThread().interrupt()
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
// U08:loop 只作最后防线——失败状态迁移已在 processOne/execute 的边界内完成;
|
||||
// 致命 Error 不在此捕获(任其终止进程,保证“异常必可见”)。
|
||||
// 最后防线:失败状态迁移已在 processOne 边界内完成;致命 Error 不捕获
|
||||
sleepQuietly(props.pipeline.pollInterval)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal fun tick() {
|
||||
val job = pumpJobs.headQueued()
|
||||
val head = procState.headUnfinished()
|
||||
|
||||
// 统一 FIFO:JOB 与消息同队列语义(定时任务产物不绕过队头顺序,决策 1)。
|
||||
val nextJob = job?.takeIf { jobBefore(job, head) }
|
||||
when {
|
||||
nextJob != null -> execute(nextJob)
|
||||
head == null -> sleepQuietly(props.pipeline.pollInterval)
|
||||
head.state == ProcStatus.FAILED && (head.nextAttemptAt ?: Instant.EPOCH) > Instant.now() ->
|
||||
if (poisoned(head)) {
|
||||
log.error("poison -> DEAD id={} attempts={} lastError={}", head.cminmsgsId, head.attempts, head.lastError)
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD,
|
||||
errorClass = ErrorClass.EXHAUSTED, lastError = head.lastError ?: "head-deadline-exceeded")
|
||||
log.error("poison -> DEAD msgId={} attempts={} lastError={}", head.msgId, head.attempts, head.lastError)
|
||||
procState.update(
|
||||
head.msgId, ProcStatus.DEAD,
|
||||
errorClass = ErrorClass.EXHAUSTED, lastError = head.lastError ?: "head-deadline-exceeded",
|
||||
)
|
||||
} else {
|
||||
sleepQuietly(Duration.between(Instant.now(), head.nextAttemptAt))
|
||||
}
|
||||
@@ -85,69 +76,41 @@ class Pump(
|
||||
head.attempts >= props.pipeline.maxAttempts ||
|
||||
Duration.between(head.updatedAt, Instant.now()) > props.pipeline.headDeadline
|
||||
|
||||
/** JOB 与队头消息的先后由入队时间近似;实装以统一序号列保证(阶段 1 后续)。 */
|
||||
private fun jobBefore(job: PumpJobRepository.Job, head: ProcState?) =
|
||||
head == null || head.state == ProcStatus.FAILED
|
||||
|
||||
private fun execute(job: PumpJobRepository.Job) {
|
||||
pumpJobs.markRunning(job.jobId)
|
||||
try {
|
||||
jobExecutor.execute(job)
|
||||
pumpJobs.markDone(job.jobId)
|
||||
} catch (e: Exception) {
|
||||
pumpJobs.markFailed(job.jobId, e.message ?: "unknown")
|
||||
}
|
||||
}
|
||||
|
||||
private fun sleepQuietly(d: Duration) {
|
||||
if (!d.isNegative && !d.isZero) Thread.sleep(d.toMillis().coerceAtLeast(1))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 2 processOne:解码→绑定→纯函数决策→提交。
|
||||
* U08/U10/U11:失败状态迁移全部在“持有具体 head”的边界内完成——
|
||||
* 任何意外异常 → FAILED(INFRA)+退避(ProcFailure);attempts 达上限 → DEAD(EXHAUSTED);
|
||||
* MALFORMED 直接 DEAD;InterruptedException 恢复中断位并上抛(不被普通恢复逻辑吞掉)。
|
||||
* processOne:解码 → 绑定 → 处理器(事务内决策+落库)→ 终态迁移 → 回填。
|
||||
* 边界化失败迁移(ProcFailure):任何意外异常归于本条 head,FAILED(INFRA)+退避,不穿出杀泵;
|
||||
* MALFORMED / PROTOCOL 直接 DEAD 不重试(§9)。
|
||||
*/
|
||||
@Singleton
|
||||
class MessageProcessor(
|
||||
private val inbox: CminmsgInboxRepository,
|
||||
private val procState: ProcStateRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val codecHolder: CodecHolder,
|
||||
private val handlers: HandlerHolder,
|
||||
private val flightSchd: FlightSchdRepository,
|
||||
private val snapshotFlow: SnapshotFlow,
|
||||
private val codec: JacksonXmlCodec,
|
||||
private val scheduleProcessor: ScheduleProcessor,
|
||||
private val flopProcessor: FlopProcessor,
|
||||
private val fdelProcessor: FdelProcessor,
|
||||
private val adftProcessor: AdftProcessor,
|
||||
private val procFailure: ProcFailure,
|
||||
private val props: PipelineProps,
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val backfillTodo: BackfillTodoRepository? = null,
|
||||
) {
|
||||
private val log = org.slf4j.LoggerFactory.getLogger(MessageProcessor::class.java)
|
||||
private val flidRegex = Regex("<FLID>(.*?)</FLID>", RegexOption.IGNORE_CASE)
|
||||
|
||||
private fun extractCandidateFlids(decoded: DecodedMessage): Set<String> {
|
||||
val fromXml = flidRegex.findAll(decoded.rawXml).map { it.groupValues[1].trim() }.filter { it.isNotEmpty() }.toSet()
|
||||
if (fromXml.isNotEmpty()) return fromXml
|
||||
val b = decoded.body
|
||||
if (b is Map<*, *>) {
|
||||
val flid = b["FLID"] ?: b["flid"]
|
||||
if (flid != null) return setOf(flid.toString())
|
||||
}
|
||||
return emptySet()
|
||||
}
|
||||
|
||||
fun processOne(head: ProcState) {
|
||||
com.gzzn.omms.msgexchange.infra.log.TraceLog.withTrace(head.cminmsgsId) {
|
||||
TraceLog.withTrace(head.msgId) {
|
||||
try {
|
||||
processInternal(head)
|
||||
} catch (e: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
// U08:边界化——异常归于本条 head,写 FAILED/DEAD,而不是穿出杀 pump
|
||||
log.warn("processOne unexpected failure id={} ec=INFRA msg={}", head.cminmsgsId, e.message ?: e.javaClass.simpleName)
|
||||
// 边界化:异常归于本条 head,写 FAILED(INFRA)/DEAD,而不是穿出杀 pump
|
||||
log.warn("processOne unexpected failure msgId={} ec=INFRA msg={}", head.msgId, e.message ?: e.javaClass.simpleName)
|
||||
procFailure.fail(head, ErrorClass.INFRA, e.message ?: e.javaClass.simpleName)
|
||||
}
|
||||
}
|
||||
@@ -156,126 +119,134 @@ class MessageProcessor(
|
||||
private fun processInternal(head: ProcState) {
|
||||
// 守卫:手工/遗留 FAILED 行若 attempts 已达上限,直接终态(防止退避到期后无限重试)
|
||||
if (head.state == ProcStatus.FAILED && procFailure.scheduler.exhausted(head.attempts)) {
|
||||
log.error("head exhausted at entry -> DEAD id={} attempts={}", head.cminmsgsId, head.attempts)
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.EXHAUSTED,
|
||||
lastError = head.lastError ?: "max-attempts")
|
||||
log.error("head exhausted at entry -> DEAD msgId={} attempts={}", head.msgId, head.attempts)
|
||||
procState.update(head.msgId, ProcStatus.DEAD, errorClass = ErrorClass.EXHAUSTED, lastError = head.lastError ?: "max-attempts")
|
||||
return
|
||||
}
|
||||
|
||||
val raw = inbox.rawOf(head.cminmsgsId) ?: run {
|
||||
log.error("raw missing -> DEAD(MALFORMED) id={}", head.cminmsgsId)
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED, lastError = "raw-missing")
|
||||
val raw = inbox.rawOf(head.msgId) ?: run {
|
||||
log.error("raw missing -> DEAD(MALFORMED) msgId={}", head.msgId)
|
||||
procState.update(head.msgId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED, lastError = "raw-missing")
|
||||
return
|
||||
}
|
||||
val decoded = when (val r = codecHolder.codec.decode(raw)) {
|
||||
val decoded = when (val r = codec.decode(raw)) {
|
||||
is com.gzzn.omms.msgexchange.codec.DecodeResult.Ok -> r.message
|
||||
is com.gzzn.omms.msgexchange.codec.DecodeResult.Err -> {
|
||||
// T06(U11):MALFORMED(报文非法)→ DEAD 不重试;CODEC_ERROR(可随 codec 修复重放)→ FAILED 退避
|
||||
// MALFORMED(报文非法)→ DEAD 不重试;CODEC_ERROR(可随 codec 修复重放)→ FAILED 退避
|
||||
if (r.failure.errorClass == ErrorClass.MALFORMED) {
|
||||
log.error("decode MALFORMED -> DEAD id={} detail={}", head.cminmsgsId, r.failure.detail)
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED,
|
||||
lastError = r.failure.detail)
|
||||
log.error("decode MALFORMED -> DEAD msgId={} detail={}", head.msgId, r.failure.detail)
|
||||
procState.update(head.msgId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED, lastError = r.failure.detail)
|
||||
} else {
|
||||
log.warn("decode {} -> FAILED id={} detail={}", r.failure.errorClass, head.cminmsgsId, r.failure.detail)
|
||||
log.warn("decode {} -> FAILED msgId={} detail={}", r.failure.errorClass, head.msgId, r.failure.detail)
|
||||
procFailure.fail(head, r.failure.errorClass, r.failure.detail)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// I3:identity 仅首次绑定(head.identityKey == null);FAILED 重试不重绑(N25:入参传递 head,不再二次查询)
|
||||
// I3:identity 仅首次绑定(head.identityKey == null);FAILED 重试不重绑
|
||||
if (head.identityKey == null) {
|
||||
val identity = Identity.of(decoded, props.identity)
|
||||
if (!procState.tryBindIdentity(head.cminmsgsId, identity)) {
|
||||
if (!procState.tryBindIdentity(head.msgId, identity)) {
|
||||
val owner = procState.ownerOfIdentity(identity) ?: -1L
|
||||
log.info("duplicate-of:{} -> SKIPPED id={}", owner, head.cminmsgsId)
|
||||
procState.update(head.cminmsgsId, ProcStatus.SKIPPED, lastError = "duplicate-of:$owner")
|
||||
// 重复报文自身也是一条信箱记录:同样回填共享信箱,防止被反复轮询(失败落补偿)
|
||||
log.info("duplicate-of:{} -> SKIPPED msgId={}", owner, head.msgId)
|
||||
procState.update(head.msgId, ProcStatus.SKIPPED, lastError = "duplicate-of:$owner")
|
||||
compensateBackfill(head, decoded)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 快照消息走专属流程(流程 4:staging → SQL 批处理/域内差删/CAS 同事务)
|
||||
val handler = handlers.registry.dispatcherFor(decoded)
|
||||
if (handler == null) {
|
||||
// U10(N21):未注册 ≠ 报文非法——写 FAILED(可重放),绝不写终态
|
||||
log.warn("no-handler:{} -> FAILED(UNSUPPORTED) id={}", decoded.typeTag, head.cminmsgsId)
|
||||
procFailure.fail(head, ErrorClass.UNSUPPORTED, "no-handler:${decoded.typeTag}")
|
||||
return
|
||||
}
|
||||
val schdKind = decoded.kind as? com.gzzn.omms.msgexchange.domain.MsgKind.Schd
|
||||
if (schdKind != null &&
|
||||
schdKind.subtype == com.gzzn.omms.msgexchange.domain.MsgKind.SchdSubtype.DNLD
|
||||
) {
|
||||
snapshotFlow.publishSnapshot(head, decoded)
|
||||
return
|
||||
}
|
||||
|
||||
val candidateFlids = extractCandidateFlids(decoded)
|
||||
val flightView = if (candidateFlids.isNotEmpty()) {
|
||||
flightSchd.findByFlids(candidateFlids)
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
|
||||
val decision = handler.decide(flightView, decoded) // 纯函数
|
||||
|
||||
// 事务 2(自有 PG 单事务原子):
|
||||
// upsert FLIGHT_SCHD(decision.flightChanges) + insert MSG_EVENT + PROC_STATE → SUCCEEDED
|
||||
val events = buildList {
|
||||
decision.msgNotifies.forEach { add(MsgEvent(target = Targets.KAFKA_MSG, payloadJson = it.payloadJson)) }
|
||||
decision.schdPush.forEach { add(MsgEvent(target = Targets.KAFKA_SCHD, partitionKey = it.flid, payloadJson = it.payloadJson)) }
|
||||
}
|
||||
|
||||
val messageId = head.cminmsgsId.toString()
|
||||
txManager.inTransaction {
|
||||
if (decision.flightChanges.isNotEmpty()) {
|
||||
val nextStates = decision.flightChanges.map { change ->
|
||||
val current = flightSchd.findNextStateByFlid(change.flid)
|
||||
val commands = FlightStateEngine.commandsFromFields(change.flid, change.fields)
|
||||
FlightStateEngine.apply(current, commands, messageId, bumpVersion = true)
|
||||
// 处理器分派:SCHD 快照主链路(§5.1)/ FLOP / FDEL / ADFT;缺载荷按 MALFORMED 终态
|
||||
val result: ApplyResult = when (val kind = decoded.kind) {
|
||||
is MsgKind.Schd -> {
|
||||
val body = decoded.body as? ScheduleBody
|
||||
if (body == null) {
|
||||
deadMalformed(head, "missing-schd-body")
|
||||
return
|
||||
}
|
||||
when (kind.subtype) {
|
||||
MsgKind.SchdSubtype.DNLD, MsgKind.SchdSubtype.RESP ->
|
||||
scheduleProcessor.applyScheduleRecords(head, decoded)
|
||||
MsgKind.SchdSubtype.ADFT -> {
|
||||
val record = body.records.singleOrNull()
|
||||
if (record == null) {
|
||||
deadMalformed(head, "adft-needs-single-fltr")
|
||||
return
|
||||
}
|
||||
adftProcessor.apply(head, decoded, record)
|
||||
}
|
||||
}
|
||||
flightSchd.persistNextStates(null, nextStates, snapshotReplace = false)
|
||||
}
|
||||
if (events.isNotEmpty()) {
|
||||
msgEvents.insertAll(events)
|
||||
MsgKind.Fdel -> {
|
||||
val payload = decoded.body as? FlopPayload
|
||||
if (payload == null) {
|
||||
deadMalformed(head, "missing-fdel-flid")
|
||||
return
|
||||
}
|
||||
fdelProcessor.apply(head, decoded, payload)
|
||||
}
|
||||
is MsgKind.Flop -> {
|
||||
val payload = decoded.body as? FlopPayload
|
||||
if (payload == null) {
|
||||
deadMalformed(head, "missing-flop-body")
|
||||
return
|
||||
}
|
||||
flopProcessor.apply(head, decoded, payload)
|
||||
}
|
||||
is MsgKind.Unsupported -> {
|
||||
// §9:未支持类型 → FAILED(UNSUPPORTED) 退避重试,达阈值转 DEAD;绝不写终态
|
||||
log.warn("unsupported type -> FAILED(UNSUPPORTED) msgId={} tag={}", head.msgId, kind.tag)
|
||||
procFailure.fail(head, ErrorClass.UNSUPPORTED, "no-handler:${kind.tag}")
|
||||
return
|
||||
}
|
||||
procState.update(head.cminmsgsId, ProcStatus.SUCCEEDED)
|
||||
}
|
||||
|
||||
try {
|
||||
inbox.backfillOnSuccess(head.cminmsgsId, decoded.meta.sndr, decoded.meta.type, decoded.meta.styp, decoded.meta.seqn)
|
||||
} catch (e: Exception) {
|
||||
log.error("backfill failed after SUCCEEDED id={} (compensation required)", head.cminmsgsId, e)
|
||||
recordBackfillTodo(head.cminmsgsId, decoded, e)
|
||||
when (result) {
|
||||
is ApplyResult.Succeeded, ApplyResult.ReplaySkipped ->
|
||||
procState.update(head.msgId, ProcStatus.SUCCEEDED)
|
||||
is ApplyResult.DeadProtocol -> {
|
||||
// §9:整包拒绝 DEAD(PROTOCOL),立即释放队头,交人工确认
|
||||
log.error("DEAD(PROTOCOL) msgId={} reason={} flags={}", head.msgId, result.reason, result.flags)
|
||||
procState.update(head.msgId, ProcStatus.DEAD, errorClass = ErrorClass.PROTOCOL, lastError = result.reason.take(1000))
|
||||
compensateBackfill(head, decoded) // 拒绝包同样要回填信箱,防止反复轮询
|
||||
return
|
||||
}
|
||||
}
|
||||
log.info("SUCCEEDED id={} events={} flightChanges={}", head.cminmsgsId, events.size, decision.flightChanges.size)
|
||||
|
||||
backfill(head, decoded)
|
||||
log.info("SUCCEEDED msgId={} kind={}", head.msgId, decoded.typeTag)
|
||||
}
|
||||
|
||||
/** v2 §5:回填失败只落补偿待办(成功终态不降级,业务不重放);由 BackfillSweepJob 到期重试。 */
|
||||
/** §7.2:提交后回填共享信箱;失败不得把 SUCCEEDED 改回 FAILED,待办已事务内预登记。 */
|
||||
private fun backfill(head: ProcState, decoded: DecodedMessage) {
|
||||
try {
|
||||
inbox.backfillOnSuccess(head.msgId, decoded.meta.sndr, decoded.meta.type, decoded.meta.styp, decoded.meta.seqn)
|
||||
backfillTodo?.delete(head.msgId)
|
||||
} catch (e: Exception) {
|
||||
log.error("backfill failed after SUCCEEDED msgId={} (todo pre-registered, sweep will retry)", head.msgId, e)
|
||||
}
|
||||
}
|
||||
|
||||
/** SKIPPED/DEAD(PROTOCOL) 包的回填(无预登记待办):失败落补偿待办。 */
|
||||
private fun compensateBackfill(head: ProcState, decoded: DecodedMessage) {
|
||||
try {
|
||||
inbox.backfillOnSuccess(head.cminmsgsId, decoded.meta.sndr, decoded.meta.type, decoded.meta.styp, decoded.meta.seqn)
|
||||
inbox.backfillOnSuccess(head.msgId, decoded.meta.sndr, decoded.meta.type, decoded.meta.styp, decoded.meta.seqn)
|
||||
} catch (e: Exception) {
|
||||
log.error("backfill failed for SKIPPED duplicate id={} (compensation required)", head.cminmsgsId, e)
|
||||
recordBackfillTodo(head.cminmsgsId, decoded, e)
|
||||
log.error("backfill failed msgId={} (compensation required)", head.msgId, e)
|
||||
backfillTodo?.record(
|
||||
BackfillTodoRepository.BackfillTask(
|
||||
msgId = head.msgId,
|
||||
sndr = decoded.meta.sndr,
|
||||
type = decoded.meta.type,
|
||||
styp = decoded.meta.styp,
|
||||
seqn = decoded.meta.seqn,
|
||||
),
|
||||
e.message ?: e.javaClass.simpleName,
|
||||
) ?: log.warn("no backfill-todo repository bound; compensation NOT persisted msgId={}", head.msgId)
|
||||
}
|
||||
}
|
||||
|
||||
private fun recordBackfillTodo(cminmsgsId: Long, decoded: DecodedMessage, e: Exception) {
|
||||
backfillTodo?.record(
|
||||
BackfillTodoRepository.BackfillTask(
|
||||
cminmsgsId = cminmsgsId,
|
||||
sndr = decoded.meta.sndr,
|
||||
type = decoded.meta.type,
|
||||
styp = decoded.meta.styp,
|
||||
seqn = decoded.meta.seqn,
|
||||
),
|
||||
e.message ?: e.javaClass.simpleName,
|
||||
) ?: log.warn("no backfill-todo repository bound; compensation NOT persisted id={}", cminmsgsId)
|
||||
private fun deadMalformed(head: ProcState, detail: String) {
|
||||
procState.update(head.msgId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED, lastError = detail)
|
||||
}
|
||||
}
|
||||
|
||||
/** 延迟装配占位(阶段 1 后续以 Micronaut Bean 替换直连构造)。 */
|
||||
class CodecHolder(val codec: com.gzzn.omms.msgexchange.codec.XmlCodec)
|
||||
class HandlerHolder(val registry: HandlerRegistry)
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
package com.gzzn.omms.msgexchange.processing
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.gzzn.omms.msgexchange.codec.ScheduleBody
|
||||
import com.gzzn.omms.msgexchange.config.OperationDayProps
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.MsgEvent
|
||||
import com.gzzn.omms.msgexchange.domain.ProcState
|
||||
import com.gzzn.omms.msgexchange.domain.ProcStatus
|
||||
import com.gzzn.omms.msgexchange.domain.SnapshotFlag
|
||||
import com.gzzn.omms.msgexchange.domain.SnapshotLogEntry
|
||||
import com.gzzn.omms.msgexchange.domain.SnapshotResult
|
||||
import com.gzzn.omms.msgexchange.domain.Targets
|
||||
import com.gzzn.omms.msgexchange.domain.OperationDayCalculator
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightSnapshot
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightState
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine
|
||||
import com.gzzn.omms.msgexchange.domain.flight.ScheduleRecord
|
||||
import com.gzzn.omms.msgexchange.domain.flight.SnapshotValidation
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.BackfillTodoRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightStateRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PersistOutcome
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineLockRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineTransactionManager
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.ProcStateRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.SnapshotLogRepository
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Instant
|
||||
import java.time.LocalDate
|
||||
import java.time.ZoneId
|
||||
|
||||
/** 处理器执行结果——终态迁移由 MessageProcessor 统一落库。 */
|
||||
sealed interface ApplyResult {
|
||||
/** 业务成功(含幂等成功)。 */
|
||||
data object Succeeded : ApplyResult
|
||||
|
||||
/** §5.1 步骤 2:MSG_ID 已有成功终态 → 重放,直接记幂等成功。 */
|
||||
data object ReplaySkipped : ApplyResult
|
||||
|
||||
/** §9:整包拒绝 DEAD(PROTOCOL),不重试,交人工确认。 */
|
||||
data class DeadProtocol(val reason: String, val flags: Set<SnapshotFlag> = emptySet()) : ApplyResult
|
||||
}
|
||||
|
||||
/** §5.3 第四行:归属日不符 = 串日/错发/污染,整包拒绝。 */
|
||||
class ProtocolViolation(message: String) : RuntimeException(message)
|
||||
|
||||
/**
|
||||
* SCHD 快照主链路(docs/flight-state.md §5.1 applyScheduleRecords,同一事务):
|
||||
* 对单日快照与滚动窗口统一适用,不做名单层面的处理。
|
||||
*/
|
||||
@Singleton
|
||||
class ScheduleProcessor(
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val lock: PipelineLockRepository,
|
||||
private val procState: ProcStateRepository,
|
||||
private val flightState: FlightStateRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val snapshotLog: SnapshotLogRepository,
|
||||
private val backfillTodo: BackfillTodoRepository?,
|
||||
operationDayProps: OperationDayProps,
|
||||
private val mapper: ObjectMapper,
|
||||
) {
|
||||
private val opDay = OperationDayCalculator(
|
||||
zone = runCatching { ZoneId.of(operationDayProps.zone) }.getOrElse { ZoneId.of("Asia/Shanghai") },
|
||||
cutoffHour = operationDayProps.cutoffHour,
|
||||
)
|
||||
|
||||
fun applyScheduleRecords(head: ProcState, msg: DecodedMessage): ApplyResult {
|
||||
val body = msg.body as? ScheduleBody ?: return ApplyResult.DeadProtocol("missing-schd-body")
|
||||
val started = System.nanoTime()
|
||||
|
||||
// ② 重放判定:MSG_ID 已有成功终态 → 幂等成功(§5.1 步骤 2,重放也记留痕)
|
||||
if (procState.findSuccessTerminal(head.msgId)) {
|
||||
logSnapshot(head, body, SnapshotResult.REPLAY_SKIPPED, upserted = 0, flags = emptySet(), started)
|
||||
return ApplyResult.ReplaySkipped
|
||||
}
|
||||
|
||||
// ③ 报文完整性(§5.2 五项):任一失败整包不落地 → DEAD(PROTOCOL)
|
||||
val validation = FlightStateEngine.validateMessage(
|
||||
recsDeclared = body.recsDeclared,
|
||||
records = body.records,
|
||||
scopeStart = body.scopeStart,
|
||||
scopeEnd = body.scopeEnd,
|
||||
opDay = opDay,
|
||||
)
|
||||
if (validation is SnapshotValidation.Invalid) {
|
||||
logSnapshot(head, body, SnapshotResult.ROLLED_BACK, upserted = 0, validation.flags, started)
|
||||
return ApplyResult.DeadProtocol(validation.reason, validation.flags)
|
||||
}
|
||||
val ok = validation as SnapshotValidation.Ok
|
||||
|
||||
if (ok.perRecordDay.isEmpty()) {
|
||||
// 空快照:合法但无写入(§5.5 EMPTY),仍算成功终态
|
||||
logSnapshot(head, body, SnapshotResult.COMMITTED, upserted = 0, setOf(SnapshotFlag.EMPTY), started)
|
||||
return ApplyResult.Succeeded
|
||||
}
|
||||
|
||||
val flags = linkedSetOf<SnapshotFlag>()
|
||||
return try {
|
||||
// ①④⑤⑥⑦ 同一事务:锁 → 归属校验 → upsert → 版本/事件/待办预登记/终态
|
||||
val upserted = txManager.inTransaction {
|
||||
lock.lock()
|
||||
|
||||
// ⑤ 归属校验(§5.3):OPERATION_DAY 不可变,批量点查避免逐航班往返
|
||||
val mains = flightState.findMainRows(ok.perRecordDay.keys)
|
||||
ok.perRecordDay.forEach { (flid, day) ->
|
||||
val existing = mains[flid] ?: return@forEach
|
||||
if (existing.operationDay != null && existing.operationDay != day) {
|
||||
throw ProtocolViolation(
|
||||
"SAME_FLID_ACROSS_OPERATION_DAYS flid=$flid existing=${existing.operationDay} incoming=$day",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
var written = 0
|
||||
val events = mutableListOf<MsgEvent>()
|
||||
ok.perRecordDay.forEach { (flid, day) ->
|
||||
val record = body.records.first { it.flid == flid }
|
||||
val existingMain = mains[flid]
|
||||
val keepDeleted = existingMain?.state == FlightState.DELETED
|
||||
if (keepDeleted) flags.add(SnapshotFlag.SCHD_REVIVE_CONFLICT) // §5.1 步骤 6:不恢复
|
||||
val current = if (existingMain != null) flightState.loadFullSnapshot(flid) else null
|
||||
val next = FlightStateEngine.snapshotState(
|
||||
current = current,
|
||||
record = record,
|
||||
operationDay = day,
|
||||
keepDeleted = keepDeleted,
|
||||
)
|
||||
when (flightState.persistFullState(next, msgId = head.msgId, now = Instant.now())) {
|
||||
PersistOutcome.DAY_GUARD_VIOLATION ->
|
||||
throw ProtocolViolation("operation-day guard violated flid=$flid")
|
||||
else -> written++
|
||||
}
|
||||
events += snapshotEvents(next)
|
||||
}
|
||||
if (events.isNotEmpty()) msgEvents.insertAll(events)
|
||||
// §7.2 目标形态:业务事务内预登记回填待办(提交后由 MessageProcessor 回填并删待办)
|
||||
backfillTodo?.record(
|
||||
BackfillTodoRepository.BackfillTask(
|
||||
msgId = head.msgId, sndr = msg.meta.sndr, type = msg.meta.type,
|
||||
styp = msg.meta.styp, seqn = msg.meta.seqn,
|
||||
),
|
||||
null,
|
||||
)
|
||||
written
|
||||
}
|
||||
logSnapshot(head, body, SnapshotResult.COMMITTED, upserted, flags, started)
|
||||
ApplyResult.Succeeded
|
||||
} catch (e: ProtocolViolation) {
|
||||
logSnapshot(head, body, SnapshotResult.ROLLED_BACK, upserted = 0, flags, started)
|
||||
ApplyResult.DeadProtocol(e.message ?: "protocol-violation", flags)
|
||||
}
|
||||
}
|
||||
|
||||
/** 状态事件:整态出站(§7.3 KAFKA_SCHD)+ 变化通知(KAFKA_MSG)。 */
|
||||
private fun snapshotEvents(next: FlightSnapshot): List<MsgEvent> {
|
||||
// KAFKA_SCHD 整态:缺失集合输出空集 → 消费者删除旧值(§5.4/§7.3)
|
||||
val payload = linkedMapOf<String, Any>(
|
||||
"flid" to next.flid,
|
||||
"stateVersion" to next.stateVersion,
|
||||
"scalars" to next.scalars,
|
||||
"collections" to next.collections,
|
||||
)
|
||||
val notify = mapper.writeValueAsString(mapOf("flid" to next.flid, "stateVersion" to next.stateVersion))
|
||||
return listOf(
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_SCHD,
|
||||
partitionKey = next.flid,
|
||||
stateVersion = next.stateVersion,
|
||||
payloadJson = mapper.writeValueAsString(payload),
|
||||
),
|
||||
MsgEvent(target = Targets.KAFKA_MSG, partitionKey = next.flid, stateVersion = next.stateVersion, payloadJson = notify),
|
||||
)
|
||||
}
|
||||
|
||||
/** §5.5 留痕:事务外追加,失败只记 error 不阻塞;scope 按记录归属运营日推导。 */
|
||||
private fun logSnapshot(
|
||||
head: ProcState,
|
||||
body: ScheduleBody,
|
||||
result: SnapshotResult,
|
||||
upserted: Int,
|
||||
flags: Set<SnapshotFlag>,
|
||||
startedNanos: Long,
|
||||
) {
|
||||
runCatching {
|
||||
val days = body.records.mapNotNull { opDay.compute(it.scalars["SODT"]) }
|
||||
snapshotLog.append(
|
||||
SnapshotLogEntry(
|
||||
msgId = head.msgId,
|
||||
recvAt = Instant.now(),
|
||||
scopeStart = days.minOrNull() ?: LocalDate.now(),
|
||||
scopeEnd = days.maxOrNull() ?: LocalDate.now(),
|
||||
recs = body.records.size,
|
||||
upserted = upserted,
|
||||
durationMs = (System.nanoTime() - startedNanos) / 1_000_000,
|
||||
result = result,
|
||||
flags = flags,
|
||||
),
|
||||
)
|
||||
}.onFailure { log.error("snap-log write failed (metric only) msgId={}", head.msgId, it) }
|
||||
}
|
||||
|
||||
private companion object {
|
||||
private val log = org.slf4j.LoggerFactory.getLogger(ScheduleProcessor::class.java)
|
||||
}
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package com.gzzn.omms.msgexchange.processing
|
||||
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine
|
||||
import com.gzzn.omms.msgexchange.domain.ErrorClass
|
||||
import com.gzzn.omms.msgexchange.domain.ProcState
|
||||
import com.gzzn.omms.msgexchange.domain.ProcStatus
|
||||
import com.gzzn.omms.msgexchange.domain.MsgEvent
|
||||
import com.gzzn.omms.msgexchange.domain.MsgKind
|
||||
import com.gzzn.omms.msgexchange.domain.Targets
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.BackfillTodoRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.CminmsgInboxRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightFields
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightFieldsJson
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightSchdRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.PipelineTransactionManager
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.ProcStateRepository
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.ReqTrackRepository
|
||||
import com.gzzn.omms.msgexchange.infra.retry.ProcFailure
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* staging(内存瞬态,崩溃从 raw 整包重放)→ PG 单事务原子“覆盖+按代差删+SQL CAS 推进+事件入队+SUCCEEDED”(I4/I5,ACM2-28 定案)。
|
||||
* U10/T07 占位安全化 + U08 统一失败迁移(ProcFailure):staging 未实装 → FAILED(UNSUPPORTED)+退避
|
||||
* (可重放,绝不写终态);CAS 冲突 → FAILED(INFRA)+退避;达上限统一 DEAD(EXHAUSTED)。
|
||||
*/
|
||||
@Singleton
|
||||
class SnapshotFlow(
|
||||
private val procState: ProcStateRepository,
|
||||
private val flightSchd: FlightSchdRepository,
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val reqTrack: ReqTrackRepository,
|
||||
private val procFailure: ProcFailure,
|
||||
private val txManager: PipelineTransactionManager,
|
||||
private val inbox: CminmsgInboxRepository,
|
||||
private val backfillTodo: BackfillTodoRepository? = null,
|
||||
) {
|
||||
private val log = org.slf4j.LoggerFactory.getLogger(SnapshotFlow::class.java)
|
||||
|
||||
fun publishSnapshot(head: ProcState, msg: DecodedMessage) {
|
||||
val messageId = head.cminmsgsId.toString()
|
||||
val staged = StageResult.stagingOf(msg)
|
||||
if (staged is StageResult.Invalid) {
|
||||
log.warn("staging not implemented -> FAILED(UNSUPPORTED) id={} reason={}", head.cminmsgsId, staged.reason)
|
||||
procFailure.fail(head, ErrorClass.UNSUPPORTED, staged.reason)
|
||||
return
|
||||
}
|
||||
val ok = staged as StageResult.Ok
|
||||
val normalized = ok.flights
|
||||
val day = ok.day
|
||||
|
||||
if (normalized.size > MAX_FLIGHTS_PER_SNAPSHOT) {
|
||||
log.error("snapshot flights exceed limit -> DEAD(MALFORMED) id={} size={}", head.cminmsgsId, normalized.size)
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED,
|
||||
lastError = "flights-exceed-limit:${normalized.size}")
|
||||
return
|
||||
}
|
||||
|
||||
// v2 §5 事务流程:锁外只做解析与整包校验;取得 PIPELINE_LOCK 后在锁内
|
||||
// 复核快照身份 → 读取当前代与当前航班态 → 计算 nextState → 写入 → 提交。
|
||||
var replayNoOp = false
|
||||
try {
|
||||
txManager.inTransaction {
|
||||
// 锁内复核快照身份:同一消息已成功提交 → 重放短路(不加版本、不重复发事件)
|
||||
val gen = flightSchd.getGen(day)
|
||||
if (gen?.lastMessageId == messageId) {
|
||||
procState.update(head.cminmsgsId, ProcStatus.SUCCEEDED)
|
||||
replayNoOp = true
|
||||
return@inTransaction
|
||||
}
|
||||
|
||||
val expected = gen?.version ?: 0L
|
||||
val newFlids = normalized.map { it.first }.toSet()
|
||||
val delFields = gen?.flids?.minus(newFlids) ?: emptySet()
|
||||
val newVersion = expected + 1L
|
||||
|
||||
// 锁内读取当前航班态,计算 nextState(单写者互斥下读到的一定是已提交最新态)
|
||||
val nextStates = normalized.map { (flid, fields) ->
|
||||
val current = flightSchd.findNextStateByFlid(flid)
|
||||
val commands = FlightStateEngine.commandsFromFields(flid, fields)
|
||||
FlightStateEngine.apply(current, commands, messageId, bumpVersion = true)
|
||||
}
|
||||
|
||||
flightSchd.persistNextStates(day, nextStates, snapshotReplace = true)
|
||||
|
||||
if (delFields.isNotEmpty()) {
|
||||
flightSchd.deleteDiffByDay(day, delFields)
|
||||
}
|
||||
|
||||
// 锁内 CAS:expected 即锁内刚读到的最新版本,仍失败属数据异常——
|
||||
// 一律回滚进入可重试 FAILED(INFRA),绝不凭版本号推断“已经是我的提交”(v2 §5)
|
||||
val casSuccess = flightSchd.putGenIfVersion(
|
||||
day = day,
|
||||
expected = expected,
|
||||
newGen = FlightSchdRepository.GenMeta(
|
||||
fday = day,
|
||||
version = newVersion,
|
||||
flids = newFlids,
|
||||
lastMessageId = messageId,
|
||||
),
|
||||
)
|
||||
if (!casSuccess) {
|
||||
throw CasConflictException(
|
||||
"gen-cas-conflict: expected=$expected msg=$messageId (lock-held CAS must not fail)",
|
||||
)
|
||||
}
|
||||
|
||||
val schdKind = msg.kind as? MsgKind.Schd
|
||||
if (schdKind?.subtype == MsgKind.SchdSubtype.RESP) {
|
||||
reqTrack.findOpenByKind("SCHD")?.let { req ->
|
||||
reqTrack.markDone(req.reqId)
|
||||
}
|
||||
}
|
||||
|
||||
val events = nextStates.map { state ->
|
||||
MsgEvent(
|
||||
target = Targets.KAFKA_SCHD,
|
||||
partitionKey = state.flid,
|
||||
payloadJson = FlightFieldsJson.toJson(state.toFlightFields()),
|
||||
)
|
||||
}
|
||||
if (events.isNotEmpty()) {
|
||||
msgEvents.insertAll(events)
|
||||
}
|
||||
|
||||
procState.update(head.cminmsgsId, ProcStatus.SUCCEEDED)
|
||||
}
|
||||
|
||||
// 提交后补偿:重放短路同样补做回填(其待办可能仍在重试中)
|
||||
safeBackfill(head, msg)
|
||||
if (replayNoOp) {
|
||||
log.info("snapshot replay no-op id={} day={}", head.cminmsgsId, day)
|
||||
} else {
|
||||
log.info("snapshot SUCCEEDED id={} day={} flights={}", head.cminmsgsId, day, normalized.size)
|
||||
}
|
||||
} catch (e: CasConflictException) {
|
||||
log.warn("gen CAS conflict -> FAILED(INFRA) id={} msg={}", head.cminmsgsId, e.message)
|
||||
procFailure.fail(head, ErrorClass.INFRA, e.message ?: "gen-cas-conflict")
|
||||
}
|
||||
}
|
||||
|
||||
private fun safeBackfill(head: ProcState, msg: DecodedMessage) {
|
||||
try {
|
||||
inbox.backfillOnSuccess(head.cminmsgsId, msg.meta.sndr, msg.meta.type, msg.meta.styp, msg.meta.seqn)
|
||||
} catch (e: Exception) {
|
||||
log.error("backfill failed after SUCCEEDED id={} (compensation required)", head.cminmsgsId, e)
|
||||
backfillTodo?.record(
|
||||
BackfillTodoRepository.BackfillTask(
|
||||
cminmsgsId = head.cminmsgsId,
|
||||
sndr = msg.meta.sndr,
|
||||
type = msg.meta.type,
|
||||
styp = msg.meta.styp,
|
||||
seqn = msg.meta.seqn,
|
||||
),
|
||||
e.message ?: e.javaClass.simpleName,
|
||||
) ?: log.warn("no backfill-todo repository bound; compensation NOT persisted id={}", head.cminmsgsId)
|
||||
}
|
||||
}
|
||||
|
||||
/** staging 结果(骨架)。 */
|
||||
sealed interface StageResult {
|
||||
data class Ok(val day: String, val flights: List<Pair<String, FlightFields>>) : StageResult
|
||||
data class Invalid(val reason: String) : StageResult
|
||||
|
||||
companion object {
|
||||
var parser: ((DecodedMessage) -> StageResult)? = null
|
||||
|
||||
fun stagingOf(msg: DecodedMessage): StageResult =
|
||||
parser?.invoke(msg) ?: Invalid("staging-not-implemented(${msg.typeTag})") // TODO(阶段2)
|
||||
}
|
||||
}
|
||||
}
|
||||
class CasConflictException(message: String) : RuntimeException(message)
|
||||
const val MAX_FLIGHTS_PER_SNAPSHOT = 10000
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.gzzn.omms.msgexchange.processing.handlers
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import com.gzzn.omms.msgexchange.codec.FlopPayload
|
||||
import com.gzzn.omms.msgexchange.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.domain.Decision
|
||||
import com.gzzn.omms.msgexchange.domain.FlightChange
|
||||
import com.gzzn.omms.msgexchange.domain.MsgKind
|
||||
import com.gzzn.omms.msgexchange.domain.SchdPush
|
||||
import com.gzzn.omms.msgexchange.domain.flight.FlightStateEngine
|
||||
import com.gzzn.omms.msgexchange.infra.persistence.FlightFieldsJson
|
||||
import com.gzzn.omms.msgexchange.processing.Handler
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* FLOP-GTDT:登机门集合级快照替换(SIS §3.34;GTNO=0 清除)。
|
||||
* 产出 FlightChange.fields["GTDT"] 为 JSON 数组,供 FlightStateEngine → persistNextStates 写入明细表。
|
||||
*/
|
||||
@Singleton
|
||||
class GtdtHandler(
|
||||
private val mapper: ObjectMapper = ObjectMapper(),
|
||||
) : Handler {
|
||||
override val kind: MsgKind = MsgKind.Flop("GTDT")
|
||||
|
||||
override fun decide(flightView: Map<String, Map<String, String>>, msg: DecodedMessage): Decision {
|
||||
val body = msg.body as? FlopPayload
|
||||
?: throw IllegalArgumentException("GTDT handler requires FlopPayload body")
|
||||
val gtdtItems = body.collections["GTDT"] ?: emptyList()
|
||||
val fields = linkedMapOf<String, String>()
|
||||
fields["FLID"] = body.flid
|
||||
body.scalars.forEach { (k, v) -> fields[k] = v }
|
||||
fields["GTDT"] = mapper.writeValueAsString(gtdtItems)
|
||||
|
||||
val current = flightView[body.flid]?.let { FlightStateEngine.fromFlightFields(body.flid, it) }
|
||||
val commands = FlightStateEngine.commandsFromFields(body.flid, fields)
|
||||
val preview = FlightStateEngine.apply(current, commands, messageId = "", bumpVersion = false)
|
||||
val payloadJson = FlightFieldsJson.toJson(preview.toFlightFields(mapper))
|
||||
|
||||
return Decision(
|
||||
flightChanges = listOf(FlightChange(body.flid, fields)),
|
||||
schdPush = listOf(SchdPush(body.flid, payloadJson)),
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user