From 8473e46ac200869d93e2e018d1e7bc2b95541806 Mon Sep 17 00:00:00 2001 From: vargm Date: Mon, 4 Jul 2022 21:09:19 +0100 Subject: [PATCH] fixed concurrent alert file loading issue with messageids --- .../logviewer/model/AlertLogEntryModel.java | 13 ++-- .../model/alert/AlertMessageListProvider.java | 76 ++++++++++++------- .../gcs/logviewer/parser/AlertLogParser.java | 17 +---- .../alert/AlertMessageReportModelFactory.java | 15 ++-- .../report/alert/AlertSummaryJPanel.java | 11 +-- 5 files changed, 62 insertions(+), 70 deletions(-) diff --git a/src/main/java/com/pega/gcs/logviewer/model/AlertLogEntryModel.java b/src/main/java/com/pega/gcs/logviewer/model/AlertLogEntryModel.java index 55c3958..30c4a7b 100644 --- a/src/main/java/com/pega/gcs/logviewer/model/AlertLogEntryModel.java +++ b/src/main/java/com/pega/gcs/logviewer/model/AlertLogEntryModel.java @@ -131,10 +131,12 @@ protected void postProcess(LogEntry logEntry, ArrayList logEntryValueLis alertMessageThresholdKPI, this, locale); alertMessageReportModelMap.put(alertId, alertMessageReportModel); + } catch (Exception e) { - Map 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); } } @@ -167,10 +169,7 @@ private void processLogSeriesCollection(Map logTime Integer alertId = alertLogEntry.getAlertId(); double observedKPI = alertLogEntry.getObservedKPI(); - Map 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()); diff --git a/src/main/java/com/pega/gcs/logviewer/model/alert/AlertMessageListProvider.java b/src/main/java/com/pega/gcs/logviewer/model/alert/AlertMessageListProvider.java index 2e4888d..9490837 100644 --- a/src/main/java/com/pega/gcs/logviewer/model/alert/AlertMessageListProvider.java +++ b/src/main/java/com/pega/gcs/logviewer/model/alert/AlertMessageListProvider.java @@ -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; @@ -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 alertMessageMap; // can store additional unknown alert message id's found during parsing - private static int adhocAlertId; + private AtomicInteger adhocAlertId; private Map messageIdAlertIdMap; @@ -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) { @@ -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 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 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> getAlertMessageTypeMap() { @@ -147,23 +176,14 @@ public List 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) { diff --git a/src/main/java/com/pega/gcs/logviewer/parser/AlertLogParser.java b/src/main/java/com/pega/gcs/logviewer/parser/AlertLogParser.java index cbdbf6a..5ba4493 100644 --- a/src/main/java/com/pega/gcs/logviewer/parser/AlertLogParser.java +++ b/src/main/java/com/pega/gcs/logviewer/parser/AlertLogParser.java @@ -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; @@ -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 { @@ -270,20 +268,7 @@ private AlertLogEntry buildLogEntry() { AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance(); - Map 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); diff --git a/src/main/java/com/pega/gcs/logviewer/report/alert/AlertMessageReportModelFactory.java b/src/main/java/com/pega/gcs/logviewer/report/alert/AlertMessageReportModelFactory.java index 6e0a7b6..9f62ec9 100644 --- a/src/main/java/com/pega/gcs/logviewer/report/alert/AlertMessageReportModelFactory.java +++ b/src/main/java/com/pega/gcs/logviewer/report/alert/AlertMessageReportModelFactory.java @@ -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; @@ -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 alertMessageMap = alertMessageListProvider.getAlertMessageMap(); - - AlertMessage alertMessage = alertMessageMap.get(alertId); + AlertMessage alertMessage = alertMessageListProvider.getAlertMessage(alertId); return getAlertMessageReportModel(alertMessage, thresholdKPI, alertLogEntryModel, locale); @@ -99,8 +96,6 @@ public static void main(String[] args) { AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance(); - Map alertMessageMap = alertMessageListProvider.getAlertMessageMap(); - AlertMessageReportModelFactory alertMessageReportModelFactory = AlertMessageReportModelFactory.getInstance(); List messageIdList = new ArrayList<>(AlertMessageListProvider.getInstance().getMessageIdSet()); @@ -108,14 +103,14 @@ public static void main(String[] args) { List 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 { diff --git a/src/main/java/com/pega/gcs/logviewer/report/alert/AlertSummaryJPanel.java b/src/main/java/com/pega/gcs/logviewer/report/alert/AlertSummaryJPanel.java index c0e4c07..8335ba5 100644 --- a/src/main/java/com/pega/gcs/logviewer/report/alert/AlertSummaryJPanel.java +++ b/src/main/java/com/pega/gcs/logviewer/report/alert/AlertSummaryJPanel.java @@ -170,12 +170,7 @@ private JPanel getGeneralJPanel(String alertMessageId) { titleJLabel.setHorizontalAlignment(SwingConstants.CENTER); AlertMessageListProvider alertMessageListProvider = AlertMessageListProvider.getInstance(); - Map alertMessageMap = alertMessageListProvider.getAlertMessageMap(); - Map messageIdAlertIdMap = alertMessageListProvider.getMessageIdAlertIdMap(); - - Integer alertId = messageIdAlertIdMap.get(alertMessageId); - - AlertMessage alertMessage = alertMessageMap.get(alertId); + AlertMessage alertMessage = alertMessageListProvider.getAlertMessage(alertMessageId); JPanel alertMessageJPanel = getAlertMessageJPanel(alertMessage); @@ -530,9 +525,7 @@ private JPanel getAlertMessageReportJPanel(LogSeriesCollection logTimeSeriesColl String messageId = logTimeSeriesCollection.getName(); - Map messageIdAlertIdMap = AlertMessageListProvider.getInstance().getMessageIdAlertIdMap(); - - Integer alertId = messageIdAlertIdMap.get(messageId); + Integer alertId = AlertMessageListProvider.getInstance().getAlertId(messageId); if (alertId != null) {