Skip to content

Commit

Permalink
Don't delete an initiator of pruning (#556)
Browse files Browse the repository at this point in the history
* Don't delete an initiator of pruning

Sometimes people have an image that is greater than half of the disk itself. In that case such image will be pulled and prunned right away.

This change makes sure that an image that is being cloned from is not pruned right away.

* Resolve symbolic links
  • Loading branch information
fkorotkov authored Jul 13, 2023
1 parent cf9a3a9 commit 93a1b70
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Sources/tart/Commands/Clone.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct Clone: AsyncParsableCommand {
// APFS is doing copy-on-write so the above cloning operation (just copying files on disk)
// is not actually claiming new space until the VM is started and it writes something to disk.
// So once we clone the VM let's try to claim a little bit of space for the VM to run.
try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes()))
try Prune.reclaimIfNeeded(UInt64(sourceVM.sizeBytes()), sourceVM)
}, onCancel: {
try? FileManager.default.removeItem(at: tmpVMDir.baseURL)
})
Expand Down
11 changes: 8 additions & 3 deletions Sources/tart/Commands/Prune.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct Prune: AsyncParsableCommand {
try prunablesToDelete.forEach { try $0.delete() }
}

static func reclaimIfNeeded(_ requiredBytes: UInt64) throws {
static func reclaimIfNeeded(_ requiredBytes: UInt64, _ initiator: Prunable? = nil) throws {
SentrySDK.configureScope { scope in
scope.setContext(value: ["requiredBytes": requiredBytes], key: "Prune")
}
Expand Down Expand Up @@ -114,10 +114,10 @@ struct Prune: AsyncParsableCommand {
return
}

try Prune.reclaimIfPossible(requiredBytes - volumeAvailableCapacityCalculated)
try Prune.reclaimIfPossible(requiredBytes - volumeAvailableCapacityCalculated, initiator)
}

private static func reclaimIfPossible(_ reclaimBytes: UInt64) throws {
private static func reclaimIfPossible(_ reclaimBytes: UInt64, _ initiator: Prunable? = nil) throws {
let transaction = SentrySDK.startTransaction(name: "Pruning cache", operation: "prune", bindToScope: true)
defer { transaction.finish() }

Expand All @@ -141,6 +141,11 @@ struct Prune: AsyncParsableCommand {
break
}

if prunable.url == initiator?.url.resolvingSymlinksInPath() {
// do not prune the initiator
continue
}

try SentrySDK.span?.setData(value: prunable.sizeBytes(), key: prunable.url.path)

cacheReclaimedBytes += try prunable.sizeBytes()
Expand Down

0 comments on commit 93a1b70

Please sign in to comment.