diff --git a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java index 97108e599b..ba923c69a9 100644 --- a/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java +++ b/flight/flight-sql-jdbc-core/src/main/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandler.java @@ -27,11 +27,13 @@ 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; @@ -39,6 +41,7 @@ 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; @@ -80,6 +83,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 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 options = new HashSet<>(); @@ -291,22 +306,30 @@ public long executeUpdate(final String query) { @Override public void close() throws SQLException { - if (catalog.isPresent()) { + final CallOption[] baseOptions = getOptions(); + final CallOption[] closeSessionOptions = Arrays.copyOf(baseOptions, baseOptions.length + 1); + closeSessionOptions[baseOptions.length] = CLOSE_SESSION_TIMEOUT; + try { + if (builder.ownsSession) { + sqlClient.closeSession(new CloseSessionRequest(), closeSessionOptions); + } + } catch (FlightRuntimeException fre) { + if (catalog.isEmpty() && NO_SESSION_STATUS_CODES.contains(fre.status().code())) { + logSuppressedCloseException(fre, "closing Flight SQL session"); + } else { + handleBenignCloseException( + fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); + } + } finally { try { - sqlClient.closeSession(new CloseSessionRequest(), getOptions()); + AutoCloseables.close(sqlClient); } catch (FlightRuntimeException fre) { handleBenignCloseException( - fre, "Failed to close Flight SQL session.", "closing Flight SQL session"); + fre, "Failed to clean up client resources.", "closing Flight SQL client"); + } catch (final Exception e) { + throw new SQLException("Failed to clean up client resources.", e); } } - try { - AutoCloseables.close(sqlClient); - } catch (FlightRuntimeException fre) { - handleBenignCloseException( - fre, "Failed to clean up client resources.", "closing Flight SQL client"); - } catch (final Exception e) { - throw new SQLException("Failed to clean up client resources.", e); - } } /** @@ -717,6 +740,11 @@ public static final class Builder { @VisibleForTesting Optional 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; @@ -764,6 +792,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; diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java index ca9ae1adf3..eda38d85ef 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ConnectionTest.java @@ -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(); diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java index 7d1bf67e45..c5c6a4fbb2 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/ResultSetTest.java @@ -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; @@ -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 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 diff --git a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java index b44b310eb5..12b8c5e1e5 100644 --- a/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java +++ b/flight/flight-sql-jdbc-core/src/test/java/org/apache/arrow/driver/jdbc/client/ArrowFlightSqlClientHandlerTest.java @@ -22,8 +22,11 @@ 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; @@ -145,6 +148,46 @@ public void testCloseHandlesFlightRuntimeException( } else { assertThrows(SQLException.class, sqlClientHandler::close); } + 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}, + }; } private static Object[] testCloseHandlesFlightRuntimeException() { @@ -165,4 +208,23 @@ private static Object[] testCloseHandlesFlightRuntimeException() { new Object[] {false, unknownError, 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(); + } }