Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chunked: fix reuse of the layers cache #2024

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

giuseppe
Copy link
Member

the global singleton was never updated, causing the cache to be always recreated for each layer.

It is not possible to keep the layersCache mutex for the entire load() since it calls into some store APIs causing a deadlock since findDigestInternal() is already called while some store locks are held.

Closes: #2023

the global singleton was never updated, causing the cache to be always
recreated for each layer.

It is not possible to keep the layersCache mutex for the entire load()
since it calls into some store APIs causing a deadlock since
findDigestInternal() is already called while some store locks are
held.

Closes: containers#2023

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
Copy link
Contributor

openshift-ci bot commented Jul 12, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: giuseppe

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@rhatdan
Copy link
Member

rhatdan commented Jul 13, 2024

LGTM
@mtrmac PTAL

@giuseppe giuseppe marked this pull request as ready for review July 15, 2024 11:45
@giuseppe giuseppe changed the title [WIP] chunked: fix reuse of the layers cache chunked: fix reuse of the layers cache Jul 15, 2024
Copy link
Collaborator

@mtrmac mtrmac left a comment

Choose a reason for hiding this comment

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

Thanks for handling this!

A fairly brief skim for now…


(It would be also convenient to have the locking rules documented in detail — e.g. which fields of layersCache and layers are immutable; which are protected by a lock (and which one).

Especially the lock hierarchy of layersCache.mutex vs. the locks of store/layerStore seems fairly non-obvious to me, given how far the code locations are from each other and how dissimilar the situation seems.

But all of that is blocked on having settled on design in the first place.)

@@ -111,7 +111,7 @@ func getLayersCacheRef(store storage.Store) *layersCache {
cache.refs++
return cache
}
cache := &layersCache{
cache = &layersCache{
Copy link
Collaborator

Choose a reason for hiding this comment

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

(I wonder about making this kind of mistake harder to do … naming the global cacheSingleton, globalCache, or something, might make it less likely to conflict with a local. OTOH that’s very likely an overreaction.)

@@ -300,7 +300,9 @@ func (c *layersCache) load() error {
for _, l := range loadedLayers {
l.release()
Copy link
Collaborator

Choose a reason for hiding this comment

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

AFAICS multiple load operations can now be happening in parallel; in that case this might release the same layer multiple times, concurrently.

(I have no opinion on an alternative design at this point, I didn’t look for one yet.)

Copy link
Member Author

Choose a reason for hiding this comment

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

could we just drop the release() here and let the GC deal with it? We are already using runtime.SetFinalizer()

Copy link
Collaborator

@mtrmac mtrmac Jul 24, 2024

Choose a reason for hiding this comment

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

That would probably work… intuitively I don’t like relying on the GC that way, but I don’t have much of an objective reason (at best, that the time of release, and total memory consumption, are hard to manage).


It’s not quite sufficient, though, especially for a single-operation command like podman pull. Normally, c/image would trigger a pull of up to 6 layers in parallel, all of those would ~concurrently trigger layersCache.load, and at least read/map all those cache files into memory, or in the worst case create them from scratch, up to 6 times in parallel.

So before this PR we build/load the cache for every pulled layer; with this PR as is, we build/load the cache for up to 6 concurrent layers (and after the first batch of layers, all future cache uses are only the metadata check, and/or update for the just-added layers). This PR is an improvement but not fully where we want to be.

Ideally, I think, the cache loading / update, at least in the case where we do some I/O, should be serialized.

Would two locks work? Nested “exclusive layer loading lock” → c/storage store+layer locks → “the current `layersCache.mutex”? It seems to me that that should be possible, but I didn’t try and I didn’t check very hard.


Very vaguely, the current reference count vs. per-cache mutex vs. loading state seems badly structured, or at least it’s unclear to me why it is so. getLayersCacheRef is already serialized and it knows that it needs the cache EDIT reloaded because it is old. But then getLayersCache triggers a load (and a reconciliation against store.Layers) every time. Do we even need the timer there? Do we need the concurrent existence of multiple layersCache objects, with each chunkedDiffer keeping a reference to a particular cache version?

I’m not saying it’s obviously wrong, and for all I know it might be the best design or the only possible design, but nothing in the code writes down why it is so, and it just … feels … off.

@TomSweeneyRedHat
Copy link
Member

@giuseppe @mtrmac this too looks like a good one to get in for the vendor dance. Would it be possible to wrap this up in the next day or so, or should we consider it for the 1.55.1 release (1.55.0 is the vendor)?

@TomSweeneyRedHat TomSweeneyRedHat added the 5.2 Wanted for Podman v5.2 label Jul 24, 2024
@TomSweeneyRedHat
Copy link
Member

If this is merged on or before August 12, 2024, please cherry-pick this to the release-1.55 branch

@cgwalters
Copy link
Contributor

@giuseppe @mtrmac this too looks like a good one to get in for the vendor dance.

This is just my opinion but basically this is attempting to optimize zstd:chunked some, and introduces some open questions around concurrency. It doesn't make sense to rush it, and if it merged it's the type of change that should only be cherry picked if there was a known reason to do so.

@cgwalters cgwalters removed the 5.2 Wanted for Podman v5.2 label Jul 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

chunked layersCache seems to never be actually reused
5 participants