@ -19,9 +19,9 @@ package org.apache.dolphinscheduler.dao;
import org.apache.dolphinscheduler.common.enums.AlertEvent ;
import org.apache.dolphinscheduler.common.enums.AlertEvent ;
import org.apache.dolphinscheduler.common.enums.AlertStatus ;
import org.apache.dolphinscheduler.common.enums.AlertStatus ;
import org.apache.dolphinscheduler.common.enums.AlertType ;
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel ;
import org.apache.dolphinscheduler.common.enums.AlertWarnLevel ;
import org.apache.dolphinscheduler.common.enums.WarningType ;
import org.apache.dolphinscheduler.common.enums.WarningType ;
import org.apache.dolphinscheduler.common.enums.AlertType ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.common.utils.JSONUtils ;
import org.apache.dolphinscheduler.dao.entity.Alert ;
import org.apache.dolphinscheduler.dao.entity.Alert ;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance ;
import org.apache.dolphinscheduler.dao.entity.AlertPluginInstance ;
@ -36,21 +36,31 @@ import org.apache.dolphinscheduler.dao.mapper.AlertMapper;
import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper ;
import org.apache.dolphinscheduler.dao.mapper.AlertPluginInstanceMapper ;
import org.apache.dolphinscheduler.dao.mapper.AlertSendStatusMapper ;
import org.apache.dolphinscheduler.dao.mapper.AlertSendStatusMapper ;
import org.apache.commons.lang.StringUtils ;
import org.apache.commons.codec.digest.DigestUtils ;
import org.apache.commons.lang3.StringUtils ;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Date ;
import java.util.Date ;
import java.util.List ;
import java.util.List ;
import java.util.Optional ;
import java.util.stream.Collectors ;
import java.util.stream.Collectors ;
import org.joda.time.DateTime ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
import com.google.common.collect.Lists ;
import com.google.common.collect.Lists ;
@Component
@Component
public class AlertDao {
public class AlertDao {
@Value ( "${alert.alarm-suppression.crash:60}" )
private Integer crashAlarmSuppression ;
@Autowired
@Autowired
private AlertMapper alertMapper ;
private AlertMapper alertMapper ;
@ -70,6 +80,8 @@ public class AlertDao {
* @return add alert result
* @return add alert result
* /
* /
public int addAlert ( Alert alert ) {
public int addAlert ( Alert alert ) {
String sign = generateSign ( alert ) ;
alert . setSign ( sign ) ;
return alertMapper . insert ( alert ) ;
return alertMapper . insert ( alert ) ;
}
}
@ -82,13 +94,28 @@ public class AlertDao {
* @return update alert result
* @return update alert result
* /
* /
public int updateAlert ( AlertStatus alertStatus , String log , int id ) {
public int updateAlert ( AlertStatus alertStatus , String log , int id ) {
Alert alert = alertMapper . selectById ( id ) ;
Alert alert = new Alert ( ) ;
alert . setId ( id ) ;
alert . setAlertStatus ( alertStatus ) ;
alert . setAlertStatus ( alertStatus ) ;
alert . setUpdateTime ( new Date ( ) ) ;
alert . setUpdateTime ( new Date ( ) ) ;
alert . setLog ( log ) ;
alert . setLog ( log ) ;
return alertMapper . updateById ( alert ) ;
return alertMapper . updateById ( alert ) ;
}
}
/ * *
* generate sign for alert
*
* @param alert alert
* @return sign ' s str
* /
private String generateSign ( Alert alert ) {
return Optional . of ( alert )
. map ( Alert : : getContent )
. map ( DigestUtils : : sha1Hex )
. map ( String : : toLowerCase )
. orElse ( StringUtils . EMPTY ) ;
}
/ * *
/ * *
* add AlertSendStatus
* add AlertSendStatus
*
*
@ -109,13 +136,13 @@ public class AlertDao {
}
}
/ * *
/ * *
* MasterServer or WorkerServer stoped
* MasterServer or WorkerServer stopp ed
*
*
* @param alertGroupId alertGroupId
* @param alertGroupId alertGroupId
* @param host host
* @param host host
* @param serverType serverType
* @param serverType serverType
* /
* /
public void sendServerStopedAlert ( int alertGroupId , String host , String serverType ) {
public void sendServerStopp edAlert ( int alertGroupId , String host , String serverType ) {
ServerAlertContent serverStopAlertContent = ServerAlertContent . newBuilder ( ) .
ServerAlertContent serverStopAlertContent = ServerAlertContent . newBuilder ( ) .
type ( serverType )
type ( serverType )
. host ( host )
. host ( host )
@ -133,8 +160,11 @@ public class AlertDao {
alert . setCreateTime ( new Date ( ) ) ;
alert . setCreateTime ( new Date ( ) ) ;
alert . setUpdateTime ( new Date ( ) ) ;
alert . setUpdateTime ( new Date ( ) ) ;
alert . setAlertType ( AlertType . FAULT_TOLERANCE_WARNING ) ;
alert . setAlertType ( AlertType . FAULT_TOLERANCE_WARNING ) ;
alert . setSign ( generateSign ( alert ) ) ;
// we use this method to avoid insert duplicate alert(issue #5525)
// we use this method to avoid insert duplicate alert(issue #5525)
alertMapper . insertAlertWhenServerCrash ( alert ) ;
// we modified this method to optimize performance(issue #9174)
Date crashAlarmSuppressionStartTime = DateTime . now ( ) . plusMinutes ( - crashAlarmSuppression ) . toDate ( ) ;
alertMapper . insertAlertWhenServerCrash ( alert , crashAlarmSuppressionStartTime ) ;
}
}
/ * *
/ * *
@ -178,6 +208,8 @@ public class AlertDao {
alert . setContent ( content ) ;
alert . setContent ( content ) ;
alert . setCreateTime ( new Date ( ) ) ;
alert . setCreateTime ( new Date ( ) ) ;
alert . setUpdateTime ( new Date ( ) ) ;
alert . setUpdateTime ( new Date ( ) ) ;
String sign = generateSign ( alert ) ;
alert . setSign ( sign ) ;
alertMapper . insert ( alert ) ;
alertMapper . insert ( alert ) ;
}
}
@ -220,7 +252,9 @@ public class AlertDao {
* List alerts that are pending for execution
* List alerts that are pending for execution
* /
* /
public List < Alert > listPendingAlerts ( ) {
public List < Alert > listPendingAlerts ( ) {
return alertMapper . listAlertByStatus ( AlertStatus . WAIT_EXECUTION ) ;
LambdaQueryWrapper < Alert > wrapper = new QueryWrapper < > ( new Alert ( ) ) . lambda ( )
. eq ( Alert : : getAlertStatus , AlertStatus . WAIT_EXECUTION ) ;
return alertMapper . selectList ( wrapper ) ;
}
}
/ * *
/ * *
@ -265,4 +299,8 @@ public class AlertDao {
public void setAlertGroupMapper ( AlertGroupMapper alertGroupMapper ) {
public void setAlertGroupMapper ( AlertGroupMapper alertGroupMapper ) {
this . alertGroupMapper = alertGroupMapper ;
this . alertGroupMapper = alertGroupMapper ;
}
}
public void setCrashAlarmSuppression ( Integer crashAlarmSuppression ) {
this . crashAlarmSuppression = crashAlarmSuppression ;
}
}
}