| | |
| | | return AutoTuneCoordinatorResult.skipped("interval_not_reached"); |
| | | } |
| | | |
| | | return runAgentWithLock(AutoTuneTriggerType.AUTO.getCode(), intervalMinutes, true); |
| | | } |
| | | |
| | | @Override |
| | | public AutoTuneCoordinatorResult runManualAutoTune() { |
| | | return runAgentWithLock(AutoTuneTriggerType.MANUAL.getCode(), null, false); |
| | | } |
| | | |
| | | private AutoTuneCoordinatorResult runAgentWithLock(String triggerType, Integer intervalMinutes, boolean writeGuard) { |
| | | String lockKey = RedisKeyType.AI_AUTO_TUNE_RUNNING_LOCK.key; |
| | | String lockToken = UUID.randomUUID().toString(); |
| | | if (!redisUtil.trySetStringIfAbsent(lockKey, lockToken, RUNNING_LOCK_SECONDS)) { |
| | |
| | | |
| | | AutoTuneAgentService.AutoTuneAgentResult agentResult = null; |
| | | try { |
| | | safeMarkLastTriggerGuard(intervalMinutes); |
| | | agentResult = autoTuneAgentService.runAutoTune(AutoTuneTriggerType.AUTO.getCode()); |
| | | if (writeGuard && intervalMinutes != null) { |
| | | safeMarkLastTriggerGuard(intervalMinutes); |
| | | } |
| | | agentResult = autoTuneAgentService.runAutoTune(triggerType); |
| | | safeWriteOperateLog(agentResult); |
| | | return AutoTuneCoordinatorResult.triggered(agentResult); |
| | | } catch (Exception exception) { |
| | | log.error("Auto tune coordinator failed to run agent", exception); |
| | | agentResult = failedAgentResult(exception); |
| | | agentResult = failedAgentResult(triggerType, exception); |
| | | safeWriteOperateLog(agentResult); |
| | | return AutoTuneCoordinatorResult.triggered(agentResult); |
| | | } finally { |
| | |
| | | String.valueOf(System.currentTimeMillis()), expireSeconds); |
| | | } |
| | | |
| | | private AutoTuneAgentService.AutoTuneAgentResult failedAgentResult(Exception exception) { |
| | | private AutoTuneAgentService.AutoTuneAgentResult failedAgentResult(String triggerType, Exception exception) { |
| | | AutoTuneAgentService.AutoTuneAgentResult result = new AutoTuneAgentService.AutoTuneAgentResult(); |
| | | result.setSuccess(false); |
| | | result.setTriggerType(AutoTuneTriggerType.AUTO.getCode()); |
| | | result.setTriggerType(triggerType); |
| | | result.setSummary("自动调参后台任务执行异常: " + exception.getMessage()); |
| | | result.setToolCallCount(0); |
| | | result.setLlmCallCount(0); |
| | |
| | | return; |
| | | } |
| | | OperateLog operateLog = new OperateLog(); |
| | | operateLog.setAction("ai_auto_tune_background_scheduler"); |
| | | operateLog.setAction(resolveOperateLogAction(agentResult)); |
| | | operateLog.setUserId(SYSTEM_USER_ID); |
| | | operateLog.setIp("system"); |
| | | operateLog.setRequest(JSON.toJSONString(buildRequestSummary(agentResult))); |
| | |
| | | operateLogService.save(operateLog); |
| | | } |
| | | |
| | | private String resolveOperateLogAction(AutoTuneAgentService.AutoTuneAgentResult agentResult) { |
| | | if (agentResult != null && AutoTuneTriggerType.MANUAL.getCode().equals(agentResult.getTriggerType())) { |
| | | return "ai_auto_tune_manual_trigger"; |
| | | } |
| | | return "ai_auto_tune_background_scheduler"; |
| | | } |
| | | |
| | | private Map<String, Object> buildRequestSummary(AutoTuneAgentService.AutoTuneAgentResult agentResult) { |
| | | Map<String, Object> request = new LinkedHashMap<>(); |
| | | request.put("trigger", agentResult.getTriggerType()); |