Skip to content

Commit 195935b

Browse files
committed
Disable Cql by default, cql fine grained config in YAML
1 parent 121b1e3 commit 195935b

File tree

56 files changed

+3040
-1188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+3040
-1188
lines changed

astra-sdk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>com.datastax.astra</groupId>
99
<artifactId>astra-sdk-parent</artifactId>
10-
<version>0.2.7-SNAPSHOT</version>
10+
<version>0.3.0-SNAPSHOT</version>
1111
</parent>
1212

1313
<dependencies>

astra-sdk/pom.xml.versionsBackup

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>astra-sdk</artifactId>
5+
<name>+ astra-sdk</name>
6+
<description>Connecting Client APIS provided by ASTRA</description>
7+
<parent>
8+
<groupId>com.datastax.astra</groupId>
9+
<artifactId>astra-sdk-parent</artifactId>
10+
<version>0.2.7-SNAPSHOT</version>
11+
</parent>
12+
13+
<dependencies>
14+
15+
<!-- ASTRA STREAMING -->
16+
<dependency>
17+
<groupId>org.apache.pulsar</groupId>
18+
<artifactId>pulsar-client-admin</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.apache.pulsar</groupId>
22+
<artifactId>pulsar-client</artifactId>
23+
</dependency>
24+
25+
<!-- Astra Expose APIs through Stargate -->
26+
<dependency>
27+
<groupId>com.datastax.stargate</groupId>
28+
<artifactId>stargate-sdk</artifactId>
29+
<version>${project.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.datastax.oss</groupId>
34+
<artifactId>java-driver-core</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.fasterxml.jackson.core</groupId>
38+
<artifactId>jackson-core</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-annotations</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.fasterxml.jackson.core</groupId>
46+
<artifactId>jackson-databind</artifactId>
47+
</dependency>
48+
<dependency>
49+
<groupId>ch.qos.logback</groupId>
50+
<artifactId>logback-classic</artifactId>
51+
</dependency>
52+
<dependency>
53+
<groupId>ch.qos.logback</groupId>
54+
<artifactId>logback-core</artifactId>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.junit.jupiter</groupId>
58+
<artifactId>junit-jupiter-engine</artifactId>
59+
<scope>test</scope>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.junit.platform</groupId>
63+
<artifactId>junit-platform-runner</artifactId>
64+
<scope>test</scope>
65+
</dependency>
66+
</dependencies>
67+
</project>

astra-sdk/src/main/java/com/datastax/astra/sdk/AstraClient.java

Lines changed: 60 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.Closeable;
2020
import java.io.File;
21-
import java.time.Duration;
2221
import java.util.Optional;
2322
import java.util.Set;
2423

@@ -31,9 +30,7 @@
3130
import com.datastax.astra.sdk.organizations.OrganizationsClient;
3231
import com.datastax.astra.sdk.streaming.StreamingClient;
3332
import com.datastax.astra.sdk.utils.ApiLocator;
34-
import com.datastax.oss.driver.api.core.ConsistencyLevel;
3533
import com.datastax.oss.driver.api.core.CqlSession;
36-
import com.datastax.oss.driver.api.core.config.TypedDriverOption;
3734
import com.datastax.stargate.sdk.StargateClient;
3835
import com.datastax.stargate.sdk.config.StargateNodeConfig;
3936
import 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

Comments
 (0)