5
5
import java .net .InetAddress ;
6
6
import java .net .InetSocketAddress ;
7
7
import java .net .Socket ;
8
+ import java .rmi .RemoteException ;
8
9
import java .security .PublicKey ;
9
10
import java .security .cert .CertificateException ;
10
11
import java .security .cert .X509Certificate ;
@@ -55,6 +56,7 @@ public class DBConnection {
55
56
private long runSeqNo_ = 0 ;
56
57
private int [] serverVersion_ ;
57
58
private boolean isReverseStreaming_ = false ;
59
+ private int tryReconnectNums = -1 ;
58
60
59
61
private static final Logger log = LoggerFactory .getLogger (DBConnection .class );
60
62
@@ -189,7 +191,6 @@ private boolean connect()throws IOException{
189
191
socket_ .connect (new InetSocketAddress (hostName_ ,port_ ), 3000 );
190
192
}
191
193
} catch (ConnectException ex ) {
192
- log .error ("com.xxdb.DBConnection.DBConnectionImpl.connect() has exception. Current node hostName: " + this .hostName_ + ", port: " + this .port_ );
193
194
log .error ("Connect to " + this .hostName_ + ":" + this .port_ + " failed." );
194
195
throw ex ;
195
196
}
@@ -668,6 +669,11 @@ public boolean connect(String hostName, int port, int timeout, boolean reconnect
668
669
return connect (hostName , port , "" , "" , null , false , null , reconnect );
669
670
}
670
671
672
+ public boolean connect (String hostName , int port , int timeout , boolean reconnect , int tryReconnectNums ) throws IOException {
673
+ this .connTimeout_ = timeout ;
674
+ return connect (hostName , port , "" , "" , null , false , null , reconnect , tryReconnectNums );
675
+ }
676
+
671
677
public boolean connect (String hostName , int port , String initialScript ) throws IOException {
672
678
return connect (hostName , port , "" , "" , initialScript , false , null );
673
679
}
@@ -723,14 +729,32 @@ public boolean connect(String hostName, int port, String userId, String password
723
729
return connect (hostName , port , userId , password , initialScript , enableHighAvailability , highAvailabilitySites , reconnect , false );
724
730
}
725
731
732
+ public boolean connect (String hostName , int port , String userId , String password , String initialScript , boolean enableHighAvailability , String [] highAvailabilitySites , boolean reconnect , int tryReconnectNums ) throws IOException {
733
+ if (enableHighAvailability )
734
+ return connect (hostName , port , userId , password , initialScript , enableHighAvailability , highAvailabilitySites , reconnect , true , tryReconnectNums );
735
+ else
736
+ return connect (hostName , port , userId , password , initialScript , enableHighAvailability , highAvailabilitySites , reconnect , false , tryReconnectNums );
737
+ }
738
+
726
739
public boolean connect (String hostName , int port , String userId , String password , String initialScript , boolean enableHighAvailability , String [] highAvailabilitySites , boolean reconnect , boolean enableLoadBalance ) throws IOException {
740
+ return connect (hostName , port , userId , password , initialScript , enableHighAvailability , highAvailabilitySites , reconnect , enableLoadBalance , -1 );
741
+ }
742
+
743
+ public boolean connect (String hostName , int port , String userId , String password , String initialScript , boolean enableHighAvailability , String [] highAvailabilitySites , boolean reconnect , boolean enableLoadBalance , int tryReconnectNums ) throws IOException {
727
744
mutex_ .lock ();
728
745
try {
729
746
this .uid_ = userId ;
730
747
this .pwd_ = password ;
731
748
this .initialScript_ = initialScript ;
732
749
this .enableHighAvailability_ = enableHighAvailability ;
733
750
this .loadBalance_ = enableLoadBalance ;
751
+ if (tryReconnectNums <= 0 ) {
752
+ this .tryReconnectNums = -1 ;
753
+ log .warn ("If the param 'tryReconnectNums' less than or equal to 0, when reconnect will be unlimited attempts." );
754
+ } else {
755
+ this .tryReconnectNums = tryReconnectNums ;
756
+ }
757
+
734
758
if (this .loadBalance_ && !this .enableHighAvailability_ )
735
759
throw new RuntimeException ("Cannot only enable loadbalance but not enable highAvailablity." );
736
760
@@ -746,20 +770,48 @@ public boolean connect(String hostName, int port, String userId, String password
746
770
747
771
Node connectedNode = new Node ();
748
772
BasicTable bt = null ;
773
+
749
774
while (!closed_ ) {
775
+ int totalConnectAttemptNums = this .tryReconnectNums * nodes_ .size ();
776
+ int attempt = 0 ;
750
777
while (!conn_ .isConnected () && !closed_ ) {
751
- for (Node one : nodes_ ) {
752
- if (connectNode (one )) {
753
- connectedNode = one ;
754
- break ;
778
+ if (this .tryReconnectNums > 0 ) {
779
+ // finite try to connect.
780
+ for (Node one : nodes_ ) {
781
+ attempt ++;
782
+ // System.out.println("Current init connect node: " + one.hostName + ":" + one.port);
783
+ if (connectNode (one )) {
784
+ connectedNode = one ;
785
+ break ;
786
+ }
787
+
788
+ try {
789
+ Thread .sleep (100 );
790
+ } catch (Exception e ){
791
+ e .printStackTrace ();
792
+ return false ;
793
+ }
755
794
}
756
795
757
- try {
758
- Thread .sleep (100 );
759
- } catch (Exception e ){
760
- e .printStackTrace ();
796
+ if (attempt == totalConnectAttemptNums ) {
797
+ log .error ("Connect failed after " + tryReconnectNums + " reconnect attemps for every node in high availability sites." );
761
798
return false ;
762
799
}
800
+ } else {
801
+ // infinite try to connect.
802
+ for (Node one : nodes_ ) {
803
+ if (connectNode (one )) {
804
+ connectedNode = one ;
805
+ break ;
806
+ }
807
+
808
+ try {
809
+ Thread .sleep (100 );
810
+ } catch (Exception e ){
811
+ e .printStackTrace ();
812
+ return false ;
813
+ }
814
+ }
763
815
}
764
816
}
765
817
@@ -882,30 +934,44 @@ private void initConnection() throws IOException{
882
934
}
883
935
884
936
public void switchDataNode (Node node ) throws IOException {
937
+ int attempt = 0 ;
938
+ boolean isConnected = false ;
885
939
do {
886
- if (node .hostName != null && node .hostName .length () > 0 ){
887
- if (connectNode (node )){
940
+ attempt ++;
941
+ if (node .hostName != null && node .hostName .length () > 0 ) {
942
+ if (connectNode (node )) {
888
943
log .info ("Switch to node: " + node .hostName + ":" + node .port + " successfully." );
944
+ isConnected = true ;
889
945
break ;
890
946
}
891
947
}
948
+
892
949
if (nodes_ .isEmpty ()){
893
950
log .error ("com.xxdb.DBConnection.switchDataNode nodes_ is empty. Current node hostName: " + node .hostName + ", port: " + node .port );
894
951
log .error ("Connect to " + node .hostName + ":" + node .port + " failed." );
895
952
throw new RuntimeException ("Connect to " + node .hostName + ":" + node .port + " failed." );
896
953
}
897
- int index = nodeRandom_ .nextInt (nodes_ .size ());
898
- if (connectNode (nodes_ .get (index ))){
899
- log .info ("Switch to node: " + nodes_ .get (index ).hostName + ":" + nodes_ .get (index ).port + " successfully." );
900
- break ;
954
+
955
+ if (nodes_ .size () > 1 ) {
956
+ int index = nodeRandom_ .nextInt (nodes_ .size ());
957
+ if (connectNode (nodes_ .get (index ))){
958
+ log .info ("Switch to node: " + nodes_ .get (index ).hostName + ":" + nodes_ .get (index ).port + " successfully." );
959
+ isConnected = true ;
960
+ break ;
961
+ }
901
962
}
963
+
902
964
try {
903
965
Thread .sleep (1000 );
904
- }catch (Exception e ){
966
+ } catch (Exception e ){
905
967
e .printStackTrace ();
906
968
return ;
907
969
}
908
- }while (!closed_ );
970
+ } while (!closed_ && (tryReconnectNums == -1 || attempt < tryReconnectNums ));
971
+
972
+ if (!closed_ && !isConnected )
973
+ throw new RuntimeException ("Connect to " + node .hostName + ":" + node .port + " failed after " + attempt + " reconnect attemps." );
974
+
909
975
if (initialScript_ !=null && initialScript_ .length () > 0 ){
910
976
run (initialScript_ );
911
977
}
0 commit comments