From 1ca241f0c0381c6ac65d71ab99e1afd9131ead03 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 11:40:22 +0700 Subject: [PATCH 1/5] perf(plugins): read row streams only as fast as their consumer takes them --- apple/Packages/TableProCore/Package.swift | 2 +- .../DuckDBFetchBudget.swift | 28 -- .../Wire/QdrantScrollPager.swift | 66 +++-- .../Execution/SpannerExecutionTypes.swift | 2 + .../Execution/SpannerExecutor+Reads.swift | 55 ++-- .../Execution/SpannerExecutor.swift | 41 +-- .../Wire/SpannerRESTClient+HTTP.swift | 50 ++-- .../Wire/URLSessionSpannerTransport.swift | 77 ++++- .../TrinoStatementClient.swift | 57 ++-- .../DuckDBBoundedStreamTests.swift | 44 --- .../PluginRowStreamPacingTests.swift | 269 ++++++++++++++++++ .../OracleCoreConnection.swift | 25 +- .../BeancountPluginDriver.swift | 8 +- .../BigQueryPluginDriver+Execution.swift | 13 +- .../BigtablePluginDriver+Execution.swift | 16 +- .../CassandraConnection.swift | 13 +- .../CassandraPlugin.swift | 2 +- .../ClickHouseHTTPChunks.swift | 12 +- .../ClickHousePlugin.swift | 51 +++- .../DuckDBDriverPlugin/DuckDBConnection.swift | 20 +- .../DuckDBDriverPlugin/DuckDBPlugin.swift | 14 +- .../DynamoDBPluginDriver+Execution.swift | 29 +- .../ElasticsearchPluginDriver+Execution.swift | 33 +-- .../ElasticsearchPluginDriver.swift | 2 + .../EtcdDriverPlugin/EtcdPluginDriver.swift | 37 ++- .../FirebasePluginDriver+Execution.swift | 22 +- .../HanaDriverPlugin/HanaPluginDriver.swift | 5 +- .../InfluxDBConnection.swift | 10 +- .../InfluxDBPluginDriver+Stream.swift | 8 +- .../LibSQLPluginDriver.swift | 13 +- .../SQLiteLocalBackend.swift | 6 +- .../MSSQLDriverPlugin/FreeTDSConnection.swift | 66 ++--- .../MSSQLDriverPlugin/MSSQLPlugin.swift | 28 +- .../MemcachedPluginDriver.swift | 5 +- .../MongoDBConnection+SyncHelpers.swift | 43 ++- .../MongoDBConnection.swift | 19 +- .../MongoDBPluginDriver.swift | 11 +- .../MariaDBPluginConnection.swift | 5 +- .../MySQLDriverPlugin/MySQLPluginDriver.swift | 16 +- .../Neo4jDriverPlugin/Neo4jConnection.swift | 4 +- .../Neo4jDriverPlugin/Neo4jPluginDriver.swift | 11 +- .../OracleDriverPlugin/OraclePlugin.swift | 19 +- .../LibPQPluginConnection.swift | 2 +- .../QdrantPluginDriver+Execution.swift | 9 +- .../RedisDriverPlugin/RedisPluginDriver.swift | 15 +- .../SQLiteAgentBackend.swift | 5 +- .../SQLiteExecutionBackend.swift | 19 +- .../SQLiteDriverPlugin/SQLitePlugin.swift | 10 +- .../SnowflakeConnection.swift | 93 +++--- .../SnowflakePluginDriver.swift | 12 +- .../SpannerPluginDriver+Execution.swift | 26 +- .../TableProPluginKit/PluginStreamAbort.swift | 232 ++++++++++++--- .../TeradataDriverPlugin/TeradataPlugin.swift | 6 +- .../TrinoDriverPlugin/TrinoPluginDriver.swift | 20 +- .../TypesenseConnection.swift | 19 +- .../TypesensePluginDriver+Execution.swift | 14 +- .../WeaviatePluginDriver+Execution.swift | 17 +- .../Core/Compare/CompareRowService.swift | 24 +- .../Core/Compare/StreamingRowProvider.swift | 8 +- .../Core/ObjectCopy/ObjectCopyRowCopier.swift | 10 +- .../Core/ObjectCopy/ObjectCopyRunner.swift | 12 +- .../Plugins/QueryResultExportDataSource.swift | 54 ++-- .../Services/Export/BulkReadTimeout.swift | 58 ++++ .../Export/TableTransferService.swift | 12 +- .../Core/Services/BulkReadTimeoutTests.swift | 40 +++ .../Services/Masking/MaskingExportTests.swift | 37 +++ .../probes/check-stream-backpressure.sh | 244 ++++++++++++++++ 67 files changed, 1611 insertions(+), 644 deletions(-) delete mode 100644 apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamPacingTests.swift create mode 100644 apple/TablePro/Core/Services/Export/BulkReadTimeout.swift create mode 100644 apple/TableProTests/Core/Services/BulkReadTimeoutTests.swift create mode 100755 apple/scripts/probes/check-stream-backpressure.sh diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index a59c9a69de..5f6954d8bc 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -556,7 +556,7 @@ let package = Package( ), .testTarget( name: "TableProPluginKitTests", - dependencies: ["TableProPluginKit", "TableProSQLQuoting"], + dependencies: ["TableProPluginKit", "TableProSQLQuoting", "TableProTestSupport"], path: "Tests/TableProPluginKitTests" ), .testTarget( diff --git a/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift b/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift index 322b9aee1d..8153f6b233 100644 --- a/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift +++ b/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift @@ -55,31 +55,3 @@ public enum DuckDBRenderSettings { } } } - -// Hands a row batch to a stream whose buffer is bounded. A buffering policy alone drops what does not fit, so the -// producer keeps the refused batch and offers it again once the consumer has read one. -public enum DuckDBBoundedStream { - public static let bufferedBatches = 64 - - // Returns false when the stream has ended and the producer should stop. - public static func deliver( - _ element: Element, - to continuation: AsyncThrowingStream.Continuation, - pause: () -> Void = { _ = usleep(2_000) } - ) -> Bool { - var pending = element - while true { - switch continuation.yield(pending) { - case .enqueued: - return true - case .dropped(let refused): - pending = refused - pause() - case .terminated: - return false - @unknown default: - return false - } - } - } -} diff --git a/apple/Packages/TableProCore/Sources/TableProQdrantCore/Wire/QdrantScrollPager.swift b/apple/Packages/TableProCore/Sources/TableProQdrantCore/Wire/QdrantScrollPager.swift index 28c0f493e8..32b5413c65 100644 --- a/apple/Packages/TableProCore/Sources/TableProQdrantCore/Wire/QdrantScrollPager.swift +++ b/apple/Packages/TableProCore/Sources/TableProQdrantCore/Wire/QdrantScrollPager.swift @@ -53,36 +53,17 @@ public extension QdrantClient { ).points } - // Without vectors this reads payloads only, which is how an export learns every key before its header. + // Without vectors this reads payloads only, which is how an export learns every key before its header. A page is + // read only when the reader asks for the next one. func pointPages( collection: String, pageSize: Int, withVector: Bool = true ) -> AsyncThrowingStream<[QdrantPoint], Error> { - AsyncThrowingStream { continuation in - let task = Task { - do { - var cursor: Any? - repeat { - try Task.checkCancellation() - let page = try await self.scroll( - collection: collection, - body: Self.scrollBody( - filter: nil, limit: max(pageSize, 1), cursor: cursor, withData: true, withVector: withVector - ) - ) - if !page.points.isEmpty { - continuation.yield(page.points) - } - cursor = page.nextCursor - } while cursor != nil - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } - continuation.onTermination = { @Sendable _ in task.cancel() } - } + let pager = QdrantPointPager( + client: self, collection: collection, pageSize: max(pageSize, 1), withVector: withVector + ) + return AsyncThrowingStream(unfolding: { try await pager.next() }) } static func scrollBody( @@ -106,3 +87,38 @@ public extension QdrantClient { return body } } + +// One reader pulls the pages, so one call at a time reaches the cursor. +private final class QdrantPointPager: @unchecked Sendable { + private let client: QdrantClient + private let collection: String + private let pageSize: Int + private let withVector: Bool + private var cursor: Any? + private var isDone = false + + init(client: QdrantClient, collection: String, pageSize: Int, withVector: Bool) { + self.client = client + self.collection = collection + self.pageSize = pageSize + self.withVector = withVector + } + + func next() async throws -> [QdrantPoint]? { + while !isDone { + try Task.checkCancellation() + let page = try await client.scroll( + collection: collection, + body: QdrantClient.scrollBody( + filter: nil, limit: pageSize, cursor: cursor, withData: true, withVector: withVector + ) + ) + cursor = page.nextCursor + isDone = cursor == nil + if !page.points.isEmpty { + return page.points + } + } + return nil + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift index 77b8aa3ee9..954956a27e 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift @@ -33,6 +33,8 @@ public enum SpannerStreamOutput: Sendable, Equatable { case rows([[SpannerCell]]) } +public typealias SpannerStreamEmit = @Sendable (SpannerStreamOutput) async -> Void + public enum SpannerExecutionError: Error, Sendable, Equatable { case transactionAlreadyOpen case noTransactionOpen diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift index b0d8bf8dff..cdbbe3de62 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift @@ -3,24 +3,29 @@ import Foundation extension SpannerExecutor { func queryStream(_ statement: SpannerPreparedStatement) -> AsyncThrowingStream { makeStream { continuation in - if await self.transactions.isIdle { - try await self.streamOnReadSession(statement, into: continuation) - return - } - try await self.transactions.queue.run { - switch try await self.transactions.claim() { - case .idle: - try await self.streamOnReadSession(statement, into: continuation) - case .open(let context): - try await self.streamInTransaction(statement, context: context, into: continuation) - } - } + try await self.streamQuery(statement) { continuation.yield($0) } } } func readStream(_ statement: SpannerPreparedStatement) -> AsyncThrowingStream { makeStream { continuation in - try await self.streamOnReadSession(statement, into: continuation) + try await self.streamOnReadSession(statement) { continuation.yield($0) } + } + } + + // The response is read on only once `emit` returns, so a caller that waits in it stops the read where it is. + func streamQuery(_ statement: SpannerPreparedStatement, into emit: @escaping SpannerStreamEmit) async throws { + if await transactions.isIdle { + try await streamOnReadSession(statement, into: emit) + return + } + try await transactions.queue.run { + switch try await self.transactions.claim() { + case .idle: + try await self.streamOnReadSession(statement, into: emit) + case .open(let context): + try await self.streamInTransaction(statement, context: context, into: emit) + } } } @@ -45,16 +50,16 @@ extension SpannerExecutor { private func streamOnReadSession( _ statement: SpannerPreparedStatement, - into continuation: AsyncThrowingStream.Continuation + into emit: SpannerStreamEmit ) async throws { let resolved = try await queryParameterTypes(for: statement) var progress = SpannerStreamProgress() do { - try await streamReadAttempt(statement, types: resolved.types, progress: &progress, into: continuation) + try await streamReadAttempt(statement, types: resolved.types, progress: &progress, into: emit) } catch let error as SpannerAPIError where progress.isPristine && resolved.cached && error.isInvalidArgument { await typeCache.evict(statement.sql) let rediscovered = try await queryParameterTypes(for: statement) - try await streamReadAttempt(statement, types: rediscovered.types, progress: &progress, into: continuation) + try await streamReadAttempt(statement, types: rediscovered.types, progress: &progress, into: emit) } } @@ -62,30 +67,30 @@ extension SpannerExecutor { _ statement: SpannerPreparedStatement, types: [SpannerType], progress: inout SpannerStreamProgress, - into continuation: AsyncThrowingStream.Continuation + into emit: SpannerStreamEmit ) async throws { let request = try executeRequest(statement, types: types, transaction: .singleUseStrongReadOnly, seqno: nil) let session = try await sessions.readSessionName() do { - try await forward(try await client.executeStreamingSql(session: session, request), progress: &progress, into: continuation) + try await forward(try await client.executeStreamingSql(session: session, request), progress: &progress, into: emit) } catch let error as SpannerAPIError where error.isSessionNotFound && progress.isPristine { await sessions.discardReadSession(session) let fresh = try await sessions.readSessionName() - try await forward(try await client.executeStreamingSql(session: fresh, request), progress: &progress, into: continuation) + try await forward(try await client.executeStreamingSql(session: fresh, request), progress: &progress, into: emit) } } private func streamInTransaction( _ statement: SpannerPreparedStatement, context: SpannerTransactionContext, - into continuation: AsyncThrowingStream.Continuation + into emit: SpannerStreamEmit ) async throws { let types = try await queryParameterTypes(for: statement).types let request = try executeRequest(statement, types: types, transaction: .id(context.transactionId), seqno: nil) var progress = SpannerStreamProgress() do { let events = try await client.executeStreamingSql(session: context.session, request) - try await forward(events, progress: &progress, into: continuation) + try await forward(events, progress: &progress, into: emit) } catch let error as SpannerAPIError where error.isAborted || error.isSessionNotFound { await transactions.markAborted(context) throw error @@ -95,24 +100,24 @@ extension SpannerExecutor { private func forward( _ events: AsyncThrowingStream, progress: inout SpannerStreamProgress, - into continuation: AsyncThrowingStream.Continuation + into emit: SpannerStreamEmit ) async throws { for try await event in events { switch event { case .metadata(let metadata): progress.fields = metadata.fields progress.headerSent = true - continuation.yield(.header(metadata.fields)) + await emit(.header(metadata.fields)) case .rows(let rows): progress.rowsSent = true - continuation.yield(.rows(SpannerValueDecoder.rows(rows, fields: progress.fields))) + await emit(.rows(SpannerValueDecoder.rows(rows, fields: progress.fields))) case .stats: continue } } if !progress.headerSent { progress.headerSent = true - continuation.yield(.header([])) + await emit(.header([])) } } diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift index b8019f082b..41ff438da2 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift @@ -84,21 +84,24 @@ public final class SpannerExecutor: Sendable { } } - public func stream(_ sql: String, hostParameters: [SpannerCell]?) -> AsyncThrowingStream { - do { - try ensureOpen() - return stream(try prepare(sql, hostParameters: hostParameters)) - } catch { - return AsyncThrowingStream { $0.finish(throwing: error) } - } + // Each output goes to `emit`, and a query's response is read on only once `emit` returns. + public func stream(_ sql: String, hostParameters: [SpannerCell]?, into emit: @escaping SpannerStreamEmit) async throws { + try ensureOpen() + try await stream(prepare(sql, hostParameters: hostParameters), into: emit) } - public func stream(_ statement: SpannerRenderedStatement) -> AsyncThrowingStream { + public func stream(_ statement: SpannerRenderedStatement, into emit: @escaping SpannerStreamEmit) async throws { + try ensureOpen() let prepared = SpannerPreparedStatement(statement) guard case .query = SpannerStatementClassifier.classify(prepared.sql, dialect: dialect) else { - return outcomeStream(statement) + let outcome = try await run(statement) + await emit(.header(outcome.fields)) + if !outcome.rows.isEmpty { + await emit(.rows(outcome.rows)) + } + return } - return queryStream(prepared) + try await streamQuery(prepared, into: emit) } public func read(_ statement: SpannerRenderedStatement) async throws -> [[SpannerCell]] { @@ -201,24 +204,6 @@ public final class SpannerExecutor: Sendable { return try await body(try await sessions.readSessionName()) } } - - private func outcomeStream(_ statement: SpannerRenderedStatement) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in - let task = Task { - do { - let outcome = try await self.run(statement) - continuation.yield(.header(outcome.fields)) - if !outcome.rows.isEmpty { - continuation.yield(.rows(outcome.rows)) - } - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } - continuation.onTermination = { _ in task.cancel() } - } - } } internal struct SpannerPreparedStatement: Sendable { diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift index c3719e4e64..ff1bdfd648 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift @@ -61,30 +61,13 @@ internal extension SpannerRESTClient { return (response, chunks) } + // Pulled: a chunk is read and decoded only when the reader wants an event past the ones already decoded. static func events( from chunks: AsyncThrowingStream, httpStatus: Int ) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in - let producer = Task { - do { - var decoder = SpannerStreamDecoder(httpStatus: httpStatus) - for try await chunk in chunks { - for event in try decoder.consume(chunk) { - continuation.yield(event) - } - } - try Task.checkCancellation() - for event in try decoder.finish() { - continuation.yield(event) - } - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } - continuation.onTermination = { _ in producer.cancel() } - } + let source = SpannerEventSource(chunks: chunks, httpStatus: httpStatus) + return AsyncThrowingStream(unfolding: { try await source.next() }) } func encode(_ value: Value) throws -> Data { @@ -142,6 +125,33 @@ internal extension SpannerRESTClient { } } +// One reader pulls the events, so one call at a time reaches the decoder. +private final class SpannerEventSource: @unchecked Sendable { + private var chunks: AsyncThrowingStream.AsyncIterator + private var decoder: SpannerStreamDecoder + private var pending: [SpannerStreamEvent] = [] + private var isFinished = false + + init(chunks: AsyncThrowingStream, httpStatus: Int) { + self.chunks = chunks.makeAsyncIterator() + decoder = SpannerStreamDecoder(httpStatus: httpStatus) + } + + func next() async throws -> SpannerStreamEvent? { + while pending.isEmpty { + guard !isFinished else { return nil } + if let chunk = try await chunks.next() { + pending = try decoder.consume(chunk) + } else { + try Task.checkCancellation() + isFinished = true + pending = try decoder.finish() + } + } + return pending.removeFirst() + } +} + internal struct SpannerDatabaseResponse: Decodable { let databaseDialect: String? } diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift index 6e12b3a72d..4aec0fb27c 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift @@ -2,6 +2,9 @@ import Foundation public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Sendable { private let session: URLSession + // A streamed read holds one response for as long as its reader takes, and a suspended task still counts toward + // the resource limit, so streams run on a session that keeps the system default. + private let streamSession: URLSession private let requestTimeout: @Sendable () -> TimeInterval private let lock = NSLock() private var isClosed = false @@ -19,6 +22,13 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send delegate: SpannerRedirectRefusingDelegate(), delegateQueue: nil ) + // The session made its own copy of the configuration above. + configuration.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource + self.streamSession = URLSession( + configuration: configuration, + delegate: SpannerRedirectRefusingDelegate(), + delegateQueue: nil + ) self.requestTimeout = requestTimeout } @@ -55,7 +65,7 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send receiver.awaitResponse(continuation) do { try startTask(cancellation: cancellation) { - let task = self.session.dataTask(with: prepared) + let task = self.streamSession.dataTask(with: prepared) task.delegate = receiver return task } @@ -66,7 +76,8 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send } onCancel: { cancellation.cancel() } - return (response, body) + let reader = SpannerResponseBody(body, receiver: receiver, cancellation: cancellation) + return (response, AsyncThrowingStream(unfolding: { try await reader.next() })) } public func close() async { @@ -77,6 +88,7 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send } guard shouldInvalidate else { return } session.finishTasksAndInvalidate() + streamSession.finishTasksAndInvalidate() } private func prepare(_ request: URLRequest) -> URLRequest { @@ -143,12 +155,46 @@ internal final class SpannerRedirectRefusingDelegate: NSObject, URLSessionTaskDe } } +// One reader pulls the body, so one call at a time reaches the iterator. The transfer runs only while that reader +// waits for its next piece, so a reader that stops reading stops the download instead of piling the body up. +private final class SpannerResponseBody: @unchecked Sendable { + private var iterator: AsyncThrowingStream.AsyncIterator + private let receiver: SpannerStreamingTaskDelegate + private let cancellation: SpannerTaskCancellation + + init( + _ body: AsyncThrowingStream, + receiver: SpannerStreamingTaskDelegate, + cancellation: SpannerTaskCancellation + ) { + iterator = body.makeAsyncIterator() + self.receiver = receiver + self.cancellation = cancellation + } + + // A reader that walks away would otherwise leave the transfer suspended, holding the connection. + deinit { + cancellation.cancel() + } + + func next() async throws -> Data? { + receiver.resumeTransfer() + let piece = try await iterator.next() + if piece != nil { + receiver.pieceTaken() + } + return piece + } +} + internal final class SpannerStreamingTaskDelegate: NSObject, URLSessionDataDelegate, @unchecked Sendable { static let maximumChunkSize = 64 * 1_024 private let lock = NSLock() private let body: AsyncThrowingStream.Continuation private var responseContinuation: CheckedContinuation? + private var suspendedTask: URLSessionDataTask? + private var unreadPieces = 0 init(body: AsyncThrowingStream.Continuation) { self.body = body @@ -178,13 +224,25 @@ internal final class SpannerStreamingTaskDelegate: NSObject, URLSessionDataDeleg return .allow } + // Counted and suspended under the lock before the reader sees any piece, so its resume can never come first. func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { + var pieces: [Data] = [] var start = data.startIndex while start < data.endIndex { let end = data.index(start, offsetBy: Self.maximumChunkSize, limitedBy: data.endIndex) ?? data.endIndex - body.yield(Data(data[start.. CheckedContinuation? { lock.withLock { () -> CheckedContinuation? in defer { responseContinuation = nil } diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift index c13edc6ba4..c7bbbb086b 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift @@ -1,11 +1,6 @@ import Foundation import os -public enum TrinoStreamElement: Sendable { - case columns([TrinoColumnDescriptor]) - case rows([[TrinoValue]]) -} - public final class TrinoStatementClient: @unchecked Sendable { private let transport: TrinoTransport private let config: TrinoClientConfig @@ -53,26 +48,18 @@ public final class TrinoStatementClient: @unchecked Sendable { ) } - /// The paging loop runs in an unstructured task, so terminating the stream has to cancel it - /// explicitly. Without that the loop keeps fetching pages nobody reads, and its own - /// `abortIfCancelled` never fires because nothing ever cancels the task it runs in. - public func executeStreamed(_ sql: String) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in - let client = self - let statementTask = Task { - do { - _ = try await client.runStatement( - sql, - onColumns: { continuation.yield(.columns(client.descriptors(from: $0))) }, - onPage: { continuation.yield(.rows($0)) } - ) - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } - continuation.onTermination = { @Sendable _ in statementTask.cancel() } - } + // The next page is asked for only once `onPage` returns, so a caller that waits there holds the query where it is. + // A cancelled caller stops at the next request and sends Trino the DELETE. + public func executeStreamed( + _ sql: String, + onColumns: ([TrinoColumnDescriptor]) async -> Void, + onPage: ([[TrinoValue]]) async -> Void + ) async throws { + _ = try await runStatement( + sql, + onColumns: { await onColumns(self.descriptors(from: $0)) }, + onPage: onPage + ) } /// Stops every statement running on this client, not only the one that started last: the app @@ -93,8 +80,8 @@ public final class TrinoStatementClient: @unchecked Sendable { private func runStatement( _ sql: String, - onColumns: ([TrinoColumn]) -> Void, - onPage: ([[TrinoValue]]) -> Void + onColumns: ([TrinoColumn]) async -> Void, + onPage: ([[TrinoValue]]) async -> Void ) async throws -> StatementOutcome { guard let statementURL = config.statementURL else { throw TrinoError.invalidConfiguration("Invalid \(config.flavor.productName) server URL") @@ -118,8 +105,8 @@ public final class TrinoStatementClient: @unchecked Sendable { _ statement: TrinoRunningStatement, url statementURL: URL, sql: String, - onColumns: ([TrinoColumn]) -> Void, - onPage: ([[TrinoValue]]) -> Void + onColumns: ([TrinoColumn]) async -> Void, + onPage: ([[TrinoValue]]) async -> Void ) async throws -> StatementOutcome { var httpResponse = try await sendWithRetry( makeRequest(method: .post, url: statementURL, headers: initialHeaders(), body: Data(sql.utf8)), @@ -134,10 +121,10 @@ public final class TrinoStatementClient: @unchecked Sendable { var columns = results.columns var columnsEmitted = false if let columns { - onColumns(columns) + await onColumns(columns) columnsEmitted = true } - emitRows(results, columns: columns, onPage: onPage) + await emitRows(results, columns: columns, onPage: onPage) var updateType = results.updateType var updateCount = results.updateCount let queryId = results.id @@ -159,10 +146,10 @@ public final class TrinoStatementClient: @unchecked Sendable { columns = results.columns } if !columnsEmitted, let columns { - onColumns(columns) + await onColumns(columns) columnsEmitted = true } - emitRows(results, columns: columns, onPage: onPage) + await emitRows(results, columns: columns, onPage: onPage) if let type = results.updateType { updateType = type } @@ -175,7 +162,7 @@ public final class TrinoStatementClient: @unchecked Sendable { return StatementOutcome(updateType: updateType, updateCount: updateCount, queryId: queryId) } - private func emitRows(_ results: TrinoQueryResults, columns: [TrinoColumn]?, onPage: ([[TrinoValue]]) -> Void) { + private func emitRows(_ results: TrinoQueryResults, columns: [TrinoColumn]?, onPage: ([[TrinoValue]]) async -> Void) async { guard let data = results.data, let columns, !data.isEmpty else { return } var page: [[TrinoValue]] = [] page.reserveCapacity(data.count) @@ -188,7 +175,7 @@ public final class TrinoStatementClient: @unchecked Sendable { } page.append(decoded) } - onPage(page) + await onPage(page) } private func descriptors(from columns: [TrinoColumn]) -> [TrinoColumnDescriptor] { diff --git a/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift b/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift deleted file mode 100644 index 1aa3be5881..0000000000 --- a/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift +++ /dev/null @@ -1,44 +0,0 @@ -import Foundation -import TableProDuckDBCore -import Testing - -@Test func aSlowConsumerReceivesEveryBatchThroughASmallBuffer() async throws { - let (stream, continuation) = AsyncThrowingStream.makeStream(bufferingPolicy: .bufferingOldest(2)) - Thread.detachNewThread { - for value in 0..<500 { - guard DuckDBBoundedStream.deliver(value, to: continuation, pause: { _ = usleep(100) }) else { break } - } - continuation.finish() - } - - var received: [Int] = [] - for try await value in stream { - received.append(value) - } - #expect(received == Array(0..<500)) -} - -@Test func theProducerWaitsWhileTheBufferIsFull() { - let (stream, continuation) = AsyncThrowingStream.makeStream(bufferingPolicy: .bufferingOldest(2)) - #expect(DuckDBBoundedStream.deliver(1, to: continuation)) - #expect(DuckDBBoundedStream.deliver(2, to: continuation)) - - var pauses = 0 - let delivered = DuckDBBoundedStream.deliver(3, to: continuation) { - pauses += 1 - if pauses == 3 { continuation.finish() } - } - #expect(!delivered) - #expect(pauses == 3) - _ = stream -} - -@Test func theProducerStopsWhenTheStreamIsGone() { - var stream: AsyncThrowingStream? - var held: AsyncThrowingStream.Continuation? - stream = AsyncThrowingStream(bufferingPolicy: .bufferingOldest(1)) { held = $0 } - let continuation = held - #expect(continuation.map { DuckDBBoundedStream.deliver(1, to: $0) } == true) - stream = nil - #expect(continuation.map { DuckDBBoundedStream.deliver(2, to: $0, pause: {}) } == false) -} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamPacingTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamPacingTests.swift new file mode 100644 index 0000000000..66bfd35b86 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamPacingTests.swift @@ -0,0 +1,269 @@ +import Foundation +import TableProPluginKit +import TableProTestSupport +import Testing + +struct PluginRowStreamPacingTests { + private static let header = PluginStreamHeader(columns: ["c"], columnTypeNames: ["TEXT"]) + + private static func batch(_ index: Int, size: Int = 10) -> [PluginRow] { + (0..? = PluginRowStream.make { continuation, abort in + queue.async { + continuation.yield(.header(Self.header)) + for index in 0..<40 { + progress.entering(index) + abort.send(Self.batch(index), to: continuation) + if abort.isAborted { break } + } + continuation.finish() + progress.finish() + } + } + + var iterator = stream?.makeAsyncIterator() + _ = try await iterator?.next() + _ = try await iterator?.next() + try await progress.waitUntilEntered(2) + + iterator = nil + stream = nil + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 2) + } + + @Test("A task producer suspends while the consumer has two batches unread") + func taskProducerWaitsForTheConsumer() async throws { + let progress = ProducerProgress() + var stream: AsyncThrowingStream? = PluginRowStream.make { continuation, abort in + let task = Task { + continuation.yield(.header(Self.header)) + for index in 0..<40 { + progress.entering(index) + await abort.send(Self.batch(index), to: continuation) + if abort.isAborted { break } + } + continuation.finish() + progress.finish() + } + abort.onAbort { task.cancel() } + } + + var iterator = stream?.makeAsyncIterator() + _ = try await iterator?.next() + _ = try await iterator?.next() + try await progress.waitUntilEntered(2) + + iterator = nil + stream = nil + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 2) + } + + @Test("A consumer cancelled between reads stops a waiting producer while the stream is still held") + func cancelledConsumerStopsAWaitingProducer() async throws { + let queue = DispatchQueue(label: "test.stream.pacing.cancel") + let progress = ProducerProgress() + let stream = PluginRowStream.make { continuation, abort in + queue.async { + continuation.yield(.header(Self.header)) + for index in 0..<40 { + progress.entering(index) + abort.send(Self.batch(index), to: continuation) + if abort.isAborted { break } + } + continuation.finish() + progress.finish() + } + } + + let reader = Task { + var iterator = stream.makeAsyncIterator() + _ = try await iterator.next() + _ = try await iterator.next() + try await progress.waitUntilEntered(2) + withUnsafeCurrentTask { $0?.cancel() } + return try await iterator.next() == nil + } + + #expect(try await reader.value) + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 2) + _ = stream + } + + @Test("Every row arrives once and in order through a paced stream") + func pacedStreamDeliversEveryRow() async throws { + let queue = DispatchQueue(label: "test.stream.pacing.order") + let stream = PluginRowStream.make { continuation, abort in + queue.async { + continuation.yield(.header(Self.header)) + for index in 0..<25 { + abort.send(Self.batch(index, size: 3), to: continuation) + } + continuation.yield(.rows(Self.batch(25, size: 1))) + continuation.finish() + } + } + + var cells: [String] = [] + var sawHeader = false + for try await element in stream { + switch element { + case .header: sawHeader = true + case .rows(let rows): cells += rows.map { $0.first?.asText ?? "" } + } + } + let expected = (0..<25).flatMap { index in (0..<3).map { "row_\(index)_\($0)" } } + ["row_25_0"] + #expect(sawHeader) + #expect(cells == expected) + } + + @Test("A producer the consumer never reads reads on once its pause limit passes") + func pauseLimitLetsTheProducerReadOn() async throws { + let queue = DispatchQueue(label: "test.stream.pacing.pause") + let progress = ProducerProgress() + let stream = PluginRowStream.make { continuation, abort in + queue.async { + continuation.yield(.header(Self.header)) + for index in 0..<6 { + progress.entering(index) + abort.send(Self.batch(index), to: continuation, maximumPause: 0.01) + } + continuation.finish() + progress.finish() + } + } + + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 5) + _ = stream + } + + @Test("A task producer reads on once its pause limit passes") + func asyncPauseLimitLetsTheProducerReadOn() async throws { + let progress = ProducerProgress() + let stream = PluginRowStream.make { continuation, abort in + Task { + continuation.yield(.header(Self.header)) + for index in 0..<6 { + progress.entering(index) + await abort.send(Self.batch(index), to: continuation, maximumPause: 0.01) + } + continuation.finish() + progress.finish() + } + } + + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 5) + _ = stream + } + + @Test("A document stream waits for its consumer and keeps each batch's documents with its rows") + func exportStreamPacesAndKeepsDocuments() async throws { + let queue = DispatchQueue(label: "test.stream.pacing.documents") + let progress = ProducerProgress() + var stream: AsyncThrowingStream? = PluginRowStream.makeExport { continuation, abort in + queue.async { + continuation.yield(.header(Self.header)) + for index in 0..<40 { + progress.entering(index) + let rows = Self.batch(index, size: 2) + let documents = rows.map { row in + PluginExportDocument(fields: [PluginExportField( + name: "c", canonicalJSON: "\"\(row[0].asText ?? "")\"", typeName: "string", cellValue: row[0] + )]) + } + abort.send(rows, documents: documents, to: continuation) + if abort.isAborted { break } + } + continuation.finish() + progress.finish() + } + } + + var iterator = stream?.makeAsyncIterator() + _ = try await iterator?.next() + let first = try await iterator?.next() + guard case .rows(let rows, let documents)? = first else { + Issue.record("expected rows") + return + } + #expect(rows.count == 2) + #expect(documents?.count == 2) + try await progress.waitUntilEntered(2) + + iterator = nil + stream = nil + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 2) + } + + @Test("A batch a bounded buffer drops is not counted against the consumer", .timeLimit(.minutes(1))) + func droppedBatchDoesNotParkTheProducer() async throws { + let queue = DispatchQueue(label: "test.stream.pacing.dropped") + let progress = ProducerProgress() + let stream = PluginRowStream.make(bufferingPolicy: .bufferingOldest(1)) { continuation, abort in + queue.async { + for index in 0..<6 { + progress.entering(index) + abort.send(Self.batch(index), to: continuation) + } + continuation.finish() + progress.finish() + } + } + + var batches = 0 + for try await element in stream { + if case .rows = element { batches += 1 } + } + try await progress.waitUntilFinished() + #expect(progress.highestEntered == 5) + #expect(batches >= 1) + } + + @Test("An abort made outside a paced stream never waits") + func unpacedAbortNeverWaits() { + let (stream, continuation) = AsyncThrowingStream.makeStream() + let abort = PluginStreamAbort() + for index in 0..<10 { + abort.send(Self.batch(index), to: continuation) + } + continuation.finish() + _ = stream + } +} + +private final class ProducerProgress: @unchecked Sendable { + private let lock = NSLock() + private var entered = -1 + private var finished = false + + var highestEntered: Int { + lock.withLock { entered } + } + + func entering(_ index: Int) { + lock.withLock { entered = max(entered, index) } + } + + func finish() { + lock.withLock { finished = true } + } + + func waitUntilEntered(_ index: Int) async throws { + try await waitUntil { self.lock.withLock { self.entered >= index } } + } + + func waitUntilFinished() async throws { + try await waitUntil { self.lock.withLock { self.finished } } + } +} diff --git a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift index 15758e58f6..fc53cd4a15 100644 --- a/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift +++ b/apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift @@ -1004,6 +1004,16 @@ public final class OracleCoreConnection: @unchecked Sendable { _ query: String, batching: OracleRowBatcher, continuation: AsyncThrowingStream.Continuation + ) async throws { + try await streamQuery(query, batching: batching, onElement: { continuation.yield($0) }) + continuation.finish() + } + + // The next row is read only once `onElement` returns, so a caller that waits in it holds the statement open. + public func streamQuery( + _ query: String, + batching: OracleRowBatcher, + onElement: @escaping @Sendable (OracleStreamElement) async -> Void ) async throws { let role = OracleTransactionRole(of: query) await queryGate.acquire() @@ -1022,13 +1032,12 @@ public final class OracleCoreConnection: @unchecked Sendable { regions: regions, batching: batching, on: connection, - continuation: continuation + onElement: onElement ) } await recordSuccess(of: role, on: admitted.session) forgetSessionZonesIfChanged(by: query) await queryGate.release() - continuation.finish() } catch { let mapped = mapExecutionError(error) forgetSessionZonesIfChanged(by: query) @@ -1045,33 +1054,33 @@ public final class OracleCoreConnection: @unchecked Sendable { regions: OracleTimeZoneRegions, batching: OracleRowBatcher, on connection: OracleNIO.OracleConnection, - continuation: AsyncThrowingStream.Continuation + onElement: @escaping @Sendable (OracleStreamElement) async -> Void ) async throws { let statement = OracleStatement(unsafeSQL: query) let stream = try await connection.execute(statement, options: options, logger: nioLogger) let columns = stream.columns.map(OracleResultColumn.init) var decoder = CellDecoder(columns: columns, zones: zones, regions: regions) - continuation.yield(.header(columns: columns.map(\.descriptor))) + await onElement(.header(columns: columns.map(\.descriptor))) var batcher = batching do { for try await row in stream { try Task.checkCancellation() if let batch = batcher.append(decoder.decode(row)) { - continuation.yield(.rows(batch)) + await onElement(.rows(batch)) } } } catch { // The rows read before a failure still reach the reader ahead of the error, as they did one by one. - if let batch = batcher.remainder() { continuation.yield(.rows(batch)) } + if let batch = batcher.remainder() { await onElement(.rows(batch)) } throw error } - if let batch = batcher.remainder() { continuation.yield(.rows(batch)) } + if let batch = batcher.remainder() { await onElement(.rows(batch)) } await warnOnce(about: decoder.unsupportedTypeNames) guard columns.isEmpty else { return } - continuation.yield(.affectedRows(try await stream.affectedRows)) + await onElement(.affectedRows(try await stream.affectedRows)) } // MARK: - Session Time Zones diff --git a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift index 661f20f618..20e586fa24 100644 --- a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift +++ b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -451,10 +451,11 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in - Task { + PluginRowStream.make { continuation, abort in + let task = Task { do { - let result = try await perform { [self] () -> PluginQueryResult in + try Task.checkCancellation() + let result = try await self.perform { [self] () -> PluginQueryResult in if let bql = Self.extractBQLQuery(from: query) { return try executeBQL(query: bql) } @@ -473,6 +474,7 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } + abort.onAbort { task.cancel() } } } diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift index 10e7bb644d..3552ff05f5 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift @@ -83,19 +83,17 @@ extension BigQueryPluginDriver { } func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let streamTask = Task { do { try await self.runningStatements.run { - try await self.performStreamRows(query: query, continuation: continuation) + try await self.performStreamRows(query: query, abort: abort, continuation: continuation) } } catch { continuation.finish(throwing: BigQueryError.wrap(error)) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } @@ -227,6 +225,7 @@ extension BigQueryPluginDriver { private func performStreamRows( query: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let conn = try requireConnection() @@ -260,7 +259,7 @@ extension BigQueryPluginDriver { let firstRows = BigQueryTypeMapper.flattenRows(from: firstPage, schema: schema) if !firstRows.isEmpty { - continuation.yield(.rows(firstRows)) + await abort.send(firstRows, to: continuation) } var pageToken = firstPage.pageToken @@ -273,7 +272,7 @@ extension BigQueryPluginDriver { ) let nextRows = BigQueryTypeMapper.flattenRows(from: nextPage, schema: schema) if !nextRows.isEmpty { - continuation.yield(.rows(nextRows)) + await abort.send(nextRows, to: continuation) } pageToken = nextPage.pageToken } diff --git a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift index e362dfd30d..b140a05cb9 100644 --- a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift +++ b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift @@ -149,11 +149,11 @@ extension BigtablePluginDriver { // MARK: - Export func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { if case .browse(let spec)? = try? BigtableStatement.parse(query), spec.limit == 0 { - try await self.export(spec, continuation: continuation) + try await self.export(spec, abort: abort, continuation: continuation) } else { let result = try await self.execute(query: query) var header = PluginStreamHeader( @@ -168,12 +168,16 @@ extension BigtablePluginDriver { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } - /// The columns come from the first page; a column only later rows hold goes into `__extra__`. - private func export(_ spec: BigtableBrowseSpec, continuation: AsyncThrowingStream.Continuation) async throws { + // The columns come from the first page; a column only later rows hold goes into `__extra__`. + private func export( + _ spec: BigtableBrowseSpec, + abort: PluginStreamAbort, + continuation: AsyncThrowingStream.Continuation + ) async throws { let client = try requireClient() var header: [String]? var after: Data? @@ -190,7 +194,7 @@ extension BigtablePluginDriver { ))) } if !rows.isEmpty { - continuation.yield(.rows(page.rows(alignedTo: columns).map { $0.map(Self.cell) })) + await abort.send(page.rows(alignedTo: columns).map { $0.map(Self.cell) }, to: continuation) } after = rows.count == Self.exportPageSize ? rows.last?.key : nil } while after != nil diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift index bbc86f6312..afb86905fd 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift @@ -955,10 +955,8 @@ actor CassandraConnectionActor { return "Unknown error" } - /// The whole loop is one non-suspending actor job holding the cooperative-pool thread, and - /// cass_future_wait blocks it, so nothing outside can interrupt a page. `abort` is the only - /// way the consumer's departure reaches this body, and it is read between pages and between - /// rows rather than mid-request. + // One non-suspending actor job, blocked in cass_future_wait and in `abort.send`, so the consumer's departure + // reaches it only through `abort`, read between pages and between rows rather than mid-request. func streamQuery( _ cql: String, consistency: CassandraConsistency?, @@ -1036,6 +1034,7 @@ actor CassandraConnectionActor { let colCount = cass_result_column_count(result) let iterator = cass_iterator_from_result(result) + var page: [PluginRow] = [] if let iterator { var rowsThisPage = 0 @@ -1047,7 +1046,7 @@ actor CassandraConnectionActor { } let row = cass_iterator_get_row(iterator) guard let row else { continue } - continuation.yield(.rows([Self.decodeRow(row, columnCount: colCount)])) + page.append(Self.decodeRow(row, columnCount: colCount)) } cass_iterator_free(iterator) } @@ -1060,6 +1059,10 @@ actor CassandraConnectionActor { cass_result_free(result) + if !page.isEmpty, !aborted { + abort.send(page, to: continuation) + } + if !hasMore || aborted { break } } diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index 64b527cbf6..e36c9bd069 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -492,7 +492,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen continuation.finish(throwing: error) } } - _ = streamTask + abort.onAbort { streamTask.cancel() } } } diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHouseHTTPChunks.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHouseHTTPChunks.swift index 704497b82a..c2e1f90f3c 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHouseHTTPChunks.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHouseHTTPChunks.swift @@ -89,14 +89,14 @@ nonisolated internal final class ClickHouseHTTPChunks: NSObject, URLSessionDataD } guard !isFailure else { return } - continuation.yield(data) - let shouldSuspend = lock.withLock { () -> Bool in - guard !isTransferSuspended else { return false } + // Suspended before the reader sees the chunk: a reader done with a short chunk first would find nothing to + // resume, and the suspend after it would stop the transfer for good. + lock.withLock { + guard !isTransferSuspended else { return } isTransferSuspended = true - return true + dataTask.suspend() } - guard shouldSuspend else { return } - dataTask.suspend() + continuation.yield(data) } internal func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift index 5166531fd1..0202c3840a 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift @@ -188,7 +188,12 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked private var _serverProfile = ClickHouseServerProfile.bootstrap let lock = NSLock() - var session: URLSession? + var session: URLSession? { + didSet { replaceStreamingSession() } + } + // A streamed read holds one response for as long as its consumer takes, and a suspended task still counts toward + // the resource limit, so streams run on a copy of `session` that keeps the system default. + private var streamingSession: URLSession? var currentTask: URLSessionDataTask? var _currentDatabase: String var _lastQueryId: String? @@ -333,6 +338,16 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked lock.unlock() } + // Called from `session`'s didSet, so it runs under whatever lock the write holds. + private func replaceStreamingSession() { + streamingSession?.invalidateAndCancel() + streamingSession = session.map { base in + let configuration = base.configuration + configuration.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource + return URLSession(configuration: configuration, delegate: base.delegate, delegateQueue: nil) + } + } + func ping() async throws { _ = try await execute(query: "SELECT 1") } @@ -588,6 +603,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked query: query, rowCap: rowCap, isOutputCut: isOutputCut, + abort: abort, continuation: continuation ) } catch { @@ -612,10 +628,11 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked query: String, rowCap: Int, isOutputCut: OSAllocatedUnfairLock, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let queryId = UUID().uuidString - let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId) + let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId, paced: false) var request = try buildRequest( query: query, database: database, @@ -630,6 +647,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked session: session, batchSize: min(5_000, rowCap + 1), boundedOutputCut: isOutputCut, + abort: abort, continuation: continuation ) } @@ -649,6 +667,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked session: URLSession, batchSize: Int, boundedOutputCut: OSAllocatedUnfairLock? = nil, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { var decoder = ClickHouseTabSeparatedRowDecoder() @@ -696,7 +715,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } batch.append(contentsOf: rows) guard batch.count >= batchSize else { return } - continuation.yield(.rows(batch)) + await abort.send(batch, to: continuation, maximumPause: Self.unreadResponsePause) batch.removeAll(keepingCapacity: true) } } catch is OtherFormatBodyFull {} @@ -736,6 +755,9 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked /// value cut, so reading stops at the first chunk over it. private static let otherFormatByteCap = 1_048_576 + // The server ends a response it could not send for http_send_timeout, 30 s by default. + private static let unreadResponsePause: TimeInterval = 10 + private struct OtherFormatBodyFull: Error {} func streamRows(query: String) -> AsyncThrowingStream { @@ -747,27 +769,31 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } private func stream(_ query: String, readOnly: Bool) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, readOnly: readOnly, continuation: continuation) + try await self.performStreamRows( + query: query, + readOnly: readOnly, + abort: abort, + continuation: continuation + ) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } private func performStreamRows( query: String, readOnly: Bool, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let queryId = UUID().uuidString - let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId) + let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId, paced: true) var request = try buildRequest( query: query, database: database, @@ -783,6 +809,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked request: request, session: session, batchSize: 5_000, + abort: abort, continuation: continuation ) } catch { @@ -791,11 +818,13 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } } + // A capped grid read finishes as fast as it arrives, so only the paced stream leaves the one-hour session cap. private func streamingContext( - queryId: String + queryId: String, + paced: Bool ) throws -> (session: URLSession, database: String, executionTimeLimit: Int?) { try lock.withLock { - guard let session = self.session else { throw ClickHouseError.notConnected } + guard let session = paced ? self.streamingSession : self.session else { throw ClickHouseError.notConnected } _lastQueryId = queryId return (session, _currentDatabase, acceptedExecutionTimeLimit) } diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift index 321389ab22..1792bb3f8d 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift @@ -206,7 +206,8 @@ actor DuckDBConnectionActor { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { try ensureOpen() guard let conn = connection else { @@ -216,7 +217,7 @@ actor DuckDBConnectionActor { noteActivity(query) var opened = try openResult(query: query, parameters: [], connection: conn, unread: .abandon) defer { duckdb_destroy_result(&opened.result) } - try streamResultRows(opened, continuation: continuation) + try streamResultRows(opened, continuation: continuation, abort: abort) } // The state check, the BEGIN, the statement and the ROLLBACK share one actor turn, so nothing lands in between. @@ -233,13 +234,14 @@ actor DuckDBConnectionActor { func streamReadOnly( _ query: String, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { let conn = try readOnlyConnection() try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState()) { var opened = try openResult(query: query, parameters: [], connection: conn, unread: .abandon) defer { duckdb_destroy_result(&opened.result) } - try streamResultRows(opened, continuation: continuation) + try streamResultRows(opened, continuation: continuation, abort: abort) } } @@ -683,7 +685,8 @@ actor DuckDBConnectionActor { private func streamResultRows( _ opened: OpenedResult, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { continuation.yield(.header(PluginStreamHeader( columns: opened.schema.names, @@ -693,8 +696,11 @@ actor DuckDBConnectionActor { var cancelled = false try readRows(of: opened) { batch in - cancelled = Task.isCancelled - || !DuckDBBoundedStream.deliver(PluginStreamElement.rows(batch), to: continuation) + if !Task.isCancelled, !abort.isAborted { + // Blocks the actor's thread: an await here would let another call reach the connection mid-result. + abort.send(batch, to: continuation) + } + cancelled = Task.isCancelled || abort.isAborted return !cancelled } if cancelled { diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift index 19da1b5586..ccc5fb18d7 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift @@ -760,28 +760,30 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func streamRows(query: String) -> AsyncThrowingStream { let actor = connectionActor - return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(DuckDBBoundedStream.bufferedBatches)) { continuation in - Task { + return PluginRowStream.make { continuation, abort in + let task = Task { do { - try await actor.streamQuery(query, continuation: continuation) + try await actor.streamQuery(query, continuation: continuation, abort: abort) } catch { continuation.finish(throwing: error) } } + abort.onAbort { task.cancel() } } } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { guard context.readOnly, context.origin == .external else { return streamRows(query: query) } let actor = connectionActor - return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(DuckDBBoundedStream.bufferedBatches)) { continuation in - Task { + return PluginRowStream.make { continuation, abort in + let task = Task { do { - try await actor.streamReadOnly(query, continuation: continuation) + try await actor.streamReadOnly(query, continuation: continuation, abort: abort) } catch { continuation.finish(throwing: error) } } + abort.onAbort { task.cancel() } } } diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift index 72389cdf12..834ea37b91 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift @@ -340,32 +340,30 @@ extension DynamoDBPluginDriver { // MARK: - Streaming - /// Streams every item a read returns, for an export, a copy or a compare. - /// - /// A stream states its columns once, before its first row, and a DynamoDB item may carry an - /// attribute no earlier item had. The items are therefore written to a temporary file while the - /// union of their attributes is collected, and the rows are sent once the read has finished, so - /// an attribute first seen on the last page still gets its column. + // A stream states its columns before its first row, and an item may carry an attribute no earlier item had, so + // the items go to a temporary file while their attributes are collected and the rows follow once the read ends. + // The rows are read back from the file only as fast as the consumer takes them. func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { try await self.run { session in - try await self.spool(query, session: session, continuation: continuation) + try await self.spool(query, session: session, continuation: continuation, abort: abort) } continuation.finish() } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { _ in task.cancel() } + abort.onAbort { task.cancel() } } } private func spool( _ text: String, session: Session, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) async throws { let statement = try DynamoDBStatement.parse(text) let source: SpoolSource @@ -415,8 +413,9 @@ extension DynamoDBPluginDriver { } ?? sorted.count let slice = source.window.offset < end ? Array(sorted[source.window.offset.. AsyncThrowingStream { guard let index = ElasticsearchOperations.decodeExport(query) else { return defaultStreamRows(query: query) } - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return PluginRowStream.make { continuation, abort in let task = Task { do { - try await self.streamIndex(index, continuation: continuation) + try await self.streamIndex(index, abort: abort, continuation: continuation) continuation.finish() } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } - /// A point-in-time pins the index for the whole export, so a document written while it runs - /// cannot shift the sort and be exported twice or skipped. `search_after` walks it, which is the - /// only way past the 10,000 `max_result_window` ceiling. + // A point-in-time pins the index for the whole export, so a document written meanwhile cannot shift the sort and + // be exported twice or skipped. `search_after` walks it, the only way past the 10,000 `max_result_window` ceiling. private func streamIndex( _ index: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let conn = connection else { throw ElasticsearchError.notConnected } @@ -100,9 +98,8 @@ extension ElasticsearchPluginDriver { let hits = extractHits(response) guard !hits.isEmpty else { break } - /// The columns are settled once, from the mapping plus the first batch, because a - /// stream has one header and a later batch holding a field the first did not must not - /// widen it. + // The columns are settled once, from the mapping and the first batch: a stream has one header, so a later + // batch holding a new field must not widen it. if columns == nil { let resolved = ElasticsearchMappingFlattener.columns(forHits: hits, mappingColumns: mappingColumns) columns = resolved @@ -113,9 +110,13 @@ extension ElasticsearchPluginDriver { ))) } if let resolved = columns { - continuation.yield(.rows(ElasticsearchMappingFlattener.rows( - forHits: hits, columns: resolved, nestedParents: nestedParents, length: .whole - ))) + await abort.send( + ElasticsearchMappingFlattener.rows( + forHits: hits, columns: resolved, nestedParents: nestedParents, length: .whole + ), + to: continuation, + maximumPause: Self.unreadPagePause + ) } searchAfter = hits.last?["sort"] as? [Any] diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift index bb48629513..96f0b7b8d5 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift @@ -19,6 +19,8 @@ internal final class ElasticsearchPluginDriver: PluginDatabaseDriver, @unchecked static let maxResultWindow = 10_000 static let deepPageBatchSize = 1_000 static let pitKeepAlive = "1m" + // The point in time expires `pitKeepAlive` after the last page that asked for it. + static let unreadPagePause: TimeInterval = 20 var connection: ElasticsearchConnection? { lock.withLock { _connection } } diff --git a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift index 9544d1e405..5c972b8bd6 100644 --- a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift +++ b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift @@ -185,24 +185,23 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, context: context, continuation: continuation) + try await self.performStreamRows(query: query, context: context, abort: abort, continuation: continuation) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } private func performStreamRows( query: String, context: PluginStatementContext, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let client = httpClient else { @@ -217,6 +216,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { sortAscending: parsed.sortAscending, filter: parsed.filter, client: client, + abort: abort, continuation: continuation ) return @@ -239,6 +239,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { sortAscending: Bool, filter: EtcdRowFilter, client: EtcdHttpClient, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let matcher = try filter.matcher() @@ -267,20 +268,32 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } continuation.yield(.header(header)) + // The scan hands each batch over synchronously, so a batch goes out just before the next is read, where the + // read can wait for the consumer. + var unsent: [PluginRow] = [] do { - try await scanKeyValues(in: range, client: client) { kvs in - let rows = kvs.map(Self.filterRow).filter(matcher.matches).map(\.cells) - if !rows.isEmpty { - continuation.yield(.rows(rows)) + try await scanKeyValues( + in: range, + client: client, + beforeNextBatch: { + guard !unsent.isEmpty else { return } + await abort.send(unsent, to: continuation) + unsent = [] + }, + body: { kvs in + unsent = kvs.map(Self.filterRow).filter(matcher.matches).map(\.cells) + return true } - return true - } + ) } catch let error where error.isEtcdRevisionCompacted { // Rows already went out, so the read cannot start over at a newer revision without repeating them. throw EtcdError.serverError(String( localized: "etcd compacted the revision this export started from, so it stopped part way. Run it again." )) } + if !unsent.isEmpty { + continuation.yield(.rows(unsent)) + } continuation.finish() } @@ -1001,6 +1014,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func scanKeyValues( in range: EtcdKeyRange, client: EtcdHttpClient, + beforeNextBatch: () async -> Void = {}, body: ([EtcdKeyValue]) throws -> Bool ) async throws { try await EtcdRangeScan.scan( @@ -1009,6 +1023,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { maxItems: Self.maxKeys, key: { $0.key }, fetch: { range, limit, revision in + await beforeNextBatch() var req = EtcdRangeRequest(key: range.key, rangeEnd: range.rangeEnd, limit: limit) req.revision = revision let response = try await client.rangeRequest(req) diff --git a/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift b/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift index f60e627413..102daad44e 100644 --- a/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift +++ b/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift @@ -234,11 +234,11 @@ extension FirebasePluginDriver { // MARK: - Export func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { if case .browse(let spec)? = try? FirebaseStatement.parse(query), spec.limit == 0 { - try await self.export(spec, continuation: continuation) + try await self.export(spec, abort: abort, continuation: continuation) } else { let result = try await self.execute(query: query) continuation.yield(.header(PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames, @@ -250,11 +250,15 @@ extension FirebasePluginDriver { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } - private func export(_ spec: FirebaseBrowseSpec, continuation: AsyncThrowingStream.Continuation) async throws { + private func export( + _ spec: FirebaseBrowseSpec, + abort: PluginStreamAbort, + continuation: AsyncThrowingStream.Continuation + ) async throws { var header: [String]? var types: [String] = [] try await forEachExportPage(spec) { columns, columnTypes, rows in @@ -264,7 +268,7 @@ extension FirebasePluginDriver { continuation.yield(.header(PluginStreamHeader(columns: header ?? [], columnTypeNames: types, estimatedRowCount: nil))) } let fixed = header ?? [] - continuation.yield(.rows(rows.map { row in Self.align(row, columns: columns, to: fixed) })) + await abort.send(rows.map { row in Self.align(row, columns: columns, to: fixed) }, to: continuation) } if header == nil { continuation.yield(.header(PluginStreamHeader(columns: [Self.extraColumn], columnTypeNames: ["map"], estimatedRowCount: nil))) @@ -282,7 +286,7 @@ extension FirebasePluginDriver { private func forEachExportPage( _ spec: FirebaseBrowseSpec, - page: ([String], [String], [[PluginCellValue]]) throws -> Void + page: ([String], [String], [[PluginCellValue]]) async throws -> Void ) async throws { switch variant { case .firestore: @@ -309,13 +313,13 @@ extension FirebasePluginDriver { operation: String(format: String(localized: "export %@"), spec.table)) guard !documents.isEmpty else { return } let rows = FirestoreRowSet(documents: documents, target: target) - try page(rows.columns, rows.columnTypes, rows.rows.map { $0.map(Self.cell) }) + try await page(rows.columns, rows.columnTypes, rows.rows.map { $0.map(Self.cell) }) cursor = documents.count == Self.exportPageSize ? documents.last?.path : nil } while cursor != nil case .realtimeDatabase: guard spec.filters.isEmpty else { let result = try await browse(spec) - try page(result.columns, result.columnTypeNames, result.rows) + try await page(result.columns, result.columnTypeNames, result.rows) return } let client = try requireRealtimeDatabase() @@ -330,7 +334,7 @@ extension FirebasePluginDriver { let children = try await client.children(node, plan: plan) guard !children.isEmpty else { return } let rows = RealtimeDatabaseRowSet(children: children, nodePath: node) - try page(rows.columns, rows.columnTypes, rows.rows.map { $0.map(PluginCellValue.fromOptional) }) + try await page(rows.columns, rows.columnTypes, rows.rows.map { $0.map(PluginCellValue.fromOptional) }) lastKey = children.count == Self.exportPageSize ? children.last?.key : nil } while lastKey != nil } diff --git a/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift b/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift index 571d6669e2..f56d1e0d5a 100644 --- a/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift +++ b/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift @@ -180,7 +180,7 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let driver = self let producer = Task { do { - try await driver.produce(sql, into: continuation, report: report) + try await driver.produce(sql, into: continuation, abort: abort, report: report) continuation.finish() } catch { continuation.finish(throwing: error) @@ -195,6 +195,7 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func produce( _ sql: String, into continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort, report: HanaStreamReport ) async throws { if HanaExplainStatement.explainedStatement(in: sql) != nil { @@ -223,7 +224,7 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { }, onBatch: { batch in report.record(truncatedLargeObjects: batch.truncatedLobCount) - continuation.yield(.rows(HanaResultMapping.pluginRows(batch.rows))) + abort.send(HanaResultMapping.pluginRows(batch.rows), to: continuation) } ) } catch let failure as HanaBridgeFailure { diff --git a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBConnection.swift b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBConnection.swift index 9e670008b7..673794c1e0 100644 --- a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBConnection.swift +++ b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBConnection.swift @@ -26,6 +26,9 @@ internal final class InfluxDBConnection: @unchecked Sendable { private let lock = NSLock() private var session: URLSession? + // A streamed read holds one response for as long as its consumer takes, and a suspended task still counts toward + // the resource limit, so streams run on a session that keeps the system default. + private var streamSession: URLSession? private var inFlight: [ObjectIdentifier: PluginURLSessionTaskBox] = [:] private let queryTimeout: HttpQueryTimeoutBox @@ -44,6 +47,9 @@ internal final class InfluxDBConnection: @unchecked Sendable { configuration.timeoutIntervalForResource = HttpQueryTimeout.sessionResourceTimeout let delegate = skipTLSVerify ? InfluxDBTrustingDelegate() : PluginRedirectRefusingDelegate() session = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) + // The session made its own copy of the configuration above. + configuration.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource + streamSession = URLSession(configuration: configuration, delegate: delegate, delegateQueue: nil) } func close() { @@ -51,6 +57,8 @@ internal final class InfluxDBConnection: @unchecked Sendable { lock.withLock { session?.invalidateAndCancel() session = nil + streamSession?.invalidateAndCancel() + streamSession = nil } } @@ -115,7 +123,7 @@ internal final class InfluxDBConnection: @unchecked Sendable { lock.withLock { inFlight[key] = taskBox } defer { lock.withLock { _ = inFlight.removeValue(forKey: key) } } // Made under the lock `close()` invalidates with, as in `perform`. - guard let task = lock.withLock({ session?.dataTask(with: urlRequest) }) else { + guard let task = lock.withLock({ streamSession?.dataTask(with: urlRequest) }) else { throw InfluxError.notConnected } let chunks = InfluxDBHTTPChunks(task: task) diff --git a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift index 9bf9af67a4..7d741c6122 100644 --- a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift +++ b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift @@ -61,24 +61,24 @@ extension InfluxDBPluginDriver { schema: schema ) var headerSent = false - func send(_ batches: [InfluxResultTable]) { + func send(_ batches: [InfluxResultTable]) async { for batch in batches { if !headerSent { headerSent = true continuation.yield(.header(PluginStreamHeader(columns: batch.columns, columnTypeNames: batch.typeNames))) } if !batch.rows.isEmpty { - continuation.yield(.rows(Self.cells(batch.rows))) + await abort.send(Self.cells(batch.rows), to: continuation) } } } try await connection.stream(request) { chunk in guard !abort.isAborted else { throw CancellationError() } let batches = try decoder.consume(chunk) - send(batches) + await send(batches) } let rest = try decoder.finish() - send(rest) + await send(rest) } // PluginKit's default stream, which the override above replaces for every statement. diff --git a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift index c60cbb0d99..84cf92572f 100644 --- a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift +++ b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift @@ -283,24 +283,23 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // MARK: - Streaming func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, continuation: continuation) + try await self.performStreamRows(query: query, continuation: continuation, abort: abort) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } private func performStreamRows( query: String, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) async throws { switch getBackend() { case .remote(let client): @@ -323,7 +322,7 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish() case .local(let localBackend): - try await localBackend.streamQuery(query, continuation: continuation) + try await localBackend.streamQuery(query, continuation: continuation, abort: abort) case nil: throw LibSQLError.notConnected } diff --git a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift index b0d9e348e9..c9222e8bbd 100644 --- a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift +++ b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift @@ -151,7 +151,8 @@ actor SQLiteLocalBackend { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { guard let db else { throw LibSQLError.notConnected @@ -188,7 +189,8 @@ actor SQLiteLocalBackend { batch.append(rowValues(of: statement, count: columnCount)) if batch.count >= batchSize { - continuation.yield(.rows(batch)) + // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. + abort.send(batch, to: continuation) batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) diff --git a/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift b/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift index c2a30356ce..1af6220e4a 100644 --- a/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift +++ b/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift @@ -685,38 +685,40 @@ nonisolated final class FreeTDSConnection: @unchecked Sendable { return MSSQLRequestEnding(sessionAnswer: answer.errors.isEmpty ? answer.resultSets.first : nil) } - /// `isAborted` is how a consumer that stopped reaches this loop. It is a closure rather than a - /// PluginKit type because this file is compiled into the iOS app too and may not import - /// PluginKit. It is polled instead of `Task.isCancelled`, which is always false here: the body - /// runs inside a bare `queue.async` with no task context. A consumer that stops ends the request - /// the way a capped read does. - /// - /// Only the request's first result set is streamed. A later one is read past, so the statements behind it still - /// run, and a server error anywhere in the request finishes the stream with that error. func streamQuery( _ query: String, isAborted: (@Sendable () -> Bool)? = nil, continuation: AsyncThrowingStream.Continuation + ) async throws { + do { + try await streamQuery(query, isAborted: isAborted, onElement: { continuation.yield($0) }) + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + + // `isAborted` is a closure because the iOS app compiles this file without PluginKit, and is polled because + // `Task.isCancelled` is always false on the bare queue. Only the first result set streams; later ones are read + // past. `onElement` runs on the connection queue, so a caller that waits in it for its reader holds the session. + func streamQuery( + _ query: String, + isAborted: (@Sendable () -> Bool)? = nil, + onElement: @escaping @Sendable (MSSQLStreamElement) -> Void ) async throws { let queryToRun = String(query) let call = requestGate.enqueue() try await withTaskCancellationHandler { try await freetdsDispatchAsync(on: queue) { [self] in - do { - let readout = try self.readSync( - queryToRun, - call: call, - plan: .stream, - sink: .stream(continuation), - isAborted: isAborted - ) - if let error = readout.errors.first { - continuation.finish(throwing: MSSQLCoreError.queryFailed(error.message.text)) - return - } - continuation.finish() - } catch { - continuation.finish(throwing: error) + let readout = try self.readSync( + queryToRun, + call: call, + plan: .stream, + sink: .stream(onElement), + isAborted: isAborted + ) + if let error = readout.errors.first { + throw MSSQLCoreError.queryFailed(error.message.text) } } } onCancel: { [weak self] in @@ -876,7 +878,7 @@ nonisolated private struct FreeTDSReadPlan: Sendable { nonisolated private enum FreeTDSRowSink { case buffer - case stream(AsyncThrowingStream.Continuation) + case stream(@Sendable (MSSQLStreamElement) -> Void) } /// Reads one request to its end, or ends it early, so the connection is never left with results nobody will read. @@ -999,8 +1001,8 @@ nonisolated private struct FreeTDSBatchReader { private mutating func readResultSet(columnCount: Int, errorsBefore: Int) throws -> ResultSetEnd { let descriptors = (1...columnCount).map(describeColumn) - if case .stream(let continuation) = sink { - continuation.yield(.header(columns: descriptors)) + if case .stream(let emit) = sink { + emit(.header(columns: descriptors)) headerStreamed = true } @@ -1124,20 +1126,20 @@ nonisolated private struct FreeTDSBatchReader { } private func streamIfFull(_ rows: inout [[MSSQLRawCell]]) { - guard case .stream(let continuation) = sink, rows.count >= MSSQLRowLimits.streamBatchSize else { return } - continuation.yield(.rows(rows)) + guard case .stream(let emit) = sink, rows.count >= MSSQLRowLimits.streamBatchSize else { return } + emit(.rows(rows)) rows.removeAll(keepingCapacity: true) } private func streamRemaining(_ rows: inout [[MSSQLRawCell]]) { - guard case .stream(let continuation) = sink, !rows.isEmpty else { return } - continuation.yield(.rows(rows)) + guard case .stream(let emit) = sink, !rows.isEmpty else { return } + emit(.rows(rows)) rows.removeAll() } private func finishCountOnlyStream() { - guard case .stream(let continuation) = sink, !headerStreamed, errorTotal == 0 else { return } - continuation.yield(.affectedRows(rowsAffected)) + guard case .stream(let emit) = sink, !headerStreamed, errorTotal == 0 else { return } + emit(.affectedRows(rowsAffected)) } private mutating func absorbMessages() { diff --git a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift index 60fb612887..057d4a4d80 100644 --- a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift +++ b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift @@ -701,26 +701,11 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return AsyncThrowingStream { $0.finish(throwing: MSSQLPluginError.notConnected) } } return PluginRowStream.make { continuation, abort in - let streamTask = Task { - let coreStream = AsyncThrowingStream { coreContinuation in - /// A plain `Task {}` inherits context but is not a child, so cancelling the - /// outer task never reaches this one. The abort closure is what does. - Task { - do { - try await conn.streamQuery( - query, - isAborted: { abort.isAborted }, - continuation: coreContinuation - ) - } catch let error as MSSQLCoreError { - coreContinuation.finish(throwing: MSSQLPluginError(coreError: error)) - } catch { - coreContinuation.finish(throwing: error) - } - } - } + // Never cancelled: cancelling reaches `cancelCurrentQuery`, which sends an attention whatever the session + // holds, while the `isAborted` poll ends the request the way `MSSQLRequestEnding` allows. + Task { do { - for try await element in coreStream { + try await conn.streamQuery(query, isAborted: { abort.isAborted }, onElement: { element in switch element { case .header(let columns): continuation.yield(.header(PluginStreamHeader( @@ -732,11 +717,11 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let pluginRows: [PluginRow] = batch.map { row in row.map { $0.asPluginCell } } - continuation.yield(.rows(pluginRows)) + abort.send(pluginRows, to: continuation) case .affectedRows: break } - } + }) continuation.finish() } catch let error as MSSQLCoreError { continuation.finish(throwing: MSSQLPluginError(coreError: error)) @@ -744,7 +729,6 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } - abort.onAbort { streamTask.cancel() } } } diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift index 93d1cda577..5b7ac04d65 100644 --- a/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift @@ -313,7 +313,7 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let task = Task { do { if let spec { - try await self.streamBrowse(spec, into: continuation) + try await self.streamBrowse(spec, abort: abort, into: continuation) } else { let result = try await self.execute(query: query) var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) @@ -332,6 +332,7 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func streamBrowse( _ spec: MemcachedBrowseSpec, + abort: PluginStreamAbort, into continuation: AsyncThrowingStream.Continuation ) async throws { let page = try await browsePage(spec, on: try liveConnection()) @@ -347,7 +348,7 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { try Task.checkCancellation() let end = min(start + Self.rowsPerStreamBatch, page.entries.endIndex) let rows = try await self.rows(of: page.entries[start ..< end], serverTime: page.serverTime, on: connection) - if !rows.isEmpty { continuation.yield(.rows(rows)) } + if !rows.isEmpty { await abort.send(rows, to: continuation) } start = end } } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift index 55c8df7418..0e6dd9f4d1 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift @@ -415,13 +415,27 @@ nonisolated extension MongoDBConnection { return (docs: results, isTruncated: truncated) } + private static let streamBatchSize = 1_000 + + // The server reaps a cursor that sees no getMore for cursorTimeoutMillis, 10 min by default. A server batch of + // `serverBatchSize` documents drains in at most 10 pauses, so a stalled consumer still asks within that limit. + private static let idleCursorPause: TimeInterval = 30 + private static let serverBatchSize: UInt32 = 10_000 + func iterateCursorStreaming( cursor: OpaquePointer, continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort, streamState: MongoStreamState, census: () throws -> MongoFieldCensus? ) { defer { cleanup(streamState) } + // A paced read drains a server batch as slowly as its consumer, and an unset batch runs to 16 MB. + if mongoc_cursor_get_batch_size(cursor) == 0 { + mongoc_cursor_set_batch_size(cursor, Self.serverBatchSize) + } + var rows: [PluginRow] = [] + var documents: [PluginExportDocument] = [] do { var docPtr: OpaquePointer? var sample: [[String: Any]] = [] @@ -440,7 +454,8 @@ nonisolated extension MongoDBConnection { } if projection == nil, sample.count >= MongoStreamProjection.sampleSize { projection = try openStream( - sample: sample, texts: sampleTexts, census: try census(), continuation: continuation + sample: sample, texts: sampleTexts, census: try census(), + continuation: continuation, abort: abort ) emitted += sample.count sample = [] @@ -459,7 +474,13 @@ nonisolated extension MongoDBConnection { documents: [dict], columns: projection.columns, kinds: projection.kinds, representation: uuidRepresentation, storedTexts: [text] )[0] - continuation.yield(.rows([row], documents: [document])) + rows.append(row) + documents.append(document) + if rows.count >= Self.streamBatchSize { + abort.send(rows, documents: documents, to: continuation, maximumPause: Self.idleCursorPause) + rows.removeAll(keepingCapacity: true) + documents.removeAll(keepingCapacity: true) + } unannounced.formUnion(projection.unannouncedFields(in: dict)) emitted += 1 if emitted >= PluginRowLimits.emergencyMax { @@ -467,14 +488,25 @@ nonisolated extension MongoDBConnection { break } } + if !rows.isEmpty { + continuation.yield(.rows(rows, documents: documents)) + rows = [] + documents = [] + } logUnannouncedFields(unannounced) var error = bson_error_t() if mongoc_cursor_error(cursor, &error) { throw makeError(error) } if projection == nil { - _ = try openStream(sample: sample, texts: sampleTexts, census: nil, continuation: continuation) + _ = try openStream( + sample: sample, texts: sampleTexts, census: nil, continuation: continuation, abort: abort + ) } continuation.finish() } catch { + // The documents read before a failure reach the reader ahead of it, as they did one at a time. + if !rows.isEmpty, !streamState.lock.withLock({ streamState.cancelled }) { + continuation.yield(.rows(rows, documents: documents)) + } continuation.finish(throwing: error) } } @@ -512,7 +544,8 @@ nonisolated extension MongoDBConnection { sample: [[String: Any]], texts: [String], census: MongoFieldCensus?, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws -> MongoStreamProjection { let projection = MongoStreamProjection(sample: sample, census: census, representation: uuidRepresentation) let documents = try MongoExportDocument.read( @@ -527,7 +560,7 @@ nonisolated extension MongoDBConnection { documents: sample, columns: projection.columns, kinds: projection.kinds, representation: uuidRepresentation, storedTexts: texts ) - continuation.yield(.rows(rows, documents: documents)) + abort.send(rows, documents: documents, to: continuation, maximumPause: Self.idleCursorPause) } return projection } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift index 2fe2d211a1..3cafc3a792 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift @@ -830,11 +830,8 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { } // MARK: - Streaming Queries - /// Streams a find whose options are given as the document the caller already built. - /// - /// Taking the text rather than rebuilding it from pieces is what keeps an export reading the - /// same rows the grid showed: `hint`, `collation` and `allowDiskUse` change which documents come - /// back, and a signature that only carries sort, projection, skip and limit drops them. + // Takes the options as the document the caller built, so an export keeps the `hint`, `collation` and + // `allowDiskUse` that decide which documents the grid showed. func streamFind( database: String, collection: String, @@ -846,8 +843,8 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { let queue = self.queue let streamState = MongoStreamState() - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in - continuation.onTermination = { @Sendable _ in + return PluginRowStream.makeExport { continuation, abort in + abort.onAbort { streamState.lock.withLock { streamState.cancelled = true } queue.async { streamState.lock.lock() @@ -891,7 +888,7 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { streamState.lock.unlock() iterateCursorStreaming( - cursor: cursor, continuation: continuation, streamState: streamState + cursor: cursor, continuation: continuation, abort: abort, streamState: streamState ) { try self.fieldCensus(census, client: client, database: database, collection: collection) } @@ -916,8 +913,8 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { let queue = self.queue let streamState = MongoStreamState() - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in - continuation.onTermination = { @Sendable _ in + return PluginRowStream.makeExport { continuation, abort in + abort.onAbort { streamState.lock.withLock { streamState.cancelled = true } queue.async { streamState.lock.lock() @@ -972,7 +969,7 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { streamState.lock.unlock() iterateCursorStreaming( - cursor: cursor, continuation: continuation, streamState: streamState + cursor: cursor, continuation: continuation, abort: abort, streamState: streamState ) { try self.fieldCensus(census, client: client, database: database, collection: collection) } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index 88935569d4..a2aa4d52d6 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift @@ -933,7 +933,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } let timeout = conn.queryTimeoutMS - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return PluginRowStream.makeExport { continuation, abort in let work = Task { do { switch try await runtime.exportPlan(for: trimmed, database: db, access: access, shell: shell) { @@ -965,7 +965,12 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se do { for try await element in inner { try Task.checkCancellation() - continuation.yield(element) + switch element { + case .rows(let rows, let documents): + await abort.send(rows, documents: documents, to: continuation) + default: + continuation.yield(element) + } } } catch { throw MongoScriptStatementFailure.carrying( @@ -983,7 +988,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } // A consumer that stops reading has to stop the cursor too, or it keeps draining the // whole query and holds the leased driver busy. - continuation.onTermination = { @Sendable _ in work.cancel() } + abort.onAbort { work.cancel() } } } diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift index ba69a63dc3..942371e3e4 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift @@ -738,6 +738,9 @@ final class MariaDBPluginConnection: @unchecked Sendable { private static let killTimeoutSeconds: UInt32 = 5 + // The server drops a connection it could not write a result to for net_write_timeout, 60 s by default. + private static let unreadResultPause: TimeInterval = 20 + // Connector/C turns TLS on for any CA, certificate or key it is given, so a plaintext attempt gets none. private func applyTLS(_ mode: SSLMode, to handle: UnsafeMutablePointer) { MariaDBTLSMode.apply(mode, serverName: tlsServerName, to: handle) @@ -1542,7 +1545,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { batch.append(textProtocolRow(rowPtr, lengths: mysql_fetch_lengths(resultPtr), columns: columns)) if batch.count >= batchSize { - continuation.yield(.rows(batch)) + abort.send(batch, to: continuation, maximumPause: Self.unreadResultPause) batch.removeAll(keepingCapacity: true) } } diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift index 6778d15221..2af2d6d5a6 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift @@ -1092,15 +1092,14 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return streamRows(query: query, readOnlySessionState: readOnlySessionState) } - /// The bridge task is retained and cancelled from `onTermination`. Without that, a consumer - /// that stops reading, which an export or a copy does on cancel, leaves this task draining the - /// whole result and holding the connection open: the inner stream's abort never fires because - /// nothing ever cancels the task awaiting it. + // The bridge task is cancelled on abort: a consumer that stops reading otherwise leaves it draining the whole + // result, since nothing cancels the task awaiting the inner stream. It forwards rows through `send`, so it waits + // with the consumer instead of reading the inner stream into its own buffer. private func streamRows( query: String, readOnlySessionState: (@Sendable (Bool) -> PluginSessionTransactionState)? ) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in + PluginRowStream.make { [self] continuation, abort in let task = Task { do { try await retryingAfterStop { @@ -1108,7 +1107,10 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { defer { self.endOperation(on: conn) } noteActivity(query) for try await element in conn.streamQuery(query, readOnlySessionState: readOnlySessionState) { - continuation.yield(element) + switch element { + case .header: continuation.yield(element) + case .rows(let rows): await abort.send(rows, to: continuation) + } } } continuation.finish() @@ -1116,7 +1118,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } - continuation.onTermination = { _ in task.cancel() } + abort.onAbort { task.cancel() } } } diff --git a/apple/Plugins/Neo4jDriverPlugin/Neo4jConnection.swift b/apple/Plugins/Neo4jDriverPlugin/Neo4jConnection.swift index 970a601ae8..ce469d21a0 100644 --- a/apple/Plugins/Neo4jDriverPlugin/Neo4jConnection.swift +++ b/apple/Plugins/Neo4jDriverPlugin/Neo4jConnection.swift @@ -381,7 +381,7 @@ actor Neo4jConnection { _ statement: BoltRequest, batchSize: Int64, interruptible: Bool, - receive: @Sendable (_ keys: [String], _ records: [[PackStreamValue]], _ isFirst: Bool) -> Bool + receive: @Sendable (_ keys: [String], _ records: [[PackStreamValue]], _ isFirst: Bool) async -> Bool ) async throws { try await withTurn(interruptible: interruptible) { wire, since in let replies = try await Self.exchange([statement, .pull(count: batchSize)], on: wire, cancellableSince: since) @@ -391,7 +391,7 @@ actor Neo4jConnection { var batch = replies[1] var isFirst = true while true { - let wanted = receive(keys, batch.records, isFirst) + let wanted = await receive(keys, batch.records, isFirst) isFirst = false guard batch.hasMore else { return } guard wanted, !Task.isCancelled else { diff --git a/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift b/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift index 4bca23eae3..73d032d7c7 100644 --- a/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift +++ b/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift @@ -236,22 +236,23 @@ final class Neo4jPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // Pulls a batch at a time, so an export never holds the whole result. func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - AsyncThrowingStream { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { - try await self.stream(query, context: context, into: continuation) + try await self.stream(query, context: context, abort: abort, into: continuation) continuation.finish() } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { _ in task.cancel() } + abort.onAbort { task.cancel() } } } private func stream( _ query: String, context: PluginStatementContext, + abort: PluginStreamAbort, into continuation: AsyncThrowingStream.Continuation ) async throws { let connection = try requireConnection() @@ -278,8 +279,8 @@ final class Neo4jPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.yield(.header(PluginStreamHeader(columns: table.columns, columnTypeNames: table.typeNames))) } guard !table.rows.isEmpty else { return true } - if case .terminated = continuation.yield(.rows(Self.cells(table.rows))) { return false } - return true + await abort.send(Self.cells(table.rows), to: continuation) + return !abort.isAborted } } diff --git a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift index f71b06fe00..39a6995d71 100644 --- a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift +++ b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift @@ -469,21 +469,10 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable { let batching = OracleRowBatcher(rowsPerBatch: OracleRowBatcher.batchRows, rowBudget: rowBudget) return PluginRowStream.make { continuation, abort in + // A plain `Task {}` is not a child of this one, so only the explicit cancel on abort stops oracle-nio. let streamTask = Task { - // A plain `Task {}` is not a child of this one, so only the explicit cancel on abort stops oracle-nio. - let coreStream = AsyncThrowingStream { coreContinuation in - let coreTask = Task { - do { - try await core.streamQuery(query, batching: batching, continuation: coreContinuation) - } catch { - coreContinuation.finish(throwing: error) - } - } - abort.onAbort { coreTask.cancel() } - coreContinuation.onTermination = { @Sendable _ in coreTask.cancel() } - } do { - for try await element in coreStream { + try await core.streamQuery(query, batching: batching, onElement: { element in switch element { case .header(let columns): continuation.yield(.header(PluginStreamHeader( @@ -491,13 +480,13 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable { columnTypeNames: columns.map(\.typeName) ))) case .rows(let batch): - continuation.yield(.rows(batch.map { $0.map(\.asPluginCell) })) + await abort.send(batch.map { $0.map(\.asPluginCell) }, to: continuation) case .affectedRows: // The plugin stream carries rows only. The app sends writes and DDL through // `execute`, which reports the count, and bounds only reads through this stream. break } - } + }) continuation.finish() } catch let error as OracleCoreError { continuation.finish(throwing: error.asPluginError) diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index a57fb8a048..6c3c46060d 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -1672,7 +1672,7 @@ final class LibPQPluginConnection: @unchecked Sendable { PQclear(result) batch.append(row) if batch.count >= batchSize { - continuation.yield(.rows(batch)) + run.abort.send(batch, to: continuation) batch.removeAll(keepingCapacity: true) } diff --git a/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift b/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift index 4bf1ae92c0..bb420973df 100644 --- a/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift +++ b/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift @@ -150,11 +150,11 @@ extension QdrantPluginDriver { // The protocol's default `streamRows` runs `execute` once and buffers the whole collection. func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { if let name = QdrantOperations.decodeExport(query) { - try await self.streamCollection(name, continuation: continuation) + try await self.streamCollection(name, abort: abort, continuation: continuation) } else { let result = try await self.execute(query: query) continuation.yield(.header(PluginStreamHeader( @@ -169,7 +169,7 @@ extension QdrantPluginDriver { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } @@ -177,6 +177,7 @@ extension QdrantPluginDriver { // alone, which skips the vectors that make up most of a page. private func streamCollection( _ name: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let client = try requireClient() @@ -192,7 +193,7 @@ extension QdrantPluginDriver { estimatedRowCount: collection.pointsCount ))) for try await points in client.pointPages(collection: name, pageSize: Self.exportPageSize) { - continuation.yield(.rows(points.map(cells(columns: columns, typeNames: typeNames)))) + await abort.send(points.map(cells(columns: columns, typeNames: typeNames)), to: continuation) } } } diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift index 75ff9f44ec..dc2bf88db5 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift @@ -519,22 +519,21 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, continuation: continuation) + try await self.performStreamRows(query: query, abort: abort, continuation: continuation) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } private func performStreamRows( query: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let conn = redisConnection else { @@ -546,7 +545,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { switch operation { case .scan(_, let pattern, _, let type): try await streamScanRows( - connection: conn, pattern: pattern, typeFilter: type, scope: .session, continuation: continuation + connection: conn, pattern: pattern, typeFilter: type, scope: .session, abort: abort, continuation: continuation ) case .keyBrowse(let pattern, let typeScope, _, _, let database): try await conn.withDatabase(database) { @@ -555,6 +554,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { pattern: pattern, typeFilter: typeScope, scope: .outsideBlock, + abort: abort, continuation: continuation ) } @@ -580,6 +580,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { pattern: String?, typeFilter: String? = nil, scope: RedisCommandScope, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { continuation.yield(.header(PluginStreamHeader( @@ -606,7 +607,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let batchKeys = Array(page.keys[batchStart ..< batchEnd]) let rowBatch = try await buildKeySummaryRows(keys: batchKeys, connection: conn) if !rowBatch.isEmpty { - continuation.yield(.rows(rowBatch)) + await abort.send(rowBatch, to: continuation) } batchStart = batchEnd } diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift index 5ff155c7d4..559b8cf0db 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift @@ -127,7 +127,8 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) async throws { _ = try await withTaskCancellationHandler { try await connection.execute( @@ -150,6 +151,8 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { batch.append(values[index..<(index + columnCount)].map(Self.decode)) index += columnCount } + // Not paced: a reader thread waiting here leaves the tunnel relay blocked sending to it, so it + // stops forwarding the heartbeats and the agent's 60 s idle watchdog ends the session. continuation.yield(.rows(batch)) } ) diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift index 5906915c69..dc3d3fb0f6 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift @@ -48,7 +48,8 @@ protocol SQLiteExecutionBackend: Actor { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) async throws } @@ -74,9 +75,10 @@ extension SQLiteExecutionBackend { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) async throws { - try await streamQuery(query, scope: .unrestricted, continuation: continuation) + try await streamQuery(query, scope: .unrestricted, continuation: continuation, abort: abort) } } @@ -432,10 +434,11 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { try withStatementHandle(scope) { handle, mustRead in - try stream(query, on: handle, mustRead: mustRead, continuation: continuation) + try stream(query, on: handle, mustRead: mustRead, continuation: continuation, abort: abort) } } @@ -443,7 +446,8 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { _ query: String, on db: OpaquePointer, mustRead: Bool, - continuation: AsyncThrowingStream.Continuation + continuation: AsyncThrowingStream.Continuation, + abort: PluginStreamAbort ) throws { let statement = try Self.prepareOnlyStatement(query, db: db) // EXPLAIN never runs its statement, but stmt_readonly reports that statement's writes. @@ -474,7 +478,8 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { } batch.append(Self.readRow(statement, columnCount: columnCount)) if batch.count >= batchSize { - continuation.yield(.rows(batch)) + // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. + abort.send(batch, to: continuation) batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift index 439acba77e..c86999fa14 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift @@ -692,17 +692,17 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func stream(_ query: String, scope: SQLiteStatementScope) -> AsyncThrowingStream { let queryToRun = String(query) - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return PluginRowStream.make { continuation, abort in let streamTask = Task { do { - try await self.backend.streamQuery(queryToRun, scope: scope, continuation: continuation) + try await self.backend.streamQuery( + queryToRun, scope: scope, continuation: continuation, abort: abort + ) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in - streamTask.cancel() - } + abort.onAbort { streamTask.cancel() } } } diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift index 6ad5f5bbf2..c0c40c3aa9 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakeConnection.swift @@ -714,7 +714,8 @@ final class SnowflakeConnection: @unchecked Sendable { let columns: [SnowflakeColumnMeta] let inlineRows: [[PluginCellValueBox]] let estimatedRowCount: Int - let batches: AsyncThrowingStream<[[PluginCellValueBox]], Error> + let chunkURLs: [URL] + let chunkHeaders: [String: String] } func queryStreamed( @@ -731,58 +732,56 @@ final class SnowflakeConnection: @unchecked Sendable { let headers = Self.chunkRequestHeaders(from: data) let urls = try Self.chunkURLs(chunks) - let stream = AsyncThrowingStream<[[PluginCellValueBox]], Error> { continuation in - guard !urls.isEmpty else { - continuation.finish() - return + return StreamedResult( + columns: columns, + inlineRows: inlineRows, + estimatedRowCount: inlineRows.count + chunkRowCount, + chunkURLs: urls, + chunkHeaders: headers + ) + } + + // Chunks reach `body` in order. A download starts only as a chunk is handed on, so the chunks downloading or + // waiting for one that lags never number more than `chunkDownloadWorkers`. + func forEachChunk( + of result: StreamedResult, + _ body: ([[PluginCellValueBox]]) async throws -> Void + ) async throws { + let urls = result.chunkURLs + guard !urls.isEmpty else { return } + let headers = result.chunkHeaders + let columns = result.columns + let session = self.session + try await withThrowingTaskGroup(of: (Int, [[PluginCellValueBox]]).self) { group in + var nextIndex = 0 + while nextIndex < min(Self.chunkDownloadWorkers, urls.count) { + let index = nextIndex + group.addTask { + (index, try await Self.downloadChunk( + urls[index], headers: headers, session: session, columns: columns + )) + } + nextIndex += 1 } - let task = Task { [session] in - do { - var buffer: [Int: [[PluginCellValueBox]]] = [:] - var nextToYield = 0 - try await withThrowingTaskGroup(of: (Int, [[PluginCellValueBox]]).self) { group in - var nextIndex = 0 - while nextIndex < min(Self.chunkDownloadWorkers, urls.count) { - let index = nextIndex - group.addTask { - (index, try await Self.downloadChunk( - urls[index], headers: headers, session: session, columns: columns - )) - } - nextIndex += 1 - } - while let (index, rows) = try await group.next() { - buffer[index] = rows - while let ready = buffer[nextToYield] { - buffer[nextToYield] = nil - continuation.yield(ready) - nextToYield += 1 - } - if nextIndex < urls.count { - let next = nextIndex - group.addTask { - (next, try await Self.downloadChunk( - urls[next], headers: headers, session: session, columns: columns - )) - } - nextIndex += 1 - } + var held: [Int: [[PluginCellValueBox]]] = [:] + var nextToHandOn = 0 + while let (index, rows) = try await group.next() { + held[index] = rows + while let ready = held.removeValue(forKey: nextToHandOn) { + try await body(ready) + nextToHandOn += 1 + if nextIndex < urls.count { + let next = nextIndex + group.addTask { + (next, try await Self.downloadChunk( + urls[next], headers: headers, session: session, columns: columns + )) } + nextIndex += 1 } - continuation.finish() - } catch { - continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } } - - return StreamedResult( - columns: columns, - inlineRows: inlineRows, - estimatedRowCount: inlineRows.count + chunkRowCount, - batches: stream - ) } private static let chunkDownloadWorkers = 4 diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift index 13c8010726..33d0ba10af 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift @@ -619,29 +619,29 @@ final class SnowflakePluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { guard let conn = self.connection else { throw SnowflakeError.notConnected } let trimmed = query.replacingOccurrences(of: ";\\s*\\z", with: "", options: .regularExpression) - let streamed = try await conn.queryStreamed(trimmed, owner: queryOwner) + let streamed = try await conn.queryStreamed(trimmed, owner: self.queryOwner) continuation.yield(.header(PluginStreamHeader( columns: streamed.columns.map(\.name), columnTypeNames: streamed.columns.map(SnowflakeTypeMapper.displayType), estimatedRowCount: streamed.estimatedRowCount ))) if !streamed.inlineRows.isEmpty { - continuation.yield(.rows(streamed.inlineRows.map { row in row.map(Self.cellValue) })) + await abort.send(streamed.inlineRows.map { row in row.map(Self.cellValue) }, to: continuation) } - for try await batch in streamed.batches { - continuation.yield(.rows(batch.map { row in row.map(Self.cellValue) })) + try await conn.forEachChunk(of: streamed) { batch in + await abort.send(batch.map { row in row.map(Self.cellValue) }, to: continuation) } continuation.finish() } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } diff --git a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift index a017a93fa2..ad48cbb5c0 100644 --- a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift +++ b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift @@ -1,4 +1,5 @@ import Foundation +import os import TableProPluginKit import TableProSpannerCore @@ -15,23 +16,26 @@ extension SpannerPluginDriver { try await boundedQueryFromStream(query: query, rowCap: rowCap) } + // Spanner may abort a read-write transaction left idle for about 10 s, and a stream can run inside one. + private static let unreadStreamPause: TimeInterval = 5 + func streamRows(query: String) -> AsyncThrowingStream { PluginRowStream.make { continuation, abort in let task = Task { do { let executor = try self.requireExecutor() - var headerSent = false - for try await output in self.outputStream(for: query, executor: executor) { + let headerSent = OSAllocatedUnfairLock(initialState: false) + try await self.streamOutput(for: query, executor: executor) { output in switch output { case .header(let fields): continuation.yield(.header(Self.streamHeader(fields))) - headerSent = true + headerSent.withLock { $0 = true } case .rows(let rows): - continuation.yield(.rows(rows.map(Self.pluginRow))) + await abort.send(rows.map(Self.pluginRow), to: continuation, maximumPause: Self.unreadStreamPause) } } try Task.checkCancellation() - if !headerSent { + if !headerSent.withLock({ $0 }) { continuation.yield(.header(PluginStreamHeader(columns: [], columnTypeNames: []))) } continuation.finish() @@ -47,14 +51,16 @@ extension SpannerPluginDriver { } } - private func outputStream( + private func streamOutput( for query: String, - executor: SpannerExecutor - ) -> AsyncThrowingStream { + executor: SpannerExecutor, + into emit: @escaping SpannerStreamEmit + ) async throws { if let request = SpannerBrowseRequest.decode(query) { - return executor.stream(SpannerBrowseRenderer.select(resolved(request), dialect: executor.dialect)) + try await executor.stream(SpannerBrowseRenderer.select(resolved(request), dialect: executor.dialect), into: emit) + return } - return executor.stream(query, hostParameters: nil) + try await executor.stream(query, hostParameters: nil, into: emit) } private func executeStatement(_ query: String, parameters: [PluginCellValue]?) async throws -> PluginQueryResult { diff --git a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift index 7ed43ac87e..3e2b007605 100644 --- a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift +++ b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift @@ -1,63 +1,182 @@ import Foundation -/// The signal a row stream's producer polls to learn that its consumer stopped. -/// -/// Terminating an `AsyncThrowingStream` runs its `onTermination` handler and nothing else, so a -/// producer that does not poll runs to completion no matter what the consumer did. Measured on a -/// producer occupying a serial queue with 40 batches of work: polling this flag stopped it after -/// one batch, while hopping the drain onto the producer's own queue stopped nothing and ran the -/// drain only after the whole read had finished. +// Terminating an AsyncThrowingStream only runs its onTermination handler, so a producer learns that its consumer +// stopped by polling `isAborted`, and learns that its consumer fell behind by sending through `send(_:to:)`. public final class PluginStreamAbort: @unchecked Sendable { - private let lock = NSLock() + public typealias Continuation = AsyncThrowingStream.Continuation + public typealias ExportContinuation = AsyncThrowingStream.Continuation + + private let condition = NSCondition() private var _isAborted = false private var actions: [@Sendable () -> Void] = [] + private let readAhead: Int? + private var sentBatches = 0 + private var takenBatches = 0 + private var waiters: [Int: CheckedContinuation] = [:] + private var nextWaiterID = 0 + + public init() { + readAhead = nil + } - public init() {} + init(readAhead: Int) { + self.readAhead = max(readAhead, 1) + } public var isAborted: Bool { - lock.lock() - defer { lock.unlock() } + condition.lock() + defer { condition.unlock() } return _isAborted } - /// Registers work to run when the consumer stops, for a producer that also needs an active - /// signal such as cancelling its own task. Registering here rather than assigning - /// `continuation.onTermination` is what keeps both from clobbering each other; the stream's - /// handler belongs to `PluginRowStream.make`. Runs immediately if the abort already fired. + // For a producer that also needs an active signal, such as cancelling its own task. The stream's onTermination + // belongs to `PluginRowStream.make`, so registering here keeps the two from clobbering each other. public func onAbort(_ action: @escaping @Sendable () -> Void) { - lock.lock() + condition.lock() if _isAborted { - lock.unlock() + condition.unlock() action() return } actions.append(action) - lock.unlock() + condition.unlock() } + // The actions run before a waiting producer wakes, so it sees what they did, such as its cancelled task. public func abort() { - lock.lock() + condition.lock() let alreadyAborted = _isAborted _isAborted = true let pending = actions actions.removeAll() - lock.unlock() - guard !alreadyAborted else { return } - for action in pending { action() } + condition.unlock() + if !alreadyAborted { + for action in pending { action() } + } + condition.lock() + let parked = Array(waiters.values) + waiters.removeAll() + condition.broadcast() + condition.unlock() + for waiter in parked { waiter.resume() } + } + + // Blocks the calling thread, so it is for a producer looping on a queue or thread of its own. Returns once the + // consumer is fewer than the stream's read-ahead batches behind, once it stops, or after `maximumPause` seconds, + // for a server that drops a connection left unread that long; past that the producer reads on as before. A batch + // a bounded buffer dropped is not counted, since the consumer never takes it. + public func send(_ rows: [PluginRow], to continuation: Continuation, maximumPause: TimeInterval? = nil) { + guard case .enqueued = continuation.yield(.rows(rows)) else { return } + waitForConsumer(maximumPause: maximumPause) + } + + // The same for a producer running in a task: it suspends instead of blocking, and a cancelled task returns. + public func send(_ rows: [PluginRow], to continuation: Continuation, maximumPause: TimeInterval? = nil) async { + guard case .enqueued = continuation.yield(.rows(rows)) else { return } + await waitForConsumer(maximumPause: maximumPause) + } + + public func send( + _ rows: [PluginRow], + documents: [PluginExportDocument]?, + to continuation: ExportContinuation, + maximumPause: TimeInterval? = nil + ) { + guard case .enqueued = continuation.yield(.rows(rows, documents: documents)) else { return } + waitForConsumer(maximumPause: maximumPause) + } + + public func send( + _ rows: [PluginRow], + documents: [PluginExportDocument]?, + to continuation: ExportContinuation, + maximumPause: TimeInterval? = nil + ) async { + guard case .enqueued = continuation.yield(.rows(rows, documents: documents)) else { return } + await waitForConsumer(maximumPause: maximumPause) + } + + private func waitForConsumer(maximumPause: TimeInterval?) { + let deadline = maximumPause.map { Date(timeIntervalSinceNow: $0) } + condition.lock() + defer { condition.unlock() } + sentBatches += 1 + while isBehind { + if let deadline { + guard condition.wait(until: deadline) else { return } + } else { + condition.wait() + } + } + } + + private func waitForConsumer(maximumPause: TimeInterval?) async { + let id: Int? = { + condition.lock() + defer { condition.unlock() } + sentBatches += 1 + guard isBehind else { return nil } + nextWaiterID += 1 + return nextWaiterID + }() + guard let id else { return } + await withTaskCancellationHandler { + await withCheckedContinuation { (waiter: CheckedContinuation) in + condition.lock() + guard isBehind, !Task.isCancelled else { + condition.unlock() + waiter.resume() + return + } + waiters[id] = waiter + condition.unlock() + if let maximumPause { + DispatchQueue.global().asyncAfter(deadline: .now() + maximumPause) { [weak self] in + self?.release(id) + } + } + } + } onCancel: { + release(id) + } + } + + func consumerTookBatch() { + condition.lock() + takenBatches += 1 + guard !isBehind else { + condition.unlock() + return + } + let parked = Array(waiters.values) + waiters.removeAll() + condition.broadcast() + condition.unlock() + for waiter in parked { waiter.resume() } + } + + // Read under the lock. + private var isBehind: Bool { + guard let readAhead, !_isAborted else { return false } + return sentBatches - takenBatches >= readAhead + } + + private func release(_ id: Int) { + condition.lock() + let waiter = waiters.removeValue(forKey: id) + condition.unlock() + waiter?.resume() } } public enum PluginRowStream { - /// Builds a row stream whose abort signal is already wired to its termination. - /// - /// The handler is installed before `build` runs and sets the flag synchronously on whichever - /// thread terminated the stream, so a producer that has not started yet still sees it and can - /// decline to send the query at all. A producer that hops the signal onto its own serial queue - /// instead sees it only after it has finished, which is the defect this exists to make - /// unrepeatable. - /// - /// Termination follows the lifetime of the stream, not the consumer's `break`: a consumer that - /// stops reading but holds the stream in scope has not terminated it. + // Batches a producer that sends through `PluginStreamAbort.send` may have unread before it waits. + static let readAhead = 2 + + // The abort handler is installed before `build` runs and sets the flag on whichever thread terminated the stream, + // so a producer that has not started yet still sees it and can decline to send the query. Termination follows the + // stream's lifetime, not the consumer's `break`: a consumer that stops reading but holds the stream has not ended it. + // The stream handed back is pulled from the producer's, so each batch the consumer takes is counted for `send`. public static func make( bufferingPolicy: AsyncThrowingStream.Continuation.BufferingPolicy = .unbounded, _ build: @escaping @Sendable ( @@ -65,10 +184,55 @@ public enum PluginRowStream { PluginStreamAbort ) -> Void ) -> AsyncThrowingStream { - let abort = PluginStreamAbort() - return AsyncThrowingStream(bufferingPolicy: bufferingPolicy) { continuation in + paced(bufferingPolicy: bufferingPolicy, build) { element in + if case .rows = element { return true } + return false + } + } + + // The same for a producer that hands documents along with its rows. + public static func makeExport( + _ build: @escaping @Sendable ( + AsyncThrowingStream.Continuation, + PluginStreamAbort + ) -> Void + ) -> AsyncThrowingStream { + paced(bufferingPolicy: .unbounded, build) { element in + if case .rows = element { return true } + return false + } + } + + private static func paced( + bufferingPolicy: AsyncThrowingStream.Continuation.BufferingPolicy, + _ build: @escaping @Sendable (AsyncThrowingStream.Continuation, PluginStreamAbort) -> Void, + isBatch: @escaping @Sendable (Element) -> Bool + ) -> AsyncThrowingStream { + let abort = PluginStreamAbort(readAhead: readAhead) + let produced = AsyncThrowingStream(bufferingPolicy: bufferingPolicy) { continuation in continuation.onTermination = { @Sendable _ in abort.abort() } build(continuation, abort) } + let source = PacedSource(produced, abort: abort, isBatch: isBatch) + return AsyncThrowingStream(unfolding: { try await source.next() }) + } +} + +// One consumer reads the stream, so one call at a time reaches the iterator. +private final class PacedSource: @unchecked Sendable { + private var iterator: AsyncThrowingStream.AsyncIterator + private let abort: PluginStreamAbort + private let isBatch: @Sendable (Element) -> Bool + + init(_ stream: AsyncThrowingStream, abort: PluginStreamAbort, isBatch: @escaping @Sendable (Element) -> Bool) { + iterator = stream.makeAsyncIterator() + self.abort = abort + self.isBatch = isBatch + } + + func next() async throws -> Element? { + let element = try await iterator.next() + if let element, isBatch(element) { abort.consumerTookBatch() } + return element } } diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift index c9dd03b96e..e880dfff88 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift @@ -235,7 +235,7 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return } do { - // Each reply's rows go out as it arrives, before the next one is asked for. + // Each reply's rows go out as it arrives, and the next one is asked for once the reader has room. try await connection.stream( query, onColumns: { columns in @@ -246,8 +246,8 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { }, onRows: { rows in guard !abort.isAborted else { return false } - continuation.yield(.rows(rows.map { row in row.map { $0.asPluginCell } })) - return true + abort.send(rows.map { row in row.map { $0.asPluginCell } }, to: continuation) + return !abort.isAborted } ) continuation.finish() diff --git a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift index 86711e30c1..57a9c1bf2f 100644 --- a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift +++ b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift @@ -14,6 +14,9 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { static let logger = Logger(subsystem: "com.TablePro", category: "TrinoPluginDriver") + // The coordinator abandons a query its client has not polled for query.client.timeout, 5 minutes by default. + static let unpolledQueryPause: TimeInterval = 60 + // PluginKit's parameter binder calls back into `execute(query:)`, so the host's split reaches it through the task. @TaskLocal private static var hostSplit = false @@ -173,18 +176,23 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } do { var headerSent = false - for try await element in client.executeStreamed(statement) { - switch element { - case .columns(let columns): + try await client.executeStreamed( + statement, + onColumns: { columns in continuation.yield(.header(PluginStreamHeader( columns: columns.map(\.name), columnTypeNames: columns.map(\.typeName) ))) headerSent = true - case .rows(let page): - continuation.yield(.rows(page.map { row in row.map(Self.cellValue) })) + }, + onPage: { page in + await abort.send( + page.map { row in row.map(Self.cellValue) }, + to: continuation, + maximumPause: Self.unpolledQueryPause + ) } - } + ) if !headerSent { continuation.yield(.header(PluginStreamHeader(columns: [], columnTypeNames: []))) } diff --git a/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift b/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift index c55ad2c08b..017488be85 100644 --- a/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift +++ b/apple/Plugins/TypesenseDriverPlugin/TypesenseConnection.swift @@ -50,6 +50,9 @@ internal struct TypesenseResponse { internal final class TypesenseConnection: NSObject, @unchecked Sendable { private let lock = NSLock() private var _session: URLSession? + // An export holds one response for as long as its consumer takes, and a paused read still counts toward the + // resource limit, so it runs on a session that keeps the system default. + private var _streamSession: URLSession? private var _currentTask: URLSessionDataTask? private var _serverVersion: String? private let queryTimeout = HttpQueryTimeoutBox() @@ -98,7 +101,13 @@ internal final class TypesenseConnection: NSObject, @unchecked Sendable { sessionConfig.timeoutIntervalForRequest = connectTimeout sessionConfig.timeoutIntervalForResource = HttpQueryTimeout.sessionResourceTimeout let session = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil) - lock.withLock { _session = session } + // The session made its own copy of the configuration above. + sessionConfig.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource + let streamSession = URLSession(configuration: sessionConfig, delegate: self, delegateQueue: nil) + lock.withLock { + _session = session + _streamSession = streamSession + } let response = try await request( method: "GET", @@ -118,6 +127,8 @@ internal final class TypesenseConnection: NSObject, @unchecked Sendable { _currentTask = nil _session?.finishTasksAndInvalidate() _session = nil + _streamSession?.finishTasksAndInvalidate() + _streamSession = nil } } @@ -180,10 +191,10 @@ internal final class TypesenseConnection: NSObject, @unchecked Sendable { /// collection never holds it in memory. func streamExport( collection: String, - onDocument: @escaping ([String: Any]) -> Bool + onDocument: ([String: Any]) async -> Bool ) async throws { let session: URLSession = try lock.withLock { - guard let session = _session else { throw TypesenseError.notConnected } + guard let session = _streamSession else { throw TypesenseError.notConnected } return session } let path = TypesenseOperations.exportPath(collection: collection) @@ -224,7 +235,7 @@ internal final class TypesenseConnection: NSObject, @unchecked Sendable { let data = line.data(using: .utf8), let document = try? JSONSerialization.jsonObject(with: data) as? [String: Any] else { continue } - if !onDocument(document) { return } + if await !onDocument(document) { return } } } diff --git a/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift b/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift index 0e86a82cdf..6fdf878921 100644 --- a/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift +++ b/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift @@ -109,24 +109,24 @@ extension TypesensePluginDriver { // MARK: - Streaming Export - /// Export reads the collection's JSONL endpoint a line at a time, so a collection larger than - /// memory still exports and the 250-hit search ceiling never applies. Anything that is not an - /// export tag runs as an ordinary query and is handed over in one batch. + // Export reads the collection's JSONL endpoint a line at a time, so a collection larger than memory still exports + // and the 250-hit search ceiling never applies. Anything else runs as an ordinary query, handed over in one batch. func streamRows(query: String) -> AsyncThrowingStream { - AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + PluginRowStream.make { continuation, abort in let task = Task { do { - try await self.performStreamRows(query: query, continuation: continuation) + try await self.performStreamRows(query: query, abort: abort, continuation: continuation) } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } private func performStreamRows( query: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let collection = TypesenseOperations.decodeExport(query) else { @@ -154,7 +154,7 @@ extension TypesensePluginDriver { try await connection.streamExport(collection: collection) { document in batch.append(document) guard batch.count >= Self.exportBatchSize else { return true } - continuation.yield(.rows(TypesenseSchema.rows(for: batch, columns: columns, length: .whole))) + await abort.send(TypesenseSchema.rows(for: batch, columns: columns, length: .whole), to: continuation) batch.removeAll(keepingCapacity: true) return true } diff --git a/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift b/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift index 0142ec23db..3efd02a8a7 100644 --- a/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift +++ b/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift @@ -41,28 +41,28 @@ extension WeaviatePluginDriver { static let exportPageSize = 500 - /// Exports one collection by walking it a page at a time, yielding each page instead of - /// holding the whole collection. The protocol default runs `execute` once and buffers - /// everything, which is fine for a grid page and not for an export. + // An export walks the collection a page at a time instead of holding it whole. The protocol default runs + // `execute` once and buffers everything, fine for a grid page and not for an export. func streamRows(query: String) -> AsyncThrowingStream { guard let collectionName = WeaviateOperations.decodeExport(query) else { return defaultStreamRows(query: query) } - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return PluginRowStream.make { continuation, abort in let task = Task { do { - try await self.streamCollection(collectionName, continuation: continuation) + try await self.streamCollection(collectionName, abort: abort, continuation: continuation) continuation.finish() } catch { continuation.finish(throwing: error) } } - continuation.onTermination = { @Sendable _ in task.cancel() } + abort.onAbort { task.cancel() } } } private func streamCollection( _ name: String, + abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let client = try requireClient() @@ -75,11 +75,12 @@ extension WeaviatePluginDriver { ))) for try await objects in client.objectPages(collection: name, pageSize: Self.exportPageSize) { - continuation.yield(.rows(objects.map { object in + let rows: [PluginRow] = objects.map { object in WeaviateObjectCodec.row(for: object, columns: columns).map { value in value.map(PluginCellValue.text) ?? .null } - })) + } + await abort.send(rows, to: continuation) } } diff --git a/apple/TablePro/Core/Compare/CompareRowService.swift b/apple/TablePro/Core/Compare/CompareRowService.swift index 909a199607..fa4275de44 100644 --- a/apple/TablePro/Core/Compare/CompareRowService.swift +++ b/apple/TablePro/Core/Compare/CompareRowService.swift @@ -188,16 +188,20 @@ internal struct CompareRowService { String(localized: "The target driver cannot stream rows for a comparison.") ) } - let sides = RowSides( - source: sourceRead.provider(driver: sourcePlugin), - target: targetRead.provider(driver: targetPlugin), - resolver: KeyLookupResolver( - source: (sourceRead, sourcePlugin), - target: (targetRead, targetPlugin) - ), - targetDriver: targetPlugin - ) - return try await body(engine, sides) + // Lifted before the providers exist: a stream starts its statement when it is built, and the lift + // would queue behind that statement on the same connection. + return try await BulkReadTimeout.lifted(on: [sourceDriver, targetDriver]) { + let sides = RowSides( + source: sourceRead.provider(driver: sourcePlugin), + target: targetRead.provider(driver: targetPlugin), + resolver: KeyLookupResolver( + source: (sourceRead, sourcePlugin), + target: (targetRead, targetPlugin) + ), + targetDriver: targetPlugin + ) + return try await body(engine, sides) + } } } } diff --git a/apple/TablePro/Core/Compare/StreamingRowProvider.swift b/apple/TablePro/Core/Compare/StreamingRowProvider.swift index 54a4ac107f..309b918eb6 100644 --- a/apple/TablePro/Core/Compare/StreamingRowProvider.swift +++ b/apple/TablePro/Core/Compare/StreamingRowProvider.swift @@ -5,11 +5,9 @@ // Feeds the merge join from a driver's row stream. It holds one batch of rows // at a time, and hands them out one row at a time. // -// It is not what bounds a comparison's memory. A driver's stream buffers -// `.unbounded` and its producer starts at the moment the stream is built, not -// at the first read, so a side the walk consumes slowly runs ahead inside the -// stream's own buffer. The row limit, the filter, and the key order the two -// sides share are what keep a comparison's footprint down. +// It is not what bounds a comparison's memory either: a driver's stream holds +// at most two unread batches and its producer waits for the walk. The producer +// starts its statement when the stream is built, not at the first read. // import Foundation diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift index 5b6028255f..d5233c42a0 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift @@ -12,11 +12,11 @@ // // The read side is only as incremental as the driver is. `streamRows` has a // protocol default that calls `execute` and yields the whole result at once, -// which Dameng and Teradata still inherit, so on those two a large table is -// materialised before the first batch is written and Stop cannot land until -// it is. Every other SQL driver implements it for real. Export has the same -// property through the same call; a capability that lets this refuse rather -// than inherit would have to be declared by all 32 plugins. +// which Dameng still inherits, so there a large table is materialised before +// the first batch is written and Stop cannot land until it is. Every other SQL +// driver implements it for real, and reads only as fast as the writes take its +// rows. Export has the same property through the same call; a capability that +// lets this refuse rather than inherit would have to be declared by every plugin. // // This is the write path CSV and JSON import already use, through the same // `SQLStatementGenerator` and its per-engine bind-parameter ceiling. The one diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index f8c04fd656..336bd5ba68 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -702,8 +702,10 @@ internal struct ObjectCopyRunner { guard let sourcePlugin = CompareMetadataService.pluginDriver(from: sourceDriver) else { throw ObjectCopyError.refused(noSourceDriver) } - return try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in - progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) + return try await BulkReadTimeout.lifted(on: [sourceDriver]) { + try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in + progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) + } } } guard !outcome.cancelled else { @@ -804,8 +806,10 @@ internal struct ObjectCopyRunner { guard let sourcePlugin = CompareMetadataService.pluginDriver(from: sourceDriver) else { throw ObjectCopyError.refused(Self.noSourceDriver) } - let outcome = try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in - progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) + let outcome = try await BulkReadTimeout.lifted(on: [sourceDriver]) { + try await copier.copy(from: sourcePlugin, to: targetPlugin) { rows in + progress.setRowsForCurrentObject(rows, completedBefore: completedBefore) + } } return CopiedRows(outcome: outcome, joinedSessionTransaction: owner == .session) } diff --git a/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift b/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift index d3a8ecfef7..8247482ecf 100644 --- a/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift +++ b/apple/TablePro/Core/Plugins/QueryResultExportDataSource.swift @@ -172,32 +172,10 @@ final class QueryResultExportDataSource: PluginExportDataSource, @unchecked Send streamExportRows(table: object.name, databaseName: object.databaseName) } + // Pulled rather than pumped by a task of its own, which kept every filled chunk the writer had not taken yet. func streamExportRows(table: String, databaseName: String) -> AsyncThrowingStream { - let stream = streamRows(table: table, databaseName: databaseName) - let documents = exportDocuments - return AsyncThrowingStream { continuation in - let task = Task { - var offset = 0 - do { - for try await element in stream { - switch element { - case .header(let header): - continuation.yield(.header(header)) - case .rows(let rows): - let end = offset + rows.count - let aligned = end <= documents.count ? Array(documents[offset...AsyncIterator + private let documents: [PluginExportDocument?] + private var offset = 0 + + init(_ stream: AsyncThrowingStream, documents: [PluginExportDocument?]) { + iterator = stream.makeAsyncIterator() + self.documents = documents + } + + func next() async throws -> PluginExportStreamElement? { + guard let element = try await iterator.next() else { return nil } + switch element { + case .header(let header): + return .header(header) + case .rows(let rows): + let end = offset + rows.count + let aligned = end <= documents.count ? Array(documents[offset..( + on drivers: [DatabaseDriver], + _ operation: () async throws -> T + ) async throws -> T { + var restored: [(DatabaseDriver, Int)] = [] + for driver in drivers { + restored.append((driver, await restoredSeconds(for: driver))) + await apply(0, on: driver) + } + do { + let value = try await operation() + await restore(restored) + return value + } catch { + await restore(restored) + throw error + } + } + + // In a task of its own, so a Stop that cancelled the read still puts the timeout back on the pooled connection. + private static func restore(_ timeouts: [(DatabaseDriver, Int)]) async { + await Task { + for (driver, seconds) in timeouts { await apply(seconds, on: driver) } + }.value + } + + static func restoredSeconds(for driver: DatabaseDriver) async -> Int { + if let adapter = driver as? PluginDriverAdapter { return adapter.effectiveQueryTimeoutSeconds } + let configured = driver.connection.queryTimeoutSeconds + return await MainActor.run { + ConnectionTimeoutPolicy.effectiveQueryTimeoutSeconds( + configuredSeconds: configured, + globalSeconds: AppSettingsManager.shared.general.queryTimeoutSeconds + ) + } + } + + private static func apply(_ seconds: Int, on driver: DatabaseDriver) async { + do { + try await driver.applyQueryTimeout(seconds) + } catch { + logger.warning("Query timeout \(seconds) not applied for a bulk read: \(error.localizedDescription, privacy: .private)") + } + } +} diff --git a/apple/TablePro/Core/Services/Export/TableTransferService.swift b/apple/TablePro/Core/Services/Export/TableTransferService.swift index 8806cac233..63555de315 100644 --- a/apple/TablePro/Core/Services/Export/TableTransferService.swift +++ b/apple/TablePro/Core/Services/Export/TableTransferService.swift @@ -177,11 +177,13 @@ final class TableTransferService: ObservableObject { workload: .bulk, cancellation: .protectedWrite ) { destinationDriver in - try await self.transfer( - request: request, - sourceDriver: sourceDriver, - destinationDriver: destinationDriver - ) + try await BulkReadTimeout.lifted(on: [sourceDriver]) { + try await self.transfer( + request: request, + sourceDriver: sourceDriver, + destinationDriver: destinationDriver + ) + } } } } diff --git a/apple/TableProTests/Core/Services/BulkReadTimeoutTests.swift b/apple/TableProTests/Core/Services/BulkReadTimeoutTests.swift new file mode 100644 index 0000000000..2109f9ee73 --- /dev/null +++ b/apple/TableProTests/Core/Services/BulkReadTimeoutTests.swift @@ -0,0 +1,40 @@ +// +// BulkReadTimeoutTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct BulkReadTimeoutTests { + private struct Failure: Error {} + + @Test("A bulk read runs with the timeout off and puts the connection's own back") + func liftsAndRestores() async throws { + var connection = TestFixtures.makeConnection() + connection.queryTimeoutSeconds = 17 + let source = MockDatabaseDriver(connection: connection) + let target = MockDatabaseDriver() + let configured = AppSettingsManager.shared.general.queryTimeoutSeconds + + let value = try await BulkReadTimeout.lifted(on: [source, target]) { 42 } + + #expect(value == 42) + #expect(source.applyQueryTimeoutValues == [0, 17]) + #expect(target.applyQueryTimeoutValues == [0, configured]) + } + + @Test("A bulk read that fails still puts the timeout back") + func restoresAfterFailure() async { + var connection = TestFixtures.makeConnection() + connection.queryTimeoutSeconds = 9 + let source = MockDatabaseDriver(connection: connection) + + await #expect(throws: Failure.self) { + try await BulkReadTimeout.lifted(on: [source]) { () async throws -> Int in throw Failure() } + } + #expect(source.applyQueryTimeoutValues == [0, 9]) + } +} diff --git a/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift b/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift index 3e467e5515..d8c12d30d0 100644 --- a/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift +++ b/apple/TableProTests/Core/Services/Masking/MaskingExportTests.swift @@ -153,4 +153,41 @@ struct MaskingExportTests { #expect(rows == [[.text("1"), .text("••••")], [.text("2"), .text("••••")]]) } + + @Test("Export Results loads each chunk of server values during the writer's pull for it") + @MainActor + func exportResultsLoadsValuesAsTheWriterPulls() async throws { + let count = 120 + let tableRows = TableRows.from( + queryRows: (0..(initialState: []) + let source = QueryResultExportDataSource( + tableRows: tableRows, + databaseType: .mysql, + driver: nil, + fillUnloaded: { rows in + let pull = pulls.withLock { $0 } + pullsAtEachFill.withLock { $0.append(pull) } + return Dictionary(uniqueKeysWithValues: rows.map { ($0.id, [1: PluginCellValue.text("x")]) }) + } + ) + let stream = source.streamExportRows(table: "customers", databaseName: "") + + // Off the main actor, so a fill that ran ahead of the writer would not wait behind it. + try await Task.detached { + var iterator = stream.makeAsyncIterator() + while true { + pulls.withLock { $0 += 1 } + guard try await iterator.next() != nil else { break } + } + }.value + + // Pull 1 is the header; the three chunks of 50, 50 and 20 rows are filled during pulls 2, 3 and 4. + #expect(pullsAtEachFill.withLock { $0 } == [2, 3, 4]) + } } diff --git a/apple/scripts/probes/check-stream-backpressure.sh b/apple/scripts/probes/check-stream-backpressure.sh new file mode 100755 index 0000000000..94509602fc --- /dev/null +++ b/apple/scripts/probes/check-stream-backpressure.sh @@ -0,0 +1,244 @@ +#!/usr/bin/env bash +# +# Measure how much memory a driver's row stream holds while its consumer is slower than the source. +# +# Builds a harness from the driver's own sources, fills a table on a real server, then reads it through +# `streamRows` with a consumer that waits per batch, the way a copy waits on its target's inserts. It prints the +# process footprint before the read and its peak during it. A stream with backpressure peaks at a few batches; one +# without it holds every row the consumer has not taken yet. +# +# Usage: +# scripts/probes/check-stream-backpressure.sh [--rows N] [--delay-ms N] [--root DIR] +# +# engine: postgresql, mysql +# --rows rows in the probe table, default 1000000 +# --delay-ms the consumer's wait per batch, default 20 +# --root the apple/ folder whose sources to build, default this one; Libs comes from it too +# +# TP_STREAM_HOST, TP_STREAM_PORT, TP_STREAM_USER, TP_STREAM_PASSWORD and TP_STREAM_DATABASE name the server, by +# default a local one on the engine's port as the current user. TP_STREAM_BUILD_DIR keeps the harness between runs. +# Exits 1 when the read fails, 3 when the probe cannot run. + +set -uo pipefail + +ENGINE="${1:-}" +shift || true +ROWS=1000000 +DELAY_MS=20 +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +while [ $# -gt 0 ]; do + case "$1" in + --rows) ROWS="${2:?}"; shift 2 ;; + --delay-ms) DELAY_MS="${2:?}"; shift 2 ;; + --root) ROOT="$(cd "${2:?}" && pwd)"; shift 2 ;; + *) echo "unknown argument $1" >&2; exit 3 ;; + esac +done +PLUGINS="$ROOT/Plugins" + +case "$ENGINE" in + postgresql) + FOLDER=PostgreSQLDriverPlugin CLASS=PostgreSQLPlugin C_MODULE=CLibPQ + PRODUCTS="TableProCoreTypes TableProGeometry TableProKerberos TableProLogRedaction TableProPostgreSQLCore TableProSQLQuoting TableProSQLVocabulary TableProTLSClientIdentity" + LINK="$ROOT/Libs/libpq.a $ROOT/Libs/libpgcommon.a $ROOT/Libs/libpgport.a -lssl.3 -lcrypto.3 -lz -framework GSS -lcom_err" + PORT_DEFAULT=5432 DATABASE_DEFAULT=postgres + CREATE="CREATE TABLE tp_stream_probe AS SELECT g AS id, repeat(md5(g::text), 6) AS payload FROM generate_series(1, $ROWS) AS g" + ;; + mysql) + FOLDER=MySQLDriverPlugin CLASS=MySQLPlugin C_MODULE=CMariaDB + PRODUCTS="TableProCoreTypes TableProKerberos TableProLogRedaction TableProMySQLCore TableProSQLQuoting TableProSQLVocabulary" + LINK="$ROOT/Libs/libmariadb.a -lssl.3 -lcrypto.3 -liconv -lz" + PORT_DEFAULT=3306 DATABASE_DEFAULT=test + digits="(SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9)" + CREATE="CREATE TABLE tp_stream_probe AS SELECT id, REPEAT(MD5(id), 6) AS payload FROM (SELECT d0.n + d1.n * 10 + d2.n * 100 + d3.n * 1000 + d4.n * 10000 + d5.n * 100000 + d6.n * 1000000 + 1 AS id FROM $digits d0, $digits d1, $digits d2, $digits d3, $digits d4, $digits d5, $digits d6) AS numbers WHERE id <= $ROWS" + ;; + *) + echo "usage: $0 [--rows N] [--delay-ms N] [--root DIR]" >&2 + exit 3 + ;; +esac + +command -v swift > /dev/null || { echo "swift not found" >&2; exit 3; } +for library in $LINK; do + case "$library" in + /*) [ -f "$library" ] || { echo "not found: $library (run scripts/download-libs.sh)" >&2; exit 3; } ;; + esac +done + +HOST="${TP_STREAM_HOST:-127.0.0.1}" +PORT="${TP_STREAM_PORT:-$PORT_DEFAULT}" +USER_NAME="${TP_STREAM_USER:-$USER}" +PASSWORD="${TP_STREAM_PASSWORD:-}" +DATABASE="${TP_STREAM_DATABASE:-$DATABASE_DEFAULT}" +nc -z "$HOST" "$PORT" > /dev/null 2>&1 || { echo "no server at $HOST:$PORT" >&2; exit 3; } + +WORK="${TP_STREAM_BUILD_DIR:-}" +if [ -z "$WORK" ]; then + WORK="$(mktemp -d)" + trap 'rm -rf "$WORK"' EXIT +fi +SOURCES="$WORK/Sources/Probe" +rm -rf "$SOURCES" "$WORK/CModule" +mkdir -p "$SOURCES" + +while IFS= read -r source; do + ln -s "$source" "$SOURCES/driver_$(basename "$source")" +done < <(find "$PLUGINS/$FOLDER" -name '*.swift' -not -path '*/Tests/*') +for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox; do + [ -f "$PLUGINS/Shared/$shared.swift" ] && ln -s "$PLUGINS/Shared/$shared.swift" "$SOURCES/shared_$shared.swift" +done +ln -s "$PLUGINS/$FOLDER/$C_MODULE" "$WORK/CModule" + +dependencies='.product(name: "TableProPluginKit", package: "TableProCore"), + .product(name: "TableProSQLGrammar", package: "TableProCore"), + .product(name: "TableProProcess", package: "TableProCore"), + "'"$C_MODULE"'",' +for product in $PRODUCTS; do + dependencies="$dependencies + .product(name: \"$product\", package: \"TableProCore\")," +done +flags="\"-L$ROOT/Libs\", \"-L$ROOT/Libs/dylibs\", \"-Xlinker\", \"-rpath\", \"-Xlinker\", \"$ROOT/Libs/dylibs\"" +for flag in $LINK; do + flags="$flags, \"$flag\"" +done + +cat > "$WORK/Package.swift" << MANIFEST +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "StreamBackpressureProbe", + platforms: [.macOS(.v14)], + dependencies: [ + .package(path: "$ROOT/Packages/TableProCore"), + ], + targets: [ + .systemLibrary(name: "$C_MODULE", path: "CModule"), + .executableTarget( + name: "Probe", + dependencies: [ + $dependencies + ], + path: "Sources/Probe", + swiftSettings: [ + .swiftLanguageMode(.v6), + .enableUpcomingFeature("MemberImportVisibility"), + .unsafeFlags(["-Xcc", "-I$PLUGINS/$FOLDER/$C_MODULE/include"]), + ], + linkerSettings: [.unsafeFlags([$flags])] + ), + ] +) +MANIFEST + +cat > "$SOURCES/probe_Engine.swift" << SWIFT +typealias EnginePlugin = $CLASS +SWIFT + +cat > "$SOURCES/probe_Probe.swift" << 'SWIFT' +import Darwin +import Foundation +import TableProPluginKit + +@main +enum Probe { + static func main() async { + let arguments = CommandLine.arguments + guard arguments.count >= 8, let delay = Int(arguments[7]) else { + print("usage: Probe ") + exit(3) + } + let driver = EnginePlugin().createDriver(config: DriverConnectionConfig( + host: arguments[1], port: Int(arguments[2]) ?? 0, username: arguments[3], password: arguments[4], + database: arguments[5], additionalFields: [:] + )) + do { + try await driver.connect() + try await driver.applyQueryTimeout(0) + _ = try await driver.execute(query: "DROP TABLE IF EXISTS tp_stream_probe") + _ = try await driver.execute(query: arguments[6]) + } catch { + print("cannot prepare the table: \(error)") + exit(3) + } + + let sampler = FootprintSampler() + let baseline = sampler.start() + let started = Date() + var rows = 0 + var batches = 0 + var failure: Error? + do { + for try await element in driver.streamRows(query: "SELECT id, payload FROM tp_stream_probe ORDER BY id") { + guard case .rows(let batch) = element else { continue } + rows += batch.count + batches += 1 + try await Task.sleep(for: .milliseconds(delay)) + } + } catch { + failure = error + } + let peak = sampler.stop() + _ = try? await driver.execute(query: "DROP TABLE tp_stream_probe") + driver.disconnect() + + let megabytes = { (bytes: UInt64) in String(format: "%.1f MB", Double(bytes) / 1_048_576) } + print("rows read: \(rows) in \(batches) batches, \(String(format: "%.1f", Date().timeIntervalSince(started))) s") + print("footprint before: \(megabytes(baseline))") + print("footprint peak: \(megabytes(peak))") + print("peak over baseline: \(megabytes(peak > baseline ? peak - baseline : 0))") + if let failure { + print("read failed: \(failure)") + exit(1) + } + } +} + +final class FootprintSampler: @unchecked Sendable { + private let lock = NSLock() + private var peak: UInt64 = 0 + private var running = false + + func start() -> UInt64 { + let baseline = Self.footprint() + lock.withLock { + peak = baseline + running = true + } + Thread.detachNewThread { [self] in + while lock.withLock({ running }) { + let now = Self.footprint() + lock.withLock { peak = max(peak, now) } + usleep(10_000) + } + } + return baseline + } + + func stop() -> UInt64 { + let now = Self.footprint() + return lock.withLock { + running = false + return max(peak, now) + } + } + + static func footprint() -> UInt64 { + var info = task_vm_info_data_t() + var count = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size) + let result = withUnsafeMutablePointer(to: &info) { pointer in + pointer.withMemoryRebound(to: integer_t.self, capacity: Int(count)) { + task_info(mach_task_self_, task_flavor_t(TASK_VM_INFO), $0, &count) + } + } + return result == KERN_SUCCESS ? info.phys_footprint : 0 + } +} +SWIFT + +echo "building the $ENGINE harness from $ROOT (the first build takes a few minutes)" +(cd "$WORK" && swift build -c release > "$WORK/build.log" 2>&1) || { + grep -E "error:" "$WORK/build.log" | head -30 >&2 + exit 3 +} +"$WORK/.build/release/Probe" "$HOST" "$PORT" "$USER_NAME" "$PASSWORD" "$DATABASE" "$CREATE" "$DELAY_MS" From e39da565e40d5415f4decb6b2cf6a368da4ba751 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 12:26:59 +0700 Subject: [PATCH 2/5] docs(changelog): add the row stream fragments --- apple/changelog.d/4227.changed.md | 1 + apple/changelog.d/4227.fixed.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 apple/changelog.d/4227.changed.md create mode 100644 apple/changelog.d/4227.fixed.md diff --git a/apple/changelog.d/4227.changed.md b/apple/changelog.d/4227.changed.md new file mode 100644 index 0000000000..7d1626af07 --- /dev/null +++ b/apple/changelog.d/4227.changed.md @@ -0,0 +1 @@ +Table Transfer, Object Copy and Data Compare read their rows without the query timeout, as exports do. diff --git a/apple/changelog.d/4227.fixed.md b/apple/changelog.d/4227.fixed.md new file mode 100644 index 0000000000..089dcee292 --- /dev/null +++ b/apple/changelog.d/4227.fixed.md @@ -0,0 +1 @@ +Copying, transferring, comparing or exporting a large table no longer holds every row it has not written in memory. From 07a67d7c7edcd392465ed6406f67b1c8c6b16712 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 19:58:25 +0700 Subject: [PATCH 3/5] refactor(plugins): pace streams through the bounded stream API main ships, abort only when the consumer leaves --- apple/Packages/TableProCore/Package.swift | 2 +- .../DuckDBFetchBudget.swift | 28 ++ .../Execution/SpannerExecutionTypes.swift | 2 - .../Execution/SpannerExecutor+Reads.swift | 55 ++-- .../Execution/SpannerExecutor.swift | 41 ++- .../Wire/SpannerRESTClient+HTTP.swift | 50 ++-- .../Wire/URLSessionSpannerTransport.swift | 77 +---- .../TrinoStatementClient.swift | 57 ++-- .../DuckDBBoundedStreamTests.swift | 44 +++ .../PluginRowStreamDeliveryTests.swift | 87 ++++++ .../PluginRowStreamPacingTests.swift | 269 ----------------- .../PluginRowStreamTerminationTests.swift | 101 +++++++ .../BigQueryPluginDriver+Execution.swift | 9 +- .../BigtablePluginDriver+Execution.swift | 14 +- .../CassandraConnection.swift | 4 +- .../CassandraPlugin.swift | 2 +- .../ClickHousePlugin.swift | 51 +--- .../DuckDBDriverPlugin/DuckDBConnection.swift | 20 +- .../DuckDBDriverPlugin/DuckDBPlugin.swift | 14 +- .../DynamoDBPluginDriver+Execution.swift | 13 +- .../ElasticsearchPluginDriver+Execution.swift | 33 +-- .../ElasticsearchPluginDriver.swift | 2 - .../EtcdDriverPlugin/EtcdPluginDriver.swift | 11 +- .../FirebasePluginDriver+Execution.swift | 12 +- .../HanaDriverPlugin/HanaPluginDriver.swift | 22 +- .../InfluxDBPluginDriver+Stream.swift | 4 +- .../LibSQLPluginDriver.swift | 9 +- .../SQLiteLocalBackend.swift | 9 +- .../MSSQLDriverPlugin/FreeTDSConnection.swift | 71 ++--- .../MSSQLDriverPlugin/MSSQLPlugin.swift | 40 ++- .../MemcachedPluginDriver.swift | 7 +- .../MongoDBConnection+SyncHelpers.swift | 43 +-- .../MongoDBConnection.swift | 19 +- .../MongoDBPluginDriver.swift | 11 +- .../MariaDBPluginConnection.swift | 21 +- .../MySQLDriverPlugin/MySQLPluginDriver.swift | 17 +- .../Neo4jDriverPlugin/Neo4jPluginDriver.swift | 8 +- .../OracleDriverPlugin/OraclePlugin.swift | 4 +- .../LibPQPluginConnection.swift | 18 +- .../QdrantPluginDriver+Execution.swift | 7 +- .../RedisDriverPlugin/RedisPluginDriver.swift | 16 +- .../SQLiteAgentBackend.swift | 5 +- .../SQLiteExecutionBackend.swift | 22 +- .../SQLiteDriverPlugin/SQLitePlugin.swift | 15 +- .../SnowflakePluginDriver.swift | 6 +- .../SpannerPluginDriver+Execution.swift | 26 +- .../TableProPluginKit/PluginStreamAbort.swift | 276 ++++++------------ .../TeradataDriverPlugin/TeradataPlugin.swift | 9 +- .../TrinoDriverPlugin/TrinoPluginDriver.swift | 20 +- .../TypesensePluginDriver+Execution.swift | 9 +- .../WeaviatePluginDriver+Execution.swift | 7 +- .../Core/Compare/StreamingRowProvider.swift | 8 +- .../Core/ObjectCopy/ObjectCopyRowCopier.swift | 6 +- .../Execution/TransactionEngineFamily.swift | 4 +- 54 files changed, 739 insertions(+), 998 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift delete mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamPacingTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamTerminationTests.swift diff --git a/apple/Packages/TableProCore/Package.swift b/apple/Packages/TableProCore/Package.swift index 5f6954d8bc..a59c9a69de 100644 --- a/apple/Packages/TableProCore/Package.swift +++ b/apple/Packages/TableProCore/Package.swift @@ -556,7 +556,7 @@ let package = Package( ), .testTarget( name: "TableProPluginKitTests", - dependencies: ["TableProPluginKit", "TableProSQLQuoting", "TableProTestSupport"], + dependencies: ["TableProPluginKit", "TableProSQLQuoting"], path: "Tests/TableProPluginKitTests" ), .testTarget( diff --git a/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift b/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift index 8153f6b233..322b9aee1d 100644 --- a/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift +++ b/apple/Packages/TableProCore/Sources/TableProDuckDBCore/DuckDBFetchBudget.swift @@ -55,3 +55,31 @@ public enum DuckDBRenderSettings { } } } + +// Hands a row batch to a stream whose buffer is bounded. A buffering policy alone drops what does not fit, so the +// producer keeps the refused batch and offers it again once the consumer has read one. +public enum DuckDBBoundedStream { + public static let bufferedBatches = 64 + + // Returns false when the stream has ended and the producer should stop. + public static func deliver( + _ element: Element, + to continuation: AsyncThrowingStream.Continuation, + pause: () -> Void = { _ = usleep(2_000) } + ) -> Bool { + var pending = element + while true { + switch continuation.yield(pending) { + case .enqueued: + return true + case .dropped(let refused): + pending = refused + pause() + case .terminated: + return false + @unknown default: + return false + } + } + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift index 954956a27e..77b8aa3ee9 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutionTypes.swift @@ -33,8 +33,6 @@ public enum SpannerStreamOutput: Sendable, Equatable { case rows([[SpannerCell]]) } -public typealias SpannerStreamEmit = @Sendable (SpannerStreamOutput) async -> Void - public enum SpannerExecutionError: Error, Sendable, Equatable { case transactionAlreadyOpen case noTransactionOpen diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift index cdbbe3de62..b0d8bf8dff 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor+Reads.swift @@ -3,29 +3,24 @@ import Foundation extension SpannerExecutor { func queryStream(_ statement: SpannerPreparedStatement) -> AsyncThrowingStream { makeStream { continuation in - try await self.streamQuery(statement) { continuation.yield($0) } + if await self.transactions.isIdle { + try await self.streamOnReadSession(statement, into: continuation) + return + } + try await self.transactions.queue.run { + switch try await self.transactions.claim() { + case .idle: + try await self.streamOnReadSession(statement, into: continuation) + case .open(let context): + try await self.streamInTransaction(statement, context: context, into: continuation) + } + } } } func readStream(_ statement: SpannerPreparedStatement) -> AsyncThrowingStream { makeStream { continuation in - try await self.streamOnReadSession(statement) { continuation.yield($0) } - } - } - - // The response is read on only once `emit` returns, so a caller that waits in it stops the read where it is. - func streamQuery(_ statement: SpannerPreparedStatement, into emit: @escaping SpannerStreamEmit) async throws { - if await transactions.isIdle { - try await streamOnReadSession(statement, into: emit) - return - } - try await transactions.queue.run { - switch try await self.transactions.claim() { - case .idle: - try await self.streamOnReadSession(statement, into: emit) - case .open(let context): - try await self.streamInTransaction(statement, context: context, into: emit) - } + try await self.streamOnReadSession(statement, into: continuation) } } @@ -50,16 +45,16 @@ extension SpannerExecutor { private func streamOnReadSession( _ statement: SpannerPreparedStatement, - into emit: SpannerStreamEmit + into continuation: AsyncThrowingStream.Continuation ) async throws { let resolved = try await queryParameterTypes(for: statement) var progress = SpannerStreamProgress() do { - try await streamReadAttempt(statement, types: resolved.types, progress: &progress, into: emit) + try await streamReadAttempt(statement, types: resolved.types, progress: &progress, into: continuation) } catch let error as SpannerAPIError where progress.isPristine && resolved.cached && error.isInvalidArgument { await typeCache.evict(statement.sql) let rediscovered = try await queryParameterTypes(for: statement) - try await streamReadAttempt(statement, types: rediscovered.types, progress: &progress, into: emit) + try await streamReadAttempt(statement, types: rediscovered.types, progress: &progress, into: continuation) } } @@ -67,30 +62,30 @@ extension SpannerExecutor { _ statement: SpannerPreparedStatement, types: [SpannerType], progress: inout SpannerStreamProgress, - into emit: SpannerStreamEmit + into continuation: AsyncThrowingStream.Continuation ) async throws { let request = try executeRequest(statement, types: types, transaction: .singleUseStrongReadOnly, seqno: nil) let session = try await sessions.readSessionName() do { - try await forward(try await client.executeStreamingSql(session: session, request), progress: &progress, into: emit) + try await forward(try await client.executeStreamingSql(session: session, request), progress: &progress, into: continuation) } catch let error as SpannerAPIError where error.isSessionNotFound && progress.isPristine { await sessions.discardReadSession(session) let fresh = try await sessions.readSessionName() - try await forward(try await client.executeStreamingSql(session: fresh, request), progress: &progress, into: emit) + try await forward(try await client.executeStreamingSql(session: fresh, request), progress: &progress, into: continuation) } } private func streamInTransaction( _ statement: SpannerPreparedStatement, context: SpannerTransactionContext, - into emit: SpannerStreamEmit + into continuation: AsyncThrowingStream.Continuation ) async throws { let types = try await queryParameterTypes(for: statement).types let request = try executeRequest(statement, types: types, transaction: .id(context.transactionId), seqno: nil) var progress = SpannerStreamProgress() do { let events = try await client.executeStreamingSql(session: context.session, request) - try await forward(events, progress: &progress, into: emit) + try await forward(events, progress: &progress, into: continuation) } catch let error as SpannerAPIError where error.isAborted || error.isSessionNotFound { await transactions.markAborted(context) throw error @@ -100,24 +95,24 @@ extension SpannerExecutor { private func forward( _ events: AsyncThrowingStream, progress: inout SpannerStreamProgress, - into emit: SpannerStreamEmit + into continuation: AsyncThrowingStream.Continuation ) async throws { for try await event in events { switch event { case .metadata(let metadata): progress.fields = metadata.fields progress.headerSent = true - await emit(.header(metadata.fields)) + continuation.yield(.header(metadata.fields)) case .rows(let rows): progress.rowsSent = true - await emit(.rows(SpannerValueDecoder.rows(rows, fields: progress.fields))) + continuation.yield(.rows(SpannerValueDecoder.rows(rows, fields: progress.fields))) case .stats: continue } } if !progress.headerSent { progress.headerSent = true - await emit(.header([])) + continuation.yield(.header([])) } } diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift index 41ff438da2..b8019f082b 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Execution/SpannerExecutor.swift @@ -84,24 +84,21 @@ public final class SpannerExecutor: Sendable { } } - // Each output goes to `emit`, and a query's response is read on only once `emit` returns. - public func stream(_ sql: String, hostParameters: [SpannerCell]?, into emit: @escaping SpannerStreamEmit) async throws { - try ensureOpen() - try await stream(prepare(sql, hostParameters: hostParameters), into: emit) + public func stream(_ sql: String, hostParameters: [SpannerCell]?) -> AsyncThrowingStream { + do { + try ensureOpen() + return stream(try prepare(sql, hostParameters: hostParameters)) + } catch { + return AsyncThrowingStream { $0.finish(throwing: error) } + } } - public func stream(_ statement: SpannerRenderedStatement, into emit: @escaping SpannerStreamEmit) async throws { - try ensureOpen() + public func stream(_ statement: SpannerRenderedStatement) -> AsyncThrowingStream { let prepared = SpannerPreparedStatement(statement) guard case .query = SpannerStatementClassifier.classify(prepared.sql, dialect: dialect) else { - let outcome = try await run(statement) - await emit(.header(outcome.fields)) - if !outcome.rows.isEmpty { - await emit(.rows(outcome.rows)) - } - return + return outcomeStream(statement) } - try await streamQuery(prepared, into: emit) + return queryStream(prepared) } public func read(_ statement: SpannerRenderedStatement) async throws -> [[SpannerCell]] { @@ -204,6 +201,24 @@ public final class SpannerExecutor: Sendable { return try await body(try await sessions.readSessionName()) } } + + private func outcomeStream(_ statement: SpannerRenderedStatement) -> AsyncThrowingStream { + AsyncThrowingStream { continuation in + let task = Task { + do { + let outcome = try await self.run(statement) + continuation.yield(.header(outcome.fields)) + if !outcome.rows.isEmpty { + continuation.yield(.rows(outcome.rows)) + } + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in task.cancel() } + } + } } internal struct SpannerPreparedStatement: Sendable { diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift index ff1bdfd648..c3719e4e64 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/SpannerRESTClient+HTTP.swift @@ -61,13 +61,30 @@ internal extension SpannerRESTClient { return (response, chunks) } - // Pulled: a chunk is read and decoded only when the reader wants an event past the ones already decoded. static func events( from chunks: AsyncThrowingStream, httpStatus: Int ) -> AsyncThrowingStream { - let source = SpannerEventSource(chunks: chunks, httpStatus: httpStatus) - return AsyncThrowingStream(unfolding: { try await source.next() }) + AsyncThrowingStream { continuation in + let producer = Task { + do { + var decoder = SpannerStreamDecoder(httpStatus: httpStatus) + for try await chunk in chunks { + for event in try decoder.consume(chunk) { + continuation.yield(event) + } + } + try Task.checkCancellation() + for event in try decoder.finish() { + continuation.yield(event) + } + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { _ in producer.cancel() } + } } func encode(_ value: Value) throws -> Data { @@ -125,33 +142,6 @@ internal extension SpannerRESTClient { } } -// One reader pulls the events, so one call at a time reaches the decoder. -private final class SpannerEventSource: @unchecked Sendable { - private var chunks: AsyncThrowingStream.AsyncIterator - private var decoder: SpannerStreamDecoder - private var pending: [SpannerStreamEvent] = [] - private var isFinished = false - - init(chunks: AsyncThrowingStream, httpStatus: Int) { - self.chunks = chunks.makeAsyncIterator() - decoder = SpannerStreamDecoder(httpStatus: httpStatus) - } - - func next() async throws -> SpannerStreamEvent? { - while pending.isEmpty { - guard !isFinished else { return nil } - if let chunk = try await chunks.next() { - pending = try decoder.consume(chunk) - } else { - try Task.checkCancellation() - isFinished = true - pending = try decoder.finish() - } - } - return pending.removeFirst() - } -} - internal struct SpannerDatabaseResponse: Decodable { let databaseDialect: String? } diff --git a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift index 4aec0fb27c..6e12b3a72d 100644 --- a/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift +++ b/apple/Packages/TableProCore/Sources/TableProSpannerCore/Wire/URLSessionSpannerTransport.swift @@ -2,9 +2,6 @@ import Foundation public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Sendable { private let session: URLSession - // A streamed read holds one response for as long as its reader takes, and a suspended task still counts toward - // the resource limit, so streams run on a session that keeps the system default. - private let streamSession: URLSession private let requestTimeout: @Sendable () -> TimeInterval private let lock = NSLock() private var isClosed = false @@ -22,13 +19,6 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send delegate: SpannerRedirectRefusingDelegate(), delegateQueue: nil ) - // The session made its own copy of the configuration above. - configuration.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource - self.streamSession = URLSession( - configuration: configuration, - delegate: SpannerRedirectRefusingDelegate(), - delegateQueue: nil - ) self.requestTimeout = requestTimeout } @@ -65,7 +55,7 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send receiver.awaitResponse(continuation) do { try startTask(cancellation: cancellation) { - let task = self.streamSession.dataTask(with: prepared) + let task = self.session.dataTask(with: prepared) task.delegate = receiver return task } @@ -76,8 +66,7 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send } onCancel: { cancellation.cancel() } - let reader = SpannerResponseBody(body, receiver: receiver, cancellation: cancellation) - return (response, AsyncThrowingStream(unfolding: { try await reader.next() })) + return (response, body) } public func close() async { @@ -88,7 +77,6 @@ public final class URLSessionSpannerTransport: SpannerTransport, @unchecked Send } guard shouldInvalidate else { return } session.finishTasksAndInvalidate() - streamSession.finishTasksAndInvalidate() } private func prepare(_ request: URLRequest) -> URLRequest { @@ -155,46 +143,12 @@ internal final class SpannerRedirectRefusingDelegate: NSObject, URLSessionTaskDe } } -// One reader pulls the body, so one call at a time reaches the iterator. The transfer runs only while that reader -// waits for its next piece, so a reader that stops reading stops the download instead of piling the body up. -private final class SpannerResponseBody: @unchecked Sendable { - private var iterator: AsyncThrowingStream.AsyncIterator - private let receiver: SpannerStreamingTaskDelegate - private let cancellation: SpannerTaskCancellation - - init( - _ body: AsyncThrowingStream, - receiver: SpannerStreamingTaskDelegate, - cancellation: SpannerTaskCancellation - ) { - iterator = body.makeAsyncIterator() - self.receiver = receiver - self.cancellation = cancellation - } - - // A reader that walks away would otherwise leave the transfer suspended, holding the connection. - deinit { - cancellation.cancel() - } - - func next() async throws -> Data? { - receiver.resumeTransfer() - let piece = try await iterator.next() - if piece != nil { - receiver.pieceTaken() - } - return piece - } -} - internal final class SpannerStreamingTaskDelegate: NSObject, URLSessionDataDelegate, @unchecked Sendable { static let maximumChunkSize = 64 * 1_024 private let lock = NSLock() private let body: AsyncThrowingStream.Continuation private var responseContinuation: CheckedContinuation? - private var suspendedTask: URLSessionDataTask? - private var unreadPieces = 0 init(body: AsyncThrowingStream.Continuation) { self.body = body @@ -224,25 +178,13 @@ internal final class SpannerStreamingTaskDelegate: NSObject, URLSessionDataDeleg return .allow } - // Counted and suspended under the lock before the reader sees any piece, so its resume can never come first. func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { - var pieces: [Data] = [] var start = data.startIndex while start < data.endIndex { let end = data.index(start, offsetBy: Self.maximumChunkSize, limitedBy: data.endIndex) ?? data.endIndex - pieces.append(Data(data[start.. CheckedContinuation? { lock.withLock { () -> CheckedContinuation? in defer { responseContinuation = nil } diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift index c7bbbb086b..c13edc6ba4 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift @@ -1,6 +1,11 @@ import Foundation import os +public enum TrinoStreamElement: Sendable { + case columns([TrinoColumnDescriptor]) + case rows([[TrinoValue]]) +} + public final class TrinoStatementClient: @unchecked Sendable { private let transport: TrinoTransport private let config: TrinoClientConfig @@ -48,18 +53,26 @@ public final class TrinoStatementClient: @unchecked Sendable { ) } - // The next page is asked for only once `onPage` returns, so a caller that waits there holds the query where it is. - // A cancelled caller stops at the next request and sends Trino the DELETE. - public func executeStreamed( - _ sql: String, - onColumns: ([TrinoColumnDescriptor]) async -> Void, - onPage: ([[TrinoValue]]) async -> Void - ) async throws { - _ = try await runStatement( - sql, - onColumns: { await onColumns(self.descriptors(from: $0)) }, - onPage: onPage - ) + /// The paging loop runs in an unstructured task, so terminating the stream has to cancel it + /// explicitly. Without that the loop keeps fetching pages nobody reads, and its own + /// `abortIfCancelled` never fires because nothing ever cancels the task it runs in. + public func executeStreamed(_ sql: String) -> AsyncThrowingStream { + AsyncThrowingStream { continuation in + let client = self + let statementTask = Task { + do { + _ = try await client.runStatement( + sql, + onColumns: { continuation.yield(.columns(client.descriptors(from: $0))) }, + onPage: { continuation.yield(.rows($0)) } + ) + continuation.finish() + } catch { + continuation.finish(throwing: error) + } + } + continuation.onTermination = { @Sendable _ in statementTask.cancel() } + } } /// Stops every statement running on this client, not only the one that started last: the app @@ -80,8 +93,8 @@ public final class TrinoStatementClient: @unchecked Sendable { private func runStatement( _ sql: String, - onColumns: ([TrinoColumn]) async -> Void, - onPage: ([[TrinoValue]]) async -> Void + onColumns: ([TrinoColumn]) -> Void, + onPage: ([[TrinoValue]]) -> Void ) async throws -> StatementOutcome { guard let statementURL = config.statementURL else { throw TrinoError.invalidConfiguration("Invalid \(config.flavor.productName) server URL") @@ -105,8 +118,8 @@ public final class TrinoStatementClient: @unchecked Sendable { _ statement: TrinoRunningStatement, url statementURL: URL, sql: String, - onColumns: ([TrinoColumn]) async -> Void, - onPage: ([[TrinoValue]]) async -> Void + onColumns: ([TrinoColumn]) -> Void, + onPage: ([[TrinoValue]]) -> Void ) async throws -> StatementOutcome { var httpResponse = try await sendWithRetry( makeRequest(method: .post, url: statementURL, headers: initialHeaders(), body: Data(sql.utf8)), @@ -121,10 +134,10 @@ public final class TrinoStatementClient: @unchecked Sendable { var columns = results.columns var columnsEmitted = false if let columns { - await onColumns(columns) + onColumns(columns) columnsEmitted = true } - await emitRows(results, columns: columns, onPage: onPage) + emitRows(results, columns: columns, onPage: onPage) var updateType = results.updateType var updateCount = results.updateCount let queryId = results.id @@ -146,10 +159,10 @@ public final class TrinoStatementClient: @unchecked Sendable { columns = results.columns } if !columnsEmitted, let columns { - await onColumns(columns) + onColumns(columns) columnsEmitted = true } - await emitRows(results, columns: columns, onPage: onPage) + emitRows(results, columns: columns, onPage: onPage) if let type = results.updateType { updateType = type } @@ -162,7 +175,7 @@ public final class TrinoStatementClient: @unchecked Sendable { return StatementOutcome(updateType: updateType, updateCount: updateCount, queryId: queryId) } - private func emitRows(_ results: TrinoQueryResults, columns: [TrinoColumn]?, onPage: ([[TrinoValue]]) async -> Void) async { + private func emitRows(_ results: TrinoQueryResults, columns: [TrinoColumn]?, onPage: ([[TrinoValue]]) -> Void) { guard let data = results.data, let columns, !data.isEmpty else { return } var page: [[TrinoValue]] = [] page.reserveCapacity(data.count) @@ -175,7 +188,7 @@ public final class TrinoStatementClient: @unchecked Sendable { } page.append(decoded) } - await onPage(page) + onPage(page) } private func descriptors(from columns: [TrinoColumn]) -> [TrinoColumnDescriptor] { diff --git a/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift b/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift new file mode 100644 index 0000000000..1aa3be5881 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProDuckDBCoreTests/DuckDBBoundedStreamTests.swift @@ -0,0 +1,44 @@ +import Foundation +import TableProDuckDBCore +import Testing + +@Test func aSlowConsumerReceivesEveryBatchThroughASmallBuffer() async throws { + let (stream, continuation) = AsyncThrowingStream.makeStream(bufferingPolicy: .bufferingOldest(2)) + Thread.detachNewThread { + for value in 0..<500 { + guard DuckDBBoundedStream.deliver(value, to: continuation, pause: { _ = usleep(100) }) else { break } + } + continuation.finish() + } + + var received: [Int] = [] + for try await value in stream { + received.append(value) + } + #expect(received == Array(0..<500)) +} + +@Test func theProducerWaitsWhileTheBufferIsFull() { + let (stream, continuation) = AsyncThrowingStream.makeStream(bufferingPolicy: .bufferingOldest(2)) + #expect(DuckDBBoundedStream.deliver(1, to: continuation)) + #expect(DuckDBBoundedStream.deliver(2, to: continuation)) + + var pauses = 0 + let delivered = DuckDBBoundedStream.deliver(3, to: continuation) { + pauses += 1 + if pauses == 3 { continuation.finish() } + } + #expect(!delivered) + #expect(pauses == 3) + _ = stream +} + +@Test func theProducerStopsWhenTheStreamIsGone() { + var stream: AsyncThrowingStream? + var held: AsyncThrowingStream.Continuation? + stream = AsyncThrowingStream(bufferingPolicy: .bufferingOldest(1)) { held = $0 } + let continuation = held + #expect(continuation.map { DuckDBBoundedStream.deliver(1, to: $0) } == true) + stream = nil + #expect(continuation.map { DuckDBBoundedStream.deliver(2, to: $0, pause: {}) } == false) +} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift new file mode 100644 index 0000000000..bf8ad65eaf --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift @@ -0,0 +1,87 @@ +// +// PluginRowStreamDeliveryTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing + +struct PluginRowStreamDeliveryTests { + private static func rows(_ value: Int) -> PluginStreamElement { + .rows([[.text(String(value))]]) + } + + private static func values(of element: PluginStreamElement) -> [String] { + guard case .rows(let rows) = element else { return [] } + return rows.compactMap { $0.first?.asText } + } + + @Test("A producer on its own thread waits for room and loses no batch") + func blockingDeliveryKeepsEveryBatch() async throws { + let total = PluginRowStream.boundedBufferBatches * 4 + let stream = PluginRowStream.makeBounded { continuation, _ in + Thread.detachNewThread { + for value in 0...makeStream() + var stream: AsyncThrowingStream? = PluginRowStream.makeBounded { continuation, _ in + Thread.detachNewThread { + var delivered = 0 + while PluginRowStream.deliverBlocking(Self.rows(delivered), to: continuation) { delivered += 1 } + ended.continuation.yield(delivered) + ended.continuation.finish() + } + } + var iterator = stream?.makeAsyncIterator() + _ = try await iterator?.next() + iterator = nil + stream = nil + var delivered = Int.max + for await count in ended.stream { delivered = count } + #expect(delivered <= PluginRowStream.boundedBufferBatches + 1) + } + + @Test("A blocked producer gives up when asked to stop, and the batch is not sent") + func blockingDeliveryHonoursStop() { + let (stream, continuation) = AsyncThrowingStream.makeStream( + bufferingPolicy: .bufferingOldest(1) + ) + #expect(PluginRowStream.deliverBlocking(Self.rows(0), to: continuation, stops: { true })) + #expect(!PluginRowStream.deliverBlocking(Self.rows(1), to: continuation, stops: { true })) + _ = stream + } + + @Test("A producer running as a task suspends for room and loses no batch") + func asyncDeliveryKeepsEveryBatch() async throws { + let total = PluginRowStream.boundedBufferBatches * 3 + let stream = PluginRowStream.makeBounded { continuation, _ in + Task { + for value in 0.. [PluginRow] { - (0..? = PluginRowStream.make { continuation, abort in - queue.async { - continuation.yield(.header(Self.header)) - for index in 0..<40 { - progress.entering(index) - abort.send(Self.batch(index), to: continuation) - if abort.isAborted { break } - } - continuation.finish() - progress.finish() - } - } - - var iterator = stream?.makeAsyncIterator() - _ = try await iterator?.next() - _ = try await iterator?.next() - try await progress.waitUntilEntered(2) - - iterator = nil - stream = nil - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 2) - } - - @Test("A task producer suspends while the consumer has two batches unread") - func taskProducerWaitsForTheConsumer() async throws { - let progress = ProducerProgress() - var stream: AsyncThrowingStream? = PluginRowStream.make { continuation, abort in - let task = Task { - continuation.yield(.header(Self.header)) - for index in 0..<40 { - progress.entering(index) - await abort.send(Self.batch(index), to: continuation) - if abort.isAborted { break } - } - continuation.finish() - progress.finish() - } - abort.onAbort { task.cancel() } - } - - var iterator = stream?.makeAsyncIterator() - _ = try await iterator?.next() - _ = try await iterator?.next() - try await progress.waitUntilEntered(2) - - iterator = nil - stream = nil - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 2) - } - - @Test("A consumer cancelled between reads stops a waiting producer while the stream is still held") - func cancelledConsumerStopsAWaitingProducer() async throws { - let queue = DispatchQueue(label: "test.stream.pacing.cancel") - let progress = ProducerProgress() - let stream = PluginRowStream.make { continuation, abort in - queue.async { - continuation.yield(.header(Self.header)) - for index in 0..<40 { - progress.entering(index) - abort.send(Self.batch(index), to: continuation) - if abort.isAborted { break } - } - continuation.finish() - progress.finish() - } - } - - let reader = Task { - var iterator = stream.makeAsyncIterator() - _ = try await iterator.next() - _ = try await iterator.next() - try await progress.waitUntilEntered(2) - withUnsafeCurrentTask { $0?.cancel() } - return try await iterator.next() == nil - } - - #expect(try await reader.value) - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 2) - _ = stream - } - - @Test("Every row arrives once and in order through a paced stream") - func pacedStreamDeliversEveryRow() async throws { - let queue = DispatchQueue(label: "test.stream.pacing.order") - let stream = PluginRowStream.make { continuation, abort in - queue.async { - continuation.yield(.header(Self.header)) - for index in 0..<25 { - abort.send(Self.batch(index, size: 3), to: continuation) - } - continuation.yield(.rows(Self.batch(25, size: 1))) - continuation.finish() - } - } - - var cells: [String] = [] - var sawHeader = false - for try await element in stream { - switch element { - case .header: sawHeader = true - case .rows(let rows): cells += rows.map { $0.first?.asText ?? "" } - } - } - let expected = (0..<25).flatMap { index in (0..<3).map { "row_\(index)_\($0)" } } + ["row_25_0"] - #expect(sawHeader) - #expect(cells == expected) - } - - @Test("A producer the consumer never reads reads on once its pause limit passes") - func pauseLimitLetsTheProducerReadOn() async throws { - let queue = DispatchQueue(label: "test.stream.pacing.pause") - let progress = ProducerProgress() - let stream = PluginRowStream.make { continuation, abort in - queue.async { - continuation.yield(.header(Self.header)) - for index in 0..<6 { - progress.entering(index) - abort.send(Self.batch(index), to: continuation, maximumPause: 0.01) - } - continuation.finish() - progress.finish() - } - } - - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 5) - _ = stream - } - - @Test("A task producer reads on once its pause limit passes") - func asyncPauseLimitLetsTheProducerReadOn() async throws { - let progress = ProducerProgress() - let stream = PluginRowStream.make { continuation, abort in - Task { - continuation.yield(.header(Self.header)) - for index in 0..<6 { - progress.entering(index) - await abort.send(Self.batch(index), to: continuation, maximumPause: 0.01) - } - continuation.finish() - progress.finish() - } - } - - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 5) - _ = stream - } - - @Test("A document stream waits for its consumer and keeps each batch's documents with its rows") - func exportStreamPacesAndKeepsDocuments() async throws { - let queue = DispatchQueue(label: "test.stream.pacing.documents") - let progress = ProducerProgress() - var stream: AsyncThrowingStream? = PluginRowStream.makeExport { continuation, abort in - queue.async { - continuation.yield(.header(Self.header)) - for index in 0..<40 { - progress.entering(index) - let rows = Self.batch(index, size: 2) - let documents = rows.map { row in - PluginExportDocument(fields: [PluginExportField( - name: "c", canonicalJSON: "\"\(row[0].asText ?? "")\"", typeName: "string", cellValue: row[0] - )]) - } - abort.send(rows, documents: documents, to: continuation) - if abort.isAborted { break } - } - continuation.finish() - progress.finish() - } - } - - var iterator = stream?.makeAsyncIterator() - _ = try await iterator?.next() - let first = try await iterator?.next() - guard case .rows(let rows, let documents)? = first else { - Issue.record("expected rows") - return - } - #expect(rows.count == 2) - #expect(documents?.count == 2) - try await progress.waitUntilEntered(2) - - iterator = nil - stream = nil - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 2) - } - - @Test("A batch a bounded buffer drops is not counted against the consumer", .timeLimit(.minutes(1))) - func droppedBatchDoesNotParkTheProducer() async throws { - let queue = DispatchQueue(label: "test.stream.pacing.dropped") - let progress = ProducerProgress() - let stream = PluginRowStream.make(bufferingPolicy: .bufferingOldest(1)) { continuation, abort in - queue.async { - for index in 0..<6 { - progress.entering(index) - abort.send(Self.batch(index), to: continuation) - } - continuation.finish() - progress.finish() - } - } - - var batches = 0 - for try await element in stream { - if case .rows = element { batches += 1 } - } - try await progress.waitUntilFinished() - #expect(progress.highestEntered == 5) - #expect(batches >= 1) - } - - @Test("An abort made outside a paced stream never waits") - func unpacedAbortNeverWaits() { - let (stream, continuation) = AsyncThrowingStream.makeStream() - let abort = PluginStreamAbort() - for index in 0..<10 { - abort.send(Self.batch(index), to: continuation) - } - continuation.finish() - _ = stream - } -} - -private final class ProducerProgress: @unchecked Sendable { - private let lock = NSLock() - private var entered = -1 - private var finished = false - - var highestEntered: Int { - lock.withLock { entered } - } - - func entering(_ index: Int) { - lock.withLock { entered = max(entered, index) } - } - - func finish() { - lock.withLock { finished = true } - } - - func waitUntilEntered(_ index: Int) async throws { - try await waitUntil { self.lock.withLock { self.entered >= index } } - } - - func waitUntilFinished() async throws { - try await waitUntil { self.lock.withLock { self.finished } } - } -} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamTerminationTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamTerminationTests.swift new file mode 100644 index 0000000000..8797f7a68b --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamTerminationTests.swift @@ -0,0 +1,101 @@ +import Foundation +import TableProPluginKit +import Testing + +struct PluginRowStreamTerminationTests { + private static let header = PluginStreamHeader(columns: ["c"], columnTypeNames: ["TEXT"]) + + @Test("A stream its producer finished runs no abort action, even once the consumer lets it go") + func finishedStreamRunsNoAbortAction() async throws { + let probe = AbortProbe() + var stream: AsyncThrowingStream? = PluginRowStream.make { continuation, abort in + probe.watch(abort) + continuation.yield(.header(Self.header)) + continuation.yield(.rows([[.text("a")]])) + continuation.finish() + } + var elements = 0 + if let stream { + for try await _ in stream { elements += 1 } + } + stream = nil + + #expect(elements == 2) + #expect(probe.actions == 0) + #expect(probe.abort?.isAborted == false) + } + + @Test("A stream its producer finished with an error runs no abort action") + func failedStreamRunsNoAbortAction() async { + let probe = AbortProbe() + var stream: AsyncThrowingStream? = PluginRowStream.make { continuation, abort in + probe.watch(abort) + continuation.yield(.header(Self.header)) + continuation.finish(throwing: ProducerFailure()) + } + await #expect(throws: ProducerFailure.self) { + if let stream { + for try await _ in stream {} + } + } + stream = nil + + #expect(probe.actions == 0) + } + + @Test("A consumer that lets the stream go before its end runs the abort actions") + func droppedStreamRunsTheAbortActions() async throws { + let probe = AbortProbe() + var stream: AsyncThrowingStream? = PluginRowStream.make { continuation, abort in + probe.watch(abort) + continuation.yield(.header(Self.header)) + } + var iterator = stream?.makeAsyncIterator() + _ = try await iterator?.next() + iterator = nil + stream = nil + + #expect(probe.actions == 1) + #expect(probe.abort?.isAborted == true) + } + + @Test("A consumer task cancelled while it waits runs the abort actions") + func cancelledConsumerRunsTheAbortActions() async throws { + let probe = AbortProbe() + let stream = PluginRowStream.make { continuation, abort in + probe.watch(abort) + continuation.yield(.header(Self.header)) + } + let reader = Task { + for try await _ in stream {} + } + reader.cancel() + _ = try? await reader.value + + #expect(probe.actions == 1) + _ = stream + } +} + +private struct ProducerFailure: Error {} + +private final class AbortProbe: @unchecked Sendable { + private let lock = NSLock() + private var watched: PluginStreamAbort? + private var count = 0 + + var abort: PluginStreamAbort? { + lock.withLock { watched } + } + + var actions: Int { + lock.withLock { count } + } + + func watch(_ abort: PluginStreamAbort) { + lock.withLock { watched = abort } + abort.onAbort { [self] in + lock.withLock { count += 1 } + } + } +} diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift index 3552ff05f5..64979b93ea 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift @@ -83,11 +83,11 @@ extension BigQueryPluginDriver { } func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let streamTask = Task { do { try await self.runningStatements.run { - try await self.performStreamRows(query: query, abort: abort, continuation: continuation) + try await self.performStreamRows(query: query, continuation: continuation) } } catch { continuation.finish(throwing: BigQueryError.wrap(error)) @@ -225,7 +225,6 @@ extension BigQueryPluginDriver { private func performStreamRows( query: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let conn = try requireConnection() @@ -259,7 +258,7 @@ extension BigQueryPluginDriver { let firstRows = BigQueryTypeMapper.flattenRows(from: firstPage, schema: schema) if !firstRows.isEmpty { - await abort.send(firstRows, to: continuation) + _ = await PluginRowStream.deliver(.rows(firstRows), to: continuation) } var pageToken = firstPage.pageToken @@ -272,7 +271,7 @@ extension BigQueryPluginDriver { ) let nextRows = BigQueryTypeMapper.flattenRows(from: nextPage, schema: schema) if !nextRows.isEmpty { - await abort.send(nextRows, to: continuation) + _ = await PluginRowStream.deliver(.rows(nextRows), to: continuation) } pageToken = nextPage.pageToken } diff --git a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift index b140a05cb9..71d84b3060 100644 --- a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift +++ b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift @@ -149,11 +149,11 @@ extension BigtablePluginDriver { // MARK: - Export func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { if case .browse(let spec)? = try? BigtableStatement.parse(query), spec.limit == 0 { - try await self.export(spec, abort: abort, continuation: continuation) + try await self.export(spec, continuation: continuation) } else { let result = try await self.execute(query: query) var header = PluginStreamHeader( @@ -172,12 +172,8 @@ extension BigtablePluginDriver { } } - // The columns come from the first page; a column only later rows hold goes into `__extra__`. - private func export( - _ spec: BigtableBrowseSpec, - abort: PluginStreamAbort, - continuation: AsyncThrowingStream.Continuation - ) async throws { + /// The columns come from the first page; a column only later rows hold goes into `__extra__`. + private func export(_ spec: BigtableBrowseSpec, continuation: AsyncThrowingStream.Continuation) async throws { let client = try requireClient() var header: [String]? var after: Data? @@ -194,7 +190,7 @@ extension BigtablePluginDriver { ))) } if !rows.isEmpty { - await abort.send(page.rows(alignedTo: columns).map { $0.map(Self.cell) }, to: continuation) + _ = await PluginRowStream.deliver(.rows(page.rows(alignedTo: columns).map { $0.map(Self.cell) }), to: continuation) } after = rows.count == Self.exportPageSize ? rows.last?.key : nil } while after != nil diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift index a22d2e5f9a..e3b4a7d825 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift @@ -959,7 +959,7 @@ actor CassandraConnectionActor { return "Unknown error" } - // One non-suspending actor job, blocked in cass_future_wait and in `abort.send`, so the consumer's departure + // One non-suspending actor job, blocked in cass_future_wait and in `deliverBlocking`, so the consumer's departure // reaches it only through `abort`, read between pages and between rows rather than mid-request. func streamQuery( _ cql: String, @@ -1064,7 +1064,7 @@ actor CassandraConnectionActor { cass_result_free(result) if !page.isEmpty, !aborted { - abort.send(page, to: continuation) + _ = PluginRowStream.deliverBlocking(.rows(page), to: continuation) } if !hasMore || aborted { break } diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index ed359ea39d..f11d9e7f63 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -483,7 +483,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen return streamBrowse(browse, consistency: consistency) } let cql = stripTrailingSemicolon(query) - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in let streamTask = Task { do { try await self.connectionActor.streamQuery( diff --git a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift index 0202c3840a..5166531fd1 100644 --- a/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift +++ b/apple/Plugins/ClickHouseDriverPlugin/ClickHousePlugin.swift @@ -188,12 +188,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked private var _serverProfile = ClickHouseServerProfile.bootstrap let lock = NSLock() - var session: URLSession? { - didSet { replaceStreamingSession() } - } - // A streamed read holds one response for as long as its consumer takes, and a suspended task still counts toward - // the resource limit, so streams run on a copy of `session` that keeps the system default. - private var streamingSession: URLSession? + var session: URLSession? var currentTask: URLSessionDataTask? var _currentDatabase: String var _lastQueryId: String? @@ -338,16 +333,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked lock.unlock() } - // Called from `session`'s didSet, so it runs under whatever lock the write holds. - private func replaceStreamingSession() { - streamingSession?.invalidateAndCancel() - streamingSession = session.map { base in - let configuration = base.configuration - configuration.timeoutIntervalForResource = URLSessionConfiguration.default.timeoutIntervalForResource - return URLSession(configuration: configuration, delegate: base.delegate, delegateQueue: nil) - } - } - func ping() async throws { _ = try await execute(query: "SELECT 1") } @@ -603,7 +588,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked query: query, rowCap: rowCap, isOutputCut: isOutputCut, - abort: abort, continuation: continuation ) } catch { @@ -628,11 +612,10 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked query: String, rowCap: Int, isOutputCut: OSAllocatedUnfairLock, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let queryId = UUID().uuidString - let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId, paced: false) + let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId) var request = try buildRequest( query: query, database: database, @@ -647,7 +630,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked session: session, batchSize: min(5_000, rowCap + 1), boundedOutputCut: isOutputCut, - abort: abort, continuation: continuation ) } @@ -667,7 +649,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked session: URLSession, batchSize: Int, boundedOutputCut: OSAllocatedUnfairLock? = nil, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { var decoder = ClickHouseTabSeparatedRowDecoder() @@ -715,7 +696,7 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } batch.append(contentsOf: rows) guard batch.count >= batchSize else { return } - await abort.send(batch, to: continuation, maximumPause: Self.unreadResponsePause) + continuation.yield(.rows(batch)) batch.removeAll(keepingCapacity: true) } } catch is OtherFormatBodyFull {} @@ -755,9 +736,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked /// value cut, so reading stops at the first chunk over it. private static let otherFormatByteCap = 1_048_576 - // The server ends a response it could not send for http_send_timeout, 30 s by default. - private static let unreadResponsePause: TimeInterval = 10 - private struct OtherFormatBodyFull: Error {} func streamRows(query: String) -> AsyncThrowingStream { @@ -769,31 +747,27 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } private func stream(_ query: String, readOnly: Bool) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in let streamTask = Task { do { - try await self.performStreamRows( - query: query, - readOnly: readOnly, - abort: abort, - continuation: continuation - ) + try await self.performStreamRows(query: query, readOnly: readOnly, continuation: continuation) } catch { continuation.finish(throwing: error) } } - abort.onAbort { streamTask.cancel() } + continuation.onTermination = { @Sendable _ in + streamTask.cancel() + } } } private func performStreamRows( query: String, readOnly: Bool, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let queryId = UUID().uuidString - let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId, paced: true) + let (session, database, executionTimeLimit) = try streamingContext(queryId: queryId) var request = try buildRequest( query: query, database: database, @@ -809,7 +783,6 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked request: request, session: session, batchSize: 5_000, - abort: abort, continuation: continuation ) } catch { @@ -818,13 +791,11 @@ nonisolated final class ClickHousePluginDriver: PluginDatabaseDriver, @unchecked } } - // A capped grid read finishes as fast as it arrives, so only the paced stream leaves the one-hour session cap. private func streamingContext( - queryId: String, - paced: Bool + queryId: String ) throws -> (session: URLSession, database: String, executionTimeLimit: Int?) { try lock.withLock { - guard let session = paced ? self.streamingSession : self.session else { throw ClickHouseError.notConnected } + guard let session = self.session else { throw ClickHouseError.notConnected } _lastQueryId = queryId return (session, _currentDatabase, acceptedExecutionTimeLimit) } diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift index 2361fa993c..017eed05f2 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBConnection.swift @@ -206,8 +206,7 @@ actor DuckDBConnectionActor { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { try ensureOpen() guard let conn = connection else { @@ -217,7 +216,7 @@ actor DuckDBConnectionActor { noteActivity(query) var opened = try openResult(query: query, parameters: [], connection: conn, unread: .abandonWhenIdle) defer { duckdb_destroy_result(&opened.result) } - try streamResultRows(opened, continuation: continuation, abort: abort) + try streamResultRows(opened, continuation: continuation) } // The state check, the BEGIN, the statement and the ROLLBACK share one actor turn, so nothing lands in between. @@ -234,14 +233,13 @@ actor DuckDBConnectionActor { func streamReadOnly( _ query: String, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { let conn = try readOnlyConnection() try DuckDBReadOnlyStatement.run(on: conn, session: sessionTransactionState()) { var opened = try openResult(query: query, parameters: [], connection: conn, unread: .abandon) defer { duckdb_destroy_result(&opened.result) } - try streamResultRows(opened, continuation: continuation, abort: abort) + try streamResultRows(opened, continuation: continuation) } } @@ -685,8 +683,7 @@ actor DuckDBConnectionActor { private func streamResultRows( _ opened: OpenedResult, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { continuation.yield(.header(PluginStreamHeader( columns: opened.schema.names, @@ -698,11 +695,8 @@ actor DuckDBConnectionActor { let reachedEnd: Bool do { reachedEnd = try readRows(of: opened) { batch in - if !Task.isCancelled, !abort.isAborted { - // Blocks the actor's thread: an await here would let another call reach the connection mid-result. - abort.send(batch, to: continuation) - } - cancelled = Task.isCancelled || abort.isAborted + cancelled = Task.isCancelled + || !DuckDBBoundedStream.deliver(PluginStreamElement.rows(batch), to: continuation) return !cancelled } } catch is CancellationError { diff --git a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift index 70be57ed1a..88d68c71c3 100644 --- a/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift +++ b/apple/Plugins/DuckDBDriverPlugin/DuckDBPlugin.swift @@ -760,30 +760,28 @@ final class DuckDBPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func streamRows(query: String) -> AsyncThrowingStream { let actor = connectionActor - return PluginRowStream.make { continuation, abort in - let task = Task { + return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(DuckDBBoundedStream.bufferedBatches)) { continuation in + Task { do { - try await actor.streamQuery(query, continuation: continuation, abort: abort) + try await actor.streamQuery(query, continuation: continuation) } catch { continuation.finish(throwing: error) } } - abort.onAbort { task.cancel() } } } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { guard context.readOnly, context.origin == .external else { return streamRows(query: query) } let actor = connectionActor - return PluginRowStream.make { continuation, abort in - let task = Task { + return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(DuckDBBoundedStream.bufferedBatches)) { continuation in + Task { do { - try await actor.streamReadOnly(query, continuation: continuation, abort: abort) + try await actor.streamReadOnly(query, continuation: continuation) } catch { continuation.finish(throwing: error) } } - abort.onAbort { task.cancel() } } } diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift index 834ea37b91..4fd1021ed5 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift @@ -344,11 +344,11 @@ extension DynamoDBPluginDriver { // the items go to a temporary file while their attributes are collected and the rows follow once the read ends. // The rows are read back from the file only as fast as the consumer takes them. func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { try await self.run { session in - try await self.spool(query, session: session, continuation: continuation, abort: abort) + try await self.spool(query, session: session, continuation: continuation) } continuation.finish() } catch { @@ -362,8 +362,7 @@ extension DynamoDBPluginDriver { private func spool( _ text: String, session: Session, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) async throws { let statement = try DynamoDBStatement.parse(text) let source: SpoolSource @@ -415,7 +414,7 @@ extension DynamoDBPluginDriver { for start in stride(from: 0, to: slice.count, by: 1_000) { try Task.checkCancellation() let batch = slice[start.. AsyncThrowingStream { guard let index = ElasticsearchOperations.decodeExport(query) else { return defaultStreamRows(query: query) } - return PluginRowStream.make { continuation, abort in + return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in let task = Task { do { - try await self.streamIndex(index, abort: abort, continuation: continuation) + try await self.streamIndex(index, continuation: continuation) continuation.finish() } catch { continuation.finish(throwing: error) } } - abort.onAbort { task.cancel() } + continuation.onTermination = { @Sendable _ in task.cancel() } } } - // A point-in-time pins the index for the whole export, so a document written meanwhile cannot shift the sort and - // be exported twice or skipped. `search_after` walks it, the only way past the 10,000 `max_result_window` ceiling. + /// A point-in-time pins the index for the whole export, so a document written while it runs + /// cannot shift the sort and be exported twice or skipped. `search_after` walks it, which is the + /// only way past the 10,000 `max_result_window` ceiling. private func streamIndex( _ index: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let conn = connection else { throw ElasticsearchError.notConnected } @@ -98,8 +100,9 @@ extension ElasticsearchPluginDriver { let hits = extractHits(response) guard !hits.isEmpty else { break } - // The columns are settled once, from the mapping and the first batch: a stream has one header, so a later - // batch holding a new field must not widen it. + /// The columns are settled once, from the mapping plus the first batch, because a + /// stream has one header and a later batch holding a field the first did not must not + /// widen it. if columns == nil { let resolved = ElasticsearchMappingFlattener.columns(forHits: hits, mappingColumns: mappingColumns) columns = resolved @@ -110,13 +113,9 @@ extension ElasticsearchPluginDriver { ))) } if let resolved = columns { - await abort.send( - ElasticsearchMappingFlattener.rows( - forHits: hits, columns: resolved, nestedParents: nestedParents, length: .whole - ), - to: continuation, - maximumPause: Self.unreadPagePause - ) + continuation.yield(.rows(ElasticsearchMappingFlattener.rows( + forHits: hits, columns: resolved, nestedParents: nestedParents, length: .whole + ))) } searchAfter = hits.last?["sort"] as? [Any] diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift index 24857a3614..bf836cfae5 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift @@ -21,8 +21,6 @@ internal final class ElasticsearchPluginDriver: PluginDatabaseDriver, @unchecked static let maxResultWindow = 10_000 static let deepPageBatchSize = 1_000 static let pitKeepAlive = "1m" - // The point in time expires `pitKeepAlive` after the last page that asked for it. - static let unreadPagePause: TimeInterval = 20 var connection: ElasticsearchConnection? { lock.withLock { _connection } } diff --git a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift index 5c972b8bd6..bcf8aef47a 100644 --- a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift +++ b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift @@ -185,10 +185,10 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, context: context, abort: abort, continuation: continuation) + try await self.performStreamRows(query: query, context: context, continuation: continuation) } catch { continuation.finish(throwing: error) } @@ -201,7 +201,6 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func performStreamRows( query: String, context: PluginStatementContext, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let client = httpClient else { @@ -216,7 +215,6 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { sortAscending: parsed.sortAscending, filter: parsed.filter, client: client, - abort: abort, continuation: continuation ) return @@ -239,7 +237,6 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { sortAscending: Bool, filter: EtcdRowFilter, client: EtcdHttpClient, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let matcher = try filter.matcher() @@ -277,7 +274,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { client: client, beforeNextBatch: { guard !unsent.isEmpty else { return } - await abort.send(unsent, to: continuation) + _ = await PluginRowStream.deliver(.rows(unsent), to: continuation) unsent = [] }, body: { kvs in @@ -292,7 +289,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { )) } if !unsent.isEmpty { - continuation.yield(.rows(unsent)) + _ = await PluginRowStream.deliver(.rows(unsent), to: continuation) } continuation.finish() } diff --git a/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift b/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift index 102daad44e..590fe67aad 100644 --- a/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift +++ b/apple/Plugins/FirebaseDriverPlugin/FirebasePluginDriver+Execution.swift @@ -234,11 +234,11 @@ extension FirebasePluginDriver { // MARK: - Export func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { if case .browse(let spec)? = try? FirebaseStatement.parse(query), spec.limit == 0 { - try await self.export(spec, abort: abort, continuation: continuation) + try await self.export(spec, continuation: continuation) } else { let result = try await self.execute(query: query) continuation.yield(.header(PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames, @@ -254,11 +254,7 @@ extension FirebasePluginDriver { } } - private func export( - _ spec: FirebaseBrowseSpec, - abort: PluginStreamAbort, - continuation: AsyncThrowingStream.Continuation - ) async throws { + private func export(_ spec: FirebaseBrowseSpec, continuation: AsyncThrowingStream.Continuation) async throws { var header: [String]? var types: [String] = [] try await forEachExportPage(spec) { columns, columnTypes, rows in @@ -268,7 +264,7 @@ extension FirebasePluginDriver { continuation.yield(.header(PluginStreamHeader(columns: header ?? [], columnTypeNames: types, estimatedRowCount: nil))) } let fixed = header ?? [] - await abort.send(rows.map { row in Self.align(row, columns: columns, to: fixed) }, to: continuation) + _ = await PluginRowStream.deliver(.rows(rows.map { row in Self.align(row, columns: columns, to: fixed) }), to: continuation) } if header == nil { continuation.yield(.header(PluginStreamHeader(columns: [Self.extraColumn], columnTypeNames: ["map"], estimatedRowCount: nil))) diff --git a/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift b/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift index 1cad25b867..7ea6ea662c 100644 --- a/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift +++ b/apple/Plugins/HanaDriverPlugin/HanaPluginDriver.swift @@ -181,12 +181,12 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { report: HanaStreamReport, refusesCutValues: Bool ) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let driver = self let producer = Task { do { try await driver.produce( - sql, into: continuation, abort: abort, report: report, refusesCutValues: refusesCutValues + sql, into: continuation, report: report, refusesCutValues: refusesCutValues ) continuation.finish() } catch { @@ -202,18 +202,17 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func produce( _ sql: String, into continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort, report: HanaStreamReport, refusesCutValues: Bool ) async throws { if HanaExplainStatement.explainedStatement(in: sql) != nil { let result = try await run(sql, parameters: nil, rowCap: nil) - continuation.yield(.header(PluginStreamHeader( + _ = await PluginRowStream.deliver(.header(PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames - ))) + )), to: continuation) if !result.rows.isEmpty { - continuation.yield(.rows(result.rows)) + _ = await PluginRowStream.deliver(.rows(result.rows), to: continuation) } return } @@ -225,10 +224,10 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { cancellation: query.cancellation, onHeader: { header in report.record(header) - continuation.yield(.header(PluginStreamHeader( + _ = PluginRowStream.deliverBlocking(.header(PluginStreamHeader( columns: header.columns, columnTypeNames: header.columnTypeNames - ))) + )), to: continuation, stops: { query.cancellation.isCancelled }) }, onBatch: { batch in report.record(truncatedLargeObjects: batch.truncatedLobCount) @@ -238,7 +237,12 @@ final class HanaPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return } // Runs on the session's own queue, which waits here while the reader is behind. - abort.send(HanaResultMapping.pluginRows(batch.rows), to: continuation) + let delivered = PluginRowStream.deliverBlocking( + .rows(HanaResultMapping.pluginRows(batch.rows)), + to: continuation, + stops: { query.cancellation.isCancelled } + ) + if !delivered { _ = query.cancellation.cancel() } } ) } catch let failure as HanaBridgeFailure { diff --git a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift index 7d741c6122..4524dde441 100644 --- a/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift +++ b/apple/Plugins/InfluxDBDriverPlugin/InfluxDBPluginDriver+Stream.swift @@ -7,7 +7,7 @@ import TableProPluginKit extension InfluxDBPluginDriver { func streamRows(query: String) -> AsyncThrowingStream { guard let remembered = streamedGrid(query) else { return collectedStream(query) } - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in let producer = Task { do { try await self.streamGrid(remembered, abort: abort, continuation: continuation) @@ -68,7 +68,7 @@ extension InfluxDBPluginDriver { continuation.yield(.header(PluginStreamHeader(columns: batch.columns, columnTypeNames: batch.typeNames))) } if !batch.rows.isEmpty { - await abort.send(Self.cells(batch.rows), to: continuation) + _ = await PluginRowStream.deliver(.rows(Self.cells(batch.rows)), to: continuation) } } } diff --git a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift index 85bff80962..2b0fbfb0bc 100644 --- a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift +++ b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift @@ -283,10 +283,10 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // MARK: - Streaming func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, continuation: continuation, abort: abort) + try await self.performStreamRows(query: query, continuation: continuation) } catch { continuation.finish(throwing: error) } @@ -298,8 +298,7 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func performStreamRows( query: String, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) async throws { switch getBackend() { case .remote(let client): @@ -322,7 +321,7 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish() case .local(let localBackend): - try await localBackend.streamQuery(query, continuation: continuation, abort: abort) + try await localBackend.streamQuery(query, continuation: continuation) case nil: throw LibSQLError.notConnected } diff --git a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift index c9222e8bbd..8994f7d575 100644 --- a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift +++ b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift @@ -151,8 +151,7 @@ actor SQLiteLocalBackend { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { guard let db else { throw LibSQLError.notConnected @@ -181,7 +180,7 @@ actor SQLiteLocalBackend { while stepResult == SQLITE_ROW { if Task.isCancelled { if !batch.isEmpty { - continuation.yield(.rows(batch)) + _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } continuation.finish(throwing: CancellationError()) return @@ -190,14 +189,14 @@ actor SQLiteLocalBackend { batch.append(rowValues(of: statement, count: columnCount)) if batch.count >= batchSize { // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. - abort.send(batch, to: continuation) + _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) } if !batch.isEmpty { - continuation.yield(.rows(batch)) + _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } do { diff --git a/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift b/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift index 1af6220e4a..1f982adca7 100644 --- a/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift +++ b/apple/Plugins/MSSQLDriverPlugin/FreeTDSConnection.swift @@ -685,40 +685,38 @@ nonisolated final class FreeTDSConnection: @unchecked Sendable { return MSSQLRequestEnding(sessionAnswer: answer.errors.isEmpty ? answer.resultSets.first : nil) } + /// `isAborted` is how a consumer that stopped reaches this loop. It is a closure rather than a + /// PluginKit type because this file is compiled into the iOS app too and may not import + /// PluginKit. It is polled instead of `Task.isCancelled`, which is always false here: the body + /// runs inside a bare `queue.async` with no task context. A consumer that stops ends the request + /// the way a capped read does. + /// + /// Only the request's first result set is streamed. A later one is read past, so the statements behind it still + /// run, and a server error anywhere in the request finishes the stream with that error. func streamQuery( _ query: String, isAborted: (@Sendable () -> Bool)? = nil, continuation: AsyncThrowingStream.Continuation - ) async throws { - do { - try await streamQuery(query, isAborted: isAborted, onElement: { continuation.yield($0) }) - continuation.finish() - } catch { - continuation.finish(throwing: error) - } - } - - // `isAborted` is a closure because the iOS app compiles this file without PluginKit, and is polled because - // `Task.isCancelled` is always false on the bare queue. Only the first result set streams; later ones are read - // past. `onElement` runs on the connection queue, so a caller that waits in it for its reader holds the session. - func streamQuery( - _ query: String, - isAborted: (@Sendable () -> Bool)? = nil, - onElement: @escaping @Sendable (MSSQLStreamElement) -> Void ) async throws { let queryToRun = String(query) let call = requestGate.enqueue() try await withTaskCancellationHandler { try await freetdsDispatchAsync(on: queue) { [self] in - let readout = try self.readSync( - queryToRun, - call: call, - plan: .stream, - sink: .stream(onElement), - isAborted: isAborted - ) - if let error = readout.errors.first { - throw MSSQLCoreError.queryFailed(error.message.text) + do { + let readout = try self.readSync( + queryToRun, + call: call, + plan: .stream, + sink: .stream(continuation), + isAborted: isAborted + ) + if let error = readout.errors.first { + continuation.finish(throwing: MSSQLCoreError.queryFailed(error.message.text)) + return + } + continuation.finish() + } catch { + continuation.finish(throwing: error) } } } onCancel: { [weak self] in @@ -878,7 +876,7 @@ nonisolated private struct FreeTDSReadPlan: Sendable { nonisolated private enum FreeTDSRowSink { case buffer - case stream(@Sendable (MSSQLStreamElement) -> Void) + case stream(AsyncThrowingStream.Continuation) } /// Reads one request to its end, or ends it early, so the connection is never left with results nobody will read. @@ -1001,8 +999,8 @@ nonisolated private struct FreeTDSBatchReader { private mutating func readResultSet(columnCount: Int, errorsBefore: Int) throws -> ResultSetEnd { let descriptors = (1...columnCount).map(describeColumn) - if case .stream(let emit) = sink { - emit(.header(columns: descriptors)) + if case .stream(let continuation) = sink { + _ = PluginRowStream.deliverBlocking(.header(columns: descriptors), to: continuation, stops: readerStopped) headerStreamed = true } @@ -1125,21 +1123,26 @@ nonisolated private struct FreeTDSBatchReader { } } + // A reader that left or a Stop is handled at the top of the row loop. private func streamIfFull(_ rows: inout [[MSSQLRawCell]]) { - guard case .stream(let emit) = sink, rows.count >= MSSQLRowLimits.streamBatchSize else { return } - emit(.rows(rows)) + guard case .stream(let continuation) = sink, rows.count >= MSSQLRowLimits.streamBatchSize else { return } + guard PluginRowStream.deliverBlocking(.rows(rows), to: continuation, stops: readerStopped) else { return } rows.removeAll(keepingCapacity: true) } private func streamRemaining(_ rows: inout [[MSSQLRawCell]]) { - guard case .stream(let emit) = sink, !rows.isEmpty else { return } - emit(.rows(rows)) + guard case .stream(let continuation) = sink, !rows.isEmpty else { return } + _ = PluginRowStream.deliverBlocking(.rows(rows), to: continuation, stops: readerStopped) rows.removeAll() } private func finishCountOnlyStream() { - guard case .stream(let emit) = sink, !headerStreamed, errorTotal == 0 else { return } - emit(.affectedRows(rowsAffected)) + guard case .stream(let continuation) = sink, !headerStreamed, errorTotal == 0 else { return } + _ = PluginRowStream.deliverBlocking(.affectedRows(rowsAffected), to: continuation, stops: readerStopped) + } + + private func readerStopped() -> Bool { + userCancelled() || consumerStopped() } private mutating func absorbMessages() { diff --git a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift index 057d4a4d80..daab01e389 100644 --- a/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift +++ b/apple/Plugins/MSSQLDriverPlugin/MSSQLPlugin.swift @@ -700,28 +700,47 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { guard let conn = freeTDSConn else { return AsyncThrowingStream { $0.finish(throwing: MSSQLPluginError.notConnected) } } - return PluginRowStream.make { continuation, abort in - // Never cancelled: cancelling reaches `cancelCurrentQuery`, which sends an attention whatever the session - // holds, while the `isAborted` poll ends the request the way `MSSQLRequestEnding` allows. - Task { + return PluginRowStream.makeBounded { continuation, abort in + let streamTask = Task { + let coreStream = AsyncThrowingStream( + bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches) + ) { coreContinuation in + /// A plain `Task {}` inherits context but is not a child, so cancelling the + /// outer task never reaches this one. The abort closure is what does. + Task { + do { + try await conn.streamQuery( + query, + isAborted: { abort.isAborted }, + continuation: coreContinuation + ) + } catch let error as MSSQLCoreError { + coreContinuation.finish(throwing: MSSQLPluginError(coreError: error)) + } catch { + coreContinuation.finish(throwing: error) + } + } + } do { - try await conn.streamQuery(query, isAborted: { abort.isAborted }, onElement: { element in + for try await element in coreStream { + let delivered: Bool switch element { case .header(let columns): - continuation.yield(.header(PluginStreamHeader( + delivered = await PluginRowStream.deliver(.header(PluginStreamHeader( columns: columns.map { $0.name }, columnTypeNames: columns.map { $0.type.canonicalName }, estimatedRowCount: nil - ))) + )), to: continuation) case .rows(let batch): let pluginRows: [PluginRow] = batch.map { row in row.map { $0.asPluginCell } } - abort.send(pluginRows, to: continuation) + delivered = await PluginRowStream.deliver(.rows(pluginRows), to: continuation) case .affectedRows: - break + delivered = true } - }) + guard delivered else { break } + } continuation.finish() } catch let error as MSSQLCoreError { continuation.finish(throwing: MSSQLPluginError(coreError: error)) @@ -729,6 +748,7 @@ final class MSSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } + abort.onAbort { streamTask.cancel() } } } diff --git a/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift index 04ba24935b..c86b077cb8 100644 --- a/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift +++ b/apple/Plugins/MemcachedDriverPlugin/MemcachedPluginDriver.swift @@ -334,11 +334,11 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func streamRows(query: String) -> AsyncThrowingStream { let statement = query.trimmingCharacters(in: .whitespacesAndNewlines) let spec = MemcachedBrowseSpec.refusalMessage(statement) == nil ? MemcachedBrowseSpec.parse(statement) : nil - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in let task = Task { do { if let spec { - try await self.streamBrowse(spec, abort: abort, into: continuation) + try await self.streamBrowse(spec, into: continuation) } else { let result = try await self.execute(query: query) var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) @@ -357,7 +357,6 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func streamBrowse( _ spec: MemcachedBrowseSpec, - abort: PluginStreamAbort, into continuation: AsyncThrowingStream.Continuation ) async throws { let page = try await browsePage(spec, on: try liveConnection()) @@ -375,7 +374,7 @@ final class MemcachedPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let batch = page.entries[start ..< end] let values = try await self.values(of: batch.map(\.key), on: connection) let rows = self.rows(of: batch, values: values, serverTime: page.serverTime) - if !rows.isEmpty { await abort.send(rows, to: continuation) } + if !rows.isEmpty { _ = await PluginRowStream.deliver(.rows(rows), to: continuation) } start = end } } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift index 0e6dd9f4d1..55c8df7418 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift @@ -415,27 +415,13 @@ nonisolated extension MongoDBConnection { return (docs: results, isTruncated: truncated) } - private static let streamBatchSize = 1_000 - - // The server reaps a cursor that sees no getMore for cursorTimeoutMillis, 10 min by default. A server batch of - // `serverBatchSize` documents drains in at most 10 pauses, so a stalled consumer still asks within that limit. - private static let idleCursorPause: TimeInterval = 30 - private static let serverBatchSize: UInt32 = 10_000 - func iterateCursorStreaming( cursor: OpaquePointer, continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort, streamState: MongoStreamState, census: () throws -> MongoFieldCensus? ) { defer { cleanup(streamState) } - // A paced read drains a server batch as slowly as its consumer, and an unset batch runs to 16 MB. - if mongoc_cursor_get_batch_size(cursor) == 0 { - mongoc_cursor_set_batch_size(cursor, Self.serverBatchSize) - } - var rows: [PluginRow] = [] - var documents: [PluginExportDocument] = [] do { var docPtr: OpaquePointer? var sample: [[String: Any]] = [] @@ -454,8 +440,7 @@ nonisolated extension MongoDBConnection { } if projection == nil, sample.count >= MongoStreamProjection.sampleSize { projection = try openStream( - sample: sample, texts: sampleTexts, census: try census(), - continuation: continuation, abort: abort + sample: sample, texts: sampleTexts, census: try census(), continuation: continuation ) emitted += sample.count sample = [] @@ -474,13 +459,7 @@ nonisolated extension MongoDBConnection { documents: [dict], columns: projection.columns, kinds: projection.kinds, representation: uuidRepresentation, storedTexts: [text] )[0] - rows.append(row) - documents.append(document) - if rows.count >= Self.streamBatchSize { - abort.send(rows, documents: documents, to: continuation, maximumPause: Self.idleCursorPause) - rows.removeAll(keepingCapacity: true) - documents.removeAll(keepingCapacity: true) - } + continuation.yield(.rows([row], documents: [document])) unannounced.formUnion(projection.unannouncedFields(in: dict)) emitted += 1 if emitted >= PluginRowLimits.emergencyMax { @@ -488,25 +467,14 @@ nonisolated extension MongoDBConnection { break } } - if !rows.isEmpty { - continuation.yield(.rows(rows, documents: documents)) - rows = [] - documents = [] - } logUnannouncedFields(unannounced) var error = bson_error_t() if mongoc_cursor_error(cursor, &error) { throw makeError(error) } if projection == nil { - _ = try openStream( - sample: sample, texts: sampleTexts, census: nil, continuation: continuation, abort: abort - ) + _ = try openStream(sample: sample, texts: sampleTexts, census: nil, continuation: continuation) } continuation.finish() } catch { - // The documents read before a failure reach the reader ahead of it, as they did one at a time. - if !rows.isEmpty, !streamState.lock.withLock({ streamState.cancelled }) { - continuation.yield(.rows(rows, documents: documents)) - } continuation.finish(throwing: error) } } @@ -544,8 +512,7 @@ nonisolated extension MongoDBConnection { sample: [[String: Any]], texts: [String], census: MongoFieldCensus?, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws -> MongoStreamProjection { let projection = MongoStreamProjection(sample: sample, census: census, representation: uuidRepresentation) let documents = try MongoExportDocument.read( @@ -560,7 +527,7 @@ nonisolated extension MongoDBConnection { documents: sample, columns: projection.columns, kinds: projection.kinds, representation: uuidRepresentation, storedTexts: texts ) - abort.send(rows, documents: documents, to: continuation, maximumPause: Self.idleCursorPause) + continuation.yield(.rows(rows, documents: documents)) } return projection } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift index 3cafc3a792..2fe2d211a1 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift @@ -830,8 +830,11 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { } // MARK: - Streaming Queries - // Takes the options as the document the caller built, so an export keeps the `hint`, `collation` and - // `allowDiskUse` that decide which documents the grid showed. + /// Streams a find whose options are given as the document the caller already built. + /// + /// Taking the text rather than rebuilding it from pieces is what keeps an export reading the + /// same rows the grid showed: `hint`, `collation` and `allowDiskUse` change which documents come + /// back, and a signature that only carries sort, projection, skip and limit drops them. func streamFind( database: String, collection: String, @@ -843,8 +846,8 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { let queue = self.queue let streamState = MongoStreamState() - return PluginRowStream.makeExport { continuation, abort in - abort.onAbort { + return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + continuation.onTermination = { @Sendable _ in streamState.lock.withLock { streamState.cancelled = true } queue.async { streamState.lock.lock() @@ -888,7 +891,7 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { streamState.lock.unlock() iterateCursorStreaming( - cursor: cursor, continuation: continuation, abort: abort, streamState: streamState + cursor: cursor, continuation: continuation, streamState: streamState ) { try self.fieldCensus(census, client: client, database: database, collection: collection) } @@ -913,8 +916,8 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { let queue = self.queue let streamState = MongoStreamState() - return PluginRowStream.makeExport { continuation, abort in - abort.onAbort { + return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + continuation.onTermination = { @Sendable _ in streamState.lock.withLock { streamState.cancelled = true } queue.async { streamState.lock.lock() @@ -969,7 +972,7 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { streamState.lock.unlock() iterateCursorStreaming( - cursor: cursor, continuation: continuation, abort: abort, streamState: streamState + cursor: cursor, continuation: continuation, streamState: streamState ) { try self.fieldCensus(census, client: client, database: database, collection: collection) } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index f5a866e8f5..0093e1b5d9 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift @@ -933,7 +933,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } let timeout = conn.queryTimeoutMS - return PluginRowStream.makeExport { continuation, abort in + return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in let work = Task { do { switch try await runtime.exportPlan(for: trimmed, database: db, access: access, shell: shell) { @@ -965,12 +965,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se do { for try await element in inner { try Task.checkCancellation() - switch element { - case .rows(let rows, let documents): - await abort.send(rows, documents: documents, to: continuation) - default: - continuation.yield(element) - } + continuation.yield(element) } } catch { throw MongoScriptStatementFailure.carrying( @@ -988,7 +983,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } // A consumer that stops reading has to stop the cursor too, or it keeps draining the // whole query and holds the leased driver busy. - abort.onAbort { work.cancel() } + continuation.onTermination = { @Sendable _ in work.cancel() } } } diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift index f1200b39db..4c893cd5ee 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift @@ -742,10 +742,6 @@ final class MariaDBPluginConnection: @unchecked Sendable { private static let killTimeoutSeconds: UInt32 = 5 - // The server drops a connection it could not write a result to for net_write_timeout, 60 s by default and 600 s - // once `raiseStreamingWriteTimeout` ran; the lower one holds when a server refused the raise. - private static let unreadResultPause: TimeInterval = 20 - // Connector/C turns TLS on for any CA, certificate or key it is given, so a plaintext attempt gets none. private func applyTLS(_ mode: SSLMode, to handle: UnsafeMutablePointer) { MariaDBTLSMode.apply(mode, serverName: tlsServerName, to: handle) @@ -1459,7 +1455,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { /// The drain belongs to the producer. Enqueued from `onTermination` it would sit behind the /// producer on this one serial queue and run only after the whole result had been read, /// which is why the abort is a polled flag instead. - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in self.queue.async { [self] in do { let stream = { @@ -1529,11 +1525,12 @@ final class MariaDBPluginConnection: @unchecked Sendable { binaryStringsAreText: binaryStringsAreText(in: queryToRun, flavor: sessionFlavor) ) - continuation.yield(.header(PluginStreamHeader( + let stops = { self.cancellationGate.isCancelled(generation) } + _ = PluginRowStream.deliverBlocking(.header(PluginStreamHeader( columns: columns.names, columnTypeNames: columns.typeNames, estimatedRowCount: nil - ))) + )), to: continuation, stops: stops) let batchSize = 5_000 var batch: [PluginRow] = [] @@ -1550,14 +1547,13 @@ final class MariaDBPluginConnection: @unchecked Sendable { } batch.append(textProtocolRow(rowPtr, lengths: mysql_fetch_lengths(resultPtr), columns: columns)) - if batch.count >= batchSize { - abort.send(batch, to: continuation, maximumPause: Self.unreadResultPause) + // A reader that left or a Stop is handled at the top of the loop. + if batch.count >= batchSize, + PluginRowStream.deliverBlocking(.rows(batch), to: continuation, stops: stops) { batch.removeAll(keepingCapacity: true) } } - if !batch.isEmpty { - continuation.yield(.rows(batch)) - } + let delivered = batch.isEmpty || PluginRowStream.deliverBlocking(.rows(batch), to: continuation, stops: stops) if mysql_errno(mysql) != 0 { let error = noting(getError(), generation: generation) @@ -1567,6 +1563,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { mysql_free_result(resultPtr) try consumeTrailingResults(on: mysql, generation: generation) + guard delivered else { throw CancellationError() } } // While the stream waits for its reader the server waits on the socket, and gives up after `net_write_timeout`, diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift index 2af2d6d5a6..2328b5082d 100644 --- a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver.swift @@ -1092,14 +1092,16 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return streamRows(query: query, readOnlySessionState: readOnlySessionState) } - // The bridge task is cancelled on abort: a consumer that stops reading otherwise leaves it draining the whole - // result, since nothing cancels the task awaiting the inner stream. It forwards rows through `send`, so it waits - // with the consumer instead of reading the inner stream into its own buffer. + /// The bridge task is retained and cancelled from `onTermination`. Without that, a consumer + /// that stops reading, which an export or a copy does on cancel, leaves this task draining the + /// whole result and holding the connection open: the inner stream's abort never fires because + /// nothing ever cancels the task awaiting it. private func streamRows( query: String, readOnlySessionState: (@Sendable (Bool) -> PluginSessionTransactionState)? ) -> AsyncThrowingStream { - PluginRowStream.make { [self] continuation, abort in + // Bounded like the connection's own stream, or this relay would read it dry into an unbounded buffer. + AsyncThrowingStream(bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches)) { continuation in let task = Task { do { try await retryingAfterStop { @@ -1107,10 +1109,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { defer { self.endOperation(on: conn) } noteActivity(query) for try await element in conn.streamQuery(query, readOnlySessionState: readOnlySessionState) { - switch element { - case .header: continuation.yield(element) - case .rows(let rows): await abort.send(rows, to: continuation) - } + guard await PluginRowStream.deliver(element, to: continuation) else { break } } } continuation.finish() @@ -1118,7 +1117,7 @@ final class MySQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.finish(throwing: error) } } - abort.onAbort { task.cancel() } + continuation.onTermination = { _ in task.cancel() } } } diff --git a/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift b/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift index 73d032d7c7..c07018f4d6 100644 --- a/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift +++ b/apple/Plugins/Neo4jDriverPlugin/Neo4jPluginDriver.swift @@ -236,10 +236,10 @@ final class Neo4jPluginDriver: PluginDatabaseDriver, @unchecked Sendable { // Pulls a batch at a time, so an export never holds the whole result. func streamRows(query: String, context: PluginStatementContext) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { - try await self.stream(query, context: context, abort: abort, into: continuation) + try await self.stream(query, context: context, into: continuation) continuation.finish() } catch { continuation.finish(throwing: error) @@ -252,7 +252,6 @@ final class Neo4jPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func stream( _ query: String, context: PluginStatementContext, - abort: PluginStreamAbort, into continuation: AsyncThrowingStream.Continuation ) async throws { let connection = try requireConnection() @@ -279,8 +278,7 @@ final class Neo4jPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation.yield(.header(PluginStreamHeader(columns: table.columns, columnTypeNames: table.typeNames))) } guard !table.rows.isEmpty else { return true } - await abort.send(Self.cells(table.rows), to: continuation) - return !abort.isAborted + return await PluginRowStream.deliver(.rows(Self.cells(table.rows)), to: continuation) } } diff --git a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift index 900b5c2c09..c3764f36da 100644 --- a/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift +++ b/apple/Plugins/OracleDriverPlugin/OraclePlugin.swift @@ -513,7 +513,7 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable { } let batching = OracleRowBatcher(rowsPerBatch: OracleRowBatcher.batchRows, rowBudget: rowBudget) - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in // A plain `Task {}` is not a child of this one, so only the explicit cancel on abort stops oracle-nio. let streamTask = Task { do { @@ -525,7 +525,7 @@ final class OraclePluginDriver: PluginDatabaseDriver, @unchecked Sendable { columnTypeNames: columns.map(\.typeName) ))) case .rows(let batch): - await abort.send(batch.map { $0.map(\.asPluginCell) }, to: continuation) + _ = await PluginRowStream.deliver(.rows(batch.map { $0.map(\.asPluginCell) }), to: continuation) case .affectedRows: // The plugin stream carries rows only. The app sends writes and DDL through // `execute`, which reports the count, and bounds only reads through this stream. diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index f0ced6bec8..18e30f256f 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -1585,7 +1585,7 @@ final class LibPQPluginConnection: @unchecked Sendable { func streamQuery(_ query: String, readOnly: Bool = false) -> AsyncThrowingStream { let queryToRun = String(query) - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in self.queue.async { [self] in let handle = connectionHandle @@ -1706,6 +1706,7 @@ final class LibPQPluginConnection: @unchecked Sendable { let batchSize = 5_000 var batch: [PluginRow] = [] batch.reserveCapacity(batchSize) + let stops = { self.cancellationGate.isCancelled(generation) } while let result = PQgetResult(conn) { let status = PQresultStatus(result) @@ -1713,7 +1714,7 @@ final class LibPQPluginConnection: @unchecked Sendable { if !headerSent, Self.carriesRowDescription(status) { let described = streamHeader(from: result) columnOids = described.columnOids - continuation.yield(.header(described.header)) + _ = PluginRowStream.deliverBlocking(.header(described.header), to: continuation, stops: stops) headerSent = true } @@ -1733,15 +1734,13 @@ final class LibPQPluginConnection: @unchecked Sendable { PQclear(result) batch.append(row) - if batch.count >= batchSize { - run.abort.send(batch, to: continuation) + // A reader that left or a Stop is handled just below. + if batch.count >= batchSize, + PluginRowStream.deliverBlocking(.rows(batch), to: continuation, stops: stops) { batch.removeAll(keepingCapacity: true) } if run.abort.isAborted || cancellationGate.isCancelled(generation) { - if !batch.isEmpty { - continuation.yield(.rows(batch)) - } _ = cancelAndDrain(conn, suppressCancel: suppressCancel) // A Stop leaves the block aborted, as it does for any statement; only a reader that left is undone. let undoing = !cancellationGate.isCancelled(generation) @@ -1770,9 +1769,7 @@ final class LibPQPluginConnection: @unchecked Sendable { } } - if !batch.isEmpty { - continuation.yield(.rows(batch)) - } + let delivered = batch.isEmpty || PluginRowStream.deliverBlocking(.rows(batch), to: continuation, stops: stops) let outcome = finishPendingResults(conn, cancellingOutput: cancelsOutput) recordTransactionState(of: conn) @@ -1787,6 +1784,7 @@ final class LibPQPluginConnection: @unchecked Sendable { if let abandoned = abandonedCopyError(outcome, generation: generation) { throw abandoned } + guard delivered else { throw CancellationError() } } private static let streamSavepoint = "SAVEPOINT tablepro_stream_read" diff --git a/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift b/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift index bb420973df..47dbf86070 100644 --- a/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift +++ b/apple/Plugins/QdrantDriverPlugin/QdrantPluginDriver+Execution.swift @@ -150,11 +150,11 @@ extension QdrantPluginDriver { // The protocol's default `streamRows` runs `execute` once and buffers the whole collection. func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { if let name = QdrantOperations.decodeExport(query) { - try await self.streamCollection(name, abort: abort, continuation: continuation) + try await self.streamCollection(name, continuation: continuation) } else { let result = try await self.execute(query: query) continuation.yield(.header(PluginStreamHeader( @@ -177,7 +177,6 @@ extension QdrantPluginDriver { // alone, which skips the vectors that make up most of a page. private func streamCollection( _ name: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let client = try requireClient() @@ -193,7 +192,7 @@ extension QdrantPluginDriver { estimatedRowCount: collection.pointsCount ))) for try await points in client.pointPages(collection: name, pageSize: Self.exportPageSize) { - await abort.send(points.map(cells(columns: columns, typeNames: typeNames)), to: continuation) + _ = await PluginRowStream.deliver(.rows(points.map(cells(columns: columns, typeNames: typeNames))), to: continuation) } } } diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift index ace135b4e2..ff639ea0f0 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift @@ -521,10 +521,10 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let streamTask = Task { do { - try await self.performStreamRows(query: query, abort: abort, continuation: continuation) + try await self.performStreamRows(query: query, continuation: continuation) } catch { continuation.finish(throwing: error) } @@ -535,7 +535,6 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func performStreamRows( query: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let conn = redisConnection else { @@ -547,7 +546,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { switch operation { case .scan(_, let pattern, _, let type): try await streamScanRows( - connection: conn, pattern: pattern, typeFilter: type, scope: .session, abort: abort, continuation: continuation + connection: conn, pattern: pattern, typeFilter: type, scope: .session, continuation: continuation ) case .keyBrowse(let pattern, let typeScope, _, _, let database): try await conn.withDatabase(database) { @@ -556,12 +555,11 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { pattern: pattern, typeFilter: typeScope, scope: .outsideBlock, - abort: abort, continuation: continuation ) } case .keys(let pattern): - try await streamKeyRows(connection: conn, pattern: pattern, abort: abort, continuation: continuation) + try await streamKeyRows(connection: conn, pattern: pattern, continuation: continuation) default: let startTime = Date() let result = try await executeOperation(operation, connection: conn, startTime: startTime) @@ -584,7 +582,6 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { pattern: String?, typeFilter: String? = nil, scope: RedisCommandScope, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { continuation.yield(.header(PluginStreamHeader( @@ -611,7 +608,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let batchKeys = Array(page.keys[batchStart ..< batchEnd]) let rowBatch = try await buildKeySummaryRows(keys: batchKeys, connection: conn, stringByteLimit: nil) if !rowBatch.isEmpty { - await abort.send(rowBatch, to: continuation) + _ = await PluginRowStream.deliver(.rows(rowBatch), to: continuation) } batchStart = batchEnd } @@ -624,7 +621,6 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func streamKeyRows( connection conn: any RedisCommandChannel, pattern: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let keys = (try await conn.run(["KEYS", pattern]).arrayValue ?? []).map(\.displayText) @@ -638,7 +634,7 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let batch = Array(keys[start ..< min(start + Self.keySummaryBatchSize, keys.count)]) let rows = try await buildKeySummaryRows(keys: batch, connection: conn, stringByteLimit: nil) if !rows.isEmpty { - await abort.send(rows, to: continuation) + _ = await PluginRowStream.deliver(.rows(rows), to: continuation) } } continuation.finish() diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift index 559b8cf0db..5ff155c7d4 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift @@ -127,8 +127,7 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) async throws { _ = try await withTaskCancellationHandler { try await connection.execute( @@ -151,8 +150,6 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { batch.append(values[index..<(index + columnCount)].map(Self.decode)) index += columnCount } - // Not paced: a reader thread waiting here leaves the tunnel relay blocked sending to it, so it - // stops forwarding the heartbeats and the agent's 60 s idle watchdog ends the session. continuation.yield(.rows(batch)) } ) diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift index dc3d3fb0f6..dadb6da54b 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift @@ -48,8 +48,7 @@ protocol SQLiteExecutionBackend: Actor { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) async throws } @@ -75,10 +74,9 @@ extension SQLiteExecutionBackend { func streamQuery( _ query: String, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) async throws { - try await streamQuery(query, scope: .unrestricted, continuation: continuation, abort: abort) + try await streamQuery(query, scope: .unrestricted, continuation: continuation) } } @@ -434,11 +432,10 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { func streamQuery( _ query: String, scope: SQLiteStatementScope, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { try withStatementHandle(scope) { handle, mustRead in - try stream(query, on: handle, mustRead: mustRead, continuation: continuation, abort: abort) + try stream(query, on: handle, mustRead: mustRead, continuation: continuation) } } @@ -446,8 +443,7 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { _ query: String, on db: OpaquePointer, mustRead: Bool, - continuation: AsyncThrowingStream.Continuation, - abort: PluginStreamAbort + continuation: AsyncThrowingStream.Continuation ) throws { let statement = try Self.prepareOnlyStatement(query, db: db) // EXPLAIN never runs its statement, but stmt_readonly reports that statement's writes. @@ -471,7 +467,7 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { var stepResult = firstStep.result while stepResult == SQLITE_ROW { if Task.isCancelled { - if !batch.isEmpty { continuation.yield(.rows(batch)) } + if !batch.isEmpty { _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } sqlite3_finalize(statement) continuation.finish(throwing: CancellationError()) return @@ -479,13 +475,13 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { batch.append(Self.readRow(statement, columnCount: columnCount)) if batch.count >= batchSize { // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. - abort.send(batch, to: continuation) + _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) } - if !batch.isEmpty { continuation.yield(.rows(batch)) } + if !batch.isEmpty { _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } guard stepResult == SQLITE_DONE else { let message = String(cString: sqlite3_errmsg(db)) diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift index c86999fa14..ee4dbfdc11 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift @@ -690,19 +690,24 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { stream(query, scope: SQLiteStatementScope(context)) } + // Over the SSH agent the stream stays unbounded: a reader waiting for its consumer stops the tunnel relaying the + // heartbeats, and the agent's idle watchdog ends the session. private func stream(_ query: String, scope: SQLiteStatementScope) -> AsyncThrowingStream { let queryToRun = String(query) - return PluginRowStream.make { continuation, abort in + let policy: AsyncThrowingStream.Continuation.BufferingPolicy = backend is SQLiteLocalBackend + ? .bufferingOldest(PluginRowStream.boundedBufferBatches) + : .unbounded + return AsyncThrowingStream(bufferingPolicy: policy) { continuation in let streamTask = Task { do { - try await self.backend.streamQuery( - queryToRun, scope: scope, continuation: continuation, abort: abort - ) + try await self.backend.streamQuery(queryToRun, scope: scope, continuation: continuation) } catch { continuation.finish(throwing: error) } } - abort.onAbort { streamTask.cancel() } + continuation.onTermination = { @Sendable _ in + streamTask.cancel() + } } } diff --git a/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift b/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift index ae75f916ea..e5d148146d 100644 --- a/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift +++ b/apple/Plugins/SnowflakeDriverPlugin/SnowflakePluginDriver.swift @@ -621,7 +621,7 @@ final class SnowflakePluginDriver: PluginDatabaseDriver, @unchecked Sendable { } func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { guard let conn = self.connection else { throw SnowflakeError.notConnected } @@ -633,10 +633,10 @@ final class SnowflakePluginDriver: PluginDatabaseDriver, @unchecked Sendable { estimatedRowCount: streamed.estimatedRowCount ))) if !streamed.inlineRows.isEmpty { - await abort.send(streamed.inlineRows.map { row in row.map(Self.cellValue) }, to: continuation) + _ = await PluginRowStream.deliver(.rows(streamed.inlineRows.map { row in row.map(Self.cellValue) }), to: continuation) } try await conn.forEachChunk(of: streamed) { batch in - await abort.send(batch.map { row in row.map(Self.cellValue) }, to: continuation) + _ = await PluginRowStream.deliver(.rows(batch.map { row in row.map(Self.cellValue) }), to: continuation) } continuation.finish() } catch { diff --git a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift index ad48cbb5c0..a017a93fa2 100644 --- a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift +++ b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift @@ -1,5 +1,4 @@ import Foundation -import os import TableProPluginKit import TableProSpannerCore @@ -16,26 +15,23 @@ extension SpannerPluginDriver { try await boundedQueryFromStream(query: query, rowCap: rowCap) } - // Spanner may abort a read-write transaction left idle for about 10 s, and a stream can run inside one. - private static let unreadStreamPause: TimeInterval = 5 - func streamRows(query: String) -> AsyncThrowingStream { PluginRowStream.make { continuation, abort in let task = Task { do { let executor = try self.requireExecutor() - let headerSent = OSAllocatedUnfairLock(initialState: false) - try await self.streamOutput(for: query, executor: executor) { output in + var headerSent = false + for try await output in self.outputStream(for: query, executor: executor) { switch output { case .header(let fields): continuation.yield(.header(Self.streamHeader(fields))) - headerSent.withLock { $0 = true } + headerSent = true case .rows(let rows): - await abort.send(rows.map(Self.pluginRow), to: continuation, maximumPause: Self.unreadStreamPause) + continuation.yield(.rows(rows.map(Self.pluginRow))) } } try Task.checkCancellation() - if !headerSent.withLock({ $0 }) { + if !headerSent { continuation.yield(.header(PluginStreamHeader(columns: [], columnTypeNames: []))) } continuation.finish() @@ -51,16 +47,14 @@ extension SpannerPluginDriver { } } - private func streamOutput( + private func outputStream( for query: String, - executor: SpannerExecutor, - into emit: @escaping SpannerStreamEmit - ) async throws { + executor: SpannerExecutor + ) -> AsyncThrowingStream { if let request = SpannerBrowseRequest.decode(query) { - try await executor.stream(SpannerBrowseRenderer.select(resolved(request), dialect: executor.dialect), into: emit) - return + return executor.stream(SpannerBrowseRenderer.select(resolved(request), dialect: executor.dialect)) } - try await executor.stream(query, hostParameters: nil, into: emit) + return executor.stream(query, hostParameters: nil) } private func executeStatement(_ query: String, parameters: [PluginCellValue]?) async throws -> PluginQueryResult { diff --git a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift index 3e2b007605..26f1a7d138 100644 --- a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift +++ b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift @@ -1,182 +1,56 @@ import Foundation -// Terminating an AsyncThrowingStream only runs its onTermination handler, so a producer learns that its consumer -// stopped by polling `isAborted`, and learns that its consumer fell behind by sending through `send(_:to:)`. +/// The signal a row stream's producer polls to learn that its consumer stopped. +/// +/// Terminating an `AsyncThrowingStream` runs its `onTermination` handler and nothing else, so a +/// producer that does not poll runs to completion no matter what the consumer did. Measured on a +/// producer occupying a serial queue with 40 batches of work: polling this flag stopped it after +/// one batch, while hopping the drain onto the producer's own queue stopped nothing and ran the +/// drain only after the whole read had finished. public final class PluginStreamAbort: @unchecked Sendable { - public typealias Continuation = AsyncThrowingStream.Continuation - public typealias ExportContinuation = AsyncThrowingStream.Continuation - - private let condition = NSCondition() + private let lock = NSLock() private var _isAborted = false private var actions: [@Sendable () -> Void] = [] - private let readAhead: Int? - private var sentBatches = 0 - private var takenBatches = 0 - private var waiters: [Int: CheckedContinuation] = [:] - private var nextWaiterID = 0 - - public init() { - readAhead = nil - } - init(readAhead: Int) { - self.readAhead = max(readAhead, 1) - } + public init() {} public var isAborted: Bool { - condition.lock() - defer { condition.unlock() } + lock.lock() + defer { lock.unlock() } return _isAborted } - // For a producer that also needs an active signal, such as cancelling its own task. The stream's onTermination - // belongs to `PluginRowStream.make`, so registering here keeps the two from clobbering each other. + /// Registers work to run when the consumer stops, for a producer that also needs an active + /// signal such as cancelling its own task. Registering here rather than assigning + /// `continuation.onTermination` is what keeps both from clobbering each other; the stream's + /// handler belongs to `PluginRowStream.make`. Runs immediately if the abort already fired. public func onAbort(_ action: @escaping @Sendable () -> Void) { - condition.lock() + lock.lock() if _isAborted { - condition.unlock() + lock.unlock() action() return } actions.append(action) - condition.unlock() + lock.unlock() } - // The actions run before a waiting producer wakes, so it sees what they did, such as its cancelled task. public func abort() { - condition.lock() + lock.lock() let alreadyAborted = _isAborted _isAborted = true let pending = actions actions.removeAll() - condition.unlock() - if !alreadyAborted { - for action in pending { action() } - } - condition.lock() - let parked = Array(waiters.values) - waiters.removeAll() - condition.broadcast() - condition.unlock() - for waiter in parked { waiter.resume() } - } - - // Blocks the calling thread, so it is for a producer looping on a queue or thread of its own. Returns once the - // consumer is fewer than the stream's read-ahead batches behind, once it stops, or after `maximumPause` seconds, - // for a server that drops a connection left unread that long; past that the producer reads on as before. A batch - // a bounded buffer dropped is not counted, since the consumer never takes it. - public func send(_ rows: [PluginRow], to continuation: Continuation, maximumPause: TimeInterval? = nil) { - guard case .enqueued = continuation.yield(.rows(rows)) else { return } - waitForConsumer(maximumPause: maximumPause) - } - - // The same for a producer running in a task: it suspends instead of blocking, and a cancelled task returns. - public func send(_ rows: [PluginRow], to continuation: Continuation, maximumPause: TimeInterval? = nil) async { - guard case .enqueued = continuation.yield(.rows(rows)) else { return } - await waitForConsumer(maximumPause: maximumPause) - } - - public func send( - _ rows: [PluginRow], - documents: [PluginExportDocument]?, - to continuation: ExportContinuation, - maximumPause: TimeInterval? = nil - ) { - guard case .enqueued = continuation.yield(.rows(rows, documents: documents)) else { return } - waitForConsumer(maximumPause: maximumPause) - } - - public func send( - _ rows: [PluginRow], - documents: [PluginExportDocument]?, - to continuation: ExportContinuation, - maximumPause: TimeInterval? = nil - ) async { - guard case .enqueued = continuation.yield(.rows(rows, documents: documents)) else { return } - await waitForConsumer(maximumPause: maximumPause) - } - - private func waitForConsumer(maximumPause: TimeInterval?) { - let deadline = maximumPause.map { Date(timeIntervalSinceNow: $0) } - condition.lock() - defer { condition.unlock() } - sentBatches += 1 - while isBehind { - if let deadline { - guard condition.wait(until: deadline) else { return } - } else { - condition.wait() - } - } - } - - private func waitForConsumer(maximumPause: TimeInterval?) async { - let id: Int? = { - condition.lock() - defer { condition.unlock() } - sentBatches += 1 - guard isBehind else { return nil } - nextWaiterID += 1 - return nextWaiterID - }() - guard let id else { return } - await withTaskCancellationHandler { - await withCheckedContinuation { (waiter: CheckedContinuation) in - condition.lock() - guard isBehind, !Task.isCancelled else { - condition.unlock() - waiter.resume() - return - } - waiters[id] = waiter - condition.unlock() - if let maximumPause { - DispatchQueue.global().asyncAfter(deadline: .now() + maximumPause) { [weak self] in - self?.release(id) - } - } - } - } onCancel: { - release(id) - } - } - - func consumerTookBatch() { - condition.lock() - takenBatches += 1 - guard !isBehind else { - condition.unlock() - return - } - let parked = Array(waiters.values) - waiters.removeAll() - condition.broadcast() - condition.unlock() - for waiter in parked { waiter.resume() } - } - - // Read under the lock. - private var isBehind: Bool { - guard let readAhead, !_isAborted else { return false } - return sentBatches - takenBatches >= readAhead - } - - private func release(_ id: Int) { - condition.lock() - let waiter = waiters.removeValue(forKey: id) - condition.unlock() - waiter?.resume() + lock.unlock() + guard !alreadyAborted else { return } + for action in pending { action() } } } public enum PluginRowStream { - // Batches a producer that sends through `PluginStreamAbort.send` may have unread before it waits. - static let readAhead = 2 - - // The abort handler is installed before `build` runs and sets the flag on whichever thread terminated the stream, - // so a producer that has not started yet still sees it and can decline to send the query. Termination follows the + // The handler is installed before `build` runs and sets the flag on whichever thread ended the stream, so a + // producer that has not started yet still sees it and can decline to send the query. Termination follows the // stream's lifetime, not the consumer's `break`: a consumer that stops reading but holds the stream has not ended it. - // The stream handed back is pulled from the producer's, so each batch the consumer takes is counted for `send`. public static func make( bufferingPolicy: AsyncThrowingStream.Continuation.BufferingPolicy = .unbounded, _ build: @escaping @Sendable ( @@ -184,55 +58,79 @@ public enum PluginRowStream { PluginStreamAbort ) -> Void ) -> AsyncThrowingStream { - paced(bufferingPolicy: bufferingPolicy, build) { element in - if case .rows = element { return true } - return false + let abort = PluginStreamAbort() + return AsyncThrowingStream(bufferingPolicy: bufferingPolicy) { continuation in + // Only the consumer ends a stream as `.cancelled`, by cancelling its task or letting the stream go. A + // producer's own `finish` is `.finished`, and aborting then ran its cancel actions after every whole read. + continuation.onTermination = { @Sendable termination in + if case .cancelled = termination { abort.abort() } + } + build(continuation, abort) } } - // The same for a producer that hands documents along with its rows. - public static func makeExport( + // A batch is a few thousand rows, so this caps what a reader that falls behind leaves in memory. + public static let boundedBufferBatches = 16 + + // Every element the producer sends goes through `deliverBlocking` or `deliver`: a full buffer drops a plain yield. + public static func makeBounded( _ build: @escaping @Sendable ( - AsyncThrowingStream.Continuation, + AsyncThrowingStream.Continuation, PluginStreamAbort ) -> Void - ) -> AsyncThrowingStream { - paced(bufferingPolicy: .unbounded, build) { element in - if case .rows = element { return true } - return false + ) -> AsyncThrowingStream { + make(bufferingPolicy: .bufferingOldest(boundedBufferBatches), build) + } + + // Blocks the calling thread while the buffer is full, so it is only for a producer on its own serial queue or + // thread, never the cooperative pool. False, with the element not delivered, once the reader has gone or `stops` + // answers true. + public static func deliverBlocking( + _ element: Element, + to continuation: AsyncThrowingStream.Continuation, + stops: () -> Bool = { false } + ) -> Bool { + var pending = element + while true { + switch continuation.yield(pending) { + case .enqueued: + return true + case .dropped(let refused): + guard !stops() else { return false } + pending = refused + usleep(pollMicroseconds) + case .terminated: + return false + @unknown default: + return false + } } } - private static func paced( - bufferingPolicy: AsyncThrowingStream.Continuation.BufferingPolicy, - _ build: @escaping @Sendable (AsyncThrowingStream.Continuation, PluginStreamAbort) -> Void, - isBatch: @escaping @Sendable (Element) -> Bool - ) -> AsyncThrowingStream { - let abort = PluginStreamAbort(readAhead: readAhead) - let produced = AsyncThrowingStream(bufferingPolicy: bufferingPolicy) { continuation in - continuation.onTermination = { @Sendable _ in abort.abort() } - build(continuation, abort) + // `deliverBlocking` for a producer running as a task: it suspends while the buffer is full. + public static func deliver( + _ element: Element, + to continuation: AsyncThrowingStream.Continuation + ) async -> Bool { + var pending = element + while true { + switch continuation.yield(pending) { + case .enqueued: + return true + case .dropped(let refused): + pending = refused + do { + try await Task.sleep(nanoseconds: UInt64(pollMicroseconds) * 1_000) + } catch { + return false + } + case .terminated: + return false + @unknown default: + return false + } } - let source = PacedSource(produced, abort: abort, isBatch: isBatch) - return AsyncThrowingStream(unfolding: { try await source.next() }) } -} - -// One consumer reads the stream, so one call at a time reaches the iterator. -private final class PacedSource: @unchecked Sendable { - private var iterator: AsyncThrowingStream.AsyncIterator - private let abort: PluginStreamAbort - private let isBatch: @Sendable (Element) -> Bool - init(_ stream: AsyncThrowingStream, abort: PluginStreamAbort, isBatch: @escaping @Sendable (Element) -> Bool) { - iterator = stream.makeAsyncIterator() - self.abort = abort - self.isBatch = isBatch - } - - func next() async throws -> Element? { - let element = try await iterator.next() - if let element, isBatch(element) { abort.consumerTookBatch() } - return element - } + private static let pollMicroseconds: UInt32 = 2_000 } diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift index 0f4d3718d0..f3c5c8b7ac 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataPlugin.swift @@ -229,7 +229,7 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func streamRows(query: String) -> AsyncThrowingStream { let connection = self.connection - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in let task = Task { guard let connection else { continuation.finish(throwing: TeradataWireError.connectionFailed("not connected")) @@ -247,8 +247,11 @@ final class TeradataPluginDriver: PluginDatabaseDriver, @unchecked Sendable { }, onRows: { rows in guard !abort.isAborted else { return false } - abort.send(rows.map { row in row.map { $0.asPluginCell } }, to: continuation) - return !abort.isAborted + let delivered = PluginRowStream.deliverBlocking( + .rows(rows.map { row in row.map { $0.asPluginCell } }), + to: continuation + ) + return delivered && !abort.isAborted } ) continuation.finish() diff --git a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift index 57a9c1bf2f..86711e30c1 100644 --- a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift +++ b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift @@ -14,9 +14,6 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { static let logger = Logger(subsystem: "com.TablePro", category: "TrinoPluginDriver") - // The coordinator abandons a query its client has not polled for query.client.timeout, 5 minutes by default. - static let unpolledQueryPause: TimeInterval = 60 - // PluginKit's parameter binder calls back into `execute(query:)`, so the host's split reaches it through the task. @TaskLocal private static var hostSplit = false @@ -176,23 +173,18 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } do { var headerSent = false - try await client.executeStreamed( - statement, - onColumns: { columns in + for try await element in client.executeStreamed(statement) { + switch element { + case .columns(let columns): continuation.yield(.header(PluginStreamHeader( columns: columns.map(\.name), columnTypeNames: columns.map(\.typeName) ))) headerSent = true - }, - onPage: { page in - await abort.send( - page.map { row in row.map(Self.cellValue) }, - to: continuation, - maximumPause: Self.unpolledQueryPause - ) + case .rows(let page): + continuation.yield(.rows(page.map { row in row.map(Self.cellValue) })) } - ) + } if !headerSent { continuation.yield(.header(PluginStreamHeader(columns: [], columnTypeNames: []))) } diff --git a/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift b/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift index 6fdf878921..b9f4c274a5 100644 --- a/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift +++ b/apple/Plugins/TypesenseDriverPlugin/TypesensePluginDriver+Execution.swift @@ -112,10 +112,10 @@ extension TypesensePluginDriver { // Export reads the collection's JSONL endpoint a line at a time, so a collection larger than memory still exports // and the 250-hit search ceiling never applies. Anything else runs as an ordinary query, handed over in one batch. func streamRows(query: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let task = Task { do { - try await self.performStreamRows(query: query, abort: abort, continuation: continuation) + try await self.performStreamRows(query: query, continuation: continuation) } catch { continuation.finish(throwing: error) } @@ -126,7 +126,6 @@ extension TypesensePluginDriver { private func performStreamRows( query: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { guard let collection = TypesenseOperations.decodeExport(query) else { @@ -154,12 +153,12 @@ extension TypesensePluginDriver { try await connection.streamExport(collection: collection) { document in batch.append(document) guard batch.count >= Self.exportBatchSize else { return true } - await abort.send(TypesenseSchema.rows(for: batch, columns: columns, length: .whole), to: continuation) + _ = await PluginRowStream.deliver(.rows(TypesenseSchema.rows(for: batch, columns: columns, length: .whole)), to: continuation) batch.removeAll(keepingCapacity: true) return true } if !batch.isEmpty { - continuation.yield(.rows(TypesenseSchema.rows(for: batch, columns: columns, length: .whole))) + _ = await PluginRowStream.deliver(.rows(TypesenseSchema.rows(for: batch, columns: columns, length: .whole)), to: continuation) } continuation.finish() } diff --git a/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift b/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift index 3efd02a8a7..c0fee2c456 100644 --- a/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift +++ b/apple/Plugins/WeaviateDriverPlugin/WeaviatePluginDriver+Execution.swift @@ -47,10 +47,10 @@ extension WeaviatePluginDriver { guard let collectionName = WeaviateOperations.decodeExport(query) else { return defaultStreamRows(query: query) } - return PluginRowStream.make { continuation, abort in + return PluginRowStream.makeBounded { continuation, abort in let task = Task { do { - try await self.streamCollection(collectionName, abort: abort, continuation: continuation) + try await self.streamCollection(collectionName, continuation: continuation) continuation.finish() } catch { continuation.finish(throwing: error) @@ -62,7 +62,6 @@ extension WeaviatePluginDriver { private func streamCollection( _ name: String, - abort: PluginStreamAbort, continuation: AsyncThrowingStream.Continuation ) async throws { let client = try requireClient() @@ -80,7 +79,7 @@ extension WeaviatePluginDriver { value.map(PluginCellValue.text) ?? .null } } - await abort.send(rows, to: continuation) + _ = await PluginRowStream.deliver(.rows(rows), to: continuation) } } diff --git a/apple/TablePro/Core/Compare/StreamingRowProvider.swift b/apple/TablePro/Core/Compare/StreamingRowProvider.swift index 309b918eb6..b6fb6f44cf 100644 --- a/apple/TablePro/Core/Compare/StreamingRowProvider.swift +++ b/apple/TablePro/Core/Compare/StreamingRowProvider.swift @@ -5,9 +5,11 @@ // Feeds the merge join from a driver's row stream. It holds one batch of rows // at a time, and hands them out one row at a time. // -// It is not what bounds a comparison's memory either: a driver's stream holds -// at most two unread batches and its producer waits for the walk. The producer -// starts its statement when the stream is built, not at the first read. +// It is not what bounds a comparison's memory. Most drivers' streams hold a +// few batches and wait for the walk; ClickHouse, Elasticsearch, Trino, MongoDB, +// Spanner and SQLite over SSH run ahead of a side the walk consumes slowly, +// inside the stream's own buffer. The row limit, the filter, and the key order +// the two sides share are what keep a comparison's footprint down there. // import Foundation diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift index 824f3b95ec..342cb3a70f 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift @@ -14,9 +14,9 @@ // protocol default that calls `execute` and yields the whole result at once, // which Dameng still inherits, so there a large table is materialised before // the first batch is written and Stop cannot land until it is. Every other SQL -// driver implements it for real, and reads only as fast as the writes take its -// rows. Export has the same property through the same call; a capability that -// lets this refuse rather than inherit would have to be declared by every plugin. +// driver implements it for real. Export has the same property through the same +// call; a capability that lets this refuse rather than inherit would have to be +// declared by every plugin. // // This is the write path CSV and JSON import already use, through the same // `SQLStatementGenerator` and its per-engine bind-parameter ceiling. The one diff --git a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift index 8861146c27..ccee1b811b 100644 --- a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift +++ b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift @@ -68,7 +68,9 @@ internal enum TransactionEngineFamily: String, Hashable, Sendable, CaseIterable // After a failed statement the block takes nothing but a rollback, so one error costs every pending change. internal var failedStatementAbortsTransaction: Bool { switch self { - case .postgres, .redshift, .cockroach, .duckdb: + // Teradata session mode rolls the whole transaction back on an error. ANSI mode keeps it, but nothing here + // knows the mode. + case .postgres, .redshift, .cockroach, .duckdb, .teradata: return true case .mysql, .sqlite, .sqlServer, .oracle, .redis, .other: return false From dd1c1c2e88c7887e48614636c0c94cbfb88e2315 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 20:26:34 +0700 Subject: [PATCH 4/5] fix(plugins): unregister finished Spanner streams, cover Teradata in the agent guard, add a SQLite stream probe --- .../SpannerPluginDriver+Execution.swift | 6 ++- .../Access/AgentTransactionGuard.swift | 2 +- .../Execution/ManualCommitTests.swift | 1 + apple/changelog.d/4227.changed.md | 1 - apple/changelog.d/4227.fixed.md | 2 +- .../probes/check-stream-backpressure.sh | 40 ++++++++++++++----- 6 files changed, 36 insertions(+), 16 deletions(-) delete mode 100644 apple/changelog.d/4227.changed.md diff --git a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift index a017a93fa2..c8a212d16b 100644 --- a/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift +++ b/apple/Plugins/SpannerDriverPlugin/SpannerPluginDriver+Execution.swift @@ -40,8 +40,10 @@ extension SpannerPluginDriver { } } let id = self.registerUserOperation { task.cancel() } - abort.onAbort { - task.cancel() + abort.onAbort { task.cancel() } + // A stream its producer finishes never aborts, so the entry goes when the task ends. + Task { + await task.value self.unregisterUserOperation(id) } } diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift index 2b9ae43802..91f6cf6d33 100644 --- a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -44,7 +44,7 @@ internal struct AgentTransactionGuard: Sendable { ) case .oracle: return Savepoint(open: savepointStatement, release: nil, rollback: rollbackStatement) - case .redshift, .duckdb, .redis, .other: + case .redshift, .duckdb, .redis, .teradata, .other: return nil } } diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift index d3ff9b353b..d5dc9d548f 100644 --- a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -147,6 +147,7 @@ struct ManualCommitTests { #expect(AgentTransactionGuard.protection(family: .cockroach, sessionState: .inTransaction, controlsTransaction: false) == .savepoint) #expect(AgentTransactionGuard.protection(family: .duckdb, sessionState: .inTransaction, controlsTransaction: false) == .refuse) #expect(AgentTransactionGuard.protection(family: .redshift, sessionState: .inTransaction, controlsTransaction: false) == .refuse) + #expect(AgentTransactionGuard.protection(family: .teradata, sessionState: .inTransaction, controlsTransaction: false) == .refuse) #expect(AgentTransactionGuard.protection(family: .mysql, sessionState: .inTransaction, controlsTransaction: false) == .none) #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .idle, controlsTransaction: false) == .none) #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: true) == .none) diff --git a/apple/changelog.d/4227.changed.md b/apple/changelog.d/4227.changed.md deleted file mode 100644 index 7d1626af07..0000000000 --- a/apple/changelog.d/4227.changed.md +++ /dev/null @@ -1 +0,0 @@ -Table Transfer, Object Copy and Data Compare read their rows without the query timeout, as exports do. diff --git a/apple/changelog.d/4227.fixed.md b/apple/changelog.d/4227.fixed.md index 089dcee292..d6509a0a02 100644 --- a/apple/changelog.d/4227.fixed.md +++ b/apple/changelog.d/4227.fixed.md @@ -1 +1 @@ -Copying, transferring, comparing or exporting a large table no longer holds every row it has not written in memory. +Copying, comparing or exporting a large SQLite, Oracle, Teradata, Cassandra or cloud table held all of it in memory. diff --git a/apple/scripts/probes/check-stream-backpressure.sh b/apple/scripts/probes/check-stream-backpressure.sh index 94509602fc..c14dba646c 100755 --- a/apple/scripts/probes/check-stream-backpressure.sh +++ b/apple/scripts/probes/check-stream-backpressure.sh @@ -10,7 +10,7 @@ # Usage: # scripts/probes/check-stream-backpressure.sh [--rows N] [--delay-ms N] [--root DIR] # -# engine: postgresql, mysql +# engine: postgresql, mysql, sqlite (a scratch file, no server) # --rows rows in the probe table, default 1000000 # --delay-ms the consumer's wait per batch, default 20 # --root the apple/ folder whose sources to build, default this one; Libs comes from it too @@ -52,8 +52,14 @@ case "$ENGINE" in digits="(SELECT 0 AS n UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9)" CREATE="CREATE TABLE tp_stream_probe AS SELECT id, REPEAT(MD5(id), 6) AS payload FROM (SELECT d0.n + d1.n * 10 + d2.n * 100 + d3.n * 1000 + d4.n * 10000 + d5.n * 100000 + d6.n * 1000000 + 1 AS id FROM $digits d0, $digits d1, $digits d2, $digits d3, $digits d4, $digits d5, $digits d6) AS numbers WHERE id <= $ROWS" ;; + sqlite) + FOLDER=SQLiteDriverPlugin CLASS=SQLitePlugin C_MODULE="" + PRODUCTS="CSQLite TableProSQLiteCore TableProSQLQuoting TableProSQLVocabulary" + LINK="$ROOT/Libs/libsqlite3_vendored.a" + CREATE="CREATE TABLE tp_stream_probe AS WITH RECURSIVE g(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM g WHERE x < $ROWS) SELECT x AS id, hex(randomblob(96)) AS payload FROM g" + ;; *) - echo "usage: $0 [--rows N] [--delay-ms N] [--root DIR]" >&2 + echo "usage: $0 [--rows N] [--delay-ms N] [--root DIR]" >&2 exit 3 ;; esac @@ -66,11 +72,13 @@ for library in $LINK; do done HOST="${TP_STREAM_HOST:-127.0.0.1}" -PORT="${TP_STREAM_PORT:-$PORT_DEFAULT}" +PORT="${TP_STREAM_PORT:-${PORT_DEFAULT:-0}}" USER_NAME="${TP_STREAM_USER:-$USER}" PASSWORD="${TP_STREAM_PASSWORD:-}" -DATABASE="${TP_STREAM_DATABASE:-$DATABASE_DEFAULT}" -nc -z "$HOST" "$PORT" > /dev/null 2>&1 || { echo "no server at $HOST:$PORT" >&2; exit 3; } +DATABASE="${TP_STREAM_DATABASE:-${DATABASE_DEFAULT:-}}" +if [ -n "$C_MODULE" ]; then + nc -z "$HOST" "$PORT" > /dev/null 2>&1 || { echo "no server at $HOST:$PORT" >&2; exit 3; } +fi WORK="${TP_STREAM_BUILD_DIR:-}" if [ -z "$WORK" ]; then @@ -80,6 +88,10 @@ fi SOURCES="$WORK/Sources/Probe" rm -rf "$SOURCES" "$WORK/CModule" mkdir -p "$SOURCES" +if [ "$ENGINE" = sqlite ] && [ -z "$DATABASE" ]; then + DATABASE="$WORK/probe.sqlite" + rm -f "$DATABASE" "$DATABASE-wal" "$DATABASE-shm" +fi while IFS= read -r source; do ln -s "$source" "$SOURCES/driver_$(basename "$source")" @@ -87,12 +99,18 @@ done < <(find "$PLUGINS/$FOLDER" -name '*.swift' -not -path '*/Tests/*') for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox; do [ -f "$PLUGINS/Shared/$shared.swift" ] && ln -s "$PLUGINS/Shared/$shared.swift" "$SOURCES/shared_$shared.swift" done -ln -s "$PLUGINS/$FOLDER/$C_MODULE" "$WORK/CModule" - dependencies='.product(name: "TableProPluginKit", package: "TableProCore"), .product(name: "TableProSQLGrammar", package: "TableProCore"), - .product(name: "TableProProcess", package: "TableProCore"), - "'"$C_MODULE"'",' + .product(name: "TableProProcess", package: "TableProCore"),' +system_target="" +c_flags="" +if [ -n "$C_MODULE" ]; then + ln -s "$PLUGINS/$FOLDER/$C_MODULE" "$WORK/CModule" + dependencies="$dependencies + \"$C_MODULE\"," + system_target=".systemLibrary(name: \"$C_MODULE\", path: \"CModule\")," + c_flags=".unsafeFlags([\"-Xcc\", \"-I$PLUGINS/$FOLDER/$C_MODULE/include\"])," +fi for product in $PRODUCTS; do dependencies="$dependencies .product(name: \"$product\", package: \"TableProCore\")," @@ -113,7 +131,7 @@ let package = Package( .package(path: "$ROOT/Packages/TableProCore"), ], targets: [ - .systemLibrary(name: "$C_MODULE", path: "CModule"), + $system_target .executableTarget( name: "Probe", dependencies: [ @@ -123,7 +141,7 @@ let package = Package( swiftSettings: [ .swiftLanguageMode(.v6), .enableUpcomingFeature("MemberImportVisibility"), - .unsafeFlags(["-Xcc", "-I$PLUGINS/$FOLDER/$C_MODULE/include"]), + $c_flags ], linkerSettings: [.unsafeFlags([$flags])] ), From dbd7dd526ed5465607b37601a35d155dd4ee769e Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 20:37:54 +0700 Subject: [PATCH 5/5] fix(macos): take #4237's Teradata answers and test that Data Compare lifts the timeout before its streams --- .../Core/Compare/CompareRowService.swift | 21 ++++++-- .../Access/AgentTransactionGuard.swift | 1 + .../Execution/TransactionEngineFamily.swift | 8 +-- .../Autocomplete/SQLSchemaProviderTests.swift | 4 ++ .../CompareTimeoutLiftOrderTests.swift | 53 +++++++++++++++++++ .../Execution/ManualCommitTests.swift | 1 - 6 files changed, 78 insertions(+), 10 deletions(-) create mode 100644 apple/TableProTests/Core/Compare/CompareTimeoutLiftOrderTests.swift diff --git a/apple/TablePro/Core/Compare/CompareRowService.swift b/apple/TablePro/Core/Compare/CompareRowService.swift index fa4275de44..310d2d6a12 100644 --- a/apple/TablePro/Core/Compare/CompareRowService.swift +++ b/apple/TablePro/Core/Compare/CompareRowService.swift @@ -132,6 +132,18 @@ internal struct CompareRowService { // MARK: - Plumbing + // The sides are built inside the lift: a stream starts its statement when it is built, and a bounded one parks + // its connection once its buffer is full, so a lift sent after it would wait behind that statement for good. + internal static func readingBothSides( + liftingTimeoutOn drivers: [DatabaseDriver], + sides makeSides: () -> Sides, + _ body: (Sides) async throws -> T + ) async throws -> T { + try await BulkReadTimeout.lifted(on: drivers) { + try await body(makeSides()) + } + } + private func withBothSides( source: DatabaseEndpoint, sourceConnection: DatabaseConnection, @@ -188,10 +200,8 @@ internal struct CompareRowService { String(localized: "The target driver cannot stream rows for a comparison.") ) } - // Lifted before the providers exist: a stream starts its statement when it is built, and the lift - // would queue behind that statement on the same connection. - return try await BulkReadTimeout.lifted(on: [sourceDriver, targetDriver]) { - let sides = RowSides( + return try await Self.readingBothSides(liftingTimeoutOn: [sourceDriver, targetDriver]) { + RowSides( source: sourceRead.provider(driver: sourcePlugin), target: targetRead.provider(driver: targetPlugin), resolver: KeyLookupResolver( @@ -200,7 +210,8 @@ internal struct CompareRowService { ), targetDriver: targetPlugin ) - return try await body(engine, sides) + } _: { sides in + try await body(engine, sides) } } } diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift index 91f6cf6d33..a69f121875 100644 --- a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -44,6 +44,7 @@ internal struct AgentTransactionGuard: Sendable { ) case .oracle: return Savepoint(open: savepointStatement, release: nil, rollback: rollbackStatement) + // Teradata has no savepoint. case .redshift, .duckdb, .redis, .teradata, .other: return nil } diff --git a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift index ccee1b811b..77fc766a05 100644 --- a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift +++ b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift @@ -68,11 +68,11 @@ internal enum TransactionEngineFamily: String, Hashable, Sendable, CaseIterable // After a failed statement the block takes nothing but a rollback, so one error costs every pending change. internal var failedStatementAbortsTransaction: Bool { switch self { - // Teradata session mode rolls the whole transaction back on an error. ANSI mode keeps it, but nothing here - // knows the mode. - case .postgres, .redshift, .cockroach, .duckdb, .teradata: + case .postgres, .redshift, .cockroach, .duckdb: return true - case .mysql, .sqlite, .sqlServer, .oracle, .redis, .other: + // Teradata mode rolls back the whole transaction on a failed request and ANSI mode only the request; the + // family cannot see the session's mode, so Teradata keeps the answer it had before it had a family. + case .mysql, .sqlite, .sqlServer, .oracle, .redis, .teradata, .other: return false } } diff --git a/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift b/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift index 7e412150bb..f1c4945f43 100644 --- a/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift +++ b/apple/TableProTests/Core/Autocomplete/SQLSchemaProviderTests.swift @@ -123,7 +123,11 @@ final class MockDatabaseDriver: DatabaseDriver, SchemaSwitchable, @unchecked Sen } } + // Runs before the timeout is recorded, for a test that makes the statement wait on the connection's queue. + var applyQueryTimeoutGate: (@Sendable () async -> Void)? + func applyQueryTimeout(_ seconds: Int) async throws { + await applyQueryTimeoutGate?() applyQueryTimeoutValues.append(seconds) } diff --git a/apple/TableProTests/Core/Compare/CompareTimeoutLiftOrderTests.swift b/apple/TableProTests/Core/Compare/CompareTimeoutLiftOrderTests.swift new file mode 100644 index 0000000000..eb6dcd9028 --- /dev/null +++ b/apple/TableProTests/Core/Compare/CompareTimeoutLiftOrderTests.swift @@ -0,0 +1,53 @@ +// +// CompareTimeoutLiftOrderTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing + +@testable import TablePro + +@MainActor +struct CompareTimeoutLiftOrderTests { + // One queue runs the connection's statements in turn, as a libpq session's does, so a lift sent while a bounded + // stream waits at a full buffer would wait behind it, and the walk that would drain the buffer never starts. + @Test( + "A comparison lifts the query timeout before its streams start their statements", + .timeLimit(.minutes(1)) + ) + func timeoutIsLiftedBeforeTheStreamsStart() async throws { + let connectionQueue = DispatchQueue(label: "test.compare.connection") + let driver = MockDatabaseDriver() + driver.applyQueryTimeoutGate = { + await withCheckedContinuation { continuation in + connectionQueue.async { continuation.resume() } + } + } + let batches = PluginRowStream.boundedBufferBatches * 3 + + let rowsRead = try await CompareRowService.readingBothSides(liftingTimeoutOn: [driver]) { + PluginRowStream.makeBounded { continuation, _ in + connectionQueue.async { + continuation.yield(.header(PluginStreamHeader(columns: ["id"], columnTypeNames: ["integer"]))) + for index in 0..