2020import com .fasterxml .jackson .databind .ObjectMapper ;
2121import org .apache .commons .codec .binary .Base64 ;
2222import org .apache .commons .lang3 .StringUtils ;
23+ import org .apache .doris .flink .cfg .ConfigurationOptions ;
24+ import org .apache .doris .flink .cfg .DorisReadOptions ;
2325import org .apache .doris .flink .exception .StreamLoadException ;
2426import org .apache .doris .flink .rest .models .RespContent ;
2527import org .apache .http .HttpHeaders ;
28+ import org .apache .http .client .config .RequestConfig ;
2629import org .apache .http .client .methods .CloseableHttpResponse ;
2730import org .apache .http .client .methods .HttpPut ;
2831import org .apache .http .entity .StringEntity ;
@@ -65,17 +68,9 @@ public class DorisStreamLoad implements Serializable {
6568 private String tbl ;
6669 private String authEncoding ;
6770 private Properties streamLoadProp ;
68- private final HttpClientBuilder httpClientBuilder = HttpClients
69- .custom ()
70- .setRedirectStrategy (new DefaultRedirectStrategy () {
71- @ Override
72- protected boolean isRedirectable (String method ) {
73- return true ;
74- }
75- });
76- private CloseableHttpClient httpClient ;
71+ private final HttpClientBuilder httpClientBuilder ;
7772
78- public DorisStreamLoad (String hostPort , String db , String tbl , String user , String passwd , Properties streamLoadProp ) {
73+ public DorisStreamLoad (String hostPort , String db , String tbl , String user , String passwd , Properties streamLoadProp , DorisReadOptions readOptions ) {
7974 this .hostPort = hostPort ;
8075 this .db = db ;
8176 this .tbl = tbl ;
@@ -84,7 +79,22 @@ public DorisStreamLoad(String hostPort, String db, String tbl, String user, Stri
8479 this .loadUrlStr = String .format (loadUrlPattern , hostPort , db , tbl );
8580 this .authEncoding = basicAuthHeader (user , passwd );
8681 this .streamLoadProp = streamLoadProp ;
87- this .httpClient = httpClientBuilder .build ();
82+ int connectTimeout = readOptions .getRequestConnectTimeoutMs () == null ? ConfigurationOptions .DORIS_REQUEST_CONNECT_TIMEOUT_MS_DEFAULT : readOptions .getRequestConnectTimeoutMs ();
83+ int socketTimeout = readOptions .getRequestReadTimeoutMs () == null ? ConfigurationOptions .DORIS_REQUEST_READ_TIMEOUT_MS_DEFAULT : readOptions .getRequestReadTimeoutMs ();
84+ this .httpClientBuilder = HttpClients
85+ .custom ()
86+ .setRedirectStrategy (new DefaultRedirectStrategy () {
87+ @ Override
88+ protected boolean isRedirectable (String method ) {
89+ return true ;
90+ }
91+ })
92+ .setDefaultRequestConfig (
93+ RequestConfig .custom ()
94+ .setConnectTimeout (connectTimeout )
95+ .setConnectionRequestTimeout (connectTimeout )
96+ .setSocketTimeout (socketTimeout )
97+ .build ());
8898 }
8999
90100 public String getLoadUrlStr () {
@@ -134,18 +144,20 @@ private LoadResponse loadBatch(String value) {
134144 StringEntity entity = new StringEntity (value , "UTF-8" );
135145 put .setEntity (entity );
136146
137- try (CloseableHttpResponse response = httpClient .execute (put )) {
138- final int statusCode = response .getStatusLine ().getStatusCode ();
139- final String reasonPhrase = response .getStatusLine ().getReasonPhrase ();
140- String loadResult = "" ;
141- if (response .getEntity () != null ) {
142- loadResult = EntityUtils .toString (response .getEntity ());
147+ try (CloseableHttpClient httpClient = httpClientBuilder .build ()){
148+ try (CloseableHttpResponse response = httpClient .execute (put )) {
149+ final int statusCode = response .getStatusLine ().getStatusCode ();
150+ final String reasonPhrase = response .getStatusLine ().getReasonPhrase ();
151+ String loadResult = "" ;
152+ if (response .getEntity () != null ) {
153+ loadResult = EntityUtils .toString (response .getEntity ());
154+ }
155+ return new LoadResponse (statusCode , reasonPhrase , loadResult );
143156 }
144- return new LoadResponse (statusCode , reasonPhrase , loadResult );
145157 }
146158 } catch (Exception e ) {
147159 String err = "failed to stream load data with label: " + label ;
148- LOG .warn (err , e );
160+ LOG .error (err , e );
149161 return new LoadResponse (-1 , e .getMessage (), err );
150162 }
151163 }
@@ -157,14 +169,6 @@ private String basicAuthHeader(String username, String password) {
157169 }
158170
159171 public void close () throws IOException {
160- if (null != httpClient ) {
161- try {
162- httpClient .close ();
163- } catch (IOException e ) {
164- LOG .error ("Closing httpClient failed." , e );
165- throw new RuntimeException ("Closing httpClient failed." , e );
166- }
167- }
168172 }
169173
170174 public static class LoadResponse {
0 commit comments