1717package com .salesforce .cdp .queryservice .core ;
1818
1919import com .google .common .annotations .VisibleForTesting ;
20+ import com .salesforce .cdp .queryservice .enums .QueryEngineEnum ;
21+ import com .salesforce .cdp .queryservice .model .QueryConfigResponse ;
2022import com .salesforce .cdp .queryservice .model .Token ;
2123import com .salesforce .cdp .queryservice .util .Constants ;
24+ import com .salesforce .cdp .queryservice .util .HttpHelper ;
25+ import com .salesforce .cdp .queryservice .util .QueryExecutor ;
26+ import com .salesforce .cdp .queryservice .util .TokenHelper ;
2227import lombok .extern .slf4j .Slf4j ;
28+ import okhttp3 .Response ;
2329import org .apache .commons .lang3 .StringUtils ;
2430
31+ import java .io .IOException ;
2532import java .sql .*;
2633import java .util .Map ;
2734import java .util .Properties ;
2835import java .util .concurrent .Executor ;
2936import java .util .concurrent .atomic .AtomicBoolean ;
3037
38+ import static com .salesforce .cdp .queryservice .util .Messages .QUERY_CONFIG_ERROR ;
39+
3140@ Slf4j
3241public class QueryServiceConnection implements Connection {
3342
34- private static final String TEST_CONNECT_QUERY = "select 1" ;
35-
3643 private AtomicBoolean closed = new AtomicBoolean (false );
3744 private Properties properties ;
3845 private final String serviceRootUrl ;
@@ -42,12 +49,15 @@ public class QueryServiceConnection implements Connection {
4249 private final boolean isSocksProxyDisabled ;
4350 private boolean enableStreamFlow = false ;
4451 private String tenantUrl ;
52+ private QueryEngineEnum queryEngineEnum ;
53+
54+ private boolean isValid = false ;
4555
4656 public QueryServiceConnection (String url , Properties properties ) throws SQLException {
4757 this .properties = properties ; // fixme: do deepCopy and modify the props
4858 this .serviceRootUrl = getServiceRootUrl (url );
4959 this .properties .put (Constants .LOGIN_URL , serviceRootUrl );
50- addClientSecretsIfRequired ( serviceRootUrl , this .properties );
60+ addClientUsernameIfRequired ( this .properties );
5161
5262 // default `enableArrowStream` is false
5363 enableArrowStream = Boolean .parseBoolean (this .properties .getProperty (Constants .ENABLE_ARROW_STREAM ));
@@ -57,8 +67,12 @@ public QueryServiceConnection(String url, Properties properties) throws SQLExcep
5767
5868 this .isSocksProxyDisabled = Boolean .parseBoolean (this .properties .getProperty (Constants .DISABLE_SOCKS_PROXY ));
5969
70+ boolean isTableauConnection = Constants .TABLEAU_USER_AGENT_VALUE .equals (properties .getProperty (Constants .USER_AGENT ));
71+
6072 // default `enableStreamFlow` is false
61- enableStreamFlow = Boolean .parseBoolean (this .properties .getProperty (Constants .ENABLE_STREAM_FLOW , Constants .FALSE_STR ));
73+ enableStreamFlow = isTableauConnection || Boolean .parseBoolean (this .properties .getProperty (Constants .ENABLE_STREAM_FLOW , Constants .FALSE_STR ));
74+
75+ log .info ("isTableauConnection {}, enableStreamFlow {}" , isTableauConnection , enableStreamFlow );
6276
6377 // use isValid to test connection
6478 this .isValid (20 );
@@ -87,37 +101,14 @@ static String getServiceRootUrl(String url) throws SQLException {
87101 /**
88102 * Adds client secrets to properties if not present and service url matches one of the existing envs.
89103 *
90- * @param serviceRootUrl service url which is used to infer the environment
91104 * @param properties Properties containing the config
92105 * @throws SQLException when given service url doesn't match any envs and config doesn't have exists secrets
93106 */
94107 @ VisibleForTesting
95- static void addClientSecretsIfRequired ( String serviceRootUrl , Properties properties ) throws SQLException {
108+ static void addClientUsernameIfRequired ( Properties properties ) throws SQLException {
96109 if (properties .containsKey (Constants .USER ) && !properties .containsKey (Constants .USER_NAME )) {
97110 properties .put (Constants .USER_NAME , properties .get (Constants .USER ));
98111 }
99-
100- if (properties .containsKey (Constants .USER_NAME )
101- && !properties .containsKey (Constants .CLIENT_ID )
102- && !properties .containsKey (Constants .CLIENT_SECRET )
103- && !properties .containsKey (Constants .PRIVATE_KEY )) {
104- log .debug ("adding client secrets for server {}" , serviceRootUrl );
105- String serverUrl = serviceRootUrl .toLowerCase ();
106- if (serverUrl .endsWith (Constants .NA45_SERVER_URL )) {
107- properties .put (Constants .CLIENT_ID , Constants .NA45_DEFAULT_CLIENT_ID );
108- properties .put (Constants .CLIENT_SECRET , Constants .NA45_DEFAULT_CLIENT_SECRET );
109- } else if (serverUrl .endsWith (Constants .NA46_SERVER_URL )) {
110- properties .put (Constants .CLIENT_ID , Constants .NA46_DEFAULT_CLIENT_ID );
111- properties .put (Constants .CLIENT_SECRET , Constants .NA46_DEFAULT_CLIENT_SECRET );
112- } else if (serverUrl .endsWith (Constants .PROD_SERVER_URL )) {
113- properties .put (Constants .CLIENT_ID , Constants .PROD_DEFAULT_CLIENT_ID );
114- properties .put (Constants .CLIENT_SECRET , Constants .PROD_DEFAULT_CLIENT_SECRET );
115- } else {
116- throw new SQLException ("specified url didn't match any existing envs" );
117- }
118- } else {
119- log .debug ("No client secrets added for server {}" , serviceRootUrl );
120- }
121112 }
122113
123114 public boolean getEnableArrowStream () {
@@ -141,6 +132,10 @@ public boolean updateStreamFlow(boolean flag) {
141132 return enableStreamFlow ;
142133 }
143134
135+ public QueryEngineEnum getQueryEngineEnum () {
136+ return queryEngineEnum ;
137+ }
138+
144139 @ Override
145140 public Statement createStatement () throws SQLException {
146141 return createStatement (ResultSet .TYPE_FORWARD_ONLY ,
@@ -351,17 +346,17 @@ public boolean isValid(int timeout) throws SQLException {
351346 }
352347
353348 try {
354- PreparedStatement statement = this .prepareStatement (TEST_CONNECT_QUERY );
355- return statement .execute ();
349+ if (this .isValid ) {
350+ log .info ("Reusing connection" );
351+ return true ;
352+ }
353+
354+ QueryConfigResponse configResponse = getQueryConfigResponse ();
355+ this .queryEngineEnum = QueryEngineEnum .fromValue (configResponse .getQueryengine ());
356+ this .isValid = true ;
357+ return true ;
356358 } catch (Exception e ) {
357359 log .error ("Exception while connecting to server" , e );
358- if (isEnableStreamFlow ()) {
359- // use http v2 api if hyper gRPC call is failing
360- updateStreamFlow (false );
361- try (PreparedStatement statement = this .prepareStatement (TEST_CONNECT_QUERY )) {
362- return statement .execute ();
363- }
364- }
365360 throw e ;
366361 }
367362 }
@@ -457,4 +452,20 @@ public String getTenantUrl() {
457452 public void setTenantUrl (String tenantUrl ) {
458453 this .tenantUrl = tenantUrl ;
459454 }
455+
456+ private QueryExecutor createQueryExecutor () {
457+ return new QueryExecutor (this );
458+ }
459+
460+ QueryConfigResponse getQueryConfigResponse () throws SQLException {
461+ try {
462+ QueryExecutor executor = createQueryExecutor ();
463+ Response response = executor .getQueryConfig ();
464+
465+ return HttpHelper .handleSuccessResponse (response , QueryConfigResponse .class , false );
466+ } catch (IOException e ) {
467+ log .error ("Exception while getting config from query service" , e );
468+ throw new SQLException (QUERY_CONFIG_ERROR , e );
469+ }
470+ }
460471}
0 commit comments