1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
|
/*-
* ============LICENSE_START=======================================================
* feature-active-standby-management
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.onap.policy.drools.activestandby;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import org.onap.policy.common.im.StateManagement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
public class DroolsPdpsElectionHandler implements ThreadRunningChecker {
// get an instance of logger
private final static Logger logger = LoggerFactory.getLogger(DroolsPdpsElectionHandler.class);
private DroolsPdpsConnector pdpsConnector;
private Object checkWaitTimerLock = new Object();
private Object designationWaiterLock = new Object();
/*
* Must be static, so it can be referenced by JpaDroolsPdpsConnector,
* without requiring a reference to the election handler instantiation.
*/
private static DroolsPdp myPdp;
private DesignationWaiter designationWaiter;
private Timer updateWorker;
private Timer waitTimer;
private Date waitTimerLastRunDate;
// The interval between checks of the DesignationWaiter to be sure it is running.
private int pdpCheckInterval;
// The interval between runs of the DesignationWaiter
private int pdpUpdateInterval;
private volatile boolean isDesignated;
private String pdpdNowActive;
private String pdpdLastActive;
/*
* Start allSeemsWell with a value of null so that, on the first run
* of the checkWaitTimer it will set the value in IntegrityMonitor
* regardless of whether it needs to be set to true or false.
*/
private Boolean allSeemsWell=null;
private StateManagementFeatureAPI stateManagementFeature;
private static boolean isUnitTesting = false;
public static void setIsUnitTesting(boolean val){
isUnitTesting = val;
}
private static boolean isStalled = false;
public static void setIsStalled(boolean val){
isStalled = val;
}
public DroolsPdpsElectionHandler(DroolsPdpsConnector pdps, DroolsPdp myPdp){
if (pdps == null) {
logger.error("DroolsPdpsElectinHandler(): pdpsConnector==null");
throw new IllegalArgumentException("DroolsPdpsElectinHandler(): pdpsConnector==null");
}
if (myPdp == null){
logger.error("DroolsPdpsElectinHandler(): droolsPdp==null");
throw new IllegalArgumentException("DroolsPdpsElectinHandler(): DroolsPdp==null");
}
pdpdNowActive = null;
pdpdLastActive = null;
this.pdpsConnector = pdps;
DroolsPdpsElectionHandler.myPdp = myPdp;
this.isDesignated = false;
pdpCheckInterval = 3000;
try{
pdpCheckInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_CHECK_INVERVAL));
}catch(Exception e){
logger.error
("Could not get pdpCheckInterval property. Using default {}",pdpCheckInterval, e);
}
pdpUpdateInterval = 2000;
try{
pdpUpdateInterval = Integer.parseInt(ActiveStandbyProperties.getProperty(ActiveStandbyProperties.PDP_UPDATE_INTERVAL));
}catch(Exception e){
logger.error
("Could not get pdpUpdateInterval property. Using default {} ", pdpUpdateInterval, e);
}
Date now = new Date();
// Retrieve the ms since the epoch
long nowMs = now.getTime();
// Create the timer which will update the updateDate in DroolsPdpEntity table.
// This is the heartbeat
updateWorker = new Timer();
// Schedule the TimerUpdateClass to run at 100 ms and run at pdpCheckInterval ms thereafter
// NOTE: The first run of the TimerUpdateClass results in myPdp being added to the
// drools droolsPdpEntity table.
updateWorker.scheduleAtFixedRate(new TimerUpdateClass(), 100, pdpCheckInterval);
// Create the timer which will run the election algorithm
waitTimer = new Timer();
// Schedule it to start in startMs ms (so it will run after the updateWorker and run at pdpUpdateInterval ms thereafter
long startMs = getDWaiterStartMs();
designationWaiter = new DesignationWaiter();
waitTimer.scheduleAtFixedRate(designationWaiter, startMs, pdpUpdateInterval);
waitTimerLastRunDate = new Date(nowMs + startMs);
//Get the StateManagementFeature instance
for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
{
if (feature.getResourceName().equals(myPdp.getPdpId()))
{
if(logger.isDebugEnabled()){
logger.debug("DroolsPdpsElectionHandler: Found StateManagementFeature"
+ " with resourceName: {}", myPdp.getPdpId());
}
stateManagementFeature = feature;
break;
}
}
if(stateManagementFeature == null){
logger.error("DroolsPdpsElectionHandler failed to initialize. "
+ "Unable to get instance of StateManagementFeatureAPI "
+ "with resourceID: {}", myPdp.getPdpId());
}
}
/*
* When the JpaDroolsPdpsConnector.standDown() method is invoked, it needs
* access to myPdp, so it can keep its designation status in sync with the
* DB.
*/
public static void setMyPdpDesignated(boolean designated) {
if(logger.isDebugEnabled()){
logger.debug
("setMyPdpDesignated: designated= {}", designated);
}
myPdp.setDesignated(designated);
}
private class DesignationWaiter extends TimerTask {
// get an instance of logger
private final Logger logger = LoggerFactory.getLogger(DesignationWaiter.class);
@Override
public void run() {
try{
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: Entering");
}
//This is for testing the checkWaitTimer
if(isUnitTesting && isStalled){
if(logger.isDebugEnabled()){
logger.debug("DesignatedWaiter.run: isUnitTesting = {} isStalled = {}", isUnitTesting, isStalled);
}
return;
}
synchronized (designationWaiterLock) {
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: Entering synchronized block");
}
//It is possible that multiple PDPs are designated lead. So, we will make a list of all designated
//PDPs and then decide which one really should be designated at the end.
ArrayList<DroolsPdp> listOfDesignated = new ArrayList<DroolsPdp>();
Collection<DroolsPdp> pdps = pdpsConnector.getDroolsPdps();
DroolsPdp designatedPdp = null;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: pdps.size= {}", pdps.size());
}
//This is only true if all designated PDPs have failed
boolean designatedPdpHasFailed = pdpsConnector.hasDesignatedPdpFailed(pdps);
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: designatedPdpHasFailed= {}", designatedPdpHasFailed);
}
for (DroolsPdp pdp : pdps) {
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: evaluating pdp ID: {}", pdp.getPdpId());
}
/*
* Note: side effect of isPdpCurrent is that any stale but
* designated PDPs will be marked as un-designated.
*/
boolean isCurrent = pdpsConnector.isPdpCurrent(pdp);
/*
* We can't use stateManagement.getStandbyStatus() here, because
* we need the standbyStatus, not for this PDP, but for the PDP
* being processed by this loop iteration.
*/
String standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
if(standbyStatus==null){
// Treat this case as a cold standby -- if we
// abort here, no sessions will be created in a
// single-node test environment.
standbyStatus = StateManagement.COLD_STANDBY;
}
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: PDP= {}, isCurrent= {}", pdp.getPdpId(), isCurrent);
}
/*
* There are 4 combinations of isDesignated and isCurrent. We will examine each one in-turn
* and evaluate the each pdp in the list of pdps against each combination.
*
* This is the first combination of isDesignated and isCurrent
*/
if (pdp.isDesignated() && isCurrent) {
//It is current, but it could have a standbystatus=coldstandby / hotstandby
//If so, we need to stand it down and demote it
if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
if(pdp.getPdpId().equals(myPdp.getPdpId())){
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
+ "butstandbystatus is not providingservice. "
+ " Executing stateManagement.demote()" + "\n\n", myPdp.getPdpId());
}
// So, we must demote it
try {
//Keep the order like this. StateManagement is last since it triggers controller shutdown
//This will change isDesignated and it can enter another if(combination) below
pdpsConnector.standDownPdp(pdp.getPdpId());
myPdp.setDesignated(false);
isDesignated = false;
if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
standbyStatus.equals(StateManagement.COLD_STANDBY))){
/*
* Only demote it if it appears it has not already been demoted. Don't worry
* about synching with the topic endpoint states. That is done by the
* refreshStateAudit
*/
stateManagementFeature.demote();
}
//update the standbystatus to check in a later combination of isDesignated and isCurrent
standbyStatus=stateManagementFeature.getStandbyStatus(pdp.getPdpId());
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: myPdp: {} "
+ "Caught Exception attempting to demote myPdp,"
+ "message= {}", myPdp.getPdpId(), e);
}
}else{
// Don't demote a remote PDP that is current. It should catch itself
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp {} is current and designated, "
+ "but standbystatus is not providingservice. "
+ " Cannot execute stateManagement.demote() since it it is not myPdp\n\n", myPdp.getPdpId());
}
}
}else{
// If we get here, it is ok to be on the list
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: PDP= {} is designated, current and {} Noting PDP as "
+ "designated, standbyStatus= {}", pdp.getPdpId(), standbyStatus, standbyStatus);
}
listOfDesignated.add(pdp);
}
}
/*
* The second combination of isDesignated and isCurrent
*
* PDP is designated but not current; it has failed. So we stand it down (it doesn't matter what
* its standbyStatus is). None of these go on the list.
*/
if (pdp.isDesignated() && !isCurrent) {
if(logger.isDebugEnabled()){
logger.debug
("INFO: DesignatedWaiter.run: PDP= {} is currently designated but is not current; "
+ "it has failed. Standing down. standbyStatus= {}", pdp.getPdpId(), standbyStatus);
}
/*
* Changes designated to 0 but it is still potentially providing service
* Will affect isDesignated, so, it can enter an if(combination) below
*/
pdpsConnector.standDownPdp(pdp.getPdpId());
//need to change standbystatus to coldstandby
if (pdp.getPdpId().equals(myPdp.getPdpId())){
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp {} is not Current. "
+ " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
}
// We found that myPdp is designated but not current
// So, we must cause it to disableFail
try {
myPdp.setDesignated(false);
pdpsConnector.setDesignated(myPdp, false);
isDesignated = false;
stateManagementFeature.disableFailed();
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: myPdp: {} Caught Exception "
+ "attempting to disableFail myPdp {}, message= {}",
myPdp.getPdpId(), myPdp.getPdpId(), e);
}
} else { //it is a remote PDP that is failed
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: PDP {} is not Current. "
+ " Executing stateManagement.disableFailed(otherResourceName)\n\n", pdp.getPdpId() );
}
// We found a PDP is designated but not current
// We already called standdown(pdp) which will change designated to false
// Now we need to disableFail it to get its states in synch. The standbyStatus
// should equal coldstandby
try {
stateManagementFeature.disableFailed(pdp.getPdpId());
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: for PDP {} Caught Exception attempting to "
+ "disableFail({}), message= {}",
pdp.getPdpId(), pdp.getPdpId(), e);
}
}
continue; //we are not going to do anything else with this pdp
}
/*
* The third combination of isDesignated and isCurrent
* /*
* If a PDP is not currently designated but is providing service (erroneous, but recoverable) or hot standby
* we can add it to the list of possible designated if all the designated have failed
*/
if (!pdp.isDesignated() && isCurrent){
if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
standbyStatus.equals(StateManagement.COLD_STANDBY))){
if(logger.isDebugEnabled()){
logger.debug("\n\nDesignatedWaiter.run: PDP {}"
+ " is NOT designated but IS current and"
+ " has a standbystatus= {}", pdp.getPdpId(), standbyStatus);
}
// Since it is current, we assume it can adjust its own state.
// We will demote if it is myPdp
if(pdp.getPdpId().equals(myPdp.getPdpId())){
//demote it
if(logger.isDebugEnabled()){
logger.debug("DesignatedWaiter.run: PDP {} going to "
+ "setDesignated = false and calling stateManagement.demote", pdp.getPdpId());
}
try {
//Keep the order like this. StateManagement is last since it triggers controller shutdown
pdpsConnector.setDesignated(myPdp, false);
myPdp.setDesignated(false);
isDesignated = false;
//This is definitely not a redundant call. It is attempting to correct a problem
stateManagementFeature.demote();
//recheck the standbystatus
standbyStatus = stateManagementFeature.getStandbyStatus(pdp.getPdpId());
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: myPdp: {} Caught Exception "
+ "attempting to demote myPdp {}, message = {}", myPdp.getPdpId(),
myPdp.getPdpId(), e);
}
}
}
if(standbyStatus.equals(StateManagement.HOT_STANDBY) && designatedPdpHasFailed){
//add it to the list
if(logger.isDebugEnabled()){
logger.debug
("INFO: DesignatedWaiter.run: PDP= {}"
+ " is not designated but is {} and designated PDP "
+ "has failed. standbyStatus= {}", pdp.getPdpId(),
standbyStatus, standbyStatus);
}
listOfDesignated.add(pdp);
}
continue; //done with this one
}
/*
* The fourth combination of isDesignated and isCurrent
*
* We are not going to put any of these on the list since it appears they have failed.
*
*/
if(!pdp.isDesignated() && !isCurrent) {
if(logger.isDebugEnabled()){
logger.debug
("INFO: DesignatedWaiter.run: PDP= {} "
+ "designated= {}, current= {}, "
+ "designatedPdpHasFailed= {}, "
+ "standbyStatus= {}",pdp.getPdpId(),
pdp.isDesignated(), isCurrent, designatedPdpHasFailed, standbyStatus);
}
if(!standbyStatus.equals(StateManagement.COLD_STANDBY)){
//stand it down
//disableFail it
pdpsConnector.standDownPdp(pdp.getPdpId());
if(pdp.getPdpId().equals(myPdp.getPdpId())){
/*
* I don't actually know how this condition could happen, but if it did, we would want
* to declare it failed.
*/
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
+ " Executing stateManagement.disableFailed()\n\n", myPdp.getPdpId());
}
// So, we must disableFail it
try {
//Keep the order like this. StateManagement is last since it triggers controller shutdown
pdpsConnector.setDesignated(myPdp, false);
myPdp.setDesignated(false);
isDesignated = false;
stateManagementFeature.disableFailed();
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
+ "disableFail myPdp {}, message= {}",
myPdp.getPdpId(), myPdp.getPdpId(), e);
}
}else{//it is remote
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp {} is !current and !designated, "
+ " Executing stateManagement.disableFailed({})\n\n",
myPdp.getPdpId(), pdp.getPdpId());
}
// We already called standdown(pdp) which will change designated to false
// Now we need to disableFail it to get its states in sync. StandbyStatus = coldstandby
try {
stateManagementFeature.disableFailed(pdp.getPdpId());
} catch (Exception e) {
logger.error
("DesignatedWaiter.run: for PDP {}"
+ " Caught Exception attempting to disableFail({})"
+ ", message=", pdp.getPdpId(), pdp.getPdpId(), e);
}
}
}
}
} // end pdps loop
/*
* We have checked the four combinations of isDesignated and isCurrent. Where appropriate,
* we added the PDPs to the potential list of designated pdps
*
* We need to give priority to pdps on the same site that is currently being used
* First, however, we must sanitize the list of designated to make sure their are
* only designated members or non-designated members. There should not be both in
* the list. Because there are real time delays, it is possible that both types could
* be on the list.
*/
listOfDesignated = santizeDesignatedList(listOfDesignated);
/*
* We need to figure out the last pdp that was the primary so we can get the last site
* name and the last session numbers. We need to create a "dummy" droolspdp since
* it will be used in later comparisons and cannot be null.
*/
DroolsPdp mostRecentPrimary = computeMostRecentPrimary(pdps, listOfDesignated);
if(mostRecentPrimary != null){
pdpdLastActive = mostRecentPrimary.getPdpId();
}
/*
* It is possible to get here with more than one pdp designated and providingservice. This normally
* occurs when there is a race condition with multiple nodes coming up at the same time. If that is
* the case we must determine which one is the one that should be designated and which one should
* be demoted.
*
* It is possible to have 0, 1, 2 or more but not all, or all designated.
* If we have one designated and current, we chose it and are done
* If we have 2 or more, but not all, we must determine which one is in the same site as
* the previously designated pdp.
*/
designatedPdp = computeDesignatedPdp(listOfDesignated, mostRecentPrimary);
if(designatedPdp != null){
pdpdNowActive = designatedPdp.getPdpId();
}
if (designatedPdp == null) {
logger.warn
("WARNING: DesignatedWaiter.run: No viable PDP found to be Designated. designatedPdp still null.");
// Just to be sure the parameters are correctly set
myPdp.setDesignated(false);
pdpsConnector.setDesignated(myPdp,false);
isDesignated = false;
waitTimerLastRunDate = new Date();
if(logger.isDebugEnabled()){
logger.debug("DesignatedWaiter.run (designatedPdp == null) waitTimerLastRunDate = {}", waitTimerLastRunDate);
}
myPdp.setUpdatedDate(waitTimerLastRunDate);
pdpsConnector.update(myPdp);
return;
} else if (designatedPdp.getPdpId().equals(myPdp.getPdpId())) {
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: designatedPdp is PDP={}", myPdp.getPdpId());
}
/*
* update function expects myPdp.isDesignated to be true.
*/
try {
//Keep the order like this. StateManagement is last since it triggers controller init
myPdp.setDesignated(true);
myPdp.setDesignatedDate(new Date());
pdpsConnector.setDesignated(myPdp, true);
isDesignated = true;
String standbyStatus = stateManagementFeature.getStandbyStatus();
if(!standbyStatus.equals(StateManagement.PROVIDING_SERVICE)){
/*
* Only call promote if it is not already in the right state. Don't worry about
* synching the lower level topic endpoint states. That is done by the
* refreshStateAudit.
* Note that we need to fetch the session list from 'mostRecentPrimary'
* at this point -- soon, 'mostRecentPrimary' will be set to this host.
*/
//this.sessions = mostRecentPrimary.getSessions();
stateManagementFeature.promote();
}
} catch (Exception e) {
logger.error
("ERROR: DesignatedWaiter.run: Caught Exception attempting to promote PDP={}"
+ ", message=", myPdp.getPdpId(), e);
myPdp.setDesignated(false);
pdpsConnector.setDesignated(myPdp,false);
isDesignated = false;
//If you can't promote it, demote it
try {
String standbyStatus = stateManagementFeature.getStandbyStatus();
if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
standbyStatus.equals(StateManagement.COLD_STANDBY))){
/*
* Only call demote if it is not already in the right state. Don't worry about
* synching the lower level topic endpoint states. That is done by the
* refreshStateAudit.
*/
stateManagementFeature.demote();
}
} catch (Exception e1) {
logger.error
("ERROR: DesignatedWaiter.run: Caught StandbyStatusException "
+ "attempting to promote then demote PDP={}, message=",
myPdp.getPdpId(), e1);
}
}
waitTimerLastRunDate = new Date();
if(logger.isDebugEnabled()){
logger.debug("DesignatedWaiter.run (designatedPdp.getPdpId().equals(myPdp.getPdpId())) "
+ "waitTimerLastRunDate = " + waitTimerLastRunDate);
}
myPdp.setUpdatedDate(waitTimerLastRunDate);
pdpsConnector.update(myPdp);
return;
}
isDesignated = false;
} // end synchronized
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: myPdp: {}; Returning, isDesignated= {}",
isDesignated, myPdp.getPdpId());
}
Date tmpDate = new Date();
if(logger.isDebugEnabled()){
logger.debug("DesignatedWaiter.run (end of run) waitTimerLastRunDate = {}", tmpDate);
}
waitTimerLastRunDate = tmpDate;
myPdp.setUpdatedDate(waitTimerLastRunDate);
pdpsConnector.update(myPdp);
}catch(Exception e){
logger.error("DesignatedWaiter.run caught an unexpected exception: ", e);
}
} // end run
}
public ArrayList<DroolsPdp> santizeDesignatedList(ArrayList<DroolsPdp> listOfDesignated){
boolean containsDesignated = false;
boolean containsHotStandby = false;
ArrayList<DroolsPdp> listForRemoval = new ArrayList<DroolsPdp>();
for(DroolsPdp pdp : listOfDesignated){
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run sanitizing: pdp = {}"
+ " isDesignated = {}",pdp.getPdpId(), pdp.isDesignated());
}
if(pdp.isDesignated()){
containsDesignated = true;
}else {
containsHotStandby = true;
listForRemoval.add(pdp);
}
}
if(containsDesignated && containsHotStandby){
//remove the hot standby from the list
listOfDesignated.removeAll(listForRemoval);
containsHotStandby = false;
}
return listOfDesignated;
}
public DroolsPdp computeMostRecentPrimary(Collection<DroolsPdp> pdps, ArrayList<DroolsPdp> listOfDesignated){
boolean containsDesignated = false;
for(DroolsPdp pdp : listOfDesignated){
if(pdp.isDesignated()){
containsDesignated = true;
}
}
DroolsPdp mostRecentPrimary = new DroolsPdpImpl(null, true, 1, new Date(0));
mostRecentPrimary.setSiteName(null);
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run listOfDesignated.size() = {}", listOfDesignated.size());
}
if(listOfDesignated.size() <=1){
if(logger.isDebugEnabled()){
logger.debug("DesignatedWainter.run: listOfDesignated.size <=1");
}
//Only one or none is designated or hot standby. Choose the latest designated date
for(DroolsPdp pdp : pdps){
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run pdp = {}"
+ " pdp.getDesignatedDate() = {}", pdp.getPdpId(), pdp.getDesignatedDate());
}
if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
mostRecentPrimary = pdp;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
}
}
}
}else if(listOfDesignated.size() == pdps.size()){
if(logger.isDebugEnabled()){
logger.debug("DesignatedWainter.run: listOfDesignated.size = pdps.size() which is {}", pdps.size());
}
//They are all designated or all hot standby.
mostRecentPrimary = null;
for(DroolsPdp pdp : pdps){
if(mostRecentPrimary == null){
mostRecentPrimary = pdp;
continue;
}
if(containsDesignated){ //Choose the site of the first designated date
if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) < 0){
mostRecentPrimary = pdp;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
}
}
}else{ //Choose the site with the latest designated date
if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
mostRecentPrimary = pdp;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
}
}
}
}
}else{
if(logger.isDebugEnabled()){
logger.debug("DesignatedWainter.run: Some but not all are designated or hot standby. ");
}
//Some but not all are designated or hot standby.
if(containsDesignated){
if(logger.isDebugEnabled()){
logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
}
/*
* The list only contains designated. This is a problem. It is most likely a race
* condition that resulted in two thinking they should be designated. Choose the
* site with the latest designated date for the pdp not included on the designated list.
* This should be the site that had the last designation before this race condition
* occurred.
*/
for(DroolsPdp pdp : pdps){
if(listOfDesignated.contains(pdp)){
continue; //Don't consider this entry
}
if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
mostRecentPrimary = pdp;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
}
}
}
}else{
if(logger.isDebugEnabled()){
logger.debug("DesignatedWainter.run: containsDesignated = {}", containsDesignated);
}
//The list only contains hot standby. Choose the site of the latest designated date
for(DroolsPdp pdp : pdps){
if(pdp.getDesignatedDate().compareTo(mostRecentPrimary.getDesignatedDate()) > 0){
mostRecentPrimary = pdp;
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run mostRecentPrimary = {}", mostRecentPrimary.getPdpId());
}
}
}
}
}
return mostRecentPrimary;
}
public DroolsPdp computeDesignatedPdp(ArrayList<DroolsPdp> listOfDesignated, DroolsPdp mostRecentPrimary){
DroolsPdp designatedPdp = null;
DroolsPdp lowestPriorityPdp = null;
if(listOfDesignated.size() > 1){
if(logger.isDebugEnabled()){
logger.debug
("DesignatedWaiter.run: myPdp: {} listOfDesignated.size(): {}", myPdp.getPdpId(), listOfDesignated.size());
}
DroolsPdp rejectedPdp = null;
DroolsPdp lowestPrioritySameSite = null;
DroolsPdp lowestPriorityDifferentSite = null;
for(DroolsPdp pdp : listOfDesignated){
// We need to determine if another PDP is the lowest priority
if(nullSafeEquals(pdp.getSiteName(),mostRecentPrimary.getSiteName())){
if(lowestPrioritySameSite == null){
if(lowestPriorityDifferentSite != null){
rejectedPdp = lowestPriorityDifferentSite;
}
lowestPrioritySameSite = pdp;
}else{
if(pdp.getPdpId().equals((lowestPrioritySameSite.getPdpId()))){
continue;//nothing to compare
}
if(pdp.comparePriority(lowestPrioritySameSite) <0){
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ " has lower priority than pdp ID: {}",myPdp.getPdpId(), pdp.getPdpId(),
lowestPrioritySameSite.getPdpId());
}
//we need to reject lowestPrioritySameSite
rejectedPdp = lowestPrioritySameSite;
lowestPrioritySameSite = pdp;
} else{
//we need to reject pdp and keep lowestPrioritySameSite
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {} "
+ " has higher priority than pdp ID: {}", myPdp.getPdpId(),pdp.getPdpId(),
lowestPrioritySameSite.getPdpId());
}
rejectedPdp = pdp;
}
}
} else{
if(lowestPrioritySameSite != null){
//if we already have a candidate for same site, we don't want to bother with different sites
rejectedPdp = pdp;
} else{
if(lowestPriorityDifferentSite == null){
lowestPriorityDifferentSite = pdp;
continue;
}
if(pdp.getPdpId().equals((lowestPriorityDifferentSite.getPdpId()))){
continue;//nothing to compare
}
if(pdp.comparePriority(lowestPriorityDifferentSite) <0){
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ " has lower priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
lowestPriorityDifferentSite.getPdpId());
}
//we need to reject lowestPriorityDifferentSite
rejectedPdp = lowestPriorityDifferentSite;
lowestPriorityDifferentSite = pdp;
} else{
//we need to reject pdp and keep lowestPriorityDifferentSite
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp {} listOfDesignated pdp ID: {}"
+ " has higher priority than pdp ID: {}", myPdp.getPdpId(), pdp.getPdpId(),
lowestPriorityDifferentSite.getPdpId());
}
rejectedPdp = pdp;
}
}
}
// If the rejectedPdp is myPdp, we need to stand it down and demote it. Each pdp is responsible
// for demoting itself
if(rejectedPdp != null && nullSafeEquals(rejectedPdp.getPdpId(),myPdp.getPdpId())){
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated myPdp ID: {}"
+ " is NOT the lowest priority. Executing stateManagement.demote()\n\n", myPdp.getPdpId(),
myPdp.getPdpId());
}
// We found that myPdp is on the listOfDesignated and it is not the lowest priority
// So, we must demote it
try {
//Keep the order like this. StateManagement is last since it triggers controller shutdown
myPdp.setDesignated(false);
pdpsConnector.setDesignated(myPdp, false);
isDesignated = false;
String standbyStatus = stateManagementFeature.getStandbyStatus();
if(!(standbyStatus.equals(StateManagement.HOT_STANDBY) ||
standbyStatus.equals(StateManagement.COLD_STANDBY))){
/*
* Only call demote if it is not already in the right state. Don't worry about
* synching the lower level topic endpoint states. That is done by the
* refreshStateAudit.
*/
stateManagementFeature.demote();
}
} catch (Exception e) {
myPdp.setDesignated(false);
pdpsConnector.setDesignated(myPdp, false);
isDesignated = false;
logger.error
("DesignatedWaiter.run: myPdp: {} Caught Exception attempting to "
+ "demote myPdp {} myPdp.getPdpId(), message= {}", myPdp.getPdpId(),
e);
}
}
} //end: for(DroolsPdp pdp : listOfDesignated)
if(lowestPrioritySameSite != null){
lowestPriorityPdp = lowestPrioritySameSite;
} else {
lowestPriorityPdp = lowestPriorityDifferentSite;
}
//now we have a valid value for lowestPriorityPdp
if(logger.isDebugEnabled()){
logger.debug
("\n\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
+ "found the LOWEST priority pdp ID: {} "
+ " It is now the designatedPpd from the perspective of myPdp ID: {} \n\n",
myPdp.getPdpId(), lowestPriorityPdp.getPdpId(), myPdp);
}
designatedPdp = lowestPriorityPdp;
} else if(listOfDesignated.isEmpty()){
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp: {} listOfDesignated is: EMPTY.", myPdp.getPdpId());
}
designatedPdp = null;
} else{ //only one in listOfDesignated
if(logger.isDebugEnabled()){
logger.debug
("\nDesignatedWaiter.run: myPdp: {} listOfDesignated "
+ "has ONE entry. PDP ID: {}", myPdp.getPdpId(), listOfDesignated.get(0).getPdpId());
}
designatedPdp = listOfDesignated.get(0);
}
return designatedPdp;
}
private class TimerUpdateClass extends TimerTask{
@Override
public void run() {
try{
if(logger.isDebugEnabled()){
logger.debug("TimerUpdateClass.run: entry");
}
checkWaitTimer();
}catch(Exception e){
logger.error("TimerUpdateClass.run caught an unexpected exception: ", e);
}
if(logger.isDebugEnabled()){
logger.debug("TimerUpdateClass.run.exit");
}
}
}
@Override
public void checkThreadStatus() {
checkWaitTimer();
}
private void checkWaitTimer(){
synchronized(checkWaitTimerLock){
try{
if(logger.isDebugEnabled()){
logger.debug("checkWaitTimer: entry");
}
Date now = new Date();
long nowMs = now.getTime();
long waitTimerMs = waitTimerLastRunDate.getTime();
//give it 10 times leeway
if((nowMs - waitTimerMs) > 10*pdpUpdateInterval){
if(allSeemsWell==null || allSeemsWell){
allSeemsWell = false;
if(logger.isDebugEnabled()){
logger.debug("checkWaitTimer: calling allSeemsWell with ALLNOTWELL param");
}
stateManagementFeature.allSeemsWell(this.getClass().getName(),
StateManagementFeatureAPI.ALLNOTWELL_STATE,
"DesignationWaiter/ElectionHandler has STALLED");
}
logger.error("checkWaitTimer: nowMs - waitTimerMs = {}"
+ ", exceeds 10* pdpUpdateInterval = {}"
+ " DesignationWaiter is STALLED!", (nowMs - waitTimerMs), (10*pdpUpdateInterval));
}else if(allSeemsWell==null || !allSeemsWell){
allSeemsWell = true;
stateManagementFeature.allSeemsWell(this.getClass().getName(),
StateManagementFeatureAPI.ALLSEEMSWELL_STATE,
"DesignationWaiter/ElectionHandler has RESUMED");
logger.info("DesignationWaiter/ElectionHandler has RESUMED");
}
if(logger.isDebugEnabled()){
logger.debug("checkWaitTimer: exit");
}
}catch(Exception e){
logger.error("checkWaitTimer: caught unexpected exception: ", e);
}
}
}
private long getDWaiterStartMs(){
Date now = new Date();
// Retrieve the ms since the epoch
long nowMs = now.getTime();
// Time since the end of the last pdpUpdateInterval multiple
long nowModMs = nowMs % pdpUpdateInterval;
// Time to the start of the next pdpUpdateInterval multiple
long startMs = 2*pdpUpdateInterval - nowModMs;
// Give the start time a minimum of a 5 second cushion
if(startMs < 5000){
// Start at the beginning of following interval
startMs = pdpUpdateInterval + startMs;
}
return startMs;
}
private boolean nullSafeEquals(Object one, Object two){
if(one == null && two == null){
return true;
}
if(one != null && two != null){
return one.equals(two);
}
return false;
}
public String getPdpdNowActive(){
return pdpdNowActive;
}
public String getPdpdLastActive(){
return pdpdLastActive;
}
}
|