|
@@ -17,8 +17,6 @@
|
|
|
|
|
|
package org.apache.dolphinscheduler.alert;
|
|
package org.apache.dolphinscheduler.alert;
|
|
|
|
|
|
-import com.google.common.collect.Lists;
|
|
|
|
-import org.apache.commons.collections.CollectionUtils;
|
|
|
|
import org.apache.dolphinscheduler.alert.api.AlertChannel;
|
|
import org.apache.dolphinscheduler.alert.api.AlertChannel;
|
|
import org.apache.dolphinscheduler.alert.api.AlertConstants;
|
|
import org.apache.dolphinscheduler.alert.api.AlertConstants;
|
|
import org.apache.dolphinscheduler.alert.api.AlertData;
|
|
import org.apache.dolphinscheduler.alert.api.AlertData;
|
|
@@ -34,20 +32,28 @@ import org.apache.dolphinscheduler.common.utils.JSONUtils;
|
|
import org.apache.dolphinscheduler.dao.AlertDao;
|
|
import org.apache.dolphinscheduler.dao.AlertDao;
|
|
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;
|
|
|
|
+import org.apache.dolphinscheduler.dao.entity.AlertSendStatus;
|
|
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand;
|
|
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseCommand;
|
|
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult;
|
|
import org.apache.dolphinscheduler.remote.command.alert.AlertSendResponseResult;
|
|
-import org.slf4j.Logger;
|
|
|
|
-import org.slf4j.LoggerFactory;
|
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
-import javax.annotation.Nullable;
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
|
+
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
import java.util.Optional;
|
|
import java.util.Optional;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.CompletableFuture;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
+import javax.annotation.Nullable;
|
|
|
|
+
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import com.google.common.collect.Lists;
|
|
|
|
+
|
|
@Service
|
|
@Service
|
|
public final class AlertSenderService extends Thread {
|
|
public final class AlertSenderService extends Thread {
|
|
|
|
|
|
@@ -71,23 +77,29 @@ public final class AlertSenderService extends Thread {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void run() {
|
|
public void run() {
|
|
- logger.info("alert sender started");
|
|
+ logger.info("Alert sender thread started");
|
|
while (!ServerLifeCycleManager.isStopped()) {
|
|
while (!ServerLifeCycleManager.isStopped()) {
|
|
try {
|
|
try {
|
|
List<Alert> alerts = alertDao.listPendingAlerts();
|
|
List<Alert> alerts = alertDao.listPendingAlerts();
|
|
|
|
+ if (CollectionUtils.isEmpty(alerts)) {
|
|
|
|
+ logger.debug("There is not waiting alerts");
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
AlertServerMetrics.registerPendingAlertGauge(alerts::size);
|
|
AlertServerMetrics.registerPendingAlertGauge(alerts::size);
|
|
this.send(alerts);
|
|
this.send(alerts);
|
|
- ThreadUtils.sleep(Constants.SLEEP_TIME_MILLIS * 5L);
|
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
- logger.error("alert sender thread error", e);
|
|
+ logger.error("Alert sender thread meet an exception", e);
|
|
|
|
+ } finally {
|
|
|
|
+ ThreadUtils.sleep(Constants.SLEEP_TIME_MILLIS * 5L);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ logger.info("Alert sender thread stopped");
|
|
}
|
|
}
|
|
|
|
|
|
public void send(List<Alert> alerts) {
|
|
public void send(List<Alert> alerts) {
|
|
for (Alert alert : alerts) {
|
|
for (Alert alert : alerts) {
|
|
|
|
|
|
- int alertId = Optional.ofNullable(alert.getId()).orElse(0);
|
|
+ int alertId = alert.getId();
|
|
int alertGroupId = Optional.ofNullable(alert.getAlertGroupId()).orElse(0);
|
|
int alertGroupId = Optional.ofNullable(alert.getAlertGroupId()).orElse(0);
|
|
List<AlertPluginInstance> alertInstanceList = alertDao.listInstanceByAlertGroupId(alertGroupId);
|
|
List<AlertPluginInstance> alertInstanceList = alertDao.listInstanceByAlertGroupId(alertGroupId);
|
|
if (CollectionUtils.isEmpty(alertInstanceList)) {
|
|
if (CollectionUtils.isEmpty(alertInstanceList)) {
|
|
@@ -107,16 +119,23 @@ public final class AlertSenderService extends Thread {
|
|
.build();
|
|
.build();
|
|
|
|
|
|
int sendSuccessCount = 0;
|
|
int sendSuccessCount = 0;
|
|
|
|
+ List<AlertSendStatus> alertSendStatuses = new ArrayList<>();
|
|
List<AlertResult> alertResults = new ArrayList<>();
|
|
List<AlertResult> alertResults = new ArrayList<>();
|
|
for (AlertPluginInstance instance : alertInstanceList) {
|
|
for (AlertPluginInstance instance : alertInstanceList) {
|
|
AlertResult alertResult = this.alertResultHandler(instance, alertData);
|
|
AlertResult alertResult = this.alertResultHandler(instance, alertData);
|
|
if (alertResult != null) {
|
|
if (alertResult != null) {
|
|
- AlertStatus sendStatus = Boolean.parseBoolean(String.valueOf(alertResult.getStatus()))
|
|
+ AlertStatus sendStatus = Boolean.parseBoolean(alertResult.getStatus())
|
|
? AlertStatus.EXECUTION_SUCCESS
|
|
? AlertStatus.EXECUTION_SUCCESS
|
|
: AlertStatus.EXECUTION_FAILURE;
|
|
: AlertStatus.EXECUTION_FAILURE;
|
|
- alertDao.addAlertSendStatus(sendStatus, JSONUtils.toJsonString(alertResult), alertId,
|
|
+ AlertSendStatus alertSendStatus = AlertSendStatus.builder()
|
|
- instance.getId());
|
|
+ .alertId(alertId)
|
|
- if (sendStatus.equals(AlertStatus.EXECUTION_SUCCESS)) {
|
|
+ .alertPluginInstanceId(instance.getId())
|
|
|
|
+ .sendStatus(sendStatus)
|
|
|
|
+ .log(JSONUtils.toJsonString(alertResult))
|
|
|
|
+ .createTime(new Date())
|
|
|
|
+ .build();
|
|
|
|
+ alertSendStatuses.add(alertSendStatus);
|
|
|
|
+ if (AlertStatus.EXECUTION_SUCCESS.equals(sendStatus)) {
|
|
sendSuccessCount++;
|
|
sendSuccessCount++;
|
|
AlertServerMetrics.incAlertSuccessCount();
|
|
AlertServerMetrics.incAlertSuccessCount();
|
|
} else {
|
|
} else {
|
|
@@ -131,7 +150,11 @@ public final class AlertSenderService extends Thread {
|
|
} else if (sendSuccessCount < alertInstanceList.size()) {
|
|
} else if (sendSuccessCount < alertInstanceList.size()) {
|
|
alertStatus = AlertStatus.EXECUTION_PARTIAL_SUCCESS;
|
|
alertStatus = AlertStatus.EXECUTION_PARTIAL_SUCCESS;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
alertDao.updateAlert(alertStatus, JSONUtils.toJsonString(alertResults), alertId);
|
|
alertDao.updateAlert(alertStatus, JSONUtils.toJsonString(alertResults), alertId);
|
|
|
|
+ alertDao.insertAlertSendStatus(alertSendStatuses);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|