Junjie
2026-04-27 0a13eabe642d7b502f91467f6c93e54f6c9aa7a2
fix: release auto tune scheduler lock on guard failure
2个文件已修改
33 ■■■■■ 已修改文件
src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java 10 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/ai/service/impl/AutoTuneCoordinatorServiceImpl.java
@@ -82,8 +82,8 @@
        }
        AutoTuneAgentService.AutoTuneAgentResult agentResult = null;
        markLastTriggerGuard(intervalMinutes);
        try {
            safeMarkLastTriggerGuard(intervalMinutes);
            agentResult = autoTuneAgentService.runAutoTune(AutoTuneTriggerType.AUTO.getCode());
            safeWriteOperateLog(agentResult);
            return AutoTuneCoordinatorResult.triggered(agentResult);
@@ -159,6 +159,14 @@
        return System.currentTimeMillis() - latestFinishTime.getTime() >= intervalMillis;
    }
    private void safeMarkLastTriggerGuard(int intervalMinutes) {
        try {
            markLastTriggerGuard(intervalMinutes);
        } catch (Exception exception) {
            log.warn("Auto tune coordinator failed to write last trigger guard", exception);
        }
    }
    private void markLastTriggerGuard(int intervalMinutes) {
        long expireSeconds = intervalMinutes * 60L;
        redisUtil.set(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key,
src/test/java/com/zy/ai/service/AutoTuneCoordinatorServiceImplTest.java
@@ -346,6 +346,29 @@
    }
    @Test
    void coordinatorRunsAgentAndReleasesLockWhenGuardWriteFails() {
        AutoTuneAgentService.AutoTuneAgentResult agentResult = successfulAgentResult();
        when(configService.getConfigValue("aiAutoTuneEnabled", "N")).thenReturn("Y");
        when(configService.getConfigValue("aiAutoTuneIntervalMinutes", "10")).thenReturn("10");
        when(wrkMastService.count(any(Wrapper.class))).thenReturn(1L);
        when(redisUtil.get(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key)).thenReturn(null);
        when(aiAutoTuneJobService.list(any(Wrapper.class))).thenReturn(Collections.emptyList());
        when(redisUtil.trySetStringIfAbsent(anyString(), anyString(), anyLong())).thenReturn(true);
        doThrow(new RuntimeException("guard failed"))
                .when(redisUtil)
                .set(eq(RedisKeyType.AI_AUTO_TUNE_LAST_TRIGGER_GUARD.key), any(), eq(600L));
        when(autoTuneAgentService.runAutoTune(AutoTuneTriggerType.AUTO.getCode())).thenReturn(agentResult);
        AutoTuneCoordinatorService.AutoTuneCoordinatorResult result = coordinatorService().runAutoTuneIfEligible();
        assertFalse(result.getSkipped());
        assertTrue(result.getTriggered());
        assertSame(agentResult, result.getAgentResult());
        verify(autoTuneAgentService).runAutoTune(AutoTuneTriggerType.AUTO.getCode());
        verify(redisUtil).compareAndDelete(anyString(), anyString());
    }
    @Test
    void coordinatorSetsGuardWhenAgentReturnsFailure() {
        AutoTuneAgentService.AutoTuneAgentResult agentResult = failedAgentResult();
        when(configService.getConfigValue("aiAutoTuneEnabled", "N")).thenReturn("Y");