Skip to content

Commit ee02524

Browse files
authored
Merge pull request #7 from wherobots/peter/multi-session
feat: add single and multi connection session type options
2 parents bd2d9e4 + bf52447 commit ee02524

File tree

6 files changed

+65
-18
lines changed

6 files changed

+65
-18
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,8 @@ build
3232
# IntelliJ
3333
.idea/
3434

35+
# VSCode
36+
.vscode/
37+
3538
# Vim
3639
*.swp

lib/build.gradle

+14-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ plugins {
1515

1616
description = 'JDBC driver for the Wherobots Cloud Spatial SQL API'
1717
group = 'com.wherobots.jdbc'
18-
version = '0.1.0'
18+
version = '0.1.1'
1919

2020
repositories {
2121
mavenCentral()
@@ -90,3 +90,16 @@ signing {
9090
test {
9191
useJUnitPlatform()
9292
}
93+
94+
// Run this task with `./gradlew runSmokeTest -Papikey=<your_api_key>`
95+
task runSmokeTest {
96+
group = 'SmokeTest'
97+
doLast {
98+
tasks.create('runSmokeTestExec', JavaExec) {
99+
main = 'com.wherobots.db.jdbc.SmokeTest'
100+
args = ["$apikey"]
101+
classpath = sourceSets.test.runtimeClasspath
102+
jvmArgs = ["--add-opens=java.base/java.nio=org.apache.arrow.memory.core,ALL-UNNAMED"]
103+
}.exec()
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.wherobots.db;
2+
3+
public enum SessionType {
4+
// Allow only a single concurrent connection to the SQL Session
5+
SINGLE("single"),
6+
// Allow multiple concurrent connections to the SQL Session
7+
MULTI("multi");
8+
9+
public final String name;
10+
11+
SessionType(String name) {
12+
this.name = name;
13+
}
14+
}

lib/src/main/java/com/wherobots/db/jdbc/WherobotsJdbcDriver.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.wherobots.db.Region;
44
import com.wherobots.db.Runtime;
5+
import com.wherobots.db.SessionType;
56
import com.wherobots.db.jdbc.session.WherobotsSession;
67
import com.wherobots.db.jdbc.session.WherobotsSessionSupplier;
78
import org.apache.commons.lang3.StringUtils;
@@ -31,7 +32,7 @@ public class WherobotsJdbcDriver implements Driver {
3132
public static final String TOKEN_PROP = "token";
3233
public static final String RUNTIME_PROP = "runtime";
3334
public static final String REGION_PROP = "region";
34-
public static final String REUSE_SESSION_PROP = "reuseSession";
35+
public static final String SESSION_TYPE_PROP = "sessionType";
3536
public static final String WS_URI_PROP = "wsUri";
3637

3738
// Results format; one of {@link DataFormat}
@@ -48,7 +49,7 @@ public class WherobotsJdbcDriver implements Driver {
4849

4950
public static final Runtime DEFAULT_RUNTIME = Runtime.TINY;
5051
public static final Region DEFAULT_REGION = Region.AWS_US_WEST_2;
51-
public static final boolean DEFAULT_REUSE_SESSION = true;
52+
public static final SessionType DEFAULT_SESSION_TYPE = SessionType.SINGLE;
5253

5354
public Map<String, String> getUserAgentHeader() {
5455
String javaVersion = System.getProperty("java.version");
@@ -86,10 +87,10 @@ public Connection connect(String url, Properties info) throws SQLException {
8687
region = Region.valueOf(regionName);
8788
}
8889

89-
boolean reuse = DEFAULT_REUSE_SESSION;
90-
String reuseSession = info.getProperty(REUSE_SESSION_PROP);
91-
if (StringUtils.isNotBlank(reuseSession)) {
92-
reuse = Boolean.parseBoolean(reuseSession);
90+
SessionType sessionType = DEFAULT_SESSION_TYPE;
91+
String sessionTypeName = info.getProperty(SESSION_TYPE_PROP);
92+
if (StringUtils.isNotBlank(sessionTypeName)) {
93+
sessionType = SessionType.valueOf(sessionTypeName);
9394
}
9495

9596
Map<String, String> headers = new HashMap<>(getAuthHeaders(info));
@@ -105,7 +106,7 @@ public Connection connect(String url, Properties info) throws SQLException {
105106
throw new SQLException("Invalid WebSocket URI: " + wsUriString, e);
106107
}
107108
} else {
108-
session = WherobotsSessionSupplier.create(host, runtime, region, reuse, headers);
109+
session = WherobotsSessionSupplier.create(host, runtime, region, sessionType, headers);
109110
}
110111

111112
return new WherobotsJdbcConnection(session, info);

lib/src/main/java/com/wherobots/db/jdbc/session/WherobotsSessionSupplier.java

+16-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.wherobots.db.AppStatus;
66
import com.wherobots.db.Region;
77
import com.wherobots.db.Runtime;
8+
import com.wherobots.db.SessionType;
89
import com.wherobots.db.jdbc.serde.JsonUtil;
910
import io.github.resilience4j.core.IntervalFunction;
1011
import io.github.resilience4j.core.functions.CheckedSupplier;
@@ -34,11 +35,14 @@ public abstract class WherobotsSessionSupplier {
3435

3536
private static final Logger logger = LoggerFactory.getLogger(WherobotsSessionSupplier.class);
3637

37-
private static final String SQL_SESSION_ENDPOINT = "https://%s/sql/session?region=%s&reuse_session=%s";
38+
private static final String SQL_SESSION_ENDPOINT = "https://%s/sql/session?region=%s";
3839
private static final String PROTOCOL_VERSION = "1.0.0";
3940

4041
@JsonInclude(JsonInclude.Include.NON_NULL)
41-
private record SqlSessionRequestPayload(String runtimeId, Integer shutdownAfterInactiveSeconds) {}
42+
private record SqlSessionRequestPayload(
43+
String runtimeId,
44+
Integer shutdownAfterInactiveSeconds,
45+
String sessionType) {}
4246
private record SqlSessionAppMeta(String url) {}
4347
private record SqlSessionResponsePayload(AppStatus status, SqlSessionAppMeta appMeta) {}
4448

@@ -52,7 +56,7 @@ private record SqlSessionResponsePayload(AppStatus status, SqlSessionAppMeta app
5256
* @return
5357
* @throws SQLException
5458
*/
55-
public static WherobotsSession create(String host, Runtime runtime, Region region, boolean reuse, Map<String, String> headers)
59+
public static WherobotsSession create(String host, Runtime runtime, Region region, SessionType sessionType, Map<String, String> headers)
5660
throws SQLException {
5761
HttpClient client = HttpClient.newBuilder()
5862
.followRedirects(HttpClient.Redirect.NORMAL)
@@ -67,7 +71,7 @@ public static WherobotsSession create(String host, Runtime runtime, Region regio
6771
Retry retry = RetryRegistry.of(config).retry("session");
6872

6973
try {
70-
URI sessionIdUri = new SqlSessionSupplier(client, headers, host, runtime, region, reuse).get();
74+
URI sessionIdUri = new SqlSessionSupplier(client, headers, host, runtime, region, sessionType).get();
7175
URI wsUri = Retry.decorateCheckedSupplier(retry, new SessionWsUriSupplier(client, headers, sessionIdUri)).get();
7276
return create(wsUri, headers);
7377
} catch (SQLException e) {
@@ -103,20 +107,23 @@ private record SqlSessionSupplier(HttpClient client,
103107
String host,
104108
Runtime runtime,
105109
Region region,
106-
boolean reuse)
110+
SessionType sessionType)
107111
implements CheckedSupplier<URI> {
108112

109113
@Override
110114
public URI get() throws IOException, InterruptedException {
111-
logger.info("{} {} runtime in {} from {}...",
112-
reuse ? "Recycling" : "Requesting", runtime.name, region.name, host);
115+
logger.info("Requesting {} runtime using {} session type in {} from {}...",
116+
runtime.name, sessionType.name, region.name, host);
113117

114118
HttpRequest.BodyPublisher body = HttpRequest.BodyPublishers.ofString(
115-
JsonUtil.serialize(new SqlSessionRequestPayload(runtime.name, null)));
119+
JsonUtil.serialize(new SqlSessionRequestPayload(
120+
runtime.name,
121+
null,
122+
sessionType.name)));
116123

117124
HttpRequest.Builder request = HttpRequest.newBuilder()
118125
.POST(body)
119-
.uri(URI.create(String.format(SQL_SESSION_ENDPOINT, host, region.name, reuse)))
126+
.uri(URI.create(String.format(SQL_SESSION_ENDPOINT, host, region.name)))
120127
.header("Content-Type", "application/json");
121128
headers.forEach(request::header);
122129

lib/src/test/java/com/wherobots/db/jdbc/SmokeTest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
import java.sql.Statement;
88
import java.util.Properties;
99

10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.wherobots.db.SessionType;
14+
1015
public class SmokeTest {
16+
private static final Logger logger = LoggerFactory.getLogger(SmokeTest.class);
17+
1118
public static void main(String[] args) throws Exception {
1219
DriverManager.setLogWriter(new PrintWriter(System.out));
1320
DriverManager.registerDriver(new WherobotsJdbcDriver());
@@ -27,7 +34,9 @@ public static void main(String[] args) throws Exception {
2734

2835
Properties props = new Properties();
2936
props.put(WherobotsJdbcDriver.API_KEY_PROP, args[0]);
30-
props.put(WherobotsJdbcDriver.REUSE_SESSION_PROP, "true");
37+
props.put(WherobotsJdbcDriver.SESSION_TYPE_PROP, SessionType.SINGLE.name());
38+
39+
logger.info("Connecting to Wherobots SQL API with properties: {}", props);
3140

3241
try (Connection conn = DriverManager.getConnection("jdbc:wherobots://api.staging.wherobots.com", props)) {
3342
try (Statement stmt = conn.createStatement()) {

0 commit comments

Comments
 (0)