From 2840427158340ec1d2db8a90597c47facf489120 Mon Sep 17 00:00:00 2001 From: Timofey Kirillov Date: Mon, 11 Oct 2021 18:17:22 +0300 Subject: [PATCH] fix(host-cleanup): "permission denied" errors, do not wipe git-patches on every run * Fix bug related to wiping of old git-patches by cache-version: fixed typo. * Do not remove git data entries which has been used recently (wait for 3 hours). --- pkg/git_repo/gitdata/gc.go | 4 +++- pkg/git_repo/gitdata/git_data_entry.go | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/git_repo/gitdata/gc.go b/pkg/git_repo/gitdata/gc.go index e7e0bf125a..84229e8e9f 100644 --- a/pkg/git_repo/gitdata/gc.go +++ b/pkg/git_repo/gitdata/gc.go @@ -110,7 +110,7 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage { cacheRoot := filepath.Join(werf.GetLocalCacheDir(), "git_patches") - if err := wipeCacheDirs(ctx, cacheRoot, []string{GitArchivesCacheVersion}); err != nil { + if err := wipeCacheDirs(ctx, cacheRoot, []string{GitPatchesCacheVersion}); err != nil { return fmt.Errorf("unable to wipe old git patches cache dirs in %q: %s", cacheRoot, err) } } @@ -201,6 +201,8 @@ func RunGC(ctx context.Context, allowedVolumeUsagePercentage, allowedVolumeUsage sort.Sort(GitDataLruSort(gitDataEntries)) + gitDataEntries = PreserveGitDataByLru(gitDataEntries) + var freedBytes uint64 for _, entry := range gitDataEntries { for _, path := range entry.GetPaths() { diff --git a/pkg/git_repo/gitdata/git_data_entry.go b/pkg/git_repo/gitdata/git_data_entry.go index dc77c4adb2..fcfe7c7bc7 100644 --- a/pkg/git_repo/gitdata/git_data_entry.go +++ b/pkg/git_repo/gitdata/git_data_entry.go @@ -15,3 +15,19 @@ func (a GitDataLruSort) Less(i, j int) bool { return a[i].GetLastAccessAt().Before(a[j].GetLastAccessAt()) } func (a GitDataLruSort) Swap(i, j int) { a[i], a[j] = a[j], a[i] } + +func PreserveGitDataByLru(entries []GitDataEntry) []GitDataEntry { + var res []GitDataEntry + + for _, entry := range entries { + if !ShouldPreserveGitDataEntryByLru(entry) { + res = append(res, entry) + } + } + + return res +} + +func ShouldPreserveGitDataEntryByLru(entry GitDataEntry) bool { + return time.Since(entry.GetLastAccessAt()) < 3*time.Hour +}