diff --git a/apple/Packages/TableProCore/Sources/TableProWireIO/SpoolFile.swift b/apple/Packages/TableProCore/Sources/TableProWireIO/SpoolFile.swift new file mode 100644 index 0000000000..a3240ce7d0 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProWireIO/SpoolFile.swift @@ -0,0 +1,178 @@ +#if canImport(Darwin) +import Darwin +import Foundation + +public enum SpoolFileError: Error, Equatable, Sendable, CustomStringConvertible, LocalizedError { + case cannotCreate(errno: Int32) + case writeFailed(errno: Int32) + case readFailed(errno: Int32) + + public var description: String { + switch self { + case .cannotCreate(let code): return "could not create a temporary file: \(String(cString: strerror(code)))" + case .writeFailed(let code): return "could not write a temporary file: \(String(cString: strerror(code)))" + case .readFailed(let code): return "could not read a temporary file: \(String(cString: strerror(code)))" + } + } + + // The system's own localized text for the errno. + public var errorDescription: String? { + switch self { + case .cannotCreate(let code), .writeFailed(let code), .readFailed(let code): + return NSError(domain: NSPOSIXErrorDomain, code: Int(code)).localizedDescription + } + } +} + +// Records read back in append order, in a file mkostemp makes 0600 and that is unlinked at once, so nothing is left +// after a crash or quit. The descriptor closes only in deinit, so no read meets a reused number. +public final class SpoolFile: @unchecked Sendable { + private static let headerSize = 8 + private static let readChunk = 64 * 1_024 + + private let descriptor: Int32 + // One thread appends while another reads; the lock covers what the two share and the truncate, not reads or writes. + private let lock = NSLock() + private var written = 0 + private var appending = false + private var readBuffer: [UInt8] = [] + private var readStart = 0 + private var fileReadOffset = 0 + + public init(directory: URL = FileManager.default.temporaryDirectory, prefix: String = "TablePro-spool") throws { + var template = Array(directory.appending(path: "\(prefix)-XXXXXX").path(percentEncoded: false).utf8CString) + let descriptor = template.withUnsafeMutableBufferPointer { buffer in + buffer.baseAddress.map { mkostemp($0, O_CLOEXEC) } ?? -1 + } + guard descriptor >= 0 else { throw SpoolFileError.cannotCreate(errno: errno) } + guard template.withUnsafeBufferPointer({ unlink($0.baseAddress) }) == 0 else { + let code = errno + close(descriptor) + throw SpoolFileError.cannotCreate(errno: code) + } + self.descriptor = descriptor + } + + deinit { + close(descriptor) + } + + // The bytes on disk, length prefixes included. Zero again once the reader has taken every record. + public var byteCount: Int { + lock.withLock { written } + } + + // A failed append leaves every earlier record readable and adds nothing. + public func append(_ record: some ContiguousBytes) throws { + let offset = lock.withLock { + appending = true + return written + } + do { + let end = try record.withUnsafeBytes { body in + var header = UInt64(body.count).bigEndian + if body.count <= Self.readChunk { + var bytes = withUnsafeBytes(of: &header) { [UInt8]($0) } + bytes.append(contentsOf: body) + try bytes.withUnsafeBytes { try write($0, at: offset) } + } else { + try withUnsafeBytes(of: &header) { try write($0, at: offset) } + try write(body, at: offset + Self.headerSize) + } + return offset + Self.headerSize + body.count + } + lock.withLock { + written = end + appending = false + } + } catch { + lock.withLock { appending = false } + throw error + } + } + + // The next record, or nil once every record appended so far has been read. + public func next() throws -> [UInt8]? { + let end = lock.withLock { written } + let position = fileReadOffset - (readBuffer.count - readStart) + guard position < end else { return nil } + let header = try take(Self.headerSize, end: end) + let length = header.reduce(0) { $0 << 8 | Int($1) } + guard (0...(end - position - Self.headerSize)).contains(length) else { + throw SpoolFileError.readFailed(errno: EIO) + } + let record = try take(length, end: end) + if fileReadOffset - (readBuffer.count - readStart) == end { + emptyFileIfIdle(at: end) + } + return record + } + + // Only while no append runs and none has landed since `end`, so the writer's next offset is the new start. + private func emptyFileIfIdle(at end: Int) { + lock.withLock { + guard !appending, written == end, ftruncate(descriptor, 0) == 0 else { return } + written = 0 + fileReadOffset = 0 + readStart = 0 + readBuffer.removeAll() + } + } + + private func take(_ count: Int, end: Int) throws -> [UInt8] { + let buffered = readBuffer.count - readStart + if buffered >= count { + defer { readStart += count } + return Array(readBuffer[readStart..<(readStart + count)]) + } + if count - buffered > Self.readChunk { + var record = Array(readBuffer[readStart...]) + readBuffer.removeAll(keepingCapacity: true) + readStart = 0 + record.append(contentsOf: repeatElement(0, count: count - buffered)) + try read(into: &record, from: buffered, at: fileReadOffset) + fileReadOffset += count - buffered + return record + } + readBuffer.removeSubrange(0.. Int in + guard let base = raw.baseAddress else { return -1 } + return pread(descriptor, base + start + done, count - done, off_t(offset + done)) + } + if read < 0, errno == EINTR { continue } + guard read > 0 else { throw SpoolFileError.readFailed(errno: read == 0 ? EIO : errno) } + done += read + } + } + + private func write(_ bytes: UnsafeRawBufferPointer, at offset: Int) throws { + guard let base = bytes.baseAddress else { return } + var done = 0 + while done < bytes.count { + let wrote = pwrite(descriptor, base + done, bytes.count - done, off_t(offset + done)) + if wrote < 0, errno == EINTR { continue } + guard wrote > 0 else { throw SpoolFileError.writeFailed(errno: wrote == 0 ? EIO : errno) } + done += wrote + } + } +} +#endif diff --git a/apple/Packages/TableProCore/Tests/TableProWireIOTests/SpoolFileTests.swift b/apple/Packages/TableProCore/Tests/TableProWireIOTests/SpoolFileTests.swift new file mode 100644 index 0000000000..da198119df --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProWireIOTests/SpoolFileTests.swift @@ -0,0 +1,104 @@ +#if canImport(Darwin) +import Darwin +import Foundation +import TableProWireIO +import Testing + +private func withScratchDirectory(_ body: (URL) async throws -> Void) async throws { + let directory = FileManager.default.temporaryDirectory.appending(path: "spool-tests-\(UUID().uuidString)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + try await body(directory) +} + +// The open descriptors whose file was made with this prefix, named or not: F_GETPATH still names an unlinked file. +private func openFiles(prefix: String) -> [(nlink: UInt16, mode: mode_t)] { + (0.. [UInt8] = { [UInt8](repeating: UInt8($0 % 251), count: 1 + ($0 * 37) % 90_000) } + let appended = DispatchSemaphore(value: 0) + Thread.detachNewThread { + for index in 0.. PluginQueryResult { @@ -377,33 +378,30 @@ extension DynamoDBPluginDriver { return } - let spoolURL = FileManager.default.temporaryDirectory - .appendingPathComponent("TablePro-DynamoDB-\(UUID().uuidString).jsonl") - guard FileManager.default.createFile(atPath: spoolURL.path, contents: nil) else { + let spool: SpoolFile + do { + spool = try SpoolFile(prefix: "TablePro-DynamoDB") + } catch { throw DynamoDBError.transport(String(localized: "Could not create a temporary file for the export")) } - defer { try? FileManager.default.removeItem(at: spoolURL) } let sortsLocally = !source.clientOrder.isEmpty let readOffset = sortsLocally ? 0 : source.window.offset let readLimit = sortsLocally ? Int.max : max(source.window.limit ?? Int.max, 0) - var collector = SpoolCollector(handle: try FileHandle(forWritingTo: spoolURL)) + var collector = SpoolCollector(spool: spool) try await source.read(readOffset, readLimit) { item in try collector.add(item) return true } - try collector.handle.close() let columns = collector.columns(preferred: source.preferredColumns, schema: source.schema) continuation.yield(.header(PluginStreamHeader(columns: columns, columnTypeNames: collector.typeNames(for: columns, schema: source.schema)))) if sortsLocally { var items: [DynamoDBItem] = [] - var lines = try SpoolLines(url: spoolURL) - defer { lines.close() } - while let line = try lines.next() { + while let record = try spool.next() { try Task.checkCancellation() - items.append(try DynamoDBItem(wireItem: DynamoDBJSON.parse(line))) + items.append(try DynamoDBItem(wireItem: DynamoDBJSON.parse(Data(record)))) } let sorted = DynamoDBItemTable.sorted(items, by: source.clientOrder) let end = source.window.limit.map { limit in @@ -421,11 +419,9 @@ extension DynamoDBPluginDriver { var batch: [PluginRow] = [] batch.reserveCapacity(1_000) - var lines = try SpoolLines(url: spoolURL) - defer { lines.close() } - while let line = try lines.next() { + while let record = try spool.next() { try Task.checkCancellation() - let item = try DynamoDBItem(wireItem: DynamoDBJSON.parse(line)) + let item = try DynamoDBItem(wireItem: DynamoDBJSON.parse(Data(record))) batch.append(columns.map { DynamoDBCellCodec.cell(for: item[$0]) }) if batch.count == 1_000 { _ = await PluginRowStream.deliver(.rows(batch), to: continuation) @@ -519,62 +515,18 @@ extension DynamoDBPluginDriver { } } -/// Reads the spool file back one line at a time with plain reads. -/// -/// `URL.lines` would read it through `FileHandle.AsyncBytes`, and Foundation serves every AsyncBytes reader in the -/// process from one serial queue. A reader blocked on a pipe that stays quiet, such as a language server's output, -/// holds that queue, and the export then waits for it. -private struct SpoolLines { - private static let chunkSize = 1 << 20 - - private let handle: FileHandle - private var buffer = Data() - private var start = 0 - private var reachedEnd = false - - init(url: URL) throws { - handle = try FileHandle(forReadingFrom: url) - } - - mutating func next() throws -> Data? { - while true { - if let newline = buffer[start...].firstIndex(of: UInt8(ascii: "\n")) { - let line = buffer[start..() private var typeCounts: [String: [DynamoDBAttributeType: Int]] = [:] - init(handle: FileHandle) { - self.handle = handle + init(spool: SpoolFile) { + self.spool = spool } mutating func add(_ item: DynamoDBItem) throws { - try handle.write(contentsOf: Data((item.wireJSON.serialized() + "\n").utf8)) + try spool.append(Array(item.wireJSON.serialized().utf8)) for (name, value) in item { attributes.insert(name) guard value != .null else { continue } diff --git a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift index 2b0fbfb0bc..a726914a4f 100644 --- a/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift +++ b/apple/Plugins/LibSQLDriverPlugin/LibSQLPluginDriver.swift @@ -284,44 +284,45 @@ final class LibSQLPluginDriver: PluginDatabaseDriver, @unchecked Sendable { func streamRows(query: String) -> AsyncThrowingStream { PluginRowStream.makeBounded { continuation, abort in + let spill = PluginRowSpill(continuation: continuation) let streamTask = Task { do { - try await self.performStreamRows(query: query, continuation: continuation) + try await self.performStreamRows(query: query, into: spill) } catch { - continuation.finish(throwing: error) + spill.finish(throwing: error) } } - abort.onAbort { streamTask.cancel() } + abort.onAbort { + spill.abandon() + streamTask.cancel() + } } } - private func performStreamRows( - query: String, - continuation: AsyncThrowingStream.Continuation - ) async throws { + private func performStreamRows(query: String, into spill: PluginRowSpill) async throws { switch getBackend() { case .remote(let client): let result = try await client.execute(sql: query) let columns = result.cols.map(\.name) let columnTypeNames = result.cols.map { $0.decltype ?? "" } - continuation.yield(.header(PluginStreamHeader( + spill.yield(PluginStreamHeader( columns: columns, columnTypeNames: columnTypeNames, estimatedRowCount: nil - ))) + )) if !result.rows.isEmpty { let rows = result.rows.map { rawRow in rawRow.map(\.stringValue).map(PluginCellValue.fromOptional) } - continuation.yield(.rows(rows)) + spill.deliver(rows) } - continuation.finish() + spill.finish() case .local(let localBackend): - try await localBackend.streamQuery(query, continuation: continuation) + try await localBackend.streamQuery(query, into: spill) case nil: throw LibSQLError.notConnected } diff --git a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift index 8994f7d575..fd456427d9 100644 --- a/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift +++ b/apple/Plugins/LibSQLDriverPlugin/SQLiteLocalBackend.swift @@ -149,10 +149,7 @@ actor SQLiteLocalBackend { ) } - func streamQuery( - _ query: String, - continuation: AsyncThrowingStream.Continuation - ) throws { + func streamQuery(_ query: String, into spill: PluginRowSpill) throws { guard let db else { throw LibSQLError.notConnected } @@ -166,11 +163,11 @@ actor SQLiteLocalBackend { let firstStep = runtime.stepFirst(statement) let columnCount = firstStep.count - continuation.yield(.header(PluginStreamHeader( + spill.yield(PluginStreamHeader( columns: firstStep.names, columnTypeNames: firstStep.typeNames, estimatedRowCount: nil - ))) + )) let batchSize = 5_000 var batch: [PluginRow] = [] @@ -180,32 +177,32 @@ actor SQLiteLocalBackend { while stepResult == SQLITE_ROW { if Task.isCancelled { if !batch.isEmpty { - _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) + spill.deliver(batch) } - continuation.finish(throwing: CancellationError()) + spill.finish(throwing: CancellationError()) return } batch.append(rowValues(of: statement, count: columnCount)) if batch.count >= batchSize { - // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. - _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) + // Synchronous on the actor's thread: an await here would let another call reach the handle mid-statement. + guard spill.deliver(batch) else { return } batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) } if !batch.isEmpty { - _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) + spill.deliver(batch) } do { try validateCompletion(stepResult, on: db) } catch { - continuation.finish(throwing: error) + spill.finish(throwing: error) return } - continuation.finish() + spill.finish() } private func validateCompletion(_ stepResult: Int32, on db: OpaquePointer) throws { diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift index 140b8615e9..156fe655af 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteAgentBackend.swift @@ -143,11 +143,7 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { ) } - func streamQuery( - _ query: String, - rules: SQLiteStatementRules, - continuation: AsyncThrowingStream.Continuation - ) async throws { + func streamQuery(_ query: String, rules: SQLiteStatementRules, into spill: PluginRowSpill) async throws { await takeTurn() defer { endTurn() } try Task.checkCancellation() @@ -160,11 +156,11 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { scope: Self.agentScope(rules), rowCap: 0, onHeader: { header in - continuation.yield(.header(PluginStreamHeader( + spill.yield(PluginStreamHeader( columns: header.map(\.name), columnTypeNames: header.map { $0.declaredType ?? "" }, estimatedRowCount: nil - ))) + )) }, onRows: { columnCount, values in guard columnCount > 0 else { return } @@ -174,14 +170,14 @@ actor SQLiteAgentBackend: SQLiteExecutionBackend { batch.append(values[index..<(index + columnCount)].map(Self.decode)) index += columnCount } - // Blocks the reader thread, so the channel and the agent wait too; heartbeats go out on their own timer. - _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) + // The spill takes rows the stream cannot, so the agent's statement ends as fast as the session carries them. + spill.deliver(batch) } ) } onCancel: { connection.sendCancel() } - continuation.finish() + spill.finish() } private func takeTurn() async { diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift index d0e4a7b6a3..84fff86cad 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLiteExecutionBackend.swift @@ -46,11 +46,7 @@ protocol SQLiteExecutionBackend: Actor { rules: SQLiteStatementRules, bound: SQLiteReadBound? ) async throws -> SQLiteRawResult - func streamQuery( - _ query: String, - rules: SQLiteStatementRules, - continuation: AsyncThrowingStream.Continuation - ) async throws + func streamQuery(_ query: String, rules: SQLiteStatementRules, into spill: PluginRowSpill) async throws } // How one statement runs: the authorizer scope, and whether read-only, with `PRAGMA query_only` around it and @@ -131,13 +127,6 @@ extension SQLiteExecutionBackend { func executeParameterizedQuery(_ query: String, parameters: [PluginCellValue]) async throws -> SQLiteRawResult { try await executeQuery(query, parameters: parameters, rules: .unrestricted, bound: nil) } - - func streamQuery( - _ query: String, - continuation: AsyncThrowingStream.Continuation - ) async throws { - try await streamQuery(query, rules: .unrestricted, continuation: continuation) - } } struct SQLiteRawResult: Sendable { @@ -507,13 +496,9 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { return Int(sqlite3_changes64(db)) } - func streamQuery( - _ query: String, - rules: SQLiteStatementRules, - continuation: AsyncThrowingStream.Continuation - ) throws { + func streamQuery(_ query: String, rules: SQLiteStatementRules, into spill: PluginRowSpill) throws { try withStatementHandle(rules) { handle, notReadRefusal in - try stream(query, on: handle, notReadRefusal: notReadRefusal, continuation: continuation) + try stream(query, on: handle, notReadRefusal: notReadRefusal, into: spill) } } @@ -521,7 +506,7 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { _ query: String, on db: OpaquePointer, notReadRefusal: SQLitePluginError?, - continuation: AsyncThrowingStream.Continuation + into spill: PluginRowSpill ) throws { let statement = try Self.prepareOnlyStatement(query, db: db) // EXPLAIN never runs its statement, but stmt_readonly reports that statement's writes. @@ -536,7 +521,7 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { columns: firstStep.names, columnTypeNames: firstStep.typeNames, estimatedRowCount: nil ) header.columnOrigins = Self.columnOrigins(after: firstStep, of: statement, on: db, text: query) - continuation.yield(.header(header)) + spill.yield(header) let batchSize = 5_000 var batch: [PluginRow] = [] @@ -545,30 +530,33 @@ actor SQLiteLocalBackend: SQLiteExecutionBackend { var stepResult = firstStep.result while stepResult == SQLITE_ROW { if Task.isCancelled { - if !batch.isEmpty { _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } + if !batch.isEmpty { spill.deliver(batch) } sqlite3_finalize(statement) - continuation.finish(throwing: CancellationError()) + spill.finish(throwing: CancellationError()) return } batch.append(Self.readRow(statement, columnCount: columnCount)) if batch.count >= batchSize { - // Blocks the actor's thread: an await here would let another call reach the handle mid-statement. - _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) + // Synchronous on the actor's thread: an await here would let another call reach the handle mid-statement. + guard spill.deliver(batch) else { + sqlite3_finalize(statement) + return + } batch.removeAll(keepingCapacity: true) } stepResult = sqlite3_step(statement) } - if !batch.isEmpty { _ = PluginRowStream.deliverBlocking(.rows(batch), to: continuation) } + if !batch.isEmpty { spill.deliver(batch) } guard stepResult == SQLITE_DONE else { let message = String(cString: sqlite3_errmsg(db)) sqlite3_finalize(statement) - continuation.finish(throwing: SQLitePluginError.queryFailed(message)) + spill.finish(throwing: SQLitePluginError.queryFailed(message)) return } sqlite3_finalize(statement) - continuation.finish() + spill.finish() } private func bind(_ parameters: [PluginCellValue], to statement: OpaquePointer?, db: OpaquePointer) throws { diff --git a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift index 4518b7c9f1..c4175d87dc 100644 --- a/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift +++ b/apple/Plugins/SQLiteDriverPlugin/SQLitePlugin.swift @@ -719,14 +719,16 @@ final class SQLitePluginDriver: PluginDatabaseDriver, @unchecked Sendable { private func stream(_ query: String, rules: SQLiteStatementRules) -> AsyncThrowingStream { let queryToRun = String(query) return AsyncThrowingStream(bufferingPolicy: .bufferingOldest(PluginRowStream.boundedBufferBatches)) { continuation in + let spill = PluginRowSpill(continuation: continuation) let streamTask = Task { do { - try await self.backend.streamQuery(queryToRun, rules: rules, continuation: continuation) + try await self.backend.streamQuery(queryToRun, rules: rules, into: spill) } catch { - continuation.finish(throwing: error) + spill.finish(throwing: error) } } continuation.onTermination = { @Sendable _ in + spill.abandon() streamTask.cancel() } } diff --git a/apple/Plugins/SQLiteDriverTests/SQLiteAgentStreamPacingTests.swift b/apple/Plugins/SQLiteDriverTests/SQLiteAgentStreamPacingTests.swift index cad42eb000..7d20e53097 100644 --- a/apple/Plugins/SQLiteDriverTests/SQLiteAgentStreamPacingTests.swift +++ b/apple/Plugins/SQLiteDriverTests/SQLiteAgentStreamPacingTests.swift @@ -14,12 +14,14 @@ import Testing struct SQLiteAgentStreamPacingTests { // About 54 MB on the wire, several times what the stream, both loopback sockets (up to 4 MB each) and the pipe hold. private static let rowCount = 250_000 - private static let bufferedBytesLimit = 12_000_000 private static let watchdogSeconds = 2.0 private static let heartbeatSeconds = 0.25 // Table order with no sort, so the first rows go out at once. private static let scan = "SELECT id, payload FROM t ORDER BY rowid" + // Never ends on its own, so the agent is still reading it whenever the test looks. + private static let endless = + "WITH RECURSIVE g(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM g) SELECT x, hex(randomblob(100)) FROM g" private static func scaledProgram() throws -> [String] { let deadline = "IDLE_DEADLINE_SECONDS = 60.0" @@ -89,21 +91,6 @@ struct SQLiteAgentStreamPacingTests { await backend.close() } - // The agent waits once the bytes it sent stop growing for a while. - private static func waitUntilAgentWaits(_ loopback: SQLiteAgentLoopback) async throws { - var last = -1 - var steadySince = ContinuousClock.now - try await waitUntil(timeout: .seconds(30)) { - let now = loopback.agentBytes - if now != last { - last = now - steadySince = .now - return false - } - return now > 0 && ContinuousClock.now - steadySince > .milliseconds(500) - } - } - private struct NoAnswer: Error {} // A statement queued behind a stream that never ends ignores the time limit's cancellation, so it gets its own. @@ -137,14 +124,17 @@ struct SQLiteAgentStreamPacingTests { } } - @Test("A reader held past the agent's watchdog still gets every row, in order, on a live session") + @Test("A reader held past the agent's watchdog gets every row, in order, and the agent's read ends without it") func heldReaderOutlastsTheWatchdog() async throws { try await Self.withAgent(arguments: try Self.scaledProgram()) { fixture in var iterator = fixture.driver.streamRows(query: Self.scan).makeAsyncIterator() var read = OrderedRead() while read.expected == 1, let element = try await iterator.next() { read.take(element) } - try await Self.waitUntilAgentWaits(fixture.loopback) + // The agent read the whole table into the spill, so the connection takes the next statement at once. + let driver = fixture.driver + #expect(try await Self.answer { try await driver.execute(query: "SELECT count(*) FROM t") } == String(Self.rowCount)) + // Long enough for the watchdog to have ended the session twice over without heartbeats. let heartbeatsBeforeHold = fixture.loopback.heartbeats let holdHeartbeats = Int(Self.watchdogSeconds * 2.5 / Self.heartbeatSeconds) @@ -152,70 +142,68 @@ struct SQLiteAgentStreamPacingTests { fixture.loopback.heartbeats - heartbeatsBeforeHold >= holdHeartbeats } #expect(fixture.loopback.agentIsRunning) - let heldBytes = fixture.loopback.agentBytes while let element = try await iterator.next() { read.take(element) } #expect(read.expected - 1 == Self.rowCount) #expect(read.mismatches == 0) - #expect(heldBytes < Self.bufferedBytesLimit, "the agent sent \(heldBytes) of \(fixture.loopback.agentBytes) bytes by the end of the hold") - - let driver = fixture.driver - #expect(try await Self.answer { try await driver.execute(query: "SELECT count(*) FROM t") } == String(Self.rowCount)) + #expect(try await Self.answer { try await driver.execute(query: "SELECT 42") } == "42") } } - @Test("Without heartbeats the same hold outlasts the watchdog and the read fails") - func holdWithoutHeartbeatsLosesTheSession() async throws { + @Test("Without heartbeats an idle session outlasts the watchdog and its next statement fails") + func idleWithoutHeartbeatsLosesTheSession() async throws { try await Self.withAgent(arguments: try Self.scaledProgram(), heartbeat: 3_600) { fixture in - var iterator = fixture.driver.streamRows(query: Self.scan).makeAsyncIterator() - var read = OrderedRead() - // On a loaded machine the watchdog can end the session before the first batch arrives. - await #expect(throws: (any Error).self) { - while read.expected == 1, let element = try await iterator.next() { read.take(element) } - try await waitUntil(timeout: .seconds(30)) { !fixture.loopback.agentIsRunning } - while let element = try await iterator.next() { read.take(element) } + _ = try await fixture.driver.execute(query: "SELECT 1") + try await waitUntil(timeout: .seconds(30)) { !fixture.loopback.agentIsRunning } + + let driver = fixture.driver + let failed = await BoundedCall.result(within: .seconds(5)) { + do { + _ = try await driver.execute(query: "SELECT 1") + return false + } catch { + return true + } } - #expect(!fixture.loopback.agentIsRunning) - #expect(read.expected - 1 < Self.rowCount) + #expect(failed == true) } } @Test("A reader that leaves stops the agent's read, and the next statement gets its own answer") func abandonedReadStopsTheAgent() async throws { try await Self.withAgent { fixture in - let bytesBeforeRead = fixture.loopback.agentBytes var iterator: AsyncThrowingStream.Iterator? = - fixture.driver.streamRows(query: Self.scan).makeAsyncIterator() + fixture.driver.streamRows(query: Self.endless).makeAsyncIterator() var rows = 0 while rows == 0, let element = try await iterator?.next() { if case .rows(let batch) = element { rows += batch.count } } - try await Self.waitUntilAgentWaits(fixture.loopback) iterator = nil let driver = fixture.driver #expect(try await Self.answer { try await driver.execute(query: "SELECT 42") } == "42") #expect(fixture.loopback.agentIsRunning) - let readBytes = fixture.loopback.agentBytes - bytesBeforeRead - #expect(readBytes < Self.bufferedBytesLimit, "the agent sent \(readBytes) bytes") } } - @Test("A statement sent while a stream is open waits for it instead of taking its rows") + @Test("A statement sent while the agent is still reading a stream waits for it instead of taking its rows") func statementWaitsForTheOpenStream() async throws { try await Self.withAgent { fixture in - var iterator = fixture.driver.streamRows(query: Self.scan).makeAsyncIterator() - var read = OrderedRead() - while read.expected == 1, let element = try await iterator.next() { read.take(element) } + var iterator: AsyncThrowingStream.Iterator? = + fixture.driver.streamRows(query: Self.endless).makeAsyncIterator() + var rows = 0 + while rows == 0, let element = try await iterator?.next() { + if case .rows(let batch) = element { rows += batch.count } + } let driver = fixture.driver let statement = Task { try await driver.execute(query: "SELECT 42") } - try await Self.waitUntilAgentWaits(fixture.loopback) + // The reader holds, and the agent keeps reading into the spill while the statement stays queued. + let bytesWhenSent = fixture.loopback.agentBytes + try await waitUntil(timeout: .seconds(30)) { fixture.loopback.agentBytes > bytesWhenSent + 20_000_000 } #expect(fixture.loopback.requests == 1, "the statement went out while the stream was open") - while let element = try await iterator.next() { read.take(element) } - #expect(read.expected - 1 == Self.rowCount) - #expect(read.mismatches == 0) + iterator = nil #expect(try await Self.answer { try await statement.value } == "42") } } diff --git a/apple/Plugins/SQLiteDriverTests/SQLiteRowSpillTests.swift b/apple/Plugins/SQLiteDriverTests/SQLiteRowSpillTests.swift new file mode 100644 index 0000000000..f97823f276 --- /dev/null +++ b/apple/Plugins/SQLiteDriverTests/SQLiteRowSpillTests.swift @@ -0,0 +1,348 @@ +// +// SQLiteRowSpillTests.swift +// SQLiteDriverTests +// + +import Foundation +import os +import TableProPluginKit +import TableProTestSupport +import TableProWireIO +import Testing + +@Suite(.timeLimit(.minutes(2))) +struct SQLiteRowSpillTests { + // Forty batches of 5,000, well past the 16 the stream buffers, so a held reader leaves most of the table unread. + private static let rowCount = 200_000 + private static let scan = "SELECT id, payload FROM t ORDER BY rowid" + + private static func withFile( + wal: Bool = false, + _ body: (_ reader: SQLitePluginDriver, _ writer: SQLitePluginDriver) async throws -> Void + ) async throws { + let path = FileManager.default.temporaryDirectory.appendingPathComponent("row-spill-\(UUID().uuidString).db").path + defer { + for suffix in ["", "-wal", "-shm", "-journal"] { try? FileManager.default.removeItem(atPath: path + suffix) } + } + let config = DriverConnectionConfig(host: "", port: 0, username: "", password: "", database: path) + let reader = SQLitePluginDriver(config: config) + let writer = SQLitePluginDriver(config: config) + try await reader.connect() + defer { reader.disconnect() } + if wal { _ = try await reader.execute(query: "PRAGMA journal_mode=WAL") } + _ = try await reader.execute(query: """ + CREATE TABLE t AS WITH RECURSIVE g(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM g WHERE x < \(rowCount)) + SELECT x AS id, hex(randomblob(50)) AS payload FROM g + """) + try await writer.connect() + defer { writer.disconnect() } + // A lock wait gives up after a second, so a check that the file is free answers quickly either way. + try await writer.applyQueryTimeout(1) + try await body(reader, writer) + } + + // Counts the rows taken and the ids that are not the next one expected. + private struct OrderedRead { + var expected = 1 + var mismatches = 0 + + mutating func take(_ element: PluginStreamElement) { + guard case .rows(let batch) = element else { return } + for id in batch.compactMap({ $0.first?.asText.flatMap(Int.init) }) { + if id != expected { mismatches += 1 } + expected += 1 + } + } + } + + private static func insert(_ batch: [PluginRow]) -> String { + let values = batch.map { row in "(\(row[0].asText ?? "NULL"), '\(row[1].asText ?? "")')" } + return "INSERT INTO copied (id, payload) VALUES " + values.joined(separator: ", ") + } + + @Test("A reader that holds its stream no longer holds the file: another connection can take it for a write") + func heldReaderLeavesTheFileFree() async throws { + try await Self.withFile { reader, writer in + var iterator = reader.streamRows(query: Self.scan).makeAsyncIterator() + var read = OrderedRead() + while read.expected == 1, let element = try await iterator.next() { read.take(element) } + + try await waitUntil(timeout: .seconds(30)) { + (try? await writer.execute(query: "BEGIN EXCLUSIVE")) != nil + } + _ = try await writer.execute(query: "COMMIT") + + while let element = try await iterator.next() { read.take(element) } + #expect(read.expected - 1 == Self.rowCount) + #expect(read.mismatches == 0) + } + } + + @Test("A copy into a second connection to the same file finishes instead of waiting on its own read") + func copyIntoASecondConnectionFinishes() async throws { + try await Self.withFile { reader, writer in + // Long enough for the read to end on a busy machine; main waits on itself past it. + try await writer.applyQueryTimeout(15) + _ = try await writer.execute(query: "CREATE TABLE copied (id INTEGER, payload TEXT)") + for try await element in reader.streamRows(query: Self.scan) { + guard case .rows(let batch) = element else { continue } + _ = try await writer.execute(query: Self.insert(batch)) + } + let copied = try await writer.execute(query: "SELECT count(*), min(id), max(id) FROM copied") + let summary = copied.rows.first?.compactMap { $0.asText } + #expect(summary == [String(Self.rowCount), "1", String(Self.rowCount)]) + } + } + + @Test("In WAL mode a reader that holds its stream no longer keeps a checkpoint from resetting the log") + func heldReaderLetsTheCheckpointRun() async throws { + try await Self.withFile(wal: true) { reader, writer in + var iterator = reader.streamRows(query: Self.scan).makeAsyncIterator() + var read = OrderedRead() + while read.expected == 1, let element = try await iterator.next() { read.take(element) } + + try await waitUntil(timeout: .seconds(30)) { + _ = try await writer.execute(query: "INSERT INTO t (id, payload) VALUES (0, 'written during the read')") + let checkpoint = try await writer.execute(query: "PRAGMA wal_checkpoint(TRUNCATE)") + return checkpoint.rows.first?.first?.asText == "0" + } + + while let element = try await iterator.next() { read.take(element) } + #expect(read.expected - 1 == Self.rowCount) + #expect(read.mismatches == 0) + } + } + + @Test("A read that fails after rows were spilled hands over every earlier row, then the error") + func errorFollowsTheSpilledRows() async throws { + let failAt = 150_000 + try await Self.withFile { reader, writer in + let query = """ + SELECT CASE WHEN id = \(failAt) THEN abs(-9223372036854775808) ELSE id END AS id, payload + FROM t ORDER BY rowid + """ + var iterator = reader.streamRows(query: query).makeAsyncIterator() + var read = OrderedRead() + while read.expected == 1, let element = try await iterator.next() { read.take(element) } + try await waitUntil(timeout: .seconds(30)) { + (try? await writer.execute(query: "BEGIN EXCLUSIVE")) != nil + } + _ = try await writer.execute(query: "COMMIT") + + await #expect(throws: SQLitePluginError.self) { + while let element = try await iterator.next() { read.take(element) } + } + #expect(read.expected == failAt) + #expect(read.mismatches == 0) + } + } + + @Test("A reader that leaves an endless read stops it, and the connection runs the next statement") + func leavingAnEndlessReadStopsIt() async throws { + try await Self.withFile { reader, _ in + let endless = "WITH RECURSIVE g(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM g) SELECT x, hex(randomblob(100)) FROM g" + var iterator: AsyncThrowingStream.Iterator? = + reader.streamRows(query: endless).makeAsyncIterator() + var rows = 0 + while rows == 0, let element = try await iterator?.next() { + if case .rows(let batch) = element { rows += batch.count } + } + iterator = nil + + let answer = await BoundedCall.result(within: .seconds(30)) { + try? await reader.execute(query: "SELECT 42").rows.first?.first?.asText + } + #expect(answer == "42") + } + } + + // MARK: - The spill on its own + + private static let cells: PluginRow = [.null, .text(""), .text("naïve 🧪 \u{0}"), .bytes(Data()), .bytes(Data([0, 255, 7]))] + + private static func batch(_ index: Int) -> [PluginRow] { + (0..<100).map { [.text("\(index)-\($0)")] + cells } + } + + private static func makeStream( + budget: SpoolBudget = SpoolBudget(limit: 1 << 30), + makeSpool: @escaping @Sendable () throws -> SpoolFile = { try SpoolFile() } + ) -> (AsyncThrowingStream, PluginRowSpill) { + let (stream, continuation) = AsyncThrowingStream.makeStream( + of: PluginStreamElement.self, bufferingPolicy: .bufferingOldest(2) + ) + let spill = PluginRowSpill(continuation: continuation, budget: budget, makeSpool: makeSpool) + continuation.onTermination = { @Sendable _ in spill.abandon() } + return (stream, spill) + } + + private static func batches(of stream: AsyncThrowingStream) async throws -> [[PluginRow]] { + var batches: [[PluginRow]] = [] + for try await element in stream { + if case .rows(let rows) = element { batches.append(rows) } + } + return batches + } + + @Test("Batches the buffer cannot take come back from the spool in order, every cell as it went in") + func spilledBatchesKeepOrderAndCells() async throws { + let (stream, spill) = Self.makeStream() + // On its own thread, so a producer that waited for the reader would hit the deadline instead of hanging. + let delivered = await BoundedCall.resultOnItsOwnThread(within: .seconds(30)) { + let accepted = (0..<40).allSatisfy { spill.deliver(Self.batch($0)) } + spill.finish() + return accepted + } + #expect(delivered == true) + let expected = (0..<40).map { Self.batch($0) } + #expect(try await Self.batches(of: stream) == expected) + } + + @Test("An error after spilled batches reaches the reader after them") + func errorAfterSpilledBatches() async throws { + struct Failed: Error {} + let (stream, spill) = Self.makeStream() + for index in 0..<10 { spill.deliver(Self.batch(index)) } + spill.finish(throwing: Failed()) + + var received: [[PluginRow]] = [] + await #expect(throws: Failed.self) { + for try await element in stream { + if case .rows(let rows) = element { received.append(rows) } + } + } + let expected = (0..<10).map { Self.batch($0) } + #expect(received == expected) + } + + @Test("Without a spool the producer waits for the reader, and every batch still arrives in order") + func failedSpoolFallsBackToPacing() async throws { + let attempts = OSAllocatedUnfairLock(initialState: 0) + let (stream, spill) = Self.makeStream { + attempts.withLock { $0 += 1 } + throw SpoolFileError.cannotCreate(errno: ENOSPC) + } + let delivered = Self.produce(0..<20, into: spill) + // Batch 2 meets a full buffer, so it asks for the spool. + try await waitUntil { delivered.withLock { $0 } == 2 } + let expected = (0..<20).map { Self.batch($0) } + #expect(try await Self.batches(of: stream) == expected) + #expect(attempts.withLock { $0 } == 1) + } + + // Delivers on its own thread and counts each deliver that returned. + private static func produce(_ indices: Range, into spill: PluginRowSpill) -> OSAllocatedUnfairLock { + let delivered = OSAllocatedUnfairLock(initialState: 0) + Thread { + for index in indices { + spill.deliver(Self.batch(index)) + delivered.withLock { $0 += 1 } + } + spill.finish() + }.start() + return delivered + } + + private static func rows(_ element: PluginStreamElement?) -> [[PluginRow]] { + guard case .rows(let rows)? = element else { return [] } + return [rows] + } + + // Batches 0 and 1 fill the buffer of two. Batch 2 spills, and the drain takes it out of the file and holds it until + // the buffer has room, so the file is empty again before batches 3 and 4 go in. + private static func fillWithHeldReader(_ spill: PluginRowSpill, spool: () -> SpoolFile?) async throws { + for index in 0..<3 { spill.deliver(Self.batch(index)) } + try await waitUntil { spool()?.byteCount == 0 } + for index in 3..<5 { spill.deliver(Self.batch(index)) } + } + + @Test("A spill charges its spool to the budget, refills the spool once the drain empties it, and gives it all back") + func spillChargesTheBudget() async throws { + let sizes = (0..<10).map { PluginRowSpill.encode(Self.batch($0)).count + 8 } + let budget = SpoolBudget(limit: sizes[3] + sizes[4] + sizes[5] - 1) + let created = OSAllocatedUnfairLock<[SpoolFile]>(initialState: []) + let (stream, spill) = Self.makeStream(budget: budget) { + let spool = try SpoolFile() + created.withLock { $0.append(spool) } + return spool + } + try await Self.fillWithHeldReader(spill) { created.withLock { $0.first } } + let spool = try #require(created.withLock { $0.first }) + #expect(spool.byteCount == sizes[3] + sizes[4]) + #expect(budget.bytesInUse == sizes[3] + sizes[4]) + #expect(!budget.admits(sizes[5])) + + _ = Self.produce(5..<10, into: spill) + var received: [[PluginRow]] = [] + for try await element in stream { received += Self.rows(element) } + let expected = (0..<10).map { Self.batch($0) } + #expect(received == expected) + #expect(created.withLock { $0.count } == 1) + #expect(budget.bytesInUse == 0) + } + + @Test("With no budget left the producer waits for the reader, makes no spool, and every batch arrives in order") + func emptyBudgetPacesEveryBatch() async throws { + let created = OSAllocatedUnfairLock(initialState: 0) + let (stream, spill) = Self.makeStream(budget: SpoolBudget(limit: 0)) { + created.withLock { $0 += 1 } + return try SpoolFile() + } + let delivered = Self.produce(0..<10, into: spill) + // Batch 2 meets a full buffer, so it asks the budget. + try await waitUntil { delivered.withLock { $0 } == 2 } + let expected = (0..<10).map { Self.batch($0) } + #expect(try await Self.batches(of: stream) == expected) + #expect(created.withLock { $0 } == 0) + } + + @Test("Spills charge one budget, and each gives back only what its own spool held") + func spillsShareTheBudget() async throws { + let sizes = (0..<5).map { PluginRowSpill.encode(Self.batch($0)).count + 8 } + let held = sizes[3] + sizes[4] + let budget = SpoolBudget(limit: held * 2) + let spools = OSAllocatedUnfairLock<[SpoolFile]>(initialState: []) + let makeSpool: @Sendable () throws -> SpoolFile = { + let spool = try SpoolFile() + spools.withLock { $0.append(spool) } + return spool + } + let (first, firstSpill) = Self.makeStream(budget: budget, makeSpool: makeSpool) + try await Self.fillWithHeldReader(firstSpill) { spools.withLock { $0.first } } + let (second, secondSpill) = Self.makeStream(budget: budget, makeSpool: makeSpool) + try await Self.fillWithHeldReader(secondSpill) { spools.withLock { $0.count > 1 ? $0[1] : nil } } + #expect(budget.bytesInUse == held * 2) + #expect(!budget.admits(1)) + + firstSpill.finish() + #expect(try await Self.batches(of: first).count == 5) + #expect(budget.bytesInUse == held) + + secondSpill.finish() + #expect(try await Self.batches(of: second).count == 5) + #expect(budget.bytesInUse == 0) + } + + @Test("Once the reader has gone the producer is told so") + func readerGoneStopsDelivery() async throws { + let spill: PluginRowSpill + var iterator: AsyncThrowingStream.Iterator? + do { + let (stream, made) = Self.makeStream() + spill = made + for index in 0..<5 { spill.deliver(Self.batch(index)) } + iterator = stream.makeAsyncIterator() + } + _ = try await iterator?.next() + iterator = nil + try await waitUntil { !spill.deliver(Self.batch(99)) } + } + + @Test("A batch survives the record format unchanged") + func recordRoundTrip() throws { + let original = Self.batch(3) + [[], [.text(String(repeating: "x", count: 100_000))]] + #expect(try PluginRowSpill.decode(PluginRowSpill.encode(original)) == original) + #expect(try PluginRowSpill.decode(PluginRowSpill.encode([])) == []) + #expect(throws: (any Error).self) { try PluginRowSpill.decode([0, 0, 0, 1, 0, 0, 0, 1, 9]) } + } +} diff --git a/apple/Plugins/SharedSpill/PluginRowSpill.swift b/apple/Plugins/SharedSpill/PluginRowSpill.swift new file mode 100644 index 0000000000..fad2534f29 --- /dev/null +++ b/apple/Plugins/SharedSpill/PluginRowSpill.swift @@ -0,0 +1,367 @@ +// +// PluginRowSpill.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit +import TableProWireIO + +// The disk every spill in this plugin draws from: half of what the volume has for important data, counting what the +// spools already hold, so concurrent runs leave room for the file an export writes. Read again at most every 10 s. +final class SpoolBudget: @unchecked Sendable { + static let shared = SpoolBudget(limit: nil) + + private let fixedLimit: Int? + private let lock = NSLock() + private var inUse = 0 + private var limit = 0 + private var checkedAt: ContinuousClock.Instant? + + init(limit: Int?) { + fixedLimit = limit + } + + var bytesInUse: Int { + lock.withLock { inUse } + } + + func admits(_ bytes: Int) -> Bool { + if let fixedLimit { + return lock.withLock { inUse + bytes <= fixedLimit } + } + let now = ContinuousClock.now + let (held, cached, stale) = lock.withLock { (inUse, limit, checkedAt.map { now - $0 > .seconds(10) } ?? true) } + var current = cached + if stale { + let values = try? FileManager.default.temporaryDirectory + .resourceValues(forKeys: [.volumeAvailableCapacityForImportantUsageKey]) + current = (Int(clamping: values?.volumeAvailableCapacityForImportantUsage ?? 0) + held) / 2 + lock.withLock { + limit = current + checkedAt = now + } + } + return held + bytes <= current + } + + func add(_ delta: Int) { + lock.withLock { inUse += delta } + } +} + +// Takes a statement's rows without waiting for the reader, so the read, and its lock on a SQLite file, ends at the +// speed SQLite reads. What the stream's buffer refuses goes to a spool file and follows in order. Past the budget, or +// without a spool, the producer waits for the reader. One producer per spill. +final class PluginRowSpill: @unchecked Sendable { + private static let logger = Logger(subsystem: "com.TablePro", category: "PluginRowSpill") + + private let continuation: AsyncThrowingStream.Continuation + private let budget: SpoolBudget + private let makeSpool: @Sendable () throws -> SpoolFile + + private let lock = NSLock() + private var spool: SpoolFile? + // The spool's size as last counted against the budget. + private var counted = 0 + private var spoolFailed = false + // Batches in the spool that have not reached the stream yet. + private var spilled = 0 + private var draining = false + // The producer's end, held back while spilled batches are still on their way. + private var outcome: Error?? + private var ended = false + + init( + continuation: AsyncThrowingStream.Continuation, + budget: SpoolBudget = .shared, + makeSpool: @escaping @Sendable () throws -> SpoolFile = { try SpoolFile() } + ) { + self.continuation = continuation + self.budget = budget + self.makeSpool = makeSpool + } + + // The header comes first, so the buffer is empty and takes it. + func yield(_ header: PluginStreamHeader) { + continuation.yield(.header(header)) + } + + // False once the reader has gone. + @discardableResult + func deliver(_ batch: [PluginRow]) -> Bool { + lock.lock() + if ended { + lock.unlock() + return false + } + if spilled == 0 { + switch continuation.yield(.rows(batch)) { + case .enqueued: + lock.unlock() + return true + case .terminated: + end() + lock.unlock() + return false + case .dropped: + break + @unknown default: + lock.unlock() + return false + } + } + let failed = spoolFailed + let existing = spool + lock.unlock() + + if !failed, spill(batch, into: existing) { + return true + } + return deliverAtTheReadersPace(batch) + } + + // False when the batch did not go to the spool: past the budget, or the spool failed. + private func spill(_ batch: [PluginRow], into existing: SpoolFile?) -> Bool { + let record = Self.encode(batch) + guard budget.admits(record.count + 8) else { return false } + do { + let target = try existing ?? makeSpool() + try target.append(record) + return didSpill(into: target) + } catch { + Self.logger.error( + "Rows no longer spill to a temporary file: \(String(describing: type(of: error)), privacy: .public) \(error.localizedDescription, privacy: .private)" + ) + lock.withLock { + spoolFailed = true + // With no drain left to notice, the space would stay taken until the stream ends. + if !draining { releaseSpool() } + } + return false + } + } + + func finish(throwing error: Error? = nil) { + lock.lock() + guard !ended else { + lock.unlock() + return + } + guard spilled == 0, !draining else { + outcome = .some(error) + lock.unlock() + return + } + end() + lock.unlock() + continuation.finish(throwing: error) + } + + // The stream ended: nothing more goes in. + func abandon() { + lock.withLock { end() } + } + + // Under the lock. The spool's space returns once the drain lets go of it too. + private func end() { + ended = true + releaseSpool() + } + + // Under the lock. + private func releaseSpool() { + spool = nil + budget.add(-counted) + counted = 0 + } + + // Under the lock. + private func count(_ target: SpoolFile) { + guard spool === target else { return } + let size = target.byteCount + budget.add(size - counted) + counted = size + } + + private func didSpill(into target: SpoolFile) -> Bool { + lock.lock() + guard !ended else { + lock.unlock() + return false + } + spool = target + spilled += 1 + count(target) + let startsDrain = !draining + draining = true + lock.unlock() + if startsDrain { + Task { await self.drain(target) } + } + return true + } + + private enum DrainStep { + case stop + case finish(Error?) + case next + } + + private func nextDrainStep() -> DrainStep { + lock.withLock { + if ended { + draining = false + return .stop + } + guard spilled == 0 else { return .next } + draining = false + // A failed spool takes no more batches, so its space goes back now rather than at the end. + if spoolFailed { releaseSpool() } + guard let outcome else { return .stop } + end() + return .finish(outcome) + } + } + + private func drain(_ target: SpoolFile) async { + while true { + switch nextDrainStep() { + case .stop: + return + case .finish(let error): + continuation.finish(throwing: error) + return + case .next: + break + } + + let batch: [PluginRow] + do { + guard let record = try target.next() else { throw SpoolFileError.readFailed(errno: EIO) } + lock.withLock { count(target) } + batch = try Self.decode(record) + } catch { + Self.logger.error( + "Spilled rows could not be read back: \(String(describing: type(of: error)), privacy: .public) \(error.localizedDescription, privacy: .private)" + ) + lock.withLock { + end() + draining = false + } + continuation.finish(throwing: error is SpoolFileError ? error : SpoolFileError.readFailed(errno: EIO)) + return + } + guard await PluginRowStream.deliver(.rows(batch), to: continuation) else { + lock.withLock { + end() + draining = false + } + return + } + lock.withLock { spilled -= 1 } + } + } + + // The batches already spilled reach the stream first; the spool is empty again by then. + private func deliverAtTheReadersPace(_ batch: [PluginRow]) -> Bool { + while true { + let (waiting, gone) = lock.withLock { (spilled > 0, ended) } + if gone { return false } + if !waiting { break } + usleep(2_000) + } + guard PluginRowStream.deliverBlocking(.rows(batch), to: continuation) else { + lock.withLock { end() } + return false + } + return true + } + + // MARK: - Record + + private enum Tag: UInt8 { + case null = 0 + case text = 1 + case bytes = 2 + } + + static func encode(_ batch: [PluginRow]) -> [UInt8] { + var size = 4 + for row in batch { + size += 4 + for cell in row { + switch cell { + case .null: size += 1 + case .text(let text): size += 5 + text.utf8.count + case .bytes(let data): size += 5 + data.count + } + } + } + var record: [UInt8] = [] + record.reserveCapacity(size) + appendLength(batch.count, to: &record) + for row in batch { + appendLength(row.count, to: &record) + for cell in row { + switch cell { + case .null: + record.append(Tag.null.rawValue) + case .text(var text): + record.append(Tag.text.rawValue) + text.withUTF8 { utf8 in + appendLength(utf8.count, to: &record) + record.append(contentsOf: utf8) + } + case .bytes(let data): + record.append(Tag.bytes.rawValue) + appendLength(data.count, to: &record) + record.append(contentsOf: data) + } + } + } + return record + } + + static func decode(_ record: [UInt8]) throws -> [PluginRow] { + var reader = ByteReader(record) + let declaredRows = try reader.u32() + let rowCount = try reader.elementCount(declaredRows, minimumElementSize: 4) + var batch: [PluginRow] = [] + batch.reserveCapacity(rowCount) + for _ in 0.. Int { + let length = try reader.u32() + let start = reader.offset + try reader.skip(length) + return start + } + + private static func appendLength(_ length: Int, to record: inout [UInt8]) { + withUnsafeBytes(of: UInt32(length).bigEndian) { record.append(contentsOf: $0) } + } +} diff --git a/apple/changelog.d/4278.fixed.md b/apple/changelog.d/4278.fixed.md new file mode 100644 index 0000000000..d0403e47fe --- /dev/null +++ b/apple/changelog.d/4278.fixed.md @@ -0,0 +1 @@ +Copying, comparing or exporting a SQLite or libSQL table kept other programs from writing the file until it ended. diff --git a/apple/project.yml b/apple/project.yml index 2a14130d72..482c603ffd 100644 --- a/apple/project.yml +++ b/apple/project.yml @@ -246,9 +246,11 @@ targetTemplates: # Links its own SQLite (scripts/build-sqlite.sh), never -lsqlite3: macOS's library is built without # extension loading. SQLiteLinkage: + sources: + - Plugins/SharedSpill dependencies: - package: TableProCore - products: [CSQLite, TableProSQLGrammar, TableProSQLiteCore, TableProSQLVocabulary] + products: [CSQLite, TableProSQLGrammar, TableProSQLiteCore, TableProSQLVocabulary, TableProWireIO] settings: base: OTHER_LDFLAGS: @@ -1028,7 +1030,7 @@ targets: principalClass: DynamoDBPlugin dependencies: - package: TableProCore - product: TableProNumberFormatting + products: [TableProNumberFormatting, TableProWireIO] DamengDriver: templates: [DriverPlugin] @@ -1138,6 +1140,8 @@ targets: templateAttributes: folder: LibSQLDriverPlugin principalClass: LibSQLPlugin + sources: + - Plugins/SharedSpill dependencies: - package: TableProCore product: CSQLite @@ -1145,6 +1149,8 @@ targets: product: TableProSQLGrammar - package: TableProCore product: TableProSQLiteCore + - package: TableProCore + product: TableProWireIO settings: base: OTHER_LDFLAGS: diff --git a/apple/scripts/probes/check-column-type-corpus.sh b/apple/scripts/probes/check-column-type-corpus.sh index 88797c1898..fd2bad9ed0 100755 --- a/apple/scripts/probes/check-column-type-corpus.sh +++ b/apple/scripts/probes/check-column-type-corpus.sh @@ -116,7 +116,7 @@ case "$ENGINE" in FIELDS="oracleServiceName=FREEPDB1" ;; sqlite) - FOLDER=SQLiteDriverPlugin CLASS=SQLitePlugin PRODUCTS="CSQLite TableProSQLiteCore" + FOLDER=SQLiteDriverPlugin CLASS=SQLitePlugin PRODUCTS="CSQLite TableProSQLiteCore TableProWireIO" LINK="$ROOT/Libs/libsqlite3_vendored.a" ;; *) @@ -186,6 +186,7 @@ link_sources corpus "$PLUGINS/ColumnTypeCorpus" link_sources csv "$PLUGINS/CSVExportPlugin" link_sources json "$PLUGINS/JSONExportPlugin" link_sources sql "$PLUGINS/SQLExportPlugin" +[ "$FOLDER" = SQLiteDriverPlugin ] && link_sources spill "$PLUGINS/SharedSpill" for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox; do ln -s "$PLUGINS/Shared/$shared.swift" "$SOURCES/shared_$shared.swift" done diff --git a/apple/scripts/probes/check-nul-text-drivers.sh b/apple/scripts/probes/check-nul-text-drivers.sh index a916a1c271..83e25fe6ef 100755 --- a/apple/scripts/probes/check-nul-text-drivers.sh +++ b/apple/scripts/probes/check-nul-text-drivers.sh @@ -10,7 +10,7 @@ LINK="" case "$ENGINE" in libsql) FOLDER=LibSQLDriverPlugin - PRODUCTS="TableProSQLiteCore CSQLite" + PRODUCTS="TableProSQLiteCore CSQLite TableProWireIO" LINK="\"$ROOT/Libs/libsqlite3_vendored.a\"" ;; beancount) @@ -32,6 +32,9 @@ mkdir -p "$SOURCES" for source in "$ROOT/Plugins/$FOLDER/"*.swift; do ln -sf "$source" "$SOURCES/$(basename "$source")" done +if [ "$ENGINE" = libsql ]; then + ln -sf "$ROOT/Plugins/SharedSpill/PluginRowSpill.swift" "$SOURCES/PluginRowSpill.swift" +fi for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox PluginRedirectRefusingDelegate; do ln -sf "$ROOT/Plugins/Shared/$shared.swift" "$SOURCES/$shared.swift" done diff --git a/apple/scripts/probes/check-sqlite-agent-app-nap.sh b/apple/scripts/probes/check-sqlite-agent-app-nap.sh new file mode 100755 index 0000000000..86bfbfd51e --- /dev/null +++ b/apple/scripts/probes/check-sqlite-agent-app-nap.sh @@ -0,0 +1,215 @@ +#!/usr/bin/env bash +# +# Shows whether App Nap delays the Remote SQLite heartbeat past the agent's 60 s watchdog. +# +# Builds the SQLite driver's sources into a small app, connects it to the shipped agent (local python3, the real +# watchdog) over the loopback relay the driver tests use, launches it in the background so it is never frontmost, and +# leaves it hidden and idle. It records each heartbeat as it reaches the relay, then runs one statement. A second +# copy with the heartbeat off is the control: the watchdog ends its session after about 60 s. +# +# The script prints the main thread's scheduling priority once the apps are hidden: 4 means App Nap took them. +# Exit 1 when the session with heartbeats did not survive, 3 when the probe cannot run. +# +# Usage: +# scripts/probes/check-sqlite-agent-app-nap.sh [--minutes N] +# +# --minutes how long the app stays hidden, default 7 + +set -uo pipefail + +MINUTES=7 +while [ $# -gt 0 ]; do + case "$1" in + --minutes) MINUTES="${2:?}"; shift 2 ;; + *) echo "unknown argument $1" >&2; exit 3 ;; + esac +done +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" +[ -f "$ROOT/Libs/libsqlite3_vendored.a" ] || { echo "run scripts/download-libs.sh first" >&2; exit 3; } +command -v python3 > /dev/null || { echo "python3 not found" >&2; exit 3; } + +WORK="$(mktemp -d)" || exit 3 +[ -n "$WORK" ] || exit 3 +cleanup() { + pkill -f "$WORK/" 2> /dev/null + rm -rf "$WORK" +} +trap cleanup EXIT + +SOURCES="$WORK/Sources/NapProbe" +mkdir -p "$SOURCES" +for source in "$ROOT"/Plugins/SQLiteDriverPlugin/*.swift "$ROOT"/Plugins/SharedSpill/*.swift; do + ln -s "$source" "$SOURCES/driver_$(basename "$source")" +done +for shared in PluginConnectTimeout PluginQueryTimeout PluginURLSessionTaskBox; do + [ -f "$ROOT/Plugins/Shared/$shared.swift" ] && ln -s "$ROOT/Plugins/Shared/$shared.swift" "$SOURCES/shared_$shared.swift" +done +for helper in TablePro/Core/RemoteSQLite/RemoteSQLiteAgentSource.swift TablePro/Core/RemoteSQLite/RemoteSQLiteWire.swift \ + Plugins/SQLiteDriverTests/SQLiteAgentLoopback.swift; do + ln -s "$ROOT/$helper" "$SOURCES/agent_$(basename "$helper")" +done + +cat > "$WORK/Package.swift" << MANIFEST +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "NapProbe", + platforms: [.macOS(.v14)], + dependencies: [.package(path: "$ROOT/Packages/TableProCore")], + targets: [ + .executableTarget( + name: "NapProbe", + dependencies: [ + .product(name: "TableProPluginKit", package: "TableProCore"), + .product(name: "TableProSQLGrammar", package: "TableProCore"), + .product(name: "TableProProcess", package: "TableProCore"), + .product(name: "CSQLite", package: "TableProCore"), + .product(name: "TableProSQLiteCore", package: "TableProCore"), + .product(name: "TableProSQLQuoting", package: "TableProCore"), + .product(name: "TableProSQLVocabulary", package: "TableProCore"), + .product(name: "TableProWireIO", package: "TableProCore"), + ], + path: "Sources/NapProbe", + swiftSettings: [.swiftLanguageMode(.v6), .enableUpcomingFeature("MemberImportVisibility")], + linkerSettings: [.unsafeFlags(["$ROOT/Libs/libsqlite3_vendored.a"])] + ), + ] +) +MANIFEST + +cat > "$SOURCES/main.swift" << 'SWIFT' +import AppKit +import Foundation +import TableProPluginKit + +// Arguments: