diff options
Diffstat (limited to 'feature-active-standby-management/src/main/java')
11 files changed, 2796 insertions, 0 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 new file mode 100644 index 00000000..d40a9e0f --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyFeature.java @@ -0,0 +1,240 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.io.IOException; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +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; + +/** + * If this feature is supported, there is a single instance of it. + * It adds persistence to Drools sessions, but it is also intertwined with + * 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 + * '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()); + } + + + + //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) { + System.out.println("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()); + } + if(myPdp != null){ + String site_name = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.SITE_NAME); + if (site_name == null) { + site_name = ""; + }else{ + site_name = site_name.trim(); + } + myPdp.setSiteName(site_name); + } + if(electionHandler == null){ + electionHandler = new DroolsPdpsElectionHandler(conn,myPdp); + } + } + System.out.println("\n\nThis controller is a standby, waiting to be chosen as primary...\n\n"); + 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<String, Object>(); + 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); + DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emf); + + return conn; + } + + /** + * {@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 new file mode 100644 index 00000000..6e26334b --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ActiveStandbyProperties.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.util.Properties; +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"; + + 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; + /* + * 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 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 new file mode 100644 index 00000000..a440a7e1 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdp.java @@ -0,0 +1,39 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +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); +} 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 new file mode 100644 index 00000000..ec1ce579 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpEntity.java @@ -0,0 +1,137 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.io.Serializable; +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; + +import org.onap.policy.drools.activestandby.DroolsPdpObject; + +@Entity +//@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") +}) +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; + } + +} 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 new file mode 100644 index 00000000..141d5857 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpImpl.java @@ -0,0 +1,92 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +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; + + } +} 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 new file mode 100644 index 00000000..e434c834 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpObject.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +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; + } + } + 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.equals("")){ + 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 new file mode 100644 index 00000000..d0d33f0f --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsConnector.java @@ -0,0 +1,63 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +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); + + +} 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 new file mode 100644 index 00000000..6edf11f8 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/DroolsPdpsElectionHandler.java @@ -0,0 +1,1076 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import org.onap.policy.common.im.StateManagement; +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 final static Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class); + private DroolsPdpsConnector pdpsConnector; + private Object pdpsConnectorLock = new Object(); + private Object checkUpdateWorkerLock = new Object(); + 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 updateWorkerLastRunDate; + private Date waitTimerLastRunDate; + private int pdpCheckInterval; + private int pdpUpdateInterval; + private volatile boolean isDesignated; + + private String pdpdNowActive; + private String pdpdLastActive; + + private StateManagementFeatureAPI stateManagementFeature; + + public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp){ + 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", 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", 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 heartbeat to start in 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); + updateWorkerLastRunDate = new Date(nowMs + 100); + + // 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()); + } + } + + /* + * 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"); + } + + // just here initially so code still works + if (pdpsConnector == null) { + waitTimerLastRunDate = new Date(); + if(logger.isDebugEnabled()){ + logger.debug("DesignatedWaiter.run (pdpsConnector==null) waitTimerLastRunDate = {}", waitTimerLastRunDate); + } + + return; + } + + synchronized (designationWaiterLock) { + + if(logger.isDebugEnabled()){ + logger.debug + ("DesignatedWaiter.run: Entering synchronized block"); + } + + checkUpdateWorkerTimer(); + + //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. + ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>(); + + 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.getMessage()); + } + }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.getMessage()); + } + } 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.getMessage()); + } + + } + 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.getMessage()); + } + + } + } + 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.getMessage()); + } + }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.getMessage()); + } + } + } + } + + + } // 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); + } + + 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.getMessage()); + 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.getMessage()); + } + + } + waitTimerLastRunDate = new Date(); + if(logger.isDebugEnabled()){ + logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) " + + "waitTimerLastRunDate = " + waitTimerLastRunDate); + } + + 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; + + }catch(Exception e){ + logger.error("DesignatedWaiter.run caught an unexpected exception: ", e); + } + } // end run + } + + public ArrayList<DroolsPdp> santizeDesignatedList(ArrayList<DroolsPdp> listOfDesignated){ + + boolean containsDesignated = false; + boolean containsHotStandby = false; + ArrayList<DroolsPdp> listForRemoval = new ArrayList<DroolsPdp>(); + 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); + containsHotStandby = false; + } + return listOfDesignated; + } + + public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, ArrayList<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(ArrayList<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.getMessage()); + } + } + } //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(); + synchronized(pdpsConnectorLock){ + + myPdp.setUpdatedDate(new Date()); + /* + Redundant with DesignationWaiter and this updates the date every + cycle instead of just when the state changes. + if(myPdp.isDesignated()){ + myPdp.setDesignatedDate(new Date()); + } + */ + pdpsConnector.update(myPdp); + + Date tmpDate = new Date(); + if(logger.isDebugEnabled()){ + logger.debug("TimerUpdateClass.run: updateWorkerLastRunDate = {}", tmpDate); + } + + updateWorkerLastRunDate = tmpDate; + } + if(logger.isDebugEnabled()){ + logger.debug("TimerUpdateClass.run.exit"); + } + }catch(Exception e){ + logger.error("TimerUpdateClass.run caught an unexpected exception: ", e); + } + } + } + @Override + public void checkThreadStatus() { + checkUpdateWorkerTimer(); + checkWaitTimer(); + } + + private void checkUpdateWorkerTimer(){ + synchronized(checkUpdateWorkerLock){ + try{ + if(logger.isDebugEnabled()){ + logger.debug("checkUpdateWorkerTimer: entry"); + } + Date now = new Date(); + long nowMs = now.getTime(); + long updateWorkerMs = updateWorkerLastRunDate.getTime(); + //give it 2 second cushion + if((nowMs - updateWorkerMs) > pdpCheckInterval + 2000){ + logger.error("checkUpdateWorkerTimer: nowMs - updateWorkerMs = {} " + + ", exceeds pdpCheckInterval + 2000 = {} " + + "Will reschedule updateWorker timer",(nowMs - updateWorkerMs), (pdpCheckInterval + 2000)); + + try{ + updateWorker.cancel(); + // Recalculate the time because this is a synchronized section and the thread could have + // been blocked. + now = new Date(); + nowMs = now.getTime(); + updateWorker = new Timer(); + // reset the updateWorkerLastRunDate + updateWorkerLastRunDate = new Date(nowMs + 100); + //execute the first time in 100 ms + updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); + if(logger.isDebugEnabled()){ + logger.debug("checkUpdateWorkerTimer: Scheduling updateWorker timer to start in 100 ms "); + } + }catch(Exception e){ + logger.error("checkUpdateWorkerTimer: Caught unexpected Exception: ", e); + // Recalculate the time because this is a synchronized section and the thread could have + // been blocked. + now = new Date(); + nowMs = now.getTime(); + updateWorker = new Timer(); + updateWorkerLastRunDate = new Date(nowMs + 100); + updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval); + if(logger.isDebugEnabled()){ + logger.debug("checkUpdateWorkerTimer: Attempting to schedule updateWorker timer in 100 ms"); + } + } + + } + if(logger.isDebugEnabled()){ + logger.debug("checkUpdateWorkerTimer: exit"); + } + }catch(Exception e){ + logger.error("checkUpdateWorkerTimer: caught unexpected exception: ", e); + } + } + } + + 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 2 times leeway + if((nowMs - waitTimerMs) > 2*pdpUpdateInterval){ + logger.error("checkWaitTimer: nowMs - waitTimerMs = {}" + + ", exceeds pdpUpdateInterval + 2000 = {}" + + "Will reschedule waitTimer timer", (nowMs - waitTimerMs), (2*pdpUpdateInterval)); + + try{ + // Recalculate since the thread could have been stalled on the synchronize() + nowMs = (new Date()).getTime(); + // Time to the start of the next pdpUpdateInterval multiple + long startMs = getDWaiterStartMs(); + waitTimer.cancel(); + designationWaiter = new DesignationWaiter(); + waitTimer = new Timer(); + waitTimerLastRunDate = new Date(nowMs + startMs); + waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval); + if(logger.isDebugEnabled()){ + logger.debug("checkWaitTimer: Scheduling waitTimer timer to start in {} ms", startMs); + } + }catch(Exception e){ + logger.error("checkWaitTimer: Caught unexpected Exception: ", e); + // Recalculate since the thread could have been stalled on the synchronize() + nowMs = (new Date()).getTime(); + // Time to the start of the next pdpUpdateInterval multiple + long startMs = getDWaiterStartMs(); + designationWaiter = new DesignationWaiter(); + waitTimer = new Timer(); + waitTimerLastRunDate = new Date(nowMs + startMs); + waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval); + if(logger.isDebugEnabled()){ + logger.debug("checkWaitTimer: Scheduling waitTimer timer in {} ms", startMs); + } + } + + } + 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 new file mode 100644 index 00000000..0d931acc --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/JpaDroolsPdpsConnector.java @@ -0,0 +1,636 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +import java.util.Collection; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.FlushModeType; +import javax.persistence.LockModeType; +import javax.persistence.Query; + +import org.slf4j.Logger; +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<DroolsPdp>(); + 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) { + if (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 new file mode 100644 index 00000000..ce62bf89 --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/PMStandbyStateChangeNotifier.java @@ -0,0 +1,345 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +/* + * Per MultiSite_v1-10.ppt: + * + * Extends the StateChangeNotifier class and overwrites the abstract handleStateChange() method to get state changes + * and do the following: + * + * When the Standby Status changes (from providingservice) to hotstandby or coldstandby, + * the Active/Standby selection algorithm must stand down if the PDP-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. + * + * When the Standby Status changes from (hotstandby) to coldstandby, the Active/Standby algorithm must NOT assume + * the active/lead role. + * + * When the Standby Status changes (from coldstandby or providingservice) to hotstandby, + * the Active/Standby algorithm may assume the active/lead role if the active/lead fails. + * + * When the Standby Status changes to providingservice (from hotstandby or coldstandby) call unlock on all + * engines in the engine management layer. + */ +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +import org.onap.policy.common.im.StateChangeNotifier; +import org.onap.policy.common.im.StateManagement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.onap.policy.drools.system.PolicyEngine; + +/* + * 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(). 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. + * 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 + private static final Logger logger = LoggerFactory.getLogger(PMStandbyStateChangeNotifier.class); + private Timer delayActivateTimer; + private int pdpUpdateInterval; + private boolean isWaitingForActivation; + private long startTimeWaitingForActivationMs; + private long waitInterval; + private boolean isNowActivating; + private String previousStandbyStatus; + public static String NONE = "none"; + public static String UNSUPPORTED = "unsupported"; + public static String HOTSTANDBY_OR_COLDSTANDBY = "hotstandby_or_coldstandby"; + + public PMStandbyStateChangeNotifier(){ + pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL)); + isWaitingForActivation = false; + startTimeWaitingForActivationMs = new Date().getTime(); + //delay the activate so the DesignatedWaiter can run twice - give it an extra 2 seconds + waitInterval = 2*pdpUpdateInterval + 2000; + isNowActivating=false; + previousStandbyStatus = PMStandbyStateChangeNotifier.NONE; + } + + @Override + public void handleStateChange() { + /* + * A note on synchronization: This method is not synchronized because the caller, stateManagememt, + * has synchronize all of its methods. Only one stateManagement operation can occur at a time. Thus, + * only one handleStateChange() call will ever be made at a time. + */ + if(logger.isInfoEnabled()){ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: Entering, message={}, standbyStatus={}", + super.getMessage(), super.getStateManagement().getStandbyStatus()); + } + } + String standbyStatus = super.getStateManagement().getStandbyStatus(); + String pdpId = ActiveStandbyProperties + .getProperty(ActiveStandbyProperties.NODE_NAME); + + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: previousStandbyStatus = {}" + + "; standbyStatus = {}", previousStandbyStatus, standbyStatus); + } + + if (standbyStatus == null || standbyStatus.equals(StateManagement.NULL_VALUE)) { + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: standbyStatus is null; standing down PDP={}", pdpId); + } + if(previousStandbyStatus.equals(StateManagement.NULL_VALUE)){ + //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); + } + return; + } + isWaitingForActivation = false; + try{ + try{ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: null: cancelling delayActivationTimer."); + } + delayActivateTimer.cancel(); + }catch(Exception e){ + if(logger.isInfoEnabled()){ + logger.info("handleStateChange: null no delayActivationTimer existed.", e); + } + //If you end of here, there was no active timer + } + //Only want to lock the endpoints, not the controllers. + PolicyEngine.manager.deactivate(); + //The operation was fully successful, but you cannot assign it a real null value + //because later we might try to execute previousStandbyStatus.equals() and get + //a null pointer exception. + previousStandbyStatus = StateManagement.NULL_VALUE; + }catch(Exception e){ + logger.warn("handleStateChange: standbyStatus == null caught exception: ", e); + } + } else if (standbyStatus.equals(StateManagement.HOT_STANDBY) || standbyStatus.equals(StateManagement.COLD_STANDBY)) { + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: standbyStatus={}; standing down PDP={}", standbyStatus, pdpId); + } + if(previousStandbyStatus.equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY)){ + //We were just here and did this successfully + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: Is returning because standbyStatus is {}" + + " and was previously {}; PDP= {}", standbyStatus, previousStandbyStatus, pdpId); + } + return; + } + isWaitingForActivation = false; + try{ + try{ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: HOT_STNDBY || COLD_STANDBY: cancelling delayActivationTimer."); + } + delayActivateTimer.cancel(); + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: HOT_STANDBY || COLD_STANDBY no delayActivationTimer existed.", e); + } + //If you end of here, there was no active timer + } + //Only want to lock the endpoints, not the controllers. + PolicyEngine.manager.deactivate(); + //The operation was fully successful + previousStandbyStatus = PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY; + }catch(Exception e){ + logger.warn("handleStateChange: standbyStatus = {} caught exception: {}", standbyStatus, e.getMessage(), e); + } + + } else if (standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) { + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: standbyStatus= {} " + + "scheduling activation of PDP={}",standbyStatus, pdpId); + } + if(previousStandbyStatus.equals(StateManagement.PROVIDING_SERVICE)){ + //We were just here and did this successfully + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: Is returning because standbyStatus is {}" + + "and was previously {}; PDP={}", standbyStatus, previousStandbyStatus, pdpId); + } + return; + } + try{ + //UnLock all the endpoints + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: standbyStatus={}; controllers must be unlocked.",standbyStatus ); + } + /* + * Only endpoints should be unlocked. Controllers have not been locked. + * Because, sometimes, it is possible for more than one PDP-D to become active (race conditions) + * we need to delay the activation of the topic endpoint interfaces to give the election algorithm + * time to resolve the conflict. + */ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE isWaitingForActivation= {}", isWaitingForActivation); + } + + //Delay activation for 2*pdpUpdateInterval+2000 ms in case of an election handler conflict. + //You could have multiple election handlers thinking they can take over. + + // First let's check that the timer has not died + if(isWaitingForActivation){ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE isWaitingForActivation = {}", isWaitingForActivation); + } + long now = new Date().getTime(); + long waitTimeMs = now - startTimeWaitingForActivationMs; + 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); + } + //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"); + } + // This will assure the timer is cancelled and rescheduled. + isWaitingForActivation = false; + } + } + + } + + if(!isWaitingForActivation){ + try{ + //Just in case there is an old timer hanging around + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE cancelling delayActivationTimer."); + } + delayActivateTimer.cancel(); + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE no delayActivationTimer existed."); + } + //If you end of here, there was no active timer + } + delayActivateTimer = new Timer(); + //delay the activate so the DesignatedWaiter can run twice + delayActivateTimer.schedule(new DelayActivateClass(), waitInterval); + isWaitingForActivation = true; + startTimeWaitingForActivationMs = new Date().getTime(); + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE scheduling delayActivationTimer in {} ms", waitInterval); + } + }else{ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: PROVIDING_SERVICE delayActivationTimer is waiting for activation."); + } + } + + }catch(Exception e){ + logger.warn("handleStateChange: PROVIDING_SERVICE standbyStatus == providingservice caught exception: ", e); + } + + } else { + logger.error("handleStateChange: Unsupported standbyStatus={}; standing down PDP={}", standbyStatus, pdpId); + if(previousStandbyStatus.equals(PMStandbyStateChangeNotifier.UNSUPPORTED)){ + //We were just here and did this successfully + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: Is returning because standbyStatus is " + + "UNSUPPORTED and was previously {}; PDP={}", previousStandbyStatus, pdpId); + } + return; + } + //Only want to lock the endpoints, not the controllers. + isWaitingForActivation = false; + try{ + try{ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: unsupported standbystatus: cancelling delayActivationTimer."); + } + delayActivateTimer.cancel(); + }catch(Exception e){ + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: unsupported standbystatus: no delayActivationTimer existed.", e); + } + //If you end of here, there was no active timer + } + PolicyEngine.manager.deactivate(); + //We know the standbystatus is unsupported + previousStandbyStatus = PMStandbyStateChangeNotifier.UNSUPPORTED; + }catch(Exception e){ + logger.warn("handleStateChange: Unsupported standbyStatus = {} " + + "caught exception: {} ",standbyStatus, e.getMessage(), e); + } + } + if(logger.isDebugEnabled()){ + logger.debug("handleStateChange: Exiting"); + } + } + + private class DelayActivateClass extends TimerTask{ + + private Object delayActivateLock = new Object(); + + + @Override + public void run() { + isNowActivating = true; + try{ + if(logger.isDebugEnabled()){ + logger.debug("DelayActivateClass.run: entry"); + } + synchronized(delayActivateLock){ + PolicyEngine.manager.activate(); + // The state change fully succeeded + previousStandbyStatus = StateManagement.PROVIDING_SERVICE; + // We want to set this to false here because the activate call can take a while + isWaitingForActivation = false; + isNowActivating = false; + } + if(logger.isDebugEnabled()){ + logger.debug("DelayActivateClass.run.exit"); + } + }catch(Exception e){ + isWaitingForActivation = false; + isNowActivating = false; + logger.warn("DelayActivateClass.run: caught an unexpected exception " + + "calling PolicyEngine.manager.activate: ", e); + } + } + } + + public String getPreviousStandbyStatus(){ + return previousStandbyStatus; + } +} 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 new file mode 100644 index 00000000..db848ebb --- /dev/null +++ b/feature-active-standby-management/src/main/java/org/onap/policy/drools/activestandby/ThreadRunningChecker.java @@ -0,0 +1,26 @@ +/*- + * ============LICENSE_START======================================================= + * feature-active-standby-management + * ================================================================================ + * Copyright (C) 2017 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.drools.activestandby; + +public interface ThreadRunningChecker { + public void checkThreadStatus(); + +} |