2 * ============LICENSE_START=======================================================
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.openecomp.policy.drools.persistence;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.LinkedList;
27 import java.util.List;
28 import java.util.Timer;
29 import java.util.TimerTask;
31 import org.openecomp.policy.common.im.StandbyStatusException;
32 import org.openecomp.policy.common.im.StateManagement;
33 import org.openecomp.policy.drools.core.DroolsPDPIntegrityMonitor;
34 import org.openecomp.policy.drools.core.IntegrityMonitorProperties;
35 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
36 import org.openecomp.policy.common.logging.flexlogger.Logger;
37 import org.openecomp.policy.common.logging.eelf.MessageCodes;
39 public class DroolsPdpsElectionHandler implements ThreadRunningChecker {
40 // get an instance of logger
41 private final static Logger logger = FlexLogger.getLogger(DroolsPdpsElectionHandler.class);
42 private DroolsPdpsConnector pdpsConnector;
43 private Object pdpsConnectorLock = new Object();
44 private Object checkUpdateWorkerLock = new Object();
45 private Object checkWaitTimerLock = new Object();
46 private Object designationWaiterLock = new Object();
49 * Must be static, so it can be referenced by JpaDroolsPdpsConnector,
50 * without requiring a reference to the election handler instantiation.
52 private static DroolsPdp myPdp;
54 private DesignationWaiter designationWaiter;
55 private Timer updateWorker;
56 private Timer waitTimer;
57 private Date updateWorkerLastRunDate;
58 private Date waitTimerLastRunDate;
59 private int pdpCheckInterval;
60 private int pdpUpdateInterval;
61 private volatile boolean isDesignated;
62 DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor;
63 StateManagement stateManagement;
65 public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp, DroolsPDPIntegrityMonitor droolsPdpIntegrityMonitor){
66 this.pdpsConnector = pdps;
67 DroolsPdpsElectionHandler.myPdp = myPdp;
68 this.isDesignated = false;
69 this.droolsPdpIntegrityMonitor = droolsPdpIntegrityMonitor;
70 this.stateManagement = droolsPdpIntegrityMonitor.getStateManager();
71 pdpCheckInterval = 3000;
73 pdpCheckInterval = Integer.parseInt(IntegrityMonitorProperties.getProperty(IntegrityMonitorProperties.PDP_CHECK_INVERVAL));
77 (MessageCodes.EXCEPTION_ERROR ,e, "Could not get pdpCheckInterval property. Using default");
79 pdpUpdateInterval = 2000;
81 pdpUpdateInterval = Integer.parseInt(IntegrityMonitorProperties.getProperty(IntegrityMonitorProperties.PDP_UPDATE_INTERVAL));
85 (MessageCodes.EXCEPTION_ERROR, e, "Could not get pdpUpdateInterval property. Using default");
88 Date now = new Date();
90 // Retrieve the ms since the epoch
91 long nowMs = now.getTime();
93 // Create the timer which will update the updateDate in DroolsPdpEntity table.
94 // This is the heartbeat
95 updateWorker = new Timer();
97 // Schedule the heartbeat to start in 100 ms and run at pdpCheckInterval ms thereafter
98 updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
99 updateWorkerLastRunDate = new Date(nowMs + 100);
101 // Create the timer which will run the election algorithm
102 waitTimer = new Timer();
104 // Schedule it to start in startMs ms (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter
105 long startMs = getDWaiterStartMs();
106 designationWaiter = new DesignationWaiter();
107 waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
108 waitTimerLastRunDate = new Date(nowMs + startMs);
111 public List<DroolsSessionEntity> waitForDesignation(){
112 while(isDesignated == false){
115 } catch (InterruptedException e) {
119 return designationWaiter.getSessions();
122 public List<DroolsSessionEntity> getSessions(){
123 return designationWaiter.getSessions();
125 public void updateMyPdp(){
126 synchronized(pdpsConnectorLock){
127 myPdp.setUpdatedDate(new Date());
128 pdpsConnector.update(myPdp);
133 * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs
134 * access to myPdp, so it can keep its designation status in sync with the
137 public static void setMyPdpDesignated(boolean designated) {
140 ("setMyPdpDesignated: designated=" + designated);
141 myPdp.setDesignated(designated);
144 private class DesignationWaiter extends TimerTask {
145 // get an instance of logger
146 private Logger logger = FlexLogger.getLogger(DesignationWaiter.class);
147 private List<DroolsSessionEntity> sessions = null;
149 public List<DroolsSessionEntity> getSessions(){
150 if(sessions != null){
153 return new LinkedList<DroolsSessionEntity>();
159 ("DesignatedWaiter.run: Entering");
161 // just here initially so code still works
162 if (pdpsConnector == null) {
163 waitTimerLastRunDate = new Date();
164 logger.info("DesignatedWaiter.run (pdpsConnector==null) waitTimerLastRunDate = " + waitTimerLastRunDate);
169 synchronized (designationWaiterLock) {
173 ("DesignatedWaiter.run: Entering synchronized block");
175 checkUpdateWorkerTimer();
177 //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated
178 //PDPs and then decide which one really should be designated at the end.
179 ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
181 Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps();
182 DroolsPdp designatedPdp = null;
183 DroolsPdp lowestPriorityPdp = null;
187 ("DesignatedWaiter.run: pdps.size="
190 //This is only true if all designated PDPs have failed
191 boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps);
194 ("DesignatedWaiter.run: designatedPdpHasFailed="
195 + designatedPdpHasFailed);
196 for (DroolsPdp pdp : pdps) {
199 ("DesignatedWaiter.run: evaluating pdp ID: " + pdp.getPdpId());
202 * Note: side effect of isPdpCurrent is that any stale but
203 * designated PDPs will be marked as un-designated.
205 boolean isCurrent = pdpsConnector.isPdpCurrent(pdp);
208 * We can't use stateManagement.getStandbyStatus() here, because
209 * we need the standbyStatus, not for this PDP, but for the PDP
210 * being processed by this loop iteration.
212 String standbyStatus = stateManagement.getStandbyStatus(pdp.getPdpId());
213 if(standbyStatus==null){
214 // Treat this case as a cold standby -- if we
215 // abort here, no sessions will be created in a
216 // single-node test environment.
217 standbyStatus = StateManagement.COLD_STANDBY;
222 ("DesignatedWaiter.run: PDP="
223 + pdp.getPdpId() + ", isCurrent=" + isCurrent);
226 * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn
227 * and evaluate the each pdp in the list of pdps against each combination.
229 * This is the first combination of isDesignated and isCurrent
231 if (pdp.isDesignated() && isCurrent) {
232 //It is current, but it could have a standbystatus=coldstandby / hotstandby
233 //If so, we need to stand it down and demote it
234 if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
235 if(pdp.getPdpId().equals(myPdp.getPdpId())){
238 ("\n\nDesignatedWaiter.run: myPdp " + myPdp.getPdpId() + " is current and designated, "
239 + "butstandbystatus is not providingservice. "
240 + " Executing stateManagement.demote()" + "\n\n");
241 // So, we must demote it
243 //Keep the order like this. StateManagement is last since it triggers controller shutdown
244 //This will change isDesignated and it can enter another if(combination) below
245 pdpsConnector.standDownPdp(pdp.getPdpId());
246 myPdp.setDesignated(false);
247 isDesignated = false;
248 if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
249 standbyStatus.equals(StateManagement.COLD_STANDBY))){
251 * Only demote it if it appears it has not already been demoted. Don't worry
252 * about synching with the topic endpoint states. That is done by the
255 stateManagement.demote();
257 //update the standbystatus to check in a later combination of isDesignated and isCurrent
258 standbyStatus=stateManagement.getStandbyStatus(pdp.getPdpId());
259 } catch (Exception e) {
262 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " Caught Exception attempting to demote myPdp'"
266 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
267 + "from stateManagement.demote()");
271 // Don't demote a remote PDP that is current. It should catch itself
274 ("\n\nDesignatedWaiter.run: myPdp " + myPdp.getPdpId() + " is current and designated, "
275 + "but standbystatus is not providingservice. "
276 + " Cannot execute stateManagement.demote() since it it is not myPdp\n\n");
280 // If we get here, it is ok to be on the list
283 ("DesignatedWaiter.run: PDP="
285 + " is designated, current and " + standbyStatus +". Noting PDP as designated. standbyStatus=" + standbyStatus);
286 listOfDesignated.add(pdp);
294 * The second combination of isDesignated and isCurrent
296 * PDP is designated but not current; it has failed. So we stand it down (it doesn't matter what
297 * its standbyStatus is). None of these go on the list.
299 if (pdp.isDesignated() && !isCurrent) {
302 ("INFO: DesignatedWaiter.run: PDP="
304 + " is currently designated but is not current; it has failed. Standing down. standbyStatus=" + standbyStatus);
307 * Changes designated to 0 but it is still potentially providing service
308 * Will affect isDesignated, so, it can enter an if(combination) below
310 pdpsConnector.standDownPdp(pdp.getPdpId());
312 //need to change standbystatus to coldstandby
313 if (pdp.getPdpId().equals(myPdp.getPdpId())){
316 ("\n\nDesignatedWaiter.run: myPdp " + myPdp.getPdpId() + " is not Current. "
317 + " Executing stateManagement.disableFailed()" + "\n\n");
318 // We found that myPdp is designated but not current
319 // So, we must cause it to disableFail
321 myPdp.setDesignated(false);
322 //pdpsConnector.setDesignated(myPdp, false);//not needed?
323 isDesignated = false;
324 stateManagement.disableFailed();
325 //stateManagement.demote();
326 } catch (Exception e) {
329 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " Caught Exception attempting to disableFail myPdp'"
333 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
334 + "from stateManagement.disableFailed()");
337 } else { //it is a remote PDP that is failed
340 ("\n\nDesignatedWaiter.run: PDP " + pdp.getPdpId() + " is not Current. "
341 + " Executing stateManagement.disableFailed(otherResourceName)" + "\n\n");
342 // We found a PDP is designated but not current
343 // We already called standdown(pdp) which will change designated to false
344 // Now we need to disableFail it to get its states in synch. The standbyStatus
345 // should equal coldstandby
347 stateManagement.disableFailed(pdp.getPdpId());
348 //stateManagement.demote(pdp.getPdpId());
349 } catch (Exception e) {
352 ("DesignatedWaiter.run: for PDP" + pdp.getPdpId()
353 + " Caught Exception attempting to disableFail(" + pdp.getPdpId() + ")'"
357 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
358 + "from stateManagement.disableFailed()");
363 continue; //we are not going to do anything else with this pdp
367 * The third combination of isDesignated and isCurrent
369 * If a PDP is not currently designated but is providing service (erroneous, but recoverable) or hot standby
370 * we can add it to the list of possible designated if all the designated have failed
372 if (!pdp.isDesignated() && isCurrent){
373 if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
374 standbyStatus.equals(StateManagement.COLD_STANDBY))){
375 logger.info("\n\nDesignatedWaiter.run: PDP " + pdp.getPdpId()
376 + " is NOT designated but IS current and"
377 + " has a standbystatus=" + standbyStatus);
378 // Since it is current, we assume it can adjust its own state.
379 // We will demote if it is myPdp
380 if(pdp.getPdpId().equals(myPdp.getPdpId())){
382 logger.info("DesignatedWaiter.run: PDP " + pdp.getPdpId() + " going to "
383 + "setDesignated = false and calling stateManagement.demote");
385 //Keep the order like this. StateManagement is last since it triggers controller shutdown
386 pdpsConnector.setDesignated(myPdp, false);
387 myPdp.setDesignated(false);
388 isDesignated = false;
389 //This is definitely not a redundant call. It is attempting to correct a problem
390 stateManagement.demote();
391 //recheck the standbystatus
392 standbyStatus = stateManagement.getStandbyStatus(pdp.getPdpId());
393 } catch (Exception e) {
396 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " Caught Exception attempting to demote myPdp'"
400 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
401 + "from stateManagement.demote()");
407 if(standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed){
411 ("INFO: DesignatedWaiter.run: PDP=" + pdp.getPdpId()
412 + " is not designated but is " + standbyStatus + " and designated PDP has failed. standbyStatus="
416 ("DesignatedWaiter.run: Designating PDP=" + pdp.getPdpId());
417 listOfDesignated.add(pdp);
419 continue; //done with this one
423 * The fourth combination of isDesignated and isCurrent
425 * We are not going to put any of these on the list since it appears they have failed.
429 if(!pdp.isDesignated() && !isCurrent) {
432 ("INFO: DesignatedWaiter.run: PDP="
433 + pdp.getPdpId() + ", designated="
434 + pdp.isDesignated() + ", current="
436 + ", designatedPdpHasFailed="
437 + designatedPdpHasFailed
438 + ", standbyStatus=" + standbyStatus);
439 if(!standbyStatus.equals(StateManagement.COLD_STANDBY)){
442 pdpsConnector.standDownPdp(pdp.getPdpId());
443 if(pdp.getPdpId().equals(myPdp.getPdpId())){
445 * I don't actually know how this condition could happen, but if it did, we would want
446 * to declare it failed.
450 ("\n\nDesignatedWaiter.run: myPdp " + myPdp.getPdpId() + " is !current and !designated, "
451 + " Executing stateManagement.disableFailed()" + "\n\n");
452 // So, we must disableFail it
454 //Keep the order like this. StateManagement is last since it triggers controller shutdown
455 myPdp.setDesignated(false);
456 isDesignated = false;
457 stateManagement.disableFailed();
458 //stateManagement.demote();
459 } catch (Exception e) {
462 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " Caught Exception attempting to disableFail myPdp'"
466 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
467 + "from stateManagement.disableFailed()");
473 ("\n\nDesignatedWaiter.run: myPdp " + myPdp.getPdpId() + " is !current and !designated, "
474 + " Executing stateManagement.disableFailed(" + pdp.getPdpId() + ")" + "\n\n");
475 // We already called standdown(pdp) which will change designated to false
476 // Now we need to disableFail it to get its states in sync. StandbyStatus = coldstandby
478 stateManagement.disableFailed(pdp.getPdpId());
479 //stateManagement.demote(pdp.getPdpId());
480 } catch (Exception e) {
483 ("DesignatedWaiter.run: for PDP" + pdp.getPdpId()
484 + " Caught Exception attempting to disableFail(" + pdp.getPdpId() + ")'"
488 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
489 + "from stateManagement.disableFailed()");
500 * We have checked the four combinations of isDesignated and isCurrent. Where appropriate,
501 * we added the PDPs to the potential list of designated pdps
502 * Check if listOfDesignated is empty, has one entry or has multiple entries
503 * If it has multiple designated PDPs, then we must determine if myPdp is on the list and if
504 * it is the lowest priority. If it is on the list and it is not the lowest
505 * priority, it must be demoted. Then, we must find the lowest priority
506 * PDP so we can get the right list of sessions
508 //we need to give priority to pdps on the same site that is currently being used
511 //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
512 DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0));
513 mostRecentPrimary.setSiteName(null);
514 for(DroolsPdp pdp : pdps){
515 if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
516 mostRecentPrimary = pdp;
520 if(listOfDesignated.size() > 1){
523 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " listOfDesignated.size(): " + listOfDesignated.size());
524 DroolsPdp rejectedPdp = null;
525 DroolsPdp lowestPrioritySameSite = null;
526 DroolsPdp lowestPriorityDifferentSite = null;
527 for(DroolsPdp pdp : listOfDesignated){
528 // We need to determine if another PDP is the lowest priority
529 if(nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())){
530 if(lowestPrioritySameSite == null){
531 if(lowestPriorityDifferentSite != null){
532 rejectedPdp = lowestPriorityDifferentSite;
534 lowestPrioritySameSite = pdp;
536 if(pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))){
537 continue;//nothing to compare
539 if(pdp.comparePriority(lowestPrioritySameSite) <0){
542 ("\nDesignatedWaiter.run: myPdp" + myPdp.getPdpId() + " listOfDesignated pdp ID: " + pdp.getPdpId()
543 + " has lower priority than pdp ID: " + lowestPrioritySameSite.getPdpId());
545 //we need to reject lowestPrioritySameSite
546 rejectedPdp = lowestPrioritySameSite;
547 lowestPrioritySameSite = pdp;
549 //we need to reject pdp and keep lowestPrioritySameSite
552 ("\nDesignatedWaiter.run: myPdp" + myPdp.getPdpId() + " listOfDesignated pdp ID: " + pdp.getPdpId()
553 + " has higher priority than pdp ID: " + lowestPrioritySameSite.getPdpId());
558 if(lowestPrioritySameSite != null){
559 //if we already have a candidate for same site, we don't want to bother with different sites
562 if(lowestPriorityDifferentSite == null){
563 lowestPriorityDifferentSite = pdp;
566 if(pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))){
567 continue;//nothing to compare
569 if(pdp.comparePriority(lowestPriorityDifferentSite) <0){
572 ("\nDesignatedWaiter.run: myPdp" + myPdp.getPdpId() + " listOfDesignated pdp ID: " + pdp.getPdpId()
573 + " has lower priority than pdp ID: " + lowestPriorityDifferentSite.getPdpId());
575 //we need to reject lowestPriorityDifferentSite
576 rejectedPdp = lowestPriorityDifferentSite;
577 lowestPriorityDifferentSite = pdp;
579 //we need to reject pdp and keep lowestPriorityDifferentSite
582 ("\nDesignatedWaiter.run: myPdp" + myPdp.getPdpId() + " listOfDesignated pdp ID: " + pdp.getPdpId()
583 + " has higher priority than pdp ID: " + lowestPriorityDifferentSite.getPdpId());
588 // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible
589 // for demoting itself
590 if(rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())){
593 ("\n\nDesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " listOfDesignated myPdp ID: " + myPdp.getPdpId()
594 + " is NOT the lowest priority. Executing stateManagement.demote()" + "\n\n");
595 // We found that myPdp is on the listOfDesignated and it is not the lowest priority
596 // So, we must demote it
598 //Keep the order like this. StateManagement is last since it triggers controller shutdown
599 myPdp.setDesignated(false);
600 pdpsConnector.setDesignated(myPdp, false);
601 isDesignated = false;
602 String standbyStatus = stateManagement.getStandbyStatus();
603 if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
604 standbyStatus.equals(StateManagement.COLD_STANDBY))){
606 * Only call demote if it is not already in the right state. Don't worry about
607 * synching the lower level topic endpoint states. That is done by the
610 stateManagement.demote();
612 } catch (Exception e) {
613 myPdp.setDesignated(false);
614 pdpsConnector.setDesignated(myPdp, false);
615 isDesignated = false;
618 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " Caught Exception attempting to demote myPdp'"
622 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
623 + "from stateManagement.demote()");
627 } //end: for(DroolsPdp pdp : listOfDesignated)
628 if(lowestPrioritySameSite != null){
629 lowestPriorityPdp = lowestPrioritySameSite;
631 lowestPriorityPdp = lowestPriorityDifferentSite;
633 //now we have a valid value for lowestPriorityPdp
636 ("\n\nDesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " listOfDesignated found the LOWEST priority pdp ID: "
637 + lowestPriorityPdp.getPdpId()
638 + " It is now the designatedPpd from the perspective of myPdp ID: " + myPdp + "\n\n");
639 designatedPdp = lowestPriorityPdp;
640 this.sessions = mostRecentPrimary.getSessions();
642 } else if(listOfDesignated.isEmpty()){
645 ("\nDesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " listOfDesignated is: EMPTY.");
646 designatedPdp = null;
647 } else{ //only one in listOfDesignated
650 ("\nDesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + " listOfDesignated has ONE entry. PDP ID: "
651 + listOfDesignated.get(0).getPdpId());
652 designatedPdp = listOfDesignated.get(0);
653 this.sessions = mostRecentPrimary.getSessions();
657 if (designatedPdp == null) {
660 ("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. designatedPdp still null.");
661 // Just to be sure the parameters are correctly set
662 myPdp.setDesignated(false);
663 pdpsConnector.setDesignated(myPdp,false);
664 isDesignated = false;
666 waitTimerLastRunDate = new Date();
667 logger.info("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = " + waitTimerLastRunDate);
671 } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) {
674 ("DesignatedWaiter.run: designatedPdp is PDP=" + myPdp.getPdpId());
676 * update function expects myPdp.isDesignated to be true.
679 //Keep the order like this. StateManagement is last since it triggers controller init
680 myPdp.setDesignated(true);
681 pdpsConnector.setDesignated(myPdp, true);
683 String standbyStatus = stateManagement.getStandbyStatus();
684 if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
686 * Only call promote if it is not already in the right state. Don't worry about
687 * synching the lower level topic endpoint states. That is done by the
690 stateManagement.promote();
692 } catch (StandbyStatusException e) {
695 ("ERROR: DesignatedWaiter.run: Caught StandbyStatusException attempting to promote PDP='"
699 myPdp.setDesignated(false);
700 pdpsConnector.setDesignated(myPdp,false);
701 isDesignated = false;
702 //If you can't promote it, demote it
704 String standbyStatus = stateManagement.getStandbyStatus();
705 if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
706 standbyStatus.equals(StateManagement.COLD_STANDBY))){
708 * Only call demote if it is not already in the right state. Don't worry about
709 * synching the lower level topic endpoint states. That is done by the
712 stateManagement.demote();
714 } catch (Exception e1) {
717 ("ERROR: DesignatedWaiter.run: Caught StandbyStatusException attempting to promote then demote PDP='"
721 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
722 + "from stateManagement.demote()");
723 e1.printStackTrace();
726 } catch (Exception e) {
729 ("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP='"
733 myPdp.setDesignated(false);
734 pdpsConnector.setDesignated(myPdp,false);
735 isDesignated = false;
736 //If you can't promote it, demote it
738 String standbyStatus = stateManagement.getStandbyStatus();
739 if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
740 standbyStatus.equals(StateManagement.COLD_STANDBY))){
742 * Only call demote if it is not already in the right state. Don't worry about
743 * synching the lower level topic endpoint states. That is done by the
746 stateManagement.demote();
748 } catch (Exception e1) {
751 ("ERROR: DesignatedWaiter.run: Caught StandbyStatusException attempting to promote then demote PDP='"
755 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception "
756 + "from stateManagement.demote()");
757 e1.printStackTrace();
761 waitTimerLastRunDate = new Date();
762 logger.info("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) waitTimerLastRunDate = " + waitTimerLastRunDate);
766 isDesignated = false;
768 } // end synchronized
772 ("DesignatedWaiter.run: myPdp: " + myPdp.getPdpId() + "; Returning, isDesignated=" + isDesignated);
774 Date tmpDate = new Date();
775 logger.info("DesignatedWaiter.run (end of run) waitTimerLastRunDate = " + tmpDate);
777 waitTimerLastRunDate = tmpDate;
780 logger.error("DesignatedWaiter.run caught an unexpected exception: " + e);
781 System.out.println(new Date() + " DesignatedWaiter.run: caught unexpected exception");
787 private class TimerUpdateClass extends TimerTask{
792 logger.info("TimerUpdateClass.run: entry");
794 synchronized(pdpsConnectorLock){
796 myPdp.setUpdatedDate(new Date());
797 if(myPdp.isDesignated()){
798 myPdp.setDesignatedDate(new Date());
800 pdpsConnector.update(myPdp);
802 Date tmpDate = new Date();
803 logger.info("TimerUpdateClass.run: updateWorkerLastRunDate = " + tmpDate);
805 updateWorkerLastRunDate = tmpDate;
807 logger.info("TimerUpdateClass.run.exit");
809 logger.error("TimerUpdateClass.run caught an unexpected exception: " + e);
810 System.out.println(new Date() + " TimerUpdateClass.run caught an unexpected exception");
816 public void checkThreadStatus() {
817 checkUpdateWorkerTimer();
821 private void checkUpdateWorkerTimer(){
822 synchronized(checkUpdateWorkerLock){
824 logger.debug("checkUpdateWorkerTimer: entry");
825 Date now = new Date();
826 long nowMs = now.getTime();
827 long updateWorkerMs = updateWorkerLastRunDate.getTime();
828 //give it 2 second cushion
829 if((nowMs - updateWorkerMs) > pdpCheckInterval + 2000){
830 logger.error("checkUpdateWorkerTimer: nowMs - updateWorkerMs = " + (nowMs - updateWorkerMs)
831 + ", exceeds pdpCheckInterval + 2000 = " + (pdpCheckInterval + 2000) + " Will reschedule updateWorker timer");
834 updateWorker.cancel();
835 // Recalculate the time because this is a synchronized section and the thread could have
838 nowMs = now.getTime();
839 updateWorker = new Timer();
840 // reset the updateWorkerLastRunDate
841 updateWorkerLastRunDate = new Date(nowMs + 100);
842 //execute the first time in 100 ms
843 updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
844 logger.info("checkUpdateWorkerTimer: Scheduling updateWorker timer to start in 100 ms ");
846 logger.error("checkUpdateWorkerTimer: Caught unexpected Exception: " + e);
847 System.out.println(new Date() + " checkUpdateWorkerTimer caught an unexpected exception");
849 // Recalculate the time because this is a synchronized section and the thread could have
852 nowMs = now.getTime();
853 updateWorker = new Timer();
854 updateWorkerLastRunDate = new Date(nowMs + 100);
855 updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
856 logger.info("checkUpdateWorkerTimer: Attempting to schedule updateWorker timer in 100 ms");
860 logger.debug("checkUpdateWorkerTimer: exit");
862 logger.error("checkUpdateWorkerTimer: caught unexpected exception: " + e);
863 System.out.println(new Date() + " checkUpdateWorkerTimer - top level - caught an unexpected exception");
869 private void checkWaitTimer(){
870 synchronized(checkWaitTimerLock){
872 logger.debug("checkWaitTimer: entry");
873 Date now = new Date();
874 long nowMs = now.getTime();
875 long waitTimerMs = waitTimerLastRunDate.getTime();
877 //give it 2 times leeway
878 if((nowMs - waitTimerMs) > 2*pdpUpdateInterval){
879 logger.error("checkWaitTimer: nowMs - waitTimerMs = " + (nowMs - waitTimerMs)
880 + ", exceeds pdpUpdateInterval + 2000 = " + (2*pdpUpdateInterval) + " Will reschedule waitTimer timer");
884 // Recalculate since the thread could have been stalled on the synchronize()
885 nowMs = (new Date()).getTime();
886 // Time to the start of the next pdpUpdateInterval multiple
887 long startMs = getDWaiterStartMs();
889 designationWaiter = new DesignationWaiter();
890 waitTimer = new Timer();
891 waitTimerLastRunDate = new Date(nowMs + startMs);
892 waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
893 logger.info("checkWaitTimer: Scheduling waitTimer timer to start in " + startMs + " ms");
895 logger.error("checkWaitTimer: Caught unexpected Exception: " + e);
896 System.out.println(new Date() + " checkWaitTimer caught an unexpected exception");
898 // Recalculate since the thread could have been stalled on the synchronize()
899 nowMs = (new Date()).getTime();
900 // Time to the start of the next pdpUpdateInterval multiple
901 long startMs = getDWaiterStartMs();
902 designationWaiter = new DesignationWaiter();
903 waitTimer = new Timer();
904 waitTimerLastRunDate = new Date(nowMs + startMs);
905 waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
906 logger.info("checkWaitTimer: Scheduling waitTimer timer in " + startMs + " ms");
910 logger.debug("checkWaitTimer: exit");
912 logger.error("checkWaitTimer: caught unexpected exception: " + e);
913 System.out.println(new Date() + " checkWaitTimer caught an unexpected exception");
919 private long getDWaiterStartMs(){
920 Date now = new Date();
922 // Retrieve the ms since the epoch
923 long nowMs = now.getTime();
925 // Time since the end of the last pdpUpdateInterval multiple
926 long nowModMs = nowMs % pdpUpdateInterval;
928 // Time to the start of the next pdpUpdateInterval multiple
929 long startMs = pdpUpdateInterval - nowModMs;
931 // Give the start time a minimum of a 5 second cushion
933 // Start at the beginning of following interval
934 startMs = pdpUpdateInterval + startMs;
939 private boolean nullSafeEquals(Object one, Object two){
940 if(one == null && two == null){
943 if(one != null && two != null){
944 return one.equals(two);