1818
1919import java .io .Closeable ;
2020import java .io .File ;
21- import java .time .Duration ;
2221import java .util .Optional ;
2322import java .util .Set ;
2423
3130import com .datastax .astra .sdk .organizations .OrganizationsClient ;
3231import com .datastax .astra .sdk .streaming .StreamingClient ;
3332import com .datastax .astra .sdk .utils .ApiLocator ;
34- import com .datastax .oss .driver .api .core .ConsistencyLevel ;
3533import com .datastax .oss .driver .api .core .CqlSession ;
36- import com .datastax .oss .driver .api .core .config .TypedDriverOption ;
3734import com .datastax .stargate .sdk .StargateClient ;
3835import com .datastax .stargate .sdk .config .StargateNodeConfig ;
3936import com .datastax .stargate .sdk .doc .ApiDocumentClient ;
@@ -109,6 +106,7 @@ public AstraClient(String dbId, String dbRegion, String token) {
109106 .withDatabaseRegion (dbRegion ));
110107 }
111108
109+
112110 /**
113111 * Initialization through builder.
114112 *
@@ -136,75 +134,47 @@ public AstraClient(AstraClientConfig config) {
136134 // ---------------------------------------------------
137135
138136 if (Utils .hasAllLength (config .getDatabaseId (), config .getDatabaseRegion ())) {
139- LOGGER .info ("+ Using db with id ["
137+ LOGGER .info ("+ Db: id ["
140138 + AnsiUtils .cyan ("{}" )+ "] and region ["
141139 + AnsiUtils .cyan ("{}" )+ "]" ,
142140 config .getDatabaseId (),
143141 config .getDatabaseRegion ());
142+
144143 this .currentDatabaseRegion = config .getDatabaseRegion ();
145144
146- // ---------------------------------------------------
147- // CQL / Cloud Secure Bundles
148- // ---------------------------------------------------
145+ // Set default region (not in the cql as SCB is there)
146+ config .getStargateConfig ().setLocalDatacenter (config .getDatabaseRegion ());
147+
148+ // CqlSession should be initialized only if the flag is on.
149+ if (config .getStargateConfig ().isEnabledCql ()) {
149150
150- if (!new File (config .getSecureConnectBundleFolder ()).exists ()) {
151- new File (config .getSecureConnectBundleFolder ()).mkdirs ();
151+ // Downloading SCB is enabled (default is true)
152+ if (config .isEnabledDownloadSecureConnectBundle ()) {
153+ downloadAndSetupSecureConnectBundle (config );
154+ }
155+
156+ // ---------------------------------------------------
157+ // CQL / Credentials
158+ // ---------------------------------------------------
159+
160+ if (Utils .hasAllLength (config .getClientId (), config .getClientSecret ())) {
161+ config .getStargateConfig ().withAuthCredentials (config .getClientId (), config .getClientSecret ());
162+ } else {
163+ config .getStargateConfig ().withAuthCredentials ("token" , config .getToken ());
164+ }
165+
152166 }
153- // Download secure bundles (if needed)
154- LOGGER .info ("+ Downloading bundles in: [" + AnsiUtils .cyan ("{}" ) + "]" , config .getSecureConnectBundleFolder ());
155- apiDevopsDatabases .database (config .getDatabaseId ())
156- .downloadAllSecureConnectBundles (config .getSecureConnectBundleFolder ());
157167
158- // Enforce secure bundle for each DC
168+ // ---------------------------------------------------
169+ // Stargate Node per region
170+ // ---------------------------------------------------
159171
172+ // Retrieve the list of regions for a db
160173 Set <Datacenter > regions = apiDevopsDatabases
161174 .database (config .getDatabaseId ())
162175 .find ().get ().getInfo ()
163176 .getDatacenters ();
164177
165- regions .stream ().forEach (dc -> {
166- config .getStargateConfig ()
167- .withCqlCloudSecureConnectBundleDC (dc .getRegion (),
168- config .getSecureConnectBundleFolder () + File .separator +
169- AstraClientConfig .buildScbFileName (config .getDatabaseId (), dc .getRegion ()));
170- });
171- // Setup the current region
172- config .getStargateConfig ().withCqlCloudSecureConnectBundle (config .getSecureConnectBundleFolder () +
173- File .separator + AstraClientConfig .buildScbFileName (config .getDatabaseId (), config .getDatabaseRegion ()));
174-
175- // ---------------------------------------------------
176- // CQL / Credentials
177- // ---------------------------------------------------
178-
179- if (Utils .hasAllLength (config .getClientId (), config .getClientSecret ())) {
180- config .getStargateConfig ().withAuthCredentials (config .getClientId (), config .getClientSecret ());
181- LOGGER .info ("+ Credentials used for Cql connection are clientId/clientSecret" );
182- } else {
183- config .getStargateConfig ().withAuthCredentials ("token" , config .getToken ());
184- LOGGER .info ("+ Credentials used for Cql are based on the token " );
185- }
186-
187- // ---------------------------------------------------
188- // CQL / Timeouts
189- // ---------------------------------------------------
190-
191- // Region to setup Stargate
192- config .getStargateConfig ()
193- .withCqlDriverOption (TypedDriverOption .REQUEST_CONSISTENCY , ConsistencyLevel .LOCAL_QUORUM .name ())
194- .withCqlDriverOption (TypedDriverOption .REQUEST_TIMEOUT , Duration .ofSeconds (10 ))
195- .withCqlDriverOption (TypedDriverOption .REQUEST_PAGE_SIZE , 100 )
196- .withCqlDriverOption (TypedDriverOption .CONNECTION_CONNECT_TIMEOUT , Duration .ofSeconds (10 ))
197- .withCqlDriverOption (TypedDriverOption .CONNECTION_INIT_QUERY_TIMEOUT , Duration .ofSeconds (10 ))
198- .withCqlDriverOption (TypedDriverOption .CONNECTION_SET_KEYSPACE_TIMEOUT , Duration .ofSeconds (10 ))
199- .withCqlDriverOption (TypedDriverOption .CONTROL_CONNECTION_TIMEOUT , Duration .ofSeconds (10 ))
200- // Failover options:
201- // https://docs.datastax.com/en/developer/java-driver/4.13/manual/core/load_balancing/#cross-datacenter-failover
202- .withCqlDriverOption (TypedDriverOption .LOAD_BALANCING_DC_FAILOVER_ALLOW_FOR_LOCAL_CONSISTENCY_LEVELS , true )
203- .withCqlDriverOption (TypedDriverOption .LOAD_BALANCING_DC_FAILOVER_MAX_NODES_PER_REMOTE_DC , 3 );
204-
205- // ---------------------------------------------------
206- // Stargate Node per region
207- // ---------------------------------------------------
208178 regions .stream ().forEach (dc -> {
209179 config .getStargateConfig ().withApiNodeDC (dc .getRegion (),
210180 new StargateNodeConfig (
@@ -218,11 +188,17 @@ public AstraClient(AstraClientConfig config) {
218188 ApiLocator .getApiGrpcEndPoint (config .getDatabaseId (), dc .getRegion ()),
219189 // port for grpc
220190 AstraClientConfig .GRPC_PORT ));
191+
192+ config .getStargateConfig ()
193+ .withCqlCloudSecureConnectBundleDC (dc .getRegion (),
194+ config .getSecureConnectBundleFolder ()
195+ + File .separator
196+ + AstraClientConfig .buildScbFileName (config .getDatabaseId (), dc .getRegion ()));
221197 }
222198 );
223- // Set default region
224- config .getStargateConfig ().withLocalDatacenter (config .getDatabaseRegion ());
199+
225200 this .stargateClient = config .getStargateConfig ().build ();
201+
226202 } else {
227203 LOGGER .info ("+ API(s) CqlSession [" + AnsiUtils .red ("DISABLED" )+ "]" );
228204 LOGGER .info ("+ API(s) Document [" + AnsiUtils .red ("DISABLED" )+ "]" );
@@ -232,6 +208,27 @@ public AstraClient(AstraClientConfig config) {
232208 LOGGER .info ("[" + AnsiUtils .yellow ("AstraClient" ) + "] has been initialized." );
233209 }
234210
211+
212+ /**
213+ * Download the secure connect bundle files
214+ * @param config
215+ */
216+ private void downloadAndSetupSecureConnectBundle (AstraClientConfig config ) {
217+ if (!new File (config .getSecureConnectBundleFolder ()).exists ()) {
218+ new File (config .getSecureConnectBundleFolder ()).mkdirs ();
219+ }
220+ // Download secure bundles (if needed)
221+ LOGGER .info ("+ Downloading bundles in: [" + AnsiUtils .cyan ("{}" ) + "]" , config .getSecureConnectBundleFolder ());
222+ apiDevopsDatabases .database (config .getDatabaseId ())
223+ .downloadAllSecureConnectBundles (config .getSecureConnectBundleFolder ());
224+
225+ // Setup the current region
226+ String scbFile = config .getSecureConnectBundleFolder ()
227+ + File .separator
228+ + AstraClientConfig .buildScbFileName (config .getDatabaseId (), config .getDatabaseRegion ());
229+ config .getStargateConfig ().withCqlCloudSecureConnectBundle (scbFile );
230+ }
231+
235232 /**
236233 * Document Api.
237234 *
@@ -332,8 +329,9 @@ public StreamingClient apiDevopsStreaming() {
332329 */
333330 public CqlSession cqlSession () {
334331 if (stargateClient == null || !stargateClient .cqlSession ().isPresent ()) {
335- throw new IllegalStateException ("CQL not available Rest is not available "
336- + "you need to provide dbId/dbRegion/username/password at initialization." );
332+ throw new IllegalStateException ("CQL Session is not available."
333+ + " Make sure you enabled it with .enableCql() and provide all"
334+ + " expected paramters: keyspace, contact points or SCB, user+password " );
337335 }
338336 return stargateClient .cqlSession ().get ();
339337 }
@@ -368,7 +366,7 @@ public void useRegion(String region) {
368366 LOGGER .info ("Switch to region : {}" , region );
369367 this .currentDatabaseRegion = region ;
370368 this .stargateClient .setCurrentDatacenter (region );
371- this .stargateClient .renewCqlSession ();
369+ this .stargateClient .initCqlSession ();
372370 }
373371
374372 /**
0 commit comments