Skip to content

Commit 07cc661

Browse files
authored
perf(plugins): read row streams only as fast as copies, compares and exports take them (#4227)
1 parent 035de0f commit 07cc661

41 files changed

Lines changed: 796 additions & 271 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apple/Packages/TableProCore/Sources/TableProQdrantClient/Wire/QdrantScrollPager.swift‎

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,17 @@ public extension QdrantClient {
5353
).points
5454
}
5555

56-
// Without vectors this reads payloads only, which is how an export learns every key before its header.
56+
// Without vectors this reads payloads only, which is how an export learns every key before its header. A page is
57+
// read only when the reader asks for the next one.
5758
func pointPages(
5859
collection: String,
5960
pageSize: Int,
6061
withVector: Bool = true
6162
) -> AsyncThrowingStream<[QdrantPoint], Error> {
62-
AsyncThrowingStream { continuation in
63-
let task = Task {
64-
do {
65-
var cursor: Any?
66-
repeat {
67-
try Task.checkCancellation()
68-
let page = try await self.scroll(
69-
collection: collection,
70-
body: Self.scrollBody(
71-
filter: nil, limit: max(pageSize, 1), cursor: cursor, withData: true, withVector: withVector
72-
)
73-
)
74-
if !page.points.isEmpty {
75-
continuation.yield(page.points)
76-
}
77-
cursor = page.nextCursor
78-
} while cursor != nil
79-
continuation.finish()
80-
} catch {
81-
continuation.finish(throwing: error)
82-
}
83-
}
84-
continuation.onTermination = { @Sendable _ in task.cancel() }
85-
}
63+
let pager = QdrantPointPager(
64+
client: self, collection: collection, pageSize: max(pageSize, 1), withVector: withVector
65+
)
66+
return AsyncThrowingStream(unfolding: { try await pager.next() })
8667
}
8768

8869
static func scrollBody(
@@ -106,3 +87,38 @@ public extension QdrantClient {
10687
return body
10788
}
10889
}
90+
91+
// One reader pulls the pages, so one call at a time reaches the cursor.
92+
private final class QdrantPointPager: @unchecked Sendable {
93+
private let client: QdrantClient
94+
private let collection: String
95+
private let pageSize: Int
96+
private let withVector: Bool
97+
private var cursor: Any?
98+
private var isDone = false
99+
100+
init(client: QdrantClient, collection: String, pageSize: Int, withVector: Bool) {
101+
self.client = client
102+
self.collection = collection
103+
self.pageSize = pageSize
104+
self.withVector = withVector
105+
}
106+
107+
func next() async throws -> [QdrantPoint]? {
108+
while !isDone {
109+
try Task.checkCancellation()
110+
let page = try await client.scroll(
111+
collection: collection,
112+
body: QdrantClient.scrollBody(
113+
filter: nil, limit: pageSize, cursor: cursor, withData: true, withVector: withVector
114+
)
115+
)
116+
cursor = page.nextCursor
117+
isDone = cursor == nil
118+
if !page.points.isEmpty {
119+
return page.points
120+
}
121+
}
122+
return nil
123+
}
124+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import Foundation
2+
import TableProPluginKit
3+
import Testing
4+
5+
struct PluginRowStreamTerminationTests {
6+
private static let header = PluginStreamHeader(columns: ["c"], columnTypeNames: ["TEXT"])
7+
8+
@Test("A stream its producer finished runs no abort action, even once the consumer lets it go")
9+
func finishedStreamRunsNoAbortAction() async throws {
10+
let probe = AbortProbe()
11+
var stream: AsyncThrowingStream<PluginStreamElement, Error>? = PluginRowStream.make { continuation, abort in
12+
probe.watch(abort)
13+
continuation.yield(.header(Self.header))
14+
continuation.yield(.rows([[.text("a")]]))
15+
continuation.finish()
16+
}
17+
var elements = 0
18+
if let stream {
19+
for try await _ in stream { elements += 1 }
20+
}
21+
stream = nil
22+
23+
#expect(elements == 2)
24+
#expect(probe.actions == 0)
25+
#expect(probe.abort?.isAborted == false)
26+
}
27+
28+
@Test("A stream its producer finished with an error runs no abort action")
29+
func failedStreamRunsNoAbortAction() async {
30+
let probe = AbortProbe()
31+
var stream: AsyncThrowingStream<PluginStreamElement, Error>? = PluginRowStream.make { continuation, abort in
32+
probe.watch(abort)
33+
continuation.yield(.header(Self.header))
34+
continuation.finish(throwing: ProducerFailure())
35+
}
36+
await #expect(throws: ProducerFailure.self) {
37+
if let stream {
38+
for try await _ in stream {}
39+
}
40+
}
41+
stream = nil
42+
43+
#expect(probe.actions == 0)
44+
}
45+
46+
@Test("A consumer that lets the stream go before its end runs the abort actions")
47+
func droppedStreamRunsTheAbortActions() async throws {
48+
let probe = AbortProbe()
49+
var stream: AsyncThrowingStream<PluginStreamElement, Error>? = PluginRowStream.make { continuation, abort in
50+
probe.watch(abort)
51+
continuation.yield(.header(Self.header))
52+
}
53+
var iterator = stream?.makeAsyncIterator()
54+
_ = try await iterator?.next()
55+
iterator = nil
56+
stream = nil
57+
58+
#expect(probe.actions == 1)
59+
#expect(probe.abort?.isAborted == true)
60+
}
61+
62+
@Test("A consumer task cancelled while it waits runs the abort actions")
63+
func cancelledConsumerRunsTheAbortActions() async throws {
64+
let probe = AbortProbe()
65+
let stream = PluginRowStream.make { continuation, abort in
66+
probe.watch(abort)
67+
continuation.yield(.header(Self.header))
68+
}
69+
let reader = Task {
70+
for try await _ in stream {}
71+
}
72+
reader.cancel()
73+
_ = try? await reader.value
74+
75+
#expect(probe.actions == 1)
76+
_ = stream
77+
}
78+
}
79+
80+
private struct ProducerFailure: Error {}
81+
82+
private final class AbortProbe: @unchecked Sendable {
83+
private let lock = NSLock()
84+
private var watched: PluginStreamAbort?
85+
private var count = 0
86+
87+
var abort: PluginStreamAbort? {
88+
lock.withLock { watched }
89+
}
90+
91+
var actions: Int {
92+
lock.withLock { count }
93+
}
94+
95+
func watch(_ abort: PluginStreamAbort) {
96+
lock.withLock { watched = abort }
97+
abort.onAbort { [self] in
98+
lock.withLock { count += 1 }
99+
}
100+
}
101+
}

‎apple/Packages/TableProOracle/Sources/TableProOracleCore/OracleCoreConnection.swift‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,6 +1004,16 @@ public final class OracleCoreConnection: @unchecked Sendable {
10041004
_ query: String,
10051005
batching: OracleRowBatcher,
10061006
continuation: AsyncThrowingStream<OracleStreamElement, Error>.Continuation
1007+
) async throws {
1008+
try await streamQuery(query, batching: batching, onElement: { continuation.yield($0) })
1009+
continuation.finish()
1010+
}
1011+
1012+
// The next row is read only once `onElement` returns, so a caller that waits in it holds the statement open.
1013+
public func streamQuery(
1014+
_ query: String,
1015+
batching: OracleRowBatcher,
1016+
onElement: @escaping @Sendable (OracleStreamElement) async -> Void
10071017
) async throws {
10081018
let role = OracleTransactionRole(of: query)
10091019
await queryGate.acquire()
@@ -1022,13 +1032,12 @@ public final class OracleCoreConnection: @unchecked Sendable {
10221032
regions: regions,
10231033
batching: batching,
10241034
on: connection,
1025-
continuation: continuation
1035+
onElement: onElement
10261036
)
10271037
}
10281038
await recordSuccess(of: role, on: admitted.session)
10291039
forgetSessionZonesIfChanged(by: query)
10301040
await queryGate.release()
1031-
continuation.finish()
10321041
} catch {
10331042
let mapped = mapExecutionError(error)
10341043
forgetSessionZonesIfChanged(by: query)
@@ -1045,33 +1054,33 @@ public final class OracleCoreConnection: @unchecked Sendable {
10451054
regions: OracleTimeZoneRegions,
10461055
batching: OracleRowBatcher,
10471056
on connection: OracleNIO.OracleConnection,
1048-
continuation: AsyncThrowingStream<OracleStreamElement, Error>.Continuation
1057+
onElement: @escaping @Sendable (OracleStreamElement) async -> Void
10491058
) async throws {
10501059
let statement = OracleStatement(unsafeSQL: query)
10511060
let stream = try await connection.execute(statement, options: options, logger: nioLogger)
10521061

10531062
let columns = stream.columns.map(OracleResultColumn.init)
10541063
var decoder = CellDecoder(columns: columns, zones: zones, regions: regions)
1055-
continuation.yield(.header(columns: columns.map(\.descriptor)))
1064+
await onElement(.header(columns: columns.map(\.descriptor)))
10561065

10571066
var batcher = batching
10581067
do {
10591068
for try await row in stream {
10601069
try Task.checkCancellation()
10611070
if let batch = batcher.append(decoder.decode(row)) {
1062-
continuation.yield(.rows(batch))
1071+
await onElement(.rows(batch))
10631072
}
10641073
}
10651074
} catch {
10661075
// The rows read before a failure still reach the reader ahead of the error, as they did one by one.
1067-
if let batch = batcher.remainder() { continuation.yield(.rows(batch)) }
1076+
if let batch = batcher.remainder() { await onElement(.rows(batch)) }
10681077
throw error
10691078
}
1070-
if let batch = batcher.remainder() { continuation.yield(.rows(batch)) }
1079+
if let batch = batcher.remainder() { await onElement(.rows(batch)) }
10711080
await warnOnce(about: decoder.unsupportedTypeNames)
10721081

10731082
guard columns.isEmpty else { return }
1074-
continuation.yield(.affectedRows(try await stream.affectedRows))
1083+
await onElement(.affectedRows(try await stream.affectedRows))
10751084
}
10761085

10771086
// MARK: - Session Time Zones

‎apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,11 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
451451
}
452452

453453
func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
454-
AsyncThrowingStream { continuation in
455-
Task {
454+
PluginRowStream.make { continuation, abort in
455+
let task = Task {
456456
do {
457-
let result = try await perform { [self] () -> PluginQueryResult in
457+
try Task.checkCancellation()
458+
let result = try await self.perform { [self] () -> PluginQueryResult in
458459
if let bql = Self.extractBQLQuery(from: query) {
459460
return try executeBQL(query: bql)
460461
}
@@ -473,6 +474,7 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
473474
continuation.finish(throwing: error)
474475
}
475476
}
477+
abort.onAbort { task.cancel() }
476478
}
477479
}
478480

‎apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ extension BigQueryPluginDriver {
8383
}
8484

8585
func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
86-
AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in
86+
PluginRowStream.makeBounded { continuation, abort in
8787
let streamTask = Task {
8888
do {
8989
try await self.runningStatements.run {
@@ -93,9 +93,7 @@ extension BigQueryPluginDriver {
9393
continuation.finish(throwing: BigQueryError.wrap(error))
9494
}
9595
}
96-
continuation.onTermination = { @Sendable _ in
97-
streamTask.cancel()
98-
}
96+
abort.onAbort { streamTask.cancel() }
9997
}
10098
}
10199

@@ -260,7 +258,7 @@ extension BigQueryPluginDriver {
260258

261259
let firstRows = BigQueryTypeMapper.flattenRows(from: firstPage, schema: schema)
262260
if !firstRows.isEmpty {
263-
continuation.yield(.rows(firstRows))
261+
_ = await PluginRowStream.deliver(.rows(firstRows), to: continuation)
264262
}
265263

266264
var pageToken = firstPage.pageToken
@@ -273,7 +271,7 @@ extension BigQueryPluginDriver {
273271
)
274272
let nextRows = BigQueryTypeMapper.flattenRows(from: nextPage, schema: schema)
275273
if !nextRows.isEmpty {
276-
continuation.yield(.rows(nextRows))
274+
_ = await PluginRowStream.deliver(.rows(nextRows), to: continuation)
277275
}
278276
pageToken = nextPage.pageToken
279277
}

‎apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extension BigtablePluginDriver {
149149
// MARK: - Export
150150

151151
func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
152-
AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in
152+
PluginRowStream.makeBounded { continuation, abort in
153153
let task = Task {
154154
do {
155155
if case .browse(let spec)? = try? BigtableStatement.parse(query), spec.limit == 0 {
@@ -168,7 +168,7 @@ extension BigtablePluginDriver {
168168
continuation.finish(throwing: error)
169169
}
170170
}
171-
continuation.onTermination = { @Sendable _ in task.cancel() }
171+
abort.onAbort { task.cancel() }
172172
}
173173
}
174174

@@ -190,7 +190,7 @@ extension BigtablePluginDriver {
190190
)))
191191
}
192192
if !rows.isEmpty {
193-
continuation.yield(.rows(page.rows(alignedTo: columns).map { $0.map(Self.cell) }))
193+
_ = await PluginRowStream.deliver(.rows(page.rows(alignedTo: columns).map { $0.map(Self.cell) }), to: continuation)
194194
}
195195
after = rows.count == Self.exportPageSize ? rows.last?.key : nil
196196
} while after != nil

‎apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -959,10 +959,8 @@ actor CassandraConnectionActor {
959959
return "Unknown error"
960960
}
961961

962-
/// The whole loop is one non-suspending actor job holding the cooperative-pool thread, and
963-
/// cass_future_wait blocks it, so nothing outside can interrupt a page. `abort` is the only
964-
/// way the consumer's departure reaches this body, and it is read between pages and between
965-
/// rows rather than mid-request.
962+
// One non-suspending actor job, blocked in cass_future_wait and in `deliverBlocking`, so the consumer's departure
963+
// reaches it only through `abort`, read between pages and between rows rather than mid-request.
966964
func streamQuery(
967965
_ cql: String,
968966
consistency: CassandraConsistency?,
@@ -1040,6 +1038,7 @@ actor CassandraConnectionActor {
10401038

10411039
let colCount = cass_result_column_count(result)
10421040
let iterator = cass_iterator_from_result(result)
1041+
var page: [PluginRow] = []
10431042

10441043
if let iterator {
10451044
var rowsThisPage = 0
@@ -1051,7 +1050,7 @@ actor CassandraConnectionActor {
10511050
}
10521051
let row = cass_iterator_get_row(iterator)
10531052
guard let row else { continue }
1054-
continuation.yield(.rows([Self.decodeRow(row, columnCount: colCount)]))
1053+
page.append(Self.decodeRow(row, columnCount: colCount))
10551054
}
10561055
cass_iterator_free(iterator)
10571056
}
@@ -1064,6 +1063,10 @@ actor CassandraConnectionActor {
10641063

10651064
cass_result_free(result)
10661065

1066+
if !page.isEmpty, !aborted {
1067+
_ = PluginRowStream.deliverBlocking(.rows(page), to: continuation)
1068+
}
1069+
10671070
if !hasMore || aborted { break }
10681071
}
10691072

0 commit comments

Comments
 (0)