Skip to content

Commit d8ea502

Browse files
committed
allow repetition for custom alerts
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent de6ff00 commit d8ea502

File tree

3 files changed

+25
-34
lines changed

3 files changed

+25
-34
lines changed

api/src/main/java/org/apache/cloudstack/alert/AlertService.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616
// under the License.
1717
package org.apache.cloudstack.alert;
1818

19-
import java.util.HashMap;
2019
import java.util.HashSet;
21-
import java.util.Map;
2220
import java.util.Set;
2321

2422
import com.cloud.capacity.Capacity;
2523
import com.cloud.exception.InvalidParameterValueException;
2624

2725
public interface AlertService {
2826
public static class AlertType {
29-
private static Set<AlertType> defaultAlertTypes = new HashSet<AlertType>();
30-
private static Map<String, AlertType> allAlertTypesMap = new HashMap<>();
27+
private static final Set<AlertType> defaultAlertTypes = new HashSet<>();
3128
private final String name;
3229
private final short type;
3330
private final boolean repetitionAllowed;
@@ -39,7 +36,6 @@ private AlertType(short type, String name, boolean isDefault, boolean repetition
3936
if (isDefault) {
4037
defaultAlertTypes.add(this);
4138
}
42-
allAlertTypesMap.put(name, this);
4339
}
4440

4541
public static final AlertType ALERT_TYPE_MEMORY = new AlertType(Capacity.CAPACITY_TYPE_MEMORY, "ALERT.MEMORY", true, false);
@@ -120,10 +116,6 @@ public static AlertType generateAlert(short type, String name) {
120116
return new AlertType(type, name, false, false);
121117
}
122118
}
123-
124-
public static AlertType getAlertTypeByName(String name) {
125-
return allAlertTypesMap.get(name);
126-
}
127119
}
128120

129121
boolean generateAlert(AlertType alertType, long dataCenterId, Long podId, String msg);

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

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@
3636
import javax.mail.MessagingException;
3737
import javax.naming.ConfigurationException;
3838

39-
import com.cloud.dc.DataCenter;
40-
import com.cloud.dc.Pod;
41-
import com.cloud.org.Cluster;
42-
4339
import org.apache.cloudstack.backup.BackupManager;
4440
import org.apache.cloudstack.framework.config.ConfigDepot;
4541
import org.apache.cloudstack.framework.config.ConfigKey;
@@ -71,9 +67,11 @@
7167
import com.cloud.configuration.Config;
7268
import com.cloud.configuration.ConfigurationManager;
7369
import com.cloud.dc.ClusterVO;
70+
import com.cloud.dc.DataCenter;
7471
import com.cloud.dc.DataCenter.NetworkType;
7572
import com.cloud.dc.DataCenterVO;
7673
import com.cloud.dc.HostPodVO;
74+
import com.cloud.dc.Pod;
7775
import com.cloud.dc.Vlan.VlanType;
7876
import com.cloud.dc.dao.ClusterDao;
7977
import com.cloud.dc.dao.DataCenterDao;
@@ -87,6 +85,7 @@
8785
import com.cloud.host.dao.HostDao;
8886
import com.cloud.network.Ipv6Service;
8987
import com.cloud.network.dao.IPAddressDao;
88+
import com.cloud.org.Cluster;
9089
import com.cloud.org.Grouping.AllocationState;
9190
import com.cloud.resource.ResourceManager;
9291
import com.cloud.storage.StorageManager;
@@ -164,7 +163,7 @@ public class AlertManagerImpl extends ManagerBase implements AlertManager, Confi
164163
protected String[] recipients = null;
165164
protected String senderAddress = null;
166165

167-
private final List<AlertType> allowedRepetitiveAlertTypes = new ArrayList<>();
166+
private final List<String> allowedRepetitiveAlertTypeNames = new ArrayList<>();
168167

169168
public AlertManagerImpl() {
170169
_executor = Executors.newCachedThreadPool(new NamedThreadFactory("Email-Alerts-Sender"));
@@ -254,23 +253,21 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
254253
}
255254

256255
protected void setupRepetitiveAlertTypes() {
257-
allowedRepetitiveAlertTypes.clear();
256+
allowedRepetitiveAlertTypeNames.clear();
258257
String allowedRepetitiveAlertsStr = AllowedRepetitiveAlertTypes.value();
259258
logger.trace("Allowed repetitive alert types specified by {}: {} ", AllowedRepetitiveAlertTypes.key(),
260259
allowedRepetitiveAlertsStr);
261260
if (StringUtils.isBlank(allowedRepetitiveAlertsStr)) {
262261
return;
263262
}
264263
String[] allowedRepetitiveAlertTypesArray = allowedRepetitiveAlertsStr.split(",");
265-
for (String alertTypeName : allowedRepetitiveAlertTypesArray) {
266-
AlertType type = AlertType.getAlertTypeByName(alertTypeName.trim());
267-
if (type == null) {
268-
logger.warn("Unknown alert type name: {}, skipping it.", alertTypeName);
264+
for (String allowedTypeName : allowedRepetitiveAlertTypesArray) {
265+
if (StringUtils.isBlank(allowedTypeName)) {
269266
continue;
270267
}
271-
allowedRepetitiveAlertTypes.add(type);
268+
allowedRepetitiveAlertTypeNames.add(allowedTypeName.toLowerCase());
272269
}
273-
logger.trace("{} alert types specified for repetitive alerts", allowedRepetitiveAlertTypes.size());
270+
logger.trace("{} alert types specified for repetitive alerts", allowedRepetitiveAlertTypeNames.size());
274271
}
275272

276273
@Override
@@ -868,7 +865,8 @@ public void sendAlert(AlertType alertType, DataCenter dataCenter, Pod pod, Clust
868865

869866
@Nullable
870867
private AlertVO getAlertForTrivialAlertType(AlertType alertType, long dataCenterId, Long podId, Long clusterId) {
871-
if (alertType.isRepetitionAllowed() || allowedRepetitiveAlertTypes.contains(alertType)) {
868+
if (alertType.isRepetitionAllowed() || (StringUtils.isNotBlank(alertType.getName()) &&
869+
allowedRepetitiveAlertTypeNames.contains(alertType.getName().toLowerCase()))) {
872870
return null;
873871
}
874872
return _alertDao.getLastAlert(alertType.getType(), dataCenterId, podId, clusterId);

server/src/test/java/com/cloud/alert/AlertManagerImplTest.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import javax.mail.MessagingException;
3131
import javax.naming.ConfigurationException;
3232

33-
import org.apache.cloudstack.alert.AlertService;
3433
import org.apache.cloudstack.backup.BackupManager;
3534
import org.apache.cloudstack.framework.config.ConfigKey;
3635
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
@@ -317,39 +316,41 @@ private void mockAllowedRepetitiveAlertTypesConfigKey(String value) {
317316

318317
@Test
319318
public void setupRepetitiveAlertTypesParsesValidAlertTypesCorrectly() {
320-
mockAllowedRepetitiveAlertTypesConfigKey("ALERT.CPU,ALERT.MEMORY");
319+
mockAllowedRepetitiveAlertTypesConfigKey(AlertManager.AlertType.ALERT_TYPE_CPU.getName() + "," + AlertManager.AlertType.ALERT_TYPE_MEMORY.getName());
321320
alertManagerImplMock.setupRepetitiveAlertTypes();
322-
List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes");
321+
List<String> expectedTypes = (List<String>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypeNames");
323322
Assert.assertNotNull(expectedTypes);
324323
Assert.assertEquals(2, expectedTypes.size());
325-
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU));
326-
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_MEMORY));
324+
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU.getName().toLowerCase()));
325+
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_MEMORY.getName().toLowerCase()));
327326
}
328327

329328
@Test
330329
public void setupRepetitiveAlertTypesHandlesEmptyConfigValue() {
331330
mockAllowedRepetitiveAlertTypesConfigKey("");
332331
alertManagerImplMock.setupRepetitiveAlertTypes();
333-
List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes");
332+
List<String> expectedTypes = (List<String>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypeNames");
334333
Assert.assertNotNull(expectedTypes);
335334
Assert.assertTrue(expectedTypes.isEmpty());
336335
}
337336

338337
@Test
339-
public void setupRepetitiveAlertTypesIgnoresUnknownAlertTypes() {
340-
mockAllowedRepetitiveAlertTypesConfigKey("ALERT.CPU,UNKNOWN_ALERT_TYPE");
338+
public void setupRepetitiveAlertTypesIgnoresCustomAlertTypes() {
339+
String customAlertTypeName = "CUSTOM_ALERT_TYPE";
340+
mockAllowedRepetitiveAlertTypesConfigKey(AlertManager.AlertType.ALERT_TYPE_CPU.getName() + "," + customAlertTypeName);
341341
alertManagerImplMock.setupRepetitiveAlertTypes();
342-
List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes");
342+
List<String> expectedTypes = (List<String>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypeNames");
343343
Assert.assertNotNull(expectedTypes);
344-
Assert.assertEquals(1, expectedTypes.size());
345-
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU));
344+
Assert.assertEquals(2, expectedTypes.size());
345+
Assert.assertTrue(expectedTypes.contains(AlertManager.AlertType.ALERT_TYPE_CPU.getName().toLowerCase()));
346+
Assert.assertTrue(expectedTypes.contains(customAlertTypeName.toLowerCase()));
346347
}
347348

348349
@Test
349350
public void setupRepetitiveAlertTypesHandlesNullConfigValue() {
350351
mockAllowedRepetitiveAlertTypesConfigKey(null);
351352
alertManagerImplMock.setupRepetitiveAlertTypes();
352-
List<AlertService.AlertType> expectedTypes = (List<AlertService.AlertType>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypes");
353+
List<String> expectedTypes = (List<String>)ReflectionTestUtils.getField(alertManagerImplMock, "allowedRepetitiveAlertTypeNames");
353354
Assert.assertNotNull(expectedTypes);
354355
Assert.assertTrue(expectedTypes.isEmpty());
355356
}

0 commit comments

Comments
 (0)