@@ -110,7 +110,7 @@ public class GMS extends Protocol implements DiagnosticsHandler.ProbeHandler {
110
110
protected int num_views ;
111
111
protected BoundedList <String > prev_views ; // History of the last N views
112
112
protected GmsImpl impl ;
113
- protected final Lock impl_lock =new ReentrantLock (); // synchronizes event entry into impl
113
+ protected final Lock lock =new ReentrantLock ();
114
114
protected final Map <String ,GmsImpl > impls =new HashMap <>(3 );
115
115
116
116
protected Merger merger ; // handles merges
@@ -309,14 +309,14 @@ public List<Integer> providedDownServices() {
309
309
}
310
310
311
311
public void setImpl (GmsImpl new_impl ) {
312
- impl_lock .lock ();
312
+ lock .lock ();
313
313
try {
314
314
if (impl == new_impl )
315
315
return ;
316
316
impl =new_impl ;
317
317
}
318
318
finally {
319
- impl_lock .unlock ();
319
+ lock .unlock ();
320
320
}
321
321
}
322
322
@@ -433,7 +433,8 @@ public void cancelMerge() {
433
433
* {@code suspected_mbrs} removed and {@code joiners} added.
434
434
*/
435
435
public View getNextView (Collection <Address > joiners , Collection <Address > leavers , Collection <Address > suspected_mbrs ) {
436
- synchronized (members ) {
436
+ lock .lock ();
437
+ try {
437
438
ViewId view_id =view != null ? view .getViewId () : null ;
438
439
if (view_id == null ) {
439
440
log .error (Util .getMessage ("ViewidIsNull" ));
@@ -461,6 +462,9 @@ public View getNextView(Collection<Address> joiners, Collection<Address> leavers
461
462
suspected_mbrs .stream ().filter (addr -> !leaving .contains (addr )).forEach (leaving ::add );
462
463
return v ;
463
464
}
465
+ finally {
466
+ lock .unlock ();
467
+ }
464
468
}
465
469
466
470
/** Computes the regular membership */
@@ -602,42 +606,43 @@ public void installView(View new_view) {
602
606
* Sets the new view and sends a VIEW_CHANGE event up and down the stack. If the view is a MergeView (subclass
603
607
* of View), then digest will be non-null and has to be set before installing the view.
604
608
*/
605
- public synchronized void installView (View new_view , Digest digest ) {
606
- ViewId vid =new_view .getViewId ();
609
+ public void installView (View new_view , Digest digest ) {
610
+ Event view_event ;
611
+ ViewId vid =new_view .getViewId ();
607
612
List <Address > mbrs =new_view .getMembers ();
608
- ltime =Math .max (vid .getId (), ltime ); // compute the logical time, regardless of whether the view is accepted
613
+ boolean am_i_coord ;
614
+ lock .lock ();
615
+ try {
616
+ ltime =Math .max (vid .getId (), ltime ); // compute the logical time, regardless of whether the view is accepted
609
617
610
- // Discards view with id lower than or equal to our own. Will be installed without check if it is the first view
611
- if (view != null && vid .compareToIDs (view .getViewId ()) <= 0 )
612
- return ;
618
+ // Discards view with id lower than or equal to our own. Will be installed without check if it is the first view
619
+ if (view != null && vid .compareToIDs (view .getViewId ()) <= 0 )
620
+ return ;
613
621
614
- /* Check for self-inclusion: if I'm not part of the new membership, I just discard it.
622
+ /* Check for self-inclusion: if I'm not part of the new membership, I just discard it.
615
623
This ensures that messages sent in view V1 are only received by members of V1 */
616
- if (!mbrs .contains (local_addr )) {
617
- if (log_view_warnings )
618
- log .warn ("%s: not member of view %s; discarding it" , local_addr , new_view .getViewId ());
619
- return ;
620
- }
624
+ if (!mbrs .contains (local_addr )) {
625
+ if (log_view_warnings )
626
+ log .warn ("%s: not member of view %s; discarding it" , local_addr , new_view .getViewId ());
627
+ return ;
628
+ }
621
629
622
- if (digest != null ) {
623
- if (new_view instanceof MergeView )
624
- mergeDigest (digest );
625
- else
626
- setDigest (digest );
627
- }
630
+ if (digest != null ) {
631
+ if (new_view instanceof MergeView )
632
+ mergeDigest (digest );
633
+ else
634
+ setDigest (digest );
635
+ }
628
636
629
- if (log .isDebugEnabled ()) {
630
- Address [][] diff =View .diff (view , new_view );
631
- log .debug ("%s: installing view %s %s" , local_addr , new_view ,
632
- print_view_details ? View .printDiff (diff ) : "" );
633
- }
637
+ if (log .isDebugEnabled ()) {
638
+ Address [][] diff =View .diff (view , new_view );
639
+ log .debug ("%s: installing view %s %s" , local_addr , new_view ,
640
+ print_view_details ? View .printDiff (diff ) : "" );
641
+ }
634
642
635
- Event view_event ;
636
- boolean was_coord , is_coord ;
637
- synchronized (members ) {
638
- was_coord =view != null && Objects .equals (local_addr , view .getCoord ());
643
+ boolean was_coord =view != null && Objects .equals (local_addr , view .getCoord ());
639
644
view =new_view ;
640
- is_coord =Objects .equals (local_addr , view .getCoord ());
645
+ boolean is_coord =Objects .equals (local_addr , view .getCoord ());
641
646
view_event =new Event (Event .VIEW_CHANGE , new_view );
642
647
643
648
// Set the membership. Take into account joining members
@@ -647,12 +652,8 @@ public synchronized void installView(View new_view, Digest digest) {
647
652
joining .removeAll (mbrs ); // remove all members in mbrs from joining
648
653
// remove all elements from 'leaving' that are not in 'mbrs'
649
654
leaving .retainAll (mbrs );
650
-
651
- tmp_members .add (joining ); // add members that haven't yet shown up in the membership
652
- tmp_members .remove (leaving ); // remove members that haven't yet been removed from the membership
655
+ tmp_members .add (joining ).remove (leaving );
653
656
suspected_mbrs .retainAll (mbrs );
654
-
655
- // add to prev_members
656
657
mbrs .stream ().filter (addr -> !prev_members .contains (addr )).forEach (addr -> prev_members .add (addr ));
657
658
}
658
659
@@ -664,6 +665,15 @@ public synchronized void installView(View new_view, Digest digest) {
664
665
if (was_coord || impl instanceof ClientGmsImpl )
665
666
becomeParticipant ();
666
667
}
668
+ ack_collector .retainAll (new_view .getMembers ());
669
+ if (stats ) {
670
+ num_views ++;
671
+ prev_views .add (Util .utcNow () + ": " + new_view );
672
+ }
673
+ am_i_coord =Objects .equals (local_addr , new_view .getCoord ());
674
+ }
675
+ finally {
676
+ lock .unlock ();
667
677
}
668
678
669
679
// - Changed order of passing view up and down (https://issues.redhat.com/browse/JGRP-347)
@@ -672,45 +682,31 @@ public synchronized void installView(View new_view, Digest digest) {
672
682
down_prot .down (view_event ); // needed e.g. by failure detector or UDP
673
683
up_prot .up (view_event );
674
684
675
- List <Address > tmp_mbrs =new_view .getMembers ();
676
- ack_collector .retainAll (tmp_mbrs );
677
-
678
- if (new_view instanceof MergeView ) {
679
- // Everybody except the merge leader cancels the merge, otherwise - if UNICAST3.loopback is true - we'd
680
- // interrupt our own thread which will fail code that later sends a message before returning!
681
- // Note that the merge leader does cancel the merge later, after having installed the MergeView
682
- // (in Merger.handleMergeView() in the finally clause)
683
- if (!Objects .equals (local_addr , new_view .getCoord ()))
684
- merger .forceCancelMerge ();
685
- }
686
-
687
- if (stats ) {
688
- num_views ++;
689
- prev_views .add (Util .utcNow () + ": " + new_view );
690
- }
685
+ // Everybody except the merge leader cancels the merge, otherwise - if UNICAST3.loopback is true - we'd
686
+ // interrupt our own thread which will fail code that later sends a message before returning!
687
+ // Note that the merge leader does cancel the merge later, after having installed the MergeView
688
+ // (in Merger.handleMergeView() in the finally clause)
689
+ if (new_view instanceof MergeView && !am_i_coord )
690
+ merger .forceCancelMerge ();
691
691
}
692
692
693
693
protected Address getCoord () {
694
- impl_lock .lock ();
694
+ lock .lock ();
695
695
try {
696
696
return isCoord ()? determineNextCoordinator () : determineCoordinator ();
697
697
}
698
698
finally {
699
- impl_lock .unlock ();
699
+ lock .unlock ();
700
700
}
701
701
}
702
702
703
703
protected Address determineCoordinator () {
704
- synchronized (members ) {
705
- return members .getFirst ();
706
- }
704
+ return members .getFirst ();
707
705
}
708
706
709
707
/** Returns the second-in-line */
710
708
protected Address determineNextCoordinator () {
711
- synchronized (members ) {
712
- return members .size () > 1 ? members .elementAt (1 ) : null ;
713
- }
709
+ return members .nextCoord ();
714
710
}
715
711
716
712
protected static View createDeltaView (final View current_view , final View next_view ) {
@@ -720,36 +716,35 @@ protected static View createDeltaView(final View current_view, final View next_v
720
716
return new DeltaView (next_view_id , current_view_id , diff [1 ], diff [0 ]);
721
717
}
722
718
723
-
724
719
/** Checks whether the potential_new_coord would be the new coordinator (2nd in line) */
725
720
protected boolean wouldBeNewCoordinator (Address potential_new_coord ) {
726
721
if (potential_new_coord == null ) return false ;
727
- synchronized (members ) {
722
+ lock .lock ();
723
+ try {
728
724
if (members .size () < 2 ) return false ;
729
725
Address new_coord =members .elementAt (1 ); // member at 2nd place
730
726
return Objects .equals (new_coord , potential_new_coord );
731
727
}
728
+ finally {
729
+ lock .unlock ();
730
+ }
732
731
}
733
732
734
-
735
733
/** Send down a SET_DIGEST event */
736
734
public void setDigest (Digest d ) {
737
735
down_prot .down (new Event (Event .SET_DIGEST , d ));
738
736
}
739
737
740
-
741
738
/** Send down a MERGE_DIGEST event */
742
739
public void mergeDigest (Digest d ) {
743
740
down_prot .down (new Event (Event .MERGE_DIGEST ,d ));
744
741
}
745
742
746
-
747
743
/** Grabs the current digest from NAKACK{2} */
748
744
public Digest getDigest () {
749
745
return (Digest )down_prot .down (Event .GET_DIGEST_EVT );
750
746
}
751
747
752
-
753
748
public Object up (Event evt ) {
754
749
switch (evt .getType ()) {
755
750
case Event .SUSPECT :
0 commit comments