Skip to content

Update Manifest interface to distinguish between blob and manifeset references - #3402

Closed
msg555 wants to merge 3 commits into
distribution:mainfrom
msg555:msg/manifest_references
Closed

msg555 wants to merge 3 commits into
distribution:mainfrom
msg555:msg/manifest_references

Conversation

@msg555

@msg555 msg555 commented Apr 25, 2021

Copy link
Copy Markdown

Updates the Manifest and ManifestBuilder to distinguish between blob and manifest references. Uses this new interface to fix the garbage collection logic to do a proper transitive closure on references so that untagged sub manifests referenced by (tagged) manifest lists do not get removed even if remove-untagged is given to the garbage collector.

PR implementing the proposal in #3401

Resolves #3178

@msg555 msg555 changed the title Msg/manifest references Update Manifest interface to distinguish between blob and manifeset references Apr 25, 2021
@msg555

msg555 commented Apr 26, 2021

Copy link
Copy Markdown
Author

The PR size is probably quite a bit smaller if I added a IsManifest field to the Descriptor field rather than split References() into BlobReferences() and ManifestReferences() if that seems like a better approach.

@deleteriousEffect

Copy link
Copy Markdown
Member

The PR size is probably quite a bit smaller if I added a IsManifest field to the Descriptor field rather than split References() into BlobReferences() and ManifestReferences() if that seems like a better approach.

@msg555 What I would recommend is keeping References in place, changing the implementation to appending the calls to BlobReferences and ManifestReferences. That way the noise can be reduced without expanding the Descriptor and we can start moving away from References where it's appropriate in future PRs.

@msg555
msg555 force-pushed the msg/manifest_references branch from 0b5ac95 to 69c1079 Compare April 27, 2021 23:27
@msg555

msg555 commented Apr 27, 2021

Copy link
Copy Markdown
Author

Updated to not remove the existing References() and AppendReference() interface methods. Also left all uses of these methods alone except within the manifest implementation and test code and within the GC fix (should I just submit this as a follow up PR?)

@deleteriousEffect

Copy link
Copy Markdown
Member

Also left all uses of these methods alone except within the manifest implementation and test code and within the GC fix (should I just submit this as a follow up PR?)

@msg555 I would say they're so closely related that they should stick together in this single PR. I'm interested in giving this a more in-depth review now that the noise has been reduced (thanks for that!!), but it will probably take me a fair amount of time due to the complexity of garbage collection.

@msg555 msg555 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments about the implementation fwiw

Comment thread registry/storage/garbagecollect.go Resolved
Comment thread registry/storage/garbagecollect.go Resolved
Comment thread registry/storage/garbagecollect.go Resolved
Comment thread registry/storage/garbagecollect.go Resolved
return nil
})
for _, descriptor := range manifest.ManifestReferences() {
if _, ok := unmarkedManifests[descriptor.Digest]; ok {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For manifest references add it to the manifestList for processing if we haven't done so already.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we would need to go through and find any potential manifests which are referenced by tagged manifests and add them to the list of referenced manifests since we need to account for their manifest blob and their layer blobs like normal, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% on what you're asking here so apologies if I'm addressing the wrong concrn.

The manifestEnumerator.Enumerate loop at line 66 computes the initial reachable set of manifests. If RemoveUntagged is set this is only manifests that are directly tagged, otherwise it trivially just includes all manifests.

Then this loop starting at line 91 is computing the transitive closure on that initial set of manifests. Each iteration we process a reachable manifest list and mark all its blobs and manifests. If it has any manifest dependencies that have not already been marked we add it to the end of the list we're iterating over. Logically we're just executing a breadth first search.

In this way all manifests and blobs that are directly or indirectly referenced by a manifest are eventually marked.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we add it to the end of the list we're iterating over. Logically we're just executing a breadth first search.

In this way all manifests and blobs that are directly or indirectly referenced by a manifest are eventually marked.

I see this now, it's pretty elegant. Sorry for missing it at first 👍

Could you add a comment above line 111 bringing attention to the fact that manifests which are referenced by manifest lists are being appended to the end of the list so that they're processed before the loop ends?

Comment thread registry/storage/garbagecollect.go Resolved
Comment thread registry/storage/garbagecollect_test.go Resolved

@deleteriousEffect deleteriousEffect left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msg555 I went through and hit some of the high points. There's a bit of an open question as you'll see below, but while that's being resolved, I left some feedback on testing that should hold true for whatever outcome that discussion ultimately has.

Thanks for taking on this work! In general, garbage collection is one of the more complex aspects of this project.


// BlobReferences returns blob descriptors referenced by this manifest.
func (sm ManifestList) BlobReferences() []distribution.Descriptor {
return nil

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@msg555 What I'm seeing here is that distribution.Manifests either have blob references or manifest references and there is nothing that would return both (at least for now). So at least in terms of garbage collection, we're only trying to know if we're dealing with a blob referencing thing or a manifest referencing thing.

With that in mind, we expect the only things that reference manifests are OCI Image indexes and Manifest Lists. These are represented by the same concrete type, so I'm wondering if we're better off doing a cast to a manifestlist.ManifestList in the garbage collector, rather than implementing these interfaces given that there's only one concrete implementation that will return anything but nil when its ManifestReferences method is called.

This is somewhat complicated by the code here which suggests that schema 2 manifests may contain schema 1 and schema 2 manifests as references, and OCI images may contain OCI images as refernces: https://github.com/distribution/distribution/blob/main/registry/storage/schema2manifesthandler.go#L109, https://github.com/distribution/distribution/blob/main/registry/storage/ocimanifesthandler.go#L103. I've never actually seen this, but I'm curious if @justincormack and/or @SteveLasker know anything about this?

If this turns out that these manifest types can reference other manifests then we should keep these interfaces, but we'll need to correct the implementations so that they'll properly return these referenced manifests when the ManifestReferences method is called.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Part of the reason I was interested in this codebase and looking at how it handled more generic dependency graphs was because I have an interest in having my own custom manifest type that has more complex dependencies. So it's definitely my preference this is written in a generic way.

Interesting point about some other types of manifests having manifest dependencies. I agree it does look like e.g. schema2 can have references to schema1 or schema2 manifets according to the implementation of verifyManifest. However I don't see anything that explains that in https://docs.docker.com/registry/spec/manifest-v2-2/ and I'm not sure what it would mean. Maybe it's just a copy-paste from the manifest list implementation?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's just a copy-paste from the manifest list implementation?

Gosh, I would hope not 😓

Part of the reason I was interested in this codebase and looking at how it handled more generic dependency graphs was because I have an interest in having my own custom manifest type that has more complex dependencies. So it's definitely my preference this is written in a generic way.

@msg555 gotcha, I'm hesitant to include those changes along with this MR. Downstream projects use us a library and making changes to these types here needs to be preceded by an in-depth, dedicated discussion and probably shouldn't be included without a major version bump.

My feelings on this is that we should split this work up by:

  1. keeping the garbage collection related changes in this MR, using type casting to discover the manifest lists, and updating the title to fit this, as the GC removing manifests removed by a manifest list is a serious bug
  2. You opening an issue to discuss the generic dependency graphs and discuss your use case and implementation
  3. Me opening an issue to investigate the mysterious validation code

WDYT?

return nil
})
for _, descriptor := range manifest.ManifestReferences() {
if _, ok := unmarkedManifests[descriptor.Digest]; ok {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like we would need to go through and find any potential manifests which are referenced by tagged manifests and add them to the list of referenced manifests since we need to account for their manifest blob and their layer blobs like normal, right?

}

unmarkedManifests := make(map[digest.Digest]struct{})
var manifestList []digest.Digest

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think could probably be called referencedManifests or so, right now the name isn't very descriptive, it collides with the package/type manifestList and in general, it's good to avoid naming variables after their types

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed


// Create random layers
randomLayers1, err := testutil.CreateRandomLayers(3)
randomLayers, err := testutil.CreateRandomLayers(12)

@deleteriousEffect deleteriousEffect May 5, 2021 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than expanding this test, can we leave it as is and write new tests? I think we'd need new tests which ensure at least the following are covered:

  • untagged manifests which are referenced by a tagged manifest list are kept when RemoveUntagged:true
    • blobs which are uniquely referenced by an untagged manifest which are referenced by a tagged manifest list are kept when RemoveUntagged:true
  • untagged manifests which are referenced by an untagged manifest list are removed when RemoveUntagged:true
    • blobs which are uniquely referenced by an untagged manifest which are referenced by an untagged manifest list are removed when RemoveUntagged:true
  • tagged manifests which are referenced by an untagged manifest list are kept when RemoveUntagged:true

Also, we should use schema 2 manifests and manifest lists in these tests. Schema 1 is very deprecated and isn't realistic in the scenario. testutil has functions for schema 2 manifests and manifest lists already.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we need to try to ensure that there are no ordering issues: ensuring that an untagged manifest referenced by a tagged manifest list is handled correctly regardless of whether it is encountered by the manifest enumerator before or after the manifest list which references it.

For example, we need to be sure to avoid deleting a manifest (or not marking its blobs, ect), only later to find it was referenced by another manifest list that was tagged.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed we should make those updates to the tests.

As far as ordering goes is there some easy way I can control what order manifests are enumerated?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as ordering goes is there some easy way I can control what order manifests are enumerated?

Not really 😅

I think as long as we provide several randomly created lists with randomly created manifests per test we'll have this covered, otherwise the test will flake, or maybe even only occasionally pass (reverse-flake?). It's not perfect, but it should get caught relatively soon.

To test for this every time always, you'd need to manually build out a list and manifests that happen to produce digests that lexicographically sort before and after the lists digests. It's a little extra to be honest, but it's fine if you chose to have a test like this or not.

@SteveLasker

Copy link
Copy Markdown
Collaborator

The distinction between blobs and manifests is pretty key for many scenarios.

To grab some eyes, folks MAY, 😃 want to review opencontainers/artifacts#29 for some work on enabling Reference Types, supporting secure supply chain efforts like SBoMs and signatures.

We have a branch of CNCF Distribtuion that works against a slightly older version of PR29 here: https://github.com/notaryproject/distribution/tree/prototype-2

Docs for the layout are described here: https://github.com/notaryproject/distribution/blob/prototype-2/docs/reference-types.md

And, an ORAS implementation that demonstrates how to push, discover, pull reference types

Apologies if this spams folks just trying to get this PR completed.

Comment thread registry/storage/garbagecollect.go Resolved
return nil
})
for _, descriptor := range manifest.ManifestReferences() {
if _, ok := unmarkedManifests[descriptor.Digest]; ok {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we add it to the end of the list we're iterating over. Logically we're just executing a breadth first search.

In this way all manifests and blobs that are directly or indirectly referenced by a manifest are eventually marked.

I see this now, it's pretty elegant. Sorry for missing it at first 👍

Could you add a comment above line 111 bringing attention to the fact that manifests which are referenced by manifest lists are being appended to the end of the list so that they're processed before the loop ends?


// Create random layers
randomLayers1, err := testutil.CreateRandomLayers(3)
randomLayers, err := testutil.CreateRandomLayers(12)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as ordering goes is there some easy way I can control what order manifests are enumerated?

Not really 😅

I think as long as we provide several randomly created lists with randomly created manifests per test we'll have this covered, otherwise the test will flake, or maybe even only occasionally pass (reverse-flake?). It's not perfect, but it should get caught relatively soon.

To test for this every time always, you'd need to manually build out a list and manifests that happen to produce digests that lexicographically sort before and after the lists digests. It's a little extra to be honest, but it's fine if you chose to have a test like this or not.

@deleteriousEffect

Copy link
Copy Markdown
Member

The distinction between blobs and manifests is pretty key for many scenarios.

To grab some eyes, folks MAY, smiley want to review opencontainers/artifacts#29 for some work on enabling Reference Types, supporting secure supply chain efforts like SBoMs and signatures.

We have a branch of CNCF Distribtuion that works against a slightly older version of PR29 here: https://github.com/notaryproject/distribution/tree/prototype-2

Docs for the layout are described here: https://github.com/notaryproject/distribution/blob/prototype-2/docs/reference-types.md

And, an ORAS implementation that demonstrates how to push, discover, pull reference types

Apologies if this spams folks just trying to get this PR completed.

@SteveLasker good points, I think we should break up the work: #3402 (comment)

@deleteriousEffect

deleteriousEffect commented May 25, 2021 •

Copy link
Copy Markdown
Member

@milosgajdos do you these changes could/should make it into the 2.8 milestone?

@msg555

msg555 commented May 19, 2024

Copy link
Copy Markdown
Author

Closing as this is pretty old by now. No idea if the original issue is still a thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Registry garbage-collect --delete-untagged removes multi-arch manifests

4 participants