-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multiple cache storages feature #156
Changes from 20 commits
6d35c14
5953d6c
a24f7c9
d7bd9ff
e7b20c5
0f1c98a
8750153
8c7c16e
f6cbef1
413bb12
c3d9efa
25f5fcb
82ec106
086970f
7434f6b
a451614
c22ed6f
0006fbb
4d8abab
76ae5af
ff1d483
4712f89
3d333e6
0c81a2e
d71053b
ed52625
4de26b5
bced0e2
dfe443c
acdd251
afafa8d
2720cde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,6 @@ struct CacheSystem: Sendable { | |
static let defaultParalellNumber = 8 | ||
private let pinsStore: PinsStore | ||
private let outputDirectory: URL | ||
private let storage: (any CacheStorage)? | ||
private let fileSystem: any FileSystem | ||
|
||
struct CacheTarget: Hashable, Sendable { | ||
|
@@ -139,18 +138,23 @@ struct CacheSystem: Sendable { | |
init( | ||
pinsStore: PinsStore, | ||
outputDirectory: URL, | ||
storage: (any CacheStorage)?, | ||
fileSystem: any FileSystem = localFileSystem | ||
) { | ||
self.pinsStore = pinsStore | ||
self.outputDirectory = outputDirectory | ||
self.storage = storage | ||
self.fileSystem = fileSystem | ||
} | ||
|
||
func cacheFrameworks(_ targets: Set<CacheTarget>) async { | ||
let chunked = targets.chunks(ofCount: storage?.parallelNumber ?? CacheSystem.defaultParalellNumber) | ||
func cacheFrameworks(_ targets: Set<CacheTarget>, to storages: [any CacheStorage]) async { | ||
for storage in storages { | ||
await cacheFrameworks(targets, to: storage) | ||
} | ||
} | ||
|
||
private func cacheFrameworks(_ targets: Set<CacheTarget>, to storage: any CacheStorage) async { | ||
ikesyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let chunked = targets.chunks(ofCount: storage.parallelNumber ?? CacheSystem.defaultParalellNumber) | ||
|
||
let storageName = storage.displayName | ||
for chunk in chunked { | ||
await withTaskGroup(of: Void.self) { group in | ||
for target in chunk { | ||
|
@@ -159,10 +163,10 @@ struct CacheSystem: Sendable { | |
let frameworkPath = outputDirectory.appendingPathComponent(frameworkName) | ||
do { | ||
logger.info( | ||
"🚀 Cache \(frameworkName) to cache storage", | ||
"🚀 Cache \(frameworkName) to cache storage: \(storageName)", | ||
metadata: .color(.green) | ||
) | ||
try await cacheFramework(target, at: frameworkPath) | ||
try await cacheFramework(target, at: frameworkPath, to: storage) | ||
} catch { | ||
logger.warning("⚠️ Can't create caches for \(frameworkPath.path)") | ||
} | ||
|
@@ -173,10 +177,10 @@ struct CacheSystem: Sendable { | |
} | ||
} | ||
|
||
private func cacheFramework(_ target: CacheTarget, at frameworkPath: URL) async throws { | ||
private func cacheFramework(_ target: CacheTarget, at frameworkPath: URL, to storage: any CacheStorage) async throws { | ||
let cacheKey = try await calculateCacheKey(of: target) | ||
|
||
try await storage?.cacheFramework(frameworkPath, for: cacheKey) | ||
try await storage.cacheFramework(frameworkPath, for: cacheKey) | ||
} | ||
|
||
func generateVersionFile(for target: CacheTarget) async throws { | ||
|
@@ -210,8 +214,8 @@ struct CacheSystem: Sendable { | |
case failed(LocalizedError?) | ||
case noCache | ||
} | ||
func restoreCacheIfPossible(target: CacheTarget) async -> RestoreResult { | ||
guard let storage = storage else { return .noCache } | ||
|
||
func restoreCacheIfPossible(target: CacheTarget, storage: any CacheStorage) async -> RestoreResult { | ||
ikesyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
do { | ||
let cacheKey = try await calculateCacheKey(of: target) | ||
if try await storage.existsValidCache(for: cacheKey) { | ||
|
@@ -225,12 +229,6 @@ struct CacheSystem: Sendable { | |
} | ||
} | ||
|
||
private func fetchArtifacts(target: CacheTarget, to destination: URL) async throws { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was unused. |
||
guard let storage = storage else { return } | ||
let cacheKey = try await calculateCacheKey(of: target) | ||
try await storage.fetchArtifacts(for: cacheKey, to: destination) | ||
} | ||
|
||
func calculateCacheKey(of target: CacheTarget) async throws -> SwiftPMCacheKey { | ||
let targetName = target.buildProduct.target.name | ||
let pin = try retrievePin(package: target.buildProduct.package) | ||
|
@@ -247,7 +245,10 @@ struct CacheSystem: Sendable { | |
buildOptions: buildOptions, | ||
clangVersion: clangVersion, | ||
xcodeVersion: xcodeVersion, | ||
scipioVersion: currentScipioVersion | ||
// Making the cache key compatible with 0.24.0 temporarily for easier debugging. | ||
// | ||
// TODO: revert this before merging | ||
scipioVersion: "578ce98d236e79dad3e473cb11153e867be07174" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the feature, we're better to add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disabling when developing is not a solution, because if excluding Ideally we should introduce some kind of Scipio's cache versioning and increment it when there are some incompatible changes to built frameworks (but it'll make Scipio's development and review process a bit complicated). |
||
) | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Foundation | ||
import ScipioStorage | ||
|
||
/// The pseudo cache storage for "project cache policy", which treats built frameworks under the project's output directory (e.g. `XCFrameworks`) | ||
/// as valid caches but does not saving / restoring anything. | ||
public struct ProjectCacheStorage: CacheStorage { | ||
public func existsValidCache(for cacheKey: some ScipioStorage.CacheKey) async throws -> Bool { false } | ||
public func fetchArtifacts(for cacheKey: some ScipioStorage.CacheKey, to destinationDir: URL) async throws {} | ||
public func cacheFramework(_ frameworkPath: URL, for cacheKey: some ScipioStorage.CacheKey) async throws {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import ScipioStorage | ||
|
||
extension CacheStorage { | ||
/// The display name of the cache storage used for logging purpose | ||
var displayName: String { | ||
// TODO: Define the property as CacheStorage's requirement in scipio-cache-storage | ||
"\(type(of: self))" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,31 +10,15 @@ struct FrameworkProducer { | |
private let descriptionPackage: DescriptionPackage | ||
private let baseBuildOptions: BuildOptions | ||
private let buildOptionsMatrix: [String: BuildOptions] | ||
private let cacheMode: Runner.Options.CacheMode | ||
private let cachePolicies: [Runner.Options.CachePolicy] | ||
private let overwrite: Bool | ||
private let outputDir: URL | ||
private let fileSystem: any FileSystem | ||
private let toolchainEnvironment: [String: String]? | ||
|
||
private var cacheStorage: (any CacheStorage)? { | ||
switch cacheMode { | ||
case .disabled, .project: return nil | ||
case .storage(let storage, _): return storage | ||
} | ||
} | ||
|
||
private var isProducingCacheEnabled: Bool { | ||
switch cacheMode { | ||
case .disabled: return false | ||
case .project: return true | ||
case .storage(_, let actors): | ||
return actors.contains(.producer) | ||
} | ||
} | ||
|
||
private var shouldGenerateVersionFile: Bool { | ||
// cacheMode is not disabled | ||
if case .disabled = cacheMode { | ||
// cache is not disabled | ||
guard !cachePolicies.isEmpty else { | ||
return false | ||
} | ||
|
||
|
@@ -49,7 +33,7 @@ struct FrameworkProducer { | |
descriptionPackage: DescriptionPackage, | ||
buildOptions: BuildOptions, | ||
buildOptionsMatrix: [String: BuildOptions], | ||
cacheMode: Runner.Options.CacheMode, | ||
cachePolicies: [Runner.Options.CachePolicy], | ||
overwrite: Bool, | ||
outputDir: URL, | ||
toolchainEnvironment: [String: String]? = nil, | ||
|
@@ -58,7 +42,7 @@ struct FrameworkProducer { | |
self.descriptionPackage = descriptionPackage | ||
self.baseBuildOptions = buildOptions | ||
self.buildOptionsMatrix = buildOptionsMatrix | ||
self.cacheMode = cacheMode | ||
self.cachePolicies = cachePolicies | ||
self.overwrite = overwrite | ||
self.outputDir = outputDir | ||
self.toolchainEnvironment = toolchainEnvironment | ||
|
@@ -108,12 +92,14 @@ struct FrameworkProducer { | |
let pinsStore = try descriptionPackage.workspace.pinsStore.load() | ||
let cacheSystem = CacheSystem( | ||
pinsStore: pinsStore, | ||
outputDirectory: outputDir, | ||
storage: cacheStorage | ||
outputDirectory: outputDir | ||
) | ||
|
||
let targetsToBuild: OrderedSet<CacheSystem.CacheTarget> | ||
if cacheMode.isConsumingCacheEnabled { | ||
if cachePolicies.isEmpty { | ||
// no-op because cache is disabled | ||
targetsToBuild = allTargets | ||
} else { | ||
let targets = Set(allTargets) | ||
|
||
// Validate the existing frameworks in `outputDir` before restoration | ||
|
@@ -122,16 +108,20 @@ struct FrameworkProducer { | |
cacheSystem: cacheSystem | ||
) | ||
|
||
let restored = await restoreAllAvailableCaches( | ||
availableTargets: targets.subtracting(valid), | ||
cacheSystem: cacheSystem | ||
) | ||
|
||
targetsToBuild = allTargets | ||
.subtracting(valid) | ||
.subtracting(restored) | ||
} else { | ||
targetsToBuild = allTargets | ||
let storagesWithConsumer = cachePolicies.storages(for: .consumer) | ||
if storagesWithConsumer.isEmpty { | ||
// no-op | ||
targetsToBuild = allTargets.subtracting(valid) | ||
} else { | ||
let restored = await restoreAllAvailableCachesIfNeeded( | ||
availableTargets: targets.subtracting(valid), | ||
to: storagesWithConsumer, | ||
cacheSystem: cacheSystem | ||
) | ||
targetsToBuild = allTargets | ||
.subtracting(valid) | ||
.subtracting(restored) | ||
} | ||
} | ||
|
||
for target in targetsToBuild { | ||
|
@@ -142,9 +132,7 @@ struct FrameworkProducer { | |
) | ||
} | ||
|
||
if isProducingCacheEnabled { | ||
await cacheSystem.cacheFrameworks(Set(targetsToBuild)) | ||
} | ||
await cacheFrameworksIfNeeded(Set(targetsToBuild), cacheSystem: cacheSystem) | ||
|
||
if shouldGenerateVersionFile { | ||
// Versionfiles should be generate for all targets | ||
|
@@ -167,13 +155,20 @@ struct FrameworkProducer { | |
group.addTask { [outputDir, fileSystem] in | ||
do { | ||
let product = target.buildProduct | ||
let outputPath = outputDir.appendingPathComponent(product.frameworkName) | ||
let frameworkName = product.frameworkName | ||
let outputPath = outputDir.appendingPathComponent(frameworkName) | ||
let exists = fileSystem.exists(outputPath.absolutePath) | ||
guard exists else { return nil } | ||
|
||
let expectedCacheKey = try await cacheSystem.calculateCacheKey(of: target) | ||
let isValidCache = await cacheSystem.existsValidCache(cacheKey: expectedCacheKey) | ||
guard isValidCache else { return nil } | ||
guard isValidCache else { | ||
logger.warning("⚠️ Existing \(frameworkName) is outdated.", metadata: .color(.yellow)) | ||
logger.info("🗑️ Delete \(frameworkName)", metadata: .color(.red)) | ||
try fileSystem.removeFileTree(outputPath.absolutePath) | ||
|
||
return nil | ||
} | ||
|
||
let expectedCacheKeyHash = try expectedCacheKey.calculateChecksum() | ||
logger.info( | ||
|
@@ -194,11 +189,58 @@ struct FrameworkProducer { | |
return validFrameworks | ||
} | ||
|
||
private func restoreAllAvailableCaches( | ||
private func restoreAllAvailableCachesIfNeeded( | ||
availableTargets: Set<CacheSystem.CacheTarget>, | ||
to storages: [any CacheStorage], | ||
cacheSystem: CacheSystem | ||
) async -> Set<CacheSystem.CacheTarget> { | ||
var remainingTargets = availableTargets | ||
var restored: Set<CacheSystem.CacheTarget> = [] | ||
|
||
for index in storages.indices { | ||
let storage = storages[index] | ||
|
||
let logSuffix = "[\(index)] \(storage.displayName)" | ||
if index == storages.startIndex { | ||
logger.info( | ||
"▶️ Starting restoration with cache storage: \(logSuffix)", | ||
metadata: .color(.green) | ||
) | ||
} else { | ||
logger.info( | ||
"⏭️ Falling back to next cache storage: \(logSuffix)", | ||
metadata: .color(.green) | ||
) | ||
} | ||
|
||
let restoredPerStorage = await restoreCaches( | ||
for: remainingTargets, | ||
from: storage, | ||
cacheSystem: cacheSystem | ||
) | ||
restored.formUnion(restoredPerStorage) | ||
|
||
logger.info( | ||
"⏸️ Restoration finished with cache storage: \(logSuffix)", | ||
metadata: .color(.green) | ||
) | ||
|
||
remainingTargets.subtract(restoredPerStorage) | ||
if remainingTargets.isEmpty { | ||
ikesyo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if all frameworks are successfully restored, we don't need to proceed to next cache storage. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [not mandatory] it's kindly to comment this |
||
} | ||
|
||
logger.info("⏹️ Restoration finished", metadata: .color(.green)) | ||
return restored | ||
} | ||
|
||
private func restoreCaches( | ||
for targets: Set<CacheSystem.CacheTarget>, | ||
from cacheStorage: any CacheStorage, | ||
cacheSystem: CacheSystem | ||
) async -> Set<CacheSystem.CacheTarget> { | ||
let chunked = availableTargets.chunks(ofCount: cacheStorage?.parallelNumber ?? CacheSystem.defaultParalellNumber) | ||
let chunked = targets.chunks(ofCount: cacheStorage.parallelNumber ?? CacheSystem.defaultParalellNumber) | ||
|
||
var restored: Set<CacheSystem.CacheTarget> = [] | ||
for chunk in chunked { | ||
|
@@ -209,7 +251,8 @@ struct FrameworkProducer { | |
do { | ||
let restored = try await restorer.restore( | ||
target: target, | ||
cacheSystem: cacheSystem | ||
cacheSystem: cacheSystem, | ||
cacheStorage: cacheStorage | ||
) | ||
return restored ? target : nil | ||
} catch { | ||
|
@@ -233,23 +276,16 @@ struct FrameworkProducer { | |
// Return true if pre-built artifact is available (already existing or restored from cache) | ||
func restore( | ||
target: CacheSystem.CacheTarget, | ||
cacheSystem: CacheSystem | ||
cacheSystem: CacheSystem, | ||
cacheStorage: any CacheStorage | ||
) async throws -> Bool { | ||
let product = target.buildProduct | ||
let frameworkName = product.frameworkName | ||
let outputPath = outputDir.appendingPathComponent(frameworkName) | ||
let exists = fileSystem.exists(outputPath.absolutePath) | ||
|
||
let expectedCacheKey = try await cacheSystem.calculateCacheKey(of: target) | ||
let expectedCacheKeyHash = try expectedCacheKey.calculateChecksum() | ||
|
||
if exists { | ||
logger.warning("⚠️ Existing \(frameworkName) is outdated.", metadata: .color(.yellow)) | ||
logger.info("🗑️ Delete \(frameworkName)", metadata: .color(.red)) | ||
try fileSystem.removeFileTree(outputPath.absolutePath) | ||
} | ||
|
||
let restoreResult = await cacheSystem.restoreCacheIfPossible(target: target) | ||
let restoreResult = await cacheSystem.restoreCacheIfPossible(target: target, storage: cacheStorage) | ||
switch restoreResult { | ||
case .succeeded: | ||
logger.info("✅ Restore \(frameworkName) (\(expectedCacheKeyHash)) from cache storage.", metadata: .color(.green)) | ||
|
@@ -303,6 +339,13 @@ struct FrameworkProducer { | |
return [] | ||
} | ||
|
||
private func cacheFrameworksIfNeeded(_ targets: Set<CacheSystem.CacheTarget>, cacheSystem: CacheSystem) async { | ||
let storagesWithProducer = cachePolicies.storages(for: .producer) | ||
if !storagesWithProducer.isEmpty { | ||
await cacheSystem.cacheFrameworks(targets, to: storagesWithProducer) | ||
} | ||
Comment on lines
+345
to
+348
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Q] I'm concerned that the way storages are handled is different for consumers and producers. For consumers, storages are evaluated in order and the top ones are used. Is this difference in behavior intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's intentional. If we don't cache built frameworks to 2nd storages and later, the storages will not be able to restore the frameworks so saving to all storages will be required I think. |
||
} | ||
|
||
private func generateVersionFile(for target: CacheSystem.CacheTarget, using cacheSystem: CacheSystem) async { | ||
do { | ||
try await cacheSystem.generateVersionFile(for: target) | ||
|
@@ -312,13 +355,12 @@ struct FrameworkProducer { | |
} | ||
} | ||
|
||
extension Runner.Options.CacheMode { | ||
fileprivate var isConsumingCacheEnabled: Bool { | ||
switch self { | ||
case .disabled: return false | ||
case .project: return true | ||
case .storage(_, let actors): | ||
return actors.contains(.consumer) | ||
extension [Runner.Options.CachePolicy] { | ||
fileprivate func storages(for actor: Runner.Options.CachePolicy.CacheActorKind) -> [any CacheStorage] { | ||
reduce(into: []) { result, cachePolicy in | ||
if cachePolicy.actors.contains(actor) { | ||
result.append(cachePolicy.storage) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Due to the introduction of cache policies, storages may be different on when saving caches and when restoring caches. So removed the property and passes by arguments instead.