From d0fa4961c5ed3309f87979fccc697063618b2d66 Mon Sep 17 00:00:00 2001 From: Kimlimp Date: Fri, 6 Nov 2026 15:11:10 +0100 Subject: [PATCH] feat(recorder): coalesce active recording disk inspection and cache live stats - Add LiveStatsCoordinator actor with single-flight coalescing and configurable freshness TTL (default 2.0s) - Offload blocking FileManager stats to a dedicated I/O executor so Hummingbird / cooperative threads stay responsive - Invalidate and clean up live cache on retry, stop, delete, and finalization - Ensure finished recordings continue to use exact cached stats from store without live disk inspection - Add full test coverage in LiveStatsTests verifying concurrent poll coalescing, TTL caching, lifecycle invalidation, racing deletion, and thousands-of-segments benchmark --- Sources/RecorderCore/HTTP/Routes.swift | 38 +- Sources/RecorderCore/RecorderConfig.swift | 9 +- Sources/RecorderCore/RecorderServices.swift | 17 +- .../Recording/LiveStatsCoordinator.swift | 161 +++++++++ .../Recording/RecordingScheduler.swift | 16 + .../lume-recorder/LumeRecorderCommand.swift | 2 + Tests/RecorderCoreTests/LiveStatsTests.swift | 337 ++++++++++++++++++ 7 files changed, 573 insertions(+), 7 deletions(-) create mode 100644 Sources/RecorderCore/Recording/LiveStatsCoordinator.swift create mode 100644 Tests/RecorderCoreTests/LiveStatsTests.swift diff --git a/Sources/RecorderCore/HTTP/Routes.swift b/Sources/RecorderCore/HTTP/Routes.swift index d882d6a..817749f 100644 --- a/Sources/RecorderCore/HTTP/Routes.swift +++ b/Sources/RecorderCore/HTTP/Routes.swift @@ -63,18 +63,18 @@ public enum RecorderRoutes { let authed = api.group().add(middleware: BearerAuthMiddleware(services: services)) authed.get("recordings") { _, _ in let records = await services.store.recordings() - return try JSONResponse.make(records.map { publicRecording($0, services: services) }) + return try await JSONResponse.make(publicRecordings(records, services: services)) } authed.post("recordings") { request, context in let body = try await request.decode(as: CreateRecordingRequest.self, context: context) let key = request.headers[idempotencyHeader] let (record, created) = try await services.scheduler.create(body, deviceID: context.device?.id, idempotencyKey: key) - return try JSONResponse.make(publicRecording(record, services: services), status: created ? .created : .ok) + return try await JSONResponse.make(publicRecording(record, services: services), status: created ? .created : .ok) } authed.post("recordings/:id/stop") { _, context in let id = try recordingID(context) let record = try await services.scheduler.stop(id) - return try JSONResponse.make(publicRecording(record, services: services)) + return try await JSONResponse.make(publicRecording(record, services: services)) } authed.delete("recordings/:id") { _, context in try await services.scheduler.delete(recordingID(context)) @@ -213,12 +213,40 @@ public enum RecorderRoutes { return id } - /// Live recordings report stats read from disk; finished ones use the cache. - static func publicRecording(_ record: RecordingRecord, services: RecorderServices) -> Recording { + /// Live recordings report stats read from disk via the coalescing live stats coordinator; + /// finished ones use the cached statistics in the store. + static func publicRecording(_ record: RecordingRecord, services: RecorderServices) async -> Recording { + guard record.status == .recording else { return record.publicRecording() } + let stats = await services.liveStats.stats(for: record.id, in: services.store.mediaDirectory(for: record.id)) + return record.publicRecording(media: stats) + } + + /// Synchronous fallback when direct inspection is needed without coordination. + static func publicRecordingSync(_ record: RecordingRecord, services: RecorderServices) -> Recording { guard record.status == .recording else { return record.publicRecording() } return record.publicRecording(media: MediaInspector.stats(in: services.store.mediaDirectory(for: record.id))) } + /// Resolves all records into public DTOs, coalescing live media stats queries concurrently. + static func publicRecordings(_ records: [RecordingRecord], services: RecorderServices) async -> [Recording] { + guard records.contains(where: { $0.status == .recording }) else { + return records.map { $0.publicRecording() } + } + return await withTaskGroup(of: (Int, Recording).self) { group in + for (index, record) in records.enumerated() { + group.addTask { + let pub = await publicRecording(record, services: services) + return (index, pub) + } + } + var output = [Recording?](repeating: nil, count: records.count) + for await (index, pub) in group { + output[index] = pub + } + return output.compactMap { $0 } + } + } + /// `scheme://host[:port]` the client used, so the link is reachable from /// where the request came from. Falls back to `PUBLIC_URL`, then localhost. static func baseURL(for request: Request, config: RecorderConfig) -> String { diff --git a/Sources/RecorderCore/RecorderConfig.swift b/Sources/RecorderCore/RecorderConfig.swift index f633a2a..e986132 100644 --- a/Sources/RecorderCore/RecorderConfig.swift +++ b/Sources/RecorderCore/RecorderConfig.swift @@ -37,6 +37,8 @@ public struct RecorderConfig: Sendable { public var singleFileRecordings: Bool /// Lifetime of playback grants. public var playbackGrantLifetime: TimeInterval = 12 * 3600 + /// `LIVE_STATS_FRESHNESS` — maximum age of cached stats for active recordings in seconds. + public var liveStatsFreshness: TimeInterval = 2.0 public init( host: String = "0.0.0.0", @@ -49,7 +51,8 @@ public struct RecorderConfig: Sendable { ffmpegPath: String = "ffmpeg", logLevel: Logger.Level = .info, bonjourEnabled: Bool = true, - singleFileRecordings: Bool = false + singleFileRecordings: Bool = false, + liveStatsFreshness: TimeInterval = 2.0 ) { self.host = host self.port = port @@ -62,6 +65,7 @@ public struct RecorderConfig: Sendable { self.logLevel = logLevel self.bonjourEnabled = bonjourEnabled self.singleFileRecordings = singleFileRecordings + self.liveStatsFreshness = liveStatsFreshness } /// Builds a config from environment variables, falling back to defaults. @@ -84,6 +88,9 @@ public struct RecorderConfig: Sendable { if let single = value("SINGLE_FILE_RECORDINGS") { config.singleFileRecordings = ["1", "true", "yes", "on"].contains(single.lowercased()) } + if let liveFreshness = value("LIVE_STATS_FRESHNESS").flatMap(Double.init) { + config.liveStatsFreshness = liveFreshness + } return config } diff --git a/Sources/RecorderCore/RecorderServices.swift b/Sources/RecorderCore/RecorderServices.swift index aa71b23..18db465 100644 --- a/Sources/RecorderCore/RecorderServices.swift +++ b/Sources/RecorderCore/RecorderServices.swift @@ -9,6 +9,7 @@ public struct RecorderServices: Sendable { public let scheduler: RecordingScheduler public let pairing: PairingManager public let signer: PlaybackSigner + public let liveStats: LiveStatsCoordinator public let now: NowProvider public let logger: Logger @@ -16,6 +17,7 @@ public struct RecorderServices: Sendable { config: RecorderConfig, launcher: any RecordingProcessLauncher, disk: any DiskSpaceProviding = FileSystemDiskSpace(), + liveStats: LiveStatsCoordinator? = nil, now: @escaping NowProvider = { Date() }, logger: Logger = Logger(label: "lume-recorder"), configureScheduler: (inout RecordingScheduler.Options) -> Void = { _ in } @@ -29,10 +31,23 @@ public struct RecorderServices: Sendable { throw RecorderError.invalidRequest("server.json holds an invalid playback secret") } self.signer = signer + let statsCoordinator = liveStats ?? LiveStatsCoordinator( + freshness: config.liveStatsFreshness, + now: now + ) + self.liveStats = statsCoordinator var options = RecordingScheduler.Options(maxConcurrent: config.maxConcurrent, minFreeBytes: config.minFreeBytes) options.singleFileRecordings = config.singleFileRecordings configureScheduler(&options) - scheduler = RecordingScheduler(store: store, launcher: launcher, options: options, disk: disk, now: now, logger: logger) + scheduler = RecordingScheduler( + store: store, + launcher: launcher, + options: options, + disk: disk, + liveStats: statsCoordinator, + now: now, + logger: logger + ) pairing = PairingManager(directory: config.dataDirectory, now: now, logger: logger) } diff --git a/Sources/RecorderCore/Recording/LiveStatsCoordinator.swift b/Sources/RecorderCore/Recording/LiveStatsCoordinator.swift new file mode 100644 index 0000000..26222b2 --- /dev/null +++ b/Sources/RecorderCore/Recording/LiveStatsCoordinator.swift @@ -0,0 +1,161 @@ +import Foundation + +/// Duration and size of a recording's media on disk. +/// Coordinates disk inspection of active recordings to: +/// 1. Coalesce concurrent requests into a single-flight scan. +/// 2. Cache stats for an explicit short freshness bound (avoiding repetitive full directory scans). +/// 3. Offload blocking disk I/O onto a dedicated executor so Hummingbird and cooperative threads remain responsive. +/// 4. Invalidate / update on append, retry, stop, delete, and finalization. +public actor LiveStatsCoordinator { + public typealias Inspector = @Sendable (URL) -> MediaStats + + public struct Options: Sendable { + public var freshness: TimeInterval + + public init(freshness: TimeInterval = 2.0) { + self.freshness = freshness + } + } + + private struct Entry: Sendable { + var stats: MediaStats + var recordedAt: Date + } + + private struct InFlightScan { + let scanID: UUID + let task: Task + } + + private var cache: [UUID: Entry] = [:] + private var inFlight: [UUID: InFlightScan] = [:] + private var generations: [UUID: Int] = [:] + + public let freshness: TimeInterval + private let now: NowProvider + private let ioQueue: DispatchQueue + private let inspector: Inspector + + public private(set) var totalScans: Int = 0 + public private(set) var scansByID: [UUID: Int] = [:] + + public init( + freshness: TimeInterval = 2.0, + now: @escaping NowProvider = { Date() }, + ioQueue: DispatchQueue = DispatchQueue( + label: "com.lume.recorder.live-stats-io", + qos: .utility, + attributes: .concurrent + ), + inspector: @escaping Inspector = { MediaInspector.stats(in: $0) } + ) { + self.freshness = freshness + self.now = now + self.ioQueue = ioQueue + self.inspector = inspector + } + + /// Returns stats for the active recording in `directory`. + /// Concurrent callers for the same `id` share an in-flight scan. + /// Subsequent callers within `freshness` receive the cached stats. + public func stats(for id: UUID, in directory: URL) async -> MediaStats { + let currentTime = now() + + // 1. Fresh cache hit + if let entry = cache[id], currentTime.timeIntervalSince(entry.recordedAt) < freshness { + return entry.stats + } + + // 2. Coalesce onto in-flight scan + if let existing = inFlight[id] { + return await existing.task.value + } + + // 3. Initiate single-flight scan on dedicated I/O executor + let gen = generations[id, default: 0] + let scanID = UUID() + totalScans += 1 + scansByID[id, default: 0] += 1 + + let queue = ioQueue + let inspect = inspector + let task = Task { + await withCheckedContinuation { continuation in + queue.async { + let s = inspect(directory) + continuation.resume(returning: s) + } + } + } + + inFlight[id] = InFlightScan(scanID: scanID, task: task) + let result = await task.value + + // Clean up in-flight pointer if it wasn't superseded + if inFlight[id]?.scanID == scanID { + inFlight.removeValue(forKey: id) + } + + // Only cache if this recording wasn't invalidated during the scan + if generations[id, default: 0] == gen { + cache[id] = Entry(stats: result, recordedAt: now()) + } + + return result + } + + /// Read cached stats without triggering an inspection. + public func cachedStats(for id: UUID) -> MediaStats? { + guard let entry = cache[id], now().timeIntervalSince(entry.recordedAt) < freshness else { + return nil + } + return entry.stats + } + + /// Directly updates cached stats (e.g. from an authoritative inspection). + public func update(_ id: UUID, stats: MediaStats) { + generations[id, default: 0] += 1 + cache[id] = Entry(stats: stats, recordedAt: now()) + } + + /// Invalidates cached stats and cancels any in-flight scan for `id`. + /// Called on retry, stop, delete, and finalization. + public func invalidate(_ id: UUID) { + generations[id, default: 0] += 1 + cache.removeValue(forKey: id) + if let scan = inFlight.removeValue(forKey: id) { + scan.task.cancel() + } + } + + /// Notifies that media was appended; invalidates cache so the next poll refreshes. + public func didAppendSegment(for id: UUID) { + invalidate(id) + } + + /// Returns the number of disk scans executed for `id`. + public func scanCount(for id: UUID) -> Int { + scansByID[id] ?? 0 + } + + /// Returns the total number of scans executed across all recordings. + public func totalScanCount() -> Int { + totalScans + } + + /// Resets all scan counters (for testing). + public func resetScanCounts() { + totalScans = 0 + scansByID.removeAll() + } + + /// Discards all cached entries and in-flight scans. + public func reset() { + cache.removeAll() + for (_, scan) in inFlight { + scan.task.cancel() + } + inFlight.removeAll() + generations.removeAll() + } +} diff --git a/Sources/RecorderCore/Recording/RecordingScheduler.swift b/Sources/RecorderCore/Recording/RecordingScheduler.swift index 9f1c7e9..04d35c6 100644 --- a/Sources/RecorderCore/Recording/RecordingScheduler.swift +++ b/Sources/RecorderCore/Recording/RecordingScheduler.swift @@ -83,6 +83,7 @@ public actor RecordingScheduler: Service { } public nonisolated let options: Options + public let liveStats: LiveStatsCoordinator? private let store: RecorderStore private let launcher: any RecordingProcessLauncher private let disk: any DiskSpaceProviding @@ -101,6 +102,7 @@ public actor RecordingScheduler: Service { launcher: any RecordingProcessLauncher, options: Options, disk: any DiskSpaceProviding = FileSystemDiskSpace(), + liveStats: LiveStatsCoordinator? = nil, now: @escaping NowProvider = { Date() }, logger: Logger = Logger(label: "lume-recorder.scheduler") ) { @@ -108,6 +110,7 @@ public actor RecordingScheduler: Service { self.launcher = launcher self.options = options self.disk = disk + self.liveStats = liveStats self.now = now self.logger = logger } @@ -243,6 +246,7 @@ public actor RecordingScheduler: Service { job.task?.cancel() } await job.task?.value + await liveStats?.invalidate(id) logger.info("Recording \(id) stopped on request") return await store.recording(id) ?? record } @@ -262,6 +266,7 @@ public actor RecordingScheduler: Service { await job.task?.value jobs[id] = nil } + await liveStats?.invalidate(id) try? FileManager.default.removeItem(at: store.mediaDirectory(for: id)) try await store.removeRecording(id) if let key = record.idempotencyKey, let device = record.createdByDeviceID { @@ -289,6 +294,12 @@ public actor RecordingScheduler: Service { await job.task?.value } jobs.removeAll() + await liveStats?.reset() + } + + /// Invalidate cached live statistics for a recording (e.g. after appending media). + public func invalidateLiveStats(_ id: UUID) async { + await liveStats?.invalidate(id) } // MARK: Queries @@ -369,6 +380,9 @@ public actor RecordingScheduler: Service { let attempt = record.attempts + 1 _ = try? await store.update(id) { $0.attempts = attempt } + if attempt > 1 { + await liveStats?.invalidate(id) + } try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) let spec = RecordingAttemptSpec( recordingID: id, @@ -442,6 +456,7 @@ public actor RecordingScheduler: Service { } let delay = min(backoff, timeLeft) logger.warning("Recording \(id) source ended early; retrying in \(Int(delay.rounded())) s") + await liveStats?.invalidate(id) do { try await Task.sleep(for: .seconds(delay)) } catch { @@ -478,6 +493,7 @@ public actor RecordingScheduler: Service { } let stats = MediaInspector.stats(in: directory) let current = now() + await liveStats?.invalidate(id) if stats.segmentCount > 0 { _ = try? await store.update(id) { $0.status = .completed diff --git a/Sources/lume-recorder/LumeRecorderCommand.swift b/Sources/lume-recorder/LumeRecorderCommand.swift index 0aa6085..a6b4a32 100644 --- a/Sources/lume-recorder/LumeRecorderCommand.swift +++ b/Sources/lume-recorder/LumeRecorderCommand.swift @@ -32,6 +32,7 @@ struct Serve: AsyncParsableCommand { name: .customLong("single-file-recordings"), help: "Merge each finished recording into one recording.ts (env SINGLE_FILE_RECORDINGS=1)." ) var singleFileRecordings = false + @Option(name: .customLong("live-stats-freshness"), help: "Live stats cache freshness in seconds (env LIVE_STATS_FRESHNESS, default 2.0).") var liveStatsFreshness: Double? func run() async throws { var config = RecorderConfig.fromEnvironment() @@ -51,6 +52,7 @@ struct Serve: AsyncParsableCommand { } if noBonjour { config.bonjourEnabled = false } if singleFileRecordings { config.singleFileRecordings = true } + if let liveStatsFreshness { config.liveStatsFreshness = liveStatsFreshness } guard config.maxConcurrent >= 1 else { throw ValidationError("max-concurrent must be at least 1") } let level = config.logLevel diff --git a/Tests/RecorderCoreTests/LiveStatsTests.swift b/Tests/RecorderCoreTests/LiveStatsTests.swift new file mode 100644 index 0000000..bf84197 --- /dev/null +++ b/Tests/RecorderCoreTests/LiveStatsTests.swift @@ -0,0 +1,337 @@ +import Foundation +import HTTPTypes +import Hummingbird +import HummingbirdTesting +import LumeRecorderKit +@testable import RecorderCore +import Testing + +@Suite("Live stats coalescing, caching and lifecycle") +struct LiveStatsTests { + @Test func concurrentListPollsShareRefreshes() async throws { + let harness = try APIHarness() + try await harness.run { client in + let token = try await harness.pair(client).token + let req = harness.createRequest(start: 0, end: 3600, title: "Live Show") + let createdRes = try await client.execute( + uri: "/api/v1/recordings", + method: .post, + headers: harness.auth(token), + body: APIHarness.body(req) + ) + #expect(createdRes.status == .created) + let created = try APIHarness.decode(Recording.self, createdRes) + #expect(created.status == .recording) + + let dir = harness.services.store.mediaDirectory(for: created.id) + #expect(await eventually { FileManager.default.fileExists(atPath: dir.appendingPathComponent("index.m3u8").path) }) + + // Invalidate the cache from POST /recordings and reset scan counts + // so we measure the cold concurrent GET /recordings coalescing + await harness.services.liveStats.invalidate(created.id) + await harness.services.liveStats.resetScanCounts() + + // Concurrently fire 20 list requests + let count = 20 + let results = try await withThrowingTaskGroup(of: [Recording].self) { group in + for _ in 0 ..< count { + group.addTask { + let res = try await client.execute( + uri: "/api/v1/recordings", + method: .get, + headers: harness.auth(token) + ) + #expect(res.status == .ok) + return try APIHarness.decode([Recording].self, res) + } + } + var all: [[Recording]] = [] + for try await list in group { + all.append(list) + } + return all + } + + #expect(results.count == count) + for list in results { + #expect(list.count == 1) + #expect(list[0].id == created.id) + #expect(list[0].status == .recording) + } + + // All 20 concurrent requests must have coalesced into exactly 1 disk scan! + let scans = await harness.services.liveStats.scanCount(for: created.id) + #expect(scans == 1) + } + } + + @Test func pollsInsideFreshnessIntervalCauseNoExtraScans() async throws { + let harness = try APIHarness() + try await harness.run { client in + let token = try await harness.pair(client).token + let req = harness.createRequest(start: 0, end: 3600, title: "Live Show") + let createdRes = try await client.execute( + uri: "/api/v1/recordings", + method: .post, + headers: harness.auth(token), + body: APIHarness.body(req) + ) + let created = try APIHarness.decode(Recording.self, createdRes) + let dir = harness.services.store.mediaDirectory(for: created.id) + #expect(await eventually { FileManager.default.fileExists(atPath: dir.appendingPathComponent("index.m3u8").path) }) + + // Invalidate to start from a cold cache + await harness.services.liveStats.invalidate(created.id) + await harness.services.liveStats.resetScanCounts() + + // First poll: cold scan + let res1 = try await client.execute(uri: "/api/v1/recordings", method: .get, headers: harness.auth(token)) + #expect(res1.status == .ok) + #expect(await harness.services.liveStats.scanCount(for: created.id) == 1) + + // Subsequent polls within freshness interval (default 2s): no additional disk scan + for _ in 0 ..< 10 { + let cachedRes = try await client.execute(uri: "/api/v1/recordings", method: .get, headers: harness.auth(token)) + #expect(cachedRes.status == .ok) + } + #expect(await harness.services.liveStats.scanCount(for: created.id) == 1) + + // Invalidate cache: next poll must rescan + await harness.services.liveStats.invalidate(created.id) + let resAfterInvalidate = try await client.execute(uri: "/api/v1/recordings", method: .get, headers: harness.auth(token)) + #expect(resAfterInvalidate.status == .ok) + #expect(await harness.services.liveStats.scanCount(for: created.id) == 2) + } + } + + @Test func finishedRecordingsUseExistingCachedStatsWithoutDiskScans() async throws { + let harness = try APIHarness() + try await harness.run { client in + let token = try await harness.pair(client).token + + // Directly insert completed and failed recordings into store + let now = Date() + let completedRecord = RecordingRecord( + id: UUID(), + streamURL: URL(string: "http://example.com/stream.ts")!, + title: "Completed Match", + start: now.addingTimeInterval(-3600), + end: now.addingTimeInterval(-1800), + status: .completed, + createdAt: now.addingTimeInterval(-3600), + finishedAt: now.addingTimeInterval(-1800), + durationSeconds: 1800.0, + sizeBytes: 50_000_000 + ) + try await harness.services.store.insert(completedRecord) + + await harness.services.liveStats.resetScanCounts() + + let res = try await client.execute(uri: "/api/v1/recordings", method: .get, headers: harness.auth(token)) + #expect(res.status == .ok) + let recordings = try APIHarness.decode([Recording].self, res) + let found = recordings.first { $0.id == completedRecord.id } + #expect(found?.durationSeconds == 1800.0) + #expect(found?.sizeBytes == 50_000_000) + + // No disk scan should have occurred for finished recording! + #expect(await harness.services.liveStats.scanCount(for: completedRecord.id) == 0) + #expect(await harness.services.liveStats.totalScanCount() == 0) + } + } + + @Test func lifecycleInvalidationOnAppendRetryStopAndFinalization() async throws { + let tempDir = makeTempDirectory() + defer { removeTempDirectory(tempDir) } + + let clock = TestClock() + let coordinator = LiveStatsCoordinator(freshness: 2.0, now: clock.provider) + + let id = UUID() + let recordingDir = tempDir.appendingPathComponent(id.uuidString.lowercased(), isDirectory: true) + try FileManager.default.createDirectory(at: recordingDir, withIntermediateDirectories: true) + + let playlistURL = recordingDir.appendingPathComponent("index.m3u8") + let seg1URL = recordingDir.appendingPathComponent("a1_00000.ts") + try Data(repeating: 0x47, count: 1880).write(to: seg1URL) + let m3u8_1 = "#EXTM3U\n#EXT-X-VERSION:6\n#EXTINF:6.0,\na1_00000.ts\n" + try m3u8_1.write(to: playlistURL, atomically: true, encoding: .utf8) + + // 1. Initial scan + let stats1 = await coordinator.stats(for: id, in: recordingDir) + #expect(stats1.segmentCount == 1) + #expect(stats1.durationSeconds == 6.0) + #expect(stats1.sizeBytes == 1880 + Int64(m3u8_1.utf8.count)) + #expect(await coordinator.scanCount(for: id) == 1) + + // 2. Append new media segment and playlist update + let seg2URL = recordingDir.appendingPathComponent("a1_00001.ts") + try Data(repeating: 0x47, count: 1880).write(to: seg2URL) + let m3u8_2 = "#EXTM3U\n#EXT-X-VERSION:6\n#EXTINF:6.0,\na1_00000.ts\n#EXTINF:6.0,\na1_00001.ts\n" + try m3u8_2.write(to: playlistURL, atomically: true, encoding: .utf8) + + // Before invalidation (within freshness): still cached + let cached = await coordinator.stats(for: id, in: recordingDir) + #expect(cached.segmentCount == 1) + #expect(await coordinator.scanCount(for: id) == 1) + + // On append notification: + await coordinator.didAppendSegment(for: id) + let stats2 = await coordinator.stats(for: id, in: recordingDir) + #expect(stats2.segmentCount == 2) + #expect(stats2.durationSeconds == 12.0) + #expect(stats2.sizeBytes == 3760 + Int64(m3u8_2.utf8.count)) + #expect(await coordinator.scanCount(for: id) == 2) + + // 3. Retry invalidation: + await coordinator.invalidate(id) + #expect(await coordinator.cachedStats(for: id) == nil) + + // 4. Stop / Finalization invalidation: + await coordinator.invalidate(id) + #expect(await coordinator.cachedStats(for: id) == nil) + } + + @Test func inspectionRacingDeletion() async throws { + let tempDir = makeTempDirectory() + defer { removeTempDirectory(tempDir) } + + let id = UUID() + let recordingDir = tempDir.appendingPathComponent(id.uuidString.lowercased(), isDirectory: true) + try FileManager.default.createDirectory(at: recordingDir, withIntermediateDirectories: true) + + let playlistURL = recordingDir.appendingPathComponent("index.m3u8") + let segURL = recordingDir.appendingPathComponent("a1_00000.ts") + try Data(repeating: 0x47, count: 1880).write(to: segURL) + let m3u8 = "#EXTM3U\n#EXT-X-VERSION:6\n#EXTINF:6.0,\na1_00000.ts\n" + try m3u8.write(to: playlistURL, atomically: true, encoding: .utf8) + + // Coordinator with artificial delay in inspector to guarantee race condition + let coordinator = LiveStatsCoordinator( + freshness: 2.0, + inspector: { url in + Thread.sleep(forTimeInterval: 0.05) + return MediaInspector.stats(in: url) + } + ) + + // Start inspection task in background + let inspectTask = Task { + await coordinator.stats(for: id, in: recordingDir) + } + + // Mid-inspection: delete recording and remove directory + try? await Task.sleep(for: .milliseconds(10)) + await coordinator.invalidate(id) + try? FileManager.default.removeItem(at: recordingDir) + + // Await the racing inspection task + let stats = await inspectTask.value + // It must finish without throwing or crashing + #expect(stats.segmentCount >= 0) + + // Because invalidation occurred during the race, coordinator must NOT retain cached entry + #expect(await coordinator.cachedStats(for: id) == nil) + } + + @Test func benchmarkConcurrentListingWithThousandsOfSegmentsDoesNotStallUnrelatedRequests() async throws { + let harness = try APIHarness() + try await harness.run { client in + let token = try await harness.pair(client).token + + // Create an active recording + let req = harness.createRequest(start: 0, end: 7200, title: "Huge Live Stream") + let createdRes = try await client.execute( + uri: "/api/v1/recordings", + method: .post, + headers: harness.auth(token), + body: APIHarness.body(req) + ) + let created = try APIHarness.decode(Recording.self, createdRes) + let dir = harness.services.store.mediaDirectory(for: created.id) + try FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true) + + // Populate directory with 2,500 segments and a large index.m3u8 + let segmentCount = 2500 + var playlistText = "#EXTM3U\n#EXT-X-VERSION:6\n#EXT-X-TARGETDURATION:6\n" + for i in 0 ..< segmentCount { + let name = String(format: "a1_%05d.ts", i) + let segURL = dir.appendingPathComponent(name) + // Write dummy segment file + FileManager.default.createFile(atPath: segURL.path, contents: Data(repeating: 0x47, count: 188)) + playlistText += "#EXTINF:6.000,\n\(name)\n" + } + try playlistText.write(to: dir.appendingPathComponent("index.m3u8"), atomically: true, encoding: .utf8) + + // Invalidate to force a cold inspection + await harness.services.liveStats.invalidate(created.id) + await harness.services.liveStats.resetScanCounts() + + // Run concurrent requests: + // 20 concurrent list requests + concurrent unrelated info requests + let startTime = Date() + var unrelatedDuration: TimeInterval = 0 + + let (listResults, infoStatus) = try await withThrowingTaskGroup(of: (String, TimeInterval, Int).self) { group in + // Launch unrelated requests + group.addTask { + let t0 = Date() + let infoRes = try await client.execute(uri: "/api/v1/info", method: .get) + let elapsed = Date().timeIntervalSince(t0) + return ("info", elapsed, infoRes.status.code) + } + + // Launch 20 concurrent listing requests + for _ in 0 ..< 20 { + group.addTask { + let t0 = Date() + let listRes = try await client.execute( + uri: "/api/v1/recordings", + method: .get, + headers: harness.auth(token) + ) + let elapsed = Date().timeIntervalSince(t0) + return ("list", elapsed, listRes.status.code) + } + } + + var listCodes: [Int] = [] + var infoCode = 0 + for try await (kind, elapsed, code) in group { + if kind == "info" { + unrelatedDuration = elapsed + infoCode = code + } else { + listCodes.append(code) + } + } + return (listCodes, infoCode) + } + + let totalElapsed = Date().timeIntervalSince(startTime) + + // Verify unrelated request completed successfully + #expect(infoStatus == 200) + #expect(listResults.count == 20) + for code in listResults { + #expect(code == 200) + } + + // Verify single-flight: only ONE cold scan was executed for all 20 concurrent requests! + let scans = await harness.services.liveStats.scanCount(for: created.id) + #expect(scans == 1) + + // Verify the cached stats reflected the cold inspection of 2,500+ segments + guard let cached = await harness.services.liveStats.cachedStats(for: created.id) else { + Issue.record("Missing cached stats for created recording") + return + } + #expect(cached.segmentCount >= segmentCount) + #expect(cached.durationSeconds >= Double(segmentCount) * 6.0) + #expect(cached.sizeBytes > 0) + + // Unrelated info request should complete quickly without stalling + print("Benchmark with \(segmentCount) segments: unrelated /info took \(String(format: "%.3f", unrelatedDuration))s, total batch took \(String(format: "%.3f", totalElapsed))s, scan count = \(scans)") + } + } +}