From 415ed3388d7a3a92d459a3d3a6a3469a73b30198 Mon Sep 17 00:00:00 2001 From: Fedor Korotkov Date: Thu, 29 Jun 2023 18:51:41 +0400 Subject: [PATCH] Optimistically check if we need to do anything on a pull (#531) * Optimistically check if we need to do anything on a pull Right now on a pull we always acquire a lock for a registry host. This is problematic because, for example, host can be pulling `ghcr.io/cirruslabs/macos-ventura-xcode:15-beta-2` image when a new request will come to pull `ghcr.io/cirruslabs/macos-ventura-xcode:latest` if needed. In this situation, even though `ghcr.io/cirruslabs/macos-ventura-xcode:latest` is already cached and linked, `tart pull` will wait for a lock. This change optimistically check if there is something to do at all before acquiring a lock. * Fix linter errors --------- Co-authored-by: Nikolay Edigaryev --- Sources/tart/VMStorageOCI.swift | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/Sources/tart/VMStorageOCI.swift b/Sources/tart/VMStorageOCI.swift index d78af34..8660048 100644 --- a/Sources/tart/VMStorageOCI.swift +++ b/Sources/tart/VMStorageOCI.swift @@ -144,6 +144,12 @@ class VMStorageOCI: PrunableStorage { let digestName = RemoteName(host: name.host, namespace: name.namespace, reference: Reference(digest: Digest.hash(manifestData))) + if exists(name) && exists(digestName) && linked(from: name, to: digestName) { + // optimistically check if we need to do anything at all before locking + defaultLogger.appendNewLine("\(digestName) image is already cached and linked!") + return + } + // Ensure that host directory for given RemoteName exists in OCI storage let hostDirectoryURL = hostDirectoryURL(digestName) try FileManager.default.createDirectory(at: hostDirectoryURL, withIntermediateDirectories: true) @@ -203,6 +209,15 @@ class VMStorageOCI: PrunableStorage { } } + func linked(from: RemoteName, to: RemoteName) -> Bool { + do { + let resolvedFrom = try FileManager.default.destinationOfSymbolicLink(atPath: vmURL(from).path) + return resolvedFrom == vmURL(to).path + } catch { + return false + } + } + func link(from: RemoteName, to: RemoteName) throws { if FileManager.default.fileExists(atPath: vmURL(from).path) { try FileManager.default.removeItem(at: vmURL(from))