@@ -65,6 +65,21 @@ public class TarantoolClientImpl extends TarantoolBase<Future<?>> implements Tar
65
65
protected Thread reader ;
66
66
protected Thread writer ;
67
67
68
+ /**
69
+ * The condition variable to signal a reconnection is needed from reader /
70
+ * writer threads and waiting for that signal from the reconnection thread.
71
+ *
72
+ * The lock variable to access this condition.
73
+ *
74
+ * XXX: Maybe it is better to move signalling / waiting support to the
75
+ * state helper (or fsm when we'll reimplement it in that way). Maybe
76
+ * additional RECONNECTION_NEEDED state is required to distinguish whether
77
+ * we're in the reconnection state already or just want to reconnect (don't
78
+ * sure).
79
+ *
80
+ * @see #awaitReconnection()
81
+ * @see #trySignalForReconnection()
82
+ */
68
83
protected final ReentrantLock connectorLock = new ReentrantLock ();
69
84
protected final Condition reconnectRequired = connectorLock .newCondition ();
70
85
@@ -478,7 +493,7 @@ protected void stopIO() {
478
493
}
479
494
480
495
/**
481
- * Blocks until a reconnection process can be carried on
496
+ * Blocks until a reconnection signal will be received.
482
497
*
483
498
* @see #trySignalForReconnection()
484
499
*/
@@ -496,7 +511,7 @@ private void awaitReconnection() {
496
511
}
497
512
498
513
/**
499
- * Signals to the connector that reconnection process can be performed
514
+ * Signals to the connector that reconnection process can be performed.
500
515
*
501
516
* @see #awaitReconnection()
502
517
*/
@@ -675,28 +690,52 @@ protected int getState() {
675
690
return state .get ();
676
691
}
677
692
693
+ /**
694
+ * Set CLOSED state, drop RECONNECT state.
695
+ */
678
696
protected boolean close () {
679
697
for (; ; ) {
680
698
int st = getState ();
699
+
700
+ /* CLOSED is the terminal state. */
681
701
if ((st & CLOSED ) == CLOSED )
682
702
return false ;
703
+
704
+ /* Drop RECONNECT, set CLOSED.
705
+ *
706
+ * XXX: Should not we also drop other states? Or reimplement
707
+ * this class as a finite state machine?
708
+ */
683
709
if (compareAndSet (st , (st & ~RECONNECT ) | CLOSED ))
684
710
return true ;
685
711
}
686
712
}
687
713
714
+ /**
715
+ * Move from a current state to a give one.
716
+ *
717
+ * Some moves are forbidden.
718
+ */
688
719
protected boolean acquire (int mask ) {
689
720
for (; ; ) {
690
721
int currentState = getState ();
722
+
723
+ /* CLOSED is the terminal state. */
691
724
if ((currentState & CLOSED ) == CLOSED ) {
692
725
return false ;
693
726
}
727
+
728
+ /* Don't move to READING, WRITING or ALIVE from RECONNECT. */
694
729
if ((currentState & RECONNECT ) > mask ) {
695
730
return false ;
696
731
}
732
+
733
+ /* Cannot move from a state to the same state. */
697
734
if ((currentState & mask ) != 0 ) {
698
735
throw new IllegalStateException ("State is already " + mask );
699
736
}
737
+
738
+ /* Set acquired state. */
700
739
if (compareAndSet (currentState , currentState | mask )) {
701
740
return true ;
702
741
}
0 commit comments