Junjie
2026-04-27 91fbede5aa117b3c010e9cbb8804df90757dbe5c
fix: isolate auto tune cache refresh failures
2个文件已修改
66 ■■■■■ 已修改文件
src/main/java/com/zy/ai/service/impl/AutoTuneApplyServiceImpl.java 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/ai/service/impl/AutoTuneApplyServiceImpl.java
@@ -141,7 +141,7 @@
                        now,
                        true
                );
                refreshSystemConfigCacheIfNeeded(persistenceResult);
                refreshSystemConfigCacheSafely(persistenceResult);
                return buildResult(job, persistenceResult.getAuditChanges(), false);
            } catch (RuntimeException exception) {
                markWriteFailure(validatedChanges, exception);
@@ -216,7 +216,7 @@
                        sourceChanges,
                        now
                );
                refreshRollbackConfigCacheIfNeeded(persistenceResult);
                refreshRollbackConfigCacheSafely(persistenceResult);
                return buildResult(rollbackJob, persistenceResult.getRollbackChanges(), false);
            } catch (RuntimeException exception) {
                Date failureNow = new Date();
@@ -484,9 +484,9 @@
        }
    }
    private void refreshSystemConfigCacheIfNeeded(ApplyPersistenceResult persistenceResult) {
    private void refreshSystemConfigCacheSafely(ApplyPersistenceResult persistenceResult) {
        if (persistenceResult != null && persistenceResult.isRefreshConfigCache()) {
            configService.refreshSystemConfigCache();
            refreshSystemConfigCacheSafely("apply");
        }
    }
@@ -584,9 +584,17 @@
        return List.of(rollbackChange);
    }
    private void refreshRollbackConfigCacheIfNeeded(RollbackPersistenceResult persistenceResult) {
    private void refreshRollbackConfigCacheSafely(RollbackPersistenceResult persistenceResult) {
        if (persistenceResult != null && persistenceResult.isRefreshConfigCache()) {
            refreshSystemConfigCacheSafely("rollback");
        }
    }
    private void refreshSystemConfigCacheSafely(String scene) {
        try {
            configService.refreshSystemConfigCache();
        } catch (RuntimeException exception) {
            LOGGER.warn("AI自动调参{}已提交,但刷新系统配置缓存失败", scene, exception);
        }
    }
src/test/java/com/zy/ai/service/AutoTuneApplyServiceImplTest.java
@@ -50,8 +50,10 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -316,6 +318,27 @@
    }
    @Test
    void applyCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
        doThrow(new IllegalStateException("cache refresh failed"))
                .when(configService).refreshSystemConfigCache();
        AutoTuneApplyResult result = service.apply(request(false,
                command("sys_config", null, "conveyorStationTaskLimit", "15")
        ));
        List<AiAutoTuneChange> changes = savedChanges();
        AiAutoTuneJob updatedJob = updatedJob();
        assertTrue(result.getSuccess());
        assertEquals("success", updatedJob.getStatus());
        assertEquals(1, changes.size());
        assertEquals("success", changes.get(0).getResultStatus());
        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
        verify(configService).refreshSystemConfigCache();
    }
    @Test
    void writeFailureReturnsFailedAuditWithoutThrowing() {
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "10"));
        when(configService.saveConfigValue("conveyorStationTaskLimit", "15")).thenThrow(new IllegalStateException("db write failed"));
@@ -438,6 +461,31 @@
    }
    @Test
    void rollbackCacheRefreshFailureDoesNotAppendFailedAuditOrJob() {
        AiAutoTuneJob latestRealJob = job(10L, "manual", "success");
        AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15");
        when(aiAutoTuneChangeService.list(any(Wrapper.class)))
                .thenReturn(List.of(configChange))
                .thenReturn(List.of(configChange));
        when(aiAutoTuneJobService.getById(10L)).thenReturn(latestRealJob);
        when(configService.getOne(any(Wrapper.class))).thenReturn(config("conveyorStationTaskLimit", "15"));
        doThrow(new IllegalStateException("cache refresh failed"))
                .when(configService).refreshSystemConfigCache();
        AutoTuneApplyResult result = service.rollbackLastSuccessfulJob("manual rollback");
        List<AiAutoTuneChange> changes = savedChanges();
        AiAutoTuneJob updatedJob = updatedJob();
        assertTrue(result.getSuccess());
        assertEquals("success", updatedJob.getStatus());
        assertEquals(1, changes.size());
        assertEquals("success", changes.get(0).getResultStatus());
        verify(aiAutoTuneChangeService, times(1)).saveBatch(any(Collection.class));
        verify(aiAutoTuneJobService, times(1)).updateById(any(AiAutoTuneJob.class));
        verify(configService).refreshSystemConfigCache();
    }
    @Test
    void rollbackAuditSaveBatchFailureRollsBackTargetWriteAndReturnsFailedAudit() {
        AiAutoTuneJob latestRealJob = job(10L, "manual", "success");
        AiAutoTuneChange configChange = successChange(10L, "sys_config", "", "conveyorStationTaskLimit", "10", "15");