Quellcode durchsuchen

[alerter]fixbug重启后状态异常监控无法触发恢复告警

tomsun28 vor 4 Jahren
Ursprung
Commit
c31a22e3ad

+ 12 - 1
alerter/src/main/java/com/usthe/alert/calculate/CalculateAlarm.java

@@ -4,17 +4,20 @@ import com.googlecode.aviator.AviatorEvaluator;
 import com.googlecode.aviator.Expression;
 import com.usthe.alert.AlerterWorkerPool;
 import com.usthe.alert.AlerterDataQueue;
+import com.usthe.alert.dao.AlertMonitorDao;
 import com.usthe.common.entity.alerter.Alert;
 import com.usthe.common.entity.alerter.AlertDefine;
 import com.usthe.alert.service.AlertDefineService;
 import com.usthe.alert.util.AlertTemplateUtil;
 import com.usthe.collector.dispatch.export.MetricsDataExporter;
+import com.usthe.common.entity.manager.Monitor;
 import com.usthe.common.entity.message.CollectRep;
 import com.usthe.common.util.CommonConstants;
 import com.usthe.common.util.CommonUtil;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.Configuration;
 
+import java.util.Arrays;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -37,13 +40,21 @@ public class CalculateAlarm {
     private Map<Long, CollectRep.Code> triggeredMonitorStateAlertMap;
 
     public CalculateAlarm (AlerterWorkerPool workerPool, AlerterDataQueue dataQueue,
-                           AlertDefineService alertDefineService, MetricsDataExporter dataExporter) {
+                           AlertDefineService alertDefineService, MetricsDataExporter dataExporter,
+                           AlertMonitorDao monitorDao) {
         this.workerPool = workerPool;
         this.dataQueue = dataQueue;
         this.dataExporter = dataExporter;
         this.alertDefineService = alertDefineService;
         this.triggeredAlertMap = new ConcurrentHashMap<>(128);
         this.triggeredMonitorStateAlertMap = new ConcurrentHashMap<>(128);
+        // 初始化stateAlertMap
+        List<Monitor> monitors = monitorDao.findMonitorsByStatusIn(Arrays.asList((byte)2, (byte)3));
+        if (monitors != null) {
+            for (Monitor monitor : monitors) {
+                this.triggeredMonitorStateAlertMap.put(monitor.getId(), CollectRep.Code.UN_AVAILABLE);
+            }
+        }
         startCalculate();
     }
 

+ 23 - 0
alerter/src/main/java/com/usthe/alert/dao/AlertMonitorDao.java

@@ -0,0 +1,23 @@
+package com.usthe.alert.dao;
+
+import com.usthe.common.entity.manager.Monitor;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+import java.util.List;
+
+/**
+ * Alert Monitor 数据库操作
+ * @author tomsun28
+ * @date 2021/11/14 11:24
+ */
+public interface AlertMonitorDao extends JpaRepository<Monitor, Long>, JpaSpecificationExecutor<Monitor> {
+
+    /**
+     * 查询指定监控状态的监控
+     * @param status 监控状态
+     * @return 监控列表
+     */
+    List<Monitor> findMonitorsByStatusIn(List<Byte> status);
+
+}