Skip to content

Commit

Permalink
GH 41262:[Java][FlightSQL] Implement stateless prepared statement
Browse files Browse the repository at this point in the history
Update to use JUnit TempDir
  • Loading branch information
stevelorddremio committed May 16, 2024
1 parent c5b2f56 commit cabf9d8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
public class FlightSqlExample implements FlightSqlProducer, AutoCloseable {
private static final Logger LOGGER = getLogger(FlightSqlExample.class);
protected static final Calendar DEFAULT_CALENDAR = JdbcToArrowUtils.getUtcCalendar();
public static final String DB_NAME = "derbyDB";
public static final Path DB_PATH = Paths.get("target", "derbyDB");
private final String databaseUri;
// ARROW-15315: Use ExecutorService to simulate an async scenario
private final ExecutorService executorService = Executors.newFixedThreadPool(10);
Expand All @@ -171,21 +171,35 @@ public class FlightSqlExample implements FlightSqlProducer, AutoCloseable {

public static void main(String[] args) throws Exception {
Location location = Location.forGrpcInsecure("localhost", 55555);
final FlightSqlExample example = new FlightSqlExample(location, DB_NAME);
final FlightSqlExample example = new FlightSqlExample(location, DB_PATH);
Location listenLocation = Location.forGrpcInsecure("0.0.0.0", 55555);
try (final BufferAllocator allocator = new RootAllocator();
final FlightServer server = FlightServer.builder(allocator, listenLocation, example).build()) {
server.start();
server.awaitTermination();
}
removeDerbyDatabaseIfExists(DB_PATH);
}

public FlightSqlExample(final Location location, final String dbName) {
private String toUri(Path path) {
if (path.getNameCount() == 0) {
return "";
}

String uri = path.getName(0).toString();
for (int i = 1; i < path.getNameCount(); i++) {
uri = uri.concat("/").concat(path.getName(i).toString());
}
return uri;
}

public FlightSqlExample(final Location location, final Path dbName) {
// TODO Constructor should not be doing work.
final String derbyDatabaseUri = toUri(dbName);
databaseUri = "jdbc:derby:" + derbyDatabaseUri;
checkState(
removeDerbyDatabaseIfExists(dbName) && populateDerbyDatabase(dbName),
populateDerbyDatabase(databaseUri),
"Failed to reset Derby database!");
databaseUri = "jdbc:derby:target/" + dbName;
final ConnectionFactory connectionFactory =
new DriverManagerConnectionFactory(databaseUri, new Properties());
final PoolableConnectionFactory poolableConnectionFactory =
Expand Down Expand Up @@ -250,9 +264,8 @@ public FlightSqlExample(final Location location, final String dbName) {

}

public static boolean removeDerbyDatabaseIfExists(final String dbName) {
public static boolean removeDerbyDatabaseIfExists(final Path path) {
boolean wasSuccess;
final Path path = Paths.get("target" + File.separator + dbName);

try (final Stream<Path> walk = Files.walk(path)) {
/*
Expand All @@ -279,8 +292,8 @@ public static boolean removeDerbyDatabaseIfExists(final String dbName) {
return wasSuccess;
}

private static boolean populateDerbyDatabase(final String dbName) {
try (final Connection connection = DriverManager.getConnection("jdbc:derby:target/" + dbName + ";create=true");
private static boolean populateDerbyDatabase(final String databaseUri) {
try (final Connection connection = DriverManager.getConnection(databaseUri + ";create=true");
Statement statement = connection.createStatement()) {

dropTable(statement, "intTable");
Expand Down Expand Up @@ -779,7 +792,6 @@ public void close() throws Exception {
} catch (Throwable t) {
LOGGER.error(format("Failed to close resources: <%s>", t.getMessage()), t);
}

AutoCloseables.close(dataSource, rootAllocator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import java.io.StreamCorruptedException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
Expand Down Expand Up @@ -66,11 +68,11 @@
*/
public class FlightSqlStatelessExample extends FlightSqlExample {
private static final Logger LOGGER = getLogger(FlightSqlStatelessExample.class);
public static final String DB_NAME = "derbyStatelessDB";
public static final Path DB_PATH = Paths.get("target", "derbyStatelessDB");


public FlightSqlStatelessExample(final Location location, final String dbName) {
super(location, dbName);
public FlightSqlStatelessExample(final Location location, final Path dbPath) {
super(location, dbPath);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.nio.file.Path;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -72,6 +73,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.api.io.TempDir;

import com.google.common.collect.ImmutableList;

Expand All @@ -95,6 +97,9 @@ public class TestFlightSql {
protected static FlightServer server;
protected static FlightSqlClient sqlClient;

@TempDir
private static Path dbPath = FlightSqlExample.DB_PATH;

@BeforeAll
public static void setUp() throws Exception {
setUpClientServer();
Expand All @@ -106,7 +111,7 @@ private static void setUpClientServer() throws Exception {

final Location serverLocation = Location.forGrpcInsecure(LOCALHOST, 0);
server = FlightServer.builder(allocator, serverLocation,
new FlightSqlExample(serverLocation, FlightSqlExample.DB_NAME))
new FlightSqlExample(serverLocation, dbPath))
.build()
.start();

Expand Down Expand Up @@ -152,7 +157,6 @@ protected static void setUpExpectedResultsMap() {
@AfterAll
public static void tearDown() throws Exception {
close(sqlClient, server, allocator);
FlightSqlExample.removeDerbyDatabaseIfExists(FlightSqlExample.DB_NAME);
}

private static List<List<String>> getNonConformingResultsForGetSqlInfo(final List<? extends List<String>> results) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.apache.arrow.util.AutoCloseables.close;
import static org.hamcrest.CoreMatchers.*;

import java.nio.file.Path;

import org.apache.arrow.flight.FlightClient;
import org.apache.arrow.flight.FlightEndpoint;
import org.apache.arrow.flight.FlightInfo;
Expand All @@ -39,11 +41,15 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Test direct usage of Flight SQL workflows.
*/
public class TestFlightSqlStateless extends TestFlightSql {
@TempDir
private static Path dbPath = FlightSqlStatelessExample.DB_PATH;


@BeforeAll
public static void setUp() throws Exception {
Expand All @@ -54,15 +60,14 @@ public static void setUp() throws Exception {
@AfterAll
public static void tearDown() throws Exception {
close(sqlClient, server, allocator);
FlightSqlStatelessExample.removeDerbyDatabaseIfExists(FlightSqlStatelessExample.DB_NAME);
}

private static void setUpClientServer() throws Exception {
allocator = new RootAllocator(Integer.MAX_VALUE);

final Location serverLocation = Location.forGrpcInsecure(LOCALHOST, 0);
server = FlightServer.builder(allocator, serverLocation,
new FlightSqlStatelessExample(serverLocation, FlightSqlStatelessExample.DB_NAME))
new FlightSqlStatelessExample(serverLocation, dbPath))
.build()
.start();

Expand Down

0 comments on commit cabf9d8

Please sign in to comment.