From 8ed5936702e7ed1648986f47bcc544eab03337d4 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 02:32:31 +0700 Subject: [PATCH 1/4] perf(plugins): pace Elasticsearch, Trino and MongoDB streams and keep the server's side alive while the reader waits --- .../TrinoStatementClient.swift | 141 +++++-- .../TableProTrinoCore/TrinoTransport.swift | 1 + .../PluginRowStreamDeliveryTests.swift | 111 ++++++ .../TrinoHeartbeatTests.swift | 181 +++++++++ .../ElasticsearchConnection.swift | 40 ++ .../ElasticsearchPluginDriver+Execution.swift | 54 ++- .../ElasticsearchPluginDriver.swift | 2 + .../MongoDBConnection+SyncHelpers.swift | 371 ++++++++++++++++-- .../MongoDBConnection.swift | 176 ++++----- .../MongoDBPluginDriver.swift | 16 +- .../TableProPluginKit/PluginStreamAbort.swift | 39 ++ .../TrinoDriverPlugin/TrinoPluginDriver.swift | 45 ++- .../probes/check-stream-idle-pacing.sh | 349 ++++++++++++++++ 13 files changed, 1307 insertions(+), 219 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProTrinoCoreTests/TrinoHeartbeatTests.swift create mode 100755 apple/scripts/probes/check-stream-idle-pacing.sh diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift index c13edc6ba4..d63785c92b 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoStatementClient.swift @@ -1,9 +1,19 @@ import Foundation import os -public enum TrinoStreamElement: Sendable { - case columns([TrinoColumnDescriptor]) - case rows([[TrinoValue]]) +// How a reader that holds a page tells the coordinator it is still there. Past `query.client.timeout` (5 minutes by +// default) without contact, the coordinator abandons the query. +public enum TrinoHeartbeat: Sendable, Equatable { + // Trino 475 and later answer HEAD on the next page's URI, with no body. + case head + // Asking again for the page already received returns it from the coordinator's cache and counts as contact. + case repeatLastPage + + // `version` is what `flavor.versionStatement` returned, such as 475 or 476-e.1. + public init(flavor: TrinoServerFlavor, version: String?) { + let release = version.flatMap { Int($0.prefix { $0.isNumber }) } ?? 0 + self = flavor == .trino && release >= 475 ? .head : .repeatLastPage + } } public final class TrinoStatementClient: @unchecked Sendable { @@ -41,8 +51,9 @@ public final class TrinoStatementClient: @unchecked Sendable { var rows: [[TrinoValue]] = [] let outcome = try await runStatement( sql, + heartbeat: .repeatLastPage, onColumns: { columns = $0 }, - onPage: { rows.append(contentsOf: $0) } + onPage: { page, _ in rows.append(contentsOf: page) } ) return TrinoResultSet( columns: descriptors(from: columns), @@ -53,26 +64,20 @@ 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 reader that waits there holds the query where it + // is. While it waits it calls `keepAlive`. A cancelled caller stops at the next request and sends Trino the DELETE. + public func executeStreamed( + _ sql: String, + heartbeat: TrinoHeartbeat, + onColumns: ([TrinoColumnDescriptor]) async throws -> Void, + onPage: (_ page: [[TrinoValue]], _ keepAlive: () async throws -> Void) async throws -> Void + ) async throws { + _ = try await runStatement( + sql, + heartbeat: heartbeat, + onColumns: { try 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 +98,9 @@ public final class TrinoStatementClient: @unchecked Sendable { private func runStatement( _ sql: String, - onColumns: ([TrinoColumn]) -> Void, - onPage: ([[TrinoValue]]) -> Void + heartbeat: TrinoHeartbeat, + onColumns: ([TrinoColumn]) async throws -> Void, + onPage: ([[TrinoValue]], () async throws -> Void) async throws -> Void ) async throws -> StatementOutcome { guard let statementURL = config.statementURL else { throw TrinoError.invalidConfiguration("Invalid \(config.flavor.productName) server URL") @@ -107,9 +113,16 @@ public final class TrinoStatementClient: @unchecked Sendable { defer { lock.withLock { running[ObjectIdentifier(statement)] = nil } } do { - return try await drive(statement, url: statementURL, sql: sql, onColumns: onColumns, onPage: onPage) + return try await drive( + statement, + url: statementURL, + sql: sql, + heartbeat: heartbeat, + onColumns: onColumns, + onPage: onPage + ) } catch { - releaseIfCancelled(statement) + releaseIfCancelled(statement, after: error) throw error } } @@ -118,8 +131,9 @@ public final class TrinoStatementClient: @unchecked Sendable { _ statement: TrinoRunningStatement, url statementURL: URL, sql: String, - onColumns: ([TrinoColumn]) -> Void, - onPage: ([[TrinoValue]]) -> Void + heartbeat: TrinoHeartbeat, + onColumns: ([TrinoColumn]) async throws -> Void, + onPage: ([[TrinoValue]], () async throws -> Void) async throws -> Void ) async throws -> StatementOutcome { var httpResponse = try await sendWithRetry( makeRequest(method: .post, url: statementURL, headers: initialHeaders(), body: Data(sql.utf8)), @@ -134,10 +148,15 @@ public final class TrinoStatementClient: @unchecked Sendable { var columns = results.columns var columnsEmitted = false if let columns { - onColumns(columns) + try await onColumns(columns) columnsEmitted = true } - emitRows(results, columns: columns, onPage: onPage) + let first = results + try await emitRows(first, columns: columns) { rows in + try await onPage(rows) { + try await self.sendHeartbeat(heartbeat, for: statement, pageURL: nil, nextUri: first.nextUri) + } + } var updateType = results.updateType var updateCount = results.updateCount let queryId = results.id @@ -159,10 +178,15 @@ public final class TrinoStatementClient: @unchecked Sendable { columns = results.columns } if !columnsEmitted, let columns { - onColumns(columns) + try await onColumns(columns) columnsEmitted = true } - emitRows(results, columns: columns, onPage: onPage) + let page = results + try await emitRows(page, columns: columns) { rows in + try await onPage(rows) { + try await self.sendHeartbeat(heartbeat, for: statement, pageURL: nextURL, nextUri: page.nextUri) + } + } if let type = results.updateType { updateType = type } @@ -175,7 +199,11 @@ 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 throws -> Void + ) async throws { guard let data = results.data, let columns, !data.isEmpty else { return } var page: [[TrinoValue]] = [] page.reserveCapacity(data.count) @@ -188,7 +216,33 @@ public final class TrinoStatementClient: @unchecked Sendable { } page.append(decoded) } - onPage(page) + try await onPage(page) + } + + // A heartbeat never asks for the next page: that would hand the reader rows it has no room for. + private func sendHeartbeat( + _ heartbeat: TrinoHeartbeat, + for statement: TrinoRunningStatement, + pageURL: URL?, + nextUri: String? + ) async throws { + if heartbeat == .head, !statement.refusesHeadHeartbeat, let nextUri { + do { + _ = try await sendWithRetry( + makeRequest(method: .head, url: try followURL(nextUri), headers: followHeaders()), + for: statement + ) + return + } catch { + // A proxy can refuse HEAD where the coordinator would answer it; the page already received works anywhere. + try abortIfCancelled(statement) + if case TrinoError.httpStatus(let code, _) = error, [404, 405, 501].contains(code) { + statement.refuseHeadHeartbeat() + } + } + } + guard let pageURL else { return } + _ = try await sendWithRetry(makeRequest(method: .get, url: pageURL, headers: followHeaders()), for: statement) } private func descriptors(from columns: [TrinoColumn]) -> [TrinoColumnDescriptor] { @@ -202,8 +256,10 @@ public final class TrinoStatementClient: @unchecked Sendable { throw TrinoError.cancelled } - private func releaseIfCancelled(_ statement: TrinoRunningStatement) { - guard statement.isCancelled || Task.isCancelled, let uri = statement.claimRelease() else { return } + // A reader that stops taking pages throws CancellationError from its callback, before its task is cancelled. + private func releaseIfCancelled(_ statement: TrinoRunningStatement, after error: Error) { + guard statement.isCancelled || Task.isCancelled || error is CancellationError, + let uri = statement.claimRelease() else { return } fireDelete(uri) } @@ -396,6 +452,15 @@ private final class TrinoRunningStatement: @unchecked Sendable { private var cancelled = false private var released = false private var nextUri: String? + private var headRefused = false + + var refusesHeadHeartbeat: Bool { + lock.withLock { headRefused } + } + + func refuseHeadHeartbeat() { + lock.withLock { headRefused = true } + } var isCancelled: Bool { lock.withLock { cancelled } diff --git a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoTransport.swift b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoTransport.swift index 484a7599de..013b77421f 100644 --- a/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoTransport.swift +++ b/apple/Packages/TableProCore/Sources/TableProTrinoCore/TrinoTransport.swift @@ -43,6 +43,7 @@ public struct TrinoHTTPRequest: Sendable { public enum Method: String, Sendable { case get = "GET" case post = "POST" + case head = "HEAD" case delete = "DELETE" } diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift index bf8ad65eaf..75f8c50f50 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginRowStreamDeliveryTests.swift @@ -66,6 +66,102 @@ struct PluginRowStreamDeliveryTests { _ = stream } + @Test( + "A blocked producer keeps the server alive while its reader holds a full buffer, and reads nothing ahead", + .timeLimit(.minutes(1)) + ) + func blockingDeliveryKeepsAliveWhileFull() async throws { + let total = PluginRowStream.boundedBufferBatches * 2 + let beats = AsyncStream.makeStream() + let delivered = Counter() + let stream = PluginRowStream.makeBounded { continuation, _ in + Thread.detachNewThread { + for value in 0...makeStream() + let delivered = Counter() + let stream = PluginRowStream.makeBounded { continuation, _ in + Task { + for value in 0...makeStream( + bufferingPolicy: .bufferingOldest(1) + ) + #expect(PluginRowStream.deliverBlocking(Self.rows(0), to: continuation)) + #expect(throws: KeepAliveFailure.self) { + try PluginRowStream.deliverBlocking(Self.rows(1), to: continuation, keepingAliveEvery: 0) { + throw KeepAliveFailure() + } + } + await #expect(throws: KeepAliveFailure.self) { + try await PluginRowStream.deliver(Self.rows(2), to: continuation, keepingAliveEvery: 0) { + throw KeepAliveFailure() + } + } + continuation.finish() + var received: [String] = [] + for try await element in stream { + received += Self.values(of: element) + } + #expect(received == ["0"]) + } + @Test("A producer running as a task suspends for room and loses no batch") func asyncDeliveryKeepsEveryBatch() async throws { let total = PluginRowStream.boundedBufferBatches * 3 @@ -85,3 +181,18 @@ struct PluginRowStreamDeliveryTests { #expect(received == (0...makeStream() + + init(headStatus: Int) { + self.headStatus = headStatus + } + + var requests: [TrinoHTTPRequest] { + lock.withLock { recorded } + } + + func advance(by interval: Duration) { + lock.withLock { now += interval } + } + + func send(_ request: TrinoHTTPRequest) async throws -> TrinoHTTPResponse { + let (status, body) = lock.withLock { () -> (Int, String) in + recorded.append(request) + if now - lastContact > Self.clientTimeout { abandoned = true } + lastContact = now + if abandoned { + return (200, #"{"id":"q1","error":{"message":"Query was abandoned","errorName":"ABANDONED_QUERY"}}"#) + } + switch request.method { + case .post: + return (200, #"{"id":"q1","nextUri":"\#(Self.base)/1"}"#) + case .head: + return (headStatus, "") + case .delete: + deletes.continuation.yield() + return (204, "") + case .get: + return (200, Self.page(Int(request.url.lastPathComponent) ?? 0)) + } + } + return TrinoHTTPResponse(statusCode: status, headers: TrinoHeaderFields([:]), body: Data(body.utf8)) + } + + func cancelAll() {} + + private static func page(_ token: Int) -> String { + let columns = #"[{"name":"n","type":"integer"}]"# + let next = token < 3 ? #","nextUri":"\#(base)/\#(token + 1)""# : "" + return #"{"id":"q1","columns":\#(columns),"data":[[\#(token)]]\#(next)}"# + } +} + +struct TrinoHeartbeatTests { + private func client(_ transport: IdleCoordinator) -> TrinoStatementClient { + let config = TrinoClientConfig( + host: "trino.example", + port: 8_443, + useTLS: true, + user: "analyst", + auth: .basic(password: "secret") + ) + return TrinoStatementClient(transport: transport, config: config, session: TrinoSessionState()) + } + + // Holds the first page for 20 minutes, sending a heartbeat every 30 seconds as a paced reader does. + private func read( + _ transport: IdleCoordinator, + heartbeat: TrinoHeartbeat, + sendsHeartbeats: Bool = true + ) async throws -> (rows: [String], requestsWhileHeld: [TrinoHTTPRequest]) { + var rows: [String] = [] + var requestsWhileHeld: [TrinoHTTPRequest] = [] + try await client(transport).executeStreamed( + "SELECT n FROM t", + heartbeat: heartbeat, + onColumns: { _ in }, + onPage: { page, keepAlive in + rows += page.compactMap { row -> String? in + guard case .text(let text)? = row.first else { return nil } + return text + } + guard rows.count == 1 else { return } + let before = transport.requests.count + for _ in 0..<40 { + transport.advance(by: .seconds(30)) + if sendsHeartbeats { try await keepAlive() } + } + requestsWhileHeld = Array(transport.requests.dropFirst(before)) + } + ) + return (rows, requestsWhileHeld) + } + + @Test("A held page outlives the client timeout on HEAD heartbeats, and no later page is fetched while held") + func headHeartbeatsKeepTheQuery() async throws { + let transport = IdleCoordinator(headStatus: 200) + let (rows, held) = try await read(transport, heartbeat: .head) + #expect(rows == ["1", "2", "3"]) + #expect(held.count == 40) + #expect(held.allSatisfy { $0.method == .head && $0.url.absoluteString == "\(IdleCoordinator.base)/2" }) + } + + @Test("A server without the HEAD heartbeat gets the held page asked for again, and its rows are not read twice") + func refusedHeadFallsBackToTheHeldPage() async throws { + let transport = IdleCoordinator(headStatus: 405) + let (rows, held) = try await read(transport, heartbeat: .head) + #expect(rows == ["1", "2", "3"]) + #expect(held.first?.method == .head) + #expect(held.dropFirst().allSatisfy { $0.method == .get && $0.url.absoluteString == "\(IdleCoordinator.base)/1" }) + #expect(held.filter { $0.method == .head }.count == 1) + } + + @Test("A HEAD that a proxy refuses falls back to the held page for that beat and is tried again on the next") + func refusedHeadBeatFallsBackOnce() async throws { + let transport = IdleCoordinator(headStatus: 403) + let (rows, held) = try await read(transport, heartbeat: .head) + #expect(rows == ["1", "2", "3"]) + #expect(held.count == 80) + #expect(held.enumerated().allSatisfy { index, request in + index.isMultiple(of: 2) + ? request.method == .head && request.url.absoluteString == "\(IdleCoordinator.base)/2" + : request.method == .get && request.url.absoluteString == "\(IdleCoordinator.base)/1" + }) + } + + @Test("Older Trino and Presto get the held page asked for again") + func repeatLastPageHeartbeat() async throws { + let transport = IdleCoordinator(headStatus: 200) + let (rows, held) = try await read(transport, heartbeat: .repeatLastPage) + #expect(rows == ["1", "2", "3"]) + #expect(held.count == 40) + #expect(held.allSatisfy { $0.method == .get && $0.url.absoluteString == "\(IdleCoordinator.base)/1" }) + } + + @Test("Without heartbeats the coordinator abandons a query held past its client timeout") + func heldPageWithoutHeartbeatsIsAbandoned() async { + let transport = IdleCoordinator(headStatus: 200) + await #expect { + _ = try await read(transport, heartbeat: .head, sendsHeartbeats: false) + } throws: { error in + guard case TrinoError.query(let failure) = error else { return false } + return failure.errorName == "ABANDONED_QUERY" + } + } + + @Test("A reader that stops taking pages ends the query on the coordinator", .timeLimit(.minutes(1))) + func readerThatStopsSendsTheDelete() async { + let transport = IdleCoordinator(headStatus: 200) + await #expect(throws: CancellationError.self) { + try await client(transport).executeStreamed( + "SELECT n FROM t", + heartbeat: .head, + onColumns: { _ in }, + onPage: { _, _ in throw CancellationError() } + ) + } + var deletes = transport.deletes.stream.makeAsyncIterator() + await deletes.next() + #expect(transport.requests.filter { $0.method == .delete }.map(\.url.absoluteString) == ["\(IdleCoordinator.base)/1"]) + } + + @Test("HEAD heartbeats start at Trino 475", arguments: [ + (TrinoServerFlavor.trino, "475" as String?, TrinoHeartbeat.head), + (.trino, "476-e.1", .head), + (.trino, "474", .repeatLastPage), + (.trino, nil, .repeatLastPage), + (.presto, "0.300", .repeatLastPage) + ]) + func heartbeatFollowsTheServer(flavor: TrinoServerFlavor, version: String?, expected: TrinoHeartbeat) { + #expect(TrinoHeartbeat(flavor: flavor, version: version) == expected) + } +} diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift index ed8324ba2b..00fae138a7 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchConnection.swift @@ -42,6 +42,25 @@ internal struct ElasticsearchResponse { let rawText: String } +// A search can answer with a new point-in-time id, and the next request has to name the newest one. +internal final class ElasticsearchPointInTime: @unchecked Sendable { + private let lock = NSLock() + private var current: String + + init(id: String) { + current = id + } + + var id: String { + lock.withLock { current } + } + + func adopt(from response: ElasticsearchResponse) { + guard let next = (response.json as? [String: Any])?["pit_id"] as? String, !next.isEmpty else { return } + lock.withLock { current = next } + } +} + internal struct ElasticsearchIndexInfo { let name: String let docsCount: Int? @@ -264,6 +283,27 @@ internal final class ElasticsearchConnection: NSObject, @unchecked Sendable { return id } + // A shard whose context is gone would otherwise answer 200 without its hits, and the page would lose its rows. + func searchPointInTime(body: [String: Any]) async throws -> ElasticsearchResponse { + let response = try await request( + method: "POST", + path: "/_search?allow_partial_search_results=false", + body: try serialize(body), + isRead: true + ) + guard response.statusCode == 200 else { throw mapError(response, fallback: "Search failed") } + return response + } + + // A search for no hits that names the point-in-time renews its keep-alive on every shard and reads nothing. + func renewPointInTime(id: String, keepAlive: String) async throws -> ElasticsearchResponse { + try await searchPointInTime(body: [ + "size": 0, + "track_total_hits": false, + "pit": ["id": id, "keep_alive": keepAlive] + ]) + } + func closePointInTime(id: String) async { let body = try? serialize(["id": id]) _ = try? await request(method: "DELETE", path: "/_pit", body: body, isRead: true) diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver+Execution.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver+Execution.swift index 437740eb29..c786699062 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver+Execution.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver+Execution.swift @@ -56,7 +56,7 @@ extension ElasticsearchPluginDriver { guard let index = ElasticsearchOperations.decodeExport(query) else { return defaultStreamRows(query: query) } - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return PluginRowStream.makeBounded { continuation, abort in let task = Task { do { try await self.streamIndex(index, continuation: continuation) @@ -65,13 +65,15 @@ extension ElasticsearchPluginDriver { 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 reader slower than the cluster would otherwise outlive the point-in-time's keep-alive. private func streamIndex( _ index: String, continuation: AsyncThrowingStream.Continuation @@ -84,8 +86,8 @@ extension ElasticsearchPluginDriver { index: index, from: 0, size: Self.deepPageBatchSize, sorts: [], filters: [], logicMode: "AND" ) - let pit = try await conn.openPointInTime(index: index, keepAlive: Self.pitKeepAlive) - defer { Task { await conn.closePointInTime(id: pit) } } + let pit = ElasticsearchPointInTime(id: try await conn.openPointInTime(index: index, keepAlive: Self.pitKeepAlive)) + defer { Task { await conn.closePointInTime(id: pit.id) } } var columns: [String]? var searchAfter: [Any]? @@ -95,8 +97,10 @@ extension ElasticsearchPluginDriver { for: parsed, fields: fields, size: Self.deepPageBatchSize, tiebreaker: true, searchAfter: searchAfter, supportsCaseInsensitive: supportsCaseInsensitiveSearch ) - body["pit"] = ["id": pit, "keep_alive": Self.pitKeepAlive] - let response = try await conn.search(index: nil, body: body) + body["pit"] = ["id": pit.id, "keep_alive": Self.pitKeepAlive] + body["track_total_hits"] = false + let response = try await conn.searchPointInTime(body: body) + pit.adopt(from: response) let hits = extractHits(response) guard !hits.isEmpty else { break } @@ -106,16 +110,18 @@ extension ElasticsearchPluginDriver { if columns == nil { let resolved = ElasticsearchMappingFlattener.columns(forHits: hits, mappingColumns: mappingColumns) columns = resolved - continuation.yield(.header(PluginStreamHeader( + let header = PluginStreamElement.header(PluginStreamHeader( columns: resolved, columnTypeNames: resolved.map { exportTypeName($0, fields: fields) }, estimatedRowCount: nil - ))) + )) + guard try await deliver(header, to: continuation, renewing: pit, on: conn) else { return } } if let resolved = columns { - continuation.yield(.rows(ElasticsearchMappingFlattener.rows( + let rows = PluginStreamElement.rows(ElasticsearchMappingFlattener.rows( forHits: hits, columns: resolved, nestedParents: nestedParents, length: .whole - ))) + )) + guard try await deliver(rows, to: continuation, renewing: pit, on: conn) else { return } } searchAfter = hits.last?["sort"] as? [Any] @@ -124,11 +130,23 @@ extension ElasticsearchPluginDriver { if columns == nil { let resolved = ElasticsearchMappingFlattener.columns(forHits: [], mappingColumns: mappingColumns) - continuation.yield(.header(PluginStreamHeader( + let header = PluginStreamElement.header(PluginStreamHeader( columns: resolved, columnTypeNames: resolved.map { exportTypeName($0, fields: fields) }, estimatedRowCount: nil - ))) + )) + _ = try await deliver(header, to: continuation, renewing: pit, on: conn) + } + } + + private func deliver( + _ element: PluginStreamElement, + to continuation: AsyncThrowingStream.Continuation, + renewing pit: ElasticsearchPointInTime, + on conn: ElasticsearchConnection + ) async throws -> Bool { + try await PluginRowStream.deliver(element, to: continuation, keepingAliveEvery: Self.pitRenewalInterval) { + pit.adopt(from: try await conn.renewPointInTime(id: pit.id, keepAlive: Self.pitKeepAlive)) } } @@ -224,13 +242,13 @@ extension ElasticsearchPluginDriver { fields: [String: ElasticsearchFieldInfo], conn: ElasticsearchConnection ) async throws -> [[String: Any]] { - let pit = try await conn.openPointInTime(index: index, keepAlive: Self.pitKeepAlive) + let pit = ElasticsearchPointInTime(id: try await conn.openPointInTime(index: index, keepAlive: Self.pitKeepAlive)) do { let hits = try await collectDeepHits(parsed: parsed, fields: fields, pit: pit, conn: conn) - await conn.closePointInTime(id: pit) + await conn.closePointInTime(id: pit.id) return hits } catch { - await conn.closePointInTime(id: pit) + await conn.closePointInTime(id: pit.id) throw error } } @@ -238,7 +256,7 @@ extension ElasticsearchPluginDriver { private func collectDeepHits( parsed: ElasticsearchParsedSearch, fields: [String: ElasticsearchFieldInfo], - pit: String, + pit: ElasticsearchPointInTime, conn: ElasticsearchConnection ) async throws -> [[String: Any]] { var searchAfter: [Any]? @@ -251,8 +269,10 @@ extension ElasticsearchPluginDriver { for: parsed, fields: fields, size: Self.deepPageBatchSize, tiebreaker: true, searchAfter: searchAfter, supportsCaseInsensitive: supportsCaseInsensitiveSearch ) - body["pit"] = ["id": pit, "keep_alive": Self.pitKeepAlive] + body["pit"] = ["id": pit.id, "keep_alive": Self.pitKeepAlive] + body["track_total_hits"] = false let response = try await conn.search(index: nil, body: body) + pit.adopt(from: response) let hits = extractHits(response) guard !hits.isEmpty else { break } diff --git a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift index bf836cfae5..e7f6755c6f 100644 --- a/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift +++ b/apple/Plugins/ElasticsearchDriverPlugin/ElasticsearchPluginDriver.swift @@ -21,6 +21,8 @@ internal final class ElasticsearchPluginDriver: PluginDatabaseDriver, @unchecked static let maxResultWindow = 10_000 static let deepPageBatchSize = 1_000 static let pitKeepAlive = "1m" + // Half the keep-alive, so a renewal that waits on a slow node still lands before the point-in-time expires. + static let pitRenewalInterval: TimeInterval = 30 var connection: ElasticsearchConnection? { lock.withLock { _connection } } diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift index 55c8df7418..ad218e5240 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift @@ -9,6 +9,7 @@ import CLibMongoc import Foundation import os import OSLog +import TableProLogRedaction import TableProPluginKit #if canImport(CLibMongoc) @@ -417,11 +418,11 @@ nonisolated extension MongoDBConnection { func iterateCursorStreaming( cursor: OpaquePointer, - continuation: AsyncThrowingStream.Continuation, - streamState: MongoStreamState, + delivery: MongoStreamDelivery, census: () throws -> MongoFieldCensus? ) { - defer { cleanup(streamState) } + defer { cleanup(delivery.state) } + var batch = MongoStreamBatch() do { var docPtr: OpaquePointer? var sample: [[String: Any]] = [] @@ -429,53 +430,69 @@ nonisolated extension MongoDBConnection { var projection: MongoStreamProjection? var emitted = 0 var unannounced = Set() - while mongoc_cursor_next(cursor, &docPtr) { - if streamState.lock.withLock({ streamState.cancelled }) { throw CancellationError() } + var reachedLimit = false + while !reachedLimit, mongoc_cursor_next(cursor, &docPtr) { + if delivery.state.isCancelled { throw CancellationError() } try checkCancelled() guard let doc = docPtr else { continue } - let text = try canonicalText(of: doc) - guard let raw = try JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any], - let dict = Self.unwrapExtendedJson(raw) as? [String: Any] else { - throw PluginExportError.exportFailed(String(localized: "A document could not be read", bundle: .main)) - } - if projection == nil, sample.count >= MongoStreamProjection.sampleSize { - projection = try openStream( - sample: sample, texts: sampleTexts, census: try census(), continuation: continuation + // The whole cursor is read in one block on the queue, so what a document leaves autoreleased goes here, + // not when the read ends. + try autoreleasepool { + let text = try canonicalText(of: doc) + guard let raw = try JSONSerialization.jsonObject(with: Data(text.utf8)) as? [String: Any], + let dict = Self.unwrapExtendedJson(raw) as? [String: Any] else { + throw PluginExportError.exportFailed(String(localized: "A document could not be read", bundle: .main)) + } + if projection == nil, sample.count >= MongoStreamProjection.sampleSize { + projection = try openStream( + sample: sample, texts: sampleTexts, census: try census(), delivery: delivery + ) + emitted += sample.count + sample = [] + sampleTexts = [] + } + guard let projection else { + sample.append(dict) + sampleTexts.append(text) + return + } + let size = MongoLibbsonCodec.size(of: doc) + let document = try MongoExportDocument.read( + canonicalJSON: text, dictionary: dict, representation: uuidRepresentation, + bsonSizeUpperBound: size ) - emitted += sample.count - sample = [] - sampleTexts = [] - } - guard let projection else { - sample.append(dict) - sampleTexts.append(text) - continue - } - let document = try MongoExportDocument.read( - canonicalJSON: text, dictionary: dict, representation: uuidRepresentation, - bsonSizeUpperBound: MongoLibbsonCodec.size(of: doc) - ) - let row = BsonDocumentFlattener.flatten( - documents: [dict], columns: projection.columns, kinds: projection.kinds, - representation: uuidRepresentation, storedTexts: [text] - )[0] - continuation.yield(.rows([row], documents: [document])) - unannounced.formUnion(projection.unannouncedFields(in: dict)) - emitted += 1 - if emitted >= PluginRowLimits.emergencyMax { - logger.warning("Streamed result truncated at \(PluginRowLimits.emergencyMax) documents") - break + let row = BsonDocumentFlattener.flatten( + documents: [dict], columns: projection.columns, kinds: projection.kinds, + representation: uuidRepresentation, storedTexts: [text] + )[0] + batch.add(row, document: document, size: size) + if batch.isFull { + try delivery.send(batch.take()) + } + unannounced.formUnion(projection.unannouncedFields(in: dict)) + emitted += 1 + if emitted >= PluginRowLimits.emergencyMax { + logger.warning("Streamed result truncated at \(PluginRowLimits.emergencyMax) documents") + reachedLimit = true + } } } + if !batch.isEmpty { + try delivery.send(batch.take()) + } 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, delivery: delivery) } - continuation.finish() + delivery.finish() } catch { - continuation.finish(throwing: error) + // The rows read before the failure still reach the reader, as they did one document at a time. + if !batch.isEmpty, !(error is CancellationError) { + try? delivery.send(batch.take()) + } + delivery.finish(throwing: error) } } @@ -512,7 +529,7 @@ nonisolated extension MongoDBConnection { sample: [[String: Any]], texts: [String], census: MongoFieldCensus?, - continuation: AsyncThrowingStream.Continuation + delivery: MongoStreamDelivery ) throws -> MongoStreamProjection { let projection = MongoStreamProjection(sample: sample, census: census, representation: uuidRepresentation) let documents = try MongoExportDocument.read( @@ -521,29 +538,297 @@ nonisolated extension MongoDBConnection { var header = projection.header // iterateCursorStreaming stops there, so a stream that reaches it was cut. header.rowLimit = PluginRowLimits.emergencyMax - continuation.yield(.header(header)) + try delivery.send(.header(header)) if !sample.isEmpty { let rows = BsonDocumentFlattener.flatten( documents: sample, columns: projection.columns, kinds: projection.kinds, representation: uuidRepresentation, storedTexts: texts ) - continuation.yield(.rows(rows, documents: documents)) + try delivery.send(.rows(rows, documents: documents)) } return projection } - private func cleanup(_ state: MongoStreamState) { + // The cursor goes before the session it runs in, as libmongoc requires. + func cleanup(_ state: MongoStreamState) { state.lock.lock() let cur = state.cursor let col = state.collection + let session = state.session let alreadyDrained = state.drained state.drained = true state.cursor = nil state.collection = nil + state.session = nil state.lock.unlock() guard !alreadyDrained else { return } if let cur { mongoc_cursor_destroy(cur) } if let col { mongoc_collection_destroy(col) } + session?.end() + } + + // Runs on the connection's queue. Nil leaves the stream reading as fast as the server sends. + func startStreamSession(isFind: Bool) -> MongoStreamSession? { + guard let client, let policy = MongoCursorIdlePolicy(serverVersion: serverVersion(), isFind: isFind) else { + return nil + } + return MongoStreamSession.start(client: client, policy: policy, connection: self) + } +} + +// A paced stream's own session. While the reader holds a full buffer, `refreshSessions` keeps it alive, and with +// it the cursor: the manual's way to hold a cursor idle (cursor.noCursorTimeout(), "Refresh a Cursor with +// refreshSessions"). +nonisolated final class MongoStreamSession: @unchecked Sendable { + let session: OpaquePointer + let policy: MongoCursorIdlePolicy + let refreshInterval: TimeInterval + private let refreshCommand: OpaquePointer + private let connection: MongoDBConnection + private var nextRefresh = ContinuousClock.now + private var reportedRefreshFailure = false + + private init( + session: OpaquePointer, + policy: MongoCursorIdlePolicy, + refreshInterval: TimeInterval, + refreshCommand: OpaquePointer, + connection: MongoDBConnection + ) { + self.session = session + self.policy = policy + self.refreshInterval = refreshInterval + self.refreshCommand = refreshCommand + self.connection = connection + } + + // A deployment that answers no session, or refuses `refreshSessions` (DocumentDB, Cosmos DB), gets nil. + static func start( + client: OpaquePointer, + policy: MongoCursorIdlePolicy, + connection: MongoDBConnection + ) -> MongoStreamSession? { + guard let options = mongoc_session_opts_new() else { return nil } + defer { mongoc_session_opts_destroy(options) } + mongoc_session_opts_set_causal_consistency(options, false) + var error = bson_error_t() + guard let session = mongoc_client_start_session(client, options, &error) else { return nil } + guard let lsid = mongoc_client_session_get_lsid(session) else { + mongoc_client_session_destroy(session) + return nil + } + guard let command = bson_new() else { + mongoc_client_session_destroy(session) + return nil + } + let sessions = bson_new() + bson_append_document(sessions, "0", -1, lsid) + bson_append_array(command, "refreshSessions", -1, sessions) + bson_destroy(sessions) + let stream = MongoStreamSession( + session: session, + policy: policy, + refreshInterval: refreshInterval(client, connection: connection), + refreshCommand: command, + connection: connection + ) + do { + try stream.refresh(client) + } catch { + logger.info("The server refused refreshSessions, so this stream reads ahead of its reader") + stream.end() + return nil + } + stream.nextRefresh = .now + .seconds(stream.refreshInterval) + return stream + } + + // Timed from the last refresh, not from the last getMore: one getMore can fill many batches, and a slow reader + // takes them one wait at a time. A refresh that fails is tried again soon; a session that does expire ends the + // read with the cursor's own error. + func refreshIfDue(_ client: OpaquePointer) { + let now = ContinuousClock.now + guard now >= nextRefresh else { return } + do { + try refresh(client) + nextRefresh = now + .seconds(refreshInterval) + } catch { + if !reportedRefreshFailure { + reportedRefreshFailure = true + logger.warning( + "refreshSessions failed, trying again: \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)" + ) + } + nextRefresh = now + .seconds(min(30, refreshInterval)) + } + } + + private func refresh(_ client: OpaquePointer) throws { + let reply = bson_new() + defer { bson_destroy(reply) } + var error = bson_error_t() + guard mongoc_client_command_simple(client, "admin", refreshCommand, nil, reply, &error) else { + throw connection.makeError(error) + } + } + + func append(to options: OpaquePointer) throws { + var error = bson_error_t() + guard mongoc_client_session_append(session, options, &error) else { throw connection.makeError(error) } + if policy.needsNoCursorTimeout { + bson_append_bool(options, "noCursorTimeout", -1, true) + } + } + + func end() { + mongoc_client_session_destroy(session) + bson_destroy(refreshCommand) + } + + // A refresh reaches config.system.sessions at the server's next refresh job, five minutes apart by default, so it + // goes out well inside the session timeout: every 5 minutes against the default 30, a sixth of a shorter one. + private static func refreshInterval(_ client: OpaquePointer, connection: MongoDBConnection) -> TimeInterval { + var error = bson_error_t() + guard let server = mongoc_client_select_server(client, false, nil, &error) else { return 300 } + defer { mongoc_server_description_destroy(server) } + let hello = connection.bsonToDict(mongoc_server_description_hello_response(server)) + let minutes = (hello["logicalSessionTimeoutMinutes"] as? NSNumber)?.intValue ?? 30 + return TimeInterval(min(300, max(minutes, 1) * 10)) + } +} + +// How a stream's documents reach its reader. With a session, the cursor waits for room and the session is refreshed +// while it does. Without one the server cannot hold an idle cursor, so the cursor never waits: what the reader has +// no room for waits here, as it did when the stream was unbounded. +nonisolated final class MongoStreamDelivery { + let continuation: AsyncThrowingStream.Continuation + let state: MongoStreamState + private let session: MongoStreamSession? + private let client: OpaquePointer + private var waiting: [PluginExportStreamElement] = [] + private var firstWaiting = 0 + + init( + continuation: AsyncThrowingStream.Continuation, + state: MongoStreamState, + session: MongoStreamSession?, + client: OpaquePointer + ) { + self.continuation = continuation + self.state = state + self.session = session + self.client = client + } + + func send(_ element: PluginExportStreamElement) throws { + guard let session else { + waiting.append(element) + handOverWaiting() + return + } + session.refreshIfDue(client) + let delivered = PluginRowStream.deliverBlocking( + element, + to: continuation, + stops: { state.isCancelled }, + keepingAliveEvery: Self.refreshCheckInterval + ) { + session.refreshIfDue(client) + } + guard delivered else { throw CancellationError() } + } + + // What the reader has not taken yet goes out from a task of its own, so the connection's queue is free once the + // cursor is done, as it was when the stream was unbounded. + func finish(throwing error: Error? = nil) { + let rest = Array(waiting[firstWaiting...]) + waiting = [] + firstWaiting = 0 + let continuation = self.continuation + guard !rest.isEmpty else { + Self.end(continuation, throwing: error) + return + } + Task.detached { + for element in rest { + guard await PluginRowStream.deliver(element, to: continuation) else { break } + } + Self.end(continuation, throwing: error) + } + } + + private static func end( + _ continuation: AsyncThrowingStream.Continuation, + throwing error: Error? + ) { + if let error { + continuation.finish(throwing: error) + } else { + continuation.finish() + } + } + + private static let refreshCheckInterval: TimeInterval = 1 + + private func handOverWaiting() { + while firstWaiting < waiting.count { + guard case .enqueued = continuation.yield(waiting[firstWaiting]) else { break } + firstWaiting += 1 + } + if firstWaiting == waiting.count { + waiting.removeAll(keepingCapacity: true) + firstWaiting = 0 + } else if firstWaiting > waiting.count / 2 { + waiting.removeFirst(firstWaiting) + firstWaiting = 0 + } } } #endif + +// From 4.4.8 a session's cursors no longer expire after cursorTimeoutMillis (SERVER-6036). Before it only a find can +// wait, with noCursorTimeout; an aggregate has no way to. An unknown version waits for nothing. +nonisolated struct MongoCursorIdlePolicy: Sendable, Equatable { + let needsNoCursorTimeout: Bool + + init?(serverVersion: String?, isFind: Bool) { + guard let serverVersion else { return nil } + let parts = serverVersion.split(separator: ".").prefix(3).map { Int($0.prefix { $0.isNumber }) ?? 0 } + guard let major = parts.first, major > 0 else { return nil } + let release = (major, parts.count > 1 ? parts[1] : 0, parts.count > 2 ? parts[2] : 0) + let sessionCursorsWait = release >= (4, 4, 8) + guard sessionCursorsWait || isFind else { return nil } + needsNoCursorTimeout = !sessionCursorsWait + } +} + +// Documents go out a batch at a time; one element per document made a bounded buffer a handful of rows deep. +nonisolated struct MongoStreamBatch { + static let maximumDocuments = 1_000 + static let maximumBytes = 4 * 1_048_576 + + private var rows: [PluginRow] = [] + private var documents: [PluginExportDocument] = [] + private var bytes = 0 + + var isEmpty: Bool { rows.isEmpty } + + var isFull: Bool { + rows.count >= Self.maximumDocuments || bytes >= Self.maximumBytes + } + + mutating func add(_ row: PluginRow, document: PluginExportDocument, size: Int) { + rows.append(row) + documents.append(document) + bytes += size + } + + mutating func take() -> PluginExportStreamElement { + defer { + rows = [] + documents = [] + bytes = 0 + } + return .rows(rows, documents: documents) + } +} diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift index 2fe2d211a1..31cf9c0b4a 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift @@ -843,62 +843,20 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { census: MongoFieldCensus.Request? ) -> AsyncThrowingStream { #if canImport(CLibMongoc) - let queue = self.queue - let streamState = MongoStreamState() - - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in - continuation.onTermination = { @Sendable _ in - streamState.lock.withLock { streamState.cancelled = true } - queue.async { - streamState.lock.lock() - let cur = streamState.cursor - let col = streamState.collection - let alreadyDrained = streamState.drained - streamState.drained = true - streamState.cursor = nil - streamState.collection = nil - streamState.lock.unlock() - guard !alreadyDrained else { return } - if let cur { mongoc_cursor_destroy(cur) } - if let col { mongoc_collection_destroy(col) } - } + cursorStream(database: database, collection: collection, isFind: true, census: census) { col, session in + guard let filterBson = self.jsonToBson(filter) else { + throw MongoDBError(code: 0, message: "Invalid JSON filter: \(filter)") } - - queue.async { [self] in - guard !isShuttingDown, let client = self.client else { - continuation.finish(throwing: MongoDBError.notConnected) - return - } - - do { - guard let filterBson = jsonToBson(filter) else { - throw MongoDBError(code: 0, message: "Invalid JSON filter: \(filter)") - } - defer { bson_destroy(filterBson) } - - let optsBson = jsonToBson(optionsJson) - defer { if let opts = optsBson { bson_destroy(opts) } } - - let col = try getCollection(client, database: database, collection: collection) - guard let cursor = mongoc_collection_find_with_opts(col, filterBson, optsBson, nil) else { - mongoc_collection_destroy(col) - throw MongoDBError(code: 0, message: "Failed to create find cursor") - } - - streamState.lock.lock() - streamState.cursor = cursor - streamState.collection = col - streamState.lock.unlock() - - iterateCursorStreaming( - cursor: cursor, continuation: continuation, streamState: streamState - ) { - try self.fieldCensus(census, client: client, database: database, collection: collection) - } - } catch { - continuation.finish(throwing: error) - } + defer { bson_destroy(filterBson) } + guard let optsBson = self.jsonToBson(optionsJson) ?? bson_new() else { + throw MongoDBError(code: 0, message: "Failed to build query options") } + defer { bson_destroy(optsBson) } + try session?.append(to: optsBson) + guard let cursor = mongoc_collection_find_with_opts(col, filterBson, optsBson, nil) else { + throw MongoDBError(code: 0, message: "Failed to create find cursor") + } + return cursor } #else return AsyncThrowingStream { $0.finish(throwing: MongoDBError.libmongocUnavailable) } @@ -913,66 +871,75 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { census: MongoFieldCensus.Request? ) -> AsyncThrowingStream { #if canImport(CLibMongoc) + cursorStream(database: database, collection: collection, isFind: false, census: census) { col, session in + guard let pipelineBson = self.jsonToBson(pipeline) else { + throw MongoDBError(code: 0, message: "Invalid JSON pipeline: \(pipeline)") + } + defer { bson_destroy(pipelineBson) } + var optsBson: OpaquePointer? + if let optionsJson { + optsBson = self.jsonToBson(optionsJson) + } else { + let timeoutMS = self.queryTimeoutMS + if timeoutMS > 0 { + optsBson = self.jsonToBson("{\"maxTimeMS\": \(timeoutMS)}") + } + } + if session != nil, optsBson == nil { + optsBson = bson_new() + } + defer { if let opts = optsBson { bson_destroy(opts) } } + if let optsBson { + try session?.append(to: optsBson) + } + guard let cursor = mongoc_collection_aggregate(col, MONGOC_QUERY_NONE, pipelineBson, optsBson, nil) else { + throw MongoDBError(code: 0, message: "Failed to create aggregation cursor") + } + return cursor + } + #else + return AsyncThrowingStream { $0.finish(throwing: MongoDBError.libmongocUnavailable) } + #endif + } + + #if canImport(CLibMongoc) + // The cursor, its collection and the stream's session live and end inside one block on the connection's queue, so + // none of them can outlast the client that `disconnect` destroys behind it. A paced stream holds that queue while + // its reader waits, as every blocking driver does, which is why bulk reads run on their own pooled connection. + private func cursorStream( + database: String, + collection: String, + isFind: Bool, + census: MongoFieldCensus.Request?, + open: @escaping @Sendable (_ collection: OpaquePointer, _ session: MongoStreamSession?) throws -> OpaquePointer + ) -> AsyncThrowingStream { let queue = self.queue let streamState = MongoStreamState() - - return AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in + return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches)) { continuation in continuation.onTermination = { @Sendable _ in streamState.lock.withLock { streamState.cancelled = true } - queue.async { - streamState.lock.lock() - let cur = streamState.cursor - let col = streamState.collection - let alreadyDrained = streamState.drained - streamState.drained = true - streamState.cursor = nil - streamState.collection = nil - streamState.lock.unlock() - guard !alreadyDrained else { return } - if let cur { mongoc_cursor_destroy(cur) } - if let col { mongoc_collection_destroy(col) } - } + queue.async { self.cleanup(streamState) } } queue.async { [self] in + defer { cleanup(streamState) } guard !isShuttingDown, let client = self.client else { continuation.finish(throwing: MongoDBError.notConnected) return } - do { - guard let pipelineBson = jsonToBson(pipeline) else { - throw MongoDBError(code: 0, message: "Invalid JSON pipeline: \(pipeline)") - } - defer { bson_destroy(pipelineBson) } - + let session = startStreamSession(isFind: isFind) + streamState.lock.withLock { streamState.session = session } let col = try getCollection(client, database: database, collection: collection) - - var optsBson: OpaquePointer? - if let optionsJson { - optsBson = jsonToBson(optionsJson) - } else { - let timeoutMS = queryTimeoutMS - if timeoutMS > 0 { - optsBson = jsonToBson("{\"maxTimeMS\": \(timeoutMS)}") - } - } - defer { if let opts = optsBson { bson_destroy(opts) } } - - guard let cursor = mongoc_collection_aggregate( - col, MONGOC_QUERY_NONE, pipelineBson, optsBson, nil - ) else { - mongoc_collection_destroy(col) - throw MongoDBError(code: 0, message: "Failed to create aggregation cursor") - } - - streamState.lock.lock() - streamState.cursor = cursor - streamState.collection = col - streamState.lock.unlock() + streamState.lock.withLock { streamState.collection = col } + let cursor = try open(col, session) + streamState.lock.withLock { streamState.cursor = cursor } iterateCursorStreaming( - cursor: cursor, continuation: continuation, streamState: streamState + cursor: cursor, + delivery: MongoStreamDelivery( + continuation: continuation, state: streamState, session: session, client: client + ) ) { try self.fieldCensus(census, client: client, database: database, collection: collection) } @@ -981,19 +948,24 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { } } } - #else - return AsyncThrowingStream { $0.finish(throwing: MongoDBError.libmongocUnavailable) } - #endif } + #endif } nonisolated final class MongoStreamState: @unchecked Sendable { var cursor: OpaquePointer? var collection: OpaquePointer? + #if canImport(CLibMongoc) + var session: MongoStreamSession? + #endif var drained = false var cancelled = false let lock = NSLock() + + var isCancelled: Bool { + lock.withLock { cancelled } + } } // MARK: - BSON Helpers diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index 0093e1b5d9..9728bffdb9 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 AsyncThrowingStream(bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches)) { continuation in let work = Task { do { switch try await runtime.exportPlan(for: trimmed, database: db, access: access, shell: shell) { @@ -965,7 +965,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se do { for try await element in inner { try Task.checkCancellation() - continuation.yield(element) + guard await PluginRowStream.deliver(element, to: continuation) else { break } } } catch { throw MongoScriptStatementFailure.carrying( @@ -974,7 +974,7 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } case .result(let outcome): if let switched = outcome.databaseSwitch { self.currentDb = switched } - self.yieldMaterialised(outcome, into: continuation) + await self.deliverMaterialised(outcome, to: continuation) } continuation.finish() } catch { @@ -1014,10 +1014,10 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se /// Hands over a statement that had already run by the time the export asked, rather than /// running it a second time. Nested values render the way a streamed cursor renders them, so /// one export never mixes two spellings. - private func yieldMaterialised( + private func deliverMaterialised( _ outcome: MongoScriptStatementResult, - into continuation: AsyncThrowingStream.Continuation - ) { + to continuation: AsyncThrowingStream.Continuation + ) async { let result = MongoScriptResultBuilder.result( for: outcome, startTime: Date(), @@ -1033,10 +1033,10 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se if !result.columns.isEmpty { var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) if result.isTruncated { header.rowLimit = result.rows.count } - continuation.yield(.header(header)) + guard await PluginRowStream.deliver(.header(header), to: continuation) else { return } } if !result.rows.isEmpty { - continuation.yield(.rows(result.rows, documents: result.exportDocuments)) + _ = await PluginRowStream.deliver(.rows(result.rows, documents: result.exportDocuments), to: continuation) } } diff --git a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift index 26f1a7d138..f5dec35e28 100644 --- a/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift +++ b/apple/Plugins/TableProPluginKit/PluginStreamAbort.swift @@ -90,7 +90,21 @@ public enum PluginRowStream { to continuation: AsyncThrowingStream.Continuation, stops: () -> Bool = { false } ) -> Bool { + deliverBlocking(element, to: continuation, stops: stops, keepingAliveEvery: .infinity) {} + } + + // For a server that ends a read nobody reads from: while the buffer stays full, `keepAlive` runs on the producer's + // thread every `interval` seconds, so the server holds the read for as long as the reader holds the stream. Its + // error ends the wait and is thrown. + public static func deliverBlocking( + _ element: Element, + to continuation: AsyncThrowingStream.Continuation, + stops: () -> Bool = { false }, + keepingAliveEvery interval: TimeInterval, + _ keepAlive: () throws -> Void + ) rethrows -> Bool { var pending = element + var lastContact = ContinuousClock.now while true { switch continuation.yield(pending) { case .enqueued: @@ -98,6 +112,10 @@ public enum PluginRowStream { case .dropped(let refused): guard !stops() else { return false } pending = refused + if isDue(since: lastContact, every: interval) { + try keepAlive() + lastContact = .now + } usleep(pollMicroseconds) case .terminated: return false @@ -112,13 +130,28 @@ public enum PluginRowStream { _ element: Element, to continuation: AsyncThrowingStream.Continuation ) async -> Bool { + await deliver(element, to: continuation, keepingAliveEvery: .infinity) {} + } + + // `deliverBlocking` with a keep-alive, for a producer running as a task. + public static func deliver( + _ element: Element, + to continuation: AsyncThrowingStream.Continuation, + keepingAliveEvery interval: TimeInterval, + _ keepAlive: () async throws -> Void + ) async rethrows -> Bool { var pending = element + var lastContact = ContinuousClock.now while true { switch continuation.yield(pending) { case .enqueued: return true case .dropped(let refused): pending = refused + if isDue(since: lastContact, every: interval) { + try await keepAlive() + lastContact = .now + } do { try await Task.sleep(nanoseconds: UInt64(pollMicroseconds) * 1_000) } catch { @@ -132,5 +165,11 @@ public enum PluginRowStream { } } + // Compared as seconds, so an infinite interval is never due and any interval converts without trapping. + private static func isDue(since lastContact: ContinuousClock.Instant, every interval: TimeInterval) -> Bool { + let elapsed = (ContinuousClock.now - lastContact).components + return Double(elapsed.seconds) + Double(elapsed.attoseconds) / 1e18 >= interval + } + private static let pollMicroseconds: UInt32 = 2_000 } diff --git a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift index 86711e30c1..6a12640a9d 100644 --- a/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift +++ b/apple/Plugins/TrinoDriverPlugin/TrinoPluginDriver.swift @@ -10,6 +10,7 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { private let lock = NSLock() private var _client: TrinoStatementClient? private var _serverVersion: String? + private var _heartbeat = TrinoHeartbeat.repeatLastPage private var _columnTypes: [String: [String: String]] = [:] static let logger = Logger(subsystem: "com.TablePro", category: "TrinoPluginDriver") @@ -87,7 +88,10 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { : try? await client.execute(flavor.versionStatement) let product = flavor.productName if case .text(let version)? = versionResult?.rows.first?.first { - lock.withLock { _serverVersion = "\(product) \(version)" } + lock.withLock { + _serverVersion = "\(product) \(version)" + _heartbeat = TrinoHeartbeat(flavor: flavor, version: version) + } } else { lock.withLock { _serverVersion = product } } @@ -164,7 +168,7 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } private func stream(_ statement: String) -> AsyncThrowingStream { - PluginRowStream.make { continuation, abort in + PluginRowStream.makeBounded { continuation, abort in let driver = self let streamTask = Task { guard let client = driver.client else { @@ -173,20 +177,32 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } do { var headerSent = false - for try await element in client.executeStreamed(statement) { - switch element { - case .columns(let columns): - continuation.yield(.header(PluginStreamHeader( + try await client.executeStreamed( + statement, + heartbeat: driver.heartbeat, + onColumns: { columns in + let header = PluginStreamHeader( columns: columns.map(\.name), columnTypeNames: columns.map(\.typeName) - ))) + ) + guard await PluginRowStream.deliver(.header(header), to: continuation) else { + throw CancellationError() + } headerSent = true - case .rows(let page): - continuation.yield(.rows(page.map { row in row.map(Self.cellValue) })) + }, + onPage: { page, keepAlive in + let rows = PluginStreamElement.rows(page.map { row in row.map(Self.cellValue) }) + let delivered = try await PluginRowStream.deliver( + rows, + to: continuation, + keepingAliveEvery: Self.heartbeatInterval, + keepAlive + ) + guard delivered else { throw CancellationError() } } - } + ) if !headerSent { - continuation.yield(.header(PluginStreamHeader(columns: [], columnTypeNames: []))) + _ = await PluginRowStream.deliver(.header(PluginStreamHeader(columns: [], columnTypeNames: [])), to: continuation) } continuation.finish() } catch { @@ -197,6 +213,13 @@ final class TrinoPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } } + private var heartbeat: TrinoHeartbeat { + lock.withLock { _heartbeat } + } + + // The official client's interval: a coordinator abandons a query after query.client.timeout, 5 minutes by default. + private static let heartbeatInterval: TimeInterval = 30 + private func pluginResult(_ result: TrinoResultSet, executionTime: TimeInterval) -> PluginQueryResult { guard !result.columns.isEmpty else { let message = Self.statusMessage(for: result) diff --git a/apple/scripts/probes/check-stream-idle-pacing.sh b/apple/scripts/probes/check-stream-idle-pacing.sh new file mode 100755 index 0000000000..1cb3fa7fb4 --- /dev/null +++ b/apple/scripts/probes/check-stream-idle-pacing.sh @@ -0,0 +1,349 @@ +#!/usr/bin/env bash +# +# Read a large result through a driver's `streamRows` the way a copy does, against a real server whose idle limit +# ends a read nobody reads from, and print the memory it held and whether the read survived its reader's pause. +# +# Builds a harness from the driver's own sources. `--hold-seconds` reads one batch, then stops reading for that long +# with the stream still held, then reads the rest: a paced stream waits there and has to keep the server's side +# alive. `--delay-us-per-row` makes the reader slower than the server, the way a target's inserts are. +# +# Usage: +# scripts/probes/check-stream-idle-pacing.sh [--rows N] [--delay-us-per-row N] [--hold-seconds N] [--root DIR] +# +# engine: elasticsearch (fills index tp_stream_probe), mongodb (fills collection tp_stream_probe), +# trino (reads tpch.sf1.orders, 1.5M rows; --rows adds a LIMIT) +# --root the apple/ folder whose driver sources to build, default this one; Libs comes from it too +# +# Servers, with the idle limits scaled down where the server allows it: +# docker run -d --name tp-es -p 9200:9200 -e discovery.type=single-node -e xpack.security.enabled=false \ +# -e search.keep_alive_interval=5s -e ES_JAVA_OPTS="-Xms1g -Xmx1g" docker.elastic.co/elasticsearch/elasticsearch:8.19.7 +# (the driver keeps its point in time for 1m; the 5 s reaper drops it soon after) +# docker run -d --name tp-mongo -p 27017:27017 mongo:8.0 --setParameter localLogicalSessionTimeoutMinutes=1 \ +# --setParameter logicalSessionRefreshMillis=1000 --setParameter cursorTimeoutMillis=10000 \ +# --setParameter ttlMonitorSleepSecs=1 +# docker run -d --name tp-trino -p 8080:8080 -v :/etc/trino trinodb/trino +# (config.properties as the image ships it, plus query.client.timeout=1m) +# +# TP_STREAM_HOST, TP_STREAM_PORT, TP_STREAM_USER and TP_STREAM_PASSWORD name the server, by default 127.0.0.1 on the +# engine's port with no password (user `probe`, none for MongoDB). 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_US=20 +HOLD=0 +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +while [ $# -gt 0 ]; do + case "$1" in + --rows) ROWS="${2:?}"; shift 2 ;; + --delay-us-per-row) DELAY_US="${2:?}"; shift 2 ;; + --hold-seconds) HOLD="${2:?}"; shift 2 ;; + --root) ROOT="$(cd "${2:?}" && pwd)"; shift 2 ;; + *) echo "unknown argument $1" >&2; exit 3 ;; + esac +done +PLUGINS="$ROOT/Plugins" + +C_MODULE="" +LINK="" +case "$ENGINE" in + elasticsearch) + FOLDER=ElasticsearchDriverPlugin CLASS=ElasticsearchPlugin PORT_DEFAULT=9200 + PRODUCTS="TableProNumberFormatting TableProDocumentPath" + ;; + mongodb) + FOLDER=MongoDBDriverPlugin CLASS=MongoDBPlugin PORT_DEFAULT=27017 C_MODULE=CLibMongoc + PRODUCTS="TableProLogRedaction TableProSQLGrammar TableProNumberFormatting" + LINK="$ROOT/Libs/libmongoc.a $ROOT/Libs/libbson.a -lssl.3 -lcrypto.3 -lresolv -lz" + ;; + trino) + FOLDER=TrinoDriverPlugin CLASS=TrinoPlugin PORT_DEFAULT=8080 + PRODUCTS="TableProTLSClientIdentity TableProTrinoCore" + ;; + *) + echo "usage: $0 [--rows N] [--delay-us-per-row N] [--hold-seconds 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_DEFAULT=probe +[ "$ENGINE" = mongodb ] && USER_DEFAULT="" +USER_NAME="${TP_STREAM_USER:-$USER_DEFAULT}" +PASSWORD="${TP_STREAM_PASSWORD:-}" +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 PluginRedirectRefusingDelegate; do + [ -f "$PLUGINS/Shared/$shared.swift" ] && ln -s "$PLUGINS/Shared/$shared.swift" "$SOURCES/shared_$shared.swift" +done +dependencies='.product(name: "TableProPluginKit", package: "TableProCore"), + .product(name: "TableProSQLQuoting", 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\")," +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: "StreamIdlePacingProbe", + platforms: [.macOS(.v14)], + dependencies: [ + .package(path: "$ROOT/Packages/TableProCore"), + ], + targets: [ + $system_target + .executableTarget( + name: "Probe", + dependencies: [ + $dependencies + ], + path: "Sources/Probe", + swiftSettings: [ + .swiftLanguageMode(.v6), + .enableUpcomingFeature("MemberImportVisibility"), + $c_flags + ], + linkerSettings: [.unsafeFlags([$flags])] + ), + ] +) +MANIFEST + +case "$ENGINE" in + elasticsearch) + cat > "$SOURCES/probe_Engine.swift" << 'SWIFT' +import Foundation +import TableProPluginKit + +typealias EnginePlugin = ElasticsearchPlugin +let probeFields: [String: String] = [:] +let probeQuery = ElasticsearchOperations.encodeExport(index: "tp_stream_probe") + +func fill(_ driver: any PluginDatabaseDriver, rows: Int, host: String, port: Int) async throws { + let base = "http://\(host):\(port)/tp_stream_probe" + _ = try? await send("DELETE", base, body: nil) + _ = try await send("PUT", base, body: #"{"settings":{"number_of_shards":2,"number_of_replicas":0}}"#) + var next = 1 + while next <= rows { + let last = min(next + 9_999, rows) + var body = "" + for id in next...last { + body += #"{"index":{"_id":"\#(id)"}}"# + "\n" + body += #"{"id":\#(id),"payload":"\#(payload(id))"}"# + "\n" + } + _ = try await send("POST", "\(base)/_bulk", body: body, contentType: "application/x-ndjson") + next = last + 1 + } + _ = try await send("POST", "\(base)/_refresh", body: nil) +} + +private func send(_ method: String, _ url: String, body: String?, contentType: String = "application/json") async throws -> Data { + var request = URLRequest(url: URL(string: url)!) + request.httpMethod = method + request.httpBody = body.map { Data($0.utf8) } + request.setValue(contentType, forHTTPHeaderField: "Content-Type") + let (data, response) = try await URLSession.shared.data(for: request) + guard let status = (response as? HTTPURLResponse)?.statusCode, status < 300 else { + throw NSError(domain: "probe", code: 1, userInfo: [NSLocalizedDescriptionKey: String(decoding: data, as: UTF8.self)]) + } + return data +} +SWIFT + ;; + mongodb) + cat > "$SOURCES/probe_Engine.swift" << 'SWIFT' +import Foundation +import TableProPluginKit + +typealias EnginePlugin = MongoDBPlugin +let probeFields: [String: String] = [:] +let probeQuery = "db.tp_stream_probe.find({}).sort({_id: 1})" + +func fill(_ driver: any PluginDatabaseDriver, rows: Int, host: String, port: Int) async throws { + _ = try? await driver.execute(query: "db.tp_stream_probe.drop()") + var next = 1 + while next <= rows { + let last = min(next + 4_999, rows) + let documents = (next...last).map { #"{_id: \#($0), payload: "\#(payload($0))"}"# }.joined(separator: ",") + _ = try await driver.execute(query: "db.tp_stream_probe.insertMany([\(documents)])") + next = last + 1 + } +} +SWIFT + ;; + trino) + LIMIT="" + [ "$ROWS" -lt 1500000 ] && LIMIT=" LIMIT $ROWS" + cat > "$SOURCES/probe_Engine.swift" << SWIFT +import Foundation +import TableProPluginKit + +typealias EnginePlugin = TrinoPlugin +let probeFields: [String: String] = [:] +let probeQuery = "SELECT orderkey, comment, clerk FROM tpch.sf1.orders$LIMIT" + +func fill(_ driver: any PluginDatabaseDriver, rows: Int, host: String, port: Int) async throws {} +SWIFT + ;; +esac + +cat > "$SOURCES/probe_Probe.swift" << 'SWIFT' +import Darwin +import Foundation +import TableProPluginKit + +func payload(_ id: Int) -> String { + String(repeating: String(format: "%08lx", (id &* 2_654_435_761) & 0xFFFF_FFFF), count: 12) +} + +@main +enum Probe { + static func main() async { + let arguments = CommandLine.arguments + guard arguments.count >= 8, + let port = Int(arguments[2]), let rows = Int(arguments[5]), + let delay = Int(arguments[6]), let hold = Int(arguments[7]) else { + print("usage: Probe ") + exit(3) + } + let driver = EnginePlugin().createDriver(config: DriverConnectionConfig( + host: arguments[1], port: port, username: arguments[3], password: arguments[4], + database: "", additionalFields: probeFields + )) + do { + try await driver.connect() + try await driver.applyQueryTimeout(0) + try await fill(driver, rows: rows, host: arguments[1], port: port) + } catch { + print("cannot prepare the data: \(error)") + exit(3) + } + + let sampler = FootprintSampler() + let baseline = sampler.start() + let started = Date() + var read = 0 + var batches = 0 + var owedMicroseconds = 0 + var failure: Error? + do { + for try await element in driver.streamRows(query: probeQuery) { + guard case .rows(let batch) = element else { continue } + read += batch.count + batches += 1 + if batches == 1, hold > 0 { + print("holding the stream for \(hold) s after the first batch") + try await Task.sleep(for: .seconds(hold)) + } + // Owed time is slept in 20 ms steps, so a driver that sends one row per element is slowed the same. + owedMicroseconds += delay * batch.count + if owedMicroseconds >= 20_000 { + try await Task.sleep(for: .microseconds(owedMicroseconds)) + owedMicroseconds = 0 + } + } + } catch { + failure = error + } + let peak = sampler.stop() + driver.disconnect() + + let megabytes = { (bytes: UInt64) in String(format: "%.1f MB", Double(bytes) / 1_048_576) } + print("rows read: \(read) 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" "$ROWS" "$DELAY_US" "$HOLD" From c6f533efbc412d7ed0ea59243ba9a37845ebd59e Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 02:33:00 +0700 Subject: [PATCH 2/4] docs(changelog): add the stream pacing fragments --- apple/changelog.d/4245.fixed.1.md | 1 + apple/changelog.d/4245.fixed.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 apple/changelog.d/4245.fixed.1.md create mode 100644 apple/changelog.d/4245.fixed.md diff --git a/apple/changelog.d/4245.fixed.1.md b/apple/changelog.d/4245.fixed.1.md new file mode 100644 index 0000000000..b37c874335 --- /dev/null +++ b/apple/changelog.d/4245.fixed.1.md @@ -0,0 +1 @@ +An Elasticsearch export could leave out the documents of a shard that stopped answering, without an error. diff --git a/apple/changelog.d/4245.fixed.md b/apple/changelog.d/4245.fixed.md new file mode 100644 index 0000000000..4d6cc8eb24 --- /dev/null +++ b/apple/changelog.d/4245.fixed.md @@ -0,0 +1 @@ +Copying, comparing or exporting a large Elasticsearch, Trino or MongoDB table held the whole table in memory. From c8c647e75d61447f99422c358292bf8725c215bd Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 02:35:38 +0700 Subject: [PATCH 3/4] fix(plugins): keep the MongoDB stream comment to what holds for every reader --- apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift index 31cf9c0b4a..58222abc9c 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection.swift @@ -905,7 +905,7 @@ nonisolated final class MongoDBConnection: @unchecked Sendable { #if canImport(CLibMongoc) // The cursor, its collection and the stream's session live and end inside one block on the connection's queue, so // none of them can outlast the client that `disconnect` destroys behind it. A paced stream holds that queue while - // its reader waits, as every blocking driver does, which is why bulk reads run on their own pooled connection. + // its reader waits, as every blocking driver does. private func cursorStream( database: String, collection: String, From 4cdadb16588d566bda90508ca290d3572c850fe4 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Fri, 13 Nov 2026 03:18:52 +0700 Subject: [PATCH 4/4] fix(scripts): drop the unused class name from the stream pacing probe --- apple/scripts/probes/check-stream-idle-pacing.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apple/scripts/probes/check-stream-idle-pacing.sh b/apple/scripts/probes/check-stream-idle-pacing.sh index 1cb3fa7fb4..010f81bb49 100755 --- a/apple/scripts/probes/check-stream-idle-pacing.sh +++ b/apple/scripts/probes/check-stream-idle-pacing.sh @@ -51,16 +51,16 @@ C_MODULE="" LINK="" case "$ENGINE" in elasticsearch) - FOLDER=ElasticsearchDriverPlugin CLASS=ElasticsearchPlugin PORT_DEFAULT=9200 + FOLDER=ElasticsearchDriverPlugin PORT_DEFAULT=9200 PRODUCTS="TableProNumberFormatting TableProDocumentPath" ;; mongodb) - FOLDER=MongoDBDriverPlugin CLASS=MongoDBPlugin PORT_DEFAULT=27017 C_MODULE=CLibMongoc + FOLDER=MongoDBDriverPlugin PORT_DEFAULT=27017 C_MODULE=CLibMongoc PRODUCTS="TableProLogRedaction TableProSQLGrammar TableProNumberFormatting" LINK="$ROOT/Libs/libmongoc.a $ROOT/Libs/libbson.a -lssl.3 -lcrypto.3 -lresolv -lz" ;; trino) - FOLDER=TrinoDriverPlugin CLASS=TrinoPlugin PORT_DEFAULT=8080 + FOLDER=TrinoDriverPlugin PORT_DEFAULT=8080 PRODUCTS="TableProTLSClientIdentity TableProTrinoCore" ;; *)