Repository navigation
storage: serve filesystem blobs directly from the file - #5081
Conversation
|
A note on error response bodies, related to #772. With this patch, a blob file that exists but cannot be opened leads to a response with status code 500. The body carries the storage driver's error text, which includes the storage path. That is how the handlers report storage errors elsewhere, and it may be a security problem. This was reported in #772. An earlier version of this patch stripped the error text out of the response. I removed that again: it changed the error body for every storage driver along this code path. I think #772 is better handled in its own change, for all handlers at once. |
448475e to
910dd4f
Compare
|
For the record, I've published the exact code used for measuring the perf impact of this patch here: https://github.com/jgehrcke/sendfile-perf-repro/tree/main/distribution |
milosgajdos
left a comment
There was a problem hiding this comment.
The logic here looks good to me and we are defo addressing an issue, BUT the implementation is not ideal.
Specifically, defining that const filesystemDriverName = "filesystem" is a codesmell in the storage package.
blobServer should ask what the driver can do, rather than infer it from its name...I think I’d prefer a small optional capability interface, conceptually:
type seekableReaderDriver interface {
ReadSeeker(ctx context.Context, path string) (io.ReadSeekCloser, error)
}The blob server needs a seekable reader whose size comes from the content. Letting drivers explicitly provide that would keep this package driver-agnostic. The filesystem driver could return its *os.File through that interface.
|
Thank you for taking the time to look at this @milosgajdos! Will adjust the patch. |
This patch fixes two defects in blob GET requests on the filesystem driver and significantly reduces the CPU time that the registry consumes for serving them. It passes the blob file directly to `http.ServeContent`. This patch was extracted from distribution#5052. That PR builds on this patch to enable sendfile(2), which requires passing an `*os.File` to `http.ServeContent`. Performance impact ------------------ With a warm page cache, this patch reduces registry total CPU time by 13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per range GET. Before this patch, `fileReader` allocated a buffer of up to 4 MiB per GET and copied blobs larger than 32 KiB a second time. Now `ServeBlob` passes `*os.File` directly to `http.ServeContent`. Registry total CPU time per GET in ms (mean +/- sample SD), warm page cache, linux/arm64: Transport Blob Before (n=15) Now (n=15) Change plain HTTP 4 MiB 1.994 +/- 0.026 1.160 +/- 0.009 -41.8% plain HTTP 32 MiB 9.423 +/- 0.061 6.851 +/- 0.021 -27.3% TLS 4 MiB 3.401 +/- 0.027 2.404 +/- 0.018 -29.3% TLS 32 MiB 20.159 +/- 0.063 17.528 +/- 0.113 -13.0% The 4 MiB buffer dates from February 2015 (0270bec). That commit message records no measurement or specific reasoning. Does removing this buffer hurt? Would a smaller buffer be better? With a warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f) used the same or more registry total CPU time than this patch. With a cold page cache, removing the buffer has a cost that is small compared to the aforementioned gains (registry total CPU time was 2% to 7% higher for 1 GiB blobs, local NVMe drive). Fixed ----- The registry now opens the blob file before it sets the response status, and sets Content-Length from the file size instead of the blob descriptor. Passing the file to `http.ServeContent` requires both changes. Each change fixes one defect (distribution#5080): - The blob file is missing or cannot be opened. Before: The registry sent status 200, a Content-Length header and no body (missing file: issues distribution#5010 and distribution#2966). Now: The registry sends status 404 with error code BLOB_UNKNOWN for a missing file. It sends status 500 for other open errors. - The blob file size is smaller than the size stored in the blob descriptor. Before: The registry set Content-Length from the size in the blob descriptor and sent a truncated body (curl, for example, reports error 18). Now: The registry sets Content-Length from the file size. Before this patch, skopeo for example would log "unexpected EOF" and retry in both cases. Remarks ------- For blob GETs, the filesystem driver opts in through `ReadSeeker` and returns the original `*os.File`. Drivers and middleware without `ReadSeeker` use `fileReader` and its buffer (capped at blob size since distribution#5070). If the blob file is missing, the registry now answers every GET with status 404. Previously, it answered some conditional GETs with status 304 or 412. The blob file is authoritative, not the blob descriptor, which can be a stale entry in the blob descriptor cache. In a pull-through cache, the registry now answers a GET with status 404 when the local blob file is missing and its blob descriptor is still cached. The registry does not fetch the blob from the upstream again. Fetching it again is what distribution#5010 prefers, and is left for a follow-up. The registry still sets Docker-Content-Digest and ETag from the blob descriptor. When the blob file is smaller or larger than the size in the blob descriptor, the registry now sends the whole file, and the transfer completes. Only a client that checks size or digest detects the mismatch. Tests ----- The stale-size and missing-file tests cover the two GET fixes. `TestBlobServerDriverWithoutReadSeeker` checks that a range GET through a filesystem wrapper makes one `Reader` call at the requested offset. Related issues: distribution#2367, distribution#5010, distribution#2966, distribution#2778. Related PR: distribution#5008. Fixes distribution#5080 Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
910dd4f to
0ecd45f
Compare
|
Updated the commit according to your feedback (also squashed, rebased on HEAD of main). The changes compared to the version you previously commented on are actually rather tiny. One commit for addressing the expected merge conflict with |
|
Thank you @milosgajdos for bearing with these nitty-gritty changes, and for pushing this along. |
storage: serve filesystem blobs directly from the file
This patch fixes two defects in blob GET requests on the filesystem
driver and significantly reduces the CPU time that the registry consumes
for serving them. It passes the blob file directly to
http.ServeContent.This patch was extracted from #5052. That PR builds on this patch to
enable sendfile(2), which requires passing an
*os.Filetohttp.ServeContent.Performance impact
With a warm page cache, this patch reduces registry total CPU time by
13% to 42% per whole-blob GET (100 KiB to 32 MiB) and by 9% to 58% per
range GET.
Before this patch,
fileReaderallocated a buffer of up to 4 MiB perGET and copied blobs larger than 32 KiB a second time. Now
ServeBlobpasses
*os.Filedirectly tohttp.ServeContent.Registry total CPU time per GET in ms (mean +/- sample SD), warm page
cache, linux/arm64:
The 4 MiB buffer dates from February 2015 (0270bec). That commit
message records no measurement or specific reasoning.
Does removing this buffer hurt? Would a smaller buffer be better? With a
warm page cache, no: buffers from 32 KiB to 1 MiB (tested on d14ff2f)
used the same or more registry total CPU time than this patch. With a
cold page cache, removing the buffer has a cost that is small compared
to the aforementioned gains (registry total CPU time was 2% to 7% higher
for 1 GiB blobs, local NVMe drive).
Fixed
The registry now opens the blob file before it sets the response
status, and sets Content-Length from the file size instead of the blob
descriptor. Passing the file to
http.ServeContentrequires bothchanges. Each change fixes one defect (#5080):
Before: The registry sent status 200, a Content-Length header and no
body (missing file: issues Garbage collection and blobdescriptor cache do not coordinate blob cleanup #5010 and Zero byte file (layer) returned after GC when running with InMemory cache #2966).
Now: The registry sends status 404 with error code BLOB_UNKNOWN for a
missing file. It sends status 500 for other open errors.
descriptor.
Before: The registry set Content-Length from the size in the blob
descriptor and sent a truncated body (curl, for example, reports
error 18).
Now: The registry sets Content-Length from the file size.
Before this patch, skopeo for example would log "unexpected EOF" and
retry in both cases.
Remarks
This patch applies only to the filesystem driver, when its
Readerreturns an
*os.File. For S3, Azure, GCS and all other storage drivers,the registry still uses
fileReaderand its buffer (capped at blob sizesince #5070). HEAD responses are unchanged (see #5082 for a
follow-up).
If the blob file is missing, the registry now answers every GET with
status 404. Previously, it answered some conditional GETs with status
304 or 412. The blob file is authoritative, not the blob descriptor,
which can be a stale entry in the blob descriptor cache.
In a pull-through cache, the registry now answers a GET with status 404
when the local blob file is missing and its blob descriptor is still
cached. The registry does not fetch the blob from the upstream again.
Fetching it again is what #5010 prefers, and is left for a follow-up.
The registry still sets Docker-Content-Digest and ETag from the blob
descriptor. When the blob file is smaller or larger than the size in the
blob descriptor, the registry now sends the whole file, and the transfer
completes. Only a client that checks size or digest detects the
mismatch.
Tests
The new tests fail without the changes to
blobserver.goandblob.go,except
TestBlobServerOtherDriverReaderCalls, which covers what thispatch does not change: one
Readercall per GET on other drivers.Related issues: #2367, #5010, #2966, #2778. Related PR: #5008.
Fixes #5080