Skip to content

Commit f81102b

Browse files
DaanHooglanddhslove
authored andcommitted
refactor create duplicate alert check (apache#10544)
1 parent 65a5766 commit f81102b

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

server/src/main/java/com/cloud/alert/AlertManagerImpl.java

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.UnsupportedEncodingException;
2020
import java.text.DecimalFormat;
2121
import java.util.ArrayList;
22+
import java.util.Arrays;
2223
import java.util.Date;
2324
import java.util.HashMap;
2425
import java.util.HashSet;
@@ -88,10 +89,25 @@
8889
import com.cloud.utils.component.ManagerBase;
8990
import com.cloud.utils.concurrency.NamedThreadFactory;
9091
import com.cloud.utils.db.SearchCriteria;
92+
import org.jetbrains.annotations.Nullable;
9193

9294
public class AlertManagerImpl extends ManagerBase implements AlertManager, Configurable {
9395
protected Logger logger = LogManager.getLogger(AlertManagerImpl.class.getName());
9496

97+
public static final List<AlertType> ALERTS = Arrays.asList(AlertType.ALERT_TYPE_HOST
98+
, AlertType.ALERT_TYPE_USERVM
99+
, AlertType.ALERT_TYPE_DOMAIN_ROUTER
100+
, AlertType.ALERT_TYPE_CONSOLE_PROXY
101+
, AlertType.ALERT_TYPE_SSVM
102+
, AlertType.ALERT_TYPE_STORAGE_MISC
103+
, AlertType.ALERT_TYPE_MANAGEMENT_NODE
104+
, AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED
105+
, AlertType.ALERT_TYPE_UPLOAD_FAILED
106+
, AlertType.ALERT_TYPE_OOBM_AUTH_ERROR
107+
, AlertType.ALERT_TYPE_HA_ACTION
108+
, AlertType.ALERT_TYPE_CA_CERT);
109+
protected Logger logger = Logger.getLogger(AlertManagerImpl.class.getName());
110+
95111
private static final long INITIAL_CAPACITY_CHECK_DELAY = 30L * 1000L; // Thirty seconds expressed in milliseconds.
96112

97113
private static final DecimalFormat DfPct = new DecimalFormat("###.##");
@@ -760,20 +776,11 @@ public void sendAlert(AlertType alertType, long dataCenterId, Long podId, Long c
760776

761777
public void sendAlert(AlertType alertType, DataCenter dataCenter, Pod pod, Cluster cluster, String subject, String content)
762778
throws MessagingException, UnsupportedEncodingException {
763-
logger.warn(String.format("alertType=[%s] dataCenter=[%s] pod=[%s] cluster=[%s] message=[%s].", alertType, dataCenter, pod, cluster, subject));
764-
AlertVO alert = null;
765779
Long clusterId = cluster == null ? null : cluster.getId();
766780
Long podId = pod == null ? null : pod.getId();
767781
long dcId = dataCenter == null ? 0L : dataCenter.getId();
768-
if ((alertType != AlertManager.AlertType.ALERT_TYPE_HOST) && (alertType != AlertManager.AlertType.ALERT_TYPE_USERVM)
769-
&& (alertType != AlertManager.AlertType.ALERT_TYPE_DOMAIN_ROUTER) && (alertType != AlertManager.AlertType.ALERT_TYPE_CONSOLE_PROXY)
770-
&& (alertType != AlertManager.AlertType.ALERT_TYPE_SSVM) && (alertType != AlertManager.AlertType.ALERT_TYPE_STORAGE_MISC)
771-
&& (alertType != AlertManager.AlertType.ALERT_TYPE_MANAGEMENT_NODE) && (alertType != AlertManager.AlertType.ALERT_TYPE_RESOURCE_LIMIT_EXCEEDED)
772-
&& (alertType != AlertManager.AlertType.ALERT_TYPE_UPLOAD_FAILED) && (alertType != AlertManager.AlertType.ALERT_TYPE_OOBM_AUTH_ERROR)
773-
&& (alertType != AlertManager.AlertType.ALERT_TYPE_HA_ACTION) && (alertType != AlertManager.AlertType.ALERT_TYPE_CA_CERT)
774-
&& (alertType != AlertManager.AlertType.EVENT_USER_SESSION_BLOCK)) {
775-
alert = _alertDao.getLastAlert(alertType.getType(), dcId, podId, clusterId);
776-
}
782+
logger.warn(String.format("alertType=[%s] dataCenterId=[%s] podId=[%s] clusterId=[%s] message=[%s].", alertType, dataCenterId, podId, clusterId, subject));
783+
AlertVO alert = getAlertForTrivialAlertType(alertType, dataCenterId, podId, clusterId);
777784

778785
if (alert == null) {
779786
AlertVO newAlert = new AlertVO();
@@ -815,6 +822,15 @@ public void sendAlert(AlertType alertType, DataCenter dataCenter, Pod pod, Clust
815822

816823
}
817824

825+
@Nullable
826+
private AlertVO getAlertForTrivialAlertType(AlertType alertType, long dataCenterId, Long podId, Long clusterId) {
827+
AlertVO alert = null;
828+
if (!ALERTS.contains(alertType)) {
829+
alert = _alertDao.getLastAlert(alertType.getType(), dataCenterId, podId, clusterId);
830+
}
831+
return alert;
832+
}
833+
818834
protected void sendMessage(SMTPMailProperties mailProps) {
819835
_executor.execute(new Runnable() {
820836
@Override

0 commit comments

Comments
 (0)