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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement more fine-grained mutexes in registryCache #44

Merged
merged 1 commit into from May 13, 2024

Conversation

tianon
Copy link
Member

@tianon tianon commented May 1, 2024

In short, this keeps the "data mutex" as-is, but only acquires it when we have some data to shove in (and only for the limited instructions necessary to update the data), and adds a new mutex per-tag or per-digest to prevent concurrent requests for the exact same upstream content (which is the main reason for this cache in the first place, so feels appropriate).

Without this, our current mutex means that any other efforts to parallelize will ultimately bottleneck on our registry cache.

In order to test this effectively, I've added a --parallel flag to lookup which just hyper-aggressively runs every single lookup in parallel inside a goroutine.

My first test of this was doing a lookup of the first tag of every DOI repository (so ~147 concurrent lookups in total), and I found it was consistently ~1m57s both with and without this change. Our Hub rate limiter is pegged at ~100/min, which seems consistent with that result. If I increase that limit to ~200/min, I was able to achieve a small speedup with this change (~43s down to ~30s).

In other words, for this to actually be effective as a speedup against Docker Hub if we implement parallel deploy, for example, we'll also have to increase our rate limit (which I think is fairly safe now that we handle 429 by explicitly emptying the limiter).

In order to really test this effectively, I took a different approach. I spun up a local registry (docker run -dit -p 127.0.0.1:5000:5000 -p [::1]:5000:5000 --name registry registry), and copied hello-world:linux into it 10,000 times (something like crane cp hello-world:linux localhost:5000/hello && echo localhost:5000/hello:{1..10000} | xargs -rtn1 -P$(nproc) crane cp localhost:5000/hello -- could also be done with jq and deploy if you are ambitious 馃憖).

Then I used time ./bin/lookup --parallel $(crane ls --full-ref localhost:5000/hello) > /dev/null to establish some benchmarks.

Without this change, it's pretty consistently in the 15-20s range on my local system. With this change, it drops down an order of magnitude to be in the 3-6s range.

@tianon tianon requested a review from yosifkit as a code owner May 1, 2024 23:34
In short, this keeps the "data mutex" as-is, but only acquires it when we have some data to shove in (and only for the limited instructions necessary to update the data), and adds a new mutex per-tag or per-digest to prevent concurrent requests for the exact same upstream content (which is the main reason for this cache in the first place, so feels appropriate).

Without this, our current mutex means that any other efforts to parallelize will ultimately bottleneck on our registry cache.

In order to test this effectively, I've added a `--parallel` flag to `lookup` which just hyper-aggressively runs every single lookup in parallel inside a goroutine.

My first test of this was doing a lookup of the first tag of every DOI repository (so ~147 concurrent lookups in total), and I found it was consistently ~1m57s both with and without this change.  Our Hub rate limiter is pegged at ~100/min, which seems consistent with that result.  If I increase that limit to ~200/min, I was able to achieve a small speedup with this change (~43s down to ~30s).

In other words, for this to actually be effective as a speedup against Docker Hub if we implement parallel deploy, for example, we'll *also* have to increase our rate limit (which I think is fairly safe now that we handle 429 by explicitly emptying the limiter).

In order to *really* test this effectively, I took a different approach.  I spun up a local registry (`docker run -dit -p 127.0.0.1:5000:5000 -p [::1]:5000:5000 --name registry registry`), and copied `hello-world:linux` into it 10,000 times (something like `crane cp hello-world:linux localhost:5000/hello && echo localhost:5000/hello:{1..10000} | xargs -rtn1 -P$(nproc) crane cp localhost:5000/hello` -- could also be done with `jq` and `deploy` if you are ambitious 馃憖).

Then I used `time ./bin/lookup --parallel $(crane ls --full-ref localhost:5000/hello) > /dev/null` to establish some benchmarks.

Without this change, it's pretty consistently in the 15-20s range on my local system.  With this change, it drops down an order of magnitude to be in the 3-6s range.
@tianon
Copy link
Member Author

tianon commented May 1, 2024

Just for giggles, here's the less speedy (but more fun) version of that xargs crane using our deploy: jq -nc '{type: "manifest", refs: ["localhost:5000/hello:" + (range(10000) | tostring)], lookup: { "": "hello-world:linux" }}' | ./bin/deploy

(I don't know if we'd want to implement deploy parallelization across refs in the same "push block" such that this would be faster, but I guess it's possible!)

Copy link
Collaborator

@whalelines whalelines left a comment

Choose a reason for hiding this comment

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

I'm guessing since these changes to not change externally observable behavior, the existing test cases cover these changes?

cmd/lookup/main.go Show resolved Hide resolved
cmd/lookup/main.go Show resolved Hide resolved
registry/cache.go Show resolved Hide resolved
@tianon
Copy link
Member Author

tianon commented May 6, 2024

I'm guessing since these changes to not change externally observable behavior, the existing test cases cover these changes?

Correct -- the existing coverage includes all these cases already (and makes our percentages slightly better because the implementations are now longer so the error cases are a less meaningful percentage of overall code 馃檭).

Copy link
Collaborator

@whalelines whalelines left a comment

Choose a reason for hiding this comment

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

LGTM

// TODO this does *not* need to lock the entire cache during the upstream push (but it *would* be good to block pushing to this specific digest)
rc.mu.Lock()
defer rc.mu.Unlock()
// TODO technically we should also be able to safely imply that "fromRepo" has digest here too (assuming MountBlob success), but need to double check whether the contract of the MountBlob API in OCI is such that it's legal for it to return success if "toRepo" already has "digest" (even if "fromRepo" doesn't)
Copy link
Member Author

Choose a reason for hiding this comment

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

At least for Docker Hub, we sure could assume this!

$ crane blob tianon/test@sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e > /dev/null
$ crane blob tianon/scratch@sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e > /dev/null
Error: fetching blob tianon/scratch@sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e: GET https://index.docker.io/v2/tianon/scratch/blobs/sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e: BLOB_UNKNOWN: blob unknown to registry; sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e
$ curl -H "$(crane auth token --header --push --mount tianon/scratch tianon/test)" 'https://registry-1.docker.io/v2/tianon/test/blobs/uploads/?mount=sha256:25be82253336f0b8c4347bc4ecbbcdc85d0e0f118ccf8dc2e119c0a47a0a486e&from=tianon/scratch'
{"errors":[{"code":"BLOB_UPLOAD_UNKNOWN","message":"blob upload unknown to registry","detail":"blob upload unknown"}]}

(but whether it's generally safe to assume is not guaranteed by the OCI specification, and in fact the opposite might actually be true -- from is allowed to be optional 馃槃 https://github.com/opencontainers/distribution-spec/blob/v1.1.0/spec.md#mounting-a-blob-from-another-repository)

@yosifkit yosifkit merged commit c6950c8 into docker-library:main May 13, 2024
1 check passed
@yosifkit yosifkit deleted the cache-mutexes branch May 13, 2024 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants