16
16
17
17
import com .databend .client .errors .CloudErrors ;
18
18
import okhttp3 .*;
19
- import okio .Buffer ;
20
19
21
20
import javax .annotation .concurrent .ThreadSafe ;
22
21
import java .io .IOException ;
@@ -50,10 +49,6 @@ public class DatabendClientV1
50
49
public static final MediaType MEDIA_TYPE_JSON = MediaType .parse ("application/json; charset=utf-8" );
51
50
public static final JsonCodec <QueryResults > QUERY_RESULTS_CODEC = jsonCodec (QueryResults .class );
52
51
public static final JsonCodec <DiscoveryResponseCodec .DiscoveryResponse > DISCOVERY_RESULT_CODEC = jsonCodec (DiscoveryResponseCodec .DiscoveryResponse .class );
53
- public static final String succeededState = "succeeded" ;
54
- public static final String failedState = "failed" ;
55
- public static final String runningState = "running" ;
56
-
57
52
58
53
public static final String QUERY_PATH = "/v1/query" ;
59
54
public static final String DISCOVERY_PATH = "/v1/discovery_nodes" ;
@@ -66,15 +61,14 @@ public class DatabendClientV1
66
61
private final PaginationOptions paginationOptions ;
67
62
// request with retry timeout
68
63
private final Integer requestTimeoutSecs ;
69
- private final Map <String , String > additonalHeaders ;
70
- private String serverVersion ;
64
+ private final Map <String , String > additionalHeaders ;
71
65
// client session
72
66
private final AtomicReference <DatabendSession > databendSession ;
73
67
private String nodeID ;
74
68
private final AtomicReference <QueryResults > currentResults = new AtomicReference <>(null );
75
69
private static final Logger logger = Logger .getLogger (DatabendClientV1 .class .getPackage ().getName ());
76
70
77
- private Consumer <DatabendSession > on_session_state_update ;
71
+ private final Consumer <DatabendSession > on_session_state_update ;
78
72
79
73
public DatabendClientV1 (OkHttpClient httpClient , String sql , ClientSettings settings ,
80
74
Consumer <DatabendSession > on_session_state_update ,
@@ -89,7 +83,7 @@ public DatabendClientV1(OkHttpClient httpClient, String sql, ClientSettings sett
89
83
this .host = settings .getHost ();
90
84
this .paginationOptions = settings .getPaginationOptions ();
91
85
this .requestTimeoutSecs = settings .getQueryTimeoutSecs ();
92
- this .additonalHeaders = settings .getAdditionalHeaders ();
86
+ this .additionalHeaders = settings .getAdditionalHeaders ();
93
87
this .maxRetryAttempts = settings .getRetryAttempts ();
94
88
this .databendSession = new AtomicReference <>(settings .getSession ());
95
89
this .nodeID = last_node_id .get ();
@@ -126,7 +120,7 @@ public static Request.Builder prepareRequest(HttpUrl url, Map<String, String> ad
126
120
}
127
121
128
122
private Request buildQueryRequest (String query , ClientSettings settings ) {
129
- HttpUrl url = HttpUrl .get (settings .getHost ());
123
+ HttpUrl url = HttpUrl .parse (settings .getHost ());
130
124
if (url == null ) {
131
125
// TODO(zhihanz) use custom exception
132
126
throw new IllegalArgumentException ("Invalid host: " + settings .getHost ());
@@ -139,7 +133,7 @@ private Request buildQueryRequest(String query, ClientSettings settings) {
139
133
}
140
134
141
135
url = url .newBuilder ().encodedPath (QUERY_PATH ).build ();
142
- Request .Builder builder = prepareRequest (url , this .additonalHeaders );
136
+ Request .Builder builder = prepareRequest (url , this .additionalHeaders );
143
137
DatabendSession session = databendSession .get ();
144
138
if (session != null && session .getNeedSticky ()) {
145
139
builder .addHeader (ClientSettings .X_DATABEND_STICKY_NODE , nodeID );
@@ -149,10 +143,6 @@ private Request buildQueryRequest(String query, ClientSettings settings) {
149
143
150
144
private static Request buildDiscoveryRequest (ClientSettings settings ) {
151
145
HttpUrl url = HttpUrl .get (settings .getHost ());
152
- if (url == null ) {
153
- // TODO(zhihanz) use custom exception
154
- throw new IllegalArgumentException ("Invalid host: " + settings .getHost ());
155
- }
156
146
String discoveryPath = DISCOVERY_PATH ;
157
147
// intentionally use unsupported discovery path for testing
158
148
if (settings .getAdditionalHeaders ().get ("~mock.unsupported.discovery" ) != null && BOOLEAN_TRUE_STR .equals (settings .getAdditionalHeaders ().get ("~mock.unsupported.discovery" ))) {
@@ -313,19 +303,6 @@ private boolean executeInternal(Request request, OptionalLong materializedJsonSi
313
303
}
314
304
}
315
305
316
- private String requestBodyToString (Request request ) {
317
- try {
318
- final Request copy = request .newBuilder ().build ();
319
- final Buffer buffer = new Buffer ();
320
- if (copy .body () != null ) {
321
- copy .body ().writeTo (buffer );
322
- }
323
- return buffer .readUtf8 ();
324
- } catch (final IOException e ) {
325
- return "did not work" ;
326
- }
327
- }
328
-
329
306
@ Override
330
307
public boolean execute (Request request ) {
331
308
return executeInternal (request , OptionalLong .empty ());
@@ -340,20 +317,13 @@ private void processResponse(Headers headers, QueryResults results) {
340
317
this .on_session_state_update .accept (session );
341
318
}
342
319
}
343
- if (results .getQueryId () != null && this .additonalHeaders .get (ClientSettings .X_Databend_Query_ID ) == null ) {
344
- this .additonalHeaders .put (ClientSettings .X_Databend_Query_ID , results .getQueryId ());
320
+ if (results .getQueryId () != null && this .additionalHeaders .get (ClientSettings .X_Databend_Query_ID ) == null ) {
321
+ this .additionalHeaders .put (ClientSettings .X_Databend_Query_ID , results .getQueryId ());
345
322
}
346
323
if (headers != null ) {
347
- String serverVersionString = headers .get (ClientSettings .X_DATABEND_VERSION );
348
- if (serverVersionString != null ) {
349
- try {
350
- serverVersion = serverVersionString ;
351
- } catch (Exception ignored ) {
352
- }
353
- }
354
324
String route_hint = headers .get (ClientSettings .X_DATABEND_ROUTE_HINT );
355
325
if (route_hint != null ) {
356
- this .additonalHeaders .put (ClientSettings .X_DATABEND_ROUTE_HINT , route_hint );
326
+ this .additionalHeaders .put (ClientSettings .X_DATABEND_ROUTE_HINT , route_hint );
357
327
}
358
328
}
359
329
currentResults .set (results );
@@ -375,7 +345,7 @@ public boolean advance() {
375
345
String nextUriPath = this .currentResults .get ().getNextUri ().toString ();
376
346
HttpUrl url = HttpUrl .get (this .host );
377
347
url = url .newBuilder ().encodedPath (nextUriPath ).build ();
378
- Request .Builder builder = prepareRequest (url , this .additonalHeaders );
348
+ Request .Builder builder = prepareRequest (url , this .additionalHeaders );
379
349
builder .addHeader (ClientSettings .X_DATABEND_STICKY_NODE , this .nodeID );
380
350
Request request = builder .get ().build ();
381
351
return executeInternal (request , OptionalLong .of (MAX_MATERIALIZED_JSON_RESPONSE_SIZE ));
@@ -388,7 +358,7 @@ public boolean hasNext() {
388
358
389
359
@ Override
390
360
public Map <String , String > getAdditionalHeaders () {
391
- return additonalHeaders ;
361
+ return additionalHeaders ;
392
362
}
393
363
394
364
@ Override
@@ -406,12 +376,6 @@ public String getNodeID() {
406
376
return this .nodeID ;
407
377
}
408
378
409
- @ Override
410
- public String getServerVersion () {
411
- return this .serverVersion ;
412
- }
413
-
414
-
415
379
@ Override
416
380
public void close () {
417
381
closeQuery ();
@@ -432,7 +396,7 @@ private void closeQuery() {
432
396
String path = uri .toString ();
433
397
HttpUrl url = HttpUrl .get (this .host );
434
398
url = url .newBuilder ().encodedPath (path ).build ();
435
- Request r = prepareRequest (url , this .additonalHeaders ).get ().build ();
399
+ Request r = prepareRequest (url , this .additionalHeaders ).get ().build ();
436
400
try {
437
401
httpClient .newCall (r ).execute ().close ();
438
402
} catch (IOException ignored ) {
0 commit comments