@@ -52,6 +52,8 @@ public class ObDirectLoadConnection {
5252 private long heartBeatTimeout = 0 ;
5353 private long heartBeatInterval = 0 ;
5454
55+ private long connectTimeout = 0 ;
56+
5557 private boolean isInited = false ;
5658 private boolean isClosed = false ;
5759
@@ -61,7 +63,7 @@ public class ObDirectLoadConnection {
6163
6264 ObDirectLoadConnection (ObDirectLoadConnectionFactory connectionFactory ) {
6365 this .connectionFactory = connectionFactory ;
64- this .traceId = ObDirectLoadTraceId . generateTraceId ();
66+ this .traceId = ObDirectLoadTraceIdGenerator . generate ();
6567 this .logger = ObDirectLoadLogger .getLogger (this .traceId );
6668 }
6769
@@ -137,6 +139,8 @@ private void fillParams(Builder builder) throws ObDirectLoadException {
137139 heartBeatInterval = builder .heartBeatInterval ;
138140
139141 writeConnectionNum = builder .writeConnectionNum ;
142+
143+ connectTimeout = builder .connectTimeout ;
140144 }
141145
142146 private void initCheck () throws ObDirectLoadException {
@@ -167,25 +171,30 @@ private void initCheck() throws ObDirectLoadException {
167171 "Param 'heartBeatInterval' must not be greater than or equal to Param 'heartBeatTimeout', heartBeatTimeout:"
168172 + heartBeatTimeout + ", heartBeatInterval:" + heartBeatInterval );
169173 }
174+ ObDirectLoadUtil .checkPositive (connectTimeout , "connectTimeout" , logger );
170175 }
171176
172177 private void initProtocol () throws ObDirectLoadException {
173178 // 构造一个连接, 获取版本号
179+ long obVersion = 0 ;
174180 ObTable table = null ;
175181 synchronized (connectionFactory ) { // 防止并发访问ObGlobal.OB_VERSION
176182 ObGlobal .OB_VERSION = 0 ;
177183 try {
178184 Properties properties = new Properties ();
179185 properties .setProperty (Property .SERVER_CONNECTION_POOL_SIZE .getKey (),
180186 String .valueOf (1 ));
187+ properties .setProperty (Property .RPC_CONNECT_TIMEOUT .getKey (),
188+ String .valueOf (connectTimeout ));
181189 table = new ObTable .Builder (ip , port )
182190 .setLoginInfo (tenantName , userName , password , databaseName )
183191 .setProperties (properties ).build ();
184192 } catch (Exception e ) {
185193 throw new ObDirectLoadException (e );
186194 }
195+ obVersion = ObGlobal .OB_VERSION ;
187196 }
188- this .protocol = ObDirectLoadProtocolFactory .getProtocol (traceId , ObGlobal . OB_VERSION );
197+ this .protocol = ObDirectLoadProtocolFactory .getProtocol (traceId , obVersion );
189198 this .protocol .init ();
190199 table .close ();
191200 }
@@ -276,6 +285,8 @@ public static final class Builder {
276285
277286 private static final long MAX_HEART_BEAT_TIMEOUT = 1L * 365 * 24 * 3600 * 1000 ; // 1year
278287
288+ private long connectTimeout = 1000 ;
289+
279290 Builder (ObDirectLoadConnectionFactory connectionFactory ) {
280291 this .connectionFactory = connectionFactory ;
281292 }
@@ -306,12 +317,17 @@ public Builder setHeartBeatInfo(long heartBeatTimeout, long heartBeatInterval) {
306317 return this ;
307318 }
308319
320+ public Builder setConnectTimeout (long connectTimeout ) {
321+ this .connectTimeout = Math .min (connectTimeout , (long ) Integer .MAX_VALUE );
322+ return this ;
323+ }
324+
309325 public String toString () {
310326 return String
311327 .format (
312- "{ip:\" %s\" , port:%d, tenantName:\" %s\" , userName:\" %s\" , databaseName:\" %s\" , writeConnectionNum:%d, heartBeatTimeout:%d, heartBeatInterval:%d}" ,
328+ "{ip:\" %s\" , port:%d, tenantName:\" %s\" , userName:\" %s\" , databaseName:\" %s\" , writeConnectionNum:%d, heartBeatTimeout:%d, heartBeatInterval:%d, connectTimeout:%d }" ,
313329 ip , port , tenantName , userName , databaseName , writeConnectionNum ,
314- heartBeatTimeout , heartBeatInterval );
330+ heartBeatTimeout , heartBeatInterval , connectTimeout );
315331 }
316332
317333 public ObDirectLoadConnection build () throws ObDirectLoadException {
@@ -370,6 +386,8 @@ private void initTables() throws ObDirectLoadException {
370386 Properties properties = new Properties ();
371387 properties
372388 .setProperty (Property .SERVER_CONNECTION_POOL_SIZE .getKey (), String .valueOf (1 ));
389+ properties .setProperty (Property .RPC_CONNECT_TIMEOUT .getKey (),
390+ String .valueOf (connection .connectTimeout ));
373391 properties .setProperty (Property .RPC_EXECUTE_TIMEOUT .getKey (),
374392 String .valueOf (timeoutMillis ));
375393 properties .setProperty (Property .RPC_OPERATION_TIMEOUT .getKey (),
0 commit comments