Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.arrow.driver.jdbc.client.oauth.OAuthConfiguration;
import org.apache.arrow.driver.jdbc.client.oauth.OAuthCredentialWriter;
import org.apache.arrow.driver.jdbc.client.oauth.OAuthTokenProvider;
import org.apache.arrow.driver.jdbc.client.utils.ClientAuthenticationUtils;
import org.apache.arrow.driver.jdbc.client.utils.FlightClientCache;
import org.apache.arrow.driver.jdbc.client.utils.FlightLocationQueue;
import org.apache.arrow.flight.CallOption;
import org.apache.arrow.flight.CallOptions;
import org.apache.arrow.flight.CallStatus;
import org.apache.arrow.flight.CloseSessionRequest;
import org.apache.arrow.flight.FlightClient;
Expand Down Expand Up @@ -81,6 +84,18 @@ public final class ArrowFlightSqlClientHandler implements AutoCloseable {
// JDBC connection string query parameter
private static final String CATALOG = "catalog";

@VisibleForTesting
static final CallOption CLOSE_SESSION_TIMEOUT = CallOptions.timeout(5, TimeUnit.SECONDS);

// No session to close: CloseSession is unsupported or unknown, none was created, or the
// client is not (or no longer) authenticated.
private static final Set<FlightStatusCode> NO_SESSION_STATUS_CODES =
EnumSet.of(
FlightStatusCode.UNIMPLEMENTED,
FlightStatusCode.INVALID_ARGUMENT,
FlightStatusCode.NOT_FOUND,
FlightStatusCode.UNAUTHENTICATED);

private final String cacheKey;
private final FlightSqlClient sqlClient;
private final Set<CallOption> options = new HashSet<>();
Expand Down Expand Up @@ -293,7 +308,7 @@ public long executeUpdate(final String query) {
@Override
public void close() throws SQLException {
try {
AutoCloseables.close(this::closeCatalogSession, sqlClient);
AutoCloseables.close(this::closeSession, sqlClient);
} catch (FlightRuntimeException fre) {
handleBenignCloseException(
fre, "Failed to clean up client resources.", "closing Flight SQL client");
Expand All @@ -304,11 +319,19 @@ public void close() throws SQLException {
}
}

private void closeCatalogSession() throws SQLException {
if (catalog.isPresent()) {
try {
sqlClient.closeSession(new CloseSessionRequest(), getOptions());
} catch (FlightRuntimeException fre) {
private void closeSession() throws SQLException {
if (!builder.ownsSession) {
return;
}
final CallOption[] baseOptions = getOptions();
final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, baseOptions.length + 1);
closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT;
try {
sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions);
} catch (FlightRuntimeException fre) {
if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is catalog such a relevant piece of information here? I don't understand why close was only called when there was a catalog and I don't understand why we surpress errors if it is empty.

This seems tuned to a specific server implementation

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xborder makes sense. I kept the catalog path as it was to keep the change small, but these statuses all mean there's no session to close, catalog or not. Happy to drop the catalog check so both work the same way.

logSuppressedCloseException(fre, "closing Flight SQL session");
} else {
handleBenignCloseException(
fre, "Failed to close Flight SQL session.", "closing Flight SQL session");
}
Expand Down Expand Up @@ -723,6 +746,11 @@ public static final class Builder {

@VisibleForTesting Optional<String> catalog = Optional.empty();

// Copies (getStreams endpoint handlers) authenticate as the connection; on servers that key
// sessions by token, closing one would end the connection's session. With retainAuth=false a
// copy's own session is left to expire.
@VisibleForTesting boolean ownsSession = true;

@VisibleForTesting @Nullable FlightClientCache flightClientCache;

@VisibleForTesting @Nullable Duration connectTimeout;
Expand Down Expand Up @@ -770,6 +798,7 @@ public Builder() {}
this.allocator = original.allocator;
this.catalog = original.catalog;
this.oauthConfig = original.oauthConfig;
this.ownsSession = false;

if (original.retainCookies) {
this.cookieFactory = original.cookieFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,27 @@ public void testJdbcDriverVersionIntegration() throws Exception {
}
}

@Test
public void testCloseWithoutCatalogClosesSession() throws Exception {
final Properties properties = new Properties();
properties.put(ArrowFlightConnectionProperty.USER.camelName(), userTest);
properties.put(ArrowFlightConnectionProperty.PASSWORD.camelName(), passTest);
properties.put("useEncryption", false);
final int closeSessionCountBefore = PRODUCER.getCloseSessionCount();

try (Connection connection =
DriverManager.getConnection(
"jdbc:arrow-flight-sql://"
+ FLIGHT_SERVER_TEST_EXTENSION.getHost()
+ ":"
+ FLIGHT_SERVER_TEST_EXTENSION.getPort(),
properties)) {
assertTrue(connection.isValid(300));
}

assertEquals(closeSessionCountBefore + 1, PRODUCER.getCloseSessionCount());
}

@Test
public void testSetCatalogShouldUpdateSessionOptions() throws Exception {
final Properties properties = new Properties();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.arrow.driver.jdbc.utils.CoreMockedSqlProducers;
import org.apache.arrow.driver.jdbc.utils.FallbackFlightSqlProducer;
import org.apache.arrow.driver.jdbc.utils.PartitionedFlightSqlProducer;
import org.apache.arrow.flight.Action;
import org.apache.arrow.flight.FlightConstants;
import org.apache.arrow.flight.FlightEndpoint;
import org.apache.arrow.flight.FlightProducer;
import org.apache.arrow.flight.FlightRuntimeException;
import org.apache.arrow.flight.FlightServer;
import org.apache.arrow.flight.FlightStatusCode;
import org.apache.arrow.flight.Location;
import org.apache.arrow.flight.Result;
import org.apache.arrow.flight.Ticket;
import org.apache.arrow.memory.BufferAllocator;
import org.apache.arrow.memory.RootAllocator;
Expand Down Expand Up @@ -559,6 +563,58 @@ public void testPartitionedFlightServerIgnoreFailure() throws Exception {
}
}

@Test
public void testEndpointFailureDoesNotCloseSession() throws Exception {
final Schema schema =
new Schema(
Collections.singletonList(
Field.nullablePrimitive("int_column", new ArrowType.Int(32, true))));
final AtomicInteger closeSessionCount = new AtomicInteger();
final FlightProducer dataProducer =
new PartitionedFlightSqlProducer.DataOnlyFlightSqlProducer(
new Ticket("first".getBytes(StandardCharsets.UTF_8)), null) {
@Override
public void doAction(
CallContext context, Action action, StreamListener<Result> listener) {
if (action.getType().equals(FlightConstants.CLOSE_SESSION.getType())) {
closeSessionCount.incrementAndGet();
}
super.doAction(context, action, listener);
}
};
try (BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
FlightServer dataServer =
FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), dataProducer)
.build()
.start()) {
final FlightEndpoint endpoint =
new FlightEndpoint(
new Ticket("wrong".getBytes(StandardCharsets.UTF_8)), dataServer.getLocation());

try (final PartitionedFlightSqlProducer rootProducer =
new PartitionedFlightSqlProducer(schema, endpoint);
FlightServer rootServer =
FlightServer.builder(allocator, forGrpcInsecure("localhost", 0), rootProducer)
.build()
.start();
Connection newConnection =
DriverManager.getConnection(
String.format(
"jdbc:arrow-flight-sql://%s:%d/?useEncryption=false",
rootServer.getLocation().getUri().getHost(), rootServer.getPort()));
Statement newStatement = newConnection.createStatement()) {
assertThrows(
SQLException.class,
() -> {
ResultSet result = newStatement.executeQuery("Select partitioned_data");
while (result.next()) {}
});
}

assertEquals(0, closeSessionCount.get());
}
}

@Test
public void testPartitionedFlightServerAllFailure() throws Exception {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -149,6 +151,46 @@ public void testCloseHandlesFlightRuntimeException(
verify(sqlClient).close();
}

@ParameterizedTest
@MethodSource
public void testCloseSessionWithoutCatalog(FlightStatusCode code, boolean shouldSuppress)
throws Exception {
FlightSqlClient sqlClient = mock(FlightSqlClient.class);
doThrow(new CallStatus(code, null, null, null).toRuntimeException())
.when(sqlClient)
.closeSession(any(CloseSessionRequest.class), any(CallOption[].class));
ArrowFlightSqlClientHandler sqlClientHandler =
new ArrowFlightSqlClientHandler(
"cacheKey",
sqlClient,
new ArrowFlightSqlClientHandler.Builder(),
new ArrayList<>(),
Optional.empty(),
null);

if (shouldSuppress) {
assertDoesNotThrow(sqlClientHandler::close);
} else {
assertThrows(SQLException.class, sqlClientHandler::close);
}
verify(sqlClient)
.closeSession(
any(CloseSessionRequest.class), eq(ArrowFlightSqlClientHandler.CLOSE_SESSION_TIMEOUT));
verify(sqlClient).close();
}

private static Object[] testCloseSessionWithoutCatalog() {
return new Object[] {
new Object[] {FlightStatusCode.UNIMPLEMENTED, true},
new Object[] {FlightStatusCode.INVALID_ARGUMENT, true},
new Object[] {FlightStatusCode.NOT_FOUND, true},
new Object[] {FlightStatusCode.UNAVAILABLE, true},
new Object[] {FlightStatusCode.UNAUTHENTICATED, true},
new Object[] {FlightStatusCode.UNKNOWN, false},
new Object[] {FlightStatusCode.TIMED_OUT, false},
};
Comment on lines +182 to +191

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we also include FlightStatusCode.DEADLINE_EXCEEDED (false) here? Since CLOSE_SESSION_TIMEOUT introduces a 5s deadline on closeSession, having an explicit test case confirms that a timeout when closing without a catalog still closes sqlClient and propagates SQLException.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering this non-blocking as well — nice-to-have if you push another update, otherwise fine as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Flight maps DEADLINE_EXCEEDED to TIMED_OUT, so the new row is {TIMED_OUT, false}.

}

private static Object[] testCloseHandlesFlightRuntimeException() {
CallStatus benignInternalError =
new CallStatus(FlightStatusCode.INTERNAL, null, "Connection closed after GOAWAY", null);
Expand All @@ -168,4 +210,23 @@ private static Object[] testCloseHandlesFlightRuntimeException() {
new Object[] {true, CallStatus.CANCELLED, false},
};
}

@Test
public void testCloseFromBuilderCopyDoesNotCloseSession() throws Exception {
FlightSqlClient sqlClient = mock(FlightSqlClient.class);
ArrowFlightSqlClientHandler sqlClientHandler =
new ArrowFlightSqlClientHandler(
"cacheKey",
sqlClient,
new ArrowFlightSqlClientHandler.Builder(new ArrowFlightSqlClientHandler.Builder()),
new ArrayList<>(),
Optional.of("test_catalog"),
null);

sqlClientHandler.close();

verify(sqlClient, never())
.closeSession(any(CloseSessionRequest.class), any(CallOption[].class));
verify(sqlClient).close();
}
}