Skip to content

Commit 76a962f

Browse files
authored
[Fix-16820][Alert] fix alert status after alert sent by AlertSender (#16824)
1 parent 9856453 commit 76a962f

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

dolphinscheduler-alert/dolphinscheduler-alert-server/src/main/java/org/apache/dolphinscheduler/alert/service/AbstractEventSender.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ public void sendEvent(T event) {
8383
alertSendStatuses.add(alertSendStatus);
8484
}
8585
long failureCount = alertSendStatuses.stream()
86-
.map(alertSendStatus -> alertSendStatus.getSendStatus() == AlertStatus.EXECUTION_FAILURE)
86+
.filter(alertSendStatus -> alertSendStatus.getSendStatus() == AlertStatus.EXECUTION_FAILURE)
8787
.count();
8888
long successCount = alertSendStatuses.stream()
89-
.map(alertSendStatus -> alertSendStatus.getSendStatus() == AlertStatus.EXECUTION_SUCCESS)
89+
.filter(alertSendStatus -> alertSendStatus.getSendStatus() == AlertStatus.EXECUTION_SUCCESS)
9090
.count();
9191
if (successCount == 0) {
9292
onError(event, JSONUtils.toJsonString(alertSendStatuses));

dolphinscheduler-alert/dolphinscheduler-alert-server/src/test/java/org/apache/dolphinscheduler/alert/runner/AlertSenderTest.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717

1818
package org.apache.dolphinscheduler.alert.runner;
1919

20+
import static org.mockito.ArgumentMatchers.anyInt;
21+
import static org.mockito.ArgumentMatchers.anyString;
22+
import static org.mockito.ArgumentMatchers.eq;
2023
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.verify;
2125
import static org.mockito.Mockito.when;
2226

2327
import org.apache.dolphinscheduler.alert.api.AlertChannel;
2428
import org.apache.dolphinscheduler.alert.api.AlertResult;
2529
import org.apache.dolphinscheduler.alert.config.AlertConfig;
2630
import org.apache.dolphinscheduler.alert.plugin.AlertPluginManager;
2731
import org.apache.dolphinscheduler.alert.service.AlertSender;
32+
import org.apache.dolphinscheduler.common.enums.AlertStatus;
33+
import org.apache.dolphinscheduler.common.enums.AlertType;
2834
import org.apache.dolphinscheduler.common.enums.WarningType;
2935
import org.apache.dolphinscheduler.common.utils.JSONUtils;
3036
import org.apache.dolphinscheduler.dao.AlertDao;
@@ -148,22 +154,46 @@ void testRun() {
148154
alert.setTitle(TITLE);
149155
alert.setContent(CONTENT);
150156
alert.setWarningType(WarningType.FAILURE);
157+
alert.setAlertType(AlertType.TASK_FAILURE);
151158

152-
int pluginDefineId = 1;
153-
String pluginInstanceParams = "alert-instance-mail-params";
154-
String pluginInstanceName = "alert-instance-mail";
155159
List<AlertPluginInstance> alertInstanceList = new ArrayList<>();
160+
when(alertDao.listInstanceByAlertGroupId(ALERT_GROUP_ID)).thenReturn(alertInstanceList);
161+
162+
// 1. alert plugin send success
156163
AlertPluginInstance alertPluginInstance = new AlertPluginInstance(
157-
pluginDefineId, pluginInstanceParams, pluginInstanceName);
164+
PLUGIN_DEFINE_ID, PLUGIN_INSTANCE_PARAMS, PLUGIN_INSTANCE_NAME);
165+
alertPluginInstance.setId(alertPluginInstance.getPluginDefineId());
158166
alertInstanceList.add(alertPluginInstance);
159-
when(alertDao.listInstanceByAlertGroupId(ALERT_GROUP_ID)).thenReturn(alertInstanceList);
160167

161-
AlertResult alertResult = new AlertResult();
162-
alertResult.setSuccess(true);
163-
alertResult.setMessage(String.format("Alert Plugin %s send success", pluginInstanceName));
164-
Assertions.assertTrue(alertResult.isSuccess());
165-
when(alertDao.listInstanceByAlertGroupId(1)).thenReturn(new ArrayList<>());
168+
AlertChannel alertChannelMock = mock(AlertChannel.class);
169+
when(alertPluginManager.getAlertChannel(PLUGIN_DEFINE_ID)).thenReturn(Optional.of(alertChannelMock));
170+
AlertResult alertSuccessResult = AlertResult.success();
171+
when(alertChannelMock.process(Mockito.any())).thenReturn(alertSuccessResult);
172+
alertSender.sendEvent(alert);
173+
verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_SUCCESS), anyString(), anyInt());
174+
175+
// 2. alert plugin send failed
176+
AlertPluginInstance otherAlertPluginInstance = new AlertPluginInstance(
177+
PLUGIN_DEFINE_ID + 1, PLUGIN_INSTANCE_PARAMS, PLUGIN_INSTANCE_NAME);
178+
otherAlertPluginInstance.setId(otherAlertPluginInstance.getPluginDefineId());
179+
alertInstanceList.clear();
180+
alertInstanceList.add(otherAlertPluginInstance);
181+
182+
AlertChannel otherAlertChannelMock = mock(AlertChannel.class);
183+
when(alertPluginManager.getAlertChannel(PLUGIN_DEFINE_ID + 1)).thenReturn(Optional.of(otherAlertChannelMock));
184+
AlertResult alertFailedResult =
185+
AlertResult.fail(String.format("Alert Plugin %s send failed", PLUGIN_INSTANCE_NAME));
186+
when(otherAlertChannelMock.process(Mockito.any())).thenReturn(alertFailedResult);
166187
alertSender.sendEvent(alert);
188+
verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_FAILURE), anyString(), anyInt());
189+
190+
// 3. alert plugin send partial success
191+
alertInstanceList.clear();
192+
alertInstanceList.add(alertPluginInstance);
193+
alertInstanceList.add(otherAlertPluginInstance);
194+
alertSender.sendEvent(alert);
195+
verify(alertDao).updateAlert(eq(AlertStatus.EXECUTION_PARTIAL_SUCCESS), anyString(), anyInt());
196+
167197
}
168198

169199
@Test

0 commit comments

Comments
 (0)