2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.drools.controller.test;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileInputStream;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.Properties;
31 import javax.persistence.EntityManager;
32 import javax.persistence.EntityManagerFactory;
33 import javax.persistence.EntityTransaction;
34 import javax.persistence.Persistence;
36 import org.apache.commons.lang3.time.DateUtils;
37 import org.junit.After;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Ignore;
42 import org.junit.Test;
43 import org.onap.policy.common.im.AdministrativeStateException;
44 import org.onap.policy.common.im.IntegrityMonitor;
45 import org.onap.policy.common.im.StandbyStatusException;
46 import org.onap.policy.common.im.StateManagement;
47 import org.onap.policy.drools.activestandby.ActiveStandbyFeatureAPI;
48 import org.onap.policy.drools.activestandby.ActiveStandbyProperties;
49 import org.onap.policy.drools.activestandby.DroolsPdp;
50 import org.onap.policy.drools.activestandby.DroolsPdpEntity;
51 import org.onap.policy.drools.activestandby.DroolsPdpImpl;
52 import org.onap.policy.drools.activestandby.DroolsPdpsConnector;
53 import org.onap.policy.drools.activestandby.DroolsPdpsElectionHandler;
54 import org.onap.policy.drools.activestandby.JpaDroolsPdpsConnector;
55 import org.onap.policy.drools.activestandby.PMStandbyStateChangeNotifier;
56 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
57 import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * All JUnits are designed to run in the local development environment
63 * where they have write privileges and can execute time-sensitive
66 * These tests can be run as JUnits, but there is some issue with running them
67 * as part of a "mvn install" build. Also, they take a very long time to run
68 * due to many real time breaks. Consequently, they are marked as @Ignore and
69 * only run from the desktop.
72 public class StandbyStateManagementTest {
73 private static final Logger logger = LoggerFactory.getLogger(StandbyStateManagementTest.class);
75 * Currently, the DroolsPdpsElectionHandler.DesignationWaiter is invoked every 1 seconds, starting
76 * at the start of the next multiple of pdpUpdateInterval, but with a minimum of 5 sec cushion
77 * to ensure that we wait for the DesignationWaiter to do its job, before
78 * checking the results. Add a few seconds for safety
81 long sleepTime = 10000;
84 * DroolsPdpsElectionHandler runs every 1 seconds, so a 6 second sleep should be
85 * plenty to ensure it has time to re-promote this PDP.
88 long electionWaitSleepTime = 6000;
91 * Sleep 1 seconds after each test to allow interrupt (shutdown) recovery.
94 long interruptRecoveryTime = 5000;
96 private static EntityManagerFactory emfx;
97 private static EntityManagerFactory emfd;
98 private static EntityManager emx;
99 private static EntityManager emd;
100 private static EntityTransaction et;
102 private final String configDir = "src/test/resources";
105 * See the IntegrityMonitor.getJmxUrl() method for the rationale behind this jmx related processing.
109 public static void setUpClass() throws Exception {
111 String userDir = System.getProperty("user.dir");
112 logger.debug("setUpClass: userDir={}", userDir);
113 System.setProperty("com.sun.management.jmxremote.port", "9980");
114 System.setProperty("com.sun.management.jmxremote.authenticate","false");
119 public static void tearDownClass() throws Exception {
123 public void setUp() throws Exception {
124 //Create teh data access for xaml db
125 Properties stateManagementProperties = new Properties();
126 stateManagementProperties.load(new FileInputStream(new File(
127 "src/test/resources/feature-state-management.properties")));
129 emfx = Persistence.createEntityManagerFactory("junitXacmlPU", stateManagementProperties);
131 // Create an entity manager to use the DB
132 emx = emfx.createEntityManager();
134 //Create the data access for drools db
135 Properties activeStandbyProperties = new Properties();
136 activeStandbyProperties.load(new FileInputStream(new File(
137 "src/test/resources/feature-active-standby-management.properties")));
139 emfd = Persistence.createEntityManagerFactory("junitDroolsPU", activeStandbyProperties);
141 // Create an entity manager to use the DB
142 emd = emfd.createEntityManager();
146 public void tearDown() throws Exception {
150 public void cleanXacmlDb(){
151 et = emx.getTransaction();
154 // Make sure we leave the DB clean
155 emx.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
156 emx.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
157 emx.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();
162 public void cleanDroolsDb(){
163 et = emd.getTransaction();
166 // Make sure we leave the DB clean
167 emd.createQuery("DELETE FROM DroolsPdpEntity").executeUpdate();
173 * These JUnit tests must be run one at a time in an eclipse environment
174 * by right-clicking StandbyStateManagementTest and selecting
175 * "Run As" -> "JUnit Test".
177 * They will run successfully when you run all of them under runAllTests(),
178 * however, you will get all sorts of non-fatal errors in the log and on the
179 * console that result from overlapping threads that are not terminated at the
180 * end of each test. The problem is that the JUnit environment does not terminate
181 * all the test threads between tests. This is true even if you break each JUnit
182 * into a separate file. Consequently, all the tests would have to be refactored
183 * so all test object initializations are coordinated. In other words, you
184 * retrieve the ActiveStandbyFeature instance and other class instances only once
185 * at the beginning of the JUnits and then reuse them throughout the tests.
186 * Initialization of the state of the objects is pretty straight forward as it
187 * just amounts to manipulating the entries in StateManagementEntity and
188 * DroolsPdpEntity tables. However, some thought needs to be given to how to
189 * "pause" the processing in ActiveStandbyFeature class. I think we could "pause"
190 * it by calling globalInit() which will, I think, restart it. So long as it
191 * does not create a new instance, it will force it to go through an initialization
192 * cycle which includes a "pause" at the beginning of proecessing. We just must
193 * be sure it does not create another instance - which may mean we need to add
194 * a factory interface instead of calling the constructor directly.
200 public void runAllTests() throws Exception {
206 testPMStandbyStateChangeNotifier();
207 testSanitizeDesignatedList();
208 testComputeMostRecentPrimary();
209 testComputeDesignatedPdp();
214 public void testPMStandbyStateChangeNotifier() throws Exception {
215 logger.debug("\n\ntestPMStandbyStateChangeNotifier: Entering\n\n");
218 logger.debug("testPMStandbyStateChangeNotifier: Reading activeStandbyProperties");
220 Properties activeStandbyProperties = new Properties();
221 activeStandbyProperties.load(new FileInputStream(new File(
222 configDir + "/feature-active-standby-management.properties")));
223 ActiveStandbyProperties.initProperties(activeStandbyProperties);
224 String thisPdpId = ActiveStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
226 logger.debug("testPMStandbyStateChangeNotifier: Getting StateManagementFeatureAPI");
228 StateManagementFeatureAPI sm = null;
229 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
231 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
233 logger.debug("testPMStandbyStateChangeNotifier stateManagementFeature.getResourceName(): {}", sm.getResourceName());
237 logger.error("testPMStandbyStateChangeNotifier failed to initialize. "
238 + "Unable to get instance of StateManagementFeatureAPI "
239 + "with resourceID: {}", thisPdpId);
240 logger.debug("testPMStandbyStateChangeNotifier failed to initialize. "
241 + "Unable to get instance of StateManagementFeatureAPI "
242 + "with resourceID: {}", thisPdpId);
245 //Create an instance of the Observer
246 PMStandbyStateChangeNotifier pmNotifier = new PMStandbyStateChangeNotifier();
248 //Register the PMStandbyStateChangeNotifier Observer
249 sm.addObserver(pmNotifier);
251 //At this point the standbystatus = 'null'
253 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
256 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.NULL_VALUE));
258 //Adding standbystatus=hotstandby
260 System.out.println(pmNotifier.getPreviousStandbyStatus());
261 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
263 //Now making standbystatus=coldstandby
265 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
267 //standbystatus = hotstandby
269 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
271 //standbystatus = providingservice
273 //The previousStandbyStatus is not updated until after the delay activation expires
274 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
276 //Sleep long enough for the delayActivationTimer to run
278 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
280 //standbystatus = providingservice
282 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(StateManagement.PROVIDING_SERVICE));
284 //standbystatus = coldstandby
286 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
288 //standbystatus = hotstandby
290 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
292 //standbystatus = hotstandby
294 assertTrue(pmNotifier.getPreviousStandbyStatus().equals(PMStandbyStateChangeNotifier.HOTSTANDBY_OR_COLDSTANDBY));
299 public void testSanitizeDesignatedList() throws Exception {
301 logger.debug("\n\ntestSanitizeDesignatedList: Entering\n\n");
303 // Get a DroolsPdpsConnector
305 logger.debug("testSanitizeDesignatedList: Reading activeStandbyProperties");
306 Properties activeStandbyProperties = new Properties();
307 activeStandbyProperties.load(new FileInputStream(new File(
308 configDir + "/feature-active-standby-management.properties")));
309 String thisPdpId = activeStandbyProperties
310 .getProperty(ActiveStandbyProperties.NODE_NAME);
312 logger.debug("testSanitizeDesignatedList: Creating emfDrools");
313 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
314 "junitDroolsPU", activeStandbyProperties);
316 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
318 // Create 4 pdpd all not designated
320 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
321 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
322 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
323 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
325 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
326 listOfDesignated.add(pdp1);
327 listOfDesignated.add(pdp2);
328 listOfDesignated.add(pdp3);
329 listOfDesignated.add(pdp4);
331 // Now we want to create a StateManagementFeature and initialize it. It will be
332 // discovered by the ActiveStandbyFeature when the election handler initializes.
334 StateManagementFeatureAPI stateManagementFeature = null;
335 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
337 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
338 stateManagementFeature = feature;
339 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
342 if(stateManagementFeature == null){
343 logger.error("testColdStandby failed to initialize. "
344 + "Unable to get instance of StateManagementFeatureAPI "
345 + "with resourceID: {}", thisPdpId);
346 logger.debug("testColdStandby failed to initialize. "
347 + "Unable to get instance of StateManagementFeatureAPI "
348 + "with resourceID: {}", thisPdpId);
352 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
354 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
356 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size = {}\n\n",listOfDesignated.size());
358 assertTrue(listOfDesignated.size()==4);
360 // Now make 2 designated
362 pdp1.setDesignated(true);
363 pdp2.setDesignated(true);
365 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
367 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after 2 designated = {}\n\n", listOfDesignated.size());
369 assertTrue(listOfDesignated.size()==2);
370 assertTrue(listOfDesignated.contains(pdp1));
371 assertTrue(listOfDesignated.contains(pdp2));
374 // Now all are designated. But, we have to add back the previously non-designated nodes
376 pdp3.setDesignated(true);
377 pdp4.setDesignated(true);
378 listOfDesignated.add(pdp3);
379 listOfDesignated.add(pdp4);
381 listOfDesignated = droolsPdpsElectionHandler.santizeDesignatedList(listOfDesignated);
383 logger.debug("\n\ntestSanitizeDesignatedList: listOfDesignated.size after all designated = {}\n\n", listOfDesignated.size());
385 assertTrue(listOfDesignated.size()==4);
392 public void testComputeMostRecentPrimary() throws Exception {
394 logger.debug("\n\ntestComputeMostRecentPrimary: Entering\n\n");
396 logger.debug("testComputeMostRecentPrimary: Reading activeStandbyProperties");
397 Properties activeStandbyProperties = new Properties();
398 activeStandbyProperties.load(new FileInputStream(new File(
399 configDir + "/feature-active-standby-management.properties")));
400 String thisPdpId = activeStandbyProperties
401 .getProperty(ActiveStandbyProperties.NODE_NAME);
403 logger.debug("testComputeMostRecentPrimary: Creating emfDrools");
404 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
405 "junitDroolsPU", activeStandbyProperties);
407 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
410 // Create 4 pdpd all not designated
413 long designatedDateMS = new Date().getTime();
414 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
415 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
417 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
419 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
421 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
422 pdp3.setDesignatedDate(new Date(designatedDateMS - 1));
424 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
426 pdp4.setDesignatedDate(new Date(designatedDateMS));
428 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
429 listOfAllPdps.add(pdp1);
430 listOfAllPdps.add(pdp2);
431 listOfAllPdps.add(pdp3);
432 listOfAllPdps.add(pdp4);
435 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
436 listOfDesignated.add(pdp1);
437 listOfDesignated.add(pdp2);
438 listOfDesignated.add(pdp3);
439 listOfDesignated.add(pdp4);
441 // Because the way we sanitize the listOfDesignated, it will always contain all hot standby
442 // or all designated members.
444 // Now we want to create a StateManagementFeature and initialize it. It will be
445 // discovered by the ActiveStandbyFeature when the election handler initializes.
447 StateManagementFeatureAPI stateManagementFeature = null;
448 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
450 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
451 stateManagementFeature = feature;
452 logger.debug("testComputeMostRecentPrimary stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
455 if(stateManagementFeature == null){
456 logger.error("testComputeMostRecentPrimary failed to initialize. "
457 + "Unable to get instance of StateManagementFeatureAPI "
458 + "with resourceID: {}", thisPdpId);
459 logger.debug("testComputeMostRecentPrimary failed to initialize. "
460 + "Unable to get instance of StateManagementFeatureAPI "
461 + "with resourceID: {}", thisPdpId);
464 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
466 DroolsPdp mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
468 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
471 // If all of the pdps are included in the listOfDesignated and none are designated, it will choose
472 // the one which has the most recent designated date.
475 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
478 // Now let's designate all of those on the listOfDesignated. It will choose the first one designated
481 pdp1.setDesignated(true);
482 pdp2.setDesignated(true);
483 pdp3.setDesignated(true);
484 pdp4.setDesignated(true);
486 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
488 logger.debug("\n\ntestComputeMostRecentPrimary: All designated all on list, mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
491 // If all of the pdps are included in the listOfDesignated and all are designated, it will choose
492 // the one which was designated first
495 assertTrue(mostRecentPrimary.getPdpId().equals("pdp2"));
498 // Now we will designate only 2 and put just them in the listOfDesignated. The algorithm will now
499 // look for the most recently designated pdp which is not currently designated.
502 pdp3.setDesignated(false);
503 pdp4.setDesignated(false);
505 listOfDesignated.remove(pdp3);
506 listOfDesignated.remove(pdp4);
508 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
510 logger.debug("\n\ntestComputeMostRecentPrimary: mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
512 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
516 // Now we will have none designated and put two of them in the listOfDesignated. The algorithm will now
517 // look for the most recently designated pdp regardless of whether it is currently marked as designated.
520 pdp1.setDesignated(false);
521 pdp2.setDesignated(false);
523 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
525 logger.debug("\n\ntestComputeMostRecentPrimary: 2 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
527 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
530 // If we have only one pdp on in the listOfDesignated, the most recently designated pdp will be chosen, regardless
531 // of its designation status
534 listOfDesignated.remove(pdp1);
536 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
538 logger.debug("\n\ntestComputeMostRecentPrimary: 1 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
540 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
543 // Finally, if none are on the listOfDesignated, it will again choose the most recently designated pdp.
546 listOfDesignated.remove(pdp2);
548 mostRecentPrimary = droolsPdpsElectionHandler.computeMostRecentPrimary(listOfAllPdps, listOfDesignated);
550 logger.debug("\n\ntestComputeMostRecentPrimary: 0 on list mostRecentPrimary.getPdpId() = {}\n\n", mostRecentPrimary.getPdpId());
552 assertTrue(mostRecentPrimary.getPdpId().equals("pdp4"));
558 public void testComputeDesignatedPdp() throws Exception{
560 logger.debug("\n\ntestComputeDesignatedPdp: Entering\n\n");
562 logger.debug("testComputeDesignatedPdp: Reading activeStandbyProperties");
563 Properties activeStandbyProperties = new Properties();
564 activeStandbyProperties.load(new FileInputStream(new File(
565 configDir + "/feature-active-standby-management.properties")));
566 String thisPdpId = activeStandbyProperties
567 .getProperty(ActiveStandbyProperties.NODE_NAME);
570 logger.debug("testComputeDesignatedPdp: Creating emfDrools");
571 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
572 "junitDroolsPU", activeStandbyProperties);
574 DroolsPdpsConnector droolsPdpsConnector = new JpaDroolsPdpsConnector(emfDrools);
577 // Create 4 pdpd all not designated. Two on site1. Two on site2
580 long designatedDateMS = new Date().getTime();
581 DroolsPdp pdp1 = new DroolsPdpImpl("pdp1", false, 4, new Date());
582 pdp1.setDesignatedDate(new Date(designatedDateMS - 2));
583 pdp1.setSiteName("site1");
585 DroolsPdp pdp2 = new DroolsPdpImpl("pdp2", false, 4, new Date());
586 pdp2.setDesignatedDate(new Date(designatedDateMS - 3));
587 pdp2.setSiteName("site1");
590 DroolsPdp pdp3 = new DroolsPdpImpl("pdp3", false, 4, new Date());
591 pdp3.setDesignatedDate(new Date(designatedDateMS - 4));
592 pdp3.setSiteName("site2");
594 DroolsPdp pdp4 = new DroolsPdpImpl("pdp4", false, 4, new Date());
596 pdp4.setDesignatedDate(new Date(designatedDateMS));
597 pdp4.setSiteName("site2");
599 ArrayList<DroolsPdp> listOfAllPdps = new ArrayList<DroolsPdp>();
600 listOfAllPdps.add(pdp1);
601 listOfAllPdps.add(pdp2);
602 listOfAllPdps.add(pdp3);
603 listOfAllPdps.add(pdp4);
606 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
609 // We will first test an empty listOfDesignated. As we know from the previous JUnit,
610 // the pdp with the most designated date will be chosen for mostRecentPrimary
612 // Now we want to create a StateManagementFeature and initialize it. It will be
613 // discovered by the ActiveStandbyFeature when the election handler initializes.
615 StateManagementFeatureAPI stateManagementFeature = null;
616 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
618 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
619 stateManagementFeature = feature;
620 logger.debug("testComputeDesignatedPdp stateManagementFeature.getResourceName(): {}", stateManagementFeature.getResourceName());
623 if(stateManagementFeature == null){
624 logger.error("testComputeDesignatedPdp failed to initialize. "
625 + "Unable to get instance of StateManagementFeatureAPI "
626 + "with resourceID: {}", thisPdpId);
627 logger.debug("testComputeDesignatedPdp failed to initialize. "
628 + "Unable to get instance of StateManagementFeatureAPI "
629 + "with resourceID: {}", thisPdpId);
633 DroolsPdpsElectionHandler droolsPdpsElectionHandler = new DroolsPdpsElectionHandler(droolsPdpsConnector, pdp1);
635 DroolsPdp mostRecentPrimary = pdp4;
637 DroolsPdp designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
640 // The designatedPdp should be null
642 assertTrue(designatedPdp==null);
645 // Now let's try having only one pdp in listOfDesignated, but not in the same site as the most recent primary
647 listOfDesignated.add(pdp2);
649 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
652 // Now the designatedPdp should be the one and only selection in the listOfDesignated
655 assertTrue(designatedPdp.getPdpId().equals(pdp2.getPdpId()));
658 // Now let's put 2 pdps in the listOfDesignated, neither in the same site as the mostRecentPrimary
661 listOfDesignated.add(pdp1);
663 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
666 // The designatedPdp should now be the one with the lowest lexiographic score - pdp1
669 assertTrue(designatedPdp.getPdpId().equals(pdp1.getPdpId()));
672 // Finally, we will have 2 pdps in the listOfDesignated, one in the same site with the mostRecentPrimary
675 listOfDesignated.remove(pdp1);
676 listOfDesignated.add(pdp3);
678 designatedPdp = droolsPdpsElectionHandler.computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
681 // The designatedPdp should now be the one on the same site as the mostRecentPrimary
684 assertTrue(designatedPdp.getPdpId().equals(pdp3.getPdpId()));
689 public void testColdStandby() throws Exception {
691 logger.debug("\n\ntestColdStandby: Entering\n\n");
695 logger.debug("testColdStandby: Reading stateManagementProperties");
696 Properties stateManagementProperties = new Properties();
697 stateManagementProperties.load(new FileInputStream(new File(
698 configDir + "/feature-state-management.properties")));
700 logger.debug("testColdStandby: Creating emfXacml");
701 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
702 "junitXacmlPU", stateManagementProperties);
704 logger.debug("testColdStandby: Reading activeStandbyProperties");
705 Properties activeStandbyProperties = new Properties();
706 activeStandbyProperties.load(new FileInputStream(new File(
707 configDir + "/feature-active-standby-management.properties")));
708 String thisPdpId = activeStandbyProperties.getProperty(ActiveStandbyProperties.NODE_NAME);
710 logger.debug("testColdStandby: Creating emfDrools");
711 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
712 "junitDroolsPU", activeStandbyProperties);
714 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
716 logger.debug("testColdStandby: Cleaning up tables");
717 conn.deleteAllPdps();
719 logger.debug("testColdStandby: Inserting PDP={} as designated", thisPdpId);
720 DroolsPdp pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
722 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
723 logger.debug("testColdStandby: After insertion, DESIGNATED= {} "
724 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
725 assertTrue(droolsPdpEntity.isDesignated() == true);
728 * When the Standby Status changes (from providingservice) to hotstandby
729 * or coldstandby,the Active/Standby selection algorithm must stand down
730 * if thePDP-D is currently the lead/active node and allow another PDP-D
733 * It must also call lock on all engines in the engine management.
738 * Yes, this is kludgy, but we have a chicken and egg problem here: we
739 * need a StateManagement object to invoke the
740 * deleteAllStateManagementEntities method.
742 logger.debug("testColdStandby: Instantiating stateManagement object");
744 StateManagement sm = new StateManagement(emfXacml, "dummy");
745 sm.deleteAllStateManagementEntities();
747 // Now we want to create a StateManagementFeature and initialize it. It will be
748 // discovered by the ActiveStandbyFeature when the election handler initializes.
750 StateManagementFeatureAPI smf = null;
751 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
753 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
755 logger.debug("testColdStandby stateManagementFeature.getResourceName(): {}", smf.getResourceName());
759 logger.error("testColdStandby failed to initialize. "
760 + "Unable to get instance of StateManagementFeatureAPI "
761 + "with resourceID: {}", thisPdpId);
762 logger.debug("testColdStandby failed to initialize. "
763 + "Unable to get instance of StateManagementFeatureAPI "
764 + "with resourceID: {}", thisPdpId);
767 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
768 // that has been created.
769 ActiveStandbyFeatureAPI activeStandbyFeature = null;
770 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
772 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
773 activeStandbyFeature = feature;
774 logger.debug("testColdStandby activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
777 if(activeStandbyFeature == null){
778 logger.error("testColdStandby failed to initialize. "
779 + "Unable to get instance of ActiveStandbyFeatureAPI "
780 + "with resourceID:{}", thisPdpId);
781 logger.debug("testColdStandby failed to initialize. "
782 + "Unable to get instance of ActiveStandbyFeatureAPI "
783 + "with resourceID:{}", thisPdpId);
786 // Artificially putting a PDP into service is really a two step process, 1)
787 // inserting it as designated and 2) promoting it so that its standbyStatus
788 // is providing service.
790 logger.debug("testColdStandby: Runner started; Sleeping "
791 + interruptRecoveryTime + "ms before promoting PDP= {}",
793 Thread.sleep(interruptRecoveryTime);
795 logger.debug("testColdStandby: Promoting PDP={}", thisPdpId);
798 String standbyStatus = sm.getStandbyStatus(thisPdpId);
799 logger.debug("testColdStandby: Before locking, PDP= {} has standbyStatus= {}",
800 thisPdpId, standbyStatus);
802 logger.debug("testColdStandby: Locking smf");
805 Thread.sleep(interruptRecoveryTime);
807 // Verify that the PDP is no longer designated.
809 droolsPdpEntity = conn.getPdp(thisPdpId);
810 logger.debug("testColdStandby: After lock sm.lock() invoked, "
811 + "DESIGNATED= {} for PDP={}", droolsPdpEntity.isDesignated(), thisPdpId);
812 assertTrue(droolsPdpEntity.isDesignated() == false);
814 logger.debug("\n\ntestColdStandby: Exiting\n\n");
815 Thread.sleep(interruptRecoveryTime);
819 // Tests hot standby when there is only one PDP.
823 public void testHotStandby1() throws Exception {
825 logger.debug("\n\ntestHotStandby1: Entering\n\n");
829 logger.debug("testHotStandby1: Reading stateManagementProperties");
830 Properties stateManagementProperties = new Properties();
831 stateManagementProperties.load(new FileInputStream(new File(
832 configDir + "/feature-state-management.properties")));
834 logger.debug("testHotStandby1: Creating emfXacml");
835 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
836 "junitXacmlPU", stateManagementProperties);
838 logger.debug("testHotStandby1: Reading activeStandbyProperties");
839 Properties activeStandbyProperties = new Properties();
840 activeStandbyProperties.load(new FileInputStream(new File(
841 configDir + "/feature-active-standby-management.properties")));
842 String thisPdpId = activeStandbyProperties
843 .getProperty(ActiveStandbyProperties.NODE_NAME);
845 logger.debug("testHotStandby1: Creating emfDrools");
846 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
847 "junitDroolsPU", activeStandbyProperties);
849 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
851 logger.debug("testHotStandby1: Cleaning up tables");
852 conn.deleteAllPdps();
855 * Insert this PDP as not designated. Initial standby state will be
856 * either null or cold standby. Demoting should transit state to
860 logger.debug("testHotStandby1: Inserting PDP={} as not designated", thisPdpId);
861 Date yesterday = DateUtils.addDays(new Date(), -1);
862 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
864 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
865 logger.debug("testHotStandby1: After insertion, PDP={} has DESIGNATED={}",
866 thisPdpId, droolsPdpEntity.isDesignated());
867 assertTrue(droolsPdpEntity.isDesignated() == false);
869 logger.debug("testHotStandby1: Instantiating stateManagement object");
870 StateManagement sm = new StateManagement(emfXacml, "dummy");
871 sm.deleteAllStateManagementEntities();
874 // Now we want to create a StateManagementFeature and initialize it. It will be
875 // discovered by the ActiveStandbyFeature when the election handler initializes.
877 StateManagementFeatureAPI smf = null;
878 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
880 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
882 logger.debug("testHotStandby1 stateManagementFeature.getResourceName(): {}", smf.getResourceName());
886 logger.error("testHotStandby1 failed to initialize. "
887 + "Unable to get instance of StateManagementFeatureAPI "
888 + "with resourceID: {}", thisPdpId);
889 logger.debug("testHotStandby1 failed to initialize. "
890 + "Unable to get instance of StateManagementFeatureAPI "
891 + "with resourceID: {}", thisPdpId);
894 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
895 // that has been created.
896 ActiveStandbyFeatureAPI activeStandbyFeature = null;
897 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
899 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
900 activeStandbyFeature = feature;
901 logger.debug("testHotStandby1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
904 if(activeStandbyFeature == null){
905 logger.error("testHotStandby1 failed to initialize. "
906 + "Unable to get instance of ActiveStandbyFeatureAPI "
907 + "with resourceID: {}", thisPdpId);
908 logger.debug("testHotStandby1 failed to initialize. "
909 + "Unable to get instance of ActiveStandbyFeatureAPI "
910 + "with resourceID: {}", thisPdpId);
914 logger.debug("testHotStandby1: Demoting PDP={}", thisPdpId);
915 // demoting should cause state to transit to hotstandby
919 logger.debug("testHotStandby1: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
920 + "time to check droolspdpentity table", sleepTime);
921 Thread.sleep(sleepTime);
924 // Verify that this formerly un-designated PDP in HOT_STANDBY is now designated and providing service.
926 droolsPdpEntity = conn.getPdp(thisPdpId);
927 logger.debug("testHotStandby1: After sm.demote() invoked, DESIGNATED= {} "
928 + "for PDP= {}", droolsPdpEntity.isDesignated(), thisPdpId);
929 assertTrue(droolsPdpEntity.isDesignated() == true);
930 String standbyStatus = smf.getStandbyStatus(thisPdpId);
931 logger.debug("testHotStandby1: After demotion, PDP= {} "
932 + "has standbyStatus= {}", thisPdpId, standbyStatus);
933 assertTrue(standbyStatus != null && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
935 logger.debug("testHotStandby1: Stopping policyManagementRunner");
936 //policyManagementRunner.stopRunner();
938 logger.debug("\n\ntestHotStandby1: Exiting\n\n");
939 Thread.sleep(interruptRecoveryTime);
944 * Tests hot standby when two PDPs are involved.
949 public void testHotStandby2() throws Exception {
951 logger.info("\n\ntestHotStandby2: Entering\n\n");
955 logger.info("testHotStandby2: Reading stateManagementProperties");
956 Properties stateManagementProperties = new Properties();
957 stateManagementProperties.load(new FileInputStream(new File(
958 configDir + "/feature-state-management.properties")));
960 logger.info("testHotStandby2: Creating emfXacml");
961 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
962 "junitXacmlPU", stateManagementProperties);
964 logger.info("testHotStandby2: Reading activeStandbyProperties");
965 Properties activeStandbyProperties = new Properties();
966 activeStandbyProperties.load(new FileInputStream(new File(
967 configDir + "/feature-active-standby-management.properties")));
968 String thisPdpId = activeStandbyProperties
969 .getProperty(ActiveStandbyProperties.NODE_NAME);
971 logger.info("testHotStandby2: Creating emfDrools");
972 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
973 "junitDroolsPU", activeStandbyProperties);
975 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
977 logger.info("testHotStandby2: Cleaning up tables");
978 conn.deleteAllPdps();
981 // Insert a PDP that's designated but not current.
983 String activePdpId = "pdp2";
984 logger.info("testHotStandby2: Inserting PDP={} as stale, designated PDP", activePdpId);
985 Date yesterday = DateUtils.addDays(new Date(), -1);
986 DroolsPdp pdp = new DroolsPdpImpl(activePdpId, true, 4, yesterday);
988 DroolsPdpEntity droolsPdpEntity = conn.getPdp(activePdpId);
989 logger.info("testHotStandby2: After insertion, PDP= {}, which is "
990 + "not current, has DESIGNATED= {}", activePdpId, droolsPdpEntity.isDesignated());
991 assertTrue(droolsPdpEntity.isDesignated() == true);
994 * Promote the designated PDP.
996 * We have a chicken and egg problem here: we need a StateManagement
997 * object to invoke the deleteAllStateManagementEntities method.
1001 logger.info("testHotStandby2: Promoting PDP={}", activePdpId);
1002 StateManagement sm = new StateManagement(emfXacml, "dummy");
1003 sm.deleteAllStateManagementEntities();
1006 sm = new StateManagement(emfXacml, activePdpId);//pdp2
1008 // Artificially putting a PDP into service is really a two step process, 1)
1009 // inserting it as designated and 2) promoting it so that its standbyStatus
1010 // is providing service.
1013 * Insert this PDP as not designated. Initial standby state will be
1014 * either null or cold standby. Demoting should transit state to
1019 logger.info("testHotStandby2: Inserting PDP= {} as not designated", thisPdpId);
1020 pdp = new DroolsPdpImpl(thisPdpId, false, 4, yesterday);
1021 conn.insertPdp(pdp);
1022 droolsPdpEntity = conn.getPdp(thisPdpId);
1023 logger.info("testHotStandby2: After insertion, PDP={} "
1024 + "has DESIGNATED= {}", thisPdpId, droolsPdpEntity.isDesignated());
1025 assertTrue(droolsPdpEntity.isDesignated() == false);
1028 // Now we want to create a StateManagementFeature and initialize it. It will be
1029 // discovered by the ActiveStandbyFeature when the election handler initializes.
1031 StateManagementFeatureAPI sm2 = null;
1032 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1034 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1036 logger.debug("testHotStandby2 stateManagementFeature.getResourceName(): {}", sm2.getResourceName());
1040 logger.error("testHotStandby2 failed to initialize. "
1041 + "Unable to get instance of StateManagementFeatureAPI "
1042 + "with resourceID: {}", thisPdpId);
1043 logger.debug("testHotStandby2 failed to initialize. "
1044 + "Unable to get instance of StateManagementFeatureAPI "
1045 + "with resourceID: {}", thisPdpId);
1048 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1049 // that has been created.
1050 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1051 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1053 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1054 activeStandbyFeature = feature;
1055 logger.debug("testHotStandby2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1058 if(activeStandbyFeature == null){
1059 logger.error("testHotStandby2 failed to initialize. "
1060 + "Unable to get instance of ActiveStandbyFeatureAPI "
1061 + "with resourceID: {}", thisPdpId);
1062 logger.debug("testHotStandby2 failed to initialize. "
1063 + "Unable to get instance of ActiveStandbyFeatureAPI "
1064 + "with resourceID: {}", thisPdpId);
1067 logger.info("testHotStandby2: Runner started; Sleeping {} "
1068 + "ms before promoting/demoting", interruptRecoveryTime);
1069 Thread.sleep(interruptRecoveryTime);
1071 logger.info("testHotStandby2: Runner started; promoting PDP={}", activePdpId);
1072 //At this point, the newly created pdp will have set the state to disabled/failed/cold standby
1073 //because it is stale. So, it cannot be promoted. We need to call sm.enableNotFailed() so we
1074 //can promote it and demote the other pdp - else the other pdp will just spring back to providingservice
1075 sm.enableNotFailed();//pdp2
1077 String standbyStatus = sm.getStandbyStatus(activePdpId);
1078 logger.info("testHotStandby2: After promoting, PDP= {} has standbyStatus= {}", activePdpId, standbyStatus);
1080 // demoting PDP should ensure that state transits to hotstandby
1081 logger.info("testHotStandby2: Runner started; demoting PDP= {}", thisPdpId);
1083 standbyStatus = sm.getStandbyStatus(thisPdpId);
1084 logger.info("testHotStandby2: After demoting, PDP={} has standbyStatus= {}",thisPdpId , standbyStatus);
1086 logger.info("testHotStandby2: Sleeping {} ms, to allow JpaDroolsPdpsConnector "
1087 + "time to check droolspdpentity table", sleepTime);
1088 Thread.sleep(sleepTime);
1091 * Verify that this PDP, demoted to HOT_STANDBY, is now
1092 * re-designated and providing service.
1095 droolsPdpEntity = conn.getPdp(thisPdpId);
1096 logger.info("testHotStandby2: After demoting PDP={}"
1097 + ", DESIGNATED= {}"
1098 + " for PDP= {}", activePdpId, droolsPdpEntity.isDesignated(), thisPdpId);
1099 assertTrue(droolsPdpEntity.isDesignated() == true);
1100 standbyStatus = sm2.getStandbyStatus(thisPdpId);
1101 logger.info("testHotStandby2: After demoting PDP={}"
1102 + ", PDP={} has standbyStatus= {}",
1103 activePdpId, thisPdpId, standbyStatus);
1104 assertTrue(standbyStatus != null
1105 && standbyStatus.equals(StateManagement.PROVIDING_SERVICE));
1107 logger.info("testHotStandby2: Stopping policyManagementRunner");
1108 //policyManagementRunner.stopRunner();
1110 logger.info("\n\ntestHotStandby2: Exiting\n\n");
1111 Thread.sleep(interruptRecoveryTime);
1116 * 1) Inserts and designates this PDP, then verifies that startTransaction
1119 * 2) Demotes PDP, and verifies that because there is only one PDP, it will
1120 * be immediately re-promoted, thus allowing startTransaction to be
1123 * 3) Locks PDP and verifies that startTransaction results in
1124 * AdministrativeStateException.
1126 * 4) Unlocks PDP and verifies that startTransaction results in
1127 * StandbyStatusException.
1129 * 5) Promotes PDP and verifies that startTransaction is once again
1135 public void testLocking1() throws Exception {
1136 logger.debug("testLocking1: Entry");
1140 logger.debug("testLocking1: Reading stateManagementProperties");
1141 Properties stateManagementProperties = new Properties();
1142 stateManagementProperties.load(new FileInputStream(new File(
1143 configDir + "/feature-state-management.properties")));
1145 logger.debug("testLocking1: Creating emfXacml");
1146 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1147 "junitXacmlPU", stateManagementProperties);
1149 logger.debug("testLocking1: Reading activeStandbyProperties");
1150 Properties activeStandbyProperties = new Properties();
1151 activeStandbyProperties.load(new FileInputStream(new File(
1152 configDir + "/feature-active-standby-management.properties")));
1153 String thisPdpId = activeStandbyProperties
1154 .getProperty(ActiveStandbyProperties.NODE_NAME);
1156 logger.debug("testLocking1: Creating emfDrools");
1157 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1158 "junitDroolsPU", activeStandbyProperties);
1160 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1162 logger.debug("testLocking1: Cleaning up tables");
1163 conn.deleteAllPdps();
1166 * Insert this PDP as designated. Initial standby state will be
1167 * either null or cold standby.
1170 logger.debug("testLocking1: Inserting PDP= {} as designated", thisPdpId);
1171 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 4, new Date());
1172 conn.insertPdp(pdp);
1173 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1174 logger.debug("testLocking1: After insertion, PDP= {} has DESIGNATED= {}",
1175 thisPdpId, droolsPdpEntity.isDesignated());
1176 assertTrue(droolsPdpEntity.isDesignated() == true);
1178 logger.debug("testLocking1: Instantiating stateManagement object");
1179 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1180 smDummy.deleteAllStateManagementEntities();
1182 // Now we want to create a StateManagementFeature and initialize it. It will be
1183 // discovered by the ActiveStandbyFeature when the election handler initializes.
1185 StateManagementFeatureAPI sm = null;
1186 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1188 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1190 logger.debug("testLocking1 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1194 logger.error("testLocking1 failed to initialize. "
1195 + "Unable to get instance of StateManagementFeatureAPI "
1196 + "with resourceID: {}", thisPdpId);
1197 logger.debug("testLocking1 failed to initialize. "
1198 + "Unable to get instance of StateManagementFeatureAPI "
1199 + "with resourceID: {}", thisPdpId);
1202 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1203 // that has been created.
1204 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1205 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1207 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1208 activeStandbyFeature = feature;
1209 logger.debug("testLocking1 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1212 if(activeStandbyFeature == null){
1213 logger.error("testLocking1 failed to initialize. "
1214 + "Unable to get instance of ActiveStandbyFeatureAPI "
1215 + "with resourceID: {}", thisPdpId);
1216 logger.debug("testLocking1 failed to initialize. "
1217 + "Unable to get instance of ActiveStandbyFeatureAPI "
1218 + "with resourceID: {}", thisPdpId);
1221 logger.debug("testLocking1: Runner started; Sleeping "
1222 + interruptRecoveryTime + "ms before promoting PDP={}",
1224 Thread.sleep(interruptRecoveryTime);
1226 logger.debug("testLocking1: Promoting PDP={}", thisPdpId);
1229 logger.debug("testLocking1: Sleeping {} ms, to allow time for "
1230 + "policy-management.Main class to come up, designated= {}",
1231 sleepTime, conn.getPdp(thisPdpId).isDesignated());
1232 Thread.sleep(sleepTime);
1234 logger.debug("testLocking1: Waking up and invoking startTransaction on active PDP={}"
1235 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1238 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1240 droolsPdpIntegrityMonitor.startTransaction();
1241 droolsPdpIntegrityMonitor.endTransaction();
1242 logger.debug("testLocking1: As expected, transaction successful");
1243 } catch (AdministrativeStateException e) {
1244 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1246 } catch (StandbyStatusException e) {
1247 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1249 } catch (Exception e) {
1250 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1254 // demoting should cause state to transit to hotstandby, followed by re-promotion,
1255 // since there is only one PDP.
1256 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1259 logger.debug("testLocking1: sleeping" + electionWaitSleepTime
1260 + " to allow election handler to re-promote PDP={}", thisPdpId);
1261 Thread.sleep(electionWaitSleepTime);
1263 logger.debug("testLocking1: Invoking startTransaction on re-promoted PDP={}"
1264 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1266 droolsPdpIntegrityMonitor.startTransaction();
1267 droolsPdpIntegrityMonitor.endTransaction();
1268 logger.debug("testLocking1: As expected, transaction successful");
1269 } catch (AdministrativeStateException e) {
1270 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1272 } catch (StandbyStatusException e) {
1273 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1275 } catch (Exception e) {
1276 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1280 // locking should cause state to transit to cold standby
1281 logger.debug("testLocking1: locking PDP={}", thisPdpId);
1284 // Just to avoid any race conditions, sleep a little after locking
1285 logger.debug("testLocking1: Sleeping a few millis after locking, to avoid race condition");
1288 logger.debug("testLocking1: Invoking startTransaction on locked PDP= {}"
1289 + ", designated= {}",thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1291 droolsPdpIntegrityMonitor.startTransaction();
1292 logger.error("testLocking1: startTransaction unexpectedly successful");
1294 } catch (AdministrativeStateException e) {
1295 logger.debug("testLocking1: As expected, caught AdministrativeStateException, ", e);
1296 } catch (StandbyStatusException e) {
1297 logger.error("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1299 } catch (Exception e) {
1300 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1303 droolsPdpIntegrityMonitor.endTransaction();
1306 // unlocking should cause state to transit to hot standby and then providing service
1307 logger.debug("testLocking1: unlocking PDP={}", thisPdpId);
1310 // Just to avoid any race conditions, sleep a little after locking
1311 logger.debug("testLocking1: Sleeping a few millis after unlocking, to avoid race condition");
1312 Thread.sleep(electionWaitSleepTime);
1314 logger.debug("testLocking1: Invoking startTransaction on unlocked PDP="
1317 + conn.getPdp(thisPdpId).isDesignated());
1319 droolsPdpIntegrityMonitor.startTransaction();
1320 logger.error("testLocking1: startTransaction successful as expected");
1321 } catch (AdministrativeStateException e) {
1322 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1324 } catch (StandbyStatusException e) {
1325 logger.debug("testLocking1: Unexpectedly caught StandbyStatusException, ", e);
1327 } catch (Exception e) {
1328 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1331 droolsPdpIntegrityMonitor.endTransaction();
1334 // demoting should cause state to transit to hot standby
1335 logger.debug("testLocking1: demoting PDP={}", thisPdpId);
1338 // Just to avoid any race conditions, sleep a little after promoting
1339 logger.debug("testLocking1: Sleeping a few millis after demoting, to avoid race condition");
1342 logger.debug("testLocking1: Invoking startTransaction on demoted PDP={}"
1343 + ", designated={}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1345 droolsPdpIntegrityMonitor.startTransaction();
1346 droolsPdpIntegrityMonitor.endTransaction();
1347 logger.debug("testLocking1: Unexpectedly, transaction successful");
1349 } catch (AdministrativeStateException e) {
1350 logger.error("testLocking1: Unexpectedly caught AdministrativeStateException, ", e);
1352 } catch (StandbyStatusException e) {
1353 logger.error("testLocking1: As expected caught StandbyStatusException, ", e);
1354 } catch (Exception e) {
1355 logger.error("testLocking1: Unexpectedly caught Exception, ", e);
1359 logger.debug("\n\ntestLocking1: Exiting\n\n");
1360 Thread.sleep(interruptRecoveryTime);
1366 * 1) Inserts and designates this PDP, then verifies that startTransaction
1369 * 2) Inserts another PDP in hotstandby.
1371 * 3) Demotes this PDP, and verifies 1) that other PDP is not promoted (because one
1372 * PDP cannot promote another PDP) and 2) that this PDP is re-promoted.
1377 public void testLocking2() throws Exception {
1379 logger.debug("\n\ntestLocking2: Entering\n\n");
1383 logger.debug("testLocking2: Reading stateManagementProperties");
1384 Properties stateManagementProperties = new Properties();
1385 stateManagementProperties.load(new FileInputStream(new File(
1386 configDir + "/feature-state-management.properties")));
1388 logger.debug("testLocking2: Creating emfXacml");
1389 EntityManagerFactory emfXacml = Persistence.createEntityManagerFactory(
1390 "junitXacmlPU", stateManagementProperties);
1392 logger.debug("testLocking2: Reading activeStandbyProperties");
1393 Properties activeStandbyProperties = new Properties();
1394 activeStandbyProperties.load(new FileInputStream(new File(
1395 configDir + "/feature-active-standby-management.properties")));
1396 String thisPdpId = activeStandbyProperties
1397 .getProperty(ActiveStandbyProperties.NODE_NAME);
1399 logger.debug("testLocking2: Creating emfDrools");
1400 EntityManagerFactory emfDrools = Persistence.createEntityManagerFactory(
1401 "junitDroolsPU", activeStandbyProperties);
1403 DroolsPdpsConnector conn = new JpaDroolsPdpsConnector(emfDrools);
1405 logger.debug("testLocking2: Cleaning up tables");
1406 conn.deleteAllPdps();
1409 * Insert this PDP as designated. Initial standby state will be
1410 * either null or cold standby. Demoting should transit state to
1414 logger.debug("testLocking2: Inserting PDP= {} as designated", thisPdpId);
1415 DroolsPdpImpl pdp = new DroolsPdpImpl(thisPdpId, true, 3, new Date());
1416 conn.insertPdp(pdp);
1417 DroolsPdpEntity droolsPdpEntity = conn.getPdp(thisPdpId);
1418 logger.debug("testLocking2: After insertion, PDP= {} has DESIGNATED= {}",
1419 thisPdpId, droolsPdpEntity.isDesignated());
1420 assertTrue(droolsPdpEntity.isDesignated() == true);
1422 logger.debug("testLocking2: Instantiating stateManagement object and promoting PDP={}", thisPdpId);
1423 StateManagement smDummy = new StateManagement(emfXacml, "dummy");
1424 smDummy.deleteAllStateManagementEntities();
1426 // Now we want to create a StateManagementFeature and initialize it. It will be
1427 // discovered by the ActiveStandbyFeature when the election handler initializes.
1429 StateManagementFeatureAPI sm = null;
1430 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
1432 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1434 logger.debug("testLocking2 stateManagementFeature.getResourceName(): {}", sm.getResourceName());
1438 logger.error("testLocking2 failed to initialize. "
1439 + "Unable to get instance of StateManagementFeatureAPI "
1440 + "with resourceID: {}", thisPdpId);
1441 logger.debug("testLocking2 failed to initialize. "
1442 + "Unable to get instance of StateManagementFeatureAPI "
1443 + "with resourceID: {}", thisPdpId);
1446 // Create an ActiveStandbyFeature and initialize it. It will discover the StateManagementFeature
1447 // that has been created.
1448 ActiveStandbyFeatureAPI activeStandbyFeature = null;
1449 for (ActiveStandbyFeatureAPI feature : ActiveStandbyFeatureAPI.impl.getList())
1451 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
1452 activeStandbyFeature = feature;
1453 logger.debug("testLocking2 activeStandbyFeature.getResourceName(): {}", activeStandbyFeature.getResourceName());
1456 if(activeStandbyFeature == null){
1457 logger.error("testLocking2 failed to initialize. "
1458 + "Unable to get instance of ActiveStandbyFeatureAPI "
1459 + "with resourceID: {}", thisPdpId);
1460 logger.debug("testLocking2 failed to initialize. "
1461 + "Unable to get instance of ActiveStandbyFeatureAPI "
1462 + "with resourceID: {}", thisPdpId);
1466 * Insert another PDP as not designated. Initial standby state will be
1467 * either null or cold standby. Demoting should transit state to
1471 String standbyPdpId = "pdp2";
1472 logger.debug("testLocking2: Inserting PDP= {} as not designated", standbyPdpId);
1473 Date yesterday = DateUtils.addDays(new Date(), -1);
1474 pdp = new DroolsPdpImpl(standbyPdpId, false, 4, yesterday);
1475 conn.insertPdp(pdp);
1476 droolsPdpEntity = conn.getPdp(standbyPdpId);
1477 logger.debug("testLocking2: After insertion, PDP={} has DESIGNATED= {}",
1478 standbyPdpId, droolsPdpEntity.isDesignated());
1479 assertTrue(droolsPdpEntity.isDesignated() == false);
1481 logger.debug("testLocking2: Demoting PDP= {}", standbyPdpId);
1482 StateManagement sm2 = new StateManagement(emfXacml, standbyPdpId);
1484 logger.debug("testLocking2: Runner started; Sleeping {} ms "
1485 + "before promoting/demoting", interruptRecoveryTime);
1486 Thread.sleep(interruptRecoveryTime);
1488 logger.debug("testLocking2: Promoting PDP= {}", thisPdpId);
1491 // demoting PDP should ensure that state transits to hotstandby
1492 logger.debug("testLocking2: Demoting PDP={}", standbyPdpId);
1495 logger.debug("testLocking2: Sleeping {} ms, to allow time for to come up", sleepTime);
1496 Thread.sleep(sleepTime);
1498 logger.debug("testLocking2: Waking up and invoking startTransaction on active PDP={}"
1499 + ", designated= {}", thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1501 IntegrityMonitor droolsPdpIntegrityMonitor = IntegrityMonitor.getInstance();
1504 droolsPdpIntegrityMonitor.startTransaction();
1505 droolsPdpIntegrityMonitor.endTransaction();
1506 logger.debug("testLocking2: As expected, transaction successful");
1507 } catch (AdministrativeStateException e) {
1508 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1510 } catch (StandbyStatusException e) {
1511 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1513 } catch (Exception e) {
1514 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1518 // demoting should cause state to transit to hotstandby followed by re-promotion.
1519 logger.debug("testLocking2: demoting PDP={}", thisPdpId);
1522 logger.debug("testLocking2: sleeping {}"
1523 + " to allow election handler to re-promote PDP={}", electionWaitSleepTime, thisPdpId);
1524 Thread.sleep(electionWaitSleepTime);
1526 logger.debug("testLocking2: Waking up and invoking startTransaction "
1527 + "on re-promoted PDP= {}, designated= {}",
1528 thisPdpId, conn.getPdp(thisPdpId).isDesignated());
1530 droolsPdpIntegrityMonitor.startTransaction();
1531 droolsPdpIntegrityMonitor.endTransaction();
1532 logger.debug("testLocking2: As expected, transaction successful");
1533 } catch (AdministrativeStateException e) {
1534 logger.error("testLocking2: Unexpectedly caught AdministrativeStateException, ", e);
1536 } catch (StandbyStatusException e) {
1537 logger.error("testLocking2: Unexpectedly caught StandbyStatusException, ", e);
1539 } catch (Exception e) {
1540 logger.error("testLocking2: Unexpectedly caught Exception, ", e);
1544 logger.debug("testLocking2: Verifying designated status for PDP= {}", standbyPdpId);
1545 boolean standbyPdpDesignated = conn.getPdp(standbyPdpId).isDesignated();
1546 assertTrue(standbyPdpDesignated == false);
1548 logger.debug("\n\ntestLocking2: Exiting\n\n");
1549 Thread.sleep(interruptRecoveryTime);