diff --git a/apple/Packages/TableProCore/Sources/TableProGoogleCloud/Auth/GoogleAccessTokenWait.swift b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/Auth/GoogleAccessTokenWait.swift new file mode 100644 index 0000000000..cf907ce7fb --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/Auth/GoogleAccessTokenWait.swift @@ -0,0 +1,87 @@ +import Foundation + +public enum GoogleAccessTokenWait { + // A cancelled caller stops waiting; the fetch, which other callers may share, runs on to its end. + public static func token(from provider: any GoogleAccessTokenProviding) async throws -> String { + let wait = TokenWait(provider: provider) + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { caller in + wait.start(caller: caller) + } + } onCancel: { + wait.cancel() + } + } +} + +private final class TokenWait: @unchecked Sendable { + typealias Caller = CheckedContinuation + + private enum Stage { + case notStarted + case cancelledBeforeStart + case waiting(Caller) + case finished + } + + private let provider: any GoogleAccessTokenProviding + private let lock = NSLock() + private var stage = Stage.notStarted + + init(provider: any GoogleAccessTokenProviding) { + self.provider = provider + } + + func start(caller waiting: Caller) { + let launches: Bool = lock.withLock { + switch stage { + case .notStarted: + stage = .waiting(waiting) + return true + case .cancelledBeforeStart: + stage = .finished + return false + case .waiting, .finished: + return false + } + } + guard launches else { + waiting.resume(throwing: CancellationError()) + return + } + Task { [provider] in + let result: Result + do { + result = .success(try await provider.accessToken()) + } catch { + result = .failure(error) + } + self.finish(with: result) + } + } + + func cancel() { + let waiting: Caller? = lock.withLock { + switch stage { + case .notStarted: + stage = .cancelledBeforeStart + return nil + case .waiting(let caller): + stage = .finished + return caller + case .cancelledBeforeStart, .finished: + return nil + } + } + waiting?.resume(throwing: CancellationError()) + } + + private func finish(with result: Result) { + let waiting: Caller? = lock.withLock { + guard case .waiting(let caller) = stage else { return nil } + stage = .finished + return caller + } + waiting?.resume(with: result) + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryRequestLoop.swift b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryRequestLoop.swift new file mode 100644 index 0000000000..a3cf83b08b --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryRequestLoop.swift @@ -0,0 +1,44 @@ +import Foundation + +public enum BigQueryRequestLoop { + public struct Answer: Sendable { + public let status: Int + public let data: Data + + public init(status: Int, data: Data) { + self.status = status + self.data = data + } + } + + public static let rateLimitRetries = 3 + private static let unauthorizedStatus = 401 + private static let rateLimitedStatus = 429 + + // Nil when Stop came before a send. Stop is read again after the token fetch, which can take a round trip. + public static func run( + signals: BigQueryRequestSignals, + authorize: () async throws -> Token, + send: (Token) async throws -> Answer, + refreshToken: () async -> Void, + backoff: (Int) async throws -> Void + ) async throws -> Answer? { + var rateLimitAttempt = 0 + var refreshedToken = false + while true { + guard !signals.isStopped() else { return nil } + let token = try await authorize() + guard signals.beginAttempt() else { return nil } + let answer = try await send(token) + if answer.status == unauthorizedStatus, !refreshedToken { + refreshedToken = true + await refreshToken() + } else if answer.status == rateLimitedStatus, rateLimitAttempt < rateLimitRetries { + rateLimitAttempt += 1 + try await backoff(rateLimitAttempt) + } else { + return answer + } + } + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStatementRequest.swift b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStatementRequest.swift new file mode 100644 index 0000000000..c9ee23e244 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStatementRequest.swift @@ -0,0 +1,94 @@ +import Foundation + +public enum BigQueryStatementPolicy { + public static let jobCreationMode = "JOB_CREATION_OPTIONAL" + + // A stopped statement's job reference arrives within this wait, so its cancel can follow. + public static let serverWaitMilliseconds = 2_000 + + public static let stopGrace: Duration = .seconds(1) + + public static let stoppedRequestLimit: Duration = .seconds(10) + + // The server caps an answer at 10 MB. + public static let answerByteLimit = 32 * 1_024 * 1_024 + + // jobs.query answers a statement the server refuses with these, where jobs.insert reported them on the job. + public static func isStatementRefusal(status: Int) -> Bool { + status == 400 || status == 403 || status == 404 || status == 409 + } +} + +public struct BigQueryStatementRequest: Encodable, Sendable { + public struct Dataset: Encodable, Sendable { + public let projectId: String + public let datasetId: String + + public init(projectId: String, datasetId: String) { + self.projectId = projectId + self.datasetId = datasetId + } + } + + public let query: String + public let useLegacySql: Bool + public let jobCreationMode: String + // A retry with the same id returns the first request's statement instead of running it again. + public let requestId: String + public let maxResults: Int + public let timeoutMs: Int + public let location: String? + public let defaultDataset: Dataset? + public let maximumBytesBilled: String? + public let parameterMode: String? + public let queryParameters: [Parameter]? + public let jobTimeoutMs: String? + + public init( + query: String, + requestId: String, + maxResults: Int, + location: String?, + defaultDataset: Dataset?, + maximumBytesBilled: String?, + parameterMode: String?, + queryParameters: [Parameter]?, + jobTimeoutMs: String? + ) { + self.query = query + self.useLegacySql = false + self.jobCreationMode = BigQueryStatementPolicy.jobCreationMode + self.requestId = requestId + self.maxResults = maxResults + self.timeoutMs = BigQueryStatementPolicy.serverWaitMilliseconds + self.location = location + self.defaultDataset = defaultDataset + self.maximumBytesBilled = maximumBytesBilled + self.parameterMode = parameterMode + self.queryParameters = queryParameters + self.jobTimeoutMs = jobTimeoutMs + } +} + +public enum BigQueryStatementProgress: Equatable, Sendable { + case finished(jobId: String?) + case running(jobId: String) + case unreadable + + // A statement that finished without a job cannot page, so a page token with no job cannot be followed. + public init(jobComplete: Bool?, jobId: String?, pageToken: String?) { + if jobComplete == false { + self = jobId.map { .running(jobId: $0) } ?? .unreadable + } else if jobId == nil, pageToken != nil { + self = .unreadable + } else { + self = .finished(jobId: jobId) + } + } + + public static func jobToCancel(jobComplete: Bool?, jobId: String?) -> String? { + guard case .running(let jobId) = BigQueryStatementProgress(jobComplete: jobComplete, jobId: jobId, pageToken: nil) + else { return nil } + return jobId + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStoppableRequest.swift b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStoppableRequest.swift new file mode 100644 index 0000000000..b2f55f6462 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/BigQuery/BigQueryStoppableRequest.swift @@ -0,0 +1,323 @@ +import Foundation + +struct BigQueryStopLifecycle: Equatable, Sendable { + enum Stage: Equatable, Sendable { + case notStarted + case cancelledBeforeStart + case running(bodySent: Bool) + case stopped(callerReleased: Bool) + case answered + case failed + case answeredAfterStop + case failedAfterStop + } + + enum Event: Equatable, Sendable, CaseIterable { + case start + case attemptStarted + case bodySent + case stop + case answer + case failure + case graceElapsed + case limitElapsed + } + + enum CallerOutcome: Equatable, Sendable { + case answer + case error + case cancelled + } + + enum Effect: Equatable, Sendable { + case launchRequest + case releaseCaller(CallerOutcome) + case armTimer + case cancelTimer + case cancelRequest + case handOverAnswer + } + + private(set) var stage: Stage + + init(stage: Stage = .notStarted) { + self.stage = stage + } + + var isStopped: Bool { + switch stage { + case .notStarted, .running, .answered, .failed: + false + case .cancelledBeforeStart, .stopped, .answeredAfterStop, .failedAfterStop: + true + } + } + + mutating func handle(_ event: Event) -> [Effect] { + switch stage { + case .notStarted: + switch event { + case .start: + stage = .running(bodySent: false) + return [.launchRequest] + case .stop: + stage = .cancelledBeforeStart + return [] + case .attemptStarted, .bodySent, .answer, .failure, .graceElapsed, .limitElapsed: + return [] + } + case .cancelledBeforeStart: + switch event { + case .start: + return [.releaseCaller(.cancelled)] + case .attemptStarted, .bodySent, .stop, .answer, .failure, .graceElapsed, .limitElapsed: + return [] + } + case .running(let bodySent): + switch event { + case .attemptStarted: + stage = .running(bodySent: false) + return [] + case .bodySent: + stage = .running(bodySent: true) + return [] + case .answer: + stage = .answered + return [.releaseCaller(.answer)] + case .failure: + stage = .failed + return [.releaseCaller(.error)] + case .stop: + // Until its body has left, the server cannot run the statement, so Stop ends the request as main did. + stage = .stopped(callerReleased: false) + return bodySent ? [.armTimer] : [.armTimer, .cancelRequest] + case .start, .graceElapsed, .limitElapsed: + return [] + } + case .stopped(let callerReleased): + let release: [Effect] = callerReleased ? [] : [.releaseCaller(.cancelled)] + switch event { + case .answer: + stage = .answeredAfterStop + return release + [.cancelTimer, .handOverAnswer] + case .failure: + stage = .failedAfterStop + return release + [.cancelTimer] + case .graceElapsed: + stage = .stopped(callerReleased: true) + return release + case .limitElapsed: + stage = .stopped(callerReleased: true) + return release + [.cancelRequest] + case .start, .attemptStarted, .bodySent, .stop: + return [] + } + case .answered, .failed, .answeredAfterStop, .failedAfterStop: + return [] + } + } +} + +public struct BigQueryRequestSignals: Sendable { + public let isStopped: @Sendable () -> Bool + // False once Stop has come; otherwise a new attempt starts with its body not yet sent. + public let beginAttempt: @Sendable () -> Bool + // The attempt's whole body has left the client, so the server may run the statement. + public let bodySent: @Sendable () -> Void + + public init( + isStopped: @escaping @Sendable () -> Bool, + beginAttempt: @escaping @Sendable () -> Bool, + bodySent: @escaping @Sendable () -> Void + ) { + self.isStopped = isStopped + self.beginAttempt = beginAttempt + self.bodySent = bodySent + } + + public static let unstoppable = BigQueryRequestSignals(isStopped: { false }, beginAttempt: { true }, bodySent: {}) +} + +public enum BigQueryStoppableRequest { + // Runs `request` apart from the caller, since cancelling a sent request on the wire would lose the job reference its + // answer carries. The caller's cancellation is Stop; an answer that arrives after Stop goes to `afterStop`. + public static func value( + grace: Duration, + limit: Duration, + clock: any Clock = ContinuousClock(), + request: @escaping @Sendable (BigQueryRequestSignals) async throws -> Answer, + afterStop: @escaping @Sendable (Answer) async -> Void + ) async throws -> Answer { + let run = StoppableRun(grace: grace, limit: limit, clock: clock, request: request, afterStop: afterStop) + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { caller in + run.start(caller: caller) + } + } onCancel: { + run.stop() + } + } +} + +private final class StoppableRun: @unchecked Sendable { + typealias Caller = CheckedContinuation + + private struct Followup { + var release: (Caller, Result)? + var cancel: [Task] = [] + var handOver: Answer? + + func perform() { + if case let (caller, result)? = release { + caller.resume(with: result) + } + cancel.forEach { $0.cancel() } + } + } + + private let grace: Duration + private let limit: Duration + private let clock: any Clock + private let request: @Sendable (BigQueryRequestSignals) async throws -> Answer + private let afterStop: @Sendable (Answer) async -> Void + + private let lock = NSLock() + private var lifecycle = BigQueryStopLifecycle() + private var caller: Caller? + private var requestTask: Task? + private var timer: Task? + + init( + grace: Duration, + limit: Duration, + clock: any Clock, + request: @escaping @Sendable (BigQueryRequestSignals) async throws -> Answer, + afterStop: @escaping @Sendable (Answer) async -> Void + ) { + self.grace = grace + self.limit = limit + self.clock = clock + self.request = request + self.afterStop = afterStop + } + + func start(caller waiting: Caller) { + lock.withLock { + caller = waiting + return apply(.start, ended: nil) + }.perform() + } + + func stop() { + lock.withLock { apply(.stop, ended: nil) }.perform() + } + + private var signals: BigQueryRequestSignals { + BigQueryRequestSignals( + isStopped: { [self] in + lock.withLock { lifecycle.isStopped } + }, + beginAttempt: { [self] in + let (followup, admitted) = lock.withLock { + (apply(.attemptStarted, ended: nil), !lifecycle.isStopped) + } + followup.perform() + return admitted + }, + bodySent: { [self] in + lock.withLock { apply(.bodySent, ended: nil) }.perform() + } + ) + } + + private func runRequest() async { + let result: Result + do { + result = .success(try await request(signals)) + } catch { + result = .failure(error) + } + let event: BigQueryStopLifecycle.Event + switch result { + case .success: + event = .answer + case .failure: + event = .failure + } + let followup = lock.withLock { apply(event, ended: result) } + followup.perform() + if let answer = followup.handOver { + await afterStop(answer) + } + } + + private func elapsed(_ event: BigQueryStopLifecycle.Event) { + lock.withLock { apply(event, ended: nil) }.perform() + } + + // Runs under `lock`; what touches the caller or another task waits for `Followup.perform`. + private func apply(_ event: BigQueryStopLifecycle.Event, ended result: Result?) -> Followup { + var followup = Followup() + for effect in lifecycle.handle(event) { + switch effect { + case .launchRequest: + requestTask = Task { await self.runRequest() } + case .releaseCaller(let outcome): + guard let waiting = caller else { continue } + caller = nil + switch outcome { + case .answer, .error: + followup.release = result.map { (waiting, $0) } + case .cancelled: + followup.release = (waiting, .failure(CancellationError())) + } + case .armTimer: + timer = clock.stopTimer(grace: grace, limit: limit) { [self] event in + elapsed(event) + } + case .cancelTimer: + if let timer { + followup.cancel.append(timer) + } + timer = nil + case .cancelRequest: + if let requestTask { + followup.cancel.append(requestTask) + } + case .handOverAnswer: + if case .success(let answer) = result { + followup.handOver = answer + } + } + } + if result != nil { + requestTask = nil + } + return followup + } +} + +fileprivate extension Clock where Duration == Swift.Duration { + // Both deadlines count from Stop, so a timer that resumes late does not push the limit back. + func stopTimer( + grace: Duration, + limit: Duration, + elapsed: @escaping @Sendable (BigQueryStopLifecycle.Event) -> Void + ) -> Task { + let stoppedAt = now + return Task { + do { + try await sleep(until: stoppedAt.advanced(by: grace), tolerance: nil) + } catch { + return + } + elapsed(.graceElapsed) + do { + try await sleep(until: stoppedAt.advanced(by: Swift.max(limit, grace)), tolerance: nil) + } catch { + return + } + elapsed(.limitElapsed) + } + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProGoogleCloud/REST/BoundedHTTPRead.swift b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/REST/BoundedHTTPRead.swift new file mode 100644 index 0000000000..0dfcc020d4 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProGoogleCloud/REST/BoundedHTTPRead.swift @@ -0,0 +1,158 @@ +import Foundation + +// Reads a response body into memory, at most `limit` bytes of it however the server frames the body. +public enum BoundedHTTPRead { + public struct BodyTooLarge: Error, Equatable { + public let limit: Int + + public init(limit: Int) { + self.limit = limit + } + } + + // `onBodySent` runs once, when the whole request body has left the client. + public static func data( + for request: URLRequest, + session: URLSession, + limit: Int, + onBodySent: @escaping @Sendable () -> Void = {} + ) async throws -> (Data, URLResponse) { + let transfer = BoundedTransfer(request: request, session: session, limit: limit, onBodySent: onBodySent) + return try await withTaskCancellationHandler { + try await withCheckedThrowingContinuation { caller in + transfer.start(caller: caller) + } + } onCancel: { + transfer.cancel() + } + } +} + +// A per-task delegate, so the session's own delegate still answers redirects and trust. +private final class BoundedTransfer: NSObject, URLSessionDataDelegate, @unchecked Sendable { + typealias Caller = CheckedContinuation<(Data, URLResponse), any Error> + + private enum Stage { + case notStarted + case cancelledBeforeStart + case running + case finished + } + + private let request: URLRequest + private let session: URLSession + private let limit: Int + private let onBodySent: @Sendable () -> Void + private let lock = NSLock() + private var stage = Stage.notStarted + private var caller: Caller? + private var task: URLSessionDataTask? + private var body = Data() + private var reportedBodySent = false + + init(request: URLRequest, session: URLSession, limit: Int, onBodySent: @escaping @Sendable () -> Void) { + self.request = request + self.session = session + self.limit = max(limit, 0) + self.onBodySent = onBodySent + } + + func start(caller waiting: Caller) { + let created: URLSessionDataTask? = lock.withLock { + switch stage { + case .notStarted: + let dataTask = session.dataTask(with: request) + dataTask.delegate = self + stage = .running + caller = waiting + task = dataTask + return dataTask + case .cancelledBeforeStart: + stage = .finished + return nil + case .running, .finished: + return nil + } + } + guard let created else { + waiting.resume(throwing: URLError(.cancelled)) + return + } + created.resume() + } + + func cancel() { + let ended: (Caller, URLSessionDataTask)? = lock.withLock { + switch stage { + case .notStarted: + stage = .cancelledBeforeStart + return nil + case .running: + return finish() + case .cancelledBeforeStart, .finished: + return nil + } + } + guard case let (waiting, dataTask)? = ended else { return } + dataTask.cancel() + waiting.resume(throwing: URLError(.cancelled)) + } + + func urlSession( + _ session: URLSession, + task: URLSessionTask, + didSendBodyData bytesSent: Int64, + totalBytesSent: Int64, + totalBytesExpectedToSend: Int64 + ) { + let reports: Bool = lock.withLock { + guard stage == .running, !reportedBodySent, + totalBytesExpectedToSend > 0, totalBytesSent >= totalBytesExpectedToSend + else { return false } + reportedBodySent = true + return true + } + if reports { + onBodySent() + } + } + + func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { + let ended: (Caller, URLSessionDataTask)? = lock.withLock { + guard stage == .running else { return nil } + guard data.count <= limit - body.count else { return finish() } + body.append(data) + return nil + } + guard case let (waiting, runningTask)? = ended else { return } + runningTask.cancel() + waiting.resume(throwing: BoundedHTTPRead.BodyTooLarge(limit: limit)) + } + + func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: (any Error)?) { + let ended: (Caller, Result<(Data, URLResponse), any Error>)? = lock.withLock { + guard stage == .running else { return nil } + let received = body + guard case let (waiting, _)? = finish() else { return nil } + if let error { + return (waiting, .failure(error)) + } + guard let response = task.response else { + return (waiting, .failure(URLError(.badServerResponse))) + } + return (waiting, .success((received, response))) + } + guard case let (waiting, result)? = ended else { return } + waiting.resume(with: result) + } + + // Runs under `lock` while running. + private func finish() -> (Caller, URLSessionDataTask)? { + stage = .finished + body = Data() + guard let waiting = caller, let running = task else { return nil } + caller = nil + task = nil + return (waiting, running) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryRequestLoopTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryRequestLoopTests.swift new file mode 100644 index 0000000000..8740786ce6 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryRequestLoopTests.swift @@ -0,0 +1,129 @@ +import Foundation +import TableProGoogleCloud +import Testing + +struct BigQueryRequestLoopTests { + private final class Script: @unchecked Sendable { + var statuses: [Int] + var sent = 0 + var refreshed = 0 + var backoffs: [Int] = [] + var stopped = false + var stopWhileAuthorizing = false + var stopDuringBackoff = false + var stopDuringRefresh = false + var attempts = 0 + + init(_ statuses: [Int]) { + self.statuses = statuses + } + + func run() async throws -> BigQueryRequestLoop.Answer? { + try await BigQueryRequestLoop.run( + signals: BigQueryRequestSignals( + isStopped: { self.stopped }, + beginAttempt: { + guard !self.stopped else { return false } + self.attempts += 1 + return true + }, + bodySent: {} + ), + authorize: { + if stopWhileAuthorizing { stopped = true } + return "token" + }, + send: { _ in + sent += 1 + return BigQueryRequestLoop.Answer(status: statuses.removeFirst(), data: Data()) + }, + refreshToken: { + refreshed += 1 + if stopDuringRefresh { stopped = true } + }, + backoff: { attempt in + backoffs.append(attempt) + if stopDuringBackoff { stopped = true } + } + ) + } + } + + @Test("A request that succeeds is sent once, in one attempt") + func successIsSentOnce() async throws { + let script = Script([200]) + #expect(try await script.run()?.status == 200) + #expect(script.sent == 1) + #expect(script.attempts == 1) + } + + @Test("Stop before the first send sends nothing") + func stopBeforeSendSendsNothing() async throws { + let script = Script([200]) + script.stopped = true + + #expect(try await script.run() == nil) + #expect(script.sent == 0) + } + + @Test("Stop while the token is fetched sends nothing") + func stopWhileAuthorizingSendsNothing() async throws { + let script = Script([200]) + script.stopWhileAuthorizing = true + + #expect(try await script.run() == nil) + #expect(script.sent == 0) + } + + @Test("Stop during a rate limit backoff sends no second request") + func stopDuringBackoffSendsNoRetry() async throws { + let script = Script([429, 200]) + script.stopDuringBackoff = true + + #expect(try await script.run() == nil) + #expect(script.sent == 1) + } + + @Test("A rate limit is waited out with a longer wait each time, each send its own attempt") + func rateLimitRetries() async throws { + let script = Script([429, 429, 200]) + + #expect(try await script.run()?.status == 200) + #expect(script.backoffs == [1, 2]) + #expect(script.attempts == 3) + } + + @Test("A rate limit that never lifts comes back to the caller") + func rateLimitGivesUp() async throws { + let script = Script([429, 429, 429, 429]) + + #expect(try await script.run()?.status == 429) + #expect(script.sent == 4) + } + + @Test("Stop during the token refresh after a 401 sends no second request") + func stopDuringRefreshSendsNoRetry() async throws { + let script = Script([401, 200]) + script.stopDuringRefresh = true + + #expect(try await script.run() == nil) + #expect(script.sent == 1) + } + + @Test("An expired token is refreshed once") + func refreshesTheTokenOnce() async throws { + let script = Script([401, 401]) + + #expect(try await script.run()?.status == 401) + #expect(script.refreshed == 1) + #expect(script.sent == 2) + } + + @Test("Any other answer comes back as it is", arguments: [400, 403, 404, 500]) + func otherStatusesReturn(status: Int) async throws { + let script = Script([status]) + + #expect(try await script.run()?.status == status) + #expect(script.sent == 1) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStatementRequestTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStatementRequestTests.swift new file mode 100644 index 0000000000..aa739fee86 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStatementRequestTests.swift @@ -0,0 +1,116 @@ +import Foundation +import TableProGoogleCloud +import Testing + +struct BigQueryStatementRequestTests { + private struct Parameter: Encodable, Sendable { + let name: String + } + + private func body(_ request: BigQueryStatementRequest) throws -> [String: Any] { + let data = try JSONEncoder().encode(request) + return try #require(try JSONSerialization.jsonObject(with: data) as? [String: Any]) + } + + private func request( + location: String? = nil, + dataset: BigQueryStatementRequest.Dataset? = nil, + bytes: String? = nil, + mode: String? = nil, + parameters: [Parameter]? = nil, + timeout: String? = nil + ) -> BigQueryStatementRequest { + BigQueryStatementRequest( + query: "SELECT 1", + requestId: "request-1", + maxResults: 10_000, + location: location, + defaultDataset: dataset, + maximumBytesBilled: bytes, + parameterMode: mode, + queryParameters: parameters, + jobTimeoutMs: timeout + ) + } + + @Test("A statement asks to skip the job and answers within the server wait") + func sendsTheJoblessMode() throws { + let sent = try body(request()) + + #expect(sent["jobCreationMode"] as? String == "JOB_CREATION_OPTIONAL") + #expect(sent["query"] as? String == "SELECT 1") + #expect(sent["useLegacySql"] as? Bool == false) + #expect(sent["requestId"] as? String == "request-1") + #expect(sent["maxResults"] as? Int == 10_000) + #expect(sent["timeoutMs"] as? Int == 2_000) + } + + @Test("What the connection does not set stays out of the body") + func omitsUnsetFields() throws { + let sent = try body(request()) + + for key in ["location", "defaultDataset", "maximumBytesBilled", "parameterMode", "queryParameters", "jobTimeoutMs"] { + #expect(sent[key] == nil) + } + } + + @Test("Everything jobs.insert carried for a statement is still sent") + func carriesTheStatementSettings() throws { + let sent = try body(request( + location: "EU", + dataset: .init(projectId: "p", datasetId: "d"), + bytes: "1000", + mode: "NAMED", + parameters: [Parameter(name: "id")], + timeout: "30000" + )) + + #expect(sent["location"] as? String == "EU") + #expect((sent["defaultDataset"] as? [String: String]) == ["projectId": "p", "datasetId": "d"]) + #expect(sent["maximumBytesBilled"] as? String == "1000") + #expect(sent["parameterMode"] as? String == "NAMED") + #expect((sent["queryParameters"] as? [[String: String]]) == [["name": "id"]]) + #expect(sent["jobTimeoutMs"] as? String == "30000") + } + + @Test("A finished statement with no job holds all of its rows") + func finishedWithoutJob() { + #expect(BigQueryStatementProgress(jobComplete: true, jobId: nil, pageToken: nil) == .finished(jobId: nil)) + } + + @Test("A finished statement with a job pages through that job") + func finishedWithJob() { + #expect(BigQueryStatementProgress(jobComplete: true, jobId: "j", pageToken: "next") == .finished(jobId: "j")) + } + + @Test("A statement still running is waited on through its job") + func runningWithJob() { + #expect(BigQueryStatementProgress(jobComplete: false, jobId: "j", pageToken: nil) == .running(jobId: "j")) + } + + @Test("An answer that leaves nothing to follow is unreadable", arguments: [ + (false as Bool?, nil as String?, nil as String?), + (true as Bool?, nil as String?, "next" as String?), + (nil as Bool?, nil as String?, "next" as String?) + ]) + func unreadableAnswers(jobComplete: Bool?, jobId: String?, pageToken: String?) { + #expect(BigQueryStatementProgress(jobComplete: jobComplete, jobId: jobId, pageToken: pageToken) == .unreadable) + } + + @Test("Only a statement still running after Stop has a job to cancel") + func jobToCancel() { + #expect(BigQueryStatementProgress.jobToCancel(jobComplete: false, jobId: "j") == "j") + #expect(BigQueryStatementProgress.jobToCancel(jobComplete: true, jobId: "j") == nil) + #expect(BigQueryStatementProgress.jobToCancel(jobComplete: false, jobId: nil) == nil) + } + + @Test("A refusal of the statement reads as a failed statement", arguments: [400, 403, 404, 409]) + func refusals(status: Int) { + #expect(BigQueryStatementPolicy.isStatementRefusal(status: status)) + } + + @Test("Auth, rate limit and server errors stay API errors", arguments: [401, 429, 500, 503]) + func notRefusals(status: Int) { + #expect(!BigQueryStatementPolicy.isStatementRefusal(status: status)) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStopLifecycleTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStopLifecycleTests.swift new file mode 100644 index 0000000000..917ece03c9 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStopLifecycleTests.swift @@ -0,0 +1,132 @@ +import Foundation +@testable import TableProGoogleCloud +import Testing + +struct BigQueryStopLifecycleTests { + typealias Stage = BigQueryStopLifecycle.Stage + typealias Event = BigQueryStopLifecycle.Event + typealias Effect = BigQueryStopLifecycle.Effect + + private struct Transition { + let from: Stage + let event: Event + let next: Stage + let effects: [Effect] + } + + static let stages: [Stage] = [ + .notStarted, + .cancelledBeforeStart, + .running(bodySent: false), + .running(bodySent: true), + .stopped(callerReleased: false), + .stopped(callerReleased: true), + .answered, + .failed, + .answeredAfterStop, + .failedAfterStop + ] + + private static let transitions: [Transition] = [ + Transition(from: .notStarted, event: .start, next: .running(bodySent: false), effects: [.launchRequest]), + Transition(from: .notStarted, event: .stop, next: .cancelledBeforeStart, effects: []), + Transition(from: .cancelledBeforeStart, event: .start, next: .cancelledBeforeStart, effects: [ + .releaseCaller(.cancelled) + ]), + Transition(from: .running(bodySent: false), event: .attemptStarted, next: .running(bodySent: false), effects: []), + Transition(from: .running(bodySent: true), event: .attemptStarted, next: .running(bodySent: false), effects: []), + Transition(from: .running(bodySent: false), event: .bodySent, next: .running(bodySent: true), effects: []), + Transition(from: .running(bodySent: true), event: .bodySent, next: .running(bodySent: true), effects: []), + Transition(from: .running(bodySent: false), event: .answer, next: .answered, effects: [.releaseCaller(.answer)]), + Transition(from: .running(bodySent: true), event: .answer, next: .answered, effects: [.releaseCaller(.answer)]), + Transition(from: .running(bodySent: false), event: .failure, next: .failed, effects: [.releaseCaller(.error)]), + Transition(from: .running(bodySent: true), event: .failure, next: .failed, effects: [.releaseCaller(.error)]), + Transition( + from: .running(bodySent: false), + event: .stop, + next: .stopped(callerReleased: false), + effects: [.armTimer, .cancelRequest] + ), + Transition(from: .running(bodySent: true), event: .stop, next: .stopped(callerReleased: false), effects: [.armTimer]), + Transition(from: .stopped(callerReleased: false), event: .answer, next: .answeredAfterStop, effects: [ + .releaseCaller(.cancelled), .cancelTimer, .handOverAnswer + ]), + Transition(from: .stopped(callerReleased: false), event: .failure, next: .failedAfterStop, effects: [ + .releaseCaller(.cancelled), .cancelTimer + ]), + Transition( + from: .stopped(callerReleased: false), + event: .graceElapsed, + next: .stopped(callerReleased: true), + effects: [.releaseCaller(.cancelled)] + ), + Transition( + from: .stopped(callerReleased: false), + event: .limitElapsed, + next: .stopped(callerReleased: true), + effects: [.releaseCaller(.cancelled), .cancelRequest] + ), + Transition(from: .stopped(callerReleased: true), event: .answer, next: .answeredAfterStop, effects: [ + .cancelTimer, .handOverAnswer + ]), + Transition(from: .stopped(callerReleased: true), event: .failure, next: .failedAfterStop, effects: [ + .cancelTimer + ]), + Transition( + from: .stopped(callerReleased: true), + event: .graceElapsed, + next: .stopped(callerReleased: true), + effects: [] + ), + Transition( + from: .stopped(callerReleased: true), + event: .limitElapsed, + next: .stopped(callerReleased: true), + effects: [.cancelRequest] + ) + ] + + @Test("Each stage moves on each event as the lifecycle table says, and nowhere else", + arguments: BigQueryStopLifecycleTests.stages, BigQueryStopLifecycle.Event.allCases) + func everyStageAndEvent(stage: Stage, event: Event) { + let expected = Self.transitions.first { $0.from == stage && $0.event == event } + var lifecycle = BigQueryStopLifecycle(stage: stage) + + let effects = lifecycle.handle(event) + + #expect(lifecycle.stage == (expected?.next ?? stage)) + #expect(effects == (expected?.effects ?? [])) + } + + @Test("A request reads as stopped from Stop on, and never before", arguments: BigQueryStopLifecycleTests.stages) + func stoppedFromStopOn(stage: Stage) { + let afterStop: [Stage] = [ + .cancelledBeforeStart, + .stopped(callerReleased: false), + .stopped(callerReleased: true), + .answeredAfterStop, + .failedAfterStop + ] + #expect(BigQueryStopLifecycle(stage: stage).isStopped == afterStop.contains(stage)) + } + + @Test("Each caller is released once, whatever order the events come in") + func callerReleasedOnce() { + for order in [ + [Event.start, .answer, .stop, .graceElapsed, .limitElapsed], + [.start, .bodySent, .stop, .graceElapsed, .answer, .limitElapsed], + [.start, .bodySent, .stop, .answer, .graceElapsed, .limitElapsed], + [.start, .bodySent, .stop, .graceElapsed, .limitElapsed, .failure], + [.start, .stop, .failure, .graceElapsed, .limitElapsed], + [.start, .stop, .graceElapsed, .bodySent, .answer], + [.stop, .start, .answer, .graceElapsed] + ] { + var lifecycle = BigQueryStopLifecycle() + let releases = order.flatMap { lifecycle.handle($0) }.filter { + if case .releaseCaller = $0 { return true } + return false + } + #expect(releases.count == 1, "\(order)") + } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStoppableRequestTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStoppableRequestTests.swift new file mode 100644 index 0000000000..b190ef73ce --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BigQueryStoppableRequestTests.swift @@ -0,0 +1,309 @@ +import Foundation +import TableProGoogleCloud +import TableProTestSupport +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct BigQueryStoppableRequestTests { + private struct Refused: Error {} + + private actor Record { + private(set) var launches = 0 + private(set) var handedOver: [Int] = [] + private(set) var requestEnded = false + private(set) var requestCancelled = false + private var signals: BigQueryRequestSignals? + + func launched(_ signals: BigQueryRequestSignals) { + launches += 1 + self.signals = signals + } + + func ended(cancelled: Bool) { + requestEnded = true + requestCancelled = cancelled + } + + func handOver(_ answer: Int) { + handedOver.append(answer) + } + + func isStopped() -> Bool? { + signals?.isStopped() + } + + func beginAttempt() -> Bool? { + signals?.beginAttempt() + } + + func bodySent() { + signals?.bodySent() + } + } + + private struct Fixture { + let gate = AsyncGate() + let clock = ManualClock() + let record = Record() + + // The request starts an attempt, reports its body sent when `sendsBody`, then answers `outcome` once the gate + // opens. The gate does not watch cancellation; the answer does. + func caller( + answering outcome: Result, + sendsBody: Bool = true, + after startGate: AsyncGate? = nil + ) -> Task { + Task { [gate, clock, record] in + await startGate?.wait() + return try await BigQueryStoppableRequest.value( + grace: .seconds(1), + limit: .seconds(10), + clock: clock + ) { signals in + await record.launched(signals) + guard signals.beginAttempt() else { throw CancellationError() } + if sendsBody { signals.bodySent() } + await gate.wait() + let cancelled = Task.isCancelled + await record.ended(cancelled: cancelled) + if cancelled { throw CancellationError() } + return try outcome.get() + } afterStop: { answer in + await record.handOver(answer) + } + } + } + + func waitForSleepers(_ count: Int) async throws { + try await waitUntil { clock.pendingSleeperCount == count } + } + } + + // A request whose wait ends only when its task is cancelled, as a stalled transfer or token fetch does. + private func stalledCaller( + clock: ManualClock, + record: Record, + sendsBody: Bool + ) -> Task { + Task { + try await BigQueryStoppableRequest.value(grace: .seconds(1), limit: .seconds(10), clock: clock) { signals in + guard signals.beginAttempt() else { throw CancellationError() } + if sendsBody { signals.bodySent() } + await record.launched(signals) + do { + try await Task.sleep(for: .seconds(3_600)) + } catch { + await record.ended(cancelled: true) + throw error + } + return 7 + } afterStop: { answer in + await record.handOver(answer) + } + } + } + + @Test("Start runs the request once and its answer reaches the caller") + func startThenAnswer() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .success(7)) + await fixture.gate.arrived(1) + #expect(await fixture.record.launches == 1) + #expect(await fixture.record.isStopped() == false) + + await fixture.gate.open() + + #expect(try await running.value == 7) + #expect(await fixture.record.handedOver.isEmpty) + #expect(fixture.clock.pendingSleeperCount == 0) + } + + @Test("A caller cancelled before the request starts runs nothing") + func cancelledBeforeStart() async throws { + let fixture = Fixture() + let startGate = AsyncGate() + let running = fixture.caller(answering: .success(7), after: startGate) + running.cancel() + await startGate.open() + + await #expect(throws: CancellationError.self) { try await running.value } + #expect(await fixture.record.launches == 0) + #expect(fixture.clock.pendingSleeperCount == 0) + } + + @Test("A failure before Stop reaches the caller") + func failureBeforeStop() async { + let fixture = Fixture() + let running = fixture.caller(answering: .failure(Refused())) + await fixture.gate.open() + + await #expect(throws: Refused.self) { try await running.value } + #expect(await fixture.record.handedOver.isEmpty) + } + + @Test("Stop before the body is sent cancels the request at once, as main did") + func stopBeforeTheBodyIsSent() async throws { + let clock = ManualClock() + let record = Record() + let running = stalledCaller(clock: clock, record: record, sendsBody: false) + try await waitUntil { await record.launches == 1 } + + running.cancel() + + await #expect(throws: CancellationError.self) { try await running.value } + try await waitUntil { await record.requestCancelled } + try await waitUntil { clock.pendingSleeperCount == 0 } + #expect(await record.handedOver.isEmpty) + } + + @Test("A new attempt after a sent one is not sent until it says so, so Stop cancels it at once") + func newAttemptStartsUnsent() async throws { + let clock = ManualClock() + let record = Record() + let running = stalledCaller(clock: clock, record: record, sendsBody: true) + try await waitUntil { await record.launches == 1 } + #expect(await record.beginAttempt() == true) + + running.cancel() + + await #expect(throws: CancellationError.self) { try await running.value } + try await waitUntil { await record.requestCancelled } + } + + @Test("Stop after the body is sent holds the request, arms one timer and refuses a new attempt") + func stopAfterTheBodyIsSent() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .success(7)) + await fixture.gate.arrived(1) + + running.cancel() + try await fixture.waitForSleepers(1) + + #expect(await fixture.record.isStopped() == true) + #expect(await fixture.record.beginAttempt() == false) + #expect(await fixture.record.requestEnded == false) + await fixture.gate.open() + await #expect(throws: CancellationError.self) { try await running.value } + } + + @Test("An answer while the caller is held releases it as cancelled and goes to afterStop") + func answerWhileHeld() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .success(7)) + await fixture.gate.arrived(1) + running.cancel() + try await fixture.waitForSleepers(1) + + await fixture.gate.open() + + await #expect(throws: CancellationError.self) { try await running.value } + try await waitUntil { await fixture.record.handedOver == [7] } + try await fixture.waitForSleepers(0) + #expect(await fixture.record.requestCancelled == false) + } + + @Test("A failure while the caller is held releases it as cancelled and hands nothing over") + func failureWhileHeld() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .failure(Refused())) + await fixture.gate.arrived(1) + running.cancel() + try await fixture.waitForSleepers(1) + + await fixture.gate.open() + + await #expect(throws: CancellationError.self) { try await running.value } + try await fixture.waitForSleepers(0) + #expect(await fixture.record.handedOver.isEmpty) + } + + @Test("The grace releases a caller whose answer stalls, and the request keeps running") + func graceReleasesTheCaller() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .success(7)) + await fixture.gate.arrived(1) + running.cancel() + try await fixture.waitForSleepers(1) + + fixture.clock.advance(by: .seconds(1)) + + await #expect(throws: CancellationError.self) { try await running.value } + try await fixture.waitForSleepers(1) + #expect(await fixture.record.requestEnded == false) + await fixture.gate.open() + try await waitUntil { await fixture.record.requestEnded } + #expect(await fixture.record.requestCancelled == false) + } + + @Test("A late answer goes to afterStop and ends the timer") + func lateAnswer() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .success(7)) + await fixture.gate.arrived(1) + running.cancel() + try await fixture.waitForSleepers(1) + fixture.clock.advance(by: .seconds(1)) + await #expect(throws: CancellationError.self) { try await running.value } + try await fixture.waitForSleepers(1) + + await fixture.gate.open() + + try await waitUntil { await fixture.record.handedOver == [7] } + try await fixture.waitForSleepers(0) + #expect(await fixture.record.requestCancelled == false) + } + + @Test("A late failure hands nothing over and ends the timer") + func lateFailure() async throws { + let fixture = Fixture() + let running = fixture.caller(answering: .failure(Refused())) + await fixture.gate.arrived(1) + running.cancel() + try await fixture.waitForSleepers(1) + fixture.clock.advance(by: .seconds(1)) + await #expect(throws: CancellationError.self) { try await running.value } + try await fixture.waitForSleepers(1) + + await fixture.gate.open() + + try await waitUntil { await fixture.record.requestEnded } + try await fixture.waitForSleepers(0) + #expect(await fixture.record.handedOver.isEmpty) + } + + @Test("The limit cancels a sent request still unanswered") + func limitCancelsTheRequest() async throws { + let clock = ManualClock() + let record = Record() + let running = stalledCaller(clock: clock, record: record, sendsBody: true) + try await waitUntil { await record.launches == 1 } + running.cancel() + try await waitUntil { clock.pendingSleeperCount == 1 } + clock.advance(by: .seconds(1)) + await #expect(throws: CancellationError.self) { try await running.value } + try await waitUntil { clock.pendingSleeperCount == 1 } + #expect(await record.requestEnded == false) + + clock.advance(by: .seconds(9)) + + try await waitUntil { await record.requestCancelled } + try await waitUntil { clock.pendingSleeperCount == 0 } + #expect(await record.handedOver.isEmpty) + } + + @Test("A timer that resumes late still cancels at the limit counted from Stop") + func lateTimerKeepsTheLimit() async throws { + let clock = ManualClock() + let record = Record() + let running = stalledCaller(clock: clock, record: record, sendsBody: true) + try await waitUntil { await record.launches == 1 } + running.cancel() + try await waitUntil { clock.pendingSleeperCount == 1 } + + clock.advance(by: .seconds(10)) + + await #expect(throws: CancellationError.self) { try await running.value } + try await waitUntil { await record.requestCancelled } + try await waitUntil { clock.pendingSleeperCount == 0 } + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BoundedHTTPReadTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BoundedHTTPReadTests.swift new file mode 100644 index 0000000000..b0a1b03501 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/BoundedHTTPReadTests.swift @@ -0,0 +1,243 @@ +import Foundation +import TableProGoogleCloud +import TableProTestSupport +import Testing + +private final class ScriptedHTTPProtocol: URLProtocol, @unchecked Sendable { + override class func canInit(with request: URLRequest) -> Bool { true } + override class func canonicalRequest(for request: URLRequest) -> URLRequest { request } + + override func startLoading() { + guard let script = ScriptedHTTP.script(for: request) else { + client?.urlProtocol(self, didFailWithError: URLError(.unsupportedURL)) + return + } + script.play(to: self) + } + + override func stopLoading() { + ScriptedHTTP.script(for: request)?.stopped() + } +} + +// One host per script, so cases running at once never share one. +private final class ScriptedHTTP: @unchecked Sendable { + enum Ending { + case finish + case fail(URLError.Code) + case hold + } + + private static let registryLock = NSLock() + nonisolated(unsafe) private static var registry: [String: ScriptedHTTP] = [:] + + let session: URLSession + let url: URL + private let status: Int + private let chunks: [Data] + private let ending: Ending + private let lock = NSLock() + private var startCount = 0 + private var stopCount = 0 + + init(status: Int = 200, chunks: [Data] = [], ending: Ending = .finish) throws { + let host = "\(UUID().uuidString.lowercased()).scripted.test" + url = try #require(URL(string: "https://\(host)/q")) + self.status = status + self.chunks = chunks + self.ending = ending + let configuration = URLSessionConfiguration.ephemeral + configuration.protocolClasses = [ScriptedHTTPProtocol.self] + session = URLSession(configuration: configuration) + Self.registryLock.withLock { Self.registry[host] = self } + } + + static func script(for request: URLRequest) -> ScriptedHTTP? { + guard let host = request.url?.host else { return nil } + return registryLock.withLock { registry[host] } + } + + var starts: Int { + lock.withLock { startCount } + } + + var stops: Int { + lock.withLock { stopCount } + } + + func read(limit: Int) async throws -> (Data, URLResponse) { + try await BoundedHTTPRead.data(for: URLRequest(url: url), session: session, limit: limit) + } + + func play(to loader: URLProtocol) { + lock.withLock { startCount += 1 } + guard let response = HTTPURLResponse(url: url, statusCode: status, httpVersion: "HTTP/1.1", headerFields: nil) + else { + loader.client?.urlProtocol(loader, didFailWithError: URLError(.badServerResponse)) + return + } + loader.client?.urlProtocol(loader, didReceive: response, cacheStoragePolicy: .notAllowed) + for chunk in chunks { + loader.client?.urlProtocol(loader, didLoad: chunk) + } + switch ending { + case .finish: + loader.client?.urlProtocolDidFinishLoading(loader) + case .fail(let code): + loader.client?.urlProtocol(loader, didFailWithError: URLError(code)) + case .hold: + break + } + } + + func stopped() { + lock.withLock { stopCount += 1 } + } + + func waitUntilNoTaskIsLeft() async throws { + try await waitUntil { await session.allTasks.isEmpty } + } +} + +private final class SendCount: @unchecked Sendable { + private let lock = NSLock() + private var count = 0 + + var value: Int { + lock.withLock { count } + } + + func add() { + lock.withLock { count += 1 } + } +} + +private final class RedirectRefusingDelegate: NSObject, URLSessionTaskDelegate, Sendable { + func urlSession( + _ session: URLSession, + task: URLSessionTask, + willPerformHTTPRedirection response: HTTPURLResponse, + newRequest request: URLRequest, + completionHandler: @escaping @Sendable (URLRequest?) -> Void + ) { + completionHandler(nil) + } +} + +@Suite(.timeLimit(.minutes(1))) +struct BoundedHTTPReadTests { + private static let kibibyte = 1_024 + + @Test("A body within the limit is read whole, with its status") + func bodyWithinTheLimit() async throws { + let script = try ScriptedHTTP(status: 400, chunks: [Data("{\"ok\":".utf8), Data("true}".utf8)]) + + let (data, response) = try await script.read(limit: 11) + + #expect(data == Data("{\"ok\":true}".utf8)) + #expect((response as? HTTPURLResponse)?.statusCode == 400) + try await script.waitUntilNoTaskIsLeft() + } + + @Test("Bytes past the limit end the read while the server still sends, and cancel the transfer") + func bodyPastTheLimit() async throws { + let chunk = Data(repeating: 0x78, count: 64 * Self.kibibyte) + let script = try ScriptedHTTP(chunks: [chunk, chunk, chunk], ending: .hold) + + await #expect(throws: BoundedHTTPRead.BodyTooLarge(limit: 100 * Self.kibibyte)) { + _ = try await script.read(limit: 100 * Self.kibibyte) + } + try await waitUntil { script.stops == 1 } + try await script.waitUntilNoTaskIsLeft() + } + + @Test("A caller cancelled before the read starts sends nothing and leaves no task") + func cancelledBeforeStart() async throws { + let script = try ScriptedHTTP(chunks: [Data("{}".utf8)]) + let gate = AsyncGate() + let read = Task { + await gate.wait() + return try await script.read(limit: Self.kibibyte) + } + read.cancel() + await gate.open() + + let error = await #expect(throws: URLError.self) { _ = try await read.value } + #expect(error?.code == .cancelled) + #expect(script.starts == 0) + #expect(await script.session.allTasks.isEmpty) + } + + @Test("Cancelling a running read ends it at once and cancels the transfer") + func cancelledWhileRunning() async throws { + let script = try ScriptedHTTP(ending: .hold) + let read = Task { try await script.read(limit: Self.kibibyte) } + try await waitUntil { script.starts == 1 } + + read.cancel() + + let error = await #expect(throws: URLError.self) { _ = try await read.value } + #expect(error?.code == .cancelled) + try await waitUntil { script.stops == 1 } + try await script.waitUntilNoTaskIsLeft() + } + + @Test("A transfer that fails ends the read with its error") + func transferFails() async throws { + let script = try ScriptedHTTP(chunks: [Data("{".utf8)], ending: .fail(.networkConnectionLost)) + + let error = await #expect(throws: URLError.self) { _ = try await script.read(limit: Self.kibibyte) } + + #expect(error?.code == .networkConnectionLost) + } + + @Test("A body the server takes whole is reported sent once") + func bodyReportedSentOnce() async throws { + let responder = try LoopbackHTTPResponder { _ in LoopbackResponse(status: 200, body: "{}") } + try await responder.start() + defer { responder.stop() } + let session = URLSession(configuration: .ephemeral, delegate: RedirectRefusingDelegate(), delegateQueue: nil) + defer { session.invalidateAndCancel() } + var request = URLRequest(url: responder.url("/q")) + request.httpMethod = "POST" + request.httpBody = Data(#"{"query":"SELECT 1"}"#.utf8) + let sent = SendCount() + + let (data, _) = try await BoundedHTTPRead.data( + for: request, + session: session, + limit: Self.kibibyte, + onBodySent: { sent.add() } + ) + + #expect(data == Data("{}".utf8)) + #expect(sent.value == 1) + } + + @Test("The session's own delegate still refuses a redirect") + func sessionDelegateRefusesRedirects() async throws { + let responder = try LoopbackHTTPResponder { request in + request.path == "/q" + ? LoopbackResponse(status: 302, headers: ["Location": "/elsewhere"]) + : LoopbackResponse(status: 200, body: "followed") + } + try await responder.start() + defer { responder.stop() } + let session = URLSession( + configuration: .ephemeral, + delegate: RedirectRefusingDelegate(), + delegateQueue: nil + ) + defer { session.invalidateAndCancel() } + + let (data, response) = try await BoundedHTTPRead.data( + for: URLRequest(url: responder.url("/q")), + session: session, + limit: Self.kibibyte + ) + + #expect((response as? HTTPURLResponse)?.statusCode == 302) + #expect(data.isEmpty) + #expect(responder.requests.map(\.path) == ["/q"]) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/GoogleAccessTokenWaitTests.swift b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/GoogleAccessTokenWaitTests.swift new file mode 100644 index 0000000000..1e8c0884c3 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProGoogleCloudTests/GoogleAccessTokenWaitTests.swift @@ -0,0 +1,81 @@ +import Foundation +import TableProGoogleCloud +import TableProTestSupport +import Testing + +@Suite(.timeLimit(.minutes(1))) +struct GoogleAccessTokenWaitTests { + private struct Refused: Error {} + + private actor GatedTokens: GoogleAccessTokenProviding { + private let gate: AsyncGate + private let outcome: Result + private(set) var asked = 0 + private(set) var answered = false + + init(gate: AsyncGate, outcome: Result) { + self.gate = gate + self.outcome = outcome + } + + // Waits like a shared refresh does: cancelling the caller does not end it. + func accessToken() async throws -> String { + asked += 1 + await gate.wait() + answered = true + return try outcome.get() + } + + func invalidateCachedToken() async {} + } + + @Test("The caller gets the token") + func tokenReachesTheCaller() async throws { + let gate = AsyncGate() + let tokens = GatedTokens(gate: gate, outcome: .success("token")) + await gate.open() + + #expect(try await GoogleAccessTokenWait.token(from: tokens) == "token") + #expect(await tokens.asked == 1) + } + + @Test("A failed fetch reaches the caller") + func failureReachesTheCaller() async { + let gate = AsyncGate() + let tokens = GatedTokens(gate: gate, outcome: .failure(Refused())) + await gate.open() + + await #expect(throws: Refused.self) { _ = try await GoogleAccessTokenWait.token(from: tokens) } + } + + @Test("A caller cancelled before the wait asks for nothing") + func cancelledBeforeStart() async { + let gate = AsyncGate() + let tokens = GatedTokens(gate: gate, outcome: .success("token")) + let startGate = AsyncGate() + let waiting = Task { + await startGate.wait() + return try await GoogleAccessTokenWait.token(from: tokens) + } + waiting.cancel() + await startGate.open() + + await #expect(throws: CancellationError.self) { _ = try await waiting.value } + #expect(await tokens.asked == 0) + } + + @Test("A cancelled caller stops waiting while the fetch runs on") + func cancelledWhileWaiting() async throws { + let gate = AsyncGate() + let tokens = GatedTokens(gate: gate, outcome: .success("token")) + let waiting = Task { try await GoogleAccessTokenWait.token(from: tokens) } + await gate.arrived(1) + + waiting.cancel() + + await #expect(throws: CancellationError.self) { _ = try await waiting.value } + #expect(await tokens.answered == false) + await gate.open() + try await waitUntil { await tokens.answered } + } +} diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryConnection.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryConnection.swift index bca253bd9e..308b42edae 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryConnection.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryConnection.swift @@ -206,6 +206,27 @@ internal struct BQQueryResponse: Codable, Sendable { let jobComplete: Bool? let jobReference: BQJobResponse.BQJobReference? let numDmlAffectedRows: String? + var totalBytesProcessed: String? + var totalBytesBilled: String? + var cacheHit: Bool? + var startTime: String? + var endTime: String? + + // jobs.query reports what jobs.get reported for a finished job on the answer itself. + var statistics: BQJobResponse.BQJobStatistics { + BQJobResponse.BQJobStatistics( + totalBytesProcessed: totalBytesProcessed, + query: BQJobResponse.BQQueryStatistics( + totalBytesProcessed: totalBytesProcessed, + totalBytesBilled: totalBytesBilled, + cacheHit: cacheHit, + numDmlAffectedRows: numDmlAffectedRows, + undeclaredQueryParameters: nil + ), + startTime: startTime, + endTime: endTime + ) + } struct BQRow: Codable, Sendable { let f: [BQCell]? @@ -262,10 +283,12 @@ internal enum BQCellValue: Codable, Sendable { } } -internal struct BQJobInfo: Sendable { - let jobId: String +// A finished statement: its first page, and the job later pages come from when the server made one. +internal struct BQStatementRun: Sendable { + let firstPage: BQQueryResponse + let jobId: String? let location: String? - var serverElapsed: TimeInterval? + let statistics: BQJobResponse.BQJobStatistics? } internal struct BQExecuteResult: Sendable { @@ -358,12 +381,10 @@ internal final class BigQueryConnection: @unchecked Sendable { private static let logger = Logger(subsystem: "com.TablePro", category: "BigQueryConnection") private static let host = "bigquery.googleapis.com" private static let basePath = "/bigquery/v2/" - private static let pageSize = "10000" + private static let pageRowLimit = 10_000 + private static let pageSize = String(pageRowLimit) private static let listPageSize = "1000" private static let maximumPages = 100 - private static let rateLimitRetries = 3 - private static let rateLimitedStatus = 429 - private static let unauthorizedStatus = 401 private static let namedParameterMode = "NAMED" private static let pathSegmentAllowed: CharacterSet = { var allowed = CharacterSet.urlPathAllowed @@ -441,26 +462,21 @@ internal final class BigQueryConnection: @unchecked Sendable { defaultDataset: String? = nil, queryParameters: [BigQueryQueryParameter]? = nil ) async throws -> BQExecuteResult { - let job = try await runJob(sql, defaultDataset: defaultDataset, queryParameters: queryParameters) - guard let jobId = job.jobReference?.jobId else { - throw BigQueryError.invalidResponse - } - let jobLocation = job.jobReference?.location - - let firstPage = try await getQueryResults(jobId: jobId, location: jobLocation) + let run = try await runStatement(sql, defaultDataset: defaultDataset, queryParameters: queryParameters) + let firstPage = run.firstPage var allRows = firstPage.rows ?? [] var currentPage = firstPage var pagesFetched = 1 while let nextToken = currentPage.pageToken, pagesFetched < Self.maximumPages { try Task.checkCancellation() - let nextPage = try await getQueryResults(jobId: jobId, location: jobLocation, pageToken: nextToken) + let nextPage = try await nextPage(of: run, pageToken: nextToken) allRows.append(contentsOf: nextPage.rows ?? []) currentPage = nextPage pagesFetched += 1 } - let statistics = job.statistics + let statistics = run.statistics return BQExecuteResult( queryResponse: BQQueryResponse( schema: firstPage.schema, @@ -479,20 +495,72 @@ internal final class BigQueryConnection: @unchecked Sendable { ) } - func executeJobAndWait( + func runStatement( _ sql: String, defaultDataset: String? = nil, queryParameters: [BigQueryQueryParameter]? = nil - ) async throws -> BQJobInfo { - let job = try await runJob(sql, defaultDataset: defaultDataset, queryParameters: queryParameters) - guard let jobId = job.jobReference?.jobId else { + ) async throws -> BQStatementRun { + let timeoutSeconds = lock.withLock { + connectDeadline.map { max(1, Int($0.remainingSeconds().rounded(.up))) } ?? queryTimeoutSeconds + } + let hasParameters = queryParameters?.isEmpty == false + let started = Date() + let answer = try await submit(BigQueryStatementRequest( + query: sql, + requestId: UUID().uuidString, + maxResults: Self.pageRowLimit, + location: location, + defaultDataset: defaultDataset.flatMap { dataset in + dataset.isEmpty ? nil : .init(projectId: projectId, datasetId: dataset) + }, + maximumBytesBilled: maximumBytesBilled, + parameterMode: hasParameters ? Self.namedParameterMode : nil, + queryParameters: hasParameters ? queryParameters : nil, + jobTimeoutMs: BigQueryJobPolling.jobTimeoutMilliseconds(queryTimeoutSeconds: timeoutSeconds) + )) + let jobLocation = answer.jobReference?.location + + switch BigQueryStatementProgress( + jobComplete: answer.jobComplete, + jobId: answer.jobReference?.jobId, + pageToken: answer.pageToken + ) { + case .unreadable: throw BigQueryError.invalidResponse + case .finished(let jobId): + return BQStatementRun(firstPage: answer, jobId: jobId, location: jobLocation, statistics: answer.statistics) + case .running(let jobId): + do { + let finished = try await waitForCompletion( + jobId: jobId, + location: jobLocation, + deadline: BigQueryJobPolling.deadline(queryTimeoutSeconds: timeoutSeconds, from: started), + timeoutSeconds: timeoutSeconds + ) + if let failure = BigQueryJobPolling.failure(of: finished) { + throw failure + } + let firstPage = try await getQueryResults(jobId: jobId, location: jobLocation) + return BQStatementRun( + firstPage: firstPage, + jobId: jobId, + location: jobLocation, + statistics: finished.statistics + ) + } catch let error as BigQueryError { + if case .cancelled = error { + cancelInBackground(jobId: jobId, location: jobLocation) + } + throw error + } } - return BQJobInfo( - jobId: jobId, - location: job.jobReference?.location, - serverElapsed: job.statistics?.elapsed - ) + } + + func nextPage(of run: BQStatementRun, pageToken: String) async throws -> BQQueryResponse { + guard let jobId = run.jobId else { + throw BigQueryError.invalidResponse + } + return try await getQueryResults(jobId: jobId, location: run.location, pageToken: pageToken) } func dryRunQuery(_ sql: String, defaultDataset: String? = nil) async throws -> BQExecuteResult { @@ -590,74 +658,53 @@ internal final class BigQueryConnection: @unchecked Sendable { _ = try await send(request) } - private func runJob( - _ sql: String, - defaultDataset: String?, - queryParameters: [BigQueryQueryParameter]? - ) async throws -> BQJobResponse { - let timeoutSeconds = lock.withLock { - connectDeadline.map { max(1, Int($0.remainingSeconds().rounded(.up))) } ?? queryTimeoutSeconds - } - let hasParameters = queryParameters?.isEmpty == false - let request = BQJobRequest( - jobReference: jobReference(), - configuration: BQJobRequest.BQJobConfiguration( - query: queryConfig( - sql, - defaultDataset: defaultDataset, - parameterMode: hasParameters ? Self.namedParameterMode : nil, - queryParameters: hasParameters ? queryParameters : nil - ), - dryRun: nil, - jobTimeoutMs: BigQueryJobPolling.jobTimeoutMilliseconds(queryTimeoutSeconds: timeoutSeconds) - ) - ) - let started = Date() - let inserted = try await insertJob(request) - guard let jobId = inserted.jobReference?.jobId else { - throw BigQueryError.invalidResponse - } - let jobLocation = inserted.jobReference?.location - + private func submit( + _ statement: BigQueryStatementRequest + ) async throws -> BQQueryResponse { + var request = URLRequest(url: try endpoint(["projects", projectId, "queries"])) + request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + request.httpBody = try JSONEncoder().encode(statement) do { - let finished = try await waitForCompletion( - of: inserted, - jobId: jobId, - location: jobLocation, - deadline: BigQueryJobPolling.deadline(queryTimeoutSeconds: timeoutSeconds, from: started), - timeoutSeconds: timeoutSeconds - ) - if let failure = BigQueryJobPolling.failure(of: finished) { - throw failure + return try await BigQueryStoppableRequest.value( + grace: BigQueryStatementPolicy.stopGrace, + limit: BigQueryStatementPolicy.stoppedRequestLimit + ) { [self, request] signals in + try await decode(send(request, signals: signals, bodyLimit: BigQueryStatementPolicy.answerByteLimit)) + } afterStop: { [self] answer in + guard let jobId = BigQueryStatementProgress.jobToCancel( + jobComplete: answer.jobComplete, + jobId: answer.jobReference?.jobId + ) else { return } + cancelInBackground(jobId: jobId, location: answer.jobReference?.location) } - return finished - } catch let error as BigQueryError { - if case .cancelled = error { - cancelInBackground(jobId: jobId, location: jobLocation) - } - throw error + } catch is CancellationError { + throw BigQueryError.cancelled + } catch BigQueryError.api(let status, let message, let reason) + where BigQueryStatementPolicy.isStatementRefusal(status: status) { + throw BigQueryError.jobFailed(message: message, reason: reason) } } private func waitForCompletion( - of job: BQJobResponse, jobId: String, location: String?, deadline: Date?, timeoutSeconds: Int ) async throws -> BQJobResponse { - var current = job var attempt = 0 - while current.status?.state != BigQueryJobPolling.doneState { + while true { if let deadline, Date() >= deadline { cancelInBackground(jobId: jobId, location: location) throw BigQueryError.jobTimedOut(seconds: timeoutSeconds) } try await sleep(nanoseconds: BigQueryJobPolling.backoffNanoseconds(attempt: attempt)) attempt += 1 - current = try await getJob(jobId: jobId, location: location) + let current = try await getJob(jobId: jobId, location: location) + if current.status?.state == BigQueryJobPolling.doneState { + return current + } } - return current } private func dryRun( @@ -759,45 +806,63 @@ internal final class BigQueryConnection: @unchecked Sendable { return session } - private func send(_ request: URLRequest) async throws -> Data { - var rateLimitAttempt = 0 - var refreshedToken = false - while true { - let (data, response) = try await perform(request) - let status = response.statusCode - if (200..<300).contains(status) { - return data - } - if status == Self.unauthorizedStatus, !refreshedToken { - refreshedToken = true - await tokenProvider.invalidateCachedToken() - continue - } - if status == Self.rateLimitedStatus, rateLimitAttempt < Self.rateLimitRetries { - rateLimitAttempt += 1 - Self.logger.info("BigQuery rate limited, retry \(rateLimitAttempt, privacy: .public)") - try await sleep(nanoseconds: BigQueryJobPolling.backoffNanoseconds(attempt: rateLimitAttempt)) - continue + private func send( + _ request: URLRequest, + signals: BigQueryRequestSignals = .unstoppable, + bodyLimit: Int? = nil + ) async throws -> Data { + let answer = try await BigQueryRequestLoop.run( + signals: signals, + authorize: { try await accessToken() }, + send: { token in + try await perform(request, token: token, bodyLimit: bodyLimit, onBodySent: signals.bodySent) + }, + refreshToken: { await tokenProvider.invalidateCachedToken() }, + backoff: { attempt in + Self.logger.info("BigQuery rate limited, retry \(attempt, privacy: .public)") + try await sleep(nanoseconds: BigQueryJobPolling.backoffNanoseconds(attempt: attempt)) } - throw BQErrorResponse.apiError(status: status, data: data) + ) + guard let answer else { + throw BigQueryError.cancelled } + guard (200..<300).contains(answer.status) else { + throw BQErrorResponse.apiError(status: answer.status, data: answer.data) + } + return answer.data } - private func perform(_ request: URLRequest) async throws -> (Data, HTTPURLResponse) { + private func perform( + _ request: URLRequest, + token: String, + bodyLimit: Int?, + onBodySent: @escaping @Sendable () -> Void + ) async throws -> BigQueryRequestLoop.Answer { let session = try currentSession() - let token = try await accessToken() var authorized = request authorized.timeoutInterval = lock.withLock { connectDeadline }?.remainingSeconds() ?? requestTimeout.requestTimeoutInterval authorized.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") do { - let (data, response) = try await session.data(for: authorized) + let (data, response): (Data, URLResponse) + if let bodyLimit { + (data, response) = try await BoundedHTTPRead.data( + for: authorized, + session: session, + limit: bodyLimit, + onBodySent: onBodySent + ) + } else { + (data, response) = try await session.data(for: authorized) + } guard let httpResponse = response as? HTTPURLResponse else { throw BigQueryError.invalidResponse } - return (data, httpResponse) + return BigQueryRequestLoop.Answer(status: httpResponse.statusCode, data: data) } catch let error as BigQueryError { throw error + } catch is BoundedHTTPRead.BodyTooLarge { + throw BigQueryError.invalidResponse } catch { throw BigQueryError.wrap(error) } @@ -805,7 +870,7 @@ internal final class BigQueryConnection: @unchecked Sendable { private func accessToken() async throws -> String { do { - return try await tokenProvider.accessToken() + return try await GoogleAccessTokenWait.token(from: tokenProvider) } catch { throw BigQueryError.wrap(error) } diff --git a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift index 64979b93ea..589fbc9853 100644 --- a/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift +++ b/apple/Plugins/BigQueryDriverPlugin/BigQueryPluginDriver+Execution.swift @@ -236,10 +236,10 @@ extension BigQueryPluginDriver { sql = trimmed.replacingOccurrences(of: ";\\s*\\z", with: "", options: .regularExpression) } - let jobInfo = try await conn.executeJobAndWait(sql, defaultDataset: currentSchema) - lastJobElapsed = jobInfo.serverElapsed + let run = try await conn.runStatement(sql, defaultDataset: currentSchema) + lastJobElapsed = run.statistics?.elapsed - let firstPage = try await conn.getQueryResults(jobId: jobInfo.jobId, location: jobInfo.location) + let firstPage = run.firstPage guard let schema = firstPage.schema, let fields = schema.fields, !fields.isEmpty else { continuation.yield(.header(PluginStreamHeader( columns: ["Result"], @@ -264,11 +264,7 @@ extension BigQueryPluginDriver { var pageToken = firstPage.pageToken while let token = pageToken { try Task.checkCancellation() - let nextPage = try await conn.getQueryResults( - jobId: jobInfo.jobId, - location: jobInfo.location, - pageToken: token - ) + let nextPage = try await conn.nextPage(of: run, pageToken: token) let nextRows = BigQueryTypeMapper.flattenRows(from: nextPage, schema: schema) if !nextRows.isEmpty { _ = await PluginRowStream.deliver(.rows(nextRows), to: continuation) diff --git a/apple/changelog.d/4352.changed.md b/apple/changelog.d/4352.changed.md new file mode 100644 index 0000000000..ec6c55d52b --- /dev/null +++ b/apple/changelog.d/4352.changed.md @@ -0,0 +1 @@ +BigQuery statements that finish within two seconds return in one request, without the half-second wait each one had.