Skip to content

Commit

Permalink
fixed concurrent alert file loading issue with messageids
Browse files Browse the repository at this point in the history
  • Loading branch information
vargm committed Jul 4, 2022
1 parent 2b80439 commit 8473e46
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,12 @@ protected void postProcess(LogEntry logEntry, ArrayList<String> logEntryValueLis
alertMessageThresholdKPI, this, locale);

alertMessageReportModelMap.put(alertId, alertMessageReportModel);

} catch (Exception e) {
Map<Integer, AlertMessage> alertMessageMap = AlertMessageListProvider.getInstance()
.getAlertMessageMap();
String alertMessageId = alertMessageMap.get(alertId).getMessageID();

AlertMessage alertMessage = AlertMessageListProvider.getInstance().getAlertMessage(alertId);
String alertMessageId = (alertMessage != null) ? alertMessage.getMessageID() : null;

LOG.error("Error building alert report model for Id: " + alertId + " Alert Id: " + alertMessageId, e);
}
}
Expand Down Expand Up @@ -167,10 +169,7 @@ private void processLogSeriesCollection(Map<String, LogSeriesCollection> logTime
Integer alertId = alertLogEntry.getAlertId();
double observedKPI = alertLogEntry.getObservedKPI();

Map<Integer, AlertMessage> alertMessageMap = AlertMessageListProvider.getInstance().getAlertMessageMap();

// alertMessage should not be null as AlertLogParser should have added unidentified AlertIds
AlertMessage alertMessage = alertMessageMap.get(alertId);
AlertMessage alertMessage = AlertMessageListProvider.getInstance().getAlertMessage(alertId);

String messageId = alertMessage.getMessageID();
boolean isCritical = Severity.CRITICAL.equals(alertMessage.getSeverity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.atomic.AtomicInteger;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
Expand All @@ -35,12 +36,13 @@ public class AlertMessageListProvider {

private static final String ALERT_MESSAGE_LIST_XML = "/AlertMessageList.xml";

private static AlertMessageListProvider _INSTANCE;
// checking for null is not thread safe; static initialisation
private static AlertMessageListProvider _INSTANCE = new AlertMessageListProvider();

private Map<Integer, AlertMessage> alertMessageMap;

// can store additional unknown alert message id's found during parsing
private static int adhocAlertId;
private AtomicInteger adhocAlertId;

private Map<String, Integer> messageIdAlertIdMap;

Expand All @@ -54,6 +56,8 @@ private AlertMessageListProvider() {
alertMessageMap = null;
messageIdAlertIdMap = null;

adhocAlertId = new AtomicInteger(10000);

InputStream amlInputStream = FileUtilities.getResourceAsStreamFromUserDir(getClass(), ALERT_MESSAGE_LIST_XML);

if (amlInputStream != null) {
Expand Down Expand Up @@ -115,23 +119,48 @@ private AlertMessageListProvider() {
} else {
LOG.info("AlertMessageList.xml could not be found");
}
}

adhocAlertId = 10000;
public Integer getAlertId(String messageId) {

Integer alertId = messageIdAlertIdMap.get(messageId);

if (alertId == null) {
AlertMessage alertMessage = addNewAlertMessage(messageId);
alertId = alertMessage.getId();
}

return alertId;
}

/*
* returns unmodifiableMap use addNewAlertMessage to append to this map
*/
public Map<Integer, AlertMessage> getAlertMessageMap() {
return Collections.unmodifiableMap(alertMessageMap);
public AlertMessage getAlertMessage(String messageId) {

Integer alertId = getAlertId(messageId);

AlertMessage alertMessage = alertMessageMap.get(alertId);

return alertMessage;
}

public AlertMessage getAlertMessage(Integer alertId) {

AlertMessage alertMessage = alertMessageMap.get(alertId);

return alertMessage;
}

/*
* returns unmodifiableMap use addNewAlertMessage to append to this map
*/
public Map<String, Integer> getMessageIdAlertIdMap() {
return Collections.unmodifiableMap(messageIdAlertIdMap);
private synchronized AlertMessage addNewAlertMessage(String messageId) {

int id = adhocAlertId.incrementAndGet();
AlertMessage alertMessage = new AlertMessage();
alertMessage.setId(id);
alertMessage.setMessageID(messageId);
alertMessage.setSeverity(Severity.NORMAL);

alertMessageMap.put(id, alertMessage);
messageIdAlertIdMap.put(messageId, id);

return alertMessage;
}

public Map<String, List<String>> getAlertMessageTypeMap() {
Expand All @@ -147,23 +176,14 @@ public List<String> getCriticalAlertList() {
}

public static AlertMessageListProvider getInstance() {
if (_INSTANCE == null) {
_INSTANCE = new AlertMessageListProvider();
}

return _INSTANCE;
}
// checking for null is not thread safe; static initialisation
//
// if (_INSTANCE == null) {
// _INSTANCE = new AlertMessageListProvider();
// }

public int getNextAdhocAlertId() {
return adhocAlertId++;
}

public void addNewAlertMessage(AlertMessage alertMessage) {
Integer id = alertMessage.getId();
String messageId = alertMessage.getMessageID();

alertMessageMap.put(id, alertMessage);
messageIdAlertIdMap.put(messageId, id);
return _INSTANCE;
}

public static void main(String[] args) {
Expand Down
17 changes: 1 addition & 16 deletions src/main/java/com/pega/gcs/logviewer/parser/AlertLogParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -28,7 +27,6 @@
import com.pega.gcs.logviewer.model.LogEntryKey;
import com.pega.gcs.logviewer.model.LogEntryModel;
import com.pega.gcs.logviewer.model.PALStatisticName;
import com.pega.gcs.logviewer.model.alert.AlertMessageList.AlertMessage;
import com.pega.gcs.logviewer.model.alert.AlertMessageListProvider;

public class AlertLogParser extends LogParser {
Expand Down Expand Up @@ -270,20 +268,7 @@ private AlertLogEntry buildLogEntry() {

AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance();

Map<String, Integer> messageIdAlertIdMap = alertMessageListProvider.getMessageIdAlertIdMap();

Integer alertId = messageIdAlertIdMap.get(messageIdStr);

if (alertId == null) {
// get an arbitrary id.
alertId = alertMessageListProvider.getNextAdhocAlertId();

AlertMessage alertMessage = new AlertMessage();
alertMessage.setId(alertId);
alertMessage.setMessageID(messageIdStr);
alertMessageListProvider.addNewAlertMessage(alertMessage);

}
Integer alertId = alertMessageListProvider.getAlertId(messageIdStr);

String observedKPIStr = fields[observedKPIIndex];
long observedKPI = Long.parseLong(observedKPIStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import com.pega.gcs.fringecommon.log4j2.Log4j2Helper;
import com.pega.gcs.logviewer.model.AlertLogEntryModel;
Expand All @@ -39,14 +38,12 @@ public static AlertMessageReportModelFactory getInstance() {
return _INSTANCE;
}

public AlertMessageReportModel getAlertMessageReportModel(int alertId, long thresholdKPI,
public AlertMessageReportModel getAlertMessageReportModel(Integer alertId, long thresholdKPI,
AlertLogEntryModel alertLogEntryModel, Locale locale) {

AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance();

Map<Integer, AlertMessage> alertMessageMap = alertMessageListProvider.getAlertMessageMap();

AlertMessage alertMessage = alertMessageMap.get(alertId);
AlertMessage alertMessage = alertMessageListProvider.getAlertMessage(alertId);

return getAlertMessageReportModel(alertMessage, thresholdKPI, alertLogEntryModel, locale);

Expand Down Expand Up @@ -99,23 +96,21 @@ public static void main(String[] args) {

AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance();

Map<Integer, AlertMessage> alertMessageMap = alertMessageListProvider.getAlertMessageMap();

AlertMessageReportModelFactory alertMessageReportModelFactory = AlertMessageReportModelFactory.getInstance();

List<String> messageIdList = new ArrayList<>(AlertMessageListProvider.getInstance().getMessageIdSet());
Collections.sort(messageIdList);

List<String> unImplementedList = new ArrayList<>();

for (AlertMessage alertMessage : alertMessageMap.values()) {
for (String messageId : alertMessageListProvider.getMessageIdSet()) {

AlertMessage alertMessage = alertMessageListProvider.getAlertMessage(messageId);

AlertMessageReportModel alertMessageReportModel;
alertMessageReportModel = alertMessageReportModelFactory.getAlertMessageReportModel(alertMessage, 0, null,
Locale.getDefault());

String messageId = alertMessage.getMessageID();

if (alertMessageReportModel != null) {
LOG.info("Report Model for message id: " + messageId + " " + alertMessageReportModel.toString());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,7 @@ private JPanel getGeneralJPanel(String alertMessageId) {
titleJLabel.setHorizontalAlignment(SwingConstants.CENTER);

AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance();
Map<Integer, AlertMessage> alertMessageMap = alertMessageListProvider.getAlertMessageMap();
Map<String, Integer> messageIdAlertIdMap = alertMessageListProvider.getMessageIdAlertIdMap();

Integer alertId = messageIdAlertIdMap.get(alertMessageId);

AlertMessage alertMessage = alertMessageMap.get(alertId);
AlertMessage alertMessage = alertMessageListProvider.getAlertMessage(alertMessageId);

JPanel alertMessageJPanel = getAlertMessageJPanel(alertMessage);

Expand Down Expand Up @@ -530,9 +525,7 @@ private JPanel getAlertMessageReportJPanel(LogSeriesCollection logTimeSeriesColl

String messageId = logTimeSeriesCollection.getName();

Map<String, Integer> messageIdAlertIdMap = AlertMessageListProvider.getInstance().getMessageIdAlertIdMap();

Integer alertId = messageIdAlertIdMap.get(messageId);
Integer alertId = AlertMessageListProvider.getInstance().getAlertId(messageId);

if (alertId != null) {

Expand Down

0 comments on commit 8473e46

Please sign in to comment.