fix(processing): U08 重试闭环 + FailureScheduler + U11 重放入口(ACM2-10)
- U08 闭环:失败状态迁移移到持有 head/事件的边界内——processOne/SnapshotFlow 统一经 ProcFailure(FAILED+attempts+退避,达上限 DEAD/EXHAUSTED 并落库 attempts);Dispatcher 逐条与 SCHD 批量共用 retryOrDead;flushSchd 失败整批 attempts+1/指数退避,队首未到期不 claim,达上限整批 DEAD(DLQ);loop 只作最后防线——仅 catch Exception,InterruptedException 恢复中断位并上抛,致命 Error 不吞(异常必可见) - 统一策略:新增 FailureScheduler(可注入 java.time.Clock + TimeFactory,backoff/exhausted 单一来源)与 ProcFailure,MessageProcessor/SnapshotFlow/Dispatcher 共用 - U11:ReplayService 显式重放入口 + ProcStateRepository.requeueByErrorClasses——仅白名单内 可恢复类(CODEC_ERROR/UNSUPPORTED/INFRA/EXHAUSTED)从 FAILED/DEAD 回 PENDING(attempts 清零), MALFORMED 永不重放 - 测试(30 个全绿):①Pump 内部异常→FAILED(INFRA)→重试达上限 DEAD;②SCHD 连续失败退避递增→ 整批 DEAD 且 KAFKA:msg 不受影响;③AssertionError/InterruptedException 不被普通恢复吞掉; 另有 ReplayService(3)、Dispatcher 批退避、InfraBindingStartupTest(DataSource 启动级绑定) - 收尾:.gitattributes(行尾/二进制);Gradle 10 弃用告警核查——仅来自 micronaut-application 插件(BOM 注入 + IDE 文件生成),升级 Gradle 10 前需先升插件版本(已记录)
This commit is contained in:
@@ -6,6 +6,7 @@ import com.gzzn.omms.msgexchange.nextgen.domain.EventStatus
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.MsgEvent
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.Targets
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.retry.FailureScheduler
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Duration
|
||||
import java.time.Instant
|
||||
@@ -23,15 +24,19 @@ interface DeliveryPort {
|
||||
}
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 3:投递调度——每 target 严格 FIFO(I1 双层同策略);
|
||||
* N03(U06):KAFKA_SCHD 不走逐条循环(唯一出口是 flushSchd 批量聚合),
|
||||
* 逐条循环显式排除,避免 schd 事件被无条件 markSent 吞掉、flush 永远 claim 不到。
|
||||
* ACMA-8 流程 3:投递调度——每 target 严格 FIFO(I1 双层同策略)。
|
||||
* N03(U06):KAFKA_SCHD 不走逐条循环(唯一出口是 flushSchd 批量聚合),逐条循环显式排除。
|
||||
* U08 闭环:逐条与批量失败都在持有具体事件/批次的边界完成状态迁移——
|
||||
* · 逐条:scheduleRetry(attempts+1, backoff) / 达上限 markDead(EXHAUSTED)(DLQ 保留行);
|
||||
* · 批量(flushSchd):整批退避,队首 nextAttemptAt 未到不 claim;达上限整批 DEAD/DLQ;
|
||||
* · loop 只作最后防线,不吞 InterruptedException(致命/中断错误不被普通恢复吞掉)。
|
||||
*/
|
||||
@Singleton
|
||||
class Dispatcher(
|
||||
private val msgEvents: MsgEventRepository,
|
||||
private val port: DeliveryPort,
|
||||
private val props: PipelineProps,
|
||||
private val scheduler: FailureScheduler,
|
||||
) {
|
||||
@Volatile
|
||||
private var running = true
|
||||
@@ -42,8 +47,11 @@ class Dispatcher(
|
||||
while (running) {
|
||||
try {
|
||||
tick()
|
||||
} catch (e: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
// U08/R02:投递循环不得因单次异常静默死亡;U12 补 ERROR 日志与告警出口。
|
||||
// U08:投递循环只作最后防线;致命 Error 不吞。tick 内失败迁移已完成。
|
||||
sleepQuietly(props.pipeline.pollInterval)
|
||||
}
|
||||
// N18:轮询间隔取参数表(下限 50ms,避免退避节律被吞)
|
||||
@@ -56,7 +64,7 @@ class Dispatcher(
|
||||
for (t in targets) {
|
||||
if (t == Targets.KAFKA_SCHD) continue // N03:schd 唯一出口 flushSchd
|
||||
val head = msgEvents.headUnsent(t) ?: continue
|
||||
if (head.state == EventStatus.PENDING && head.nextAttemptAt != null && head.nextAttemptAt > Instant.now()) {
|
||||
if (head.state == EventStatus.PENDING && head.nextAttemptAt != null && head.nextAttemptAt > scheduler.now()) {
|
||||
// 队头退避未到期:等待,不跳过(保序);超时升级归 U13(WP2)
|
||||
continue
|
||||
}
|
||||
@@ -68,30 +76,26 @@ class Dispatcher(
|
||||
msgEvents.insertSync(listOf(MsgEvent(target = Targets.REDIS_FLIGHT_INFO, payloadJson = deleteOf(head))))
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
val attempts = head.attempts + 1
|
||||
if (attempts >= props.pipeline.maxAttempts) {
|
||||
msgEvents.markDead(head.eventId!!, ErrorClass.EXHAUSTED, e.message ?: "unknown")
|
||||
} else {
|
||||
msgEvents.scheduleRetry(
|
||||
head.eventId!!,
|
||||
Instant.now().plusMillis(props.pipeline.backoffFor(attempts)),
|
||||
attempts,
|
||||
)
|
||||
}
|
||||
retryOrDead(head, e.message ?: "unknown")
|
||||
}
|
||||
}
|
||||
if (flushDue()) {
|
||||
try {
|
||||
flushSchd()
|
||||
} catch (e: Exception) {
|
||||
// U08/N24:批发送失败不得终止 loop——事件保持 PENDING,下个 flush 周期整批重试;
|
||||
// 毒丸/告警随 U12/U13 补齐。lastFlush 仅在成功后推进(见 flushSchd)。
|
||||
}
|
||||
flushSchd()
|
||||
}
|
||||
}
|
||||
|
||||
private fun flushDue(): Boolean =
|
||||
Duration.between(lastFlush, Instant.now()) >= props.schd.flushPeriod
|
||||
Duration.between(lastFlush, scheduler.now()) >= props.schd.flushPeriod
|
||||
|
||||
/** 单条事件失败迁移:attempts+1;达上限 DEAD(EXHAUSTED)(DLQ,attempts 落库审计),否则退避重试。 */
|
||||
private fun retryOrDead(e: MsgEvent, lastError: String) {
|
||||
val attempts = e.attempts + 1
|
||||
if (scheduler.exhausted(attempts)) {
|
||||
msgEvents.markDead(e.eventId!!, ErrorClass.EXHAUSTED, lastError, attempts)
|
||||
} else {
|
||||
msgEvents.scheduleRetry(e.eventId!!, scheduler.nextAttemptAt(attempts), attempts)
|
||||
}
|
||||
}
|
||||
|
||||
private fun deliver(target: String, e: MsgEvent) = when (target) {
|
||||
Targets.KAFKA_MSG -> port.sendKafka("msg", e.payloadJson)
|
||||
@@ -103,17 +107,30 @@ class Dispatcher(
|
||||
|
||||
private fun deleteOf(e: MsgEvent) = """{"op":"delete","refs":${e.partitionKey ?: ""}}"""
|
||||
|
||||
/** 流程 3 flushSchd:批上限 + 每 FLID 最新一态聚合(topic "schd",wire=FLTR JSON 数组)。 */
|
||||
/**
|
||||
* 流程 3 flushSchd:批上限 + 每 FLID 最新一态聚合(topic "schd",wire=FLTR JSON 数组)。
|
||||
* U08 批量闭环:队首退避未到期不 claim;发送失败 → 整批 attempts+1(退避)或达上限整批 DEAD/DLQ;
|
||||
* lastFlush 仅在成功(含空批)后推进。
|
||||
*/
|
||||
internal fun flushSchd() {
|
||||
val pending = msgEvents.claimBatch(Targets.KAFKA_SCHD, limit = props.schd.flushLimit)
|
||||
if (pending.isEmpty()) {
|
||||
lastFlush = Instant.now()
|
||||
lastFlush = scheduler.now()
|
||||
return
|
||||
}
|
||||
val due = pending.first()
|
||||
if (due.nextAttemptAt != null && due.nextAttemptAt > scheduler.now()) {
|
||||
return // 队首仍在退避:整批等待,不推进 lastFlush(到期再试)
|
||||
}
|
||||
val payload = SchdAggregation.latestPerFlight(pending).joinToString(",", "[", "]")
|
||||
port.sendKafka("schd", payload) // 失败整批退避(at-least-once):异常上抛由 tick 隔离,事件保持 PENDING
|
||||
try {
|
||||
port.sendKafka("schd", payload)
|
||||
} catch (e: Exception) {
|
||||
pending.forEach { retryOrDead(it, "schd-send: ${e.message ?: "unknown"}") }
|
||||
return // 不推进 lastFlush:整批退避(含 DEAD 出队)后到期重试
|
||||
}
|
||||
msgEvents.markAllSent(pending.mapNotNull { it.eventId })
|
||||
lastFlush = Instant.now() // N24:仅在成功后推进
|
||||
lastFlush = scheduler.now()
|
||||
}
|
||||
|
||||
private fun sleepQuietly(d: Duration) {
|
||||
|
||||
@@ -29,6 +29,13 @@ interface ProcStateRepository {
|
||||
errorClass: ErrorClass? = null,
|
||||
lastError: String? = null,
|
||||
)
|
||||
|
||||
/**
|
||||
* U11 显式重放入口(ReplayService):仅把给定 errorClass 集合中的行从 FAILED/DEAD 置回 PENDING,
|
||||
* 以便主泵重新领取。实现约定:ATTEMPTS=0、NEXT_ATTEMPT_AT=NULL(立即重试),
|
||||
* ERROR_CLASS/LAST_ERROR 保留作审计。返回受影响行数。
|
||||
*/
|
||||
fun requeueByErrorClasses(errorClasses: List<ErrorClass>): Int
|
||||
}
|
||||
|
||||
interface MsgEventRepository {
|
||||
@@ -45,7 +52,7 @@ interface MsgEventRepository {
|
||||
|
||||
fun scheduleRetry(eventId: Long, nextAttemptAt: Instant, attempts: Int)
|
||||
|
||||
fun markDead(eventId: Long, errorClass: ErrorClass, lastError: String)
|
||||
fun markDead(eventId: Long, errorClass: ErrorClass, lastError: String, attempts: Int? = null)
|
||||
|
||||
/** 阶段 B(定案 2):Delivery 同线程在 ES 投递成功后同步 enqueue 删除事件。 */
|
||||
fun insertSync(events: List<MsgEvent>)
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.gzzn.omms.msgexchange.nextgen.infra.retry
|
||||
|
||||
import com.gzzn.omms.msgexchange.nextgen.config.PipelineProps
|
||||
import io.micronaut.context.annotation.Factory
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Clock
|
||||
import java.time.Instant
|
||||
|
||||
/**
|
||||
* 统一重试策略(ACM2-10 U08):供 MessageProcessor / SnapshotFlow(ProcState 侧)与
|
||||
* Dispatcher(MsgEvent 侧)共用——attempts 递增后按 backoff 表给 nextAttemptAt;
|
||||
* exhausted 判定与两侧同源(maxAttempts)。时间一律经可注入 Clock(测试用固定钟,避免脆弱睡眠)。
|
||||
*/
|
||||
@Singleton
|
||||
class FailureScheduler(
|
||||
private val props: PipelineProps,
|
||||
private val clock: Clock,
|
||||
) {
|
||||
/** 便捷构造:默认系统时钟(生产路径)。 */
|
||||
constructor(props: PipelineProps) : this(props, Clock.systemUTC())
|
||||
|
||||
fun now(): Instant = clock.instant()
|
||||
|
||||
fun exhausted(attempts: Int): Boolean = attempts >= props.pipeline.maxAttempts
|
||||
|
||||
/** attempts 指递增后的值;N28:attempt ≤ 0 由 backoffFor 兜底为首档。 */
|
||||
fun nextAttemptAt(attemptsAfterIncrement: Int): Instant =
|
||||
now().plusMillis(props.pipeline.backoffFor(attemptsAfterIncrement))
|
||||
}
|
||||
|
||||
/** 提供可注入 Clock(java.time.Clock);测试可用 Clock.fixed(...) 或自定义可变钟覆盖。 */
|
||||
@Factory
|
||||
class TimeFactory {
|
||||
@Singleton
|
||||
fun systemClock(): Clock = Clock.systemUTC()
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.gzzn.omms.msgexchange.nextgen.infra.retry
|
||||
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ErrorClass
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ProcState
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ProcStatus
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.ProcStateRepository
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* ProcState 侧统一失败迁移(U08/U10):处理/快照路径共用——
|
||||
* attempts+1 后若 exhausted → DEAD(EXHAUSTED)(终态,errorClass 规范化,原因保留在 lastError);
|
||||
* 否则 FAILED + attempts + nextAttemptAt(退避)(可重放)。任何“失败”都不得在无退避下直接终态化。
|
||||
*/
|
||||
@Singleton
|
||||
class ProcFailure(
|
||||
private val procState: ProcStateRepository,
|
||||
val scheduler: FailureScheduler,
|
||||
) {
|
||||
fun fail(head: ProcState, ec: ErrorClass, reason: String) {
|
||||
val attempts = head.attempts + 1
|
||||
if (scheduler.exhausted(attempts)) {
|
||||
procState.update(
|
||||
head.cminmsgsId, ProcStatus.DEAD,
|
||||
attempts = attempts,
|
||||
errorClass = ErrorClass.EXHAUSTED,
|
||||
lastError = "$reason; attempts=$attempts",
|
||||
)
|
||||
} else {
|
||||
procState.update(
|
||||
head.cminmsgsId, ProcStatus.FAILED,
|
||||
attempts = attempts,
|
||||
nextAttemptAt = scheduler.nextAttemptAt(attempts),
|
||||
errorClass = ec,
|
||||
lastError = reason,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.gzzn.omms.msgexchange.nextgen.infra.retry
|
||||
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ErrorClass
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.ProcStateRepository
|
||||
import jakarta.inject.Singleton
|
||||
|
||||
/**
|
||||
* U11 显式重放入口:只允许“可恢复”的错误类从 FAILED/DEAD 回到 PENDING(主泵重领)。
|
||||
* 不可恢复类(MALFORMED——报文非法,重放必再失败)与未知类一律不在白名单内。
|
||||
*/
|
||||
@Singleton
|
||||
class ReplayService(
|
||||
private val procState: ProcStateRepository,
|
||||
) {
|
||||
/** 可恢复错误类:codec 修复可重放 / 未实装补齐可重放 / 基础设施抖动可重放 / 重试耗尽后人工复核可重放。 */
|
||||
val replayableErrorClasses: Set<ErrorClass> =
|
||||
setOf(ErrorClass.CODEC_ERROR, ErrorClass.UNSUPPORTED, ErrorClass.INFRA, ErrorClass.EXHAUSTED)
|
||||
|
||||
/** 只重放白名单内的类;请求含 MALFORMED 等非法类时静默忽略该类。 */
|
||||
fun replay(requested: Collection<ErrorClass>): Int {
|
||||
val allowed = requested.filter { it in replayableErrorClasses }
|
||||
if (allowed.isEmpty()) return 0
|
||||
return procState.requeueByErrorClasses(allowed)
|
||||
}
|
||||
|
||||
/** 默认入口:重放全部可恢复类。 */
|
||||
fun replayAll(): Int = replay(replayableErrorClasses)
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import com.gzzn.omms.msgexchange.nextgen.infra.persistence.MsgEventRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.ProcStateRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.PumpJobRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.redis.FlightRedisClient
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.retry.ProcFailure
|
||||
import com.gzzn.omms.msgexchange.nextgen.jobs.JobExecutor
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Duration
|
||||
@@ -37,9 +38,12 @@ class Pump(
|
||||
while (running) {
|
||||
try {
|
||||
tick()
|
||||
} catch (t: Throwable) {
|
||||
// U08/R02:主泵不得因单次异常静默死亡——任一 codec/DB/Redis 抖动不得终止队列;
|
||||
// 具体失败落 FAILED/DEAD 与告警见 MessageProcessor/U12。
|
||||
} catch (e: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
// U08:loop 只作最后防线——失败状态迁移已在 processOne/execute 的边界内完成;
|
||||
// 致命 Error 不在此捕获(任其终止进程,保证“异常必可见”)。
|
||||
sleepQuietly(props.pipeline.pollInterval)
|
||||
}
|
||||
}
|
||||
@@ -56,11 +60,12 @@ class Pump(
|
||||
head == null -> sleepQuietly(props.pipeline.pollInterval)
|
||||
head.state == ProcStatus.FAILED && (head.nextAttemptAt ?: Instant.EPOCH) > Instant.now() ->
|
||||
if (poisoned(head)) {
|
||||
upgradeDead(head, head.lastError ?: "head-deadline-exceeded")
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD,
|
||||
errorClass = ErrorClass.EXHAUSTED, lastError = head.lastError ?: "head-deadline-exceeded")
|
||||
} else {
|
||||
sleepQuietly(Duration.between(Instant.now(), head.nextAttemptAt))
|
||||
}
|
||||
// PENDING、或 FAILED 退避已到期:交处理入口(入口再做 attempts 毒丸守卫)
|
||||
// PENDING、或 FAILED 退避已到期:交处理入口(内部有边界化失败迁移与 attempts 守卫)
|
||||
else -> processor.processOne(head)
|
||||
}
|
||||
}
|
||||
@@ -69,10 +74,6 @@ class Pump(
|
||||
head.attempts >= props.pipeline.maxAttempts ||
|
||||
Duration.between(head.updatedAt, Instant.now()) > props.pipeline.headDeadline
|
||||
|
||||
private fun upgradeDead(head: ProcState, reason: String) {
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.EXHAUSTED, lastError = reason)
|
||||
}
|
||||
|
||||
/** JOB 与队头消息的先后由入队时间近似;实装以统一序号列保证(阶段 1 后续)。 */
|
||||
private fun jobBefore(job: PumpJobRepository.Job, head: ProcState?) =
|
||||
head == null || head.state == ProcStatus.FAILED
|
||||
@@ -94,9 +95,9 @@ class Pump(
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 2 processOne:解码→绑定→纯函数决策→提交。
|
||||
* U08/U10/U11(N06/N21/T06):统一失败治理——「未实装/未注册/CODEC_ERROR/staging 未实装」
|
||||
* 一律 FAILED(可重放,带 attempts+nextAttemptAt,绝不直接写终态);仅 MALFORMED(报文非法)
|
||||
* 与「重试耗尽」写 DEAD 终态。attempts ≥ maxAttempts 的 FAILED 在入口即升级 DEAD(防退避到期后无限重试)。
|
||||
* U08/U10/U11:失败状态迁移全部在“持有具体 head”的边界内完成——
|
||||
* 任何意外异常 → FAILED(INFRA)+退避(ProcFailure);attempts 达上限 → DEAD(EXHAUSTED);
|
||||
* MALFORMED 直接 DEAD;InterruptedException 恢复中断位并上抛(不被普通恢复逻辑吞掉)。
|
||||
*/
|
||||
@Singleton
|
||||
class MessageProcessor(
|
||||
@@ -107,11 +108,24 @@ class MessageProcessor(
|
||||
private val handlers: HandlerHolder,
|
||||
private val redis: FlightRedisClient,
|
||||
private val snapshotFlow: SnapshotFlow,
|
||||
private val procFailure: ProcFailure,
|
||||
private val props: PipelineProps,
|
||||
) {
|
||||
fun processOne(head: ProcState) {
|
||||
// U08 入口守卫:FAILED 行退避到期后再处理前,先判 attempts 毒丸(与 tick poisoned 同值)
|
||||
if (head.state == ProcStatus.FAILED && head.attempts >= props.pipeline.maxAttempts) {
|
||||
try {
|
||||
processInternal(head)
|
||||
} catch (e: InterruptedException) {
|
||||
Thread.currentThread().interrupt()
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
// U08:边界化——异常归于本条 head,写 FAILED/DEAD,而不是穿出杀 pump
|
||||
procFailure.fail(head, ErrorClass.INFRA, e.message ?: e.javaClass.simpleName)
|
||||
}
|
||||
}
|
||||
|
||||
private fun processInternal(head: ProcState) {
|
||||
// 守卫:手工/遗留 FAILED 行若 attempts 已达上限,直接终态(防止退避到期后无限重试)
|
||||
if (head.state == ProcStatus.FAILED && procFailure.scheduler.exhausted(head.attempts)) {
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.EXHAUSTED,
|
||||
lastError = head.lastError ?: "max-attempts")
|
||||
return
|
||||
@@ -129,7 +143,7 @@ class MessageProcessor(
|
||||
procState.update(head.cminmsgsId, ProcStatus.DEAD, errorClass = ErrorClass.MALFORMED,
|
||||
lastError = r.failure.detail)
|
||||
} else {
|
||||
failWithBackoff(head, r.failure.errorClass, r.failure.detail)
|
||||
procFailure.fail(head, r.failure.errorClass, r.failure.detail)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -149,7 +163,7 @@ class MessageProcessor(
|
||||
val handler = handlers.registry.dispatcherFor(decoded)
|
||||
if (handler == null) {
|
||||
// U10(N21):未注册 ≠ 报文非法——写 FAILED(可重放),绝不写终态
|
||||
failWithBackoff(head, ErrorClass.UNSUPPORTED, "no-handler:${decoded.typeTag}")
|
||||
procFailure.fail(head, ErrorClass.UNSUPPORTED, "no-handler:${decoded.typeTag}")
|
||||
return
|
||||
}
|
||||
val schdKind = decoded.kind as? com.gzzn.omms.msgexchange.nextgen.domain.MsgKind.Schd
|
||||
@@ -175,18 +189,6 @@ class MessageProcessor(
|
||||
procState.update(head.cminmsgsId, ProcStatus.SUCCEEDED)
|
||||
// 阶段 B:flightState.apply(decision.flightChanges) 进入同一事务;投影事件(ES/REDIS)追加。
|
||||
}
|
||||
|
||||
/** 统一失败路径:FAILED + attempts+1 + nextAttemptAt=backoff(N28 下界由 backoffFor 保证)。 */
|
||||
private fun failWithBackoff(head: ProcState, ec: ErrorClass, reason: String) {
|
||||
val attempts = head.attempts + 1
|
||||
procState.update(
|
||||
head.cminmsgsId, ProcStatus.FAILED,
|
||||
attempts = attempts,
|
||||
nextAttemptAt = Instant.now().plusMillis(props.pipeline.backoffFor(attempts)),
|
||||
errorClass = ec,
|
||||
lastError = reason,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/** 延迟装配占位(阶段 1 后续以 Micronaut Bean 替换直连构造)。 */
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.gzzn.omms.msgexchange.nextgen.processing
|
||||
|
||||
import com.gzzn.omms.msgexchange.nextgen.config.PipelineProps
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.DecodedMessage
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ErrorClass
|
||||
import com.gzzn.omms.msgexchange.nextgen.domain.ProcState
|
||||
@@ -9,28 +8,28 @@ import com.gzzn.omms.msgexchange.nextgen.infra.persistence.ProcStateRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.persistence.RefDataRepository
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.redis.FlightRedisClient
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.redis.RedisScript
|
||||
import com.gzzn.omms.msgexchange.nextgen.infra.retry.ProcFailure
|
||||
import jakarta.inject.Singleton
|
||||
import java.time.Instant
|
||||
|
||||
/**
|
||||
* ACMA-8 流程 4:日计划快照(generation,主泵内执行)。
|
||||
* staging(内存瞬态,崩溃从 raw 整包重放)→ Lua 原子“覆盖+按代差删”(I4/I5)
|
||||
* → putGenIfVersion CAS + SUCCEEDED(重放幂等,版本不二次自增——恢复协议细节见 U09)。
|
||||
* U10/T07 占位安全化:staging 未实装 → FAILED(UNSUPPORTED)+退避(可重放),绝不写 DEAD 终态;
|
||||
* CAS 冲突 → FAILED(INFRA)+退避(不再无 nextAttemptAt 紧循环,N06/N28)。
|
||||
* U10/T07 占位安全化 + U08 统一失败迁移(ProcFailure):staging 未实装 → FAILED(UNSUPPORTED)+退避
|
||||
* (可重放,绝不写终态);CAS 冲突 → FAILED(INFRA)+退避;达上限统一 DEAD(EXHAUSTED)。
|
||||
*/
|
||||
@Singleton
|
||||
class SnapshotFlow(
|
||||
private val procState: ProcStateRepository,
|
||||
private val refData: RefDataRepository,
|
||||
private val redis: FlightRedisClient,
|
||||
private val props: PipelineProps,
|
||||
private val procFailure: ProcFailure,
|
||||
) {
|
||||
fun publishSnapshot(head: ProcState, msg: DecodedMessage) {
|
||||
// 1) staging:流式解析 + 整包校验(TODO(阶段2): 流式 codec;千级 FLTR 为 MB 级,内存瞬态)
|
||||
val staged = StageResult.stagingOf(msg) // 骨架:TODO 解析 FLTR 集与重组(KEEP 现役 MAFL/登机桥规则)
|
||||
if (staged is StageResult.Invalid) {
|
||||
failWithBackoff(head, ErrorClass.UNSUPPORTED, staged.reason) // U10:未实装 → 可重放,非终态
|
||||
procFailure.fail(head, ErrorClass.UNSUPPORTED, staged.reason) // U10:未实装 → 可重放,非终态
|
||||
return
|
||||
}
|
||||
val normalized = (staged as StageResult.Ok).flights // (flid, payloadJson)
|
||||
@@ -48,24 +47,13 @@ class SnapshotFlow(
|
||||
// 重放路径:version 已是目标值 → no-op 视为成功
|
||||
val again = refData.getGen(day)
|
||||
if (again == null || again.version != expected + 1) {
|
||||
failWithBackoff(head, ErrorClass.INFRA, "gen-cas-conflict") // N06/N28:带退避,禁止紧循环
|
||||
procFailure.fail(head, ErrorClass.INFRA, "gen-cas-conflict") // N06/N28:带退避,禁止紧循环
|
||||
return
|
||||
}
|
||||
}
|
||||
procState.update(head.cminmsgsId, ProcStatus.SUCCEEDED)
|
||||
}
|
||||
|
||||
private fun failWithBackoff(head: ProcState, ec: ErrorClass, reason: String) {
|
||||
val attempts = head.attempts + 1
|
||||
procState.update(
|
||||
head.cminmsgsId, ProcStatus.FAILED,
|
||||
attempts = attempts,
|
||||
nextAttemptAt = Instant.now().plusMillis(props.pipeline.backoffFor(attempts)),
|
||||
errorClass = ec,
|
||||
lastError = reason,
|
||||
)
|
||||
}
|
||||
|
||||
/** staging 结果(骨架)。 */
|
||||
sealed interface StageResult {
|
||||
data class Ok(val day: String, val flights: List<Pair<String, String>>) : StageResult
|
||||
|
||||
Reference in New Issue
Block a user