-
Notifications
You must be signed in to change notification settings - Fork 31
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 30 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: some CacheStorage) async { | ||
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: some CacheStorage) async -> RestoreResult { | ||
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 |
---|---|---|
|
@@ -3,37 +3,33 @@ import ScipioStorage | |
import PackageGraph | ||
import TSCBasic | ||
|
||
public struct LocalCacheStorage: CacheStorage { | ||
struct LocalDiskCacheStorage: CacheStorage { | ||
private let fileSystem: any FileSystem | ||
|
||
public var parallelNumber: Int? { nil } | ||
var parallelNumber: Int? { nil } | ||
|
||
enum Error: Swift.Error { | ||
case cacheDirectoryIsNotFound | ||
} | ||
|
||
public enum CacheDirectory: Sendable { | ||
case system | ||
case custom(URL) | ||
} | ||
|
||
private let cacheDirectroy: CacheDirectory | ||
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. 👀 Directroy |
||
private let baseURL: URL? | ||
|
||
public init(cacheDirectory: CacheDirectory = .system, fileSystem: FileSystem = localFileSystem) { | ||
self.cacheDirectroy = cacheDirectory | ||
/// - Parameters: | ||
/// - baseURL: The base url for the local disk cache. When it is nil, the system cache directory (`~/Library/Caches`) will be used. | ||
init(baseURL: URL?, fileSystem: FileSystem = localFileSystem) { | ||
self.baseURL = baseURL | ||
self.fileSystem = fileSystem | ||
} | ||
|
||
private func buildBaseDirectoryPath() throws -> URL { | ||
let cacheDir: URL | ||
switch cacheDirectroy { | ||
case .system: | ||
if let baseURL { | ||
cacheDir = baseURL | ||
} else { | ||
guard let systemCacheDir = fileSystem.cachesDirectory else { | ||
throw Error.cacheDirectoryIsNotFound | ||
} | ||
cacheDir = systemCacheDir.asURL | ||
case .custom(let customPath): | ||
cacheDir = customPath | ||
} | ||
return cacheDir.appendingPathComponent("Scipio") | ||
} | ||
|
@@ -51,7 +47,7 @@ public struct LocalCacheStorage: CacheStorage { | |
.appendingPathComponent(xcFrameworkFileName(for: cacheKey)) | ||
} | ||
|
||
public func existsValidCache(for cacheKey: some CacheKey) async -> Bool { | ||
func existsValidCache(for cacheKey: some CacheKey) async -> Bool { | ||
do { | ||
let xcFrameworkPath = try cacheFrameworkPath(for: cacheKey) | ||
return fileSystem.exists(xcFrameworkPath.absolutePath) | ||
|
@@ -60,7 +56,7 @@ public struct LocalCacheStorage: CacheStorage { | |
} | ||
} | ||
|
||
public func cacheFramework(_ frameworkPath: URL, for cacheKey: some CacheKey) async { | ||
func cacheFramework(_ frameworkPath: URL, for cacheKey: some CacheKey) async { | ||
do { | ||
let destination = try cacheFrameworkPath(for: cacheKey) | ||
let directoryPath = destination.deletingLastPathComponent() | ||
|
@@ -72,7 +68,7 @@ public struct LocalCacheStorage: CacheStorage { | |
} | ||
} | ||
|
||
public func fetchArtifacts(for cacheKey: some CacheKey, to destinationDir: URL) async throws { | ||
func fetchArtifacts(for cacheKey: some CacheKey, to destinationDir: URL) async throws { | ||
let source = try cacheFrameworkPath(for: cacheKey) | ||
let destination = destinationDir.appendingPathComponent(xcFrameworkFileName(for: cacheKey)) | ||
try fileSystem.copy(from: source.absolutePath, to: destination.absolutePath) | ||
|
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. | ||
struct ProjectCacheStorage: CacheStorage { | ||
func existsValidCache(for cacheKey: some ScipioStorage.CacheKey) async throws -> Bool { false } | ||
func fetchArtifacts(for cacheKey: some ScipioStorage.CacheKey, to destinationDir: URL) async throws {} | ||
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))" | ||
} | ||
} |
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.