2 * ============LICENSE_START=======================================================
3 * feature-active-standby-management
4 * ================================================================================
5 * Copyright (C) 2017-2019 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.activestandby;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.List;
27 import java.util.Timer;
28 import java.util.TimerTask;
30 import org.onap.policy.common.im.StateManagement;
31 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class DroolsPdpsElectionHandler implements ThreadRunningChecker {
36 // get an instance of logger
37 private static final Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class);
38 private DroolsPdpsConnector pdpsConnector;
39 private Object checkWaitTimerLock = new Object();
40 private Object designationWaiterLock = new Object();
43 * Must be static, so it can be referenced by JpaDroolsPdpsConnector,
44 * without requiring a reference to the election handler instantiation.
46 private static DroolsPdp myPdp;
48 private DesignationWaiter designationWaiter;
49 private Timer updateWorker;
50 private Timer waitTimer;
51 private Date waitTimerLastRunDate;
53 // The interval between checks of the DesignationWaiter to be sure it is running.
54 private int pdpCheckInterval;
56 // The interval between runs of the DesignationWaiter
57 private int pdpUpdateInterval;
59 private volatile boolean isDesignated;
61 private String pdpdNowActive;
62 private String pdpdLastActive;
65 * Start allSeemsWell with a value of null so that, on the first run
66 * of the checkWaitTimer it will set the value in IntegrityMonitor
67 * regardless of whether it needs to be set to true or false.
69 private Boolean allSeemsWell = null;
71 private StateManagementFeatureApi stateManagementFeature;
73 private static boolean isUnitTesting = false;
74 private static boolean isStalled = false;
79 * @param pdps connectors
82 public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp) {
84 logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null");
85 throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null");
88 logger.error("DroolsPdpsElectinHandler(): droolsPdp==null");
89 throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null");
93 pdpdLastActive = null;
94 this.pdpsConnector = pdps;
95 DroolsPdpsElectionHandler.myPdp = myPdp;
96 this.isDesignated = false;
97 pdpCheckInterval = 3000;
99 pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(
100 ActiveStandbyProperties.PDP_CHECK_INVERVAL));
101 } catch (Exception e) {
102 logger.error("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e);
104 pdpUpdateInterval = 2000;
106 pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(
107 ActiveStandbyProperties.PDP_UPDATE_INTERVAL));
108 } catch (Exception e) {
109 logger.error("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e);
112 Date now = new Date();
114 // Retrieve the ms since the epoch
115 final long nowMs = now.getTime();
117 // Create the timer which will update the updateDate in DroolsPdpEntity table.
118 // This is the heartbeat
119 updateWorker = new Timer();
121 // Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter
122 // NOTE: The first run of the TimerUpdateClass results in myPdp being added to the
123 // drools droolsPdpEntity table.
124 updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
126 // Create the timer which will run the election algorithm
127 waitTimer = new Timer();
129 // Schedule it to start in startMs ms
130 // (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter
131 long startMs = getDWaiterStartMs();
132 designationWaiter = new DesignationWaiter();
133 waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
134 waitTimerLastRunDate = new Date(nowMs + startMs);
136 //Get the StateManagementFeature instance
138 for (StateManagementFeatureApi feature : StateManagementFeatureApi.impl.getList()) {
139 if (feature.getResourceName().equals(myPdp.getPdpId())) {
140 if (logger.isDebugEnabled()) {
141 logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature"
142 + " with resourceName: {}", myPdp.getPdpId());
144 stateManagementFeature = feature;
148 if (stateManagementFeature == null) {
149 logger.error("DroolsPdpsElectionHandler failed to initialize. "
150 + "Unable to get instance of StateManagementFeatureApi "
151 + "with resourceID: {}", myPdp.getPdpId());
155 public static void setIsUnitTesting(boolean val) {
159 public static void setIsStalled(boolean val) {
164 * When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs
165 * access to myPdp, so it can keep its designation status in sync with the
168 * @param designated is designated value
170 public static void setMyPdpDesignated(boolean designated) {
171 if (logger.isDebugEnabled()) {
172 logger.debug("setMyPdpDesignated: designated= {}", designated);
174 myPdp.setDesignated(designated);
177 private class DesignationWaiter extends TimerTask {
178 // get an instance of logger
179 private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class);
184 if (logger.isDebugEnabled()) {
185 logger.debug("DesignatedWaiter.run: Entering");
188 //This is for testing the checkWaitTimer
189 if (isUnitTesting && isStalled) {
190 if (logger.isDebugEnabled()) {
191 logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}",
192 isUnitTesting, isStalled);
197 synchronized (designationWaiterLock) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("DesignatedWaiter.run: Entering synchronized block");
203 //It is possible that multiple PDPs are designated lead. So, we will make a list of all designated
204 //PDPs and then decide which one really should be designated at the end.
205 List<DroolsPdp> listOfDesignated = new ArrayList<>();
207 Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps();
209 if (logger.isDebugEnabled()) {
210 logger.debug("DesignatedWaiter.run: pdps.size= {}", pdps.size());
213 //This is only true if all designated PDPs have failed
214 boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps);
215 if (logger.isDebugEnabled()) {
216 logger.debug("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed);
218 for (DroolsPdp pdp : pdps) {
219 if (logger.isDebugEnabled()) {
220 logger.debug("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId());
224 * Note: side effect of isPdpCurrent is that any stale but
225 * designated PDPs will be marked as un-designated.
227 boolean isCurrent = pdpsConnector.isPdpCurrent(pdp);
230 * We can't use stateManagement.getStandbyStatus() here, because
231 * we need the standbyStatus, not for this PDP, but for the PDP
232 * being processed by this loop iteration.
234 String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
235 if (standbyStatus == null) {
236 // Treat this case as a cold standby -- if we
237 // abort here, no sessions will be created in a
238 // single-node test environment.
239 standbyStatus = StateManagement.COLD_STANDBY;
241 if (logger.isDebugEnabled()) {
242 logger.debug("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent);
246 * There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn
247 * and evaluate the each pdp in the list of pdps against each combination.
249 * This is the first combination of isDesignated and isCurrent
251 if (pdp.isDesignated() && isCurrent) {
252 //It is current, but it could have a standbystatus=coldstandby / hotstandby
253 //If so, we need to stand it down and demote it
254 if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) {
255 if (pdp.getPdpId().equals(myPdp.getPdpId())) {
256 if (logger.isDebugEnabled()) {
257 logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
258 + "butstandbystatus is not providingservice. "
259 + " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId());
261 // So, we must demote it
263 //Keep the order like this. StateManagement is last since it
264 //triggers controller shutdown
265 //This will change isDesignated and it can enter another if(combination) below
266 pdpsConnector.standDownPdp(pdp.getPdpId());
267 myPdp.setDesignated(false);
268 isDesignated = false;
269 if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
270 || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
272 * Only demote it if it appears it has not already been demoted. Don't worry
273 * about synching with the topic endpoint states. That is done by the
276 stateManagementFeature.demote();
278 //update the standbystatus to check in a later
279 //combination of isDesignated and isCurrent
280 standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
281 } catch (Exception e) {
282 logger.error("DesignatedWaiter.run: myPdp: {} "
283 + "Caught Exception attempting to demote myPdp,"
284 + "message= {}", myPdp.getPdpId(), e);
287 // Don't demote a remote PDP that is current. It should catch itself
288 if (logger.isDebugEnabled()) {
289 logger.debug("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
290 + "but standbystatus is not providingservice. "
291 + " Cannot execute stateManagement.demote() "
292 + "since it it is not myPdp\n\n",
298 // If we get here, it is ok to be on the list
299 if (logger.isDebugEnabled()) {
300 logger.debug("DesignatedWaiter.run: PDP= {} is designated, "
301 + "current and {} Noting PDP as "
302 + "designated, standbyStatus= {}",
303 pdp.getPdpId(), standbyStatus, standbyStatus);
305 listOfDesignated.add(pdp);
313 * The second combination of isDesignated and isCurrent
315 * PDP is designated but not current; it has failed.
316 * So we stand it down (it doesn't matter what
317 * its standbyStatus is). None of these go on the list.
319 if (pdp.isDesignated() && !isCurrent) {
320 if (logger.isDebugEnabled()) {
321 logger.debug("INFO: DesignatedWaiter.run: PDP= {} is currently "
322 + "designated but is not current; "
323 + "it has failed. Standing down. standbyStatus= {}",
324 pdp.getPdpId(), standbyStatus);
327 * Changes designated to 0 but it is still potentially providing service
328 * Will affect isDesignated, so, it can enter an if(combination) below
330 pdpsConnector.standDownPdp(pdp.getPdpId());
332 //need to change standbystatus to coldstandby
333 if (pdp.getPdpId().equals(myPdp.getPdpId())) {
334 if (logger.isDebugEnabled()) {
335 logger.debug("\n\nDesignatedWaiter.run: myPdp {} is not Current. "
336 + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
338 // We found that myPdp is designated but not current
339 // So, we must cause it to disableFail
341 myPdp.setDesignated(false);
342 pdpsConnector.setDesignated(myPdp, false);
343 isDesignated = false;
344 stateManagementFeature.disableFailed();
345 } catch (Exception e) {
346 logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception "
347 + "attempting to disableFail myPdp {}, message= {}",
348 myPdp.getPdpId(), myPdp.getPdpId(), e);
350 } else { //it is a remote PDP that is failed
351 if (logger.isDebugEnabled()) {
352 logger.debug("\n\nDesignatedWaiter.run: PDP {} is not Current. "
353 + " Executing stateManagement.disableFailed(otherResourceName)\n\n",
356 // We found a PDP is designated but not current
357 // We already called standdown(pdp) which will change designated to false
358 // Now we need to disableFail it to get its states in synch. The standbyStatus
359 // should equal coldstandby
361 stateManagementFeature.disableFailed(pdp.getPdpId());
362 } catch (Exception e) {
363 logger.error("DesignatedWaiter.run: for PDP {} Caught Exception attempting to "
364 + "disableFail({}), message= {}",
365 pdp.getPdpId(), pdp.getPdpId(), e);
369 continue; //we are not going to do anything else with this pdp
373 * The third combination of isDesignated and isCurrent
375 * If a PDP is not currently designated but is providing service
376 * (erroneous, but recoverable) or hot standby
377 * we can add it to the list of possible designated if all the designated have failed
379 if (!pdp.isDesignated() && isCurrent) {
380 if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
381 || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
382 if (logger.isDebugEnabled()) {
383 logger.debug("\n\nDesignatedWaiter.run: PDP {}"
384 + " is NOT designated but IS current and"
385 + " has a standbystatus= {}", pdp.getPdpId(), standbyStatus);
387 // Since it is current, we assume it can adjust its own state.
388 // We will demote if it is myPdp
389 if (pdp.getPdpId().equals(myPdp.getPdpId())) {
391 if (logger.isDebugEnabled()) {
392 logger.debug("DesignatedWaiter.run: PDP {} going to "
393 + "setDesignated = false and calling stateManagement.demote",
397 //Keep the order like this.
398 //StateManagement is last since it triggers controller shutdown
399 pdpsConnector.setDesignated(myPdp, false);
400 myPdp.setDesignated(false);
401 isDesignated = false;
402 //This is definitely not a redundant call.
403 //It is attempting to correct a problem
404 stateManagementFeature.demote();
405 //recheck the standbystatus
406 standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
407 } catch (Exception e) {
408 logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception "
409 + "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(),
410 myPdp.getPdpId(), e);
415 if (standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed) {
417 if (logger.isDebugEnabled()) {
418 logger.debug("INFO: DesignatedWaiter.run: PDP= {}"
419 + " is not designated but is {} and designated PDP "
420 + "has failed. standbyStatus= {}", pdp.getPdpId(),
421 standbyStatus, standbyStatus);
423 listOfDesignated.add(pdp);
425 continue; //done with this one
429 * The fourth combination of isDesignated and isCurrent
431 * We are not going to put any of these on the list since it appears they have failed.
435 if (!pdp.isDesignated() && !isCurrent) {
436 if (logger.isDebugEnabled()) {
437 logger.debug("INFO: DesignatedWaiter.run: PDP= {} "
438 + "designated= {}, current= {}, "
439 + "designatedPdpHasFailed= {}, "
440 + "standbyStatus= {}",pdp.getPdpId(),
441 pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus);
443 if (!standbyStatus.equals(StateManagement.COLD_STANDBY)) {
446 pdpsConnector.standDownPdp(pdp.getPdpId());
447 if (pdp.getPdpId().equals(myPdp.getPdpId())) {
449 * I don't actually know how this condition could happen,
450 * but if it did, we would want
451 * to declare it failed.
453 if (logger.isDebugEnabled()) {
454 logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
455 + " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
457 // So, we must disableFail it
459 //Keep the order like this.
460 //StateManagement is last since it triggers controller shutdown
461 pdpsConnector.setDesignated(myPdp, false);
462 myPdp.setDesignated(false);
463 isDesignated = false;
464 stateManagementFeature.disableFailed();
465 } catch (Exception e) {
466 logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
467 + "disableFail myPdp {}, message= {}",
468 myPdp.getPdpId(), myPdp.getPdpId(), e);
470 } else { //it is remote
471 if (logger.isDebugEnabled()) {
472 logger.debug("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
473 + " Executing stateManagement.disableFailed({})\n\n",
474 myPdp.getPdpId(), pdp.getPdpId());
476 // We already called standdown(pdp) which will change designated to false
477 // Now we need to disableFail it to get its states in sync.
478 // StandbyStatus = coldstandby
480 stateManagementFeature.disableFailed(pdp.getPdpId());
481 } catch (Exception e) {
482 logger.error("DesignatedWaiter.run: for PDP {}"
483 + " Caught Exception attempting to disableFail({})"
484 + ", message=", pdp.getPdpId(), pdp.getPdpId(), e);
494 * We have checked the four combinations of isDesignated and isCurrent. Where appropriate,
495 * we added the PDPs to the potential list of designated pdps
497 * We need to give priority to pdps on the same site that is currently being used
498 * First, however, we must sanitize the list of designated to make sure their are
499 * only designated members or non-designated members. There should not be both in
500 * the list. Because there are real time delays, it is possible that both types could
504 listOfDesignated = santizeDesignatedList(listOfDesignated);
507 * We need to figure out the last pdp that was the primary so we can get the last site
508 * name and the last session numbers. We need to create a "dummy" droolspdp since
509 * it will be used in later comparisons and cannot be null.
512 DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated);
514 if (mostRecentPrimary != null) {
515 pdpdLastActive = mostRecentPrimary.getPdpId();
520 * It is possible to get here with more than one pdp designated and providingservice. This normally
521 * occurs when there is a race condition with multiple nodes coming up at the same time. If that is
522 * the case we must determine which one is the one that should be designated and which one should
525 * It is possible to have 0, 1, 2 or more but not all, or all designated.
526 * If we have one designated and current, we chose it and are done
527 * If we have 2 or more, but not all, we must determine which one is in the same site as
528 * the previously designated pdp.
530 DroolsPdp designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
531 if (designatedPdp != null) {
532 pdpdNowActive = designatedPdp.getPdpId();
535 if (designatedPdp == null) {
536 logger.warn("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. "
537 + "designatedPdp still null.");
538 // Just to be sure the parameters are correctly set
539 myPdp.setDesignated(false);
540 pdpsConnector.setDesignated(myPdp,false);
541 isDesignated = false;
543 waitTimerLastRunDate = new Date();
544 if (logger.isDebugEnabled()) {
545 logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}",
546 waitTimerLastRunDate);
548 myPdp.setUpdatedDate(waitTimerLastRunDate);
549 pdpsConnector.update(myPdp);
553 } else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) {
554 if (logger.isDebugEnabled()) {
555 logger.debug("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId());
558 * update function expects myPdp.isDesignated to be true.
561 //Keep the order like this. StateManagement is last since it triggers controller init
562 myPdp.setDesignated(true);
563 myPdp.setDesignatedDate(new Date());
564 pdpsConnector.setDesignated(myPdp, true);
566 String standbyStatus = stateManagementFeature.getStandbyStatus();
567 if (!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)) {
569 * Only call promote if it is not already in the right state. Don't worry about
570 * synching the lower level topic endpoint states. That is done by the
572 * Note that we need to fetch the session list from 'mostRecentPrimary'
573 * at this point -- soon, 'mostRecentPrimary' will be set to this host.
575 //this.sessions = mostRecentPrimary.getSessions();
576 stateManagementFeature.promote();
578 } catch (Exception e) {
579 logger.error("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}"
580 + ", message=", myPdp.getPdpId(), e);
581 myPdp.setDesignated(false);
582 pdpsConnector.setDesignated(myPdp,false);
583 isDesignated = false;
584 //If you can't promote it, demote it
586 String standbyStatus = stateManagementFeature.getStandbyStatus();
587 if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
588 || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
590 * Only call demote if it is not already in the right state. Don't worry about
591 * synching the lower level topic endpoint states. That is done by the
594 stateManagementFeature.demote();
596 } catch (Exception e1) {
597 logger.error("ERROR: DesignatedWaiter.run: Caught StandbyStatusException "
598 + "attempting to promote then demote PDP={}, message=",
599 myPdp.getPdpId(), e1);
603 waitTimerLastRunDate = new Date();
604 if (logger.isDebugEnabled()) {
605 logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) "
606 + "waitTimerLastRunDate = " + waitTimerLastRunDate);
608 myPdp.setUpdatedDate(waitTimerLastRunDate);
609 pdpsConnector.update(myPdp);
613 isDesignated = false;
615 } // end synchronized
616 if (logger.isDebugEnabled()) {
617 logger.debug("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}",
618 isDesignated, myPdp.getPdpId());
621 Date tmpDate = new Date();
622 if (logger.isDebugEnabled()) {
623 logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate);
626 waitTimerLastRunDate = tmpDate;
627 myPdp.setUpdatedDate(waitTimerLastRunDate);
628 pdpsConnector.update(myPdp);
630 } catch (Exception e) {
631 logger.error("DesignatedWaiter.run caught an unexpected exception: ", e);
637 * Sanitize designated list.
639 * @param listOfDesignated list of designated pdps
640 * @return list of drools pdps
642 public List<DroolsPdp> santizeDesignatedList(List<DroolsPdp> listOfDesignated) {
644 boolean containsDesignated = false;
645 boolean containsHotStandby = false;
646 List<DroolsPdp> listForRemoval = new ArrayList<>();
647 for (DroolsPdp pdp : listOfDesignated) {
648 if (logger.isDebugEnabled()) {
649 logger.debug("DesignatedWaiter.run sanitizing: pdp = {}"
650 + " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated());
652 if (pdp.isDesignated()) {
653 containsDesignated = true;
655 containsHotStandby = true;
656 listForRemoval.add(pdp);
659 if (containsDesignated && containsHotStandby) {
660 //remove the hot standby from the list
661 listOfDesignated.removeAll(listForRemoval);
663 return listOfDesignated;
667 * Compute most recent primary.
669 * @param pdps collection of pdps
670 * @param listOfDesignated list of designated pdps
671 * @return drools pdp object
673 public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, List<DroolsPdp> listOfDesignated) {
674 boolean containsDesignated = false;
675 for (DroolsPdp pdp : listOfDesignated) {
676 if (pdp.isDesignated()) {
677 containsDesignated = true;
680 DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0));
681 mostRecentPrimary.setSiteName(null);
682 if (logger.isDebugEnabled()) {
683 logger.debug("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size());
685 if (listOfDesignated.size() <= 1) {
686 if (logger.isDebugEnabled()) {
687 logger.debug("DesignatedWainter.run: listOfDesignated.size <=1");
689 //Only one or none is designated or hot standby. Choose the latest designated date
690 for (DroolsPdp pdp : pdps) {
691 if (logger.isDebugEnabled()) {
692 logger.debug("DesignatedWaiter.run pdp = {}"
693 + " pdp.getDesignatedDate() = {}",
694 pdp.getPdpId(), pdp.getDesignatedDate());
696 if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
697 mostRecentPrimary = pdp;
698 if (logger.isDebugEnabled()) {
699 logger.debug("DesignatedWaiter.run mostRecentPrimary = {}",
700 mostRecentPrimary.getPdpId());
704 } else if (listOfDesignated.size() == pdps.size()) {
705 if (logger.isDebugEnabled()) {
706 logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size());
708 //They are all designated or all hot standby.
709 mostRecentPrimary = null;
710 for (DroolsPdp pdp : pdps) {
711 if (mostRecentPrimary == null) {
712 mostRecentPrimary = pdp;
715 if (containsDesignated) { //Choose the site of the first designated date
716 if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0) {
717 mostRecentPrimary = pdp;
718 if (logger.isDebugEnabled()) {
719 logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
722 } else { //Choose the site with the latest designated date
723 if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
724 mostRecentPrimary = pdp;
725 if (logger.isDebugEnabled()) {
726 logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
732 if (logger.isDebugEnabled()) {
733 logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. ");
735 //Some but not all are designated or hot standby.
736 if (containsDesignated) {
737 if (logger.isDebugEnabled()) {
738 logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
741 * The list only contains designated. This is a problem. It is most likely a race
742 * condition that resulted in two thinking they should be designated. Choose the
743 * site with the latest designated date for the pdp not included on the designated list.
744 * This should be the site that had the last designation before this race condition
747 for (DroolsPdp pdp : pdps) {
748 if (listOfDesignated.contains(pdp)) {
749 continue; //Don't consider this entry
751 if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
752 mostRecentPrimary = pdp;
753 if (logger.isDebugEnabled()) {
754 logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
759 if (logger.isDebugEnabled()) {
760 logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
762 //The list only contains hot standby. Choose the site of the latest designated date
763 for (DroolsPdp pdp : pdps) {
764 if (pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0) {
765 mostRecentPrimary = pdp;
766 if (logger.isDebugEnabled()) {
767 logger.debug("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
773 return mostRecentPrimary;
777 * Compue designated pdp.
779 * @param listOfDesignated list of designated pdps
780 * @param mostRecentPrimary most recent primary pdpd
781 * @return drools pdp object
783 public DroolsPdp computeDesignatedPdp(List<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary) {
784 DroolsPdp designatedPdp = null;
785 DroolsPdp lowestPriorityPdp = null;
786 if (listOfDesignated.size() > 1) {
787 if (logger.isDebugEnabled()) {
788 logger.debug("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(),
789 listOfDesignated.size());
791 DroolsPdp rejectedPdp = null;
792 DroolsPdp lowestPrioritySameSite = null;
793 DroolsPdp lowestPriorityDifferentSite = null;
794 for (DroolsPdp pdp : listOfDesignated) {
795 // We need to determine if another PDP is the lowest priority
796 if (nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())) {
797 if (lowestPrioritySameSite == null) {
798 if (lowestPriorityDifferentSite != null) {
799 rejectedPdp = lowestPriorityDifferentSite;
801 lowestPrioritySameSite = pdp;
803 if (pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))) {
804 continue;//nothing to compare
806 if (pdp.comparePriority(lowestPrioritySameSite) < 0) {
807 if (logger.isDebugEnabled()) {
808 logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
809 + " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(),
810 lowestPrioritySameSite.getPdpId());
812 //we need to reject lowestPrioritySameSite
813 rejectedPdp = lowestPrioritySameSite;
814 lowestPrioritySameSite = pdp;
816 //we need to reject pdp and keep lowestPrioritySameSite
817 if (logger.isDebugEnabled()) {
818 logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} "
819 + " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(),
820 lowestPrioritySameSite.getPdpId());
826 if (lowestPrioritySameSite != null) {
827 //if we already have a candidate for same site, we don't want to bother with different sites
830 if (lowestPriorityDifferentSite == null) {
831 lowestPriorityDifferentSite = pdp;
834 if (pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))) {
835 continue;//nothing to compare
837 if (pdp.comparePriority(lowestPriorityDifferentSite) < 0) {
838 if (logger.isDebugEnabled()) {
839 logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
840 + " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
841 lowestPriorityDifferentSite.getPdpId());
843 //we need to reject lowestPriorityDifferentSite
844 rejectedPdp = lowestPriorityDifferentSite;
845 lowestPriorityDifferentSite = pdp;
847 //we need to reject pdp and keep lowestPriorityDifferentSite
848 if (logger.isDebugEnabled()) {
849 logger.debug("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
850 + " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
851 lowestPriorityDifferentSite.getPdpId());
857 // If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible
858 // for demoting itself
859 if (rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())) {
860 if (logger.isDebugEnabled()) {
861 logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}"
862 + " is NOT the lowest priority. Executing stateManagement.demote()\n\n",
866 // We found that myPdp is on the listOfDesignated and it is not the lowest priority
867 // So, we must demote it
869 //Keep the order like this. StateManagement is last since it triggers controller shutdown
870 myPdp.setDesignated(false);
871 pdpsConnector.setDesignated(myPdp, false);
872 isDesignated = false;
873 String standbyStatus = stateManagementFeature.getStandbyStatus();
874 if (!(standbyStatus.equals(StateManagement.HOT_STANDBY)
875 || standbyStatus.equals(StateManagement.COLD_STANDBY))) {
877 * Only call demote if it is not already in the right state. Don't worry about
878 * synching the lower level topic endpoint states. That is done by the
881 stateManagementFeature.demote();
883 } catch (Exception e) {
884 myPdp.setDesignated(false);
885 pdpsConnector.setDesignated(myPdp, false);
886 isDesignated = false;
887 logger.error("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
888 + "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(),
892 } //end: for(DroolsPdp pdp : listOfDesignated)
893 if (lowestPrioritySameSite != null) {
894 lowestPriorityPdp = lowestPrioritySameSite;
896 lowestPriorityPdp = lowestPriorityDifferentSite;
898 //now we have a valid value for lowestPriorityPdp
899 if (logger.isDebugEnabled()) {
900 logger.debug("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
901 + "found the LOWEST priority pdp ID: {} "
902 + " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n",
903 myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp);
905 designatedPdp = lowestPriorityPdp;
907 } else if (listOfDesignated.isEmpty()) {
908 if (logger.isDebugEnabled()) {
909 logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId());
911 designatedPdp = null;
912 } else { //only one in listOfDesignated
913 if (logger.isDebugEnabled()) {
914 logger.debug("\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
915 + "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId());
917 designatedPdp = listOfDesignated.get(0);
919 return designatedPdp;
923 private class TimerUpdateClass extends TimerTask {
928 if (logger.isDebugEnabled()) {
929 logger.debug("TimerUpdateClass.run: entry");
932 } catch (Exception e) {
933 logger.error("TimerUpdateClass.run caught an unexpected exception: ", e);
935 if (logger.isDebugEnabled()) {
936 logger.debug("TimerUpdateClass.run.exit");
942 public void checkThreadStatus() {
946 private void checkWaitTimer() {
947 synchronized (checkWaitTimerLock) {
949 if (logger.isDebugEnabled()) {
950 logger.debug("checkWaitTimer: entry");
952 Date now = new Date();
953 long nowMs = now.getTime();
954 long waitTimerMs = waitTimerLastRunDate.getTime();
956 //give it 10 times leeway
957 if ((nowMs - waitTimerMs) > 10 * pdpUpdateInterval) {
958 if (allSeemsWell == null || allSeemsWell) {
959 allSeemsWell = false;
960 if (logger.isDebugEnabled()) {
961 logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param");
963 stateManagementFeature.allSeemsWell(this.getClass().getName(),
964 StateManagementFeatureApi.ALLNOTWELL_STATE,
965 "DesignationWaiter/ElectionHandler has STALLED");
967 logger.error("checkWaitTimer: nowMs - waitTimerMs = {}"
968 + ", exceeds 10* pdpUpdateInterval = {}"
969 + " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10 * pdpUpdateInterval));
970 } else if (allSeemsWell == null || !allSeemsWell) {
972 stateManagementFeature.allSeemsWell(this.getClass().getName(),
973 StateManagementFeatureApi.ALLSEEMSWELL_STATE,
974 "DesignationWaiter/ElectionHandler has RESUMED");
975 logger.info("DesignationWaiter/ElectionHandler has RESUMED");
977 if (logger.isDebugEnabled()) {
978 logger.debug("checkWaitTimer: exit");
980 } catch (Exception e) {
981 logger.error("checkWaitTimer: caught unexpected exception: ", e);
986 private long getDWaiterStartMs() {
987 Date now = new Date();
989 // Retrieve the ms since the epoch
990 long nowMs = now.getTime();
992 // Time since the end of the last pdpUpdateInterval multiple
993 long nowModMs = nowMs % pdpUpdateInterval;
995 // Time to the start of the next pdpUpdateInterval multiple
996 long startMs = 2 * pdpUpdateInterval - nowModMs;
998 // Give the start time a minimum of a 5 second cushion
999 if (startMs < 5000) {
1000 // Start at the beginning of following interval
1001 startMs = pdpUpdateInterval + startMs;
1006 private boolean nullSafeEquals(Object one, Object two) {
1007 if (one == null && two == null) {
1010 if (one != null && two != null) {
1011 return one.equals(two);
1016 public String getPdpdNowActive() {
1017 return pdpdNowActive;
1020 public String getPdpdLastActive() {
1021 return pdpdLastActive;