fix(心跳): 影子扫描不再新增 catch 块——静默 catch 棘轮当场抓住

闸门红在 CatchSilenceRatchetTest:影子服务新增的 2 处 log-only catch 与位置指纹
新增的 1 处 fail-open catch 都会抬「只 log / 不重抛」台账。改法是消除新增 catch
而不是调基线:tryLock 内部已把 Redis 异常降级成 null,扫描异常交给 Spring 调度器
记录;指纹复用 describe/readPosition 各自的降级出口。之后 1281 例与棘轮全绿。
This commit is contained in:
2026-10-20 12:42:06 +08:00
parent eb6f082674
commit 3ac859e05f
3 changed files with 20 additions and 30 deletions
@@ -328,28 +328,24 @@ public class TaskHeartbeatPositionService {
} }
/** /**
* 影子判官的推进指纹:位置哈希一次读出,计数/文本/阶段任一变化都算推进。 * 影子判官的推进指纹:计数/文本任一变化都算推进。
* 返回 null 表示这个任务没有任何位置信息(老客户端不传位置),不能作为判死证据。 *
* <p>复用 {@link #describe} 与 {@link #readPosition}(两者各自把 Redis 异常降级成 null),
* 不新增 catch;老客户端只发文本时由 describe 兜住。返回 null 表示没有任何位置信息,
* 不能作为判死证据。
*/ */
private String readProgressFingerprint(Long taskId) { private String readProgressFingerprint(Long taskId) {
Map<Object, Object> entries; String text = describe(taskId);
try { Position position = readPosition(taskId);
entries = stringRedisTemplate.opsForHash().entries(KEY_PREFIX + taskId); if ((text == null || text.isBlank()) && position == null) {
} catch (Exception ex) {
log.warn("[heartbeat-position] 读取影子位置失败 taskId={} msg={}", taskId, ex.getMessage());
return null; return null;
} }
if (entries == null || entries.isEmpty()) { String current = position == null || position.current() == null
return null; ? "" : String.valueOf(position.current());
} String total = position == null || position.total() == null
String text = value(entries, "progressText"); ? "" : String.valueOf(position.total());
String current = value(entries, "current"); String grain = position == null || position.grain() == null ? "" : position.grain();
String total = value(entries, "total"); return grain + "|" + current + "|" + total + "|" + (text == null ? "" : text);
if (text.isBlank() && current.isBlank() && total.isBlank()) {
return null;
}
return value(entries, "phase") + "|" + value(entries, "grain")
+ "|" + current + "|" + total + "|" + text;
} }
private StallState evaluateAgainstSnapshot(Long taskId, private StallState evaluateAgainstSnapshot(Long taskId,
@@ -75,22 +75,16 @@ public class TaskLivenessShadowService {
if (!enabled) { if (!enabled) {
return; return;
} }
DistributedJobLockService.LockHandle lock; // tryLock 内部已把 Redis 异常降级成 null;扫描失败交给 Spring 调度器记录并继续下一轮,
try { // 不在这里吞异常——影子线只读,抛出去也不会影响任务本身。
lock = distributedJobLockService.tryLock(SCAN_LOCK_NAME, Duration.ofMinutes(5)); DistributedJobLockService.LockHandle lock =
} catch (Exception ex) { distributedJobLockService.tryLock(SCAN_LOCK_NAME, Duration.ofMinutes(5));
log.warn("{} 获取扫描锁失败,本轮跳过 msg={}", LOG_ANCHOR, ex.getMessage());
return;
}
if (lock == null) { if (lock == null) {
log.info("{} 另一实例持有扫描锁,本轮跳过", LOG_ANCHOR); log.info("{} 另一实例持有扫描锁,本轮跳过", LOG_ANCHOR);
return; return;
} }
try (lock) { try (lock) {
scanOnce(); scanOnce();
} catch (Exception ex) {
// 影子线只读:扫描失败绝不能影响任务本身
log.warn("{} 扫描失败(只读,不影响任务) msg={}", LOG_ANCHOR, ex.getMessage(), ex);
} }
} }
@@ -337,7 +337,7 @@ class TaskHeartbeatPositionServiceTest {
.thenReturn(Map.of("progressText", "ASIN B0F44Y3YYQ(401/1300)", .thenReturn(Map.of("progressText", "ASIN B0F44Y3YYQ(401/1300)",
"current", "401", "total", "1300")); "current", "401", "total", "1300"));
when(hashOperations.entries("task:progress:shadow-stall:123")) when(hashOperations.entries("task:progress:shadow-stall:123"))
.thenReturn(Map.of("pos", "crawling||400|1300|ASIN B0F44Y3YYQ(400/1300)", .thenReturn(Map.of("pos", "|400|1300|ASIN B0F44Y3YYQ(400/1300)",
"at", String.valueOf(Long.MAX_VALUE))); "at", String.valueOf(Long.MAX_VALUE)));
assertEquals(TaskHeartbeatPositionService.StallState.ADVANCING, assertEquals(TaskHeartbeatPositionService.StallState.ADVANCING,
@@ -354,7 +354,7 @@ class TaskHeartbeatPositionServiceTest {
.thenReturn(Map.of("progressText", "明细处理中", .thenReturn(Map.of("progressText", "明细处理中",
"current", "401", "total", "1300")); "current", "401", "total", "1300"));
when(hashOperations.entries("task:progress:shadow-stall:123")) when(hashOperations.entries("task:progress:shadow-stall:123"))
.thenReturn(Map.of("pos", "crawling||400|1300|明细处理中", .thenReturn(Map.of("pos", "|400|1300|明细处理中",
"at", String.valueOf(Long.MAX_VALUE))); "at", String.valueOf(Long.MAX_VALUE)));
assertEquals(TaskHeartbeatPositionService.StallState.ADVANCING, assertEquals(TaskHeartbeatPositionService.StallState.ADVANCING,