diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift index 2a47629565..37c02a0267 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQCopyIn.swift @@ -8,9 +8,14 @@ import Foundation import TableProPluginKit import TableProPostgreSQLCore -/// Sends a `COPY ... FROM STDIN` its rows, after the statement answered `PGRES_COPY_IN`, the way psql 18 sends a -/// script's rows: the data as `CopyData`, never the `\.` line, then `CopyDone`. Measured with libpq 17.4 against -/// PostgreSQL 17.11 and 18.6 (`apple/scripts/probes/check-postgresql-copy-in.sh`). +/// Sends a `COPY ... FROM STDIN` its rows the way psql 18 sends a script's rows: the data as `CopyData`, never the +/// `\.` line, then `CopyDone`. Measured with libpq 17.4 against PostgreSQL 17.11 and 18.6 +/// (`apple/scripts/probes/check-postgresql-copy-in.sh`). +/// +/// The connection is nonblocking from the statement to its result and every wait is a slice, so a Stop is seen while +/// the server takes no data. A Stop ends the rows with `CopyFail`; when the server has not answered a slice later +/// (a row lock wait, or rows a proxy still holds), one cancel request follows from this thread, so it has reached the +/// server before the results are read and cannot stop the statement after the COPY. nonisolated enum LibPQCopyIn { struct Sent { // The copy's own result, or nil when libpq had none to give. The caller clears it. @@ -26,6 +31,29 @@ nonisolated enum LibPQCopyIn { case connection } + // Read on the connection's queue only, for one COPY. + final class Stop { + private let requested: () -> Bool + private let closing: () -> Bool + private let sendCancelRequest: () -> Void + private var sentCancelRequest = false + + init(requested: @escaping () -> Bool, closing: @escaping () -> Bool, sendCancelRequest: @escaping () -> Void) { + self.requested = requested + self.closing = closing + self.sendCancelRequest = sendCancelRequest + } + + var isRequested: Bool { requested() } + var isClosing: Bool { closing() } + + func cancelServerOnce() { + guard !sentCancelRequest else { return } + sentCancelRequest = true + sendCancelRequest() + } + } + // psql's own CopyFail reasons, which the server echoes as "COPY from stdin failed: ". static let cancelledReason = "canceled by user" static let sourceFailedReason = "aborted because of read failure" @@ -37,41 +65,76 @@ nonisolated enum LibPQCopyIn { // PQputCopyData takes an `int` length and grows its buffer to hold a whole message. private static let sliceLength = 1 << 20 + private static let waitMicroseconds: Int64 = 100_000 - static func send( - _ rows: PluginCopyData, - format: LibPQCopyFormat, - conn: OpaquePointer, - isCancelled: () -> Bool - ) -> Sent { + /// The statement's result as `PQexec` gives it: the last one, or the first that starts a copy. Nil when the + /// connection was lost. The connection stays nonblocking only after `PGRES_COPY_IN`, for `send`. + static func begin(_ statement: String, conn: OpaquePointer, stop: Stop) -> OpaquePointer? { + guard PQsetnonblocking(conn, 1) == 0, statement.withCString({ PQsendQuery(conn, $0) }) == 1 else { + _ = PQsetnonblocking(conn, 0) + return nil + } + var last: OpaquePointer? + // Nothing to retract while the statement starts, so a Stop sends the cancel request at once. + while awaitResult(conn, stop: stop, patience: 0) == .ready, let next = PQgetResult(conn) { + PQclear(last) + last = next + let status = PQresultStatus(next) + if status == PGRES_COPY_IN || status == PGRES_COPY_OUT || status == PGRES_COPY_BOTH + || PQstatus(conn) == CONNECTION_BAD { + break + } + } + if last.map({ PQresultStatus($0) != PGRES_COPY_IN }) ?? true { + _ = PQsetnonblocking(conn, 0) + } + return last + } + + /// After `begin` answered `PGRES_COPY_IN`. Returns with the connection blocking again. + static func send(_ rows: PluginCopyData, format: LibPQCopyFormat, conn: OpaquePointer, stop: Stop) -> Sent { // A script holds text, and psql would send the rest of the file as the binary stream. - let interruption: Interruption? = format == .binary - ? .binaryFormat - : sendPieces(rows, conn: conn, isCancelled: isCancelled) + let interruption: Interruption? = format == .binary ? .binaryFormat : sendPieces(rows, conn: conn, stop: stop) let ended: Int32 + let stopped: Bool switch interruption { case nil: ended = PQputCopyEnd(conn, nil) + stopped = false case .connection: ended = -1 + stopped = false case .cancelled: ended = cancelledReason.withCString { PQputCopyEnd(conn, $0) } + stopped = true case .binaryFormat: ended = binaryReason.withCString { PQputCopyEnd(conn, $0) } + stopped = false case .source(let error): - let reason = isStop(error) ? cancelledReason : sourceFailedReason - ended = reason.withCString { PQputCopyEnd(conn, $0) } + stopped = isStop(error) + ended = (stopped ? cancelledReason : sourceFailedReason).withCString { PQputCopyEnd(conn, $0) } + } + // A CopyFail can still be answered on its own, so the cancel request waits a slice; after CopyDone it cannot. + let answered = ended == 1 + && awaitResult(conn, stop: stop, patience: interruption == nil ? 0 : 1, stopped: stopped) == .ready + _ = PQsetnonblocking(conn, 0) + guard answered else { + return Sent(result: nil, interruption: interruption ?? .connection) } - let reported = ended == 1 ? interruption : (interruption ?? .connection) - return Sent(result: finalResult(conn), interruption: reported) + return Sent(result: finalResult(conn), interruption: interruption) } - private static func sendPieces(_ rows: PluginCopyData, conn: OpaquePointer, isCancelled: () -> Bool) -> Interruption? { + private static func sendPieces(_ rows: PluginCopyData, conn: OpaquePointer, stop: Stop) -> Interruption? { do { while true { - if isCancelled() { return .cancelled } + if stop.isRequested { return .cancelled } guard let piece = try rows.nextPiece() else { return nil } guard put(piece, conn: conn) else { return .connection } + switch flush(conn, stop: stop) { + case .sent: continue + case .stopped: return .cancelled + case .lost: return .connection + } } } catch { return .source(error) @@ -83,6 +146,7 @@ nonisolated enum LibPQCopyIn { error is PluginImportCancellationError || error is CancellationError } + // Nonblocking, so each slice only queues it: `flush` sends it. private static func put(_ piece: Data, conn: OpaquePointer) -> Bool { piece.withUnsafeBytes { raw in guard let base = raw.baseAddress?.assumingMemoryBound(to: CChar.self) else { return true } @@ -96,6 +160,55 @@ nonisolated enum LibPQCopyIn { } } + private enum Flush { + case sent + case stopped + case lost + } + + private static func flush(_ conn: OpaquePointer, stop: Stop) -> Flush { + while true { + let flushed = PQflush(conn) + guard flushed >= 0, !stop.isClosing, PQstatus(conn) != CONNECTION_BAD else { return .lost } + if flushed == 0 { return .sent } + if stop.isRequested { return .stopped } + guard wait(conn, forWrite: true) else { return .lost } + // The server's notices, so it never blocks writing to us while we wait to write to it. + _ = PQconsumeInput(conn) + } + } + + private enum Answer { + case ready + case lost + } + + // Until libpq holds a result and has sent everything queued. A proxy can take every byte while the server reads + // none, so this is the wait that tells such a server. `patience` is how many slices a Stop waits before its + // cancel request goes. + private static func awaitResult(_ conn: OpaquePointer, stop: Stop, patience: Int, stopped: Bool = false) -> Answer { + var unanswered = 0 + while true { + let flushed = PQflush(conn) + guard flushed >= 0, !stop.isClosing else { return .lost } + if flushed == 0, PQisBusy(conn) == 0 { return .ready } + if stopped || stop.isRequested { + if unanswered >= patience { stop.cancelServerOnce() } + unanswered += 1 + } + guard wait(conn, forWrite: flushed == 1) else { return .lost } + // A failed read leaves the connection bad, and PQgetResult then gives the reason. + if PQconsumeInput(conn) == 0, PQisBusy(conn) == 0 { return .ready } + } + } + + private static func wait(_ conn: OpaquePointer, forWrite: Bool) -> Bool { + let socket = PQsocket(conn) + guard socket >= 0 else { return false } + let deadline = pg_usec_time_t(Int64(PQgetCurrentTimeUSec()) + waitMicroseconds) + return PQsocketPoll(socket, 1, forWrite ? 1 : 0, deadline) >= 0 || errno == EINTR + } + // While libpq still answers PGRES_COPY_IN the copy is not over: end it again, as psql does. The first other // result is the copy's; the rest are drained so the connection takes the next statement. private static func finalResult(_ conn: OpaquePointer) -> OpaquePointer? { diff --git a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift index d45c9cc798..bb8ccf010b 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/LibPQPluginConnection.swift @@ -76,6 +76,8 @@ final class LibPQPluginConnection: @unchecked Sendable { private var _lastTransactionState: LibPQTransactionState = .idle private var _hasLostConnection = false private var _interruptsBlockedCallOnDisconnect = false + // A copy-in sends its own cancel request (LibPQCopyIn), so none outlives it. + private var copyInRunning = false private var serverMessages: Unmanaged? private var _standardConformingStrings = true // Queue-confined. Non-nil until every schema/flavor/catalog bootstrap probe has completed. @@ -711,7 +713,7 @@ final class LibPQPluginConnection: @unchecked Sendable { // MARK: - Query Cancellation func cancelCurrentQuery() { - guard cancellationGate.cancel() != nil else { return } + guard cancellationGate.cancel() != nil, !stateLock.withLock({ copyInRunning }) else { return } requestServerCancelOnCurrentConnection() } @@ -1087,13 +1089,26 @@ final class LibPQPluginConnection: @unchecked Sendable { rows: PluginCopyData, conn: OpaquePointer ) throws -> LibPQPluginQueryResult { + // Set around the whole generation, so no Stop finds it running without the flag. + stateLock.withLock { copyInRunning = true } let generation = cancellationGate.beginQuery() - defer { cancellationGate.endQuery(generation) } + defer { + cancellationGate.endQuery(generation) + stateLock.withLock { copyInRunning = false } + } defer { refreshStandardConformingStrings(from: conn) } + // PQexec drained them itself; PQsendQuery refuses while any are left. let cancelsOutput = cancelsAbandonedOutput(conn) + finishAbandonedResults(conn, cancellingOutput: cancelsOutput) if let ended = sessionEndedBeforeSending(conn) { throw ended } - guard let start = statement.withCString({ PQexec(conn, $0) }) else { + if cancellationGate.isCancelled(generation) { throw CancellationError() } + let stop = LibPQCopyIn.Stop( + requested: { [cancellationGate] in cancellationGate.isCancelled(generation) }, + closing: { [self] in isShuttingDown }, + sendCancelRequest: { [self] in sendServerCancelAndWait(on: conn) } + ) + guard let start = LibPQCopyIn.begin(statement, conn: conn, stop: stop) else { throw lostConnection(getError(from: conn), on: conn, sent: true) } recordTransactionState(of: conn) @@ -1108,9 +1123,7 @@ final class LibPQPluginConnection: @unchecked Sendable { } PQclear(start) - let sent = LibPQCopyIn.send(rows, format: copy.format, conn: conn) { - cancellationGate.isCancelled(generation) - } + let sent = LibPQCopyIn.send(rows, format: copy.format, conn: conn, stop: stop) recordTransactionState(of: conn) guard let result = sent.result else { throw lostConnection(getError(from: conn), on: conn, sent: true) @@ -1977,6 +1990,23 @@ extension LibPQPluginConnection { // Outside the class body, which is at its length limit. An extension in this file still reaches its private members. extension LibPQPluginConnection { + // The call waits for it, unlike the queued requests: PQcancel returns once the server has the request, so it + // cannot reach a statement the call sends later. PQcancel has no timeout of its own, so the wait ends at the + // connect timeout or a disconnect. + fileprivate func sendServerCancelAndWait(on connection: OpaquePointer) { + guard !suppressServerSideCancel, let cancelObject = PQgetCancel(connection) else { return } + let request = LibPQCancelRequest(cancelObject) + let delivered = DispatchSemaphore(value: 0) + Self.cancellationQueue.async { + request.perform() + delivered.signal() + } + let deadline = DispatchTime.now() + .milliseconds(connectTimeout.milliseconds) + while delivered.wait(timeout: .now() + .milliseconds(100)) == .timedOut { + guard !isShuttingDown, DispatchTime.now() < deadline else { return } + } + } + // `executeReadOnlyStatement` bounded at the source: the statement stops at `bound`, so no more than that is ever // held. Stopping ends only the read-only transaction this opened. Unknown column types are looked up after its // ROLLBACK, since a cancel can leave it aborted. diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQCopyInTests.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQCopyInTests.swift index 0375b98a04..0feba75642 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQCopyInTests.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQCopyInTests.swift @@ -131,7 +131,7 @@ struct LibPQCopyInTests { defer { connection.disconnect() } let pieces = Pieces(["1\tone\n", "2\ttwo\n", "3\tthree\n"]) { count in - if count == 1 { _ = connection.cancellationGate.cancel() } + if count == 1 { connection.cancelCurrentQuery() } } await #expect(throws: CancellationError.self) { _ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil) @@ -140,6 +140,65 @@ struct LibPQCopyInTests { #expect(server.copyData == Data("1\tone\n".utf8)) #expect(server.copyFailMessages == ["canceled by user"]) #expect(pieces.piecesRead == 1) + _ = try await connection.executeQuery("SELECT 1") + #expect(server.cancelRequests == 0) + } + + @Test("A Stop while the server takes no data ends the copy with one cancel request, and the session goes on") + func stopReachesAServerThatTakesNoData() async throws { + let server = try Self.server(.init(takesNoDataUntilCancelled: true)) + defer { server.close() } + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + + let pieces = Pieces(["1\tone\n", "2\ttwo\n"]) { count in + if count == 1 { connection.cancelCurrentQuery() } + } + await #expect(throws: CancellationError.self) { + _ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil) + } + + #expect(server.cancelRequests == 1) + #expect(server.copyFailMessages == ["canceled by user"]) + _ = try await connection.executeQuery("SELECT 1") + #expect(server.cancelRequests == 1) + } + + @Test("A Stop while the COPY waits to start sends the cancel request at once and reads no piece") + func stopBeforeTheCopyStarts() async throws { + let stop = StopBox() + let server = try LibPQScriptedServer( + onQuery: { query in if query.hasPrefix("COPY") { stop.press() } }, + answer: { query in query.hasPrefix("COPY") ? LibPQScriptedServer.Answer(waitsForCancel: true) : nil } + ) + server.start() + defer { server.close() } + let connection = try await Self.connected(to: server) + defer { connection.disconnect() } + stop.connection = connection + + let pieces = Pieces(["1\tone\n"]) + await #expect(throws: CancellationError.self) { + _ = try await connection.copyFromStandardInput(Self.statement, rows: pieces.data, readOnly: nil) + } + + #expect(pieces.piecesRead == 0) + #expect(server.cancelRequests == 1) + _ = try await connection.executeQuery("SELECT 1") + } + + private final class StopBox: @unchecked Sendable { + private let lock = NSLock() + private weak var _connection: LibPQPluginConnection? + + var connection: LibPQPluginConnection? { + get { lock.withLock { _connection } } + set { lock.withLock { _connection = newValue } } + } + + func press() { + connection?.cancelCurrentQuery() + } } @Test("A binary COPY is refused with CopyFail before any piece is read") diff --git a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift index b7fd8c19d0..976411bfdc 100644 --- a/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift +++ b/apple/Plugins/PostgreSQLDriverTests/LibPQScriptedServer.swift @@ -13,7 +13,8 @@ import Foundation // completion, except that a statement holding `failingStatement` gets an error and the ones after it nothing. Cancel // requests arrive on connections of their own: each is counted, and a statement waiting for one then fails as a // cancelled statement does. A query answered with `copyIn` starts a copy-in: the server keeps every CopyData byte, and -// answers CopyDone with `COPY ` (or the scripted failure) and CopyFail with 57014, as PostgreSQL does. +// answers CopyDone with `COPY ` (or the scripted failure) and CopyFail with 57014, as PostgreSQL does, or +// takes no data until a cancel request arrives. final class LibPQScriptedServer: @unchecked Sendable { struct SetupFailure: Error {} @@ -42,6 +43,9 @@ final class LibPQScriptedServer: @unchecked Sendable { var binary = false // Sent after CopyDone in place of the completion, as a row the server refuses is reported. var failure: Failure? + // Reads nothing until a cancel request arrives, then fails with 57014 and passes over the rest of the copy, + // as a server waiting for a row lock does. + var takesNoDataUntilCancelled = false } static let idle = UInt8(ascii: "I") @@ -243,6 +247,18 @@ final class LibPQScriptedServer: @unchecked Sendable { guard send(client, Self.message("G", [format] + Self.bytes(UInt16(1)) + Self.bytes(UInt16(format)))) else { return false } + if copyIn.takesNoDataUntilCancelled { + guard cancelArrived.wait(timeout: .now() + Self.cancelWait) == .success else { return false } + failed = true + guard send(client, Self.errorResponse(Failure( + sqlState: "57014", message: "canceling statement due to user request" + ))) else { return false } + while let type = receive(client, 1)?.first, let body = receiveBody(client) { + if type == UInt8(ascii: "f") { lock.withLock { copyFailures.append(Self.cString(body, from: 0).text) } } + if type == UInt8(ascii: "c") || type == UInt8(ascii: "f") { return true } + } + return false + } var lines = 0 while let type = receive(client, 1)?.first, let body = receiveBody(client) { switch type { diff --git a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift index 4b71c40824..adb9b50c60 100644 --- a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift +++ b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift @@ -234,10 +234,12 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { } } + // The driver's Stop ends a copy at once (a CopyFail, and a cancel request when the server takes no data). func copyFromStandardInput(statement: String, line: Int, rows: PluginCopyData) async throws { guard driver.supportsCopyFromStandardInput else { throw SQLFileParseError.copyFromStandardInput(line: line) } - try await importedData(writes: true, statement: statement) { - _ = try await self.driver.copyFromStandardInput(statement: statement, rows: rows, context: nil) + let driver = self.driver + try await importedData(writes: true, statement: statement, interrupt: { try? driver.cancelQuery() }) { + _ = try await driver.copyFromStandardInput(statement: statement, rows: rows, context: nil) } } @@ -767,11 +769,13 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { writes: Bool = false, deletes: Bool = false, statement: String? = nil, + interrupt: (@Sendable () -> Void)? = nil, operation: () async throws -> T ) async throws -> T { if let importControl { return try await importControl.data( - rows: rows, writes: writes, deletes: deletes, statement: statement, operation: operation + rows: rows, writes: writes, deletes: deletes, statement: statement, interrupt: interrupt, + operation: operation ) } return try await operation() diff --git a/apple/TablePro/Core/Plugins/ImportSinkRun.swift b/apple/TablePro/Core/Plugins/ImportSinkRun.swift index 528c131a3b..d0f50b8eaa 100644 --- a/apple/TablePro/Core/Plugins/ImportSinkRun.swift +++ b/apple/TablePro/Core/Plugins/ImportSinkRun.swift @@ -100,6 +100,7 @@ internal final class ImportSinkRun: @unchecked Sendable { writes: Bool = false, deletes: Bool = false, statement: String? = nil, + interrupt: (@Sendable () -> Void)? = nil, operation: () async throws -> T ) async throws -> T { try await admitted(.data) { @@ -111,7 +112,12 @@ internal final class ImportSinkRun: @unchecked Sendable { let before = self.run.snapshot.currentState let hadAuthority = self.lock.withLock { self.ownsTransaction } do { - let value = try await operation() + let value: T + if let interrupt { + value = try await self.run.interruptible(by: interrupt, operation) + } else { + value = try await operation() + } if let index = self.fileIndex { self.run.record { $0.effects[index]?.submittedRows += rows diff --git a/apple/TablePro/Core/Plugins/SqlFileImportSource.swift b/apple/TablePro/Core/Plugins/SqlFileImportSource.swift index aa5fe6971f..12c2abeef3 100644 --- a/apple/TablePro/Core/Plugins/SqlFileImportSource.swift +++ b/apple/TablePro/Core/Plugins/SqlFileImportSource.swift @@ -15,6 +15,7 @@ final class SqlFileImportSource: PluginImportSource, @unchecked Sendable { private let url: URL private let encoding: String.Encoding private let grammar: OSAllocatedUnfairLock<@Sendable () -> SQLLexicalGrammar> + private let readReporter = OSAllocatedUnfairLock<(@Sendable (Int64, Int64) -> Void)?>(initialState: nil) private let family: TransactionEngineFamily private let parser = SQLFileParser() @@ -58,10 +59,16 @@ final class SqlFileImportSource: PluginImportSource, @unchecked Sendable { grammar.withLock { $0 = provider } } + // Bytes read from the file the parser opens, which for a `.gz` is the decompressed copy. + func reportsReading(to handler: @escaping @Sendable (_ bytesRead: Int64, _ fileSize: Int64) -> Void) { + readReporter.withLock { $0 = handler } + } + func statements() async throws -> AsyncThrowingStream<(statement: String, lineNumber: Int), Error> { let fileURL = try await resolveURL() let grammar = self.grammar.withLock { $0 } - let parsed = parser.parseFile(url: fileURL, encoding: encoding, grammar: grammar) + let onRead = readReporter.withLock { $0 } + let parsed = parser.parseFile(url: fileURL, encoding: encoding, grammar: grammar, onRead: onRead) guard !sendsFileBytesUnchanged else { return parsed } let rewriting = TranscodedDumpStatements(parsed, rewriter: rewriter(grammar: grammar)) return AsyncThrowingStream(unfolding: { @@ -72,7 +79,8 @@ final class SqlFileImportSource: PluginImportSource, @unchecked Sendable { func elements() async throws -> AsyncThrowingStream { let fileURL = try await resolveURL() let grammar = self.grammar.withLock { $0 } - let parsed = parser.parseElements(url: fileURL, encoding: encoding, grammar: grammar) + let onRead = readReporter.withLock { $0 } + let parsed = parser.parseElements(url: fileURL, encoding: encoding, grammar: grammar, onRead: onRead) guard !sendsFileBytesUnchanged else { return parsed } let rewriting = TranscodedDumpElements(parsed, rewriter: rewriter(grammar: grammar)) return AsyncThrowingStream(unfolding: { diff --git a/apple/TablePro/Core/Services/Export/ImportRun.swift b/apple/TablePro/Core/Services/Export/ImportRun.swift index 70fa682d39..37f0faa411 100644 --- a/apple/TablePro/Core/Services/Export/ImportRun.swift +++ b/apple/TablePro/Core/Services/Export/ImportRun.swift @@ -43,6 +43,7 @@ internal final class ImportRun: @unchecked Sendable { private var waiters: [UUID: CheckedContinuation] = [:] private var ledger = ImportLedger() private var onChange: (@Sendable () -> Void)? + private var interrupt: (@Sendable () -> Void)? internal var currentPhase: ImportRunPhase { lock.withLock { phase } @@ -107,11 +108,15 @@ internal final class ImportRun: @unchecked Sendable { if changed { notify() } } + // A COPY streams its whole block as one call, so a pause would wait for all of it. While one runs, Stop ends it + // and the run, as any failure would: its rows cannot be resumed, and an owned transaction can only roll back. @discardableResult internal func requestPause() -> Bool { - let changed = lock.withLock { () -> Bool in + transition { phase in guard phase == .running else { return false } - if inFlight == 0 { + if interrupt != nil { + phase = .stopping(.rollBack) + } else if inFlight == 0 { generation += 1 phase = .paused(generation: generation) } else { @@ -119,8 +124,25 @@ internal final class ImportRun: @unchecked Sendable { } return true } - if changed { notify() } - return changed + } + + // `interrupt` ends the call early; it runs when the run stops while the call is in flight. A Stop that came after + // the call was admitted stops it here, before it starts. + internal func interruptible( + by interrupt: @escaping @Sendable () -> Void, + _ body: () async throws -> T + ) async throws -> T { + let starts = lock.withLock { () -> Bool in + guard phase == .running else { return false } + self.interrupt = interrupt + return true + } + guard starts else { + stop(.rollBack) + throw PluginImportCancellationError() + } + defer { lock.withLock { self.interrupt = nil } } + return try await body() } // An answer to a question asked at `generation`: ignored once that pause has ended. @@ -278,7 +300,11 @@ internal final class ImportRun: @unchecked Sendable { private func transition(_ change: (inout ImportRunPhase) -> Bool) -> Bool { let wakes = lock.withLock { () -> [CheckedContinuation]? in guard change(&phase) else { return nil } - if case .stopping(let choice) = phase { chosenStop = choice } + if case .stopping(let choice) = phase { + chosenStop = choice + // Under the lock: the interrupted call cannot return and let the next statement start meanwhile. + interrupt?() + } switch phase { case .pausing, .paused: return [] diff --git a/apple/TablePro/Core/Services/Export/ImportService.swift b/apple/TablePro/Core/Services/Export/ImportService.swift index 0c1c351013..6959fc99ef 100644 --- a/apple/TablePro/Core/Services/Export/ImportService.swift +++ b/apple/TablePro/Core/Services/Export/ImportService.swift @@ -27,6 +27,8 @@ struct ImportState { // A row import counts rows through the statement counter. var countsRows = false var totalIsEstimate = true + var fileBytesRead: Int64 = 0 + var fileBytes: Int64 = 0 } // MARK: - Import Service @@ -85,6 +87,8 @@ final class ImportService: ObservableObject { func requestStop() { run.requestPause() + // A Stop during a COPY stops the run at once, and the file's reader has to see it too. + if run.stopChoice != nil { currentProgress?.cancel() } syncStopState() } @@ -183,6 +187,7 @@ final class ImportService: ObservableObject { decompressedURL: decompressedURL, ownsDecompressedFile: ownsDecompressedFile ) + sqlSource?.reportsReading(to: fileReadingReporter()) let source: any PluginImportSource if let sqlSource { source = sqlSource @@ -304,6 +309,26 @@ final class ImportService: ObservableObject { return [counts, status] } + // The parser reads a chunk every 64 KiB, so the main actor hears only of a move of a whole permille. + private func fileReadingReporter() -> @Sendable (Int64, Int64) -> Void { + let lastPermille = OSAllocatedUnfairLock(initialState: -1) + return { [weak self] read, size in + let permille = size > 0 ? min(1_000, read * 1_000 / size) : 0 + let moved = lastPermille.withLock { last in + guard last != permille else { return false } + last = permille + return true + } + guard moved else { return } + Task { @MainActor [weak self] in + guard let self else { return } + self.state.fileBytes = size + // Hops can land out of order, and the bar never moves back. + self.state.fileBytesRead = max(self.state.fileBytesRead, read) + } + } + } + // MARK: - Several files func importFiles( diff --git a/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift b/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift index 475e977f90..3e7573c572 100644 --- a/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift +++ b/apple/TablePro/Core/Utilities/SQL/SQLFileParser.swift @@ -967,9 +967,10 @@ final class SQLFileParser: Sendable { func parseFile( url: URL, encoding: String.Encoding, - grammar: @escaping @Sendable () -> SQLLexicalGrammar + grammar: @escaping @Sendable () -> SQLLexicalGrammar, + onRead: (@Sendable (_ bytesRead: Int64, _ fileSize: Int64) -> Void)? = nil ) -> AsyncThrowingStream<(statement: String, lineNumber: Int), Error> { - let session = ParseSession(url: url, encoding: encoding, grammar: grammar, countOnly: false, limits: limits) + let session = ParseSession(url: url, encoding: encoding, grammar: grammar, countOnly: false, limits: limits, onRead: onRead) return AsyncThrowingStream(unfolding: { try await session.nextRun() }) @@ -987,9 +988,10 @@ final class SQLFileParser: Sendable { func parseElements( url: URL, encoding: String.Encoding, - grammar: @escaping @Sendable () -> SQLLexicalGrammar + grammar: @escaping @Sendable () -> SQLLexicalGrammar, + onRead: (@Sendable (_ bytesRead: Int64, _ fileSize: Int64) -> Void)? = nil ) -> AsyncThrowingStream { - let session = ParseSession(url: url, encoding: encoding, grammar: grammar, countOnly: false, limits: limits) + let session = ParseSession(url: url, encoding: encoding, grammar: grammar, countOnly: false, limits: limits, onRead: onRead) return AsyncThrowingStream(unfolding: { try await session.nextElement() }) @@ -1028,8 +1030,11 @@ extension SQLFileParser { private let encoding: String.Encoding private let grammar: @Sendable () -> SQLLexicalGrammar private let limits: ReadLimits + private let onRead: (@Sendable (Int64, Int64) -> Void)? private var fileHandle: FileHandle? + private var fileSize: Int64 = 0 + private var bytesRead: Int64 = 0 private var ctx: ParserContext private let nsBuffer = NSMutableString() // Text before it is lexed; it is deleted when the next chunk is read, not once per statement. @@ -1053,12 +1058,14 @@ extension SQLFileParser { grammar: @escaping @Sendable () -> SQLLexicalGrammar, countOnly: Bool, limits: ReadLimits, - reading: Reading = .fromFile + reading: Reading = .fromFile, + onRead: (@Sendable (Int64, Int64) -> Void)? = nil ) { self.url = url self.encoding = encoding self.grammar = grammar self.limits = limits + self.onRead = onRead self.decoder = SQLChunkDecoder(encoding: encoding) self.readingSettled = reading == .batches let initialGrammar = grammar() @@ -1194,6 +1201,10 @@ extension SQLFileParser { private func readChunk() throws { let handle = try openFileIfNeeded() let rawData = handle.readData(ofLength: limits.chunkSize) + if !rawData.isEmpty, let onRead { + bytesRead += Int64(rawData.count) + onRead(bytesRead, fileSize) + } if rawData.isEmpty && !decoder.hasPendingBytes { ctx.atEndOfInput = true @@ -1388,6 +1399,11 @@ extension SQLFileParser { } let handle = try FileHandle(forReadingFrom: url) fileHandle = handle + // Taken from the open handle; a size of 0 leaves the import's bar on statements. + if onRead != nil { + var info = stat() + fileSize = fstat(handle.fileDescriptor, &info) == 0 ? Int64(info.st_size) : 0 + } return handle } diff --git a/apple/TablePro/Views/Components/TransferProgress.swift b/apple/TablePro/Views/Components/TransferProgress.swift index 9bbda34a00..5f93bbf28f 100644 --- a/apple/TablePro/Views/Components/TransferProgress.swift +++ b/apple/TablePro/Views/Components/TransferProgress.swift @@ -133,6 +133,7 @@ internal struct TransferProgress: Equatable, Sendable { internal var unit: TransferUnit internal var isEstimate: Bool internal var note: String? + internal var fileFraction: Double? internal var isStopping: Bool internal init( @@ -144,6 +145,7 @@ internal struct TransferProgress: Equatable, Sendable { unit: TransferUnit, isEstimate: Bool = false, note: String? = nil, + fileFraction: Double? = nil, isStopping: Bool = false ) { self.job = job @@ -154,6 +156,7 @@ internal struct TransferProgress: Equatable, Sendable { self.unit = unit self.isEstimate = isEstimate self.note = note.flatMap { $0.isEmpty ? nil : $0 } + self.fileFraction = fileFraction self.isStopping = isStopping } @@ -161,8 +164,11 @@ internal struct TransferProgress: Equatable, Sendable { internal var stopPolicy: TransferStopPolicy { job.stopPolicy } - // An estimate stops short of the end, so a bar never reads done while the job still runs. + // An estimate stops short of the end, so a bar never reads done while the job still runs. So does a file read to + // its end: its last statements still run. internal var fraction: Double? { + // A statement count cannot move while one COPY streams its rows, and the file's bytes read can. + if let fileFraction { return min(0.99, max(0.0, fileFraction)) } guard let total else { return nil } let ceiling = isEstimate ? 0.95 : 1.0 return min(ceiling, Double(completed) / Double(total)) diff --git a/apple/TablePro/Views/Import/ImportProgressView.swift b/apple/TablePro/Views/Import/ImportProgressView.swift index 02aa447db0..939bd6d089 100644 --- a/apple/TablePro/Views/Import/ImportProgressView.swift +++ b/apple/TablePro/Views/Import/ImportProgressView.swift @@ -67,7 +67,8 @@ extension ImportState { total: Int64(estimatedTotalStatements), unit: countsRows ? .rows : .statements, isEstimate: totalIsEstimate, - note: statusMessage + note: statusMessage, + fileFraction: !countsRows && fileBytes > 0 ? Double(fileBytesRead) / Double(fileBytes) : nil ) } } diff --git a/apple/TableProTests/Core/Plugins/ImportDataSinkAdapterCopyTests.swift b/apple/TableProTests/Core/Plugins/ImportDataSinkAdapterCopyTests.swift index 4417e5f628..2df5b46f5f 100644 --- a/apple/TableProTests/Core/Plugins/ImportDataSinkAdapterCopyTests.swift +++ b/apple/TableProTests/Core/Plugins/ImportDataSinkAdapterCopyTests.swift @@ -7,6 +7,7 @@ import Foundation import os @testable import TablePro import TableProPluginKit +import TableProTestSupport import Testing private final class CopyingDriver: PluginDriverStub, @unchecked Sendable { @@ -40,6 +41,34 @@ private final class CopyingDriver: PluginDriverStub, @unchecked Sendable { } } +// Reads the first piece, then holds the copy until a Stop cancels the query, as a copy waiting on its server does. +private final class StoppableCopyDriver: PluginDriverStub, @unchecked Sendable { + let started = AsyncGate() + private let stopped = AsyncGate() + private let lock = NSLock() + private var cancels = 0 + + var capabilities: PluginCapabilities { [.transactions, .copyFromStandardInput] } + + var cancelCount: Int { lock.withLock { cancels } } + + func cancelQuery() throws { + lock.withLock { cancels += 1 } + Task { await stopped.open() } + } + + func copyFromStandardInput( + statement: String, + rows: PluginCopyData, + context: PluginStatementContext + ) async throws -> PluginQueryResult { + _ = try rows.nextPiece() + await started.open() + await stopped.wait() + throw CancellationError() + } +} + struct ImportDataSinkAdapterCopyTests { private static func rows(_ text: String) -> PluginCopyData { let remaining = OSAllocatedUnfairLock(initialState: Data(text.utf8)) @@ -79,4 +108,27 @@ struct ImportDataSinkAdapterCopyTests { } #expect(driver.received.isEmpty) } + + @Test("Stop during an import's COPY cancels the driver's copy at once and stops the run with rollback") + func stopCancelsTheCopy() async throws { + let driver = StoppableCopyDriver() + let run = ImportRun() + let connection = TestFixtures.makeConnection(type: .postgresql) + let sink = ImportDataSinkAdapter( + driver: PluginDriverAdapter(connection: connection, pluginDriver: driver), + databaseType: .postgresql, + importRun: run + ) + let copy = Task { + try await sink.copyFromStandardInput(statement: "COPY t (a) FROM stdin", line: 1, rows: Self.rows("1\n")) + } + await driver.started.wait() + + run.requestPause() + + #expect(driver.cancelCount == 1) + #expect(run.stopChoice == .rollBack) + await #expect(throws: CancellationError.self) { try await copy.value } + #expect(run.questionIfPaused() == nil) + } } diff --git a/apple/TableProTests/Core/Services/ImportRunTests.swift b/apple/TableProTests/Core/Services/ImportRunTests.swift index d43c3a4a25..011073d9ca 100644 --- a/apple/TableProTests/Core/Services/ImportRunTests.swift +++ b/apple/TableProTests/Core/Services/ImportRunTests.swift @@ -208,4 +208,101 @@ struct ImportStopResultTests { #expect(value.hasWrites) #expect(ImportStopResult(ledger: value).fileText(for: 0).contains("emptied")) } + + private final class Interrupts: @unchecked Sendable { + private let lock = NSLock() + private var value = 0 + + var count: Int { lock.withLock { value } } + + func record() { lock.withLock { value += 1 } } + } + + // Admits one call and holds it inside `interruptible` until `release` opens. + private static func copyInFlight( + on run: ImportRun, + interrupts: Interrupts, + release: AsyncGate + ) async throws -> Task { + #expect(try await run.admit(.data) == .proceed) + let inside = AsyncGate() + let call = Task { + _ = try? await run.interruptible(by: { interrupts.record() }) { + await inside.open() + await release.wait() + } + run.release() + } + await inside.wait() + return call + } + + @Test("Stop during a COPY ends it at once and stops the run with rollback, asking nothing") + func stopInterruptsACopy() async throws { + let run = ImportRun() + let interrupts = Interrupts() + let release = AsyncGate() + let call = try await Self.copyInFlight(on: run, interrupts: interrupts, release: release) + + run.requestPause() + + #expect(interrupts.count == 1) + #expect(run.currentPhase == .stopping(.rollBack)) + #expect(run.questionIfPaused() == nil) + await release.open() + await call.value + await #expect(throws: PluginImportCancellationError.self) { try await run.admit(.data) } + await #expect(throws: PluginImportCancellationError.self) { try await run.admit(.commit) } + #expect(try await run.admit(.rollback) == .proceed) + run.release() + #expect(interrupts.count == 1) + } + + @Test("Closing the import during a COPY ends the COPY too") + func teardownInterruptsACopy() async throws { + let run = ImportRun() + let interrupts = Interrupts() + let release = AsyncGate() + let call = try await Self.copyInFlight(on: run, interrupts: interrupts, release: release) + + run.stop(.rollBack) + + #expect(interrupts.count == 1) + await release.open() + await call.value + } + + @Test("Once the COPY returned, Stop pauses for the question as for any statement") + func stopAfterTheCopyPauses() async throws { + let run = ImportRun() + let interrupts = Interrupts() + #expect(try await run.admit(.data) == .proceed) + try await run.interruptible(by: { interrupts.record() }) {} + + run.requestPause() + + #expect(run.currentPhase == .pausing) + run.release() + #expect(run.questionIfPaused() != nil) + #expect(interrupts.count == 0) + } + + @Test("A Stop between a COPY's admission and its start stops it before a row is sent") + func stopBeforeTheCopyStarts() async throws { + let run = ImportRun() + let interrupts = Interrupts() + #expect(try await run.admit(.data) == .proceed) + run.requestPause() + + let bodyRuns = Interrupts() + await #expect(throws: PluginImportCancellationError.self) { + try await run.interruptible(by: { interrupts.record() }) { bodyRuns.record() } + } + + #expect(bodyRuns.count == 0) + #expect(interrupts.count == 0) + #expect(run.currentPhase == .stopping(.rollBack)) + run.release() + #expect(run.questionIfPaused() == nil) + } } diff --git a/apple/TableProTests/Core/Utilities/SQL/SQLFileParserBytesReadTests.swift b/apple/TableProTests/Core/Utilities/SQL/SQLFileParserBytesReadTests.swift new file mode 100644 index 0000000000..a517d3b548 --- /dev/null +++ b/apple/TableProTests/Core/Utilities/SQL/SQLFileParserBytesReadTests.swift @@ -0,0 +1,151 @@ +// +// SQLFileParserBytesReadTests.swift +// TableProTests +// + +import Foundation +import os +@testable import TablePro +import TableProPluginKit +import TableProSQLGrammar +import Testing + +private final class ReadReports: Sendable { + struct Report: Equatable, Sendable { + let read: Int64 + let size: Int64 + } + + private let reports = OSAllocatedUnfairLock<[Report]>(initialState: []) + + func record(read: Int64, size: Int64) { + reports.withLock { $0.append(Report(read: read, size: size)) } + } + + var all: [Report] { + reports.withLock { $0 } + } +} + +struct SQLFileParserBytesReadTests { + private static func withFile(_ sql: String, _ body: (URL) async throws -> T) async throws -> T { + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("SQLFileParserBytesReadTests-\(UUID().uuidString).sql") + try Data(sql.utf8).write(to: url) + defer { try? FileManager.default.removeItem(at: url) } + return try await body(url) + } + + private static func expectGrowsToTheEnd( + _ reports: [ReadReports.Report], + of sql: String, + sourceLocation: SourceLocation = #_sourceLocation + ) { + let fileSize = Int64(Data(sql.utf8).count) + let grows = zip(reports, reports.dropFirst()).allSatisfy { pair in pair.0.read < pair.1.read } + let sizes = Set(reports.map { report in report.size }) + + #expect(reports.count > 1, sourceLocation: sourceLocation) + #expect(grows, "\(reports.map { report in report.read })", sourceLocation: sourceLocation) + #expect(reports.last?.read == fileSize, sourceLocation: sourceLocation) + #expect(sizes == [fileSize], sourceLocation: sourceLocation) + } + + @Test("Each chunk read reports the bytes read so far and the file's size, up to the whole file") + func reportsEachChunk() async throws { + let sql = (1...200).map { "SELECT \($0);\n" }.joined() + let reports = ReadReports() + let parser = SQLFileParser(chunkSize: 64) + + let (elements, count): (Int, Int) = try await Self.withFile(sql) { url in + var yielded = 0 + let stream = parser.parseElements( + url: url, + encoding: .utf8, + grammar: { TestGrammar.postgres }, + onRead: { read, size in reports.record(read: read, size: size) } + ) + for try await _ in stream { + yielded += 1 + } + let counted = try await parser.countStatements(url: url, encoding: .utf8, grammar: TestGrammar.postgres) + return (yielded, counted) + } + + #expect(elements == 200) + #expect(count == 200) + Self.expectGrowsToTheEnd(reports.all, of: sql) + } + + @Test("The statement stream reports reading the same way") + func statementStreamReports() async throws { + let sql = (1...100).map { "SELECT \($0);\n" }.joined() + let reports = ReadReports() + + try await Self.withFile(sql) { url in + let stream = SQLFileParser(chunkSize: 50).parseFile( + url: url, + encoding: .utf8, + grammar: { TestGrammar.postgres }, + onRead: { read, size in reports.record(read: read, size: size) } + ) + for try await _ in stream {} + } + + Self.expectGrowsToTheEnd(reports.all, of: sql) + } + + @Test("Bytes read grow while a COPY's rows are read, before the next element is asked for") + func bytesGrowWhileCopyRowsAreRead() async throws { + let rows = (1...2_000).map { "\($0)\trow \($0)\n" }.joined() + let sql = "CREATE TABLE t (a int, b text);\nCOPY t (a, b) FROM stdin;\n\(rows)\\.\nSELECT 1;\n" + let reports = ReadReports() + let parser = SQLFileParser(chunkSize: 256, copyPieceLength: 128) + + let copies: Int = try await Self.withFile(sql) { url in + var copiesRead = 0 + let stream = parser.parseElements( + url: url, + encoding: .utf8, + grammar: { TestGrammar.postgres }, + onRead: { read, size in reports.record(read: read, size: size) } + ) + for try await element in stream { + guard case let .copyFromStandardInput(_, _, copyRows) = element else { continue } + copiesRead += 1 + let before = reports.all + var data = Data() + while let piece = try copyRows.nextPiece() { + data.append(piece) + } + let after = reports.all + + #expect(data == Data(rows.utf8)) + #expect(after.count > before.count + 1) + #expect((after.last?.read ?? 0) > (before.last?.read ?? 0)) + } + return copiesRead + } + + #expect(copies == 1) + Self.expectGrowsToTheEnd(reports.all, of: sql) + } + + @Test("The scan for GO lines before a SQL Server script is cut reports nothing") + func batchSeparatorScanReportsNothing() async throws { + let sql = (1...100).map { "SELECT \($0);\n" }.joined() + "GO\n" + let reports = ReadReports() + + try await Self.withFile(sql) { url in + let stream = SQLFileParser(chunkSize: 64).parseElements( + url: url, + encoding: .utf8, + grammar: { TestGrammar.sqlServer }, + onRead: { read, size in reports.record(read: read, size: size) } + ) + for try await _ in stream {} + } + + Self.expectGrowsToTheEnd(reports.all, of: sql) + } +} diff --git a/apple/TableProTests/Views/Components/TransferProgressTests.swift b/apple/TableProTests/Views/Components/TransferProgressTests.swift index 168f4dcac1..df84675e92 100644 --- a/apple/TableProTests/Views/Components/TransferProgressTests.swift +++ b/apple/TableProTests/Views/Components/TransferProgressTests.swift @@ -47,6 +47,19 @@ struct TransferProgressTests { #expect(progress.amountText == "\(grouped(5_000)) of about \(grouped(4_000)) statements") } + @Test("A file fraction drives the bar, short of the end, while the amount still counts statements") + func fileFractionDrivesTheBar() { + let reading = TransferProgress( + job: .dataImport, completed: 1, total: 40, unit: .statements, isEstimate: true, fileFraction: 0.6 + ) + let past = TransferProgress(job: .dataImport, completed: 40, total: 40, unit: .statements, fileFraction: 1.4) + + #expect(reading.fraction == 0.6) + #expect(reading.amountText == "1 of about 40 statements") + #expect(past.fraction == 0.99) + #expect(past.amountText == "40 of 40 statements") + } + @Test("Counts pick the singular where the number is one") func singular() { #expect(TransferProgress(job: .export, completed: 1, unit: .rows).amountText == "1 row") diff --git a/apple/TableProTests/Views/Import/ImportStateTransferProgressTests.swift b/apple/TableProTests/Views/Import/ImportStateTransferProgressTests.swift new file mode 100644 index 0000000000..d42279ebae --- /dev/null +++ b/apple/TableProTests/Views/Import/ImportStateTransferProgressTests.swift @@ -0,0 +1,53 @@ +// +// ImportStateTransferProgressTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct ImportStateTransferProgressTests { + @Test("An SQL import's bar follows the file's bytes read, and its amount still counts statements") + func sqlImportFollowsBytesRead() { + var state = ImportState( + isImporting: true, processedStatements: 3, estimatedTotalStatements: 100, fileName: "dump.sql" + ) + state.fileBytes = 1_000 + state.fileBytesRead = 250 + + let progress = state.transferProgress + + #expect(progress.fraction == 0.25) + #expect(progress.amountText == "3 of about 100 statements") + } + + @Test("Before the file's size is known, an SQL import's bar is statements done of the total") + func noFileSizeFallsBackToStatements() { + var state = ImportState( + isImporting: true, processedStatements: 25, estimatedTotalStatements: 100, fileName: "dump.sql" + ) + state.totalIsEstimate = false + + let progress = state.transferProgress + + #expect(progress.fraction == 0.25) + #expect(progress.amountText == "25 of 100 statements") + } + + @Test("A row import's bar ignores the file's bytes read") + func rowImportIgnoresBytes() { + var state = ImportState( + isImporting: true, processedStatements: 30, estimatedTotalStatements: 120, fileName: "people.csv" + ) + state.countsRows = true + state.totalIsEstimate = false + state.fileBytes = 1_000 + state.fileBytesRead = 900 + + let progress = state.transferProgress + + #expect(progress.fraction == 0.25) + #expect(progress.amountText == "30 of 120 rows") + } +} diff --git a/apple/changelog.d/4263.changed.md b/apple/changelog.d/4263.changed.md new file mode 100644 index 0000000000..783b896f14 --- /dev/null +++ b/apple/changelog.d/4263.changed.md @@ -0,0 +1 @@ +The SQL import progress bar follows how much of the file has been read, so it moves while a large COPY block loads. diff --git a/apple/scripts/probes/check-postgresql-copy-in.sh b/apple/scripts/probes/check-postgresql-copy-in.sh index ea1722620f..233983bc9d 100755 --- a/apple/scripts/probes/check-postgresql-copy-in.sh +++ b/apple/scripts/probes/check-postgresql-copy-in.sh @@ -8,6 +8,11 @@ # 25006; a cancel request mid-stream fails it with 57014. It also prints what a sent `\.` line does, # which PostgreSQL 18 reads as data in CSV. # +# Stop, sent the way LibPQCopyIn sends rows (nonblocking, waiting for write-ready in 100 ms slices): a Stop +# while the server takes rows ends the COPY with its CopyFail at once; a Stop while the server waits on a row +# lock takes no data, so CopyFail alone waits for the lock, and one cancel request ends it at once. Either way +# the session runs the next statement, an open block is left aborted and no row stays. +# # Usage: # apple/scripts/probes/check-postgresql-copy-in.sh "host=127.0.0.1 port=5432 user=postgres dbname=postgres" # @@ -27,7 +32,9 @@ trap 'rm -rf "$WORK_DIR"' EXIT cat > "$WORK_DIR/probe.c" << 'C_SOURCE' #include #include +#include #include +#include #include #include #include @@ -122,7 +129,122 @@ static void *cancel_after(void *arg) { return NULL; } +static atomic_int stop_requested; +static double stop_time; + +static double now_ms(void) { + struct timespec t; + clock_gettime(CLOCK_MONOTONIC, &t); + return t.tv_sec * 1e3 + t.tv_nsec / 1e6; +} + +static void *stop_after(void *arg) { + usleep((useconds_t)(*(int *)arg) * 1000); + stop_time = now_ms(); + atomic_store(&stop_requested, 1); + return NULL; +} + +typedef struct { + Outcome outcome; + double stop_to_end_ms; + int sent_cancel; + int gave_up; +} Stopped; + +// One 100 ms slice of waiting for the queued bytes to go out: 0 once they all went, -1 on a lost connection. +static int flush_slice(PGconn *c) { + int flushed = PQflush(c); + if (flushed <= 0) return flushed; + if (PQsocketPoll(PQsocket(c), 1, 1, PQgetCurrentTimeUSec() + 100000) > 0) PQconsumeInput(c); + return 1; +} + +// One slice of waiting for the COPY's result once it ended: 0 once the result is in, -1 on a lost connection. A +// proxy can take every byte while the server reads none, so a flushed CopyFail is not an answered one. +static int answer_slice(PGconn *c) { + int flushed = PQflush(c); + if (flushed < 0) return -1; + if (flushed == 0 && !PQisBusy(c)) return 0; + if (PQsocketPoll(PQsocket(c), 1, flushed, PQgetCurrentTimeUSec() + 100000) > 0 && !PQconsumeInput(c)) return -1; + return flushed == 0 && !PQisBusy(c) ? 0 : 1; +} + +// LibPQCopyIn's loop: pieces queued in nonblocking mode and flushed in slices, so a Stop is seen while the server +// takes no data. A Stop while rows go out ends the copy with CopyFail, and with `cancels` one cancel request follows +// once a slice passes with no answer; a Stop after CopyDone sends the cancel request at once. Without `cancels` the +// wait gives up after `give_up_ms` and the copy stays open for the caller to finish. +static Stopped copy_in_stoppable(PGconn *c, const char *header, const char *data, size_t length, size_t piece, + int stop_delay_ms, int cancels, double give_up_ms) { + Stopped s; + memset(&s, 0, sizeof s); + atomic_store(&stop_requested, 0); + PGresult *start = PQexec(c, header); + if (PQresultStatus(start) != PGRES_COPY_IN) { + s.outcome.status = PQresultStatus(start); + PQclear(start); + while ((start = PQgetResult(c))) PQclear(start); + return s; + } + PQclear(start); + pthread_t timer; + pthread_create(&timer, NULL, stop_after, &stop_delay_ms); + PQsetnonblocking(c, 1); + int failed_copy = 0; + for (size_t at = 0; at < length && !failed_copy; at += piece) { + size_t n = length - at < piece ? length - at : piece; + if (atomic_load(&stop_requested)) { failed_copy = 1; break; } + if (PQputCopyData(c, data + at, (int)n) != 1) { snprintf(s.outcome.message, sizeof s.outcome.message, "put failed"); break; } + int flushing; + while ((flushing = flush_slice(c)) == 1) { + if (atomic_load(&stop_requested)) { failed_copy = 1; break; } + } + if (flushing < 0) break; + } + PQputCopyEnd(c, failed_copy ? "canceled by user" : NULL); + int waited = 0; + while (answer_slice(c) == 1) { + if (!atomic_load(&stop_requested)) continue; + if (cancels && !s.sent_cancel && (!failed_copy || waited >= 1)) { + PGcancel *cancel = PQgetCancel(c); + char err[256]; + PQcancel(cancel, err, sizeof err); + PQfreeCancel(cancel); + s.sent_cancel = 1; + } + waited++; + if (!cancels && now_ms() - stop_time > give_up_ms) { s.gave_up = 1; break; } + } + pthread_join(timer, NULL); + if (s.gave_up) return s; + PQsetnonblocking(c, 0); + PGresult *r = PQgetResult(c); + s.stop_to_end_ms = now_ms() - stop_time; + s.outcome.status = PQresultStatus(r); + snprintf(s.outcome.tuples, sizeof s.outcome.tuples, "%s", PQcmdTuples(r)); + const char *st = PQresultErrorField(r, PG_DIAG_SQLSTATE); + snprintf(s.outcome.state, sizeof s.outcome.state, "%s", st ? st : ""); + const char *msg = PQresultErrorField(r, PG_DIAG_MESSAGE_PRIMARY); + if (msg) snprintf(s.outcome.message, sizeof s.outcome.message, "%s", msg); + PQclear(r); + while ((r = PQgetResult(c))) { s.outcome.extra++; PQclear(r); } + return s; +} + +static void describe_stop(char *out, size_t size, Stopped s) { + char d[700]; + describe(d, sizeof d, s.outcome); + snprintf(out, size, "%s, %.0f ms from Stop to the result, cancel request %s", d, s.stop_to_end_ms, + s.sent_cancel ? "sent" : "not sent"); +} + +// The rest of the block after a Stop: what a session that takes the next statement answers. +static int runs_next_statement(PGconn *c) { + return strcmp(scalar(c, "SELECT 1"), "1") == 0; +} + int main(int argc, char **argv) { + setvbuf(stdout, NULL, _IONBF, 0); PGconn *c = PQconnectdb(argv[1]); if (PQstatus(c) != CONNECTION_OK) { fprintf(stderr, "no server: %s", PQerrorMessage(c)); return 3; } printf("libpq %d, server %d\n", PQlibVersion(), PQserverVersion(c)); @@ -224,6 +346,69 @@ int main(int argc, char **argv) { check("a cancel request mid-stream fails the COPY with 57014", o.status == PGRES_FATAL_ERROR && strcmp(o.state, "57014") == 0, d); free(lots); + // Stop while the server takes rows: CopyFail goes out behind the piece being sent. + size_t streamed = 128u * 1024u * 1024u; + char *stream = malloc(streamed); + size_t streamable = streamed - (streamed % 7); + for (size_t i = 0; i + 7 <= streamable; i += 7) memcpy(stream + i, "1\tabcd\n", 7); + run(c, "TRUNCATE copy_probe"); + Stopped s = copy_in_stoppable(c, "COPY copy_probe (a, b) FROM STDIN", stream, streamable, 1 << 20, 300, 1, 0); + describe_stop(d, sizeof d, s); + check("a Stop mid-stream ends the COPY within a second", + s.outcome.status == PGRES_FATAL_ERROR && strcmp(s.outcome.state, "57014") == 0 && s.stop_to_end_ms < 1000, d); + check("after the Stop an autocommit session is idle and runs the next statement", + PQtransactionStatus(c) == PQTRANS_IDLE && runs_next_statement(c), NULL); + check("no row of the stopped COPY stays", strcmp(scalar(c, "SELECT count(*) FROM copy_probe"), "0") == 0, NULL); + s = copy_in_stoppable(c, "COPY copy_probe (a, b) FROM STDIN", stream, streamable, 1 << 20, 300, 0, 10000); + describe_stop(d, sizeof d, s); + printf("info the same Stop with CopyFail alone: %s\n", s.gave_up ? "no answer in 10 s" : d); + + run(c, "BEGIN"); + s = copy_in_stoppable(c, "COPY copy_probe (a, b) FROM STDIN", stream, streamable, 1 << 20, 300, 1, 0); + describe_stop(d, sizeof d, s); + check("a Stop mid-stream inside a block fails the COPY", s.outcome.status == PGRES_FATAL_ERROR && strcmp(s.outcome.state, "57014") == 0, d); + check("the stopped COPY leaves the block aborted", PQtransactionStatus(c) == PQTRANS_INERROR, NULL); + run(c, "ROLLBACK"); + check("after ROLLBACK the session runs the next statement", runs_next_statement(c), NULL); + free(stream); + + // Stop while the server waits on a row lock another session holds: it takes no data until the lock goes. + PGconn *holder = PQconnectdb(argv[1]); + if (PQstatus(holder) != CONNECTION_OK) { fprintf(stderr, "no second session: %s", PQerrorMessage(holder)); return 3; } + run(c, "DROP TABLE IF EXISTS copy_lock; CREATE TABLE copy_lock (a int PRIMARY KEY)"); + size_t keys = 20000000; + char *numbers = malloc(keys * 10); + size_t numbers_length = 0; + for (size_t k = 1; k <= keys; k++) numbers_length += (size_t)sprintf(numbers + numbers_length, "%zu\n", k); + + run(holder, "BEGIN; INSERT INTO copy_lock VALUES (400000)"); + s = copy_in_stoppable(c, "COPY copy_lock FROM STDIN", numbers, numbers_length, 1 << 20, 1500, 0, 2000); + check("on a server waiting for a row lock, CopyFail alone is still unsent 2 s after the Stop", s.gave_up, NULL); + run(holder, "ROLLBACK"); + while (PQflush(c) == 1) PQsocketPoll(PQsocket(c), 1, 1, PQgetCurrentTimeUSec() + 100000); + PQsetnonblocking(c, 0); + PGresult *late = PQgetResult(c); + const char *late_state = PQresultErrorField(late, PG_DIAG_SQLSTATE); + check("once the lock goes, the CopyFail fails the COPY", PQresultStatus(late) == PGRES_FATAL_ERROR && late_state && strcmp(late_state, "57014") == 0, + PQresultErrorField(late, PG_DIAG_MESSAGE_PRIMARY)); + PQclear(late); + while ((late = PQgetResult(c))) PQclear(late); + + run(holder, "BEGIN; INSERT INTO copy_lock VALUES (400000)"); + run(c, "BEGIN"); + s = copy_in_stoppable(c, "COPY copy_lock FROM STDIN", numbers, numbers_length, 1 << 20, 1500, 1, 0); + describe_stop(d, sizeof d, s); + check("on a server waiting for a row lock, a Stop with one cancel request ends the COPY within a second", + s.sent_cancel && s.outcome.status == PGRES_FATAL_ERROR && strcmp(s.outcome.state, "57014") == 0 && s.stop_to_end_ms < 1000, d); + check("the COPY stopped in a lock wait leaves the block aborted", PQtransactionStatus(c) == PQTRANS_INERROR, NULL); + run(c, "ROLLBACK"); + check("after the cancel request the session runs the next statement", runs_next_statement(c), NULL); + check("no row of the COPY stopped in a lock wait stays", strcmp(scalar(c, "SELECT count(*) FROM copy_lock"), "0") == 0, NULL); + run(holder, "ROLLBACK"); + PQfinish(holder); + free(numbers); + run(c, "DROP TABLE copy_lock"); + run(c, "DROP TABLE copy_probe"); PQfinish(c); printf("%d failure(s)\n", failures);