Skip to content

Commit

Permalink
resolved some issues
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <[email protected]>
  • Loading branch information
wangxiaoxuan273 committed Dec 28, 2023
1 parent 1212ffa commit 228d818
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
27 changes: 17 additions & 10 deletions content/oci/oci.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,12 +403,25 @@ func (s *Store) writeIndexFile() error {
}

// GC removes garbage from Store. The garbage to be cleaned are:
// - garbage blobs in the storage whose metadata is not stored in Store.
// - unreferenced (dangling) blobs in Store which have no predecessors
// - garbage blobs in the storage whose metadata is not stored in Store
func (s *Store) GC(ctx context.Context) error {
s.sync.RLock()
defer s.sync.RUnlock()
s.sync.Lock()
defer s.sync.Unlock()

// clean up dangling layers in Store
danglings := s.graph.GetDanglingLayers()
for _, desc := range danglings {
// do not remove existing manifests in the index
if _, err := s.tagResolver.Resolve(ctx, string(desc.Digest)); err == errdef.ErrNotFound {
// remove the blob and its metadata from the Store
if err := s.deleteNode(ctx, desc); err != nil {
return err
}
}
}

// clean up garbage blobs in the storage
rootpath := filepath.Join(s.root, "blobs")
return filepath.Walk(rootpath, func(path string, info fs.FileInfo, err error) error {
if err != nil {
Expand All @@ -421,8 +434,7 @@ func (s *Store) GC(ctx context.Context) error {
if err != nil {
return err
}
exists, desc := s.graph.ExistsInStore(blobDigest)
if !exists {
if exists := s.graph.Exists(blobDigest); !exists {
// remove the blob from storage if it does not exist in Store
err = os.Remove(path)
if err != nil {
Expand All @@ -431,11 +443,6 @@ func (s *Store) GC(ctx context.Context) error {
}
return err
}
} else if !s.graph.HasPredecessors(blobDigest) {
if _, err := s.tagResolver.Resolve(ctx, string(blobDigest)); err != nil {
// remove the blob and its metadata from the Store
s.deleteNode(ctx, desc)
}
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion content/oci/oci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2412,7 +2412,7 @@ func TestStore_GC(t *testing.T) {
appendBlob(ocispec.MediaTypeImageConfig, []byte("config")) // Blob 0
appendBlob(ocispec.MediaTypeImageLayer, []byte("blob")) // Blob 1
appendBlob(ocispec.MediaTypeImageLayer, []byte("dangling layer")) // Blob 2, dangling layer
generateManifest(descs[0], descs[1]) // Blob 3
generateManifest(descs[0], descs[1]) // Blob 3, valid manifest
appendBlob(ocispec.MediaTypeImageLayer, []byte("garbage layer 1")) // Blob 4, garbage layer 1
generateManifest(descs[0], descs[4]) // Blob 5, garbage manifest 1
appendBlob(ocispec.MediaTypeImageConfig, []byte("garbage config")) // Blob 6, garbage config
Expand Down
22 changes: 13 additions & 9 deletions internal/graph/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,19 @@ func (m *Memory) index(ctx context.Context, fetcher content.Fetcher, node ocispe
return successors, nil
}

// ExistsInStore returns if a blob denoted by its digest exists in the memory.
// The descriptor associated with the digest is also returned.
func (m *Memory) ExistsInStore(dgst digest.Digest) (bool, ocispec.Descriptor) {
desc, exists := m.nodes[dgst]
return exists, desc
// Exists returns if a blob denoted by its digest exists in the memory.
func (m *Memory) Exists(dgst digest.Digest) bool {
_, exists := m.nodes[dgst]
return exists
}

// HasPredecessors returns if a blob denoted by its digest has any predecessors.
func (m *Memory) HasPredecessors(dgst digest.Digest) bool {
_, hasPredecessors := m.predecessors[dgst]
return hasPredecessors
// GetDanglingLayers returns the dangling (unreferenced) layer nodes in the memory.
func (m *Memory) GetDanglingLayers() []ocispec.Descriptor {
var danglings []ocispec.Descriptor
for key, desc := range m.nodes {
if _, exist := m.predecessors[key]; !exist {
danglings = append(danglings, desc)
}
}
return danglings
}

0 comments on commit 228d818

Please sign in to comment.