summaryrefslogtreecommitdiffstats
path: root/feature-active-standby-management
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2018-09-13 20:37:15 -0400
committerPamela Dragosh <pdragosh@research.att.com>2018-09-13 20:56:08 -0400
commitad83319991bdd6a25416eff9f0adde148e2eee35 (patch)
treebbaa601361802763cebf014160b87fa897d62a58 /feature-active-standby-management
parentc733c08b7201ffdee81c7dab2ed50a1ce8fd5bbb (diff)
Fix checkstyle for features submodules.
These are the remaining submodules that have checkstyle. Pretty clean compile after this. There were a couple of sonar fixes in there also. Issue-ID: POLICY-882 Change-Id: I8191ea1aa261f4a7b9d3d21c108572fd31db1b8c Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'feature-active-standby-management')
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java378
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java94
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java37
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java228
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java181
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java106
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java75
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java1971
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java1190
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java57
-rw-r--r--feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java2
-rw-r--r--feature-active-standby-management/src/main/resources/META-INF/persistence.xml22
-rw-r--r--feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java537
-rw-r--r--feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java3012
-rw-r--r--feature-active-standby-management/src/test/resources/META-INF/persistence.xml65
-rw-r--r--feature-active-standby-management/src/test/resources/logback-test.xml45
16 files changed, 4080 insertions, 3920 deletions
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java
index 932ebb2d..9e481f01 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java
@@ -29,13 +29,13 @@ import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.onap.policy.drools.core.PolicySessionFeatureAPI;
import org.onap.policy.drools.features.PolicyEngineFeatureAPI;
import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
import org.onap.policy.drools.system.PolicyEngine;
import org.onap.policy.drools.utils.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* If this feature is supported, there is a single instance of it.
@@ -43,197 +43,193 @@ import org.onap.policy.drools.utils.PropertyUtil;
* active/standby state management and IntegrityMonitor. For now, they are
* all treated as a single feature, but it would be nice to separate them.
*
- * The bulk of the code here was once in other classes, such as
+ * <p>The bulk of the code here was once in other classes, such as
* 'PolicyContainer' and 'Main'. It was moved here as part of making this
* a separate optional feature.
*/
public class ActiveStandbyFeature implements ActiveStandbyFeatureAPI,
- PolicySessionFeatureAPI, PolicyEngineFeatureAPI
-{
- // get an instance of logger
- private static final Logger logger =
- LoggerFactory.getLogger(ActiveStandbyFeature.class);
-
- private static DroolsPdp myPdp;
- private static Object myPdpSync = new Object();
- private static DroolsPdpsElectionHandler electionHandler;
-
- private StateManagementFeatureAPI stateManagementFeature;
-
- public static final int SEQ_NUM = 1;
-
-
- /**************************/
- /* 'FeatureAPI' interface */
- /**************************/
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int getSequenceNumber()
- {
- return(SEQ_NUM);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void globalInit(String[] args, String configDir)
- {
- // This must come first since it initializes myPdp
- initializePersistence(configDir);
-
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- if (feature.getResourceName().equals(myPdp.getPdpId()))
- {
- if(logger.isDebugEnabled()){
- logger.debug("ActiveStandbyFeature.globalInit: Found StateManagementFeature"
- + " with resourceName: {}", myPdp.getPdpId());
- }
- stateManagementFeature = feature;
- break;
- }
- }
- if(stateManagementFeature == null){
- if(logger.isDebugEnabled()){
- logger.debug("ActiveStandbyFeature failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", myPdp.getPdpId());
- }
- logger.error("ActiveStandbyFeature failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", myPdp.getPdpId());
- //
- // Cannot add observer since stateManagementFeature is null
- //
- return;
- }
-
-
-
- //Create an instance of the Observer
- PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
-
- //Register the PMStandbyStateChangeNotifier Observer
- stateManagementFeature.addObserver(pmNotifier);
- if(logger.isDebugEnabled()){
- logger.debug("ActiveStandbyFeature.globalInit() exit");
- }
- }
-
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean afterStart(PolicyEngine engine)
- {
- // ASSERTION: engine == PolicyEngine.manager
- PolicyEngine.manager.lock();
- return false;
- }
-
- /**
- * Read in the persistence properties, determine whether persistence is
- * enabled or disabled, and initialize persistence if enabled.
- */
- private static void initializePersistence(String configDir)
- {
- //Get the Active Standby properties
- try {
- Properties activeStandbyProperties =
- PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties");
- ActiveStandbyProperties.initProperties(activeStandbyProperties);
- logger.info("initializePersistence: ActiveStandbyProperties success");
- } catch (IOException e) {
- logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e);
- }
-
- DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU");
- String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
- if(resourceName == null){
- throw new NullPointerException();
- }
-
- /*
- * In a JUnit test environment, one or more PDPs may already have been
- * inserted in the DB, so we need to check for this.
- */
- DroolsPdp existingPdp = conn.getPdp(resourceName);
- if (existingPdp != null) {
- logger.info("Found existing PDP record, pdpId="
- + existingPdp.getPdpId() + ", isDesignated="
- + existingPdp.isDesignated() + ", updatedDate="
- + existingPdp.getUpdatedDate());
- myPdp = existingPdp;
- }
-
- synchronized(myPdpSync){
- if(myPdp == null){
-
- myPdp = new DroolsPdpImpl(resourceName,false,4,new Date());
- }
- String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME);
- if (siteName == null) {
- siteName = "";
- }else{
- siteName = siteName.trim();
- }
- myPdp.setSiteName(siteName);
- if(electionHandler == null){
- electionHandler = new DroolsPdpsElectionHandler(conn,myPdp);
- }
- }
- logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n");
- }
-
-
- /*
- * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to
- * this method, so it can also be accessed from StandbyStateChangeNotifier
- * class.
- */
- public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) {
-
- Map<String, Object> propMap = new HashMap<>();
- propMap.put("javax.persistence.jdbc.driver", ActiveStandbyProperties
- .getProperty(ActiveStandbyProperties.DB_DRIVER));
- propMap.put("javax.persistence.jdbc.url",
- ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_URL));
- propMap.put("javax.persistence.jdbc.user", ActiveStandbyProperties
- .getProperty(ActiveStandbyProperties.DB_USER));
- propMap.put("javax.persistence.jdbc.password",
- ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_PWD));
-
- EntityManagerFactory emf = Persistence.createEntityManagerFactory(
- pu, propMap);
- return new JpaDroolsPdpsConnector(emf);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getPdpdNowActive(){
- return electionHandler.getPdpdNowActive();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getPdpdLastActive(){
- return electionHandler.getPdpdLastActive();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public String getResourceName() {
- return myPdp.getPdpId();
- }
+ PolicySessionFeatureAPI, PolicyEngineFeatureAPI {
+ // get an instance of logger
+ private static final Logger logger =
+ LoggerFactory.getLogger(ActiveStandbyFeature.class);
+
+ private static DroolsPdp myPdp;
+ private static Object myPdpSync = new Object();
+ private static DroolsPdpsElectionHandler electionHandler;
+
+ private StateManagementFeatureAPI stateManagementFeature;
+
+ public static final int SEQ_NUM = 1;
+
+
+ /**************************/
+ /* 'FeatureAPI' interface */
+ /**************************/
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int getSequenceNumber() {
+ return SEQ_NUM;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void globalInit(String[] args, String configDir) {
+ // This must come first since it initializes myPdp
+ initializePersistence(configDir);
+
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ if (feature.getResourceName().equals(myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("ActiveStandbyFeature.globalInit: Found StateManagementFeature"
+ + " with resourceName: {}", myPdp.getPdpId());
+ }
+ stateManagementFeature = feature;
+ break;
+ }
+ }
+ if (stateManagementFeature == null) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("ActiveStandbyFeature failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", myPdp.getPdpId());
+ }
+ logger.error("ActiveStandbyFeature failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", myPdp.getPdpId());
+ //
+ // Cannot add observer since stateManagementFeature is null
+ //
+ return;
+ }
+
+
+
+ //Create an instance of the Observer
+ PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
+
+ //Register the PMStandbyStateChangeNotifier Observer
+ stateManagementFeature.addObserver(pmNotifier);
+ if (logger.isDebugEnabled()) {
+ logger.debug("ActiveStandbyFeature.globalInit() exit");
+ }
+ }
+
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean afterStart(PolicyEngine engine) {
+ // ASSERTION: engine == PolicyEngine.manager
+ PolicyEngine.manager.lock();
+ return false;
+ }
+
+ /**
+ * Read in the persistence properties, determine whether persistence is
+ * enabled or disabled, and initialize persistence if enabled.
+ */
+ private static void initializePersistence(String configDir) {
+ //Get the Active Standby properties
+ try {
+ Properties activeStandbyProperties =
+ PropertyUtil.getProperties(configDir + "/feature-active-standby-management.properties");
+ ActiveStandbyProperties.initProperties(activeStandbyProperties);
+ logger.info("initializePersistence: ActiveStandbyProperties success");
+ } catch (IOException e) {
+ logger.error("ActiveStandbyFeature: initializePersistence ActiveStandbyProperties", e);
+ }
+
+ DroolsPdpsConnector conn = getDroolsPdpsConnector("activeStandbyPU");
+ String resourceName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
+ if (resourceName == null) {
+ throw new NullPointerException();
+ }
+
+ /*
+ * In a JUnit test environment, one or more PDPs may already have been
+ * inserted in the DB, so we need to check for this.
+ */
+ DroolsPdp existingPdp = conn.getPdp(resourceName);
+ if (existingPdp != null) {
+ logger.info("Found existing PDP record, pdpId="
+ + existingPdp.getPdpId() + ", isDesignated="
+ + existingPdp.isDesignated() + ", updatedDate="
+ + existingPdp.getUpdatedDate());
+ myPdp = existingPdp;
+ }
+
+ synchronized (myPdpSync) {
+ if (myPdp == null) {
+
+ myPdp = new DroolsPdpImpl(resourceName,false,4,new Date());
+ }
+ String siteName = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME);
+ if (siteName == null) {
+ siteName = "";
+ } else {
+ siteName = siteName.trim();
+ }
+ myPdp.setSiteName(siteName);
+ if (electionHandler == null) {
+ electionHandler = new DroolsPdpsElectionHandler(conn,myPdp);
+ }
+ }
+ logger.info("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n");
+ }
+
+
+ /**
+ * Moved code to instantiate a JpaDroolsPdpsConnector object from main() to
+ * this method, so it can also be accessed from StandbyStateChangeNotifier
+ * class.
+ *
+ * @param pu string
+ * @return connector object
+ */
+ public static DroolsPdpsConnector getDroolsPdpsConnector(String pu) {
+
+ Map<String, Object> propMap = new HashMap<>();
+ propMap.put("javax.persistence.jdbc.driver", ActiveStandbyProperties
+ .getProperty(ActiveStandbyProperties.DB_DRIVER));
+ propMap.put("javax.persistence.jdbc.url",
+ ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_URL));
+ propMap.put("javax.persistence.jdbc.user", ActiveStandbyProperties
+ .getProperty(ActiveStandbyProperties.DB_USER));
+ propMap.put("javax.persistence.jdbc.password",
+ ActiveStandbyProperties.getProperty(ActiveStandbyProperties.DB_PWD));
+
+ EntityManagerFactory emf = Persistence.createEntityManagerFactory(
+ pu, propMap);
+ return new JpaDroolsPdpsConnector(emf);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getPdpdNowActive() {
+ return electionHandler.getPdpdNowActive();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getPdpdLastActive() {
+ return electionHandler.getPdpdLastActive();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public String getResourceName() {
+ return myPdp.getPdpId();
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java
index a70c71f6..009ff8c6 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,51 +25,53 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ActiveStandbyProperties {
- // get an instance of logger
- private static final Logger logger = LoggerFactory.getLogger(ActiveStandbyProperties.class);
-
- public static final String PDP_CHECK_INVERVAL = "pdp.checkInterval";
- public static final String PDP_UPDATE_INTERVAL = "pdp.updateInterval";
- public static final String PDP_TIMEOUT = "pdp.timeout";
- public static final String PDP_INITIAL_WAIT_PERIOD = "pdp.initialWait";
+ // get an instance of logger
+ private static final Logger logger = LoggerFactory.getLogger(ActiveStandbyProperties.class);
- public static final String NODE_NAME = "resource.name";
- public static final String SITE_NAME = "site_name";
-
- /*
- * feature-active-standby-management.properties parameter key values
- */
- public static final String DB_DRIVER = "javax.persistence.jdbc.driver";
- public static final String DB_URL = "javax.persistence.jdbc.url";
- public static final String DB_USER = "javax.persistence.jdbc.user";
- public static final String DB_PWD = "javax.persistence.jdbc.password";
-
- private static Properties properties = null;
-
- private ActiveStandbyProperties() {
- throw new IllegalStateException("Utility class");
- }
- /*
- * Initialize the parameter values from the droolsPersitence.properties file values
- *
- * This is designed so that the Properties object is obtained from properties
- * file and then is passed to this method to initialize the value of the parameters.
- * This allows the flexibility of JUnit tests using getProperties(filename) to get the
- * properties while runtime methods can use getPropertiesFromClassPath(filename).
- *
- */
- public static void initProperties (Properties prop){
- logger.info("ActiveStandbyProperties.initProperties(Properties): entry");
- logger.info("\n\nActiveStandbyProperties.initProperties: Properties = \n{}\n\n", prop);
-
- properties = prop;
- }
+ public static final String PDP_CHECK_INVERVAL = "pdp.checkInterval";
+ public static final String PDP_UPDATE_INTERVAL = "pdp.updateInterval";
+ public static final String PDP_TIMEOUT = "pdp.timeout";
+ public static final String PDP_INITIAL_WAIT_PERIOD = "pdp.initialWait";
- public static String getProperty(String key){
- return properties.getProperty(key);
- }
-
- public static Properties getProperties() {
- return properties;
- }
+ public static final String NODE_NAME = "resource.name";
+ public static final String SITE_NAME = "site_name";
+
+ /*
+ * feature-active-standby-management.properties parameter key values
+ */
+ public static final String DB_DRIVER = "javax.persistence.jdbc.driver";
+ public static final String DB_URL = "javax.persistence.jdbc.url";
+ public static final String DB_USER = "javax.persistence.jdbc.user";
+ public static final String DB_PWD = "javax.persistence.jdbc.password";
+
+ private static Properties properties = null;
+
+ private ActiveStandbyProperties() {
+ throw new IllegalStateException("Utility class");
+ }
+
+ /**
+ * Initialize the parameter values from the droolsPersitence.properties file values.
+ *
+ * <p>This is designed so that the Properties object is obtained from properties
+ * file and then is passed to this method to initialize the value of the parameters.
+ * This allows the flexibility of JUnit tests using getProperties(filename) to get the
+ * properties while runtime methods can use getPropertiesFromClassPath(filename).
+ *
+ * @param prop properties
+ */
+ public static void initProperties(Properties prop) {
+ logger.info("ActiveStandbyProperties.initProperties(Properties): entry");
+ logger.info("\n\nActiveStandbyProperties.initProperties: Properties = \n{}\n\n", prop);
+
+ properties = prop;
+ }
+
+ public static String getProperty(String key) {
+ return properties.getProperty(key);
+ }
+
+ public static Properties getProperties() {
+ return properties;
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java
index a440a7e1..46096bdd 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,16 +24,27 @@ import java.util.Date;
public interface DroolsPdp {
- public String getPdpId();
- public boolean isDesignated();
- public int getPriority();
- public Date getUpdatedDate();
- public void setDesignated(boolean isDesignated);
- public void setUpdatedDate(Date updatedDate);
- public int comparePriority(DroolsPdp other);
- public int comparePriority(DroolsPdp other,String previousSite);
- public String getSiteName();
- public void setSiteName(String siteName);
- public Date getDesignatedDate();
- public void setDesignatedDate(Date designatedDate);
+ public String getPdpId();
+
+ public boolean isDesignated();
+
+ public int getPriority();
+
+ public Date getUpdatedDate();
+
+ public void setDesignated(boolean isDesignated);
+
+ public void setUpdatedDate(Date updatedDate);
+
+ public int comparePriority(DroolsPdp other);
+
+ public int comparePriority(DroolsPdp other,String previousSite);
+
+ public String getSiteName();
+
+ public void setSiteName(String siteName);
+
+ public Date getDesignatedDate();
+
+ public void setDesignatedDate(Date designatedDate);
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java
index ed10f4c2..f0fd2789 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,121 +37,123 @@ import org.onap.policy.drools.activestandby.DroolsPdpObject;
//@Table(name="DroolsPdpEntity")
@NamedQueries({
- @NamedQuery(name="DroolsPdpEntity.findAll", query="SELECT e FROM DroolsPdpEntity e "),
- @NamedQuery(name="DroolsPdpEntity.deleteAll", query="DELETE FROM DroolsPdpEntity WHERE 1=1")
+ @NamedQuery(name = "DroolsPdpEntity.findAll", query = "SELECT e FROM DroolsPdpEntity e "),
+ @NamedQuery(name = "DroolsPdpEntity.deleteAll", query = "DELETE FROM DroolsPdpEntity WHERE 1=1")
})
-public class DroolsPdpEntity extends DroolsPdpObject implements Serializable{
-
- private static final long serialVersionUID = 1L;
-
- @Id
- @Column(name="pdpId", nullable=false)
- private String pdpId="-1";
-
- @Column(name="designated", nullable=false)
- private boolean designated=false;
-
- @Column(name="priority", nullable=false)
- private int priority=0;
-
- @Temporal(TemporalType.TIMESTAMP)
- @Column(name="updatedDate", nullable=false)
- private Date updatedDate;
-
- @Temporal(TemporalType.TIMESTAMP)
- @Column(name="designatedDate",nullable=false)
- private Date designatedDate;
-
- @Column(name="site", nullable=true, length = 50)
- private String site;
-
-
- public DroolsPdpEntity(){
- updatedDate = new Date();
- //When this is translated to a TimeStamp in MySQL, it assumes the date is relative
- //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969
- //which is an invalid value for the MySql TimeStamp
- designatedDate = new Date(864000000);
- }
-
- @Override
- public String getPdpId() {
- return this.pdpId;
- }
-
- public void setPdpId(String pdpId) {
- this.pdpId = pdpId;
- }
-
- @Override
- public boolean isDesignated() {
- return this.designated;
- }
-
- @Override
- public int getPriority() {
- return this.priority;
- }
-
- public void setPriority(int priority) {
- this.priority = priority;
- }
-
- @Override
- public Date getUpdatedDate() {
- return this.updatedDate;
- }
-
- @Override
- public void setDesignated(boolean isDesignated) {
- this.designated=isDesignated;
- }
-
- @Override
- public void setUpdatedDate(Date updatedDate) {
- this.updatedDate=updatedDate;
- }
-
-
- @Override
- public String getSiteName() {
- return site;
- }
-
- @Override
- public void setSiteName(String siteName) {
- site = siteName;
-
- }
-
- @Override
- public Date getDesignatedDate() {
- return designatedDate;
- }
-
- @Override
- public void setDesignatedDate(Date designatedDate) {
- this.designatedDate = designatedDate;
- }
-
- @Override
- public boolean equals(Object obj){
-
- if (obj instanceof DroolsPdp) {
- DroolsPdpEntity d = (DroolsPdpEntity) obj;
- return this.pdpId.equals(d.getPdpId());
- } else {
- return false;
- }
-
- }
-
- @Override
- public int hashCode() {
+public class DroolsPdpEntity extends DroolsPdpObject implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ @Id
+ @Column(name = "pdpId", nullable = false)
+ private String pdpId = "-1";
+
+ @Column(name = "designated", nullable = false)
+ private boolean designated = false;
+
+ @Column(name = "priority", nullable = false)
+ private int priority = 0;
+
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "updatedDate", nullable = false)
+ private Date updatedDate;
+
+ @Temporal(TemporalType.TIMESTAMP)
+ @Column(name = "designatedDate",nullable = false)
+ private Date designatedDate;
+
+ @Column(name = "site", nullable = true, length = 50)
+ private String site;
+
+ /**
+ * Constructor.
+ */
+ public DroolsPdpEntity() {
+ updatedDate = new Date();
+ //When this is translated to a TimeStamp in MySQL, it assumes the date is relative
+ //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969
+ //which is an invalid value for the MySql TimeStamp
+ designatedDate = new Date(864000000);
+ }
+
+ @Override
+ public String getPdpId() {
+ return this.pdpId;
+ }
+
+ public void setPdpId(String pdpId) {
+ this.pdpId = pdpId;
+ }
+
+ @Override
+ public boolean isDesignated() {
+ return this.designated;
+ }
+
+ @Override
+ public int getPriority() {
+ return this.priority;
+ }
+
+ public void setPriority(int priority) {
+ this.priority = priority;
+ }
+
+ @Override
+ public Date getUpdatedDate() {
+ return this.updatedDate;
+ }
+
+ @Override
+ public void setDesignated(boolean isDesignated) {
+ this.designated = isDesignated;
+ }
+
+ @Override
+ public void setUpdatedDate(Date updatedDate) {
+ this.updatedDate = updatedDate;
+ }
+
+
+ @Override
+ public String getSiteName() {
+ return site;
+ }
+
+ @Override
+ public void setSiteName(String siteName) {
+ site = siteName;
+
+ }
+
+ @Override
+ public Date getDesignatedDate() {
+ return designatedDate;
+ }
+
+ @Override
+ public void setDesignatedDate(Date designatedDate) {
+ this.designatedDate = designatedDate;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+
+ if (obj instanceof DroolsPdp) {
+ DroolsPdpEntity entity = (DroolsPdpEntity) obj;
+ return this.pdpId.equals(entity.getPdpId());
+ } else {
+ return false;
+ }
+
+ }
+
+ @Override
+ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.pdpId == null ? 0 : this.pdpId.hashCode());
- return result;
- }
+ return result;
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java
index f54a18cc..31728bc2 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,89 +24,104 @@ import java.util.Date;
public class DroolsPdpImpl extends DroolsPdpObject {
- private boolean designated;
- private int priority;
- private Date updatedDate;
- private Date designatedDate;
- private String pdpId;
- private String site;
-
- public DroolsPdpImpl(String pdpId, boolean designated, int priority, Date updatedDate){
- this.pdpId = pdpId;
- this.designated = designated;
- this.priority = priority;
- this.updatedDate = updatedDate;
- //When this is translated to a TimeStamp in MySQL, it assumes the date is relative
- //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969
- //which is an invalid value for the MySql TimeStamp
- this.designatedDate = new Date(864000000);
-
- }
- @Override
- public boolean isDesignated() {
-
- return designated;
- }
-
- @Override
- public int getPriority() {
- return priority;
- }
- @Override
- public void setUpdatedDate(Date date){
- this.updatedDate = date;
- }
- @Override
- public Date getUpdatedDate() {
- return updatedDate;
- }
-
- @Override
- public String getPdpId() {
- return pdpId;
- }
- @Override
- public void setDesignated(boolean isDesignated) {
- this.designated = isDesignated;
-
- }
-
- @Override
- public String getSiteName() {
- return site;
- }
- @Override
- public void setSiteName(String siteName) {
- this.site = siteName;
-
- }
- @Override
- public Date getDesignatedDate() {
- return designatedDate;
- }
- @Override
- public void setDesignatedDate(Date designatedDate) {
- this.designatedDate = designatedDate;
-
- }
-
- @Override
- public boolean equals(Object obj){
-
-
- if (obj instanceof DroolsPdp) {
- DroolsPdpImpl p = (DroolsPdpImpl) obj;
- return this.pdpId.equals(p.getPdpId());
- } else {
- return false;
- }
- }
-
- @Override
- public int hashCode() {
+ private boolean designated;
+ private int priority;
+ private Date updatedDate;
+ private Date designatedDate;
+ private String pdpId;
+ private String site;
+
+ /**
+ * Contructor.
+ *
+ * @param pdpId ID for the PDP
+ * @param designated is designated
+ * @param priority priority
+ * @param updatedDate date updated
+ */
+ public DroolsPdpImpl(String pdpId, boolean designated, int priority, Date updatedDate) {
+ this.pdpId = pdpId;
+ this.designated = designated;
+ this.priority = priority;
+ this.updatedDate = updatedDate;
+ //When this is translated to a TimeStamp in MySQL, it assumes the date is relative
+ //to the local timezone. So, a value of Date(0) is actually Dec 31 18:00:00 CST 1969
+ //which is an invalid value for the MySql TimeStamp
+ this.designatedDate = new Date(864000000);
+
+ }
+
+ @Override
+ public boolean isDesignated() {
+
+ return designated;
+ }
+
+ @Override
+ public int getPriority() {
+ return priority;
+ }
+
+ @Override
+ public void setUpdatedDate(Date date) {
+ this.updatedDate = date;
+ }
+
+ @Override
+ public Date getUpdatedDate() {
+ return updatedDate;
+ }
+
+ @Override
+ public String getPdpId() {
+ return pdpId;
+ }
+
+ @Override
+ public void setDesignated(boolean isDesignated) {
+ this.designated = isDesignated;
+
+ }
+
+ @Override
+ public String getSiteName() {
+ return site;
+ }
+
+ @Override
+ public void setSiteName(String siteName) {
+ this.site = siteName;
+
+ }
+
+ @Override
+ public Date getDesignatedDate() {
+ return designatedDate;
+ }
+
+ @Override
+ public void setDesignatedDate(Date designatedDate) {
+ this.designatedDate = designatedDate;
+
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+
+
+ if (obj instanceof DroolsPdp) {
+ DroolsPdpImpl temp = (DroolsPdpImpl) obj;
+ return this.pdpId.equals(temp.getPdpId());
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.pdpId == null ? 0 : this.pdpId.hashCode());
- return result;
- }
+ return result;
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java
index 690b260c..49f6b02d 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java
@@ -21,60 +21,64 @@
package org.onap.policy.drools.activestandby;
-public abstract class DroolsPdpObject implements DroolsPdp{
-
- @Override
- public boolean equals(Object other){
- if(other instanceof DroolsPdp){
- return this.getPdpId().equals(((DroolsPdp)other).getPdpId());
- }else{
- return false;
- }
- }
- @Override
- public int hashCode() {
+public abstract class DroolsPdpObject implements DroolsPdp {
+
+ @Override
+ public boolean equals(Object other) {
+ if (other instanceof DroolsPdp) {
+ return this.getPdpId().equals(((DroolsPdp)other).getPdpId());
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (this.getPdpId() == null ? 0 : this.getPdpId().hashCode());
result = prime * result + (this.getSiteName() == null ? 0 : this.getSiteName().hashCode());
result = prime * result + this.getPriority();
- return super.hashCode();
- }
- private int nullSafeCompare(String one, String two){
- if(one != null && two != null){
- return one.compareTo(two);
- }
- if(one == null && two != null){
- return -1;
- }
- if(one != null && two == null){
- return 1;
- }
- return 0;
- }
- @Override
- public int comparePriority(DroolsPdp other){
- if(nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0){
- if(this.getPriority() != other.getPriority()){
- return this.getPriority() - other.getPriority();
- }
- return this.getPdpId().compareTo(other.getPdpId());
- } else {
- return nullSafeCompare(this.getSiteName(),other.getSiteName());
- }
- }
- @Override
- public int comparePriority(DroolsPdp other, String previousSite){
- if(previousSite == null || previousSite.isEmpty()){
- return comparePriority(other);
- }
- if(nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0){
- if(this.getPriority() != other.getPriority()){
- return this.getPriority() - other.getPriority();
- }
- return this.getPdpId().compareTo(other.getPdpId());
- } else {
- return nullSafeCompare(this.getSiteName(),other.getSiteName());
- }
- }
+ return super.hashCode();
+ }
+
+ private int nullSafeCompare(String one, String two) {
+ if (one != null && two != null) {
+ return one.compareTo(two);
+ }
+ if (one == null && two != null) {
+ return -1;
+ }
+ if (one != null && two == null) {
+ return 1;
+ }
+ return 0;
+ }
+
+ @Override
+ public int comparePriority(DroolsPdp other) {
+ if (nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0) {
+ if (this.getPriority() != other.getPriority()) {
+ return this.getPriority() - other.getPriority();
+ }
+ return this.getPdpId().compareTo(other.getPdpId());
+ } else {
+ return nullSafeCompare(this.getSiteName(),other.getSiteName());
+ }
+ }
+
+ @Override
+ public int comparePriority(DroolsPdp other, String previousSite) {
+ if (previousSite == null || previousSite.isEmpty()) {
+ return comparePriority(other);
+ }
+ if (nullSafeCompare(this.getSiteName(),other.getSiteName()) == 0) {
+ if (this.getPriority() != other.getPriority()) {
+ return this.getPriority() - other.getPriority();
+ }
+ return this.getPdpId().compareTo(other.getPdpId());
+ } else {
+ return nullSafeCompare(this.getSiteName(),other.getSiteName());
+ }
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java
index d0d33f0f..fdb87378 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,40 +24,41 @@ import java.util.Collection;
public interface DroolsPdpsConnector {
-
- //return a list of PDPs, NOT including this PDP
- public Collection<DroolsPdp> getDroolsPdps();
-
- public void update(DroolsPdp pdp);
-
- //determines if the DroolsPdp parameter is considered "current" or expired (has it been too long since the Pdp sent an update)
- public boolean isPdpCurrent(DroolsPdp pdp);
-
- // Updates DESIGNATED boolean in PDP record.
- public void setDesignated(DroolsPdp pdp, boolean designated);
-
- // Marks droolspdpentity.DESIGNATED=false, so another PDP-D will go active.
- public void standDownPdp(String pdpId);
-
- // This is used in a JUnit test environment to manually
- // insert a PDP
- public void insertPdp(DroolsPdp pdp);
-
- // This is used in a JUnit test environment to manually
- // delete a PDP
- public void deletePdp(String pdpId);
-
- // This is used in a JUnit test environment to manually
- // clear the droolspdpentity table.
- public void deleteAllPdps();
-
- // This is used in a JUnit test environment to manually
- // get a PDP
- public DroolsPdpEntity getPdp(String pdpId);
-
- // Used by DroolsPdpsElectionHandler to determine if the currently designated
- // PDP has failed.
- public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps);
-
-
+
+ //return a list of PDPs, NOT including this PDP
+ public Collection<DroolsPdp> getDroolsPdps();
+
+ public void update(DroolsPdp pdp);
+
+ //determines if the DroolsPdp parameter is considered "current" or expired
+ //(has it been too long since the Pdp sent an update)
+ public boolean isPdpCurrent(DroolsPdp pdp);
+
+ // Updates DESIGNATED boolean in PDP record.
+ public void setDesignated(DroolsPdp pdp, boolean designated);
+
+ // Marks droolspdpentity.DESIGNATED=false, so another PDP-D will go active.
+ public void standDownPdp(String pdpId);
+
+ // This is used in a JUnit test environment to manually
+ // insert a PDP
+ public void insertPdp(DroolsPdp pdp);
+
+ // This is used in a JUnit test environment to manually
+ // delete a PDP
+ public void deletePdp(String pdpId);
+
+ // This is used in a JUnit test environment to manually
+ // clear the droolspdpentity table.
+ public void deleteAllPdps();
+
+ // This is used in a JUnit test environment to manually
+ // get a PDP
+ public DroolsPdpEntity getPdp(String pdpId);
+
+ // Used by DroolsPdpsElectionHandler to determine if the currently designated
+ // PDP has failed.
+ public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps);
+
+
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java
index 9b172e13..08592f23 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java
@@ -28,991 +28,996 @@ import java.util.Timer;
import java.util.TimerTask;
import org.onap.policy.common.im.StateManagement;
+import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
public class DroolsPdpsElectionHandler implements ThreadRunningChecker {
- // get an instance of logger
- private static final Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class);
- private DroolsPdpsConnector pdpsConnector;
- private Object checkWaitTimerLock = new Object();
- private Object designationWaiterLock = new Object();
-
- /*
- * Must be static, so it can be referenced by JpaDroolsPdpsConnector,
- * without requiring a reference to the election handler instantiation.
- */
- private static DroolsPdp myPdp;
-
- private DesignationWaiter designationWaiter;
- private Timer updateWorker;
- private Timer waitTimer;
- private Date waitTimerLastRunDate;
-
- // The interval between checks of the DesignationWaiter to be sure it is running.
- private int pdpCheckInterval;
-
- // The interval between runs of the DesignationWaiter
- private int pdpUpdateInterval;
-
- private volatile boolean isDesignated;
-
- private String pdpdNowActive;
- private String pdpdLastActive;
-
- /*
- * Start allSeemsWell with a value of null so that, on the first run
- * of the checkWaitTimer it will set the value in IntegrityMonitor
- * regardless of whether it needs to be set to true or false.
- */
- private Boolean allSeemsWell=null;
-
- private StateManagementFeatureAPI stateManagementFeature;
-
- private static boolean isUnitTesting = false;
- private static boolean isStalled = false;
-
- public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp){
- if (pdps == null) {
- logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null");
- throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null");
- }
- if (myPdp == null){
- logger.error("DroolsPdpsElectinHandler(): droolsPdp==null");
- throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null");
- }
-
- pdpdNowActive = null;
- pdpdLastActive = null;
- this.pdpsConnector = pdps;
- DroolsPdpsElectionHandler.myPdp = myPdp;
- this.isDesignated = false;
- pdpCheckInterval = 3000;
- try{
- pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_CHECK_INVERVAL));
- }catch(Exception e){
- logger.error
- ("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e);
- }
- pdpUpdateInterval = 2000;
- try{
- pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL));
- }catch(Exception e){
- logger.error
- ("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e);
- }
-
- Date now = new Date();
-
- // Retrieve the ms since the epoch
- long nowMs = now.getTime();
-
- // Create the timer which will update the updateDate in DroolsPdpEntity table.
- // This is the heartbeat
- updateWorker = new Timer();
-
- // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter
- // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the
- // drools droolsPdpEntity table.
- updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
-
- // Create the timer which will run the election algorithm
- waitTimer = new Timer();
-
- // Schedule it to start in startMs ms (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter
- long startMs = getDWaiterStartMs();
- designationWaiter = new DesignationWaiter();
- waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
- waitTimerLastRunDate = new Date(nowMs + startMs);
-
- //Get the StateManagementFeature instance
-
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- if (feature.getResourceName().equals(myPdp.getPdpId()))
- {
- if(logger.isDebugEnabled()){
- logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature"
- + " with resourceName: {}", myPdp.getPdpId());
- }
- stateManagementFeature = feature;
- break;
- }
- }
- if(stateManagementFeature == null){
- logger.error("DroolsPdpsElectionHandler failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", myPdp.getPdpId());
- }
- }
-
- public static void setIsUnitTesting(boolean val){
- isUnitTesting = val;
- }
- public static void setIsStalled(boolean val){
- isStalled = val;
- }
-
- /*
- * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs
- * access to myPdp, so it can keep its designation status in sync with the
- * DB.
- */
- public static void setMyPdpDesignated(boolean designated) {
- if(logger.isDebugEnabled()){
- logger.debug
- ("setMyPdpDesignated: designated= {}", designated);
- }
- myPdp.setDesignated(designated);
- }
-
- private class DesignationWaiter extends TimerTask {
- // get an instance of logger
- private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class);
-
- @Override
- public void run() {
- try{
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: Entering");
- }
-
- //This is for testing the checkWaitTimer
- if(isUnitTesting && isStalled){
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}", isUnitTesting, isStalled);
- }
- return;
- }
-
- synchronized (designationWaiterLock) {
-
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: Entering synchronized block");
- }
-
- //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated
- //PDPs and then decide which one really should be designated at the end.
- List<DroolsPdp> listOfDesignated = new ArrayList<>();
-
- Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps();
- DroolsPdp designatedPdp = null;
-
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: pdps.size= {}", pdps.size());
- }
-
- //This is only true if all designated PDPs have failed
- boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps);
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed);
- }
- for (DroolsPdp pdp : pdps) {
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId());
- }
-
- /*
- * Note: side effect of isPdpCurrent is that any stale but
- * designated PDPs will be marked as un-designated.
- */
- boolean isCurrent = pdpsConnector.isPdpCurrent(pdp);
-
- /*
- * We can't use stateManagement.getStandbyStatus() here, because
- * we need the standbyStatus, not for this PDP, but for the PDP
- * being processed by this loop iteration.
- */
- String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
- if(standbyStatus==null){
- // Treat this case as a cold standby -- if we
- // abort here, no sessions will be created in a
- // single-node test environment.
- standbyStatus = StateManagement.COLD_STANDBY;
- }
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent);
- }
-
- /*
- * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn
- * and evaluate the each pdp in the list of pdps against each combination.
- *
- * This is the first combination of isDesignated and isCurrent
- */
- if (pdp.isDesignated() && isCurrent) {
- //It is current, but it could have a standbystatus=coldstandby / hotstandby
- //If so, we need to stand it down and demote it
- if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
- if(pdp.getPdpId().equals(myPdp.getPdpId())){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
- + "butstandbystatus is not providingservice. "
- + " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId());
- }
- // So, we must demote it
- try {
- //Keep the order like this. StateManagement is last since it triggers controller shutdown
- //This will change isDesignated and it can enter another if(combination) below
- pdpsConnector.standDownPdp(pdp.getPdpId());
- myPdp.setDesignated(false);
- isDesignated = false;
- if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
- standbyStatus.equals(StateManagement.COLD_STANDBY))){
- /*
- * Only demote it if it appears it has not already been demoted. Don't worry
- * about synching with the topic endpoint states. That is done by the
- * refreshStateAudit
- */
- stateManagementFeature.demote();
- }
- //update the standbystatus to check in a later combination of isDesignated and isCurrent
- standbyStatus=stateManagementFeature.getStandbyStatus(pdp.getPdpId());
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: myPdp: {} "
- + "Caught Exception attempting to demote myPdp,"
- + "message= {}", myPdp.getPdpId(), e);
- }
- }else{
- // Don't demote a remote PDP that is current. It should catch itself
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
- + "but standbystatus is not providingservice. "
- + " Cannot execute stateManagement.demote() since it it is not myPdp\n\n", myPdp.getPdpId());
- }
- }
-
- }else{
- // If we get here, it is ok to be on the list
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: PDP= {} is designated, current and {} Noting PDP as "
- + "designated, standbyStatus= {}", pdp.getPdpId(), standbyStatus, standbyStatus);
- }
- listOfDesignated.add(pdp);
- }
-
-
- }
-
-
- /*
- * The second combination of isDesignated and isCurrent
- *
- * PDP is designated but not current; it has failed. So we stand it down (it doesn't matter what
- * its standbyStatus is). None of these go on the list.
- */
- if (pdp.isDesignated() && !isCurrent) {
- if(logger.isDebugEnabled()){
- logger.debug
- ("INFO: DesignatedWaiter.run: PDP= {} is currently designated but is not current; "
- + "it has failed. Standing down. standbyStatus= {}", pdp.getPdpId(), standbyStatus);
- }
- /*
- * Changes designated to 0 but it is still potentially providing service
- * Will affect isDesignated, so, it can enter an if(combination) below
- */
- pdpsConnector.standDownPdp(pdp.getPdpId());
-
- //need to change standbystatus to coldstandby
- if (pdp.getPdpId().equals(myPdp.getPdpId())){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp {} is not Current. "
- + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
- }
- // We found that myPdp is designated but not current
- // So, we must cause it to disableFail
- try {
- myPdp.setDesignated(false);
- pdpsConnector.setDesignated(myPdp, false);
- isDesignated = false;
- stateManagementFeature.disableFailed();
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: myPdp: {} Caught Exception "
- + "attempting to disableFail myPdp {}, message= {}",
- myPdp.getPdpId(), myPdp.getPdpId(), e);
- }
- } else { //it is a remote PDP that is failed
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: PDP {} is not Current. "
- + " Executing stateManagement.disableFailed(otherResourceName)\n\n", pdp.getPdpId() );
- }
- // We found a PDP is designated but not current
- // We already called standdown(pdp) which will change designated to false
- // Now we need to disableFail it to get its states in synch. The standbyStatus
- // should equal coldstandby
- try {
- stateManagementFeature.disableFailed(pdp.getPdpId());
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: for PDP {} Caught Exception attempting to "
- + "disableFail({}), message= {}",
- pdp.getPdpId(), pdp.getPdpId(), e);
- }
-
- }
- continue; //we are not going to do anything else with this pdp
- }
-
- /*
- * The third combination of isDesignated and isCurrent
- * /*
- * If a PDP is not currently designated but is providing service (erroneous, but recoverable) or hot standby
- * we can add it to the list of possible designated if all the designated have failed
- */
- if (!pdp.isDesignated() && isCurrent){
- if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
- standbyStatus.equals(StateManagement.COLD_STANDBY))){
- if(logger.isDebugEnabled()){
- logger.debug("\n\nDesignatedWaiter.run: PDP {}"
- + " is NOT designated but IS current and"
- + " has a standbystatus= {}", pdp.getPdpId(), standbyStatus);
- }
- // Since it is current, we assume it can adjust its own state.
- // We will demote if it is myPdp
- if(pdp.getPdpId().equals(myPdp.getPdpId())){
- //demote it
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWaiter.run: PDP {} going to "
- + "setDesignated = false and calling stateManagement.demote", pdp.getPdpId());
- }
- try {
- //Keep the order like this. StateManagement is last since it triggers controller shutdown
- pdpsConnector.setDesignated(myPdp, false);
- myPdp.setDesignated(false);
- isDesignated = false;
- //This is definitely not a redundant call. It is attempting to correct a problem
- stateManagementFeature.demote();
- //recheck the standbystatus
- standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: myPdp: {} Caught Exception "
- + "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(),
- myPdp.getPdpId(), e);
- }
-
- }
- }
- if(standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed){
- //add it to the list
- if(logger.isDebugEnabled()){
- logger.debug
- ("INFO: DesignatedWaiter.run: PDP= {}"
- + " is not designated but is {} and designated PDP "
- + "has failed. standbyStatus= {}", pdp.getPdpId(),
- standbyStatus, standbyStatus);
- }
- listOfDesignated.add(pdp);
- }
- continue; //done with this one
- }
-
- /*
- * The fourth combination of isDesignated and isCurrent
- *
- * We are not going to put any of these on the list since it appears they have failed.
-
- *
- */
- if(!pdp.isDesignated() && !isCurrent) {
- if(logger.isDebugEnabled()){
- logger.debug
- ("INFO: DesignatedWaiter.run: PDP= {} "
- + "designated= {}, current= {}, "
- + "designatedPdpHasFailed= {}, "
- + "standbyStatus= {}",pdp.getPdpId(),
- pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus);
- }
- if(!standbyStatus.equals(StateManagement.COLD_STANDBY)){
- //stand it down
- //disableFail it
- pdpsConnector.standDownPdp(pdp.getPdpId());
- if(pdp.getPdpId().equals(myPdp.getPdpId())){
- /*
- * I don't actually know how this condition could happen, but if it did, we would want
- * to declare it failed.
- */
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
- + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
- }
- // So, we must disableFail it
- try {
- //Keep the order like this. StateManagement is last since it triggers controller shutdown
- pdpsConnector.setDesignated(myPdp, false);
- myPdp.setDesignated(false);
- isDesignated = false;
- stateManagementFeature.disableFailed();
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
- + "disableFail myPdp {}, message= {}",
- myPdp.getPdpId(), myPdp.getPdpId(), e);
- }
- }else{//it is remote
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
- + " Executing stateManagement.disableFailed({})\n\n",
- myPdp.getPdpId(), pdp.getPdpId());
- }
- // We already called standdown(pdp) which will change designated to false
- // Now we need to disableFail it to get its states in sync. StandbyStatus = coldstandby
- try {
- stateManagementFeature.disableFailed(pdp.getPdpId());
- } catch (Exception e) {
- logger.error
- ("DesignatedWaiter.run: for PDP {}"
- + " Caught Exception attempting to disableFail({})"
- + ", message=", pdp.getPdpId(), pdp.getPdpId(), e);
- }
- }
- }
- }
-
-
- } // end pdps loop
-
- /*
- * We have checked the four combinations of isDesignated and isCurrent. Where appropriate,
- * we added the PDPs to the potential list of designated pdps
- *
- * We need to give priority to pdps on the same site that is currently being used
- * First, however, we must sanitize the list of designated to make sure their are
- * only designated members or non-designated members. There should not be both in
- * the list. Because there are real time delays, it is possible that both types could
- * be on the list.
- */
-
- listOfDesignated = santizeDesignatedList(listOfDesignated);
-
- /*
- * We need to figure out the last pdp that was the primary so we can get the last site
- * name and the last session numbers. We need to create a "dummy" droolspdp since
- * it will be used in later comparisons and cannot be null.
- */
-
- DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated);
-
- if(mostRecentPrimary != null){
- pdpdLastActive = mostRecentPrimary.getPdpId();
- }
-
-
- /*
- * It is possible to get here with more than one pdp designated and providingservice. This normally
- * occurs when there is a race condition with multiple nodes coming up at the same time. If that is
- * the case we must determine which one is the one that should be designated and which one should
- * be demoted.
- *
- * It is possible to have 0, 1, 2 or more but not all, or all designated.
- * If we have one designated and current, we chose it and are done
- * If we have 2 or more, but not all, we must determine which one is in the same site as
- * the previously designated pdp.
- */
-
- designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
- if(designatedPdp != null){
- pdpdNowActive = designatedPdp.getPdpId();
- }
-
- if (designatedPdp == null) {
- logger.warn
- ("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. designatedPdp still null.");
- // Just to be sure the parameters are correctly set
- myPdp.setDesignated(false);
- pdpsConnector.setDesignated(myPdp,false);
- isDesignated = false;
-
- waitTimerLastRunDate = new Date();
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}", waitTimerLastRunDate);
- }
- myPdp.setUpdatedDate(waitTimerLastRunDate);
- pdpsConnector.update(myPdp);
-
- return;
-
- } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) {
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId());
- }
- /*
- * update function expects myPdp.isDesignated to be true.
- */
- try {
- //Keep the order like this. StateManagement is last since it triggers controller init
- myPdp.setDesignated(true);
- myPdp.setDesignatedDate(new Date());
- pdpsConnector.setDesignated(myPdp, true);
- isDesignated = true;
- String standbyStatus = stateManagementFeature.getStandbyStatus();
- if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
- /*
- * Only call promote if it is not already in the right state. Don't worry about
- * synching the lower level topic endpoint states. That is done by the
- * refreshStateAudit.
- * Note that we need to fetch the session list from 'mostRecentPrimary'
- * at this point -- soon, 'mostRecentPrimary' will be set to this host.
- */
- //this.sessions = mostRecentPrimary.getSessions();
- stateManagementFeature.promote();
- }
- } catch (Exception e) {
- logger.error
- ("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}"
- + ", message=", myPdp.getPdpId(), e);
- myPdp.setDesignated(false);
- pdpsConnector.setDesignated(myPdp,false);
- isDesignated = false;
- //If you can't promote it, demote it
- try {
- String standbyStatus = stateManagementFeature.getStandbyStatus();
- if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
- standbyStatus.equals(StateManagement.COLD_STANDBY))){
- /*
- * Only call demote if it is not already in the right state. Don't worry about
- * synching the lower level topic endpoint states. That is done by the
- * refreshStateAudit.
- */
- stateManagementFeature.demote();
- }
- } catch (Exception e1) {
- logger.error
- ("ERROR: DesignatedWaiter.run: Caught StandbyStatusException "
- + "attempting to promote then demote PDP={}, message=",
- myPdp.getPdpId(), e1);
- }
-
- }
- waitTimerLastRunDate = new Date();
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) "
- + "waitTimerLastRunDate = " + waitTimerLastRunDate);
- }
- myPdp.setUpdatedDate(waitTimerLastRunDate);
- pdpsConnector.update(myPdp);
-
- return;
- }
- isDesignated = false;
-
- } // end synchronized
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}",
- isDesignated, myPdp.getPdpId());
- }
-
- Date tmpDate = new Date();
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate);
- }
-
- waitTimerLastRunDate = tmpDate;
- myPdp.setUpdatedDate(waitTimerLastRunDate);
- pdpsConnector.update(myPdp);
-
- }catch(Exception e){
- logger.error("DesignatedWaiter.run caught an unexpected exception: ", e);
- }
- } // end run
- }
-
- public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated){
-
- boolean containsDesignated = false;
- boolean containsHotStandby = false;
- List<DroolsPdp> listForRemoval = new ArrayList<>();
- for(DroolsPdp pdp : listOfDesignated){
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run sanitizing: pdp = {}"
- + " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated());
- }
- if(pdp.isDesignated()){
- containsDesignated = true;
- }else {
- containsHotStandby = true;
- listForRemoval.add(pdp);
- }
- }
- if(containsDesignated && containsHotStandby){
- //remove the hot standby from the list
- listOfDesignated.removeAll(listForRemoval);
- }
- return listOfDesignated;
- }
-
- public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, List<DroolsPdp> listOfDesignated){
- boolean containsDesignated = false;
- for(DroolsPdp pdp : listOfDesignated){
- if(pdp.isDesignated()){
- containsDesignated = true;
- }
- }
- DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0));
- mostRecentPrimary.setSiteName(null);
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size());
- }
- if(listOfDesignated.size() <=1){
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWainter.run: listOfDesignated.size <=1");
- }
- //Only one or none is designated or hot standby. Choose the latest designated date
- for(DroolsPdp pdp : pdps){
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run pdp = {}"
- + " pdp.getDesignatedDate() = {}", pdp.getPdpId(), pdp.getDesignatedDate());
- }
- if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
- mostRecentPrimary = pdp;
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
- }
- }
- }
- }else if(listOfDesignated.size() == pdps.size()){
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size());
- }
- //They are all designated or all hot standby.
- mostRecentPrimary = null;
- for(DroolsPdp pdp : pdps){
- if(mostRecentPrimary == null){
- mostRecentPrimary = pdp;
- continue;
- }
- if(containsDesignated){ //Choose the site of the first designated date
- if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0){
- mostRecentPrimary = pdp;
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
- }
- }
- }else{ //Choose the site with the latest designated date
- if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
- mostRecentPrimary = pdp;
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
- }
- }
- }
- }
- }else{
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. ");
- }
- //Some but not all are designated or hot standby.
- if(containsDesignated){
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
- }
- /*
- * The list only contains designated. This is a problem. It is most likely a race
- * condition that resulted in two thinking they should be designated. Choose the
- * site with the latest designated date for the pdp not included on the designated list.
- * This should be the site that had the last designation before this race condition
- * occurred.
- */
- for(DroolsPdp pdp : pdps){
- if(listOfDesignated.contains(pdp)){
- continue; //Don't consider this entry
- }
- if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
- mostRecentPrimary = pdp;
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
- }
- }
- }
- }else{
- if(logger.isDebugEnabled()){
- logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
- }
- //The list only contains hot standby. Choose the site of the latest designated date
- for(DroolsPdp pdp : pdps){
- if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
- mostRecentPrimary = pdp;
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
- }
- }
- }
- }
- }
- return mostRecentPrimary;
- }
-
- public DroolsPdp computeDesignatedPdp(List<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary){
- DroolsPdp designatedPdp = null;
- DroolsPdp lowestPriorityPdp = null;
- if(listOfDesignated.size() > 1){
- if(logger.isDebugEnabled()){
- logger.debug
- ("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(), listOfDesignated.size());
- }
- DroolsPdp rejectedPdp = null;
- DroolsPdp lowestPrioritySameSite = null;
- DroolsPdp lowestPriorityDifferentSite = null;
- for(DroolsPdp pdp : listOfDesignated){
- // We need to determine if another PDP is the lowest priority
- if(nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())){
- if(lowestPrioritySameSite == null){
- if(lowestPriorityDifferentSite != null){
- rejectedPdp = lowestPriorityDifferentSite;
- }
- lowestPrioritySameSite = pdp;
- }else{
- if(pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))){
- continue;//nothing to compare
- }
- if(pdp.comparePriority(lowestPrioritySameSite) <0){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
- + " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(),
- lowestPrioritySameSite.getPdpId());
- }
- //we need to reject lowestPrioritySameSite
- rejectedPdp = lowestPrioritySameSite;
- lowestPrioritySameSite = pdp;
- } else{
- //we need to reject pdp and keep lowestPrioritySameSite
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} "
- + " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(),
- lowestPrioritySameSite.getPdpId());
- }
- rejectedPdp = pdp;
- }
- }
- } else{
- if(lowestPrioritySameSite != null){
- //if we already have a candidate for same site, we don't want to bother with different sites
- rejectedPdp = pdp;
- } else{
- if(lowestPriorityDifferentSite == null){
- lowestPriorityDifferentSite = pdp;
- continue;
- }
- if(pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))){
- continue;//nothing to compare
- }
- if(pdp.comparePriority(lowestPriorityDifferentSite) <0){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
- + " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
- lowestPriorityDifferentSite.getPdpId());
- }
- //we need to reject lowestPriorityDifferentSite
- rejectedPdp = lowestPriorityDifferentSite;
- lowestPriorityDifferentSite = pdp;
- } else{
- //we need to reject pdp and keep lowestPriorityDifferentSite
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
- + " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
- lowestPriorityDifferentSite.getPdpId());
- }
- rejectedPdp = pdp;
- }
- }
- }
- // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible
- // for demoting itself
- if(rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}"
- + " is NOT the lowest priority. Executing stateManagement.demote()\n\n", myPdp.getPdpId(),
- myPdp.getPdpId());
- }
- // We found that myPdp is on the listOfDesignated and it is not the lowest priority
- // So, we must demote it
- try {
- //Keep the order like this. StateManagement is last since it triggers controller shutdown
- myPdp.setDesignated(false);
- pdpsConnector.setDesignated(myPdp, false);
- isDesignated = false;
- String standbyStatus = stateManagementFeature.getStandbyStatus();
- if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
- standbyStatus.equals(StateManagement.COLD_STANDBY))){
- /*
- * Only call demote if it is not already in the right state. Don't worry about
- * synching the lower level topic endpoint states. That is done by the
- * refreshStateAudit.
- */
- stateManagementFeature.demote();
- }
- } catch (Exception e) {
- myPdp.setDesignated(false);
- pdpsConnector.setDesignated(myPdp, false);
- isDesignated = false;
- logger.error
- ("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
- + "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(),
- e);
- }
- }
- } //end: for(DroolsPdp pdp : listOfDesignated)
- if(lowestPrioritySameSite != null){
- lowestPriorityPdp = lowestPrioritySameSite;
- } else {
- lowestPriorityPdp = lowestPriorityDifferentSite;
- }
- //now we have a valid value for lowestPriorityPdp
- if(logger.isDebugEnabled()){
- logger.debug
- ("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
- + "found the LOWEST priority pdp ID: {} "
- + " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n",
- myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp);
- }
- designatedPdp = lowestPriorityPdp;
-
- } else if(listOfDesignated.isEmpty()){
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId());
- }
- designatedPdp = null;
- } else{ //only one in listOfDesignated
- if(logger.isDebugEnabled()){
- logger.debug
- ("\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
- + "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId());
- }
- designatedPdp = listOfDesignated.get(0);
- }
- return designatedPdp;
-
- }
-
- private class TimerUpdateClass extends TimerTask{
-
- @Override
- public void run() {
- try{
- if(logger.isDebugEnabled()){
- logger.debug("TimerUpdateClass.run: entry");
- }
- checkWaitTimer();
- }catch(Exception e){
- logger.error("TimerUpdateClass.run caught an unexpected exception: ", e);
- }
- if(logger.isDebugEnabled()){
- logger.debug("TimerUpdateClass.run.exit");
- }
- }
- }
- @Override
- public void checkThreadStatus() {
- checkWaitTimer();
- }
-
- private void checkWaitTimer(){
- synchronized(checkWaitTimerLock){
- try{
- if(logger.isDebugEnabled()){
- logger.debug("checkWaitTimer: entry");
- }
- Date now = new Date();
- long nowMs = now.getTime();
- long waitTimerMs = waitTimerLastRunDate.getTime();
-
- //give it 10 times leeway
- if((nowMs - waitTimerMs) > 10*pdpUpdateInterval){
- if(allSeemsWell==null || allSeemsWell){
- allSeemsWell = false;
- if(logger.isDebugEnabled()){
- logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param");
- }
- stateManagementFeature.allSeemsWell(this.getClass().getName(),
- StateManagementFeatureAPI.ALLNOTWELL_STATE,
- "DesignationWaiter/ElectionHandler has STALLED");
- }
- logger.error("checkWaitTimer: nowMs - waitTimerMs = {}"
- + ", exceeds 10* pdpUpdateInterval = {}"
- + " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10*pdpUpdateInterval));
- }else if(allSeemsWell==null || !allSeemsWell){
- allSeemsWell = true;
- stateManagementFeature.allSeemsWell(this.getClass().getName(),
- StateManagementFeatureAPI.ALLSEEMSWELL_STATE,
- "DesignationWaiter/ElectionHandler has RESUMED");
- logger.info("DesignationWaiter/ElectionHandler has RESUMED");
- }
- if(logger.isDebugEnabled()){
- logger.debug("checkWaitTimer: exit");
- }
- }catch(Exception e){
- logger.error("checkWaitTimer: caught unexpected exception: ", e);
- }
- }
- }
-
- private long getDWaiterStartMs(){
- Date now = new Date();
-
- // Retrieve the ms since the epoch
- long nowMs = now.getTime();
-
- // Time since the end of the last pdpUpdateInterval multiple
- long nowModMs = nowMs % pdpUpdateInterval;
-
- // Time to the start of the next pdpUpdateInterval multiple
- long startMs = 2*pdpUpdateInterval - nowModMs;
-
- // Give the start time a minimum of a 5 second cushion
- if(startMs < 5000){
- // Start at the beginning of following interval
- startMs = pdpUpdateInterval + startMs;
- }
- return startMs;
- }
-
- private boolean nullSafeEquals(Object one, Object two){
- if(one == null && two == null){
- return true;
- }
- if(one != null && two != null){
- return one.equals(two);
- }
- return false;
- }
-
- public String getPdpdNowActive(){
- return pdpdNowActive;
- }
-
- public String getPdpdLastActive(){
- return pdpdLastActive;
- }
+ // get an instance of logger
+ private static final Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class);
+ private DroolsPdpsConnector pdpsConnector;
+ private Object checkWaitTimerLock = new Object();
+ private Object designationWaiterLock = new Object();
+
+ /*
+ * Must be static, so it can be referenced by JpaDroolsPdpsConnector,
+ * without requiring a reference to the election handler instantiation.
+ */
+ private static DroolsPdp myPdp;
+
+ private DesignationWaiter designationWaiter;
+ private Timer updateWorker;
+ private Timer waitTimer;
+ private Date waitTimerLastRunDate;
+
+ // The interval between checks of the DesignationWaiter to be sure it is running.
+ private int pdpCheckInterval;
+
+ // The interval between runs of the DesignationWaiter
+ private int pdpUpdateInterval;
+
+ private volatile boolean isDesignated;
+
+ private String pdpdNowActive;
+ private String pdpdLastActive;
+
+ /*
+ * Start allSeemsWell with a value of null so that, on the first run
+ * of the checkWaitTimer it will set the value in IntegrityMonitor
+ * regardless of whether it needs to be set to true or false.
+ */
+ private Boolean allSeemsWell = null;
+
+ private StateManagementFeatureAPI stateManagementFeature;
+
+ private static boolean isUnitTesting = false;
+ private static boolean isStalled = false;
+
+ /**
+ * Constructor.
+ *
+ * @param pdps connectors
+ * @param myPdp pdp
+ */
+ public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp) {
+ if (pdps == null) {
+ logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null");
+ throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null");
+ }
+ if (myPdp == null) {
+ logger.error("DroolsPdpsElectinHandler(): droolsPdp==null");
+ throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null");
+ }
+
+ pdpdNowActive = null;
+ pdpdLastActive = null;
+ this.pdpsConnector = pdps;
+ DroolsPdpsElectionHandler.myPdp = myPdp;
+ this.isDesignated = false;
+ pdpCheckInterval = 3000;
+ try {
+ pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(
+ ActiveStandbyProperties.PDP_CHECK_INVERVAL));
+ } catch (Exception e) {
+ logger.error("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e);
+ }
+ pdpUpdateInterval = 2000;
+ try {
+ pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(
+ ActiveStandbyProperties.PDP_UPDATE_INTERVAL));
+ } catch (Exception e) {
+ logger.error("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e);
+ }
+
+ Date now = new Date();
+
+ // Retrieve the ms since the epoch
+ final long nowMs = now.getTime();
+
+ // Create the timer which will update the updateDate in DroolsPdpEntity table.
+ // This is the heartbeat
+ updateWorker = new Timer();
+
+ // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter
+ // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the
+ // drools droolsPdpEntity table.
+ updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
+
+ // Create the timer which will run the election algorithm
+ waitTimer = new Timer();
+
+ // Schedule it to start in startMs ms
+ // (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter
+ long startMs = getDWaiterStartMs();
+ designationWaiter = new DesignationWaiter();
+ waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
+ waitTimerLastRunDate = new Date(nowMs + startMs);
+
+ //Get the StateManagementFeature instance
+
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ if (feature.getResourceName().equals(myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature"
+ + " with resourceName: {}", myPdp.getPdpId());
+ }
+ stateManagementFeature = feature;
+ break;
+ }
+ }
+ if (stateManagementFeature == null) {
+ logger.error("DroolsPdpsElectionHandler failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", myPdp.getPdpId());
+ }
+ }
+
+ public static void setIsUnitTesting(boolean val) {
+ isUnitTesting = val;
+ }
+
+ public static void setIsStalled(boolean val) {
+ isStalled = val;
+ }
+
+ /**
+ * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs
+ * access to myPdp, so it can keep its designation status in sync with the
+ * DB.
+ *
+ * @param designated is designated value
+ */
+ public static void setMyPdpDesignated(boolean designated) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("setMyPdpDesignated: designated= {}", designated);
+ }
+ myPdp.setDesignated(designated);
+ }
+
+ private class DesignationWaiter extends TimerTask {
+ // get an instance of logger
+ private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class);
+
+ @Override
+ public void run() {
+ try {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: Entering");
+ }
+
+ //This is for testing the checkWaitTimer
+ if (isUnitTesting && isStalled) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}",
+ isUnitTesting, isStalled);
+ }
+ return;
+ }
+
+ synchronized (designationWaiterLock) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: Entering synchronized block");
+ }
+
+ //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated
+ //PDPs and then decide which one really should be designated at the end.
+ List<DroolsPdp> listOfDesignated = new ArrayList<>();
+
+ Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps();
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: pdps.size= {}", pdps.size());
+ }
+
+ //This is only true if all designated PDPs have failed
+ boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps);
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed);
+ }
+ for (DroolsPdp pdp : pdps) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId());
+ }
+
+ /*
+ * Note: side effect of isPdpCurrent is that any stale but
+ * designated PDPs will be marked as un-designated.
+ */
+ boolean isCurrent = pdpsConnector.isPdpCurrent(pdp);
+
+ /*
+ * We can't use stateManagement.getStandbyStatus() here, because
+ * we need the standbyStatus, not for this PDP, but for the PDP
+ * being processed by this loop iteration.
+ */
+ String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
+ if (standbyStatus == null) {
+ // Treat this case as a cold standby -- if we
+ // abort here, no sessions will be created in a
+ // single-node test environment.
+ standbyStatus = StateManagement.COLD_STANDBY;
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent);
+ }
+
+ /*
+ * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn
+ * and evaluate the each pdp in the list of pdps against each combination.
+ *
+ * This is the first combination of isDesignated and isCurrent
+ */
+ if (pdp.isDesignated() && isCurrent) {
+ //It is current, but it could have a standbystatus=coldstandby / hotstandby
+ //If so, we need to stand it down and demote it
+ if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) {
+ if (pdp.getPdpId().equals(myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
+ + "butstandbystatus is not providingservice. "
+ + " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId());
+ }
+ // So, we must demote it
+ try {
+ //Keep the order like this. StateManagement is last since it
+ //triggers controller shutdown
+ //This will change isDesignated and it can enter another if(combination) below
+ pdpsConnector.standDownPdp(pdp.getPdpId());
+ myPdp.setDesignated(false);
+ isDesignated = false;
+ if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
+ || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
+ /*
+ * Only demote it if it appears it has not already been demoted. Don't worry
+ * about synching with the topic endpoint states. That is done by the
+ * refreshStateAudit
+ */
+ stateManagementFeature.demote();
+ }
+ //update the standbystatus to check in a later
+ //combination of isDesignated and isCurrent
+ standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: myPdp: {} "
+ + "Caught Exception attempting to demote myPdp,"
+ + "message= {}", myPdp.getPdpId(), e);
+ }
+ } else {
+ // Don't demote a remote PDP that is current. It should catch itself
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
+ + "but standbystatus is not providingservice. "
+ + " Cannot execute stateManagement.demote() "
+ + "since it it is not myPdp\n\n",
+ myPdp.getPdpId());
+ }
+ }
+
+ } else {
+ // If we get here, it is ok to be on the list
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: PDP= {} is designated, "
+ + "current and {} Noting PDP as "
+ + "designated, standbyStatus= {}",
+ pdp.getPdpId(), standbyStatus, standbyStatus);
+ }
+ listOfDesignated.add(pdp);
+ }
+
+
+ }
+
+
+ /*
+ * The second combination of isDesignated and isCurrent
+ *
+ * PDP is designated but not current; it has failed.
+ * So we stand it down (it doesn't matter what
+ * its standbyStatus is). None of these go on the list.
+ */
+ if (pdp.isDesignated() && !isCurrent) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("INFO: DesignatedWaiter.run: PDP= {} is currently "
+ + "designated but is not current; "
+ + "it has failed. Standing down. standbyStatus= {}",
+ pdp.getPdpId(), standbyStatus);
+ }
+ /*
+ * Changes designated to 0 but it is still potentially providing service
+ * Will affect isDesignated, so, it can enter an if(combination) below
+ */
+ pdpsConnector.standDownPdp(pdp.getPdpId());
+
+ //need to change standbystatus to coldstandby
+ if (pdp.getPdpId().equals(myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp {} is not Current. "
+ + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
+ }
+ // We found that myPdp is designated but not current
+ // So, we must cause it to disableFail
+ try {
+ myPdp.setDesignated(false);
+ pdpsConnector.setDesignated(myPdp, false);
+ isDesignated = false;
+ stateManagementFeature.disableFailed();
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception "
+ + "attempting to disableFail myPdp {}, message= {}",
+ myPdp.getPdpId(), myPdp.getPdpId(), e);
+ }
+ } else { //it is a remote PDP that is failed
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: PDP {} is not Current. "
+ + " Executing stateManagement.disableFailed(otherResourceName)\n\n",
+ pdp.getPdpId() );
+ }
+ // We found a PDP is designated but not current
+ // We already called standdown(pdp) which will change designated to false
+ // Now we need to disableFail it to get its states in synch. The standbyStatus
+ // should equal coldstandby
+ try {
+ stateManagementFeature.disableFailed(pdp.getPdpId());
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: for PDP {} Caught Exception attempting to "
+ + "disableFail({}), message= {}",
+ pdp.getPdpId(), pdp.getPdpId(), e);
+ }
+
+ }
+ continue; //we are not going to do anything else with this pdp
+ }
+
+ /*
+ * The third combination of isDesignated and isCurrent
+ * /*
+ * If a PDP is not currently designated but is providing service
+ * (erroneous, but recoverable) or hot standby
+ * we can add it to the list of possible designated if all the designated have failed
+ */
+ if (!pdp.isDesignated() && isCurrent) {
+ if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
+ || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: PDP {}"
+ + " is NOT designated but IS current and"
+ + " has a standbystatus= {}", pdp.getPdpId(), standbyStatus);
+ }
+ // Since it is current, we assume it can adjust its own state.
+ // We will demote if it is myPdp
+ if (pdp.getPdpId().equals(myPdp.getPdpId())) {
+ //demote it
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: PDP {} going to "
+ + "setDesignated = false and calling stateManagement.demote",
+ pdp.getPdpId());
+ }
+ try {
+ //Keep the order like this.
+ //StateManagement is last since it triggers controller shutdown
+ pdpsConnector.setDesignated(myPdp, false);
+ myPdp.setDesignated(false);
+ isDesignated = false;
+ //This is definitely not a redundant call.
+ //It is attempting to correct a problem
+ stateManagementFeature.demote();
+ //recheck the standbystatus
+ standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception "
+ + "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(),
+ myPdp.getPdpId(), e);
+ }
+
+ }
+ }
+ if (standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed) {
+ //add it to the list
+ if (logger.isDebugEnabled()) {
+ logger.debug("INFO: DesignatedWaiter.run: PDP= {}"
+ + " is not designated but is {} and designated PDP "
+ + "has failed. standbyStatus= {}", pdp.getPdpId(),
+ standbyStatus, standbyStatus);
+ }
+ listOfDesignated.add(pdp);
+ }
+ continue; //done with this one
+ }
+
+ /*
+ * The fourth combination of isDesignated and isCurrent
+ *
+ * We are not going to put any of these on the list since it appears they have failed.
+
+ *
+ */
+ if (!pdp.isDesignated() && !isCurrent) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("INFO: DesignatedWaiter.run: PDP= {} "
+ + "designated= {}, current= {}, "
+ + "designatedPdpHasFailed= {}, "
+ + "standbyStatus= {}",pdp.getPdpId(),
+ pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus);
+ }
+ if (!standbyStatus.equals(StateManagement.COLD_STANDBY)) {
+ //stand it down
+ //disableFail it
+ pdpsConnector.standDownPdp(pdp.getPdpId());
+ if (pdp.getPdpId().equals(myPdp.getPdpId())) {
+ /*
+ * I don't actually know how this condition could happen,
+ * but if it did, we would want
+ * to declare it failed.
+ */
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
+ + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
+ }
+ // So, we must disableFail it
+ try {
+ //Keep the order like this.
+ //StateManagement is last since it triggers controller shutdown
+ pdpsConnector.setDesignated(myPdp, false);
+ myPdp.setDesignated(false);
+ isDesignated = false;
+ stateManagementFeature.disableFailed();
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
+ + "disableFail myPdp {}, message= {}",
+ myPdp.getPdpId(), myPdp.getPdpId(), e);
+ }
+ } else { //it is remote
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
+ + " Executing stateManagement.disableFailed({})\n\n",
+ myPdp.getPdpId(), pdp.getPdpId());
+ }
+ // We already called standdown(pdp) which will change designated to false
+ // Now we need to disableFail it to get its states in sync.
+ // StandbyStatus = coldstandby
+ try {
+ stateManagementFeature.disableFailed(pdp.getPdpId());
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run: for PDP {}"
+ + " Caught Exception attempting to disableFail({})"
+ + ", message=", pdp.getPdpId(), pdp.getPdpId(), e);
+ }
+ }
+ }
+ }
+
+
+ } // end pdps loop
+
+ /*
+ * We have checked the four combinations of isDesignated and isCurrent. Where appropriate,
+ * we added the PDPs to the potential list of designated pdps
+ *
+ * We need to give priority to pdps on the same site that is currently being used
+ * First, however, we must sanitize the list of designated to make sure their are
+ * only designated members or non-designated members. There should not be both in
+ * the list. Because there are real time delays, it is possible that both types could
+ * be on the list.
+ */
+
+ listOfDesignated = santizeDesignatedList(listOfDesignated);
+
+ /*
+ * We need to figure out the last pdp that was the primary so we can get the last site
+ * name and the last session numbers. We need to create a "dummy" droolspdp since
+ * it will be used in later comparisons and cannot be null.
+ */
+
+ DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated);
+
+ if (mostRecentPrimary != null) {
+ pdpdLastActive = mostRecentPrimary.getPdpId();
+ }
+
+
+ /*
+ * It is possible to get here with more than one pdp designated and providingservice. This normally
+ * occurs when there is a race condition with multiple nodes coming up at the same time. If that is
+ * the case we must determine which one is the one that should be designated and which one should
+ * be demoted.
+ *
+ * It is possible to have 0, 1, 2 or more but not all, or all designated.
+ * If we have one designated and current, we chose it and are done
+ * If we have 2 or more, but not all, we must determine which one is in the same site as
+ * the previously designated pdp.
+ */
+ DroolsPdp designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
+ if (designatedPdp != null) {
+ pdpdNowActive = designatedPdp.getPdpId();
+ }
+
+ if (designatedPdp == null) {
+ logger.warn("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. "
+ + "designatedPdp still null.");
+ // Just to be sure the parameters are correctly set
+ myPdp.setDesignated(false);
+ pdpsConnector.setDesignated(myPdp,false);
+ isDesignated = false;
+
+ waitTimerLastRunDate = new Date();
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}",
+ waitTimerLastRunDate);
+ }
+ myPdp.setUpdatedDate(waitTimerLastRunDate);
+ pdpsConnector.update(myPdp);
+
+ return;
+
+ } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId());
+ }
+ /*
+ * update function expects myPdp.isDesignated to be true.
+ */
+ try {
+ //Keep the order like this. StateManagement is last since it triggers controller init
+ myPdp.setDesignated(true);
+ myPdp.setDesignatedDate(new Date());
+ pdpsConnector.setDesignated(myPdp, true);
+ isDesignated = true;
+ String standbyStatus = stateManagementFeature.getStandbyStatus();
+ if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) {
+ /*
+ * Only call promote if it is not already in the right state. Don't worry about
+ * synching the lower level topic endpoint states. That is done by the
+ * refreshStateAudit.
+ * Note that we need to fetch the session list from 'mostRecentPrimary'
+ * at this point -- soon, 'mostRecentPrimary' will be set to this host.
+ */
+ //this.sessions = mostRecentPrimary.getSessions();
+ stateManagementFeature.promote();
+ }
+ } catch (Exception e) {
+ logger.error("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}"
+ + ", message=", myPdp.getPdpId(), e);
+ myPdp.setDesignated(false);
+ pdpsConnector.setDesignated(myPdp,false);
+ isDesignated = false;
+ //If you can't promote it, demote it
+ try {
+ String standbyStatus = stateManagementFeature.getStandbyStatus();
+ if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
+ || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
+ /*
+ * Only call demote if it is not already in the right state. Don't worry about
+ * synching the lower level topic endpoint states. That is done by the
+ * refreshStateAudit.
+ */
+ stateManagementFeature.demote();
+ }
+ } catch (Exception e1) {
+ logger.error("ERROR: DesignatedWaiter.run: Caught StandbyStatusException "
+ + "attempting to promote then demote PDP={}, message=",
+ myPdp.getPdpId(), e1);
+ }
+
+ }
+ waitTimerLastRunDate = new Date();
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) "
+ + "waitTimerLastRunDate = " + waitTimerLastRunDate);
+ }
+ myPdp.setUpdatedDate(waitTimerLastRunDate);
+ pdpsConnector.update(myPdp);
+
+ return;
+ }
+ isDesignated = false;
+
+ } // end synchronized
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}",
+ isDesignated, myPdp.getPdpId());
+ }
+
+ Date tmpDate = new Date();
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate);
+ }
+
+ waitTimerLastRunDate = tmpDate;
+ myPdp.setUpdatedDate(waitTimerLastRunDate);
+ pdpsConnector.update(myPdp);
+
+ } catch (Exception e) {
+ logger.error("DesignatedWaiter.run caught an unexpected exception: ", e);
+ }
+ } // end run
+ }
+
+ /**
+ * Sanitize designated list.
+ *
+ * @param listOfDesignated list of designated pdps
+ * @return
+ */
+ public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated) {
+
+ boolean containsDesignated = false;
+ boolean containsHotStandby = false;
+ List<DroolsPdp> listForRemoval = new ArrayList<>();
+ for (DroolsPdp pdp : listOfDesignated) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run sanitizing: pdp = {}"
+ + " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated());
+ }
+ if (pdp.isDesignated()) {
+ containsDesignated = true;
+ } else {
+ containsHotStandby = true;
+ listForRemoval.add(pdp);
+ }
+ }
+ if (containsDesignated && containsHotStandby) {
+ //remove the hot standby from the list
+ listOfDesignated.removeAll(listForRemoval);
+ }
+ return listOfDesignated;
+ }
+
+ /**
+ * Compute most recent primary.
+ *
+ * @param pdps collection of pdps
+ * @param listOfDesignated list of designated pdps
+ * @return
+ */
+ public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, List<DroolsPdp> listOfDesignated) {
+ boolean containsDesignated = false;
+ for (DroolsPdp pdp : listOfDesignated) {
+ if (pdp.isDesignated()) {
+ containsDesignated = true;
+ }
+ }
+ DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0));
+ mostRecentPrimary.setSiteName(null);
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size());
+ }
+ if (listOfDesignated.size() <= 1) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWainter.run: listOfDesignated.size <=1");
+ }
+ //Only one or none is designated or hot standby. Choose the latest designated date
+ for (DroolsPdp pdp : pdps) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run pdp = {}"
+ + " pdp.getDesignatedDate() = {}",
+ pdp.getPdpId(), pdp.getDesignatedDate());
+ }
+ if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
+ mostRecentPrimary = pdp;
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run mostRecentPrimary = {}",
+ mostRecentPrimary.getPdpId());
+ }
+ }
+ }
+ } else if (listOfDesignated.size() == pdps.size()) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size());
+ }
+ //They are all designated or all hot standby.
+ mostRecentPrimary = null;
+ for (DroolsPdp pdp : pdps) {
+ if (mostRecentPrimary == null) {
+ mostRecentPrimary = pdp;
+ continue;
+ }
+ if (containsDesignated) { //Choose the site of the first designated date
+ if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0) {
+ mostRecentPrimary = pdp;
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
+ }
+ }
+ } else { //Choose the site with the latest designated date
+ if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
+ mostRecentPrimary = pdp;
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
+ }
+ }
+ }
+ }
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. ");
+ }
+ //Some but not all are designated or hot standby.
+ if (containsDesignated) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
+ }
+ /*
+ * The list only contains designated. This is a problem. It is most likely a race
+ * condition that resulted in two thinking they should be designated. Choose the
+ * site with the latest designated date for the pdp not included on the designated list.
+ * This should be the site that had the last designation before this race condition
+ * occurred.
+ */
+ for (DroolsPdp pdp : pdps) {
+ if (listOfDesignated.contains(pdp)) {
+ continue; //Don't consider this entry
+ }
+ if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
+ mostRecentPrimary = pdp;
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
+ }
+ }
+ }
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
+ }
+ //The list only contains hot standby. Choose the site of the latest designated date
+ for (DroolsPdp pdp : pdps) {
+ if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
+ mostRecentPrimary = pdp;
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
+ }
+ }
+ }
+ }
+ }
+ return mostRecentPrimary;
+ }
+
+ /**
+ * Compue designated pdp.
+ *
+ * @param listOfDesignated list of designated pdps
+ * @param mostRecentPrimary most recent primary pdpd
+ * @return
+ */
+ public DroolsPdp computeDesignatedPdp(List<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary) {
+ DroolsPdp designatedPdp = null;
+ DroolsPdp lowestPriorityPdp = null;
+ if (listOfDesignated.size() > 1) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(),
+ listOfDesignated.size());
+ }
+ DroolsPdp rejectedPdp = null;
+ DroolsPdp lowestPrioritySameSite = null;
+ DroolsPdp lowestPriorityDifferentSite = null;
+ for (DroolsPdp pdp : listOfDesignated) {
+ // We need to determine if another PDP is the lowest priority
+ if (nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())) {
+ if (lowestPrioritySameSite == null) {
+ if (lowestPriorityDifferentSite != null) {
+ rejectedPdp = lowestPriorityDifferentSite;
+ }
+ lowestPrioritySameSite = pdp;
+ } else {
+ if (pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))) {
+ continue;//nothing to compare
+ }
+ if (pdp.comparePriority(lowestPrioritySameSite) < 0) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ + " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(),
+ lowestPrioritySameSite.getPdpId());
+ }
+ //we need to reject lowestPrioritySameSite
+ rejectedPdp = lowestPrioritySameSite;
+ lowestPrioritySameSite = pdp;
+ } else {
+ //we need to reject pdp and keep lowestPrioritySameSite
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} "
+ + " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(),
+ lowestPrioritySameSite.getPdpId());
+ }
+ rejectedPdp = pdp;
+ }
+ }
+ } else {
+ if (lowestPrioritySameSite != null) {
+ //if we already have a candidate for same site, we don't want to bother with different sites
+ rejectedPdp = pdp;
+ } else {
+ if (lowestPriorityDifferentSite == null) {
+ lowestPriorityDifferentSite = pdp;
+ continue;
+ }
+ if (pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))) {
+ continue;//nothing to compare
+ }
+ if (pdp.comparePriority(lowestPriorityDifferentSite) < 0) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ + " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
+ lowestPriorityDifferentSite.getPdpId());
+ }
+ //we need to reject lowestPriorityDifferentSite
+ rejectedPdp = lowestPriorityDifferentSite;
+ lowestPriorityDifferentSite = pdp;
+ } else {
+ //we need to reject pdp and keep lowestPriorityDifferentSite
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ + " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
+ lowestPriorityDifferentSite.getPdpId());
+ }
+ rejectedPdp = pdp;
+ }
+ }
+ }
+ // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible
+ // for demoting itself
+ if (rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}"
+ + " is NOT the lowest priority. Executing stateManagement.demote()\n\n",
+ myPdp.getPdpId(),
+ myPdp.getPdpId());
+ }
+ // We found that myPdp is on the listOfDesignated and it is not the lowest priority
+ // So, we must demote it
+ try {
+ //Keep the order like this. StateManagement is last since it triggers controller shutdown
+ myPdp.setDesignated(false);
+ pdpsConnector.setDesignated(myPdp, false);
+ isDesignated = false;
+ String standbyStatus = stateManagementFeature.getStandbyStatus();
+ if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
+ || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
+ /*
+ * Only call demote if it is not already in the right state. Don't worry about
+ * synching the lower level topic endpoint states. That is done by the
+ * refreshStateAudit.
+ */
+ stateManagementFeature.demote();
+ }
+ } catch (Exception e) {
+ myPdp.setDesignated(false);
+ pdpsConnector.setDesignated(myPdp, false);
+ isDesignated = false;
+ logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
+ + "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(),
+ e);
+ }
+ }
+ } //end: for(DroolsPdp pdp : listOfDesignated)
+ if (lowestPrioritySameSite != null) {
+ lowestPriorityPdp = lowestPrioritySameSite;
+ } else {
+ lowestPriorityPdp = lowestPriorityDifferentSite;
+ }
+ //now we have a valid value for lowestPriorityPdp
+ if (logger.isDebugEnabled()) {
+ logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
+ + "found the LOWEST priority pdp ID: {} "
+ + " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n",
+ myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp);
+ }
+ designatedPdp = lowestPriorityPdp;
+
+ } else if (listOfDesignated.isEmpty()) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId());
+ }
+ designatedPdp = null;
+ } else { //only one in listOfDesignated
+ if (logger.isDebugEnabled()) {
+ logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
+ + "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId());
+ }
+ designatedPdp = listOfDesignated.get(0);
+ }
+ return designatedPdp;
+
+ }
+
+ private class TimerUpdateClass extends TimerTask {
+
+ @Override
+ public void run() {
+ try {
+ if (logger.isDebugEnabled()) {
+ logger.debug("TimerUpdateClass.run: entry");
+ }
+ checkWaitTimer();
+ } catch (Exception e) {
+ logger.error("TimerUpdateClass.run caught an unexpected exception: ", e);
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("TimerUpdateClass.run.exit");
+ }
+ }
+ }
+
+ @Override
+ public void checkThreadStatus() {
+ checkWaitTimer();
+ }
+
+ private void checkWaitTimer() {
+ synchronized (checkWaitTimerLock) {
+ try {
+ if (logger.isDebugEnabled()) {
+ logger.debug("checkWaitTimer: entry");
+ }
+ Date now = new Date();
+ long nowMs = now.getTime();
+ long waitTimerMs = waitTimerLastRunDate.getTime();
+
+ //give it 10 times leeway
+ if ((nowMs - waitTimerMs) > 10 * pdpUpdateInterval) {
+ if (allSeemsWell == null || allSeemsWell) {
+ allSeemsWell = false;
+ if (logger.isDebugEnabled()) {
+ logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param");
+ }
+ stateManagementFeature.allSeemsWell(this.getClass().getName(),
+ StateManagementFeatureAPI.ALLNOTWELL_STATE,
+ "DesignationWaiter/ElectionHandler has STALLED");
+ }
+ logger.error("checkWaitTimer: nowMs - waitTimerMs = {}"
+ + ", exceeds 10* pdpUpdateInterval = {}"
+ + " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10 * pdpUpdateInterval));
+ } else if (allSeemsWell == null || !allSeemsWell) {
+ allSeemsWell = true;
+ stateManagementFeature.allSeemsWell(this.getClass().getName(),
+ StateManagementFeatureAPI.ALLSEEMSWELL_STATE,
+ "DesignationWaiter/ElectionHandler has RESUMED");
+ logger.info("DesignationWaiter/ElectionHandler has RESUMED");
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("checkWaitTimer: exit");
+ }
+ } catch (Exception e) {
+ logger.error("checkWaitTimer: caught unexpected exception: ", e);
+ }
+ }
+ }
+
+ private long getDWaiterStartMs() {
+ Date now = new Date();
+
+ // Retrieve the ms since the epoch
+ long nowMs = now.getTime();
+
+ // Time since the end of the last pdpUpdateInterval multiple
+ long nowModMs = nowMs % pdpUpdateInterval;
+
+ // Time to the start of the next pdpUpdateInterval multiple
+ long startMs = 2 * pdpUpdateInterval - nowModMs;
+
+ // Give the start time a minimum of a 5 second cushion
+ if (startMs < 5000) {
+ // Start at the beginning of following interval
+ startMs = pdpUpdateInterval + startMs;
+ }
+ return startMs;
+ }
+
+ private boolean nullSafeEquals(Object one, Object two) {
+ if (one == null && two == null) {
+ return true;
+ }
+ if (one != null && two != null) {
+ return one.equals(two);
+ }
+ return false;
+ }
+
+ public String getPdpdNowActive() {
+ return pdpdNowActive;
+ }
+
+ public String getPdpdLastActive() {
+ return pdpdLastActive;
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java
index dc907b27..04448052 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java
@@ -36,599 +36,599 @@ import org.slf4j.LoggerFactory;
public class JpaDroolsPdpsConnector implements DroolsPdpsConnector {
- // get an instance of logger
- private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class);
- private EntityManagerFactory emf;
-
-
- //not sure if we want to use the same entity manager factory for drools session and pass it in here, or create a new one
- public JpaDroolsPdpsConnector(EntityManagerFactory emf){
- this.emf = emf;
- }
- @Override
- public Collection<DroolsPdp> getDroolsPdps() {
- //return a list of all the DroolsPdps in the database
- EntityManager em = emf.createEntityManager();
- try {
- em.getTransaction().begin();
- Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p");
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>();
- for(Object o : droolsPdpsList){
- if(o instanceof DroolsPdp){
- //Make sure it is not a cached version
- em.refresh((DroolsPdpEntity)o);
- droolsPdpsReturnList.add((DroolsPdp)o);
- if (logger.isDebugEnabled()) {
- DroolsPdp droolsPdp = (DroolsPdp)o;
- logger.debug("getDroolsPdps: PDP= {}"
- + ", isDesignated= {}"
- + ", updatedDate= {}"
- + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(),
- droolsPdp.getUpdatedDate(), droolsPdp.getPriority());
- }
- }
- }
- try{
- em.getTransaction().commit();
- }catch(Exception e){
- logger.error
- ("Cannot commit getDroolsPdps() transaction", e);
- }
- return droolsPdpsReturnList;
- } finally {
- cleanup(em, "getDroolsPdps");
- }
- }
-
- private boolean nullSafeEquals(Object one, Object two){
- if(one == null && two == null){
- return true;
- }
- if(one != null && two != null){
- return one.equals(two);
- }
- return false;
- }
-
- @Override
- public void update(DroolsPdp pdp) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("update: Entering, pdpId={}", pdp.getPdpId());
- }
-
- //this is to update our own pdp in the database
- EntityManager em = emf.createEntityManager();
- try {
- em.getTransaction().begin();
- Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
- droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- DroolsPdpEntity droolsPdpEntity;
- if(droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)){
- droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
- em.refresh(droolsPdpEntity); //Make sure we have current values
- Date currentDate = new Date();
- long difference = currentDate.getTime()-droolsPdpEntity.getUpdatedDate().getTime();
- //just set some kind of default here
- long pdpTimeout = 15000;
- try{
- pdpTimeout = Long.parseLong(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
- }catch(Exception e){
- logger.error
- ("Could not get PDP timeout property, using default.", e);
- }
- boolean isCurrent = difference<pdpTimeout;
- if (logger.isDebugEnabled()) {
- logger.debug("update: PDP= {}, isCurrent={}"
- + " difference= {}"
- + ", pdpTimeout= {}, designated= {}",
- pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated());
- }
- } else {
- if (logger.isDebugEnabled()) {
- logger.debug("update: For PDP={}"
- + ", instantiating new DroolsPdpEntity", pdp.getPdpId());
- }
- droolsPdpEntity = new DroolsPdpEntity();
- em.persist(droolsPdpEntity);
- droolsPdpEntity.setPdpId(pdp.getPdpId());
- }
- if(droolsPdpEntity.getPriority() != pdp.getPriority()){
- droolsPdpEntity.setPriority(pdp.getPriority());
- }
- if(!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())){
- droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
- }
- /*if(!droolsPdpEntity.getDesignatedDate().equals(pdp.getDesignatedDate())){
- droolsPdpEntity.setDesignatedDate(pdp.getDesignatedDate());
- } The designated date is only set below when this first becomes designated*/
- if(!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())){
- droolsPdpEntity.setSiteName(pdp.getSiteName());
- }
-
- if(droolsPdpEntity.isDesignated() != pdp.isDesignated()){
- if (logger.isDebugEnabled()) {
- logger.debug("update: pdpId={}"
- + ", pdp.isDesignated={}"
- + ", droolsPdpEntity.pdpId= {}"
- + ", droolsPdpEntity.isDesignated={}",
- pdp.getPdpId(), pdp.isDesignated(),droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated());
- }
- droolsPdpEntity.setDesignated(pdp.isDesignated());
- //The isDesignated value is not the same and the new one == true
- if(pdp.isDesignated()){
- droolsPdpEntity.setDesignatedDate(new Date());
- }
- }
- em.getTransaction().commit();
- } finally {
- cleanup(em, "update");
- }
-
- if (logger.isDebugEnabled()) {
- logger.debug("update: Exiting");
- }
-
- }
-
- /*
- * Note: A side effect of this boolean method is that if the PDP is designated but not current, the
- * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as
- * being in standby mode)
- */
- @Override
- public boolean isPdpCurrent(DroolsPdp pdp) {
-
- boolean isCurrent = isCurrent(pdp);
-
- EntityManager em = emf.createEntityManager();
- try{
- if(!isCurrent && pdp.isDesignated()){
- em.getTransaction().begin();
- Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
- droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- if(droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity){
- if (logger.isDebugEnabled()) {
- logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false", pdp.getPdpId());
- }
- DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
- droolsPdpEntity.setDesignated(false);
- em.getTransaction().commit();
- } else {
- logger.warn("isPdpCurrent: PDP={} is designated but not current; "
- + "however it does not have a DB entry, so cannot set DESIGNATED to false!", pdp.getPdpId());
- }
- } else {
- if (logger.isDebugEnabled()) {
- logger.debug("isPdpCurrent: For PDP= {}, "
- + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent);
- }
- }
- }catch(Exception e){
- logger.error
- ("Could not update expired record marked as designated in the database", e);
- } finally {
- cleanup(em, "isPdpCurrent");
- }
- return isCurrent;
-
- }
-
- @Override
- public void setDesignated(DroolsPdp pdp, boolean designated) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("setDesignated: Entering, pdpId={}"
- + ", designated={}", pdp.getPdpId(), designated);
- }
-
- EntityManager em = null;
- try {
- em = emf.createEntityManager();
- em.getTransaction().begin();
- Query droolsPdpsListQuery = em
- .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
- droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
- LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- if (droolsPdpsList.size() == 1
- && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
- DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList
- .get(0);
-
- if (logger.isDebugEnabled()) {
- logger.debug("setDesignated: PDP={}"
- + " found, designated= {}"
- + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(),
- designated);
- }
- droolsPdpEntity.setDesignated(designated);
- if(designated){
- em.refresh(droolsPdpEntity); //make sure we get the DB value
- if(!droolsPdpEntity.isDesignated()){
- droolsPdpEntity.setDesignatedDate(new Date());
- }
-
- }
- em.getTransaction().commit();
- } else {
- logger.error("setDesignated: PDP={}"
- + " not in DB; cannot update designation", pdp.getPdpId());
- }
- } catch (Exception e) {
- logger.error("setDesignated: Caught Exception", e);
- } finally {
- cleanup(em, "setDesignated");
- }
-
- if (logger.isDebugEnabled()) {
- logger.debug("setDesignated: Exiting");
- }
-
- }
-
-
- @Override
- public void standDownPdp(String pdpId) {
- if(logger.isDebugEnabled()){
- logger.debug("standDownPdp: Entering, pdpId={}", pdpId);
- }
-
- EntityManager em = null;
- try {
- /*
- * Start transaction.
- */
- em = emf.createEntityManager();
- em.getTransaction().begin();
-
- /*
- * Get droolspdpentity record for this PDP and mark DESIGNATED as
- * false.
- */
- Query droolsPdpsListQuery = em
- .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
- droolsPdpsListQuery.setParameter("pdpId", pdpId);
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
- LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- DroolsPdpEntity droolsPdpEntity;
- if (droolsPdpsList.size() == 1
- && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
- droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
- droolsPdpEntity.setDesignated(false);
- em.persist(droolsPdpEntity);
- if(logger.isDebugEnabled()){
- logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId );
- }
- } else {
- logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}"
- + "; cannot stand down PDP", pdpId);
- }
-
- /*
- * End transaction.
- */
- em.getTransaction().commit();
- cleanup(em, "standDownPdp");
- em = null;
-
- // Keep the election handler in sync with the DB
- DroolsPdpsElectionHandler.setMyPdpDesignated(false);
-
- } catch (Exception e) {
- logger.error("standDownPdp: Unexpected Exception attempting to mark "
- + "DESIGNATED as false for droolspdpentity, pdpId={}"
- + ". Cannot stand down PDP; message={}", pdpId, e.getMessage(), e);
- } finally {
- cleanup(em, "standDownPdp");
- }
- if(logger.isDebugEnabled()){
- logger.debug("standDownPdp: Exiting");
- }
-
- }
-
- /*
- * Determines whether or not a designated PDP has failed.
- *
- * Note: The update method, which is run periodically by the
- * TimerUpdateClass, will un-designate a PDP that is stale.
- */
- @Override
- public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size());
- }
-
- boolean failed = true;
- boolean foundDesignatedPdp = false;
-
- for (DroolsPdp pdp : pdps) {
-
- /*
- * Normally, the update method will un-designate any stale PDP, but
- * we check here to see if the PDP has gone stale since the update
- * method was run.
- *
- * Even if we determine that the designated PDP is current, we keep
- * going (we don't break), so we can get visibility into the other
- * PDPs, when in DEBUG mode.
- */
- if (pdp.isDesignated() && isCurrent(pdp)) {
- if (logger.isDebugEnabled()) {
- logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId());
- }
- failed = false;
- foundDesignatedPdp = true;
- } else if (pdp.isDesignated() && !isCurrent(pdp)) {
- logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId());
- foundDesignatedPdp = true;
- } else {
- if (logger.isDebugEnabled()) {
- logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId());
- }
- }
- }
-
- if (logger.isDebugEnabled()) {
- logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}",
- foundDesignatedPdp);
- }
- return failed;
- }
-
-
- private boolean isCurrent(DroolsPdp pdp) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId());
- }
-
- boolean current = false;
-
- // Return if the current PDP is considered "current" based on whatever
- // time box that may be.
- // If the the PDP is not current, we should mark it as not primary in
- // the database
- Date currentDate = new Date();
- long difference = currentDate.getTime()
- - pdp.getUpdatedDate().getTime();
- // just set some kind of default here
- long pdpTimeout = 15000;
- try {
- pdpTimeout = Long.parseLong(ActiveStandbyProperties
- .getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
- if (logger.isDebugEnabled()) {
- logger.debug("isCurrent: pdp.timeout={}", pdpTimeout);
- }
- } catch (Exception e) {
- logger.error
- ("isCurrent: Could not get PDP timeout property, using default.", e);
- }
- current = difference < pdpTimeout;
-
- if (logger.isDebugEnabled()) {
- logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}"
- + "; returning current={}", difference, pdpTimeout, current);
- }
-
- return current;
- }
-
-
- /*
- * Currently this method is only used in a JUnit test environment. Gets a
- * PDP record from droolspdpentity table.
- */
- @Override
- public DroolsPdpEntity getPdp(String pdpId) {
-
- if (logger.isDebugEnabled()) {
- logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId);
- }
-
- DroolsPdpEntity droolsPdpEntity = null;
-
- EntityManager em = null;
- try {
- em = emf.createEntityManager();
- em.getTransaction().begin();
- Query droolsPdpsListQuery = em
- .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
- droolsPdpsListQuery.setParameter("pdpId", pdpId);
- List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
- LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- if (droolsPdpsList.size() == 1
- && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
- droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
- if (logger.isDebugEnabled()) {
- logger.debug("getPdp: PDP={}"
- + " found, isDesignated={},"
- + " updatedDate={}, "
- + "priority={}", pdpId,
- droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(),
- droolsPdpEntity.getPriority());
- }
-
- // Make sure the droolsPdpEntity is not a cached version
- em.refresh(droolsPdpEntity);
-
- em.getTransaction().commit();
- } else {
- logger.error("getPdp: PDP={} not found!?", pdpId);
- }
- } catch (Exception e) {
- logger.error
- ("getPdp: Caught Exception attempting to get PDP", e);
- } finally {
- cleanup(em, "getPdp");
- }
-
- if (logger.isDebugEnabled()) {
- logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity);
- }
- return droolsPdpEntity;
-
- }
-
- /*
- * Normally this method should only be used in a JUnit test environment.
- * Manually inserts a PDP record in droolspdpentity table.
- */
- @Override
- public void insertPdp(DroolsPdp pdp) {
- if(logger.isDebugEnabled()){
- logger.debug("insertPdp: Entering and manually inserting PDP");
- }
-
- /*
- * Start transaction
- */
- EntityManager em = emf.createEntityManager();
- try {
- em.getTransaction().begin();
-
- /*
- * Insert record.
- */
- DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity();
- em.persist(droolsPdpEntity);
- droolsPdpEntity.setPdpId(pdp.getPdpId());
- droolsPdpEntity.setDesignated(pdp.isDesignated());
- droolsPdpEntity.setPriority(pdp.getPriority());
- droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
- droolsPdpEntity.setSiteName(pdp.getSiteName());
-
- /*
- * End transaction.
- */
- em.getTransaction().commit();
- } finally {
- cleanup(em, "insertPdp");
- }
- if(logger.isDebugEnabled()){
- logger.debug("insertPdp: Exiting");
- }
-
- }
-
- /*
- * Normally this method should only be used in a JUnit test environment.
- * Manually deletes all PDP records in droolspdpentity table.
- */
- @Override
- public void deleteAllPdps() {
-
- if(logger.isDebugEnabled()){
- logger.debug("deleteAllPdps: Entering");
- }
-
- /*
- * Start transaction
- */
- EntityManager em = emf.createEntityManager();
- try {
- em.getTransaction().begin();
-
- Query droolsPdpsListQuery = em
- .createQuery("SELECT p FROM DroolsPdpEntity p");
- @SuppressWarnings("unchecked")
- List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode(
- LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
- if(logger.isDebugEnabled()){
- logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size());
- }
- for (DroolsPdp droolsPdp : droolsPdpsList) {
- String pdpId = droolsPdp.getPdpId();
- deletePdp(pdpId);
- }
-
- /*
- * End transaction.
- */
- em.getTransaction().commit();
- } finally {
- cleanup(em, "deleteAllPdps");
- }
- if(logger.isDebugEnabled()){
- logger.debug("deleteAllPdps: Exiting");
- }
-
- }
-
- /*
- * Normally this method should only be used in a JUnit test environment.
- * Manually deletes a PDP record in droolspdpentity table.
- */
- @Override
- public void deletePdp(String pdpId) {
- if(logger.isDebugEnabled()){
- logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId);
- }
-
- /*
- * Start transaction
- */
- EntityManager em = emf.createEntityManager();
- try {
- em.getTransaction().begin();
-
- /*
- * Delete record.
- */
- DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId);
- if (droolsPdpEntity != null) {
- if(logger.isDebugEnabled()){
- logger.debug("deletePdp: Removing PDP");
- }
- em.remove(droolsPdpEntity);
- } else {
- if(logger.isDebugEnabled()){
- logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId);
- }
- }
-
- /*
- * End transaction.
- */
- em.getTransaction().commit();
- } finally {
- cleanup(em, "deletePdp");
- }
- if(logger.isDebugEnabled()){
- logger.debug("deletePdp: Exiting");
- }
-
- }
-
- /*
- * Close the specified EntityManager, rolling back any pending transaction
- *
- * @param em the EntityManager to close ('null' is OK)
- * @param method the invoking Java method (used for log messages)
- */
- private static void cleanup(EntityManager em, String method)
- {
- if (em != null && em.isOpen()) {
- if (em.getTransaction().isActive()) {
- // there is an active EntityTransaction -- roll it back
- try {
- em.getTransaction().rollback();
- } catch (Exception e) {
- logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e);
- }
- }
-
- // now, close the EntityManager
- try {
- em.close();
- } catch (Exception e) {
- logger.error(method + ": Caught Exception attempting to close EntityManager, ", e);
- }
- }
- }
+ // get an instance of logger
+ private static final Logger logger = LoggerFactory.getLogger(JpaDroolsPdpsConnector.class);
+ private EntityManagerFactory emf;
+
+
+ //not sure if we want to use the same entity manager factory
+ //for drools session and pass it in here, or create a new one
+ public JpaDroolsPdpsConnector(EntityManagerFactory emf) {
+ this.emf = emf;
+ }
+
+ @Override
+ public Collection<DroolsPdp> getDroolsPdps() {
+ //return a list of all the DroolsPdps in the database
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.getTransaction().begin();
+ Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p");
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
+ .setFlushMode(FlushModeType.COMMIT).getResultList();
+ LinkedList<DroolsPdp> droolsPdpsReturnList = new LinkedList<>();
+ for (Object o : droolsPdpsList) {
+ if (o instanceof DroolsPdp) {
+ //Make sure it is not a cached version
+ em.refresh((DroolsPdpEntity)o);
+ droolsPdpsReturnList.add((DroolsPdp)o);
+ if (logger.isDebugEnabled()) {
+ DroolsPdp droolsPdp = (DroolsPdp)o;
+ logger.debug("getDroolsPdps: PDP= {}"
+ + ", isDesignated= {}"
+ + ", updatedDate= {}"
+ + ", priority= {}", droolsPdp.getPdpId(), droolsPdp.isDesignated(),
+ droolsPdp.getUpdatedDate(), droolsPdp.getPriority());
+ }
+ }
+ }
+ try {
+ em.getTransaction().commit();
+ } catch (Exception e) {
+ logger.error("Cannot commit getDroolsPdps() transaction", e);
+ }
+ return droolsPdpsReturnList;
+ } finally {
+ cleanup(em, "getDroolsPdps");
+ }
+ }
+
+ private boolean nullSafeEquals(Object one, Object two) {
+ if (one == null && two == null) {
+ return true;
+ }
+ if (one != null && two != null) {
+ return one.equals(two);
+ }
+ return false;
+ }
+
+ @Override
+ public void update(DroolsPdp pdp) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("update: Entering, pdpId={}", pdp.getPdpId());
+ }
+
+ //this is to update our own pdp in the database
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.getTransaction().begin();
+ Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
+ droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
+ .setFlushMode(FlushModeType.COMMIT).getResultList();
+ DroolsPdpEntity droolsPdpEntity;
+ if (droolsPdpsList.size() == 1 && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
+ droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
+ em.refresh(droolsPdpEntity); //Make sure we have current values
+ Date currentDate = new Date();
+ long difference = currentDate.getTime() - droolsPdpEntity.getUpdatedDate().getTime();
+ //just set some kind of default here
+ long pdpTimeout = 15000;
+ try {
+ pdpTimeout = Long.parseLong(
+ ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
+ } catch (Exception e) {
+ logger.error("Could not get PDP timeout property, using default.", e);
+ }
+ boolean isCurrent = difference < pdpTimeout;
+ if (logger.isDebugEnabled()) {
+ logger.debug("update: PDP= {}, isCurrent={}"
+ + " difference= {}"
+ + ", pdpTimeout= {}, designated= {}",
+ pdp.getPdpId(), isCurrent, difference, pdpTimeout, droolsPdpEntity.isDesignated());
+ }
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("update: For PDP={}"
+ + ", instantiating new DroolsPdpEntity", pdp.getPdpId());
+ }
+ droolsPdpEntity = new DroolsPdpEntity();
+ em.persist(droolsPdpEntity);
+ droolsPdpEntity.setPdpId(pdp.getPdpId());
+ }
+ if (droolsPdpEntity.getPriority() != pdp.getPriority()) {
+ droolsPdpEntity.setPriority(pdp.getPriority());
+ }
+ if (!droolsPdpEntity.getUpdatedDate().equals(pdp.getUpdatedDate())) {
+ droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
+ }
+ if (!nullSafeEquals(droolsPdpEntity.getSiteName(),pdp.getSiteName())) {
+ droolsPdpEntity.setSiteName(pdp.getSiteName());
+ }
+
+ if (droolsPdpEntity.isDesignated() != pdp.isDesignated()) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("update: pdpId={}"
+ + ", pdp.isDesignated={}"
+ + ", droolsPdpEntity.pdpId= {}"
+ + ", droolsPdpEntity.isDesignated={}",
+ pdp.getPdpId(), pdp.isDesignated(),
+ droolsPdpEntity.getPdpId(), droolsPdpEntity.isDesignated());
+ }
+ droolsPdpEntity.setDesignated(pdp.isDesignated());
+ //The isDesignated value is not the same and the new one == true
+ if (pdp.isDesignated()) {
+ droolsPdpEntity.setDesignatedDate(new Date());
+ }
+ }
+ em.getTransaction().commit();
+ } finally {
+ cleanup(em, "update");
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("update: Exiting");
+ }
+
+ }
+
+ /*
+ * Note: A side effect of this boolean method is that if the PDP is designated but not current, the
+ * droolspdpentity.DESIGNATED column will be set to false (the PDP will be un-designated, i.e. marked as
+ * being in standby mode)
+ */
+ @Override
+ public boolean isPdpCurrent(DroolsPdp pdp) {
+
+ boolean isCurrent = isCurrent(pdp);
+
+ EntityManager em = emf.createEntityManager();
+ try {
+ if (!isCurrent && pdp.isDesignated()) {
+ em.getTransaction().begin();
+ Query droolsPdpsListQuery = em.createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
+ droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(LockModeType.NONE)
+ .setFlushMode(FlushModeType.COMMIT).getResultList();
+ if (droolsPdpsList.size() == 1 && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("isPdpCurrent: PDP={} designated but not current; setting designated to false",
+ pdp.getPdpId());
+ }
+ DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity)droolsPdpsList.get(0);
+ droolsPdpEntity.setDesignated(false);
+ em.getTransaction().commit();
+ } else {
+ logger.warn("isPdpCurrent: PDP={} is designated but not current; "
+ + "however it does not have a DB entry, so cannot set DESIGNATED to false!",
+ pdp.getPdpId());
+ }
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("isPdpCurrent: For PDP= {}, "
+ + "designated={}, isCurrent={}", pdp.getPdpId(), pdp.isDesignated(), isCurrent);
+ }
+ }
+ } catch (Exception e) {
+ logger.error("Could not update expired record marked as designated in the database", e);
+ } finally {
+ cleanup(em, "isPdpCurrent");
+ }
+ return isCurrent;
+
+ }
+
+ @Override
+ public void setDesignated(DroolsPdp pdp, boolean designated) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("setDesignated: Entering, pdpId={}"
+ + ", designated={}", pdp.getPdpId(), designated);
+ }
+
+ EntityManager em = null;
+ try {
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ Query droolsPdpsListQuery = em
+ .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
+ droolsPdpsListQuery.setParameter("pdpId", pdp.getPdpId());
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
+ LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
+ if (droolsPdpsList.size() == 1
+ && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
+ DroolsPdpEntity droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList
+ .get(0);
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("setDesignated: PDP={}"
+ + " found, designated= {}"
+ + ", setting to {}", pdp.getPdpId(), droolsPdpEntity.isDesignated(),
+ designated);
+ }
+ droolsPdpEntity.setDesignated(designated);
+ if (designated) {
+ em.refresh(droolsPdpEntity); //make sure we get the DB value
+ if (!droolsPdpEntity.isDesignated()) {
+ droolsPdpEntity.setDesignatedDate(new Date());
+ }
+
+ }
+ em.getTransaction().commit();
+ } else {
+ logger.error("setDesignated: PDP={}"
+ + " not in DB; cannot update designation", pdp.getPdpId());
+ }
+ } catch (Exception e) {
+ logger.error("setDesignated: Caught Exception", e);
+ } finally {
+ cleanup(em, "setDesignated");
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("setDesignated: Exiting");
+ }
+
+ }
+
+
+ @Override
+ public void standDownPdp(String pdpId) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("standDownPdp: Entering, pdpId={}", pdpId);
+ }
+
+ EntityManager em = null;
+ try {
+ /*
+ * Start transaction.
+ */
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+
+ /*
+ * Get droolspdpentity record for this PDP and mark DESIGNATED as
+ * false.
+ */
+ Query droolsPdpsListQuery = em
+ .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
+ droolsPdpsListQuery.setParameter("pdpId", pdpId);
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
+ LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
+ DroolsPdpEntity droolsPdpEntity;
+ if (droolsPdpsList.size() == 1
+ && (droolsPdpsList.get(0) instanceof DroolsPdpEntity)) {
+ droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
+ droolsPdpEntity.setDesignated(false);
+ em.persist(droolsPdpEntity);
+ if (logger.isDebugEnabled()) {
+ logger.debug("standDownPdp: PDP={} persisted as non-designated.", pdpId );
+ }
+ } else {
+ logger.error("standDownPdp: Missing record in droolspdpentity for pdpId={}"
+ + "; cannot stand down PDP", pdpId);
+ }
+
+ /*
+ * End transaction.
+ */
+ em.getTransaction().commit();
+ cleanup(em, "standDownPdp");
+ em = null;
+
+ // Keep the election handler in sync with the DB
+ DroolsPdpsElectionHandler.setMyPdpDesignated(false);
+
+ } catch (Exception e) {
+ logger.error("standDownPdp: Unexpected Exception attempting to mark "
+ + "DESIGNATED as false for droolspdpentity, pdpId={}"
+ + ". Cannot stand down PDP; message={}", pdpId, e.getMessage(), e);
+ } finally {
+ cleanup(em, "standDownPdp");
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("standDownPdp: Exiting");
+ }
+
+ }
+
+ /*
+ * Determines whether or not a designated PDP has failed.
+ *
+ * Note: The update method, which is run periodically by the
+ * TimerUpdateClass, will un-designate a PDP that is stale.
+ */
+ @Override
+ public boolean hasDesignatedPdpFailed(Collection<DroolsPdp> pdps) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("hasDesignatedPdpFailed: Entering, pdps.size()={}", pdps.size());
+ }
+
+ boolean failed = true;
+ boolean foundDesignatedPdp = false;
+
+ for (DroolsPdp pdp : pdps) {
+
+ /*
+ * Normally, the update method will un-designate any stale PDP, but
+ * we check here to see if the PDP has gone stale since the update
+ * method was run.
+ *
+ * Even if we determine that the designated PDP is current, we keep
+ * going (we don't break), so we can get visibility into the other
+ * PDPs, when in DEBUG mode.
+ */
+ if (pdp.isDesignated() && isCurrent(pdp)) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("hasDesignatedPdpFailed: Designated PDP={} is current", pdp.getPdpId());
+ }
+ failed = false;
+ foundDesignatedPdp = true;
+ } else if (pdp.isDesignated() && !isCurrent(pdp)) {
+ logger.error("hasDesignatedPdpFailed: Designated PDP={} has failed", pdp.getPdpId());
+ foundDesignatedPdp = true;
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("hasDesignatedPdpFailed: PDP={} is not designated", pdp.getPdpId());
+ }
+ }
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("hasDesignatedPdpFailed: Exiting and returning, foundDesignatedPdp={}",
+ foundDesignatedPdp);
+ }
+ return failed;
+ }
+
+
+ private boolean isCurrent(DroolsPdp pdp) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("isCurrent: Entering, pdpId={}", pdp.getPdpId());
+ }
+
+ boolean current = false;
+
+ // Return if the current PDP is considered "current" based on whatever
+ // time box that may be.
+ // If the the PDP is not current, we should mark it as not primary in
+ // the database
+ Date currentDate = new Date();
+ long difference = currentDate.getTime()
+ - pdp.getUpdatedDate().getTime();
+ // just set some kind of default here
+ long pdpTimeout = 15000;
+ try {
+ pdpTimeout = Long.parseLong(ActiveStandbyProperties
+ .getProperty(ActiveStandbyProperties.PDP_TIMEOUT));
+ if (logger.isDebugEnabled()) {
+ logger.debug("isCurrent: pdp.timeout={}", pdpTimeout);
+ }
+ } catch (Exception e) {
+ logger.error("isCurrent: Could not get PDP timeout property, using default.", e);
+ }
+ current = difference < pdpTimeout;
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("isCurrent: Exiting, difference={}, pdpTimeout={}"
+ + "; returning current={}", difference, pdpTimeout, current);
+ }
+
+ return current;
+ }
+
+
+ /*
+ * Currently this method is only used in a JUnit test environment. Gets a
+ * PDP record from droolspdpentity table.
+ */
+ @Override
+ public DroolsPdpEntity getPdp(String pdpId) {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("getPdp: Entering and getting PDP with pdpId={}", pdpId);
+ }
+
+ DroolsPdpEntity droolsPdpEntity = null;
+
+ EntityManager em = null;
+ try {
+ em = emf.createEntityManager();
+ em.getTransaction().begin();
+ Query droolsPdpsListQuery = em
+ .createQuery("SELECT p FROM DroolsPdpEntity p WHERE p.pdpId=:pdpId");
+ droolsPdpsListQuery.setParameter("pdpId", pdpId);
+ List<?> droolsPdpsList = droolsPdpsListQuery.setLockMode(
+ LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
+ if (droolsPdpsList.size() == 1
+ && droolsPdpsList.get(0) instanceof DroolsPdpEntity) {
+ droolsPdpEntity = (DroolsPdpEntity) droolsPdpsList.get(0);
+ if (logger.isDebugEnabled()) {
+ logger.debug("getPdp: PDP={}"
+ + " found, isDesignated={},"
+ + " updatedDate={}, "
+ + "priority={}", pdpId,
+ droolsPdpEntity.isDesignated(), droolsPdpEntity.getUpdatedDate(),
+ droolsPdpEntity.getPriority());
+ }
+
+ // Make sure the droolsPdpEntity is not a cached version
+ em.refresh(droolsPdpEntity);
+
+ em.getTransaction().commit();
+ } else {
+ logger.error("getPdp: PDP={} not found!?", pdpId);
+ }
+ } catch (Exception e) {
+ logger.error("getPdp: Caught Exception attempting to get PDP", e);
+ } finally {
+ cleanup(em, "getPdp");
+ }
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("getPdp: Returning droolsPdpEntity={}", droolsPdpEntity);
+ }
+ return droolsPdpEntity;
+
+ }
+
+ /*
+ * Normally this method should only be used in a JUnit test environment.
+ * Manually inserts a PDP record in droolspdpentity table.
+ */
+ @Override
+ public void insertPdp(DroolsPdp pdp) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("insertPdp: Entering and manually inserting PDP");
+ }
+
+ /*
+ * Start transaction
+ */
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.getTransaction().begin();
+
+ /*
+ * Insert record.
+ */
+ DroolsPdpEntity droolsPdpEntity = new DroolsPdpEntity();
+ em.persist(droolsPdpEntity);
+ droolsPdpEntity.setPdpId(pdp.getPdpId());
+ droolsPdpEntity.setDesignated(pdp.isDesignated());
+ droolsPdpEntity.setPriority(pdp.getPriority());
+ droolsPdpEntity.setUpdatedDate(pdp.getUpdatedDate());
+ droolsPdpEntity.setSiteName(pdp.getSiteName());
+
+ /*
+ * End transaction.
+ */
+ em.getTransaction().commit();
+ } finally {
+ cleanup(em, "insertPdp");
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("insertPdp: Exiting");
+ }
+
+ }
+
+ /*
+ * Normally this method should only be used in a JUnit test environment.
+ * Manually deletes all PDP records in droolspdpentity table.
+ */
+ @Override
+ public void deleteAllPdps() {
+
+ if (logger.isDebugEnabled()) {
+ logger.debug("deleteAllPdps: Entering");
+ }
+
+ /*
+ * Start transaction
+ */
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.getTransaction().begin();
+
+ Query droolsPdpsListQuery = em
+ .createQuery("SELECT p FROM DroolsPdpEntity p");
+ @SuppressWarnings("unchecked")
+ List<DroolsPdp> droolsPdpsList = droolsPdpsListQuery.setLockMode(
+ LockModeType.NONE).setFlushMode(FlushModeType.COMMIT).getResultList();
+ if (logger.isDebugEnabled()) {
+ logger.debug("deleteAllPdps: Deleting {} PDPs", droolsPdpsList.size());
+ }
+ for (DroolsPdp droolsPdp : droolsPdpsList) {
+ String pdpId = droolsPdp.getPdpId();
+ deletePdp(pdpId);
+ }
+
+ /*
+ * End transaction.
+ */
+ em.getTransaction().commit();
+ } finally {
+ cleanup(em, "deleteAllPdps");
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("deleteAllPdps: Exiting");
+ }
+
+ }
+
+ /*
+ * Normally this method should only be used in a JUnit test environment.
+ * Manually deletes a PDP record in droolspdpentity table.
+ */
+ @Override
+ public void deletePdp(String pdpId) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("deletePdp: Entering and manually deleting pdpId={}", pdpId);
+ }
+
+ /*
+ * Start transaction
+ */
+ EntityManager em = emf.createEntityManager();
+ try {
+ em.getTransaction().begin();
+
+ /*
+ * Delete record.
+ */
+ DroolsPdpEntity droolsPdpEntity = em.find(DroolsPdpEntity.class, pdpId);
+ if (droolsPdpEntity != null) {
+ if (logger.isDebugEnabled()) {
+ logger.debug("deletePdp: Removing PDP");
+ }
+ em.remove(droolsPdpEntity);
+ } else {
+ if (logger.isDebugEnabled()) {
+ logger.debug("deletePdp: PDP with ID={} not currently in DB", pdpId);
+ }
+ }
+
+ /*
+ * End transaction.
+ */
+ em.getTransaction().commit();
+ } finally {
+ cleanup(em, "deletePdp");
+ }
+ if (logger.isDebugEnabled()) {
+ logger.debug("deletePdp: Exiting");
+ }
+
+ }
+
+ /*
+ * Close the specified EntityManager, rolling back any pending transaction
+ *
+ * @param em the EntityManager to close ('null' is OK)
+ * @param method the invoking Java method (used for log messages)
+ */
+ private static void cleanup(EntityManager em, String method) {
+ if (em != null && em.isOpen()) {
+ if (em.getTransaction().isActive()) {
+ // there is an active EntityTransaction -- roll it back
+ try {
+ em.getTransaction().rollback();
+ } catch (Exception e) {
+ logger.error(method + ": Caught Exception attempting to rollback EntityTransaction,", e);
+ }
+ }
+
+ // now, close the EntityManager
+ try {
+ em.close();
+ } catch (Exception e) {
+ logger.error(method + ": Caught Exception attempting to close EntityManager, ", e);
+ }
+ }
+ }
}
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java
index 805c4b80..554376ce 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java
@@ -51,24 +51,32 @@ import org.slf4j.LoggerFactory;
/*
* Some background:
+ *
+ * Originally, there was a "StandbyStateChangeNotifier" that belonged to policy-core, and this class's
+ * handleStateChange() method used to take care of invoking conn.standDownPdp().
*
- * Originally, there was a "StandbyStateChangeNotifier" that belonged to policy-core, and this class's handleStateChange() method
- * used to take care of invoking conn.standDownPdp(). But testing revealed that when a state change to hot standby occurred
- * from a demote() operation, first the PMStandbyStateChangeNotifier.handleStateChange() method would be invoked and then the
- * StandbyStateChangeNotifier.handleStateChange() method would be invoked, and this ordering was creating the following problem:
+ * But testing revealed that when a state change to hot standby
+ * occurred from a demote() operation, first the PMStandbyStateChangeNotifier.handleStateChange() method
+ * would be invoked and then the StandbyStateChangeNotifier.handleStateChange() method would be invoked,
+ * and this ordering was creating the following problem:
+ *
+ * When PMStandbyStateChangeNotifier.handleStateChange() was invoked it would take a long time to finish,
+ * because it would result in SingleThreadedUebTopicSource.stop() being invoked, which can potentially do a
+ * 5 second sleep for each controller being stopped.
*
- * When PMStandbyStateChangeNotifier.handleStateChange() was invoked it would take a long time to finish, because it would result
- * in SingleThreadedUebTopicSource.stop() being invoked, which can potentially do a 5 second sleep for each controller being stopped.
- * Meanwhile, as these controller stoppages and their associated sleeps were occurring, the election handler would discover the
- * demoted PDP in hotstandby (but still designated!) and promote it, resulting in the standbyStatus going from hotstandby
- * to providingservice. So then, by the time that PMStandbyStateChangeNotifier.handleStateChange() finished its work and
- * StandbyStateChangeNotifier.handleStateChange() started executing, the standbyStatus was no longer hotstandby (as effected by
- * the demote), but providingservice (as reset by the election handling logic) and conn.standDownPdp() would not get called!
- *
- * To fix this bug, we consolidated StandbyStateChangeNotifier and PMStandbyStateChangeNotifier, with the standDownPdp() always
- * being invoked prior to the TopicEndpoint.manager.lock(). In this way, when the election handling logic is invoked
+ * Meanwhile, as these controller stoppages and their associated sleeps were occurring, the election handler
+ * would discover the demoted PDP in hotstandby (but still designated!) and promote it, resulting in the
+ * standbyStatus going from hotstandby to providingservice. So then, by the time that
+ * PMStandbyStateChangeNotifier.handleStateChange() finished its work and
+ * StandbyStateChangeNotifier.handleStateChange() started executing, the standbyStatus was no longer hotstandby
+ * (as effected by the demote), but providingservice (as reset by the election handling logic) and
+ * conn.standDownPdp() would not get called!
+ *
+ * To fix this bug, we consolidated StandbyStateChangeNotifier and PMStandbyStateChangeNotifier,
+ * with the standDownPdp() always
+ * being invoked prior to the TopicEndpoint.manager.lock(). In this way, when the election handling logic is invoked
* during the controller stoppages, the PDP is in hotstandby and the standdown occurs.
- *
+ *
*/
public class PMStandbyStateChangeNotifier extends StateChangeNotifier {
// get an instance of logger
@@ -84,6 +92,10 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier {
public static final String UNSUPPORTED = "unsupported";
public static final String HOTSTANDBY_OR_COLDSTANDBY = "hotstandby_or_coldstandby";
+ /**
+ * Constructor.
+ *
+ */
public PMStandbyStateChangeNotifier() {
pdpUpdateInterval =
Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL));
@@ -122,8 +134,9 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier {
// We were just here and did this successfully
if (logger.isDebugEnabled()) {
logger.debug(
- "handleStateChange: Is returning because standbyStatus is null and was previously 'null'; PDP={}",
- pdpId);
+ "handleStateChange: "
+ + "Is returning because standbyStatus is null and was previously 'null'; PDP={}",
+ pdpId);
}
return;
}
@@ -230,16 +243,16 @@ public class PMStandbyStateChangeNotifier extends StateChangeNotifier {
if (waitTimeMs > 3 * waitInterval) {
if (logger.isDebugEnabled()) {
logger.debug(
- "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer may be hung,"
- + " waitTimeMs = {} and allowable waitInterval = {}"
- + " Checking whether it is currently in activation. isNowActivating = {}",
- waitTimeMs, waitInterval, isNowActivating);
+ "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer may be hung,"
+ + " waitTimeMs = {} and allowable waitInterval = {}"
+ + " Checking whether it is currently in activation. isNowActivating = {}",
+ waitTimeMs, waitInterval, isNowActivating);
}
// Now check that it is not currently executing an activation
if (!isNowActivating) {
if (logger.isDebugEnabled()) {
logger.debug(
- "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer died");
+ "handleStateChange: PROVIDING_SERVICE looks like the activation wait timer died");
}
// This will assure the timer is cancelled and rescheduled.
isWaitingForActivation = false;
diff --git a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java
index b5fe3071..373c373c 100644
--- a/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java
+++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java
@@ -22,6 +22,6 @@ package org.onap.policy.drools.activestandby;
@FunctionalInterface
public interface ThreadRunningChecker {
- public void checkThreadStatus();
+ public void checkThreadStatus();
}
diff --git a/feature-active-standby-management/src/main/resources/META-INF/persistence.xml b/feature-active-standby-management/src/main/resources/META-INF/persistence.xml
index 4a625b55..36f65ad8 100644
--- a/feature-active-standby-management/src/main/resources/META-INF/persistence.xml
+++ b/feature-active-standby-management/src/main/resources/META-INF/persistence.xml
@@ -3,7 +3,7 @@
============LICENSE_START=======================================================
feature-active-standby-management
================================================================================
- Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -20,14 +20,16 @@
-->
<persistence version="2.1"
- xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="activeStandbyPU" transaction-type="RESOURCE_LOCAL">
- <!-- This is for database access by non-drools methods -->
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class>
- <properties>
- <!-- Properties are passed in -->
+ xmlns="http://xmlns.jcp.org/xml/ns/persistence"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
+ <persistence-unit name="activeStandbyPU"
+ transaction-type="RESOURCE_LOCAL">
+ <!-- This is for database access by non-drools methods -->
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+ <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class>
+ <properties>
+ <!-- Properties are passed in -->
</properties>
- </persistence-unit>
+ </persistence-unit>
</persistence>
diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java
index 19265206..dfc69304 100644
--- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java
+++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/AllSeemsWellTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,265 +55,280 @@ import org.slf4j.LoggerFactory;
* Testing the allSeemsWell interface to verify that it correctly affects the
* operational state.
*/
-
+
public class AllSeemsWellTest {
- private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class);
- /*
- * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
- * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
- * to ensure that we wait for the DesignationWaiter to do its job, before
- * checking the results. Add a few seconds for safety
- */
-
- long sleepTime = 10000;
-
- /*
- * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the
- * checkWaitTimer() method to time out and call allSeemsWell which then requires
- * the forward progress counter to go stale which should add an additional 5 sec.
- */
-
- long stalledElectionHandlerSleepTime = 15000;
-
- /*
- * As soon as the election hander successfully runs, it will resume the forward progress.
- * If the election handler runs ever 1 sec and test transaction is run every 1 sec and
- * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could
- * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety.
- */
- long resumedElectionHandlerSleepTime = 6000;
-
- private static EntityManagerFactory emfx;
- private static EntityManagerFactory emfd;
- private static EntityManager emx;
- private static EntityManager emd;
- private static EntityTransaction et;
-
- private final String configDir = "src/test/resources/asw";
-
- /*
- * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
- */
-
- @BeforeClass
- public static void setUpClass() throws Exception {
-
- String userDir = System.getProperty("user.dir");
- logger.debug("setUpClass: userDir={}", userDir);
- System.setProperty("com.sun.management.jmxremote.port", "9980");
- System.setProperty("com.sun.management.jmxremote.authenticate","false");
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
-
- @Before
- public void setUp() throws Exception {
- //Create teh data access for xaml db
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
-
- // Create an entity manager to use the DB
- emx = emfx.createEntityManager();
-
- //Create the data access for drools db
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
-
- emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
-
- // Create an entity manager to use the DB
- emd = emfd.createEntityManager();
-
- DroolsPdpsElectionHandler.setIsUnitTesting(true);
- }
-
- @After
- public void tearDown() throws Exception {
-
- }
-
- public void cleanXacmlDb(){
- et = emx.getTransaction();
-
- et.begin();
- // Make sure we leave the DB clean
- emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
- emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
- emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
- emx.flush();
- et.commit();
- }
-
- public void cleanDroolsDb(){
- et = emd.getTransaction();
-
- et.begin();
- // Make sure we leave the DB clean
- emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
- emd.flush();
- et.commit();
- }
-
-
- // Tests hot standby when there is only one PDP.
-
- //@Ignore
- @Test
- public void testAllSeemsWell() throws Exception {
-
- logger.debug("\n\ntestAllSeemsWell: Entering\n\n");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.debug("testAllSeemsWell: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.debug("testAllSeemsWell: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.debug("testAllSeemsWell: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testAllSeemsWell: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.debug("testAllSeemsWell: Cleaning up tables");
- conn.deleteAllPdps();
-
- /*
- * Insert this PDP as not designated. Initial standby state will be
- * either null or cold standby. Demoting should transit state to
- * hot standby.
- */
-
- logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId);
- Date yesterday = DateUtils.addDays(new Date(), -1);
- DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testAllSeemsWell: After insertion, PDP={} has DESIGNATED={}",
- thisPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == false);
-
- logger.debug("testAllSeemsWell: Instantiating stateManagement object");
- StateManagement sm = new StateManagement(emfXacml, "dummy");
- sm.deleteAllStateManagementEntities();
-
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI smf = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- smf = feature;
- logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", smf.getResourceName());
- break;
- }
- if(smf == null){
- logger.error("testAllSeemsWell failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testAllSeemsWell failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testAllSeemsWell failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testAllSeemsWell failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
-
- logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId);
- // demoting should cause state to transit to hotstandby
- smf.demote();
-
-
- logger.debug("testAllSeemsWell: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
- + "time to check droolspdpentity table", sleepTime);
- sleep(sleepTime);
-
-
- // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
-
- droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testAllSeemsWell: After sm.demote() invoked, DESIGNATED= {} "
- + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
- assertTrue(droolsPdpEntity.isDesignated() == true);
- String standbyStatus = smf.getStandbyStatus(thisPdpId);
- logger.debug("testAllSeemsWell: After demotion, PDP= {} "
- + "has standbyStatus= {}", thisPdpId, standbyStatus);
- assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
-
- //Now we want to stall the election handler and see the if AllSeemsWell will make the
- //standbystatus = coldstandby
-
- DroolsPdpsElectionHandler.setIsStalled(true);
-
- logger.debug("testAllSeemsWell: Sleeping {} ms, to allow checkWaitTimer to recognize "
- + "the election handler has stalled and for the testTransaction to fail to "
- + "increment forward progress and for the lack of forward progress to be recognized.",
- stalledElectionHandlerSleepTime);
-
-
- //It takes 10x the update interval (1 sec) before the watcher will declare the election handler dead
- //and that just stops forward progress counter. So, the fp monitor must then run to determine
- //if the fpc has stalled. That will take about another 5 sec.
- sleep(stalledElectionHandlerSleepTime);
-
- logger.debug("testAllSeemsWell: After isStalled=true, PDP= {} "
- + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
-
- assertTrue(smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY));
-
- //Now lets resume the election handler
- DroolsPdpsElectionHandler.setIsStalled(false);
-
- sleep(resumedElectionHandlerSleepTime);
-
- logger.debug("testAllSeemsWell: After isStalled=false, PDP= {} "
- + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
-
- assertTrue(smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
-
- //resumedElectionHandlerSleepTime = 5000;
- logger.debug("\n\ntestAllSeemsWell: Exiting\n\n");
-
- }
-
- private void sleep(long sleepms) throws InterruptedException {
- Thread.sleep(sleepms);
- }
+ private static final Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class);
+ /*
+ * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
+ * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
+ * to ensure that we wait for the DesignationWaiter to do its job, before
+ * checking the results. Add a few seconds for safety
+ */
+
+ long sleepTime = 10000;
+
+ /*
+ * DroolsPdpsElectionHandler runs every 1 seconds, so it takes 10 seconds for the
+ * checkWaitTimer() method to time out and call allSeemsWell which then requires
+ * the forward progress counter to go stale which should add an additional 5 sec.
+ */
+
+ long stalledElectionHandlerSleepTime = 15000;
+
+ /*
+ * As soon as the election hander successfully runs, it will resume the forward progress.
+ * If the election handler runs ever 1 sec and test transaction is run every 1 sec and
+ * then fpc is written every 1 sec and then the fpc is checked every 2 sec, that could
+ * take a total of 5 sec to recognize the resumption of progress. So, add 1 for safety.
+ */
+ long resumedElectionHandlerSleepTime = 6000;
+
+ private static EntityManagerFactory emfx;
+ private static EntityManagerFactory emfd;
+ private static EntityManager emx;
+ private static EntityManager emd;
+ private static EntityTransaction et;
+
+ private final String configDir = "src/test/resources/asw";
+
+ /*
+ * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
+ */
+
+ /**
+ * Setup the class.
+ *
+ * @throws Exception exception
+ */
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+
+ String userDir = System.getProperty("user.dir");
+ logger.debug("setUpClass: userDir={}", userDir);
+ System.setProperty("com.sun.management.jmxremote.port", "9980");
+ System.setProperty("com.sun.management.jmxremote.authenticate","false");
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ /**
+ * Setup.
+ *
+ * @throws Exception exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ //Create teh data access for xaml db
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
+
+ // Create an entity manager to use the DB
+ emx = emfx.createEntityManager();
+
+ //Create the data access for drools db
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+
+ emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
+
+ // Create an entity manager to use the DB
+ emd = emfd.createEntityManager();
+
+ DroolsPdpsElectionHandler.setIsUnitTesting(true);
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ }
+
+ /**
+ * Clean the xacml database.
+ */
+ public void cleanXacmlDb() {
+ et = emx.getTransaction();
+
+ et.begin();
+ // Make sure we leave the DB clean
+ emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
+ emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
+ emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
+ emx.flush();
+ et.commit();
+ }
+
+ /**
+ * Clean the drools database.
+ */
+ public void cleanDroolsDb() {
+ et = emd.getTransaction();
+
+ et.begin();
+ // Make sure we leave the DB clean
+ emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
+ emd.flush();
+ et.commit();
+ }
+
+
+ // Tests hot standby when there is only one PDP.
+
+ //@Ignore
+ @Test
+ public void testAllSeemsWell() throws Exception {
+
+ logger.debug("\n\ntestAllSeemsWell: Entering\n\n");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.debug("testAllSeemsWell: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.debug("testAllSeemsWell: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.debug("testAllSeemsWell: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testAllSeemsWell: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.debug("testAllSeemsWell: Cleaning up tables");
+ conn.deleteAllPdps();
+
+ /*
+ * Insert this PDP as not designated. Initial standby state will be
+ * either null or cold standby. Demoting should transit state to
+ * hot standby.
+ */
+
+ logger.debug("testAllSeemsWell: Inserting PDP={} as not designated", thisPdpId);
+ Date yesterday = DateUtils.addDays(new Date(), -1);
+ DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testAllSeemsWell: After insertion, PDP={} has DESIGNATED={}",
+ thisPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == false);
+
+ logger.debug("testAllSeemsWell: Instantiating stateManagement object");
+ StateManagement sm = new StateManagement(emfXacml, "dummy");
+ sm.deleteAllStateManagementEntities();
+
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI smf = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ smf = feature;
+ logger.debug("testAllSeemsWell stateManagementFeature.getResourceName(): {}", smf.getResourceName());
+ break;
+ }
+ if (smf == null) {
+ logger.error("testAllSeemsWell failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testAllSeemsWell failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testAllSeemsWell activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testAllSeemsWell failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testAllSeemsWell failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+
+ logger.debug("testAllSeemsWell: Demoting PDP={}", thisPdpId);
+ // demoting should cause state to transit to hotstandby
+ smf.demote();
+
+
+ logger.debug("testAllSeemsWell: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
+ + "time to check droolspdpentity table", sleepTime);
+ sleep(sleepTime);
+
+
+ // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
+
+ droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testAllSeemsWell: After sm.demote() invoked, DESIGNATED= {} "
+ + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+ String standbyStatus = smf.getStandbyStatus(thisPdpId);
+ logger.debug("testAllSeemsWell: After demotion, PDP= {} "
+ + "has standbyStatus= {}", thisPdpId, standbyStatus);
+ assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
+
+ //Now we want to stall the election handler and see the if AllSeemsWell will make the
+ //standbystatus = coldstandby
+
+ DroolsPdpsElectionHandler.setIsStalled(true);
+
+ logger.debug("testAllSeemsWell: Sleeping {} ms, to allow checkWaitTimer to recognize "
+ + "the election handler has stalled and for the testTransaction to fail to "
+ + "increment forward progress and for the lack of forward progress to be recognized.",
+ stalledElectionHandlerSleepTime);
+
+
+ //It takes 10x the update interval (1 sec) before the watcher will declare the election handler dead
+ //and that just stops forward progress counter. So, the fp monitor must then run to determine
+ //if the fpc has stalled. That will take about another 5 sec.
+ sleep(stalledElectionHandlerSleepTime);
+
+ logger.debug("testAllSeemsWell: After isStalled=true, PDP= {} "
+ + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
+
+ assertTrue(smf.getStandbyStatus().equals(StateManagement.COLD_STANDBY));
+
+ //Now lets resume the election handler
+ DroolsPdpsElectionHandler.setIsStalled(false);
+
+ sleep(resumedElectionHandlerSleepTime);
+
+ logger.debug("testAllSeemsWell: After isStalled=false, PDP= {} "
+ + "has standbyStatus= {}", thisPdpId, smf.getStandbyStatus(thisPdpId));
+
+ assertTrue(smf.getStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
+
+ //resumedElectionHandlerSleepTime = 5000;
+ logger.debug("\n\ntestAllSeemsWell: Exiting\n\n");
+
+ }
+
+ private void sleep(long sleepms) throws InterruptedException {
+ Thread.sleep(sleepms);
+ }
}
diff --git a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java
index b79deeb0..aca2021d 100644
--- a/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java
+++ b/feature-active-standby-management/src/test/java/org/onap/policy/drools/controller/test/StandbyStateManagementTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,1473 +69,1545 @@ import org.slf4j.LoggerFactory;
*/
public class StandbyStateManagementTest {
- private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
- /*
- * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
- * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
- * to ensure that we wait for the DesignationWaiter to do its job, before
- * checking the results. Add a few seconds for safety
- */
-
- long sleepTime = 10000;
-
- /*
- * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
- * plenty to ensure it has time to re-promote this PDP.
- */
-
- long electionWaitSleepTime = 6000;
-
- /*
- * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery.
- */
-
- long interruptRecoveryTime = 5000;
-
- private static EntityManagerFactory emfx;
- private static EntityManagerFactory emfd;
- private static EntityManager emx;
- private static EntityManager emd;
- private static EntityTransaction et;
-
- private final String configDir = "src/test/resources";
-
- /*
- * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
- */
-
- @BeforeClass
- public static void setUpClass() throws Exception {
-
- String userDir = System.getProperty("user.dir");
- logger.debug("setUpClass: userDir={}", userDir);
- System.setProperty("com.sun.management.jmxremote.port", "9980");
- System.setProperty("com.sun.management.jmxremote.authenticate","false");
-
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
-
- @Before
- public void setUp() throws Exception {
- //Create teh data access for xaml db
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- "src/test/resources/feature-state-management.properties")));
-
- emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
-
- // Create an entity manager to use the DB
- emx = emfx.createEntityManager();
-
- //Create the data access for drools db
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- "src/test/resources/feature-active-standby-management.properties")));
-
- emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
-
- // Create an entity manager to use the DB
- emd = emfd.createEntityManager();
- }
-
- @After
- public void tearDown() throws Exception {
-
- }
-
- public void cleanXacmlDb(){
- et = emx.getTransaction();
-
- et.begin();
- // Make sure we leave the DB clean
- emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
- emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
- emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
- emx.flush();
- et.commit();
- }
-
- public void cleanDroolsDb(){
- et = emd.getTransaction();
-
- et.begin();
- // Make sure we leave the DB clean
- emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
- emd.flush();
- et.commit();
- }
-
- /*
- * These JUnit tests must be run one at a time in an eclipse environment
- * by right-clicking StandbyStateManagementTest and selecting
- * "Run As" -> "JUnit Test".
- *
- * They will run successfully when you run all of them under runAllTests(),
- * however, you will get all sorts of non-fatal errors in the log and on the
- * console that result from overlapping threads that are not terminated at the
- * end of each test. The problem is that the JUnit environment does not terminate
- * all the test threads between tests. This is true even if you break each JUnit
- * into a separate file. Consequently, all the tests would have to be refactored
- * so all test object initializations are coordinated. In other words, you
- * retrieve the ActiveStandbyFeature instance and other class instances only once
- * at the beginning of the JUnits and then reuse them throughout the tests.
- * Initialization of the state of the objects is pretty straight forward as it
- * just amounts to manipulating the entries in StateManagementEntity and
- * DroolsPdpEntity tables. However, some thought needs to be given to how to
- * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
- * it by calling globalInit() which will, I think, restart it. So long as it
- * does not create a new instance, it will force it to go through an initialization
- * cycle which includes a "pause" at the beginning of proecessing. We just must
- * be sure it does not create another instance - which may mean we need to add
- * a factory interface instead of calling the constructor directly.
- */
-
-
- //@Ignore
- @Test
- public void runAllTests() throws Exception {
- testColdStandby();
- testHotStandby1();
- testHotStandby2();
- testLocking1();
- testLocking2();
- testPMStandbyStateChangeNotifier();
- testSanitizeDesignatedList();
- testComputeMostRecentPrimary();
- testComputeDesignatedPdp();
- }
-
- //@Ignore
- //@Test
- public void testPMStandbyStateChangeNotifier() throws Exception {
- logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
- cleanXacmlDb();
-
- logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties");
-
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
-
- String resourceName = "testPMS";
- activeStandbyProperties.setProperty("resource.name", resourceName);
- ActiveStandbyProperties.initProperties(activeStandbyProperties);
-
- logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance");
-
- StateManagement sm = new StateManagement(emfx, resourceName);
-
- //Create an instance of the Observer
- PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
-
- //Register the PMStandbyStateChangeNotifier Observer
- sm.addObserver(pmNotifier);
-
- //At this point the standbystatus = 'null'
- sm.lock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
-
- sm.unlock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
-
- //Adding standbystatus=hotstandby
- sm.demote();
- System.out.println(pmNotifier.getPreviousStandbyStatus());
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //Now making standbystatus=coldstandby
- sm.lock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //standbystatus = hotstandby
- sm.unlock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //standbystatus = providingservice
- sm.promote();
- //The previousStandbyStatus is not updated until after the delay activation expires
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //Sleep long enough for the delayActivationTimer to run
- sleep(5000);
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
-
- //standbystatus = providingservice
- sm.promote();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
-
- //standbystatus = coldstandby
- sm.lock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //standbystatus = hotstandby
- sm.unlock();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
-
- //standbystatus = hotstandby
- sm.demote();
- assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
- }
-
- //@Ignore
- //@Test
- public void testSanitizeDesignatedList() throws Exception {
-
- logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
-
- // Get a DroolsPdpsConnector
-
- logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testSanitizeDesignatedList: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
-
- // Create 4 pdpd all not designated
-
- DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
- DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
- DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
- DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
-
- List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
- listOfDesignated.add(pdp1);
- listOfDesignated.add(pdp2);
- listOfDesignated.add(pdp3);
- listOfDesignated.add(pdp4);
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI stateManagementFeature = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- stateManagementFeature = feature;
- logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
- break;
- }
- if(stateManagementFeature == null){
- logger.error("testColdStandby failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testColdStandby failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
-
- DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
-
- listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
-
- logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
-
- assertTrue(listOfDesignated.size()==4);
-
- // Now make 2 designated
-
- pdp1.setDesignated(true);
- pdp2.setDesignated(true);
-
- listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
-
- logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", listOfDesignated.size());
-
- assertTrue(listOfDesignated.size()==2);
- assertTrue(listOfDesignated.contains(pdp1));
- assertTrue(listOfDesignated.contains(pdp2));
-
-
- // Now all are designated. But, we have to add back the previously non-designated nodes
-
- pdp3.setDesignated(true);
- pdp4.setDesignated(true);
- listOfDesignated.add(pdp3);
- listOfDesignated.add(pdp4);
-
- listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
-
- logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", listOfDesignated.size());
-
- assertTrue(listOfDesignated.size()==4);
-
- }
-
-
- //@Ignore
- //@Test
- public void testComputeMostRecentPrimary() throws Exception {
-
- logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
-
- logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
-
-
- // Create 4 pdpd all not designated
-
-
- long designatedDateMS = new Date().getTime();
- DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
- pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
-
- DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
- //oldest
- pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
-
- DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
- pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
-
- DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
- //most recent
- pdp4.setDesignatedDate(new Date(designatedDateMS));
-
- ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
- listOfAllPdps.add(pdp1);
- listOfAllPdps.add(pdp2);
- listOfAllPdps.add(pdp3);
- listOfAllPdps.add(pdp4);
-
-
- ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
- listOfDesignated.add(pdp1);
- listOfDesignated.add(pdp2);
- listOfDesignated.add(pdp3);
- listOfDesignated.add(pdp4);
-
- // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
- // or all designated members.
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI stateManagementFeature = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- stateManagementFeature = feature;
- logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
- break;
- }
- if(stateManagementFeature == null){
- logger.error("testComputeMostRecentPrimary failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testComputeMostRecentPrimary failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
-
- DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
-
- // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
- // the one which has the most recent designated date.
-
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
-
-
- // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
-
-
- pdp1.setDesignated(true);
- pdp2.setDesignated(true);
- pdp3.setDesignated(true);
- pdp4.setDesignated(true);
-
- mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
-
- // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
- // the one which was designated first
-
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
-
-
- // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
- // look for the most recently designated pdp which is not currently designated.
-
-
- pdp3.setDesignated(false);
- pdp4.setDesignated(false);
-
- listOfDesignated.remove(pdp3);
- listOfDesignated.remove(pdp4);
-
- mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
-
-
-
- // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
- // look for the most recently designated pdp regardless of whether it is currently marked as designated.
-
-
- pdp1.setDesignated(false);
- pdp2.setDesignated(false);
-
- mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
-
-
- // If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
- // of its designation status
-
-
- listOfDesignated.remove(pdp1);
-
- mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
-
-
- // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
-
-
- listOfDesignated.remove(pdp2);
-
- mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
-
- logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
-
- assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
-
- }
-
- //@Ignore
- //@Test
- public void testComputeDesignatedPdp() throws Exception{
-
- logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
-
- logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
-
- logger.debug("testComputeDesignatedPdp: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
-
-
- // Create 4 pdpd all not designated. Two on site1. Two on site2
-
-
- long designatedDateMS = new Date().getTime();
- DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
- pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
- pdp1.setSiteName("site1");
-
- DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
- pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
- pdp2.setSiteName("site1");
-
- //oldest
- DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
- pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
- pdp3.setSiteName("site2");
-
- DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
- //most recent
- pdp4.setDesignatedDate(new Date(designatedDateMS));
- pdp4.setSiteName("site2");
-
- ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
- listOfAllPdps.add(pdp1);
- listOfAllPdps.add(pdp2);
- listOfAllPdps.add(pdp3);
- listOfAllPdps.add(pdp4);
-
-
- ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
-
-
- // We will first test an empty listOfDesignated. As we know from the previous JUnit,
- // the pdp with the most designated date will be chosen for mostRecentPrimary
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI stateManagementFeature = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- stateManagementFeature = feature;
- logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
- break;
- }
- if(stateManagementFeature == null){
- logger.error("testComputeDesignatedPdp failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testComputeDesignatedPdp failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
-
- DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
-
- DroolsPdp mostRecentPrimary = pdp4;
-
- DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
-
-
- // The designatedPdp should be null
-
- assertTrue(designatedPdp==null);
-
-
- // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
-
- listOfDesignated.add(pdp2);
-
- designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
-
-
- // Now the designatedPdp should be the one and only selection in the listOfDesignated
-
-
- assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
-
-
- // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
-
-
- listOfDesignated.add(pdp1);
-
- designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
-
-
- // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
-
-
- assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
-
-
- // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
-
-
- listOfDesignated.remove(pdp1);
- listOfDesignated.add(pdp3);
-
- designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
-
-
- // The designatedPdp should now be the one on the same site as the mostRecentPrimary
-
-
- assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
- }
-
- //@Ignore
- //@Test
- public void testColdStandby() throws Exception {
-
- logger.debug("\n\ntestColdStandby: Entering\n\n");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.debug("testColdStandby: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.debug("testColdStandby: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.debug("testColdStandby: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testColdStandby: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.debug("testColdStandby: Cleaning up tables");
- conn.deleteAllPdps();
-
- logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
- DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
- + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
- assertTrue(droolsPdpEntity.isDesignated() == true);
-
- /*
- * When the Standby Status changes (from providingservice) to hotstandby
- * or coldstandby,the Active/Standby selection algorithm must stand down
- * if thePDP-D is currently the lead/active node and allow another PDP-D
- * to take over.
- *
- * It must also call lock on all engines in the engine management.
- */
-
-
- /*
- * Yes, this is kludgy, but we have a chicken and egg problem here: we
- * need a StateManagement object to invoke the
- * deleteAllStateManagementEntities method.
- */
- logger.debug("testColdStandby: Instantiating stateManagement object");
-
- StateManagement sm = new StateManagement(emfXacml, "dummy");
- sm.deleteAllStateManagementEntities();
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI smf = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- smf = feature;
- logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
- break;
- }
- if(smf == null){
- logger.error("testColdStandby failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testColdStandby failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testColdStandby failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID:{}", thisPdpId);
- logger.debug("testColdStandby failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID:{}", thisPdpId);
- }
-
- // Artificially putting a PDP into service is really a two step process, 1)
- // inserting it as designated and 2) promoting it so that its standbyStatus
- // is providing service.
-
- logger.debug("testColdStandby: Runner started; Sleeping "
- + interruptRecoveryTime + "ms before promoting PDP= {}",
- thisPdpId);
- sleep(interruptRecoveryTime);
-
- logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
- smf.promote();
-
- String standbyStatus = sm.getStandbyStatus(thisPdpId);
- logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
- thisPdpId, standbyStatus);
-
- logger.debug("testColdStandby: Locking smf");
- smf.lock();
-
- sleep(interruptRecoveryTime);
-
- // Verify that the PDP is no longer designated.
-
- droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testColdStandby: After lock sm.lock() invoked, "
- + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
- assertTrue(droolsPdpEntity.isDesignated() == false);
-
- logger.debug("\n\ntestColdStandby: Exiting\n\n");
- sleep(interruptRecoveryTime);
-
- }
-
- // Tests hot standby when there is only one PDP.
-
- //@Ignore
- //@Test
- public void testHotStandby1() throws Exception {
-
- logger.debug("\n\ntestHotStandby1: Entering\n\n");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.debug("testHotStandby1: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.debug("testHotStandby1: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.debug("testHotStandby1: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testHotStandby1: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.debug("testHotStandby1: Cleaning up tables");
- conn.deleteAllPdps();
-
- /*
- * Insert this PDP as not designated. Initial standby state will be
- * either null or cold standby. Demoting should transit state to
- * hot standby.
- */
-
- logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
- Date yesterday = DateUtils.addDays(new Date(), -1);
- DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
- thisPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == false);
-
- logger.debug("testHotStandby1: Instantiating stateManagement object");
- StateManagement sm = new StateManagement(emfXacml, "dummy");
- sm.deleteAllStateManagementEntities();
-
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI smf = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- smf = feature;
- logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
- break;
- }
- if(smf == null){
- logger.error("testHotStandby1 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testHotStandby1 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testHotStandby1 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testHotStandby1 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
-
- logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
- // demoting should cause state to transit to hotstandby
- smf.demote();
-
-
- logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
- + "time to check droolspdpentity table", sleepTime);
- sleep(sleepTime);
-
-
- // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
-
- droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
- + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
- assertTrue(droolsPdpEntity.isDesignated() == true);
- String standbyStatus = smf.getStandbyStatus(thisPdpId);
- logger.debug("testHotStandby1: After demotion, PDP= {} "
- + "has standbyStatus= {}", thisPdpId, standbyStatus);
- assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
-
- logger.debug("testHotStandby1: Stopping policyManagementRunner");
- //policyManagementRunner.stopRunner();
-
- logger.debug("\n\ntestHotStandby1: Exiting\n\n");
- sleep(interruptRecoveryTime);
-
- }
-
- /*
- * Tests hot standby when two PDPs are involved.
- */
-
- //@Ignore
- //@Test
- public void testHotStandby2() throws Exception {
-
- logger.info("\n\ntestHotStandby2: Entering\n\n");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.info("testHotStandby2: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.info("testHotStandby2: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.info("testHotStandby2: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.info("testHotStandby2: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.info("testHotStandby2: Cleaning up tables");
- conn.deleteAllPdps();
-
-
- // Insert a PDP that's designated but not current.
-
- String activePdpId = "pdp2";
- logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
- Date yesterday = DateUtils.addDays(new Date(), -1);
- DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
- logger.info("testHotStandby2: After insertion, PDP= {}, which is "
- + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == true);
-
- /*
- * Promote the designated PDP.
- *
- * We have a chicken and egg problem here: we need a StateManagement
- * object to invoke the deleteAllStateManagementEntities method.
- */
-
-
- logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
- StateManagement sm = new StateManagement(emfXacml, "dummy");
- sm.deleteAllStateManagementEntities();
-
-
- sm = new StateManagement(emfXacml, activePdpId);//pdp2
-
- // Artificially putting a PDP into service is really a two step process, 1)
- // inserting it as designated and 2) promoting it so that its standbyStatus
- // is providing service.
-
- /*
- * Insert this PDP as not designated. Initial standby state will be
- * either null or cold standby. Demoting should transit state to
- * hot standby.
- */
-
-
- logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
- pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
- conn.insertPdp(pdp);
- droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.info("testHotStandby2: After insertion, PDP={} "
- + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == false);
-
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI sm2 = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- sm2 = feature;
- logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
- break;
- }
- if(sm2 == null){
- logger.error("testHotStandby2 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testHotStandby2 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testHotStandby2 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testHotStandby2 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- logger.info("testHotStandby2: Runner started; Sleeping {} "
- + "ms before promoting/demoting", interruptRecoveryTime);
- sleep(interruptRecoveryTime);
-
- logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
- //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
- //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
- //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
- sm.enableNotFailed();//pdp2
- sm.promote();
- String standbyStatus = sm.getStandbyStatus(activePdpId);
- logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
-
- // demoting PDP should ensure that state transits to hotstandby
- logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
- sm2.demote();//pdp1
- standbyStatus = sm.getStandbyStatus(thisPdpId);
- logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
-
- logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
- + "time to check droolspdpentity table", sleepTime);
- sleep(sleepTime);
-
- /*
- * Verify that this PDP, demoted to HOT_STANDBY, is now
- * re-designated and providing service.
- */
-
- droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.info("testHotStandby2: After demoting PDP={}"
- + ", DESIGNATED= {}"
- + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
- assertTrue(droolsPdpEntity.isDesignated() == true);
- standbyStatus = sm2.getStandbyStatus(thisPdpId);
- logger.info("testHotStandby2: After demoting PDP={}"
- + ", PDP={} has standbyStatus= {}",
- activePdpId, thisPdpId, standbyStatus);
- assertTrue(standbyStatus != null
- && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
-
- logger.info("testHotStandby2: Stopping policyManagementRunner");
- //policyManagementRunner.stopRunner();
-
- logger.info("\n\ntestHotStandby2: Exiting\n\n");
- sleep(interruptRecoveryTime);
-
- }
-
- /*
- * 1) Inserts and designates this PDP, then verifies that startTransaction
- * is successful.
- *
- * 2) Demotes PDP, and verifies that because there is only one PDP, it will
- * be immediately re-promoted, thus allowing startTransaction to be
- * successful.
- *
- * 3) Locks PDP and verifies that startTransaction results in
- * AdministrativeStateException.
- *
- * 4) Unlocks PDP and verifies that startTransaction results in
- * StandbyStatusException.
- *
- * 5) Promotes PDP and verifies that startTransaction is once again
- * successful.
- */
-
- //@Ignore
- //@Test
- public void testLocking1() throws Exception {
- logger.debug("testLocking1: Entry");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.debug("testLocking1: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.debug("testLocking1: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.debug("testLocking1: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testLocking1: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.debug("testLocking1: Cleaning up tables");
- conn.deleteAllPdps();
-
- /*
- * Insert this PDP as designated. Initial standby state will be
- * either null or cold standby.
- */
-
- logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
- DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
- thisPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == true);
-
- logger.debug("testLocking1: Instantiating stateManagement object");
- StateManagement smDummy = new StateManagement(emfXacml, "dummy");
- smDummy.deleteAllStateManagementEntities();
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI sm = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- sm = feature;
- logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
- break;
- }
- if(sm == null){
- logger.error("testLocking1 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testLocking1 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testLocking1 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testLocking1 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- logger.debug("testLocking1: Runner started; Sleeping "
- + interruptRecoveryTime + "ms before promoting PDP={}",
- thisPdpId);
- sleep(interruptRecoveryTime);
-
- logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
- sm.promote();
-
- logger.debug("testLocking1: Sleeping {} ms, to allow time for "
- + "policy-management.Main class to come up, designated= {}",
- sleepTime, conn.getPdp(thisPdpId).isDesignated());
- sleep(sleepTime);
-
- logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
- + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
-
-
- IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- droolsPdpIntegrityMonitor.endTransaction();
- logger.debug("testLocking1: As expected, transaction successful");
- } catch (AdministrativeStateException e) {
- logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking1: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- }
-
- // demoting should cause state to transit to hotstandby, followed by re-promotion,
- // since there is only one PDP.
- logger.debug("testLocking1: demoting PDP={}", thisPdpId);
- sm.demote();
-
- logger.debug("testLocking1: sleeping" + electionWaitSleepTime
- + " to allow election handler to re-promote PDP={}", thisPdpId);
- sleep(electionWaitSleepTime);
-
- logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
- + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- droolsPdpIntegrityMonitor.endTransaction();
- logger.debug("testLocking1: As expected, transaction successful");
- } catch (AdministrativeStateException e) {
- logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking1: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- }
-
- // locking should cause state to transit to cold standby
- logger.debug("testLocking1: locking PDP={}", thisPdpId);
- sm.lock();
-
- // Just to avoid any race conditions, sleep a little after locking
- logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
- sleep(100);
-
- logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
- + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- logger.error("testLocking1: startTransaction unexpectedly successful");
- assertTrue(false);
- } catch (AdministrativeStateException e) {
- logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
- } catch (StandbyStatusException e) {
- logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking1: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- } finally {
- droolsPdpIntegrityMonitor.endTransaction();
- }
-
- // unlocking should cause state to transit to hot standby and then providing service
- logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
- sm.unlock();
-
- // Just to avoid any race conditions, sleep a little after locking
- logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
- sleep(electionWaitSleepTime);
-
- logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
- + thisPdpId
- + ", designated="
- + conn.getPdp(thisPdpId).isDesignated());
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- logger.error("testLocking1: startTransaction successful as expected");
- } catch (AdministrativeStateException e) {
- logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking1: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- } finally {
- droolsPdpIntegrityMonitor.endTransaction();
- }
-
- // demoting should cause state to transit to hot standby
- logger.debug("testLocking1: demoting PDP={}", thisPdpId);
- sm.demote();
-
- // Just to avoid any race conditions, sleep a little after promoting
- logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
- sleep(100);
-
- logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
- + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- droolsPdpIntegrityMonitor.endTransaction();
- logger.debug("testLocking1: Unexpectedly, transaction successful");
- assertTrue(false);
- } catch (AdministrativeStateException e) {
- logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
- } catch (Exception e) {
- logger.error("testLocking1: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- }
-
- logger.debug("\n\ntestLocking1: Exiting\n\n");
- sleep(interruptRecoveryTime);
-
- }
-
-
- /*
- * 1) Inserts and designates this PDP, then verifies that startTransaction
- * is successful.
- *
- * 2) Inserts another PDP in hotstandby.
- *
- * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
- * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
- */
-
- //@Ignore
- //@Test
- public void testLocking2() throws Exception {
-
- logger.debug("\n\ntestLocking2: Entering\n\n");
- cleanXacmlDb();
- cleanDroolsDb();
-
- logger.debug("testLocking2: Reading stateManagementProperties");
- Properties stateManagementProperties = new Properties();
- stateManagementProperties.load(new FileInputStream(new File(
- configDir + "/feature-state-management.properties")));
-
- logger.debug("testLocking2: Creating emfXacml");
- EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
- "junitXacmlPU", stateManagementProperties);
-
- logger.debug("testLocking2: Reading activeStandbyProperties");
- Properties activeStandbyProperties = new Properties();
- activeStandbyProperties.load(new FileInputStream(new File(
- configDir + "/feature-active-standby-management.properties")));
- String thisPdpId = activeStandbyProperties
- .getProperty(ActiveStandbyProperties.NODE_NAME);
-
- logger.debug("testLocking2: Creating emfDrools");
- EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
- "junitDroolsPU", activeStandbyProperties);
-
- DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
-
- logger.debug("testLocking2: Cleaning up tables");
- conn.deleteAllPdps();
-
- /*
- * Insert this PDP as designated. Initial standby state will be
- * either null or cold standby. Demoting should transit state to
- * hot standby.
- */
-
- logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
- DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
- conn.insertPdp(pdp);
- DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
- logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
- thisPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == true);
-
- logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
- StateManagement smDummy = new StateManagement(emfXacml, "dummy");
- smDummy.deleteAllStateManagementEntities();
-
- // Now we want to create a StateManagementFeature and initialize it. It will be
- // discovered by the ActiveStandbyFeature when the election handler initializes.
-
- StateManagementFeatureAPI sm = null;
- for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- sm = feature;
- logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
- break;
- }
- if(sm == null){
- logger.error("testLocking2 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testLocking2 failed to initialize. "
- + "Unable to get instance of StateManagementFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
- // that has been created.
- ActiveStandbyFeatureAPI activeStandbyFeature = null;
- for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
- {
- ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
- activeStandbyFeature = feature;
- logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
- break;
- }
- if(activeStandbyFeature == null){
- logger.error("testLocking2 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- logger.debug("testLocking2 failed to initialize. "
- + "Unable to get instance of ActiveStandbyFeatureAPI "
- + "with resourceID: {}", thisPdpId);
- }
-
- /*
- * Insert another PDP as not designated. Initial standby state will be
- * either null or cold standby. Demoting should transit state to
- * hot standby.
- */
-
- String standbyPdpId = "pdp2";
- logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
- Date yesterday = DateUtils.addDays(new Date(), -1);
- pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
- conn.insertPdp(pdp);
- droolsPdpEntity = conn.getPdp(standbyPdpId);
- logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
- standbyPdpId, droolsPdpEntity.isDesignated());
- assertTrue(droolsPdpEntity.isDesignated() == false);
-
- logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
- StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
-
- logger.debug("testLocking2: Runner started; Sleeping {} ms "
- + "before promoting/demoting", interruptRecoveryTime);
- sleep(interruptRecoveryTime);
-
- logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
- sm.promote();
-
- // demoting PDP should ensure that state transits to hotstandby
- logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
- sm2.demote();
-
- logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
- sleep(sleepTime);
-
- logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
- + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
-
- IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
-
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- droolsPdpIntegrityMonitor.endTransaction();
- logger.debug("testLocking2: As expected, transaction successful");
- } catch (AdministrativeStateException e) {
- logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking2: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- }
-
- // demoting should cause state to transit to hotstandby followed by re-promotion.
- logger.debug("testLocking2: demoting PDP={}", thisPdpId);
- sm.demote();
-
- logger.debug("testLocking2: sleeping {}"
- + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
- sleep(electionWaitSleepTime);
-
- logger.debug("testLocking2: Waking up and invoking startTransaction "
- + "on re-promoted PDP= {}, designated= {}",
- thisPdpId, conn.getPdp(thisPdpId).isDesignated());
- try {
- droolsPdpIntegrityMonitor.startTransaction();
- droolsPdpIntegrityMonitor.endTransaction();
- logger.debug("testLocking2: As expected, transaction successful");
- } catch (AdministrativeStateException e) {
- logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
- assertTrue(false);
- } catch (StandbyStatusException e) {
- logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
- assertTrue(false);
- } catch (Exception e) {
- logger.error("testLocking2: Unexpectedly caught Exception, ", e);
- assertTrue(false);
- }
-
- logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
- boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
- assertTrue(standbyPdpDesignated == false);
-
- logger.debug("\n\ntestLocking2: Exiting\n\n");
- sleep(interruptRecoveryTime);
- }
-
- private void sleep(long sleepms) throws InterruptedException {
- Thread.sleep(sleepms);
- }
+ private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
+ /*
+ * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
+ * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
+ * to ensure that we wait for the DesignationWaiter to do its job, before
+ * checking the results. Add a few seconds for safety
+ */
+
+ long sleepTime = 10000;
+
+ /*
+ * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
+ * plenty to ensure it has time to re-promote this PDP.
+ */
+
+ long electionWaitSleepTime = 6000;
+
+ /*
+ * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery.
+ */
+
+ long interruptRecoveryTime = 5000;
+
+ private static EntityManagerFactory emfx;
+ private static EntityManagerFactory emfd;
+ private static EntityManager emx;
+ private static EntityManager emd;
+ private static EntityTransaction et;
+
+ private final String configDir = "src/test/resources";
+
+ /*
+ * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
+ */
+
+ /**
+ * Setup the class.
+ *
+ * @throws Exception exception
+ */
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+
+ String userDir = System.getProperty("user.dir");
+ logger.debug("setUpClass: userDir={}", userDir);
+ System.setProperty("com.sun.management.jmxremote.port", "9980");
+ System.setProperty("com.sun.management.jmxremote.authenticate","false");
+
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ /**
+ * Setup.
+ *
+ * @throws Exception exception
+ */
+ @Before
+ public void setUp() throws Exception {
+ //Create teh data access for xaml db
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ "src/test/resources/feature-state-management.properties")));
+
+ emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
+
+ // Create an entity manager to use the DB
+ emx = emfx.createEntityManager();
+
+ //Create the data access for drools db
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ "src/test/resources/feature-active-standby-management.properties")));
+
+ emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
+
+ // Create an entity manager to use the DB
+ emd = emfd.createEntityManager();
+ }
+
+ @After
+ public void tearDown() throws Exception {
+
+ }
+
+ /**
+ * Clean up the xacml database.
+ *
+ */
+ public void cleanXacmlDb() {
+ et = emx.getTransaction();
+
+ et.begin();
+ // Make sure we leave the DB clean
+ emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
+ emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
+ emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
+ emx.flush();
+ et.commit();
+ }
+
+ /**
+ * Clean up the drools db.
+ */
+ public void cleanDroolsDb() {
+ et = emd.getTransaction();
+
+ et.begin();
+ // Make sure we leave the DB clean
+ emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
+ emd.flush();
+ et.commit();
+ }
+
+ /*
+ * These JUnit tests must be run one at a time in an eclipse environment
+ * by right-clicking StandbyStateManagementTest and selecting
+ * "Run As" -> "JUnit Test".
+ *
+ * They will run successfully when you run all of them under runAllTests(),
+ * however, you will get all sorts of non-fatal errors in the log and on the
+ * console that result from overlapping threads that are not terminated at the
+ * end of each test. The problem is that the JUnit environment does not terminate
+ * all the test threads between tests. This is true even if you break each JUnit
+ * into a separate file. Consequently, all the tests would have to be refactored
+ * so all test object initializations are coordinated. In other words, you
+ * retrieve the ActiveStandbyFeature instance and other class instances only once
+ * at the beginning of the JUnits and then reuse them throughout the tests.
+ * Initialization of the state of the objects is pretty straight forward as it
+ * just amounts to manipulating the entries in StateManagementEntity and
+ * DroolsPdpEntity tables. However, some thought needs to be given to how to
+ * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
+ * it by calling globalInit() which will, I think, restart it. So long as it
+ * does not create a new instance, it will force it to go through an initialization
+ * cycle which includes a "pause" at the beginning of proecessing. We just must
+ * be sure it does not create another instance - which may mean we need to add
+ * a factory interface instead of calling the constructor directly.
+ */
+
+
+ //@Ignore
+ @Test
+ public void runAllTests() throws Exception {
+ testColdStandby();
+ testHotStandby1();
+ testHotStandby2();
+ testLocking1();
+ testLocking2();
+ testPmStandbyStateChangeNotifier();
+ testSanitizeDesignatedList();
+ testComputeMostRecentPrimary();
+ testComputeDesignatedPdp();
+ }
+
+ /**
+ * Test the standby state change notifier.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testPmStandbyStateChangeNotifier() throws Exception {
+ logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
+ cleanXacmlDb();
+
+ logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties");
+
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+
+ String resourceName = "testPMS";
+ activeStandbyProperties.setProperty("resource.name", resourceName);
+ ActiveStandbyProperties.initProperties(activeStandbyProperties);
+
+ logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagement instance");
+
+ StateManagement sm = new StateManagement(emfx, resourceName);
+
+ //Create an instance of the Observer
+ PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
+
+ //Register the PMStandbyStateChangeNotifier Observer
+ sm.addObserver(pmNotifier);
+
+ //At this point the standbystatus = 'null'
+ sm.lock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
+
+ sm.unlock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
+
+ //Adding standbystatus=hotstandby
+ sm.demote();
+ System.out.println(pmNotifier.getPreviousStandbyStatus());
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //Now making standbystatus=coldstandby
+ sm.lock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //standbystatus = hotstandby
+ sm.unlock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //standbystatus = providingservice
+ sm.promote();
+ //The previousStandbyStatus is not updated until after the delay activation expires
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //Sleep long enough for the delayActivationTimer to run
+ sleep(5000);
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
+
+ //standbystatus = providingservice
+ sm.promote();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
+
+ //standbystatus = coldstandby
+ sm.lock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //standbystatus = hotstandby
+ sm.unlock();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+
+ //standbystatus = hotstandby
+ sm.demote();
+ assertTrue(pmNotifier.getPreviousStandbyStatus().equals(
+ PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
+ }
+
+ /**
+ * Test sanitize designated list.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testSanitizeDesignatedList() throws Exception {
+
+ logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
+
+ // Get a DroolsPdpsConnector
+
+ logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testSanitizeDesignatedList: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
+
+ // Create 4 pdpd all not designated
+
+ DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
+ DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
+ DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
+ DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
+
+ List<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
+ listOfDesignated.add(pdp1);
+ listOfDesignated.add(pdp2);
+ listOfDesignated.add(pdp3);
+ listOfDesignated.add(pdp4);
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI stateManagementFeature = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ stateManagementFeature = feature;
+ logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}",
+ stateManagementFeature.getResourceName());
+ break;
+ }
+ if (stateManagementFeature == null) {
+ logger.error("testColdStandby failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testColdStandby failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+
+ DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
+
+ listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
+
+ logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
+
+ assertTrue(listOfDesignated.size() == 4);
+
+ // Now make 2 designated
+
+ pdp1.setDesignated(true);
+ pdp2.setDesignated(true);
+
+ listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
+
+ logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n",
+ listOfDesignated.size());
+
+ assertTrue(listOfDesignated.size() == 2);
+ assertTrue(listOfDesignated.contains(pdp1));
+ assertTrue(listOfDesignated.contains(pdp2));
+
+
+ // Now all are designated. But, we have to add back the previously non-designated nodes
+
+ pdp3.setDesignated(true);
+ pdp4.setDesignated(true);
+ listOfDesignated.add(pdp3);
+ listOfDesignated.add(pdp4);
+
+ listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
+
+ logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n",
+ listOfDesignated.size());
+
+ assertTrue(listOfDesignated.size() == 4);
+
+ }
+
+ /**
+ * Test Compute most recent primary.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testComputeMostRecentPrimary() throws Exception {
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
+
+ logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
+
+
+ // Create 4 pdpd all not designated
+
+
+ long designatedDateMs = new Date().getTime();
+ DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
+ pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
+
+ DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
+ //oldest
+ pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
+
+ DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
+ pdp3.setDesignatedDate(new Date(designatedDateMs - 1));
+
+ DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
+ //most recent
+ pdp4.setDesignatedDate(new Date(designatedDateMs));
+
+ ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
+ listOfAllPdps.add(pdp1);
+ listOfAllPdps.add(pdp2);
+ listOfAllPdps.add(pdp3);
+ listOfAllPdps.add(pdp4);
+
+
+ ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
+ listOfDesignated.add(pdp1);
+ listOfDesignated.add(pdp2);
+ listOfDesignated.add(pdp3);
+ listOfDesignated.add(pdp4);
+
+ // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
+ // or all designated members.
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI stateManagementFeature = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ stateManagementFeature = feature;
+ logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}",
+ stateManagementFeature.getResourceName());
+ break;
+ }
+ if (stateManagementFeature == null) {
+ logger.error("testComputeMostRecentPrimary failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testComputeMostRecentPrimary failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
+
+ DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(
+ listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+
+ // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
+ // the one which has the most recent designated date.
+
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
+
+
+ // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
+
+
+ pdp1.setDesignated(true);
+ pdp2.setDesignated(true);
+ pdp3.setDesignated(true);
+ pdp4.setDesignated(true);
+
+ mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, "
+ + "mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+
+ // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
+ // the one which was designated first
+
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
+
+
+ // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
+ // look for the most recently designated pdp which is not currently designated.
+
+
+ pdp3.setDesignated(false);
+ pdp4.setDesignated(false);
+
+ listOfDesignated.remove(pdp3);
+ listOfDesignated.remove(pdp4);
+
+ mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
+
+
+
+ // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
+ // look for the most recently designated pdp regardless of whether it is currently marked as designated.
+
+
+ pdp1.setDesignated(false);
+ pdp2.setDesignated(false);
+
+ mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
+
+
+ // If we have only one pdp on in the listOfDesignated,
+ // the most recently designated pdp will be chosen, regardless
+ // of its designation status
+
+
+ listOfDesignated.remove(pdp1);
+
+ mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
+
+
+ // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
+
+
+ listOfDesignated.remove(pdp2);
+
+ mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
+
+ logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n",
+ mostRecentPrimary.getPdpId());
+
+ assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
+
+ }
+
+ /**
+ * Test compute designated PDP.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testComputeDesignatedPdp() throws Exception {
+
+ logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
+
+ logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+
+ logger.debug("testComputeDesignatedPdp: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ final DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
+
+
+ // Create 4 pdpd all not designated. Two on site1. Two on site2
+
+
+ long designatedDateMs = new Date().getTime();
+ DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
+ pdp1.setDesignatedDate(new Date(designatedDateMs - 2));
+ pdp1.setSiteName("site1");
+
+ DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
+ pdp2.setDesignatedDate(new Date(designatedDateMs - 3));
+ pdp2.setSiteName("site1");
+
+ //oldest
+ DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
+ pdp3.setDesignatedDate(new Date(designatedDateMs - 4));
+ pdp3.setSiteName("site2");
+
+ DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
+ //most recent
+ pdp4.setDesignatedDate(new Date(designatedDateMs));
+ pdp4.setSiteName("site2");
+
+ ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
+ listOfAllPdps.add(pdp1);
+ listOfAllPdps.add(pdp2);
+ listOfAllPdps.add(pdp3);
+ listOfAllPdps.add(pdp4);
+
+
+ ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
+
+
+ // We will first test an empty listOfDesignated. As we know from the previous JUnit,
+ // the pdp with the most designated date will be chosen for mostRecentPrimary
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI stateManagementFeature = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ stateManagementFeature = feature;
+ logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}",
+ stateManagementFeature.getResourceName());
+ break;
+ }
+ if (stateManagementFeature == null) {
+ logger.error("testComputeDesignatedPdp failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testComputeDesignatedPdp failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+
+ DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
+
+ DroolsPdp mostRecentPrimary = pdp4;
+
+ DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
+
+
+ // The designatedPdp should be null
+
+ assertTrue(designatedPdp == null);
+
+
+ // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
+
+ listOfDesignated.add(pdp2);
+
+ designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
+
+
+ // Now the designatedPdp should be the one and only selection in the listOfDesignated
+
+
+ assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
+
+
+ // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
+
+
+ listOfDesignated.add(pdp1);
+
+ designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
+
+
+ // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
+
+
+ assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
+
+
+ // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
+
+
+ listOfDesignated.remove(pdp1);
+ listOfDesignated.add(pdp3);
+
+ designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
+
+
+ // The designatedPdp should now be the one on the same site as the mostRecentPrimary
+
+
+ assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
+ }
+
+ /**
+ * Test cold standby.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testColdStandby() throws Exception {
+
+ logger.debug("\n\ntestColdStandby: Entering\n\n");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.debug("testColdStandby: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.debug("testColdStandby: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.debug("testColdStandby: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testColdStandby: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.debug("testColdStandby: Cleaning up tables");
+ conn.deleteAllPdps();
+
+ logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
+ DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
+ + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+
+ /*
+ * When the Standby Status changes (from providingservice) to hotstandby
+ * or coldstandby,the Active/Standby selection algorithm must stand down
+ * if thePDP-D is currently the lead/active node and allow another PDP-D
+ * to take over.
+ *
+ * It must also call lock on all engines in the engine management.
+ */
+
+
+ /*
+ * Yes, this is kludgy, but we have a chicken and egg problem here: we
+ * need a StateManagement object to invoke the
+ * deleteAllStateManagementEntities method.
+ */
+ logger.debug("testColdStandby: Instantiating stateManagement object");
+
+ StateManagement sm = new StateManagement(emfXacml, "dummy");
+ sm.deleteAllStateManagementEntities();
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI smf = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ smf = feature;
+ logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
+ break;
+ }
+ if (smf == null) {
+ logger.error("testColdStandby failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testColdStandby failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testColdStandby failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID:{}", thisPdpId);
+ logger.debug("testColdStandby failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID:{}", thisPdpId);
+ }
+
+ // Artificially putting a PDP into service is really a two step process, 1)
+ // inserting it as designated and 2) promoting it so that its standbyStatus
+ // is providing service.
+
+ logger.debug("testColdStandby: Runner started; Sleeping "
+ + interruptRecoveryTime + "ms before promoting PDP= {}",
+ thisPdpId);
+ sleep(interruptRecoveryTime);
+
+ logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
+ smf.promote();
+
+ String standbyStatus = sm.getStandbyStatus(thisPdpId);
+ logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
+ thisPdpId, standbyStatus);
+
+ logger.debug("testColdStandby: Locking smf");
+ smf.lock();
+
+ sleep(interruptRecoveryTime);
+
+ // Verify that the PDP is no longer designated.
+
+ droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testColdStandby: After lock sm.lock() invoked, "
+ + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
+ assertTrue(droolsPdpEntity.isDesignated() == false);
+
+ logger.debug("\n\ntestColdStandby: Exiting\n\n");
+ sleep(interruptRecoveryTime);
+
+ }
+
+ // Tests hot standby when there is only one PDP.
+
+ /**
+ * Test hot standby 1.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testHotStandby1() throws Exception {
+
+ logger.debug("\n\ntestHotStandby1: Entering\n\n");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.debug("testHotStandby1: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.debug("testHotStandby1: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.debug("testHotStandby1: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testHotStandby1: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.debug("testHotStandby1: Cleaning up tables");
+ conn.deleteAllPdps();
+
+ /*
+ * Insert this PDP as not designated. Initial standby state will be
+ * either null or cold standby. Demoting should transit state to
+ * hot standby.
+ */
+
+ logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
+ Date yesterday = DateUtils.addDays(new Date(), -1);
+ DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
+ thisPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == false);
+
+ logger.debug("testHotStandby1: Instantiating stateManagement object");
+ StateManagement sm = new StateManagement(emfXacml, "dummy");
+ sm.deleteAllStateManagementEntities();
+
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI smf = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ smf = feature;
+ logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
+ break;
+ }
+ if (smf == null) {
+ logger.error("testHotStandby1 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testHotStandby1 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testHotStandby1 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testHotStandby1 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+
+ logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
+ // demoting should cause state to transit to hotstandby
+ smf.demote();
+
+
+ logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
+ + "time to check droolspdpentity table", sleepTime);
+ sleep(sleepTime);
+
+
+ // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
+
+ droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
+ + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+ String standbyStatus = smf.getStandbyStatus(thisPdpId);
+ logger.debug("testHotStandby1: After demotion, PDP= {} "
+ + "has standbyStatus= {}", thisPdpId, standbyStatus);
+ assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
+
+ logger.debug("testHotStandby1: Stopping policyManagementRunner");
+
+ logger.debug("\n\ntestHotStandby1: Exiting\n\n");
+ sleep(interruptRecoveryTime);
+
+ }
+
+ /*
+ * Tests hot standby when two PDPs are involved.
+ */
+
+ /**
+ * Test hot standby 2.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testHotStandby2() throws Exception {
+
+ logger.info("\n\ntestHotStandby2: Entering\n\n");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.info("testHotStandby2: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.info("testHotStandby2: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.info("testHotStandby2: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.info("testHotStandby2: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.info("testHotStandby2: Cleaning up tables");
+ conn.deleteAllPdps();
+
+
+ // Insert a PDP that's designated but not current.
+
+ String activePdpId = "pdp2";
+ logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
+ Date yesterday = DateUtils.addDays(new Date(), -1);
+ DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
+ logger.info("testHotStandby2: After insertion, PDP= {}, which is "
+ + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+
+ /*
+ * Promote the designated PDP.
+ *
+ * We have a chicken and egg problem here: we need a StateManagement
+ * object to invoke the deleteAllStateManagementEntities method.
+ */
+
+
+ logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
+ StateManagement sm = new StateManagement(emfXacml, "dummy");
+ sm.deleteAllStateManagementEntities();
+
+
+ sm = new StateManagement(emfXacml, activePdpId);//pdp2
+
+ // Artificially putting a PDP into service is really a two step process, 1)
+ // inserting it as designated and 2) promoting it so that its standbyStatus
+ // is providing service.
+
+ /*
+ * Insert this PDP as not designated. Initial standby state will be
+ * either null or cold standby. Demoting should transit state to
+ * hot standby.
+ */
+
+
+ logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
+ pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
+ conn.insertPdp(pdp);
+ droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.info("testHotStandby2: After insertion, PDP={} "
+ + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == false);
+
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI sm2 = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ sm2 = feature;
+ logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
+ break;
+ }
+ if (sm2 == null) {
+ logger.error("testHotStandby2 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testHotStandby2 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testHotStandby2 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testHotStandby2 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ logger.info("testHotStandby2: Runner started; Sleeping {} "
+ + "ms before promoting/demoting", interruptRecoveryTime);
+ sleep(interruptRecoveryTime);
+
+ logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
+ //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
+ //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
+ //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
+ sm.enableNotFailed();//pdp2
+ sm.promote();
+ String standbyStatus = sm.getStandbyStatus(activePdpId);
+ logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
+
+ // demoting PDP should ensure that state transits to hotstandby
+ logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
+ sm2.demote();//pdp1
+ standbyStatus = sm.getStandbyStatus(thisPdpId);
+ logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
+
+ logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
+ + "time to check droolspdpentity table", sleepTime);
+ sleep(sleepTime);
+
+ /*
+ * Verify that this PDP, demoted to HOT_STANDBY, is now
+ * re-designated and providing service.
+ */
+
+ droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.info("testHotStandby2: After demoting PDP={}"
+ + ", DESIGNATED= {}"
+ + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+ standbyStatus = sm2.getStandbyStatus(thisPdpId);
+ logger.info("testHotStandby2: After demoting PDP={}"
+ + ", PDP={} has standbyStatus= {}",
+ activePdpId, thisPdpId, standbyStatus);
+ assertTrue(standbyStatus != null
+ && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
+
+ logger.info("testHotStandby2: Stopping policyManagementRunner");
+
+ logger.info("\n\ntestHotStandby2: Exiting\n\n");
+ sleep(interruptRecoveryTime);
+
+ }
+
+ /*
+ * 1) Inserts and designates this PDP, then verifies that startTransaction
+ * is successful.
+ *
+ * 2) Demotes PDP, and verifies that because there is only one PDP, it will
+ * be immediately re-promoted, thus allowing startTransaction to be
+ * successful.
+ *
+ * 3) Locks PDP and verifies that startTransaction results in
+ * AdministrativeStateException.
+ *
+ * 4) Unlocks PDP and verifies that startTransaction results in
+ * StandbyStatusException.
+ *
+ * 5) Promotes PDP and verifies that startTransaction is once again
+ * successful.
+ */
+
+ /**
+ * Test locking.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testLocking1() throws Exception {
+ logger.debug("testLocking1: Entry");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.debug("testLocking1: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.debug("testLocking1: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.debug("testLocking1: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testLocking1: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.debug("testLocking1: Cleaning up tables");
+ conn.deleteAllPdps();
+
+ /*
+ * Insert this PDP as designated. Initial standby state will be
+ * either null or cold standby.
+ */
+
+ logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
+ DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
+ thisPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+
+ logger.debug("testLocking1: Instantiating stateManagement object");
+ StateManagement smDummy = new StateManagement(emfXacml, "dummy");
+ smDummy.deleteAllStateManagementEntities();
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI sm = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ sm = feature;
+ logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
+ break;
+ }
+ if (sm == null) {
+ logger.error("testLocking1 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testLocking1 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testLocking1 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testLocking1 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ logger.debug("testLocking1: Runner started; Sleeping "
+ + interruptRecoveryTime + "ms before promoting PDP={}",
+ thisPdpId);
+ sleep(interruptRecoveryTime);
+
+ logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
+ sm.promote();
+
+ logger.debug("testLocking1: Sleeping {} ms, to allow time for "
+ + "policy-management.Main class to come up, designated= {}",
+ sleepTime, conn.getPdp(thisPdpId).isDesignated());
+ sleep(sleepTime);
+
+ logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
+ + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+
+
+ IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ droolsPdpIntegrityMonitor.endTransaction();
+ logger.debug("testLocking1: As expected, transaction successful");
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking1: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ }
+
+ // demoting should cause state to transit to hotstandby, followed by re-promotion,
+ // since there is only one PDP.
+ logger.debug("testLocking1: demoting PDP={}", thisPdpId);
+ sm.demote();
+
+ logger.debug("testLocking1: sleeping" + electionWaitSleepTime
+ + " to allow election handler to re-promote PDP={}", thisPdpId);
+ sleep(electionWaitSleepTime);
+
+ logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
+ + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ droolsPdpIntegrityMonitor.endTransaction();
+ logger.debug("testLocking1: As expected, transaction successful");
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking1: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ }
+
+ // locking should cause state to transit to cold standby
+ logger.debug("testLocking1: locking PDP={}", thisPdpId);
+ sm.lock();
+
+ // Just to avoid any race conditions, sleep a little after locking
+ logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
+ sleep(100);
+
+ logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
+ + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ logger.error("testLocking1: startTransaction unexpectedly successful");
+ assertTrue(false);
+ } catch (AdministrativeStateException e) {
+ logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking1: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ } finally {
+ droolsPdpIntegrityMonitor.endTransaction();
+ }
+
+ // unlocking should cause state to transit to hot standby and then providing service
+ logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
+ sm.unlock();
+
+ // Just to avoid any race conditions, sleep a little after locking
+ logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
+ sleep(electionWaitSleepTime);
+
+ logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
+ + thisPdpId
+ + ", designated="
+ + conn.getPdp(thisPdpId).isDesignated());
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ logger.error("testLocking1: startTransaction successful as expected");
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking1: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ } finally {
+ droolsPdpIntegrityMonitor.endTransaction();
+ }
+
+ // demoting should cause state to transit to hot standby
+ logger.debug("testLocking1: demoting PDP={}", thisPdpId);
+ sm.demote();
+
+ // Just to avoid any race conditions, sleep a little after promoting
+ logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
+ sleep(100);
+
+ logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
+ + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ droolsPdpIntegrityMonitor.endTransaction();
+ logger.debug("testLocking1: Unexpectedly, transaction successful");
+ assertTrue(false);
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
+ } catch (Exception e) {
+ logger.error("testLocking1: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ }
+
+ logger.debug("\n\ntestLocking1: Exiting\n\n");
+ sleep(interruptRecoveryTime);
+
+ }
+
+
+ /*
+ * 1) Inserts and designates this PDP, then verifies that startTransaction
+ * is successful.
+ *
+ * 2) Inserts another PDP in hotstandby.
+ *
+ * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
+ * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
+ */
+
+ /**
+ * Test locking 2.
+ *
+ * @throws Exception exception
+ */
+ //@Ignore
+ //@Test
+ public void testLocking2() throws Exception {
+
+ logger.debug("\n\ntestLocking2: Entering\n\n");
+ cleanXacmlDb();
+ cleanDroolsDb();
+
+ logger.debug("testLocking2: Reading stateManagementProperties");
+ Properties stateManagementProperties = new Properties();
+ stateManagementProperties.load(new FileInputStream(new File(
+ configDir + "/feature-state-management.properties")));
+
+ logger.debug("testLocking2: Creating emfXacml");
+ final EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
+ "junitXacmlPU", stateManagementProperties);
+
+ logger.debug("testLocking2: Reading activeStandbyProperties");
+ Properties activeStandbyProperties = new Properties();
+ activeStandbyProperties.load(new FileInputStream(new File(
+ configDir + "/feature-active-standby-management.properties")));
+ final String thisPdpId = activeStandbyProperties
+ .getProperty(ActiveStandbyProperties.NODE_NAME);
+
+ logger.debug("testLocking2: Creating emfDrools");
+ EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
+ "junitDroolsPU", activeStandbyProperties);
+
+ DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
+
+ logger.debug("testLocking2: Cleaning up tables");
+ conn.deleteAllPdps();
+
+ /*
+ * Insert this PDP as designated. Initial standby state will be
+ * either null or cold standby. Demoting should transit state to
+ * hot standby.
+ */
+
+ logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
+ DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
+ conn.insertPdp(pdp);
+ DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
+ logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
+ thisPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == true);
+
+ logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
+ StateManagement smDummy = new StateManagement(emfXacml, "dummy");
+ smDummy.deleteAllStateManagementEntities();
+
+ // Now we want to create a StateManagementFeature and initialize it. It will be
+ // discovered by the ActiveStandbyFeature when the election handler initializes.
+
+ StateManagementFeatureAPI sm = null;
+ for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ sm = feature;
+ logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
+ break;
+ }
+ if (sm == null) {
+ logger.error("testLocking2 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testLocking2 failed to initialize. "
+ + "Unable to get instance of StateManagementFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
+ // that has been created.
+ ActiveStandbyFeatureAPI activeStandbyFeature = null;
+ for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList()) {
+ ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
+ activeStandbyFeature = feature;
+ logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}",
+ activeStandbyFeature.getResourceName());
+ break;
+ }
+ if (activeStandbyFeature == null) {
+ logger.error("testLocking2 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ logger.debug("testLocking2 failed to initialize. "
+ + "Unable to get instance of ActiveStandbyFeatureAPI "
+ + "with resourceID: {}", thisPdpId);
+ }
+
+ /*
+ * Insert another PDP as not designated. Initial standby state will be
+ * either null or cold standby. Demoting should transit state to
+ * hot standby.
+ */
+
+ String standbyPdpId = "pdp2";
+ logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
+ Date yesterday = DateUtils.addDays(new Date(), -1);
+ pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
+ conn.insertPdp(pdp);
+ droolsPdpEntity = conn.getPdp(standbyPdpId);
+ logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
+ standbyPdpId, droolsPdpEntity.isDesignated());
+ assertTrue(droolsPdpEntity.isDesignated() == false);
+
+ logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
+ final StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
+
+ logger.debug("testLocking2: Runner started; Sleeping {} ms "
+ + "before promoting/demoting", interruptRecoveryTime);
+ sleep(interruptRecoveryTime);
+
+ logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
+ sm.promote();
+
+ // demoting PDP should ensure that state transits to hotstandby
+ logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
+ sm2.demote();
+
+ logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
+ sleep(sleepTime);
+
+ logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
+ + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+
+ IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
+
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ droolsPdpIntegrityMonitor.endTransaction();
+ logger.debug("testLocking2: As expected, transaction successful");
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking2: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ }
+
+ // demoting should cause state to transit to hotstandby followed by re-promotion.
+ logger.debug("testLocking2: demoting PDP={}", thisPdpId);
+ sm.demote();
+
+ logger.debug("testLocking2: sleeping {}"
+ + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
+ sleep(electionWaitSleepTime);
+
+ logger.debug("testLocking2: Waking up and invoking startTransaction "
+ + "on re-promoted PDP= {}, designated= {}",
+ thisPdpId, conn.getPdp(thisPdpId).isDesignated());
+ try {
+ droolsPdpIntegrityMonitor.startTransaction();
+ droolsPdpIntegrityMonitor.endTransaction();
+ logger.debug("testLocking2: As expected, transaction successful");
+ } catch (AdministrativeStateException e) {
+ logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
+ assertTrue(false);
+ } catch (StandbyStatusException e) {
+ logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
+ assertTrue(false);
+ } catch (Exception e) {
+ logger.error("testLocking2: Unexpectedly caught Exception, ", e);
+ assertTrue(false);
+ }
+
+ logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
+ boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
+ assertTrue(standbyPdpDesignated == false);
+
+ logger.debug("\n\ntestLocking2: Exiting\n\n");
+ sleep(interruptRecoveryTime);
+ }
+
+ private void sleep(long sleepms) throws InterruptedException {
+ Thread.sleep(sleepms);
+ }
}
diff --git a/feature-active-standby-management/src/test/resources/META-INF/persistence.xml b/feature-active-standby-management/src/test/resources/META-INF/persistence.xml
index ff6ac58a..549f01de 100644
--- a/feature-active-standby-management/src/test/resources/META-INF/persistence.xml
+++ b/feature-active-standby-management/src/test/resources/META-INF/persistence.xml
@@ -3,7 +3,7 @@
============LICENSE_START=======================================================
feature-active-standby-management
================================================================================
- Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -20,28 +20,47 @@
-->
<persistence version="2.1"
- xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="junitDroolsPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class>
- <properties>
- <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
- <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
- <property name="javax.persistence.schema-generation.scripts.create-target" value="./sql/generatedCreateDrools.ddl"/>
- <property name="javax.persistence.schema-generation.scripts.drop-target" value="./sql/generatedDropDrools.ddl"/>
+ xmlns="http://xmlns.jcp.org/xml/ns/persistence"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
+ <persistence-unit name="junitDroolsPU"
+ transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+ <class>org.onap.policy.drools.activestandby.DroolsPdpEntity</class>
+ <properties>
+ <property
+ name="javax.persistence.schema-generation.database.action"
+ value="drop-and-create" />
+ <property
+ name="javax.persistence.schema-generation.scripts.action"
+ value="drop-and-create" />
+ <property
+ name="javax.persistence.schema-generation.scripts.create-target"
+ value="./sql/generatedCreateDrools.ddl" />
+ <property
+ name="javax.persistence.schema-generation.scripts.drop-target"
+ value="./sql/generatedDropDrools.ddl" />
</properties>
- </persistence-unit>
- <persistence-unit name="junitXacmlPU" transaction-type="RESOURCE_LOCAL">
- <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
- <class>org.onap.policy.common.im.jpa.StateManagementEntity</class>
- <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class>
- <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class>
- <properties>
- <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
- <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create"/>
- <property name="javax.persistence.schema-generation.scripts.create-target" value="./sql/generatedCreateXacml.ddl"/>
- <property name="javax.persistence.schema-generation.scripts.drop-target" value="./sql/generatedDropXacml.ddl"/>
+ </persistence-unit>
+ <persistence-unit name="junitXacmlPU"
+ transaction-type="RESOURCE_LOCAL">
+ <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
+ <class>org.onap.policy.common.im.jpa.StateManagementEntity</class>
+ <class>org.onap.policy.common.im.jpa.ForwardProgressEntity</class>
+ <class>org.onap.policy.common.im.jpa.ResourceRegistrationEntity</class>
+ <properties>
+ <property
+ name="javax.persistence.schema-generation.database.action"
+ value="drop-and-create" />
+ <property
+ name="javax.persistence.schema-generation.scripts.action"
+ value="drop-and-create" />
+ <property
+ name="javax.persistence.schema-generation.scripts.create-target"
+ value="./sql/generatedCreateXacml.ddl" />
+ <property
+ name="javax.persistence.schema-generation.scripts.drop-target"
+ value="./sql/generatedDropXacml.ddl" />
</properties>
- </persistence-unit>
+ </persistence-unit>
</persistence>
diff --git a/feature-active-standby-management/src/test/resources/logback-test.xml b/feature-active-standby-management/src/test/resources/logback-test.xml
index a36bdb16..583b9667 100644
--- a/feature-active-standby-management/src/test/resources/logback-test.xml
+++ b/feature-active-standby-management/src/test/resources/logback-test.xml
@@ -2,7 +2,7 @@
============LICENSE_START=======================================================
feature-state-management
================================================================================
- Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
================================================================================
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -22,25 +22,28 @@
<configuration>
- <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
- <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
- <Pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n
- </Pattern>
- </encoder>
- </appender>
- <appender name="FILE" class="ch.qos.logback.core.FileAppender">
- <file>logs/debug.log</file>
- <encoder>
- <Pattern>
- %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n
- </Pattern>
- </encoder>
- </appender>
-
- <root level="debug">
- <appender-ref ref="STDOUT" />
- <appender-ref ref="FILE" />
- </root>
+ <appender name="STDOUT"
+ class="ch.qos.logback.core.ConsoleAppender">
+ <encoder
+ class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+ <Pattern>
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n
+ </Pattern>
+ </encoder>
+ </appender>
+ <appender name="FILE"
+ class="ch.qos.logback.core.FileAppender">
+ <file>logs/debug.log</file>
+ <encoder>
+ <Pattern>
+ %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}.%M\(%line\) - %msg%n
+ </Pattern>
+ </encoder>
+ </appender>
+
+ <root level="debug">
+ <appender-ref ref="STDOUT" />
+ <appender-ref ref="FILE" />
+ </root>
</configuration>