Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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<PluginStreamElement, Error>? = 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<PluginStreamElement, Error>? = 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<PluginStreamElement, Error>? = 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 }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,16 @@ public final class OracleCoreConnection: @unchecked Sendable {
_ query: String,
batching: OracleRowBatcher,
continuation: AsyncThrowingStream<OracleStreamElement, Error>.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()
Expand All @@ -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)
Expand All @@ -1045,33 +1054,33 @@ public final class OracleCoreConnection: @unchecked Sendable {
regions: OracleTimeZoneRegions,
batching: OracleRowBatcher,
on connection: OracleNIO.OracleConnection,
continuation: AsyncThrowingStream<OracleStreamElement, Error>.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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,11 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
}

func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
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)
}
Expand All @@ -473,6 +474,7 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable {
continuation.finish(throwing: error)
}
}
abort.onAbort { task.cancel() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension BigQueryPluginDriver {
}

func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in
PluginRowStream.makeBounded { continuation, abort in
let streamTask = Task {
do {
try await self.runningStatements.run {
Expand All @@ -93,9 +93,7 @@ extension BigQueryPluginDriver {
continuation.finish(throwing: BigQueryError.wrap(error))
}
}
continuation.onTermination = { @Sendable _ in
streamTask.cancel()
}
abort.onAbort { streamTask.cancel() }
}
}

Expand Down Expand Up @@ -260,7 +258,7 @@ extension BigQueryPluginDriver {

let firstRows = BigQueryTypeMapper.flattenRows(from: firstPage, schema: schema)
if !firstRows.isEmpty {
continuation.yield(.rows(firstRows))
_ = await PluginRowStream.deliver(.rows(firstRows), to: continuation)
}

var pageToken = firstPage.pageToken
Expand All @@ -273,7 +271,7 @@ extension BigQueryPluginDriver {
)
let nextRows = BigQueryTypeMapper.flattenRows(from: nextPage, schema: schema)
if !nextRows.isEmpty {
continuation.yield(.rows(nextRows))
_ = await PluginRowStream.deliver(.rows(nextRows), to: continuation)
}
pageToken = nextPage.pageToken
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension BigtablePluginDriver {
// MARK: - Export

func streamRows(query: String) -> AsyncThrowingStream<PluginStreamElement, Error> {
AsyncThrowingStream(bufferingPolicy: .unbounded) { continuation in
PluginRowStream.makeBounded { continuation, abort in
let task = Task {
do {
if case .browse(let spec)? = try? BigtableStatement.parse(query), spec.limit == 0 {
Expand All @@ -168,7 +168,7 @@ extension BigtablePluginDriver {
continuation.finish(throwing: error)
}
}
continuation.onTermination = { @Sendable _ in task.cancel() }
abort.onAbort { task.cancel() }
}
}

Expand All @@ -190,7 +190,7 @@ extension BigtablePluginDriver {
)))
}
if !rows.isEmpty {
continuation.yield(.rows(page.rows(alignedTo: columns).map { $0.map(Self.cell) }))
_ = 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
Expand Down
13 changes: 8 additions & 5 deletions apple/Plugins/CassandraDriverPlugin/CassandraConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,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 `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,
consistency: CassandraConsistency?,
Expand Down Expand Up @@ -1040,6 +1038,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
Expand All @@ -1051,7 +1050,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)
}
Expand All @@ -1064,6 +1063,10 @@ actor CassandraConnectionActor {

cass_result_free(result)

if !page.isEmpty, !aborted {
_ = PluginRowStream.deliverBlocking(.rows(page), to: continuation)
}

if !hasMore || aborted { break }
}

Expand Down
Loading
Loading