mirror of
https://github.com/project-zot/zot.git
synced 2026-06-16 04:17:55 +08:00
fix(zb): fixed remote repositories cleanup (#1461)
fix(storage/local): also put deduped blobs in cache, not just origin blobs this caused an error when trying to delete deduped blobs from multiple repositories fix(storage/s3): check blob is present in cache before deleting this is an edge case where dedupe is false but cacheDriver is not nil (because in s3 we open the cache.db if storage find it in rootDir) it caused an error when trying to delete blobs uploaded with dedupe false Signed-off-by: Petu Eusebiu <peusebiu@cisco.com>
This commit is contained in:
@@ -1045,6 +1045,13 @@ retry:
|
||||
}
|
||||
}
|
||||
|
||||
// also put dedupe blob in cache
|
||||
if err := is.cache.PutBlob(dstDigest, dst); err != nil {
|
||||
is.log.Error().Err(err).Str("blobPath", dst).Msg("dedupe: unable to insert blob record")
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Remove(src); err != nil {
|
||||
is.log.Error().Err(err).Str("src", src).Msg("dedupe: uname to remove blob")
|
||||
|
||||
|
||||
@@ -733,6 +733,48 @@ func FuzzFullBlobUpload(f *testing.F) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestStorageCacheErrors(t *testing.T) {
|
||||
Convey("get error in DedupeBlob() when cache.Put() deduped blob", t, func() {
|
||||
log := log.Logger{Logger: zerolog.New(os.Stdout)}
|
||||
metrics := monitoring.NewMetricsServer(false, log)
|
||||
|
||||
dir := t.TempDir()
|
||||
|
||||
originRepo := "dedupe1"
|
||||
dedupedRepo := "dedupe2"
|
||||
|
||||
cblob, cdigest := test.GetRandomImageConfig()
|
||||
|
||||
getBlobPath := ""
|
||||
imgStore := local.NewImageStore(dir, false, storage.DefaultGCDelay,
|
||||
true, true, log, metrics, nil, &mocks.CacheMock{
|
||||
PutBlobFn: func(digest godigest.Digest, path string) error {
|
||||
if strings.Contains(path, dedupedRepo) {
|
||||
return errCache
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
GetBlobFn: func(digest godigest.Digest) (string, error) {
|
||||
return getBlobPath, nil
|
||||
},
|
||||
})
|
||||
|
||||
err := imgStore.InitRepo(originRepo)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.InitRepo(dedupedRepo)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
_, _, err = imgStore.FullBlobUpload(originRepo, bytes.NewReader(cblob), cdigest)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
getBlobPath = strings.ReplaceAll(imgStore.BlobPath(originRepo, cdigest), imgStore.RootDir(), "")
|
||||
_, _, err = imgStore.FullBlobUpload(dedupedRepo, bytes.NewReader(cblob), cdigest)
|
||||
So(err, ShouldNotBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func FuzzDedupeBlob(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data string) {
|
||||
log := &log.Logger{Logger: zerolog.New(os.Stdout)}
|
||||
@@ -1107,8 +1149,15 @@ func TestDedupeLinks(t *testing.T) {
|
||||
UseRelPaths: true,
|
||||
}, log)
|
||||
|
||||
imgStore := local.NewImageStore(dir, false, storage.DefaultGCDelay,
|
||||
testCase.dedupe, true, log, metrics, nil, cacheDriver)
|
||||
var imgStore storage.ImageStore
|
||||
|
||||
if testCase.dedupe {
|
||||
imgStore = local.NewImageStore(dir, false, storage.DefaultGCDelay,
|
||||
testCase.dedupe, true, log, metrics, nil, cacheDriver)
|
||||
} else {
|
||||
imgStore = local.NewImageStore(dir, false, storage.DefaultGCDelay,
|
||||
testCase.dedupe, true, log, metrics, nil, nil)
|
||||
}
|
||||
|
||||
Convey(fmt.Sprintf("Dedupe %t", testCase.dedupe), t, func(c C) {
|
||||
// manifest1
|
||||
@@ -1238,6 +1287,16 @@ func TestDedupeLinks(t *testing.T) {
|
||||
So(os.SameFile(fi1, fi2), ShouldEqual, testCase.expected)
|
||||
|
||||
if !testCase.dedupe {
|
||||
Convey("delete blobs from storage/cache should work when dedupe is false", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", godigest.NewDigestFromEncoded(godigest.SHA256, blobDigest1))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", godigest.NewDigestFromEncoded(godigest.SHA256, blobDigest2))
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Intrerrupt rebuilding and restart, checking idempotency", func() {
|
||||
for i := 0; i < 10; i++ {
|
||||
taskScheduler, cancel := runAndGetScheduler()
|
||||
@@ -1358,6 +1417,16 @@ func TestDedupeLinks(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
Convey("delete blobs from storage/cache should work when dedupe is true", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", godigest.NewDigestFromEncoded(godigest.SHA256, blobDigest1))
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", godigest.NewDigestFromEncoded(godigest.SHA256, blobDigest2))
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("storage and cache inconsistency", func() {
|
||||
// delete blobs
|
||||
err = os.Remove(path.Join(dir, "dedupe1", "blobs", "sha256", blobDigest1))
|
||||
|
||||
@@ -1358,11 +1358,13 @@ func (is *ObjectStorage) DeleteBlob(repo string, digest godigest.Digest) error {
|
||||
}
|
||||
|
||||
// remove cache entry and move blob contents to the next candidate if there is any
|
||||
if err := is.cache.DeleteBlob(digest, blobPath); err != nil {
|
||||
is.log.Error().Err(err).Str("digest", digest.String()).Str("blobPath", blobPath).
|
||||
Msg("unable to remove blob path from cache")
|
||||
if ok := is.cache.HasBlob(digest, blobPath); ok {
|
||||
if err := is.cache.DeleteBlob(digest, blobPath); err != nil {
|
||||
is.log.Error().Err(err).Str("digest", digest.String()).Str("blobPath", blobPath).
|
||||
Msg("unable to remove blob path from cache")
|
||||
|
||||
return err
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// if the deleted blob is one with content
|
||||
|
||||
@@ -1400,6 +1400,16 @@ func TestS3Dedupe(t *testing.T) {
|
||||
// deduped blob should be of size 0
|
||||
So(fi2.Size(), ShouldEqual, 0)
|
||||
|
||||
Convey("delete blobs from storage/cache should work when dedupe is true", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", blobDigest1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", blobDigest2)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("Check that delete blobs moves the real content to the next contenders", func() {
|
||||
// if we delete blob1, the content should be moved to blob2
|
||||
err = imgStore.DeleteBlob("dedupe1", blobDigest1)
|
||||
@@ -1537,6 +1547,19 @@ func TestS3Dedupe(t *testing.T) {
|
||||
// the new blob with dedupe false should be equal with the origin blob from dedupe1
|
||||
So(fi1.Size(), ShouldEqual, fi3.Size())
|
||||
|
||||
Convey("delete blobs from storage/cache should work when dedupe is false", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", blobDigest1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", blobDigest2)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe3", blobDigest2)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("rebuild s3 dedupe index from true to false", func() { //nolint: dupl
|
||||
taskScheduler, cancel := runAndGetScheduler()
|
||||
|
||||
@@ -1749,6 +1772,16 @@ func TestS3Dedupe(t *testing.T) {
|
||||
// deduped blob should be of size 0
|
||||
So(fi2.Size(), ShouldEqual, 0)
|
||||
|
||||
Convey("delete blobs from storage/cache should work when dedupe is true", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", blobDigest1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", blobDigest2)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("rebuild s3 dedupe index from true to false", func() { //nolint: dupl
|
||||
taskScheduler, cancel := runAndGetScheduler()
|
||||
|
||||
@@ -1777,6 +1810,16 @@ func TestS3Dedupe(t *testing.T) {
|
||||
So(err, ShouldBeNil)
|
||||
So(len(blobContent), ShouldEqual, fi1.Size())
|
||||
|
||||
Convey("delete blobs from storage/cache should work when dedupe is false", func() {
|
||||
So(blobDigest1, ShouldEqual, blobDigest2)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe1", blobDigest1)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
err = imgStore.DeleteBlob("dedupe2", blobDigest2)
|
||||
So(err, ShouldBeNil)
|
||||
})
|
||||
|
||||
Convey("rebuild s3 dedupe index from false to true", func() {
|
||||
taskScheduler, cancel := runAndGetScheduler()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user