aboutsummaryrefslogtreecommitdiffstats
path: root/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java
diff options
context:
space:
mode:
Diffstat (limited to 'integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java')
-rw-r--r--integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java101
1 files changed, 44 insertions, 57 deletions
diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java
index 4e718a60..22ffa08e 100644
--- a/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java
+++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/DbAudit.java
@@ -1,8 +1,9 @@
-/*
+/*--
* ============LICENSE_START=======================================================
* Integrity Audit
* ================================================================================
- * Copyright (C) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2021 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +21,7 @@
package org.onap.policy.common.ia;
+import jakarta.persistence.Table;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
@@ -29,7 +31,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
-import javax.persistence.Table;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.builder.RecursiveToStringStyle;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
@@ -60,16 +61,11 @@ public class DbAudit {
*/
public DbAudit(DbDao dbDao) {
- if (logger.isDebugEnabled()) {
- logger.debug("Constructor: Entering");
- }
+ logger.debug("Constructor: Entering");
this.dbDao = dbDao;
- if (logger.isDebugEnabled()) {
- logger.debug("Constructor: Exiting");
- }
-
+ logger.debug("Constructor: Exiting");
}
/**
@@ -128,7 +124,7 @@ public class DbAudit {
* nodes. Since the audit is run in a round-robin, every instance will be compared against
* every other instance.
*/
- IntegrityAuditEntity myIae = dbDao.getMyIntegrityAuditEntity();
+ var myIae = dbDao.getMyIntegrityAuditEntity();
if (myIae == null) {
@@ -153,8 +149,6 @@ public class DbAudit {
if (logger.isDebugEnabled()) {
logger.debug("dbAudit: Exiting");
}
-
- return; // all done
}
private void compareList(String persistenceUnit, List<IntegrityAuditEntity> iaeList, IntegrityAuditEntity myIae,
@@ -181,22 +175,8 @@ public class DbAudit {
compareMineWithTheirs(persistenceUnit, iaeList, myIae, misMatchedMap, clazzName, myEntries);
// Time check
- if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) {
- // update the timestamp
- dbDao.setLastUpdated();
- // reset the startTime
- startTime = AuditorTime.getInstance().getMillis();
- } else {
- // sleep a couple seconds to break up the activity
- if (logger.isDebugEnabled()) {
- logger.debug("dbAudit: Sleeping " + DB_AUDIT_SLEEP_MS + "ms");
- }
- sleep();
- if (logger.isDebugEnabled()) {
- logger.debug("dbAudit: Waking from sleep");
- }
- }
- } // end: for(String clazzName: classNameList)
+ startTime = timeCheck("First", startTime);
+ }
// check if misMatchedMap is empty
if (misMatchedMap.isEmpty()) {
@@ -204,8 +184,6 @@ public class DbAudit {
if (logger.isDebugEnabled()) {
logger.debug("dbAudit: Exiting, misMatchedMap is empty");
}
- // we are done
- return;
} else {
if (logger.isDebugEnabled()) {
logger.debug("dbAudit: Doing another comparison; misMatchedMap.size()=" + misMatchedMap.size());
@@ -246,7 +224,7 @@ public class DbAudit {
* again for all nodes later.
*/
compareMineWithTheirs(myEntries, theirEntries, clazzName, misMatchedMap);
- } // end for (IntegrityAuditEntity iae : iaeList)
+ }
}
private void compareMineWithTheirs(Map<Object, Object> myEntries, Map<Object, Object> theirEntries,
@@ -265,13 +243,32 @@ public class DbAudit {
}
}
+ private long timeCheck(String type, long startTime) throws IntegrityAuditException {
+ if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) {
+ // update the timestamp
+ dbDao.setLastUpdated();
+ // reset the startTime
+ return AuditorTime.getInstance().getMillis();
+ } else {
+ // sleep a couple seconds to break up the activity
+ if (logger.isDebugEnabled()) {
+ logger.debug("dbAudit: " + type + " comparison; sleeping " + DB_AUDIT_SLEEP_MS + "ms");
+ }
+ sleep();
+ if (logger.isDebugEnabled()) {
+ logger.debug("dbAudit: " + type + " comparison; waking from sleep");
+ }
+ return startTime;
+ }
+ }
+
/**
* Creates properties for the other db node.
* @param iae target DB node
* @return DAO properties for the given DB node
*/
private Properties getTheirDaoProperties(IntegrityAuditEntity iae) {
- Properties theirProperties = new Properties();
+ var theirProperties = new Properties();
theirProperties.put(IntegrityAuditProperties.DB_DRIVER, iae.getJdbcDriver());
theirProperties.put(IntegrityAuditProperties.DB_URL, iae.getJdbcUrl());
@@ -298,7 +295,7 @@ public class DbAudit {
logger.debug("dbAudit: Second comparison; traversing classNameSet, size=" + classNameSet.size());
}
- int errorCount = 0;
+ var errorCount = 0;
for (String clazzName : classNameSet) {
@@ -317,24 +314,10 @@ public class DbAudit {
errorCount += recompareMineWithTheirs(resourceName, persistenceUnit, iaeList, myIae, clazzName,
keySet, myEntries);
// Time check
- if ((AuditorTime.getInstance().getMillis() - startTime) >= DB_AUDIT_UPDATE_MS) {
- // update the timestamp
- dbDao.setLastUpdated();
- // reset the startTime
- startTime = AuditorTime.getInstance().getMillis();
- } else {
- // sleep a couple seconds to break up the activity
- if (logger.isDebugEnabled()) {
- logger.debug("dbAudit: Second comparison; sleeping " + DB_AUDIT_SLEEP_MS + "ms");
- }
- sleep();
- if (logger.isDebugEnabled()) {
- logger.debug("dbAudit: Second comparison; waking from sleep");
- }
- }
- } // end: for(String clazzName: classNameList)
+ startTime = timeCheck("Second", startTime);
+ }
- if (errorCount != 0) {
+ if (errorCount > 0) {
String msg = " DB Audit: " + errorCount
+ " errors found. A large number of errors may indicate DB replication has stopped";
logger.error(MessageCodes.ERROR_AUDIT, msg);
@@ -345,7 +328,7 @@ public class DbAudit {
IntegrityAuditEntity myIae, String clazzName, Set<Object> keySet, Map<Object, Object> myEntries)
throws IntegrityAuditException {
- int errorCount = 0;
+ var errorCount = 0;
for (IntegrityAuditEntity iae : iaeList) {
if (iae.getId() == myIae.getId()) {
if (logger.isDebugEnabled()) {
@@ -381,7 +364,7 @@ public class DbAudit {
return 0;
}
- StringBuilder keyBuilder = new StringBuilder();
+ var keyBuilder = new StringBuilder();
for (Object key : misMatchedKeySet) {
keyBuilder.append(key.toString());
keyBuilder.append(", ");
@@ -477,13 +460,17 @@ public class DbAudit {
String tableName = entityClass.getAnnotation(Table.class).name();
String msg = "\nDB Audit Error: " + "\n Table Name: " + tableName
+ "\n Entry 1 (short prefix style): " + resourceName1 + ": "
- + new ReflectionToStringBuilder(entry1, ToStringStyle.SHORT_PREFIX_STYLE).toString()
+ + new ReflectionToStringBuilder(entry1, ToStringStyle.SHORT_PREFIX_STYLE)
+ "\n Entry 2 (short prefix style): " + resourceName2 + ": "
- + new ReflectionToStringBuilder(entry2, ToStringStyle.SHORT_PREFIX_STYLE).toString()
+ + (entry2 != null
+ ? new ReflectionToStringBuilder(entry2, ToStringStyle.SHORT_PREFIX_STYLE).toString()
+ : "null")
+ "\n Entry 1 (recursive style): " + resourceName1 + ": "
- + new ReflectionToStringBuilder(entry1, new RecursiveToStringStyle()).toString()
+ + new ReflectionToStringBuilder(entry1, new RecursiveToStringStyle())
+ "\n Entry 2 (recursive style): " + resourceName2 + ": "
- + new ReflectionToStringBuilder(entry2, new RecursiveToStringStyle()).toString();
+ + (entry2 != null
+ ? new ReflectionToStringBuilder(entry2, new RecursiveToStringStyle()).toString()
+ : "null");
logger.debug(msg);
} catch (ClassNotFoundException e) {