mirror of
https://github.com/project-zot/zot.git
synced 2026-06-17 04:48:26 +08:00
fix periodic background tasks - gc and scrub
Signed-off-by: Andreea-Lupu <andreealupu1470@yahoo.com>
This commit is contained in:
committed by
Ramkumar Chinchani
parent
d0b52612a2
commit
081ba0b2f2
@@ -1117,7 +1117,7 @@ retry:
|
||||
return nil
|
||||
}
|
||||
|
||||
func (is *ObjectStorage) RunGCPeriodically(gcInterval time.Duration) {
|
||||
func (is *ObjectStorage) RunGCRepo(repo string) {
|
||||
}
|
||||
|
||||
// DeleteBlobUpload deletes an existing blob upload that is currently in progress.
|
||||
|
||||
@@ -73,7 +73,7 @@ func CheckImageStoreBlobsIntegrity(imgStore ImageStore) ([]ScrubImageResult, err
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
imageResults, err := checkRepo(repo, imgStore)
|
||||
imageResults, err := CheckRepo(repo, imgStore)
|
||||
if err != nil {
|
||||
return results, err
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func CheckImageStoreBlobsIntegrity(imgStore ImageStore) ([]ScrubImageResult, err
|
||||
return results, nil
|
||||
}
|
||||
|
||||
func checkRepo(imageName string, imgStore ImageStore) ([]ScrubImageResult, error) {
|
||||
func CheckRepo(imageName string, imgStore ImageStore) ([]ScrubImageResult, error) {
|
||||
results := []ScrubImageResult{}
|
||||
|
||||
dir := path.Join(imgStore.RootDir(), imageName)
|
||||
|
||||
@@ -44,5 +44,5 @@ type ImageStore interface {
|
||||
GetIndexContent(repo string) ([]byte, error)
|
||||
GetBlobContent(repo, digest string) ([]byte, error)
|
||||
GetReferrers(repo, digest string, mediaType string) ([]artifactspec.Descriptor, error)
|
||||
RunGCPeriodically(gcInterval time.Duration)
|
||||
RunGCRepo(repo string)
|
||||
}
|
||||
|
||||
+18
-34
@@ -1647,47 +1647,31 @@ func DirExists(d string) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func gcAllRepos(imgStore *ImageStoreFS) error {
|
||||
repos, err := imgStore.GetRepositories()
|
||||
func (is *ImageStoreFS) gcRepo(repo string) error {
|
||||
dir := path.Join(is.RootDir(), repo)
|
||||
|
||||
var lockLatency time.Time
|
||||
|
||||
is.Lock(&lockLatency)
|
||||
|
||||
err := is.garbageCollect(dir, repo)
|
||||
|
||||
is.Unlock(&lockLatency)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, repo := range repos {
|
||||
dir := path.Join(imgStore.RootDir(), repo)
|
||||
|
||||
var lockLatency time.Time
|
||||
|
||||
imgStore.Lock(&lockLatency)
|
||||
|
||||
err := imgStore.garbageCollect(dir, repo)
|
||||
|
||||
imgStore.Unlock(&lockLatency)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (is *ImageStoreFS) RunGCPeriodically(gcInterval time.Duration) {
|
||||
go func() {
|
||||
for {
|
||||
execMessage := fmt.Sprintf("executing GC of orphaned blobs for %s", is.RootDir())
|
||||
is.log.Info().Msg(execMessage)
|
||||
func (is *ImageStoreFS) RunGCRepo(repo string) {
|
||||
is.log.Info().Msg(fmt.Sprintf("executing GC of orphaned blobs for %s", path.Join(is.RootDir(), repo)))
|
||||
|
||||
err := gcAllRepos(is)
|
||||
if err != nil {
|
||||
errMessage := fmt.Sprintf("error while running GC for %s", is.RootDir())
|
||||
is.log.Error().Err(err).Msg(errMessage)
|
||||
}
|
||||
if err := is.gcRepo(repo); err != nil {
|
||||
errMessage := fmt.Sprintf("error while running GC for %s", path.Join(is.RootDir(), repo))
|
||||
is.log.Error().Err(err).Msg(errMessage)
|
||||
}
|
||||
|
||||
completedMessage := fmt.Sprintf("GC completed for %s, next GC scheduled after", is.RootDir())
|
||||
is.log.Info().Str(completedMessage, gcInterval.String()).Msg("")
|
||||
|
||||
time.Sleep(gcInterval)
|
||||
}
|
||||
}()
|
||||
is.log.Info().Msg(fmt.Sprintf("GC completed for %s", path.Join(is.RootDir(), repo)))
|
||||
}
|
||||
|
||||
@@ -1090,7 +1090,7 @@ func TestGarbageCollect(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGarbageCollectForImageStore(t *testing.T) {
|
||||
Convey("Garbage collect for all repos from an ImageStore", t, func(c C) {
|
||||
Convey("Garbage collect for a specific repo from an ImageStore", t, func(c C) {
|
||||
dir := t.TempDir()
|
||||
|
||||
Convey("Garbage collect error for repo with config removed", func() {
|
||||
@@ -1115,13 +1115,14 @@ func TestGarbageCollectForImageStore(t *testing.T) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
imgStore.RunGCPeriodically(24 * time.Hour)
|
||||
imgStore.RunGCRepo(repoName)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
data, err := os.ReadFile(logFile.Name())
|
||||
So(err, ShouldBeNil)
|
||||
So(string(data), ShouldContainSubstring, fmt.Sprintf("error while running GC for %s", imgStore.RootDir()))
|
||||
So(string(data), ShouldContainSubstring,
|
||||
fmt.Sprintf("error while running GC for %s", path.Join(imgStore.RootDir(), repoName)))
|
||||
})
|
||||
|
||||
Convey("Garbage collect error - not enough permissions to access index.json", func() {
|
||||
@@ -1141,13 +1142,14 @@ func TestGarbageCollectForImageStore(t *testing.T) {
|
||||
|
||||
So(os.Chmod(path.Join(dir, repoName, "index.json"), 0o000), ShouldBeNil)
|
||||
|
||||
imgStore.RunGCPeriodically(24 * time.Hour)
|
||||
imgStore.RunGCRepo(repoName)
|
||||
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
data, err := os.ReadFile(logFile.Name())
|
||||
So(err, ShouldBeNil)
|
||||
So(string(data), ShouldContainSubstring, fmt.Sprintf("error while running GC for %s", imgStore.RootDir()))
|
||||
So(string(data), ShouldContainSubstring,
|
||||
fmt.Sprintf("error while running GC for %s", path.Join(imgStore.RootDir(), repoName)))
|
||||
So(os.Chmod(path.Join(dir, repoName, "index.json"), 0o755), ShouldBeNil)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user