diff --git a/wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators/SqlToStreamOperator.java b/wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators/SqlToStreamOperator.java index fbf20776b..d8ed3d9ed 100644 --- a/wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators/SqlToStreamOperator.java +++ b/wayang-platforms/wayang-jdbc-template/src/main/java/org/apache/wayang/jdbc/operators/SqlToStreamOperator.java @@ -27,6 +27,8 @@ import org.apache.wayang.core.plan.wayangplan.UnaryToUnaryOperator; import org.apache.wayang.core.platform.ChannelDescriptor; import org.apache.wayang.core.platform.ChannelInstance; +import org.apache.wayang.core.platform.CrossPlatformExecutor; +import org.apache.wayang.core.platform.ExecutionResourceTemplate; import org.apache.wayang.core.platform.lineage.ExecutionLineageNode; import org.apache.wayang.core.types.DataSetType; import org.apache.wayang.core.util.JsonSerializable; @@ -97,30 +99,52 @@ public Tuple, Collection> eval final StreamChannel.Instance output = (StreamChannel.Instance) outputs[0]; JdbcPlatformTemplate producerPlatform = (JdbcPlatformTemplate) input.getChannel().getProducer().getPlatform(); + final String sqlQuery = input.getSqlQuery(); final Connection connection = producerPlatform .createDatabaseDescriptor(executor.getConfiguration()) .createJdbcConnection(); - Iterator resultSetIterator = new ResultSetIterator(connection, input.getSqlQuery()); - Spliterator resultSetSpliterator = Spliterators.spliteratorUnknownSize(resultSetIterator, 0); - Stream resultSetStream = StreamSupport.stream(resultSetSpliterator, false); + ResultSetIterator resultSetIterator = new ResultSetIterator(connection, sqlQuery); + boolean success = false; + try { + Spliterator resultSetSpliterator = Spliterators.spliteratorUnknownSize(resultSetIterator, 0); + Stream resultSetStream = StreamSupport.stream(resultSetSpliterator, false) + .onClose(resultSetIterator::close); - output.accept(resultSetStream); + // Terminal operations do not close streams, and StreamChannel disposal only drops its reference. + // Register cleanup for job shutdown: live channels can keep the JavaExecutor referenced after + // downstream failures, so executor disposal alone is insufficient. Closing on channel disposal + // could instead interrupt a lazy successor that still needs the JDBC resources. + CrossPlatformExecutor crossPlatformExecutor = executor.getCrossPlatformExecutor(); + if (crossPlatformExecutor != null) { + crossPlatformExecutor.registerGlobal(new ExecutionResourceTemplate(null) { + @Override + protected void doDispose() { + resultSetIterator.close(); + } + }); + } + output.accept(resultSetStream); - ExecutionLineageNode queryLineageNode = new ExecutionLineageNode(operatorContext); - queryLineageNode.add(LoadProfileEstimators.createFromSpecification( - String.format("wayang.%s.sqltostream.load.query", this.jdbcPlatform.getPlatformId()), - executor.getConfiguration() - )); - queryLineageNode.addPredecessor(input.getLineage()); - ExecutionLineageNode outputLineageNode = new ExecutionLineageNode(operatorContext); - outputLineageNode.add(LoadProfileEstimators.createFromSpecification( - String.format("wayang.%s.sqltostream.load.output", this.jdbcPlatform.getPlatformId()), - executor.getConfiguration() - )); - output.getLineage().addPredecessor(outputLineageNode); - - return queryLineageNode.collectAndMark(); + ExecutionLineageNode queryLineageNode = new ExecutionLineageNode(operatorContext); + queryLineageNode.add(LoadProfileEstimators.createFromSpecification( + String.format("wayang.%s.sqltostream.load.query", this.jdbcPlatform.getPlatformId()), + executor.getConfiguration() + )); + queryLineageNode.addPredecessor(input.getLineage()); + ExecutionLineageNode outputLineageNode = new ExecutionLineageNode(operatorContext); + outputLineageNode.add(LoadProfileEstimators.createFromSpecification( + String.format("wayang.%s.sqltostream.load.output", this.jdbcPlatform.getPlatformId()), + executor.getConfiguration() + )); + output.getLineage().addPredecessor(outputLineageNode); + + Tuple, Collection> result = queryLineageNode.collectAndMark(); + success = true; + return result; + } finally { + if (!success) resultSetIterator.close(); + } } @Override @@ -146,6 +170,10 @@ public Collection getLoadProfileEstimatorConfigurationKeys() { */ public static class ResultSetIterator implements Iterator, AutoCloseable { + private Connection connection; + + private Statement statement; + /** * Keeps around the {@link ResultSet} of the SQL query. */ @@ -159,26 +187,33 @@ public static class ResultSetIterator implements Iterator, AutoCloseable /** * Creates a new instance. * - * @param connection the JDBC connection on which to execute a SQL query + * @param connection the JDBC connection on which to execute a SQL query; ownership is transferred + * to this iterator, including when construction fails * @param sqlQuery the SQL query */ ResultSetIterator(Connection connection, String sqlQuery) { + this.connection = connection; + boolean success = false; try { //connection.setAutoCommit(false); Statement st = connection.createStatement(); + this.statement = st; //st.setFetchSize(100000000); this.resultSet = st.executeQuery(sqlQuery); + this.moveToNext(); + success = true; } catch (SQLException e) { - this.close(); throw new WayangException("Could not execute SQL.", e); + } finally { + if (!success) this.close(); } - this.moveToNext(); } /** * Moves this instance to the next {@link Record}. */ private void moveToNext() { + boolean success = false; try { if (this.resultSet == null || !this.resultSet.next()) { this.next = null; @@ -191,10 +226,11 @@ private void moveToNext() { } this.next = new Record(values); } + success = true; } catch (SQLException e) { - this.next = null; - this.close(); throw new WayangException("Exception while iterating the result set.", e); + } finally { + if (!success) this.close(); } } @@ -212,13 +248,17 @@ public Record next() { @Override public void close() { - if (this.resultSet != null) { + AutoCloseable[] resources = {this.resultSet, this.statement, this.connection}; + this.resultSet = null; + this.statement = null; + this.connection = null; + this.next = null; + for (AutoCloseable resource : resources) { + if (resource == null) continue; try { - this.resultSet.close(); - } catch (Throwable t) { - LogManager.getLogger(this.getClass()).error("Could not close result set.", t); - } finally { - this.resultSet = null; + resource.close(); + } catch (Exception e) { + LogManager.getLogger(this.getClass()).error("Could not close JDBC resource.", e); } } } diff --git a/wayang-platforms/wayang-jdbc-template/src/test/java/org/apache/wayang/jdbc/operators/SqlToStreamOperatorTest.java b/wayang-platforms/wayang-jdbc-template/src/test/java/org/apache/wayang/jdbc/operators/SqlToStreamOperatorTest.java index da3a96dd1..8e430b927 100644 --- a/wayang-platforms/wayang-jdbc-template/src/test/java/org/apache/wayang/jdbc/operators/SqlToStreamOperatorTest.java +++ b/wayang-platforms/wayang-jdbc-template/src/test/java/org/apache/wayang/jdbc/operators/SqlToStreamOperatorTest.java @@ -21,6 +21,7 @@ import org.apache.wayang.basic.data.Record; import org.apache.wayang.core.api.Configuration; import org.apache.wayang.core.api.Job; +import org.apache.wayang.core.api.exception.WayangException; import org.apache.wayang.core.function.PredicateDescriptor; import org.apache.wayang.core.optimizer.OptimizationContext; import org.apache.wayang.core.plan.executionplan.ExecutionTask; @@ -33,20 +34,36 @@ import org.apache.wayang.java.execution.JavaExecutor; import org.apache.wayang.java.platform.JavaPlatform; import org.apache.wayang.jdbc.channels.SqlQueryChannel; +import org.apache.wayang.jdbc.execution.DatabaseDescriptor; +import org.apache.wayang.jdbc.platform.JdbcPlatformTemplate; import org.apache.wayang.jdbc.test.HsqldbFilterOperator; import org.apache.wayang.jdbc.test.HsqldbPlatform; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.InOrder; import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +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.Mockito.doThrow; +import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -169,4 +186,344 @@ void testWithEmptyHsqldb() throws SQLException { assertTrue(output.isEmpty()); } + @Test + void testExhaustionClosesResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + when(jdbc.resultSet.next()).thenReturn(true, true, false); + SqlToStreamOperator.ResultSetIterator iterator = jdbc.iterator(); + + assertEquals(new Record(1), iterator.next()); + jdbc.verifyOpen(); + assertEquals(new Record(1), iterator.next()); + assertFalse(iterator.hasNext()); + jdbc.verifyClosed(); + iterator.close(); + jdbc.verifyClosed(); + } + + @Test + void testEmptyResultsCloseResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + when(jdbc.resultSet.next()).thenReturn(false); + + assertFalse(jdbc.iterator().hasNext()); + jdbc.verifyClosed(); + } + + @Test + void testStatementCreationFailureClosesConnection() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SQLException failure = new SQLException("createStatement"); + when(jdbc.connection.createStatement()).thenThrow(failure); + + assertSame(failure, assertThrows(WayangException.class, jdbc::iterator).getCause()); + verify(jdbc.connection).close(); + verify(jdbc.statement, never()).close(); + verify(jdbc.resultSet, never()).close(); + } + + @Test + void testQueryFailureClosesStatementAndConnection() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SQLException failure = new SQLException("executeQuery"); + when(jdbc.statement.executeQuery("SELECT 1")).thenThrow(failure); + doThrow(new SQLException("close statement")).when(jdbc.statement).close(); + + assertSame(failure, assertThrows(WayangException.class, jdbc::iterator).getCause()); + InOrder order = inOrder(jdbc.statement, jdbc.connection); + order.verify(jdbc.statement).close(); + order.verify(jdbc.connection).close(); + verify(jdbc.resultSet, never()).close(); + } + + @Test + void testUncheckedQueryFailureClosesResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + IllegalStateException failure = new IllegalStateException("driver failure"); + when(jdbc.statement.executeQuery("SELECT 1")).thenThrow(failure); + + assertSame(failure, assertThrows(IllegalStateException.class, jdbc::iterator)); + InOrder order = inOrder(jdbc.statement, jdbc.connection); + order.verify(jdbc.statement).close(); + order.verify(jdbc.connection).close(); + verify(jdbc.resultSet, never()).close(); + } + + @ParameterizedTest + @ValueSource(strings = {"next", "metadata", "columnCount", "row"}) + void testInitialReadFailureClosesResources(String operation) throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SQLException failure = new SQLException(operation); + jdbc.failRead(operation, failure); + + assertSame(failure, assertThrows(WayangException.class, jdbc::iterator).getCause()); + jdbc.verifyClosed(); + } + + @ParameterizedTest + @ValueSource(strings = {"next", "metadata", "columnCount", "row"}) + void testLaterReadFailureClosesResources(String operation) throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SqlToStreamOperator.ResultSetIterator iterator = jdbc.iterator(); + SQLException failure = new SQLException(operation); + jdbc.failRead(operation, failure); + doThrow(new SQLException("close result set")).when(jdbc.resultSet).close(); + + assertSame(failure, assertThrows(WayangException.class, iterator::next).getCause()); + assertFalse(iterator.hasNext()); + jdbc.verifyClosed(); + iterator.close(); + jdbc.verifyClosed(); + } + + @Test + void testUncheckedReadFailureClosesResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SqlToStreamOperator.ResultSetIterator iterator = jdbc.iterator(); + IllegalStateException failure = new IllegalStateException("driver failure"); + when(jdbc.resultSet.getObject(1)).thenThrow(failure); + + assertSame(failure, assertThrows(IllegalStateException.class, iterator::next)); + jdbc.verifyClosed(); + } + + @Test + void testExplicitCloseIsIdempotent() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SqlToStreamOperator.ResultSetIterator iterator = jdbc.iterator(); + assertTrue(iterator.hasNext()); + jdbc.verifyOpen(); + + iterator.close(); + iterator.close(); + + assertFalse(iterator.hasNext()); + jdbc.verifyClosed(); + } + + @Test + void testCloseFailuresDoNotPreventRemainingCleanup() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + SqlToStreamOperator.ResultSetIterator iterator = jdbc.iterator(); + doThrow(new SQLException("close result set")).when(jdbc.resultSet).close(); + doThrow(new IllegalStateException("close statement")).when(jdbc.statement).close(); + doThrow(new SQLException("close connection")).when(jdbc.connection).close(); + + iterator.close(); + iterator.close(); + + jdbc.verifyClosed(); + } + + @Test + void testExplicitStreamClose() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = createJavaExecutor(); + StreamChannel.Instance output = jdbc.evaluate(executor); + Stream stream = output.provideStream(); + assertEquals(new Record(1), stream.findFirst().get()); + jdbc.verifyOpen(); + + stream.close(); + stream.close(); + jdbc.verifyClosed(); + executor.getCrossPlatformExecutor().shutdown(); + jdbc.verifyClosed(); + } + + @ParameterizedTest + @ValueSource(strings = {"findFirst", "limit", "unconsumed", "downstreamFailure"}) + void testJobShutdownClosesUnexhaustedStream(String consumption) throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = createJavaExecutor(); + StreamChannel.Instance output = jdbc.evaluate(executor); + Stream stream = output.provideStream(); + if ("findFirst".equals(consumption)) { + assertEquals(new Record(1), stream.findFirst().get()); + } else if ("limit".equals(consumption)) { + assertEquals(1, stream.limit(1).count()); + } else if ("downstreamFailure".equals(consumption)) { + IllegalArgumentException failure = new IllegalArgumentException("downstream"); + assertSame(failure, assertThrows(IllegalArgumentException.class, + () -> stream.forEach(record -> { + throw failure; + }))); + } + jdbc.verifyOpen(); + output.dispose(); + executor.getCrossPlatformExecutor().shutdown(); + + jdbc.verifyClosed(); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void testJobShutdownClosesStreamWithLiveChannel(boolean downstreamFails) throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = createJavaExecutor(); + CrossPlatformExecutor crossPlatformExecutor = executor.getCrossPlatformExecutor(); + crossPlatformExecutor.registerGlobal(executor); + StreamChannel.Instance output = jdbc.evaluate(executor); + // An unfinished task or lazy successor can still hold its input channel at job shutdown. + output.noteObtainedReference(); + try { + Stream stream = output.provideStream(); + if (downstreamFails) { + IllegalStateException failure = new IllegalStateException("downstream"); + assertSame(failure, assertThrows(IllegalStateException.class, + () -> stream.forEach(record -> { + throw failure; + }))); + doThrow(new SQLException("close result set")).when(jdbc.resultSet).close(); + } else { + assertEquals(new Record(1), stream.findFirst().get()); + } + jdbc.verifyOpen(); + + crossPlatformExecutor.shutdown(); + + // Releasing the job's reference alone cannot dispose an executor with live channels. + assertFalse(executor.isDisposed()); + assertTrue(executor.getNumReferences() > 0); + jdbc.verifyClosed(); + } finally { + output.noteDiscardedReference(true); + } + jdbc.verifyClosed(); + } + + @Test + void testPublishingFailureClosesResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = createJavaExecutor(); + StreamChannel.Instance output = mock(StreamChannel.Instance.class); + IllegalStateException failure = new IllegalStateException("accept stream"); + doThrow(failure).when(output).accept(any(Stream.class)); + + assertSame(failure, assertThrows(IllegalStateException.class, () -> jdbc.evaluate(executor, output))); + jdbc.verifyClosed(); + executor.getCrossPlatformExecutor().shutdown(); + jdbc.verifyClosed(); + } + + @Test + void testChannelDisposalDoesNotCloseLazyDownstreamStream() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + when(jdbc.resultSet.next()).thenReturn(true, true, false); + JavaExecutor executor = createJavaExecutor(); + StreamChannel.Instance output = jdbc.evaluate(executor); + Stream downstream = output.provideStream().map(record -> record); + + output.dispose(); + jdbc.verifyOpen(); + assertEquals(Arrays.asList(new Record(1), new Record(1)), downstream.collect(Collectors.toList())); + jdbc.verifyClosed(); + executor.getCrossPlatformExecutor().shutdown(); + jdbc.verifyClosed(); + } + + @Test + void testLineageFailureClosesPublishedResources() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = createJavaExecutor(); + StreamChannel.Instance output = mock(StreamChannel.Instance.class); + IllegalStateException failure = new IllegalStateException("output lineage"); + when(output.getLineage()).thenThrow(failure); + + assertSame(failure, assertThrows(IllegalStateException.class, () -> jdbc.evaluate(executor, output))); + jdbc.verifyClosed(); + executor.getCrossPlatformExecutor().shutdown(); + jdbc.verifyClosed(); + } + + @Test + void testExplicitCloseWithoutCrossPlatformExecutor() throws SQLException { + JdbcResources jdbc = new JdbcResources(); + JavaExecutor executor = mock(JavaExecutor.class); + when(executor.getConfiguration()).thenReturn(configuration); + StreamChannel.Instance output = jdbc.evaluate(executor); + jdbc.verifyOpen(); + + output.provideStream().close(); + + jdbc.verifyClosed(); + } + + private static class JdbcResources { + private final Connection connection = mock(Connection.class); + private final Statement statement = mock(Statement.class); + private final ResultSet resultSet = mock(ResultSet.class); + private final ResultSetMetaData metadata = mock(ResultSetMetaData.class); + + private JdbcResources() throws SQLException { + when(this.connection.createStatement()).thenReturn(this.statement); + when(this.statement.executeQuery("SELECT 1")).thenReturn(this.resultSet); + when(this.resultSet.next()).thenReturn(true); + when(this.resultSet.getMetaData()).thenReturn(this.metadata); + when(this.metadata.getColumnCount()).thenReturn(1); + when(this.resultSet.getObject(1)).thenReturn(1); + } + + private SqlToStreamOperator.ResultSetIterator iterator() { + return new SqlToStreamOperator.ResultSetIterator(this.connection, "SELECT 1"); + } + + private void failRead(String operation, SQLException failure) throws SQLException { + switch (operation) { + case "next": + when(this.resultSet.next()).thenThrow(failure); + break; + case "metadata": + when(this.resultSet.getMetaData()).thenThrow(failure); + break; + case "columnCount": + when(this.metadata.getColumnCount()).thenThrow(failure); + break; + case "row": + when(this.resultSet.getObject(1)).thenThrow(failure); + break; + default: + throw new IllegalArgumentException(operation); + } + } + + private StreamChannel.Instance evaluate(JavaExecutor executor) { + StreamChannel.Instance output = new StreamChannel(StreamChannel.DESCRIPTOR, null) + .createInstance(executor, null, 0); + this.evaluate(executor, output); + return output; + } + + private void evaluate(JavaExecutor executor, StreamChannel.Instance output) { + JdbcPlatformTemplate platform = mock(JdbcPlatformTemplate.class); + DatabaseDescriptor database = mock(DatabaseDescriptor.class); + when(platform.createDatabaseDescriptor(executor.getConfiguration())).thenReturn(database); + when(database.createJdbcConnection()).thenReturn(this.connection); + ExecutionOperator producer = mock(ExecutionOperator.class); + when(producer.getPlatform()).thenReturn(platform); + SqlQueryChannel channel = new SqlQueryChannel(HsqldbPlatform.getInstance().getSqlQueryChannelDescriptor(), null); + new ExecutionTask(producer, 0, 1).setOutputChannel(0, channel); + SqlQueryChannel.Instance input = channel.createInstance(null, null, 0); + input.setSqlQuery("SELECT 1"); + SqlToStreamOperator operator = new SqlToStreamOperator(HsqldbPlatform.getInstance()); + operator.evaluate(new ChannelInstance[]{input}, new ChannelInstance[]{output}, + executor, createOperatorContext(operator)); + } + + private void verifyOpen() throws SQLException { + verify(this.resultSet, never()).close(); + verify(this.statement, never()).close(); + verify(this.connection, never()).close(); + } + + private void verifyClosed() throws SQLException { + InOrder order = inOrder(this.resultSet, this.statement, this.connection); + order.verify(this.resultSet).close(); + order.verify(this.statement).close(); + order.verify(this.connection).close(); + verify(this.resultSet).close(); + verify(this.statement).close(); + verify(this.connection).close(); + } + } }