From 0b8f4922ebebcf421fb5bc6c25b22380edef6270 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Wed, 13 Jun 2018 12:23:26 -0400 Subject: IntegrityAudit - remove latches from non-test code Change-Id: I5d5cc7d581f78d5551e2fe7447720403bb63ada2 Issue-ID: POLICY-908 Signed-off-by: Jim Hahn --- .../org/onap/policy/common/ia/AuditThread.java | 80 +++++++--------------- .../org/onap/policy/common/ia/IntegrityAudit.java | 49 +++++++------ 2 files changed, 53 insertions(+), 76 deletions(-) (limited to 'integrity-audit/src/main/java') diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java index f1839b12..58ed8b99 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/AuditThread.java @@ -25,10 +25,7 @@ import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Properties; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; - import org.onap.policy.common.ia.jpa.IntegrityAuditEntity; import org.onap.policy.common.logging.eelf.MessageCodes; import org.onap.policy.common.logging.flexlogger.FlexLogger; @@ -107,18 +104,6 @@ public class AuditThread extends Thread { */ private IntegrityAudit integrityAudit; - /** - * A latch is taken from this queue before starting an audit. May be {@code null}. Used by JUnit - * tests. - */ - private BlockingQueue auditLatchQueue; - - /** - * Latch to be decremented when the next audit completes. May be {@code null}. Used by JUnit - * tests to wait for an audit to complete. - */ - private CountDownLatch auditCompletionLatch = null; - /** * AuditThread constructor. * @@ -133,7 +118,7 @@ public class AuditThread extends Thread { int integrityAuditPeriodSeconds, IntegrityAudit integrityAudit) throws IntegrityAuditException { this(resourceName, persistenceUnit, properties, TimeUnit.SECONDS.toMillis(integrityAuditPeriodSeconds), - integrityAudit, null); + integrityAudit); } /** @@ -148,13 +133,12 @@ public class AuditThread extends Thread { * @throws IntegrityAuditException if an error occurs */ public AuditThread(String resourceName, String persistenceUnit, Properties properties, long integrityAuditMillis, - IntegrityAudit integrityAudit, BlockingQueue queue) throws IntegrityAuditException { + IntegrityAudit integrityAudit) throws IntegrityAuditException { this.resourceName = resourceName; this.persistenceUnit = persistenceUnit; this.properties = properties; this.integrityAuditPeriodMillis = integrityAuditMillis; this.integrityAudit = integrityAudit; - this.auditLatchQueue = queue; /* * The DbDAO Constructor registers this node in the IntegrityAuditEntity table. Each @@ -174,14 +158,8 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Entering"); try { - /* - * For JUnit testing: wait for the first latch, decrement it to indicate that the thread - * has started, and then wait for the next latch, before we actually start doing - * anything. These simply return if there is no latch queue defined. - */ - getNextLatch(); - decrementLatch(); - getNextLatch(); + // for junit testing + runStarted(); /* * Triggers change in designation, unless no other viable candidate. @@ -284,11 +262,8 @@ public class AuditThread extends Thread { * property, otherwise just sleep the normal interval. */ if (auditCompleted) { - // indicate that an audit has completed - decrementLatch(); - - // don't start the next audit cycle until a latch has been provided - getNextLatch(); + // for junit testing: indicate that an audit has completed + auditCompleted(); if (logger.isDebugEnabled()) { logger.debug("AuditThread.run: Audit completed; resourceName=" + this.resourceName @@ -341,29 +316,6 @@ public class AuditThread extends Thread { logger.info("AuditThread.run: Exiting"); } - /** - * Gets the next audit-completion latch from the queue. Blocks, if the queue is empty. - * - * @throws InterruptedException if interrupted while waiting - */ - private void getNextLatch() throws InterruptedException { - BlockingQueue queue = this.auditLatchQueue; - if (queue != null) { - this.auditCompletionLatch = queue.take(); - } - } - - /** - * Decrements the current audit-completion latch, if any. - */ - private void decrementLatch() { - CountDownLatch latch = this.auditCompletionLatch; - if (latch != null) { - this.auditCompletionLatch = null; - latch.countDown(); - } - } - /** * Determines if an exception is an InterruptedException or was caused by an * InterruptedException. @@ -787,6 +739,26 @@ public class AuditThread extends Thread { } + /** + * Indicates that the {@link #run()} method has started. This method simply returns, + * and may overridden by junit tests. + * + * @throws InterruptedException + */ + public void runStarted() throws InterruptedException { + // does nothing + } + + /** + * Indicates that an audit has completed. This method simply returns, and may + * overridden by junit tests. + * + * @throws InterruptedException + */ + public void auditCompleted() throws InterruptedException { + // does nothing + } + /** * Adjusts the thread-sleep-interval to be used when an audit has not been completed. * Used by JUnit tests. diff --git a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java index 9abdbe52..b3330faf 100644 --- a/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java +++ b/integrity-audit/src/main/java/org/onap/policy/common/ia/IntegrityAudit.java @@ -21,9 +21,6 @@ package org.onap.policy.common.ia; import java.util.Properties; -import java.util.concurrent.BlockingQueue; -import java.util.concurrent.CountDownLatch; - import org.onap.policy.common.ia.IntegrityAuditProperties.NodeTypeEnum; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; @@ -217,37 +214,20 @@ public class IntegrityAudit { * @throws IntegrityAuditException if an error occurs */ public void startAuditThread() throws IntegrityAuditException { - startAuditThread(null); - } - - /** - * Starts the audit thread. - * - * @param queue the queue - * @return {@code true} if the thread was started, {@code false} otherwise - * @throws IntegrityAuditException if an error occurs - */ - protected boolean startAuditThread(BlockingQueue queue) throws IntegrityAuditException { - logger.info("startAuditThread: Entering"); - boolean success = false; - if (integrityAuditPeriodMillis >= 0) { - this.auditThread = new AuditThread(this.resourceName, this.persistenceUnit, this.properties, - integrityAuditPeriodMillis, this, queue); + this.auditThread = makeAuditThread(this.resourceName, this.persistenceUnit, this.properties, integrityAuditPeriodMillis); logger.info("startAuditThread: Audit started and will run every " + integrityAuditPeriodMillis / 1000 + " seconds"); this.auditThread.start(); - success = true; + } else { logger.info("startAuditThread: Suppressing integrity audit, integrityAuditPeriodSeconds=" + integrityAuditPeriodMillis / 1000); } logger.info("startAuditThread: Exiting"); - - return success; } /** @@ -299,4 +279,29 @@ public class IntegrityAudit { return !this.auditThread.isAlive(); } } + + /** + * + * @return {@code true} if an audit thread exists, {@code false} otherwise + */ + protected boolean haveAuditThread() { + return (this.auditThread != null); + } + + /** + * Creates an audit thread. May be overridden by junit tests. + * + * @param resourceName2 + * @param persistenceUnit2 + * @param properties2 + * @param integrityAuditPeriodMillis2 + * + * @return a new audit thread + * @throws IntegrityAuditException + */ + protected AuditThread makeAuditThread(String resourceName2, String persistenceUnit2, Properties properties2, + long integrityAuditPeriodMillis2) throws IntegrityAuditException { + + return new AuditThread(resourceName2, persistenceUnit2, properties2, integrityAuditPeriodMillis2, this); + } } -- cgit 1.2.3-korg