Skip to content

upload using gzip #5015

Open
Open

Description

@axkibe

Hi guys, finally finished my LFS implementation, so now comes my feature suggestion (as announced to an issue reply a while back)

Describe the issue
There is a good likelihood that binaries files compress well with gzip, and while the LFS client supports downloading the file gzip'ed (it advertises that to the objects server with the Accept-Encoding HTTP header) -- uploads are currently always done verbatim creating network traffic that could be optimized.

As far I realized, in case of the PUT in an upload there is no HTTP handshake from server to client where the former can tell the client what kind of content encoding it accepts, in case of the PUT the client sends the 'Content-Encoding' of it's choice and the server must either handle it or return an error (like 405). I suppose usually this is assumed to be agreed on between client and server in the context. So let's just do that.

Suggested solution
Since we have already a batch interface, I propose the possibility for the server in the batch answer to an upload to suggest the client other encoding it is cool with than verbatim. I see no reason not to simply reuse https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding as part of the batch answer.

In my demo implementation for a server this looks like this

resObj.actions =
{
	upload:
	{
		href: 'https://' + req.headers.host + '/objects/' + lfData.handle,
		header:
		{
			'Accept': 'application/vnd.git-lfs',
			'Accept-Encoding': 'gzip, identity',
			'Authorization': req.headers.authorization,
		}
	}
};

Reasonable requirements are likely:

  • for backward compatibility, supporting identity encoding as server is always suggested.
  • if 'Accept-Encoding' is missing in the upload answer, identity encoding is supposed to be implicit.

In the actual PUT request the client is suggested to use HTTP as per standardization in which case it can dictate the Content-Encoding (by setting respective HTTP header) which of course should follow the Accepted-Encoding of the server upload reply.

Additional context
Took me a while now to write the issue, since as previously stated I wanted to finish my LFS implementation before suggesting any improvement. It is here https://gitlab.com/csc1/gitengine similar to gitea or gitolite but in node.js instead of perl/go.

I implemented the suggestion already on server side, but couldn't test it out / didn't change the client side.

Having a standalone server in my opinion the most sensible way to store the objects is gzipped, since it likely saves disk space and the most likely down request is gzip supported download in which case I can just stream it directly. (I'm aware though more complicated cloud setups may need to store verbatim)

Activity

  1. KalleOlaviNiemitalo commented on May 20, 2022

    @KalleOlaviNiemitalo

    Should that use Accept-Encoding and Content-Encoding, or TE and Transfer-Encoding? I imagine the difference would matter for range requests.

  2. bk2204 commented on May 20, 2022

    @bk2204
    Member

    Hey,

    Thanks for this proposal. Are you suggesting that we overload the Accept-Encoding header provided by the server as an indication that it's safe to send encoded data when uploading? If so, that seems like it's not the right place to put that information.

  3. KalleOlaviNiemitalo commented on May 20, 2022

    @KalleOlaviNiemitalo

    git-lfs/docs/api/batch.md

    Lines 146 to 147 in 74c2fca

    * `header` - Optional hash of String HTTP header key/value pairs to apply
    to the request.

    Right, if the server puts 'Accept-Encoding': 'gzip, identity' in there, then that should mean the server is asking the client to add Accept-Encoding: gzip, identity to the request that the client sends to upload the file.

  4. axkibe commented on May 20, 2022

    @axkibe
    Author

    Should that use Accept-Encoding and Content-Encoding, or TE and Transfer-Encoding? I imagine the difference would matter for range requests.

    In my opinion it's obviously Accept-Encoding, because the server is doing just the same, it tells the other side a list of encodings it can handle. Content-Encoding is the actual encoding used (which the client has to set in following the PUT request).

    Hey,

    Thanks for this proposal. Are you suggesting that we overload the Accept-Encoding header provided by the server as an indication that it's safe to send encoded data when uploading? If so, that seems like it's not the right place to put that information.

    No, not overloading, it puts is as part of the JSON response to the batch API request, for simplicity I would just reuse the HTTP specification, but technically it's not part of HTTP. The client on the other hand when PUT'ing the data has to set the Content-Encoding value to the encoding it chose (or leave it empty when using verbatim like currently).

    Oh you are right, it should not be part of the header field.

    resObj.actions =
    {
    	upload:
    	{
    		href: 'https://' + req.headers.host + '/objects/' + lfData.handle,
    		header:
    		{
    			'Accept': 'application/vnd.git-lfs',
    			'Authorization': req.headers.authorization,
    		},
    		'Accept-Encoding': 'gzip, identity',
    	}
    };
    

    Sorry for the confusion, it should be part of the standard response header.

    git-lfs/docs/api/batch.md

    Lines 146 to 147 in 74c2fca

    * `header` - Optional hash of String HTTP header key/value pairs to apply
    to the request.

    Right, if the server puts 'Accept-Encoding': 'gzip, identity' in there, then that should mean the server is asking the client to add Accept-Encoding: gzip, identity to the request that the client sends to upload the file.

    You are right, it shouldn't be in the header field but one level up. Then the clients uses that info to set Content-Encoding (and encode the content accordingly. (Setting Content-Encoding in the header field wouldn't be right too, since the client still should have choice from the list provided, while setting it, would give it no freedom, and would break backward compatibility (since older clients would just set it, but not actually encode it.)

  5. KalleOlaviNiemitalo commented on May 21, 2022

    @KalleOlaviNiemitalo

    https://gitlab.com/csc1/gitengine does not appear to support https://tus.io/protocols/resumable-upload.html. Although it is not necessary to immediately implement compressed resumable uploads, I would like to have a clear plan for how it can be implemented in the future without making it unnecessarily different from basic uploads.

    The tus.io protocol spec does not mention any of Accept-Encoding, Content-Encoding, TE, or Transfer-Encoding.

    If Content-Encoding is used, then Content-Length, Upload-Length, Upload-Offset, Tus-Max-Size, and Upload-Checksum should correspond to the compressed data, not the uncompressed data. That seems feasible to implement, provided that the client has compressed the whole file in advance and doesn't try to compress it during the upload.

  6. axkibe commented on May 23, 2022

    @axkibe
    Author

    Thanks, yes to be honest I gave resumeabilty no thought so far. And yes it complicated things.

    The basic idea is to mirror the HTTP download as much as possible, since I presume, here it all is pretty much solved. This also answers the question what Content-Length should be, the length of compressed data (as it is in a GET request) or unset in case it isn't known ahead.

    Regarding Upload-Length, we don't know, since it's tus specific and they don't define it, maybe we should ask them what they think about encoded uploads.

    About resuming with Content-Encoding, in HTTP GET/Download world the answer is as far I searched: one just don't. Here resumes are done with Download Ranges and they don't work with Content-Encoding other than verbatim. However, chunked Transfer-Encoding is used here in which case you can request missing chunks. I don't have an issue to have both as possible server reply, where the server announces in its batch reply what it can do on object upload, and the client picks what it likes.

    In case of resuming gzip data, in my implementation it would be hard, as long the the offset is at or below the one received so far (since I store gzip content verbatim to disk) on the other hand I would have a harder time resuming identity uploads, since the storing immediately as gzip I would have to rerun a gzip stream through the data so far to get the compressor in the needed state to resume. (IMO for simplicity I would just write identity to a scratchpad and compress it after the upload is complete).

    However to complicate things, and this has been discussed in using stored compressed data on client side #260 and I searched here somewhat as well, the LZ compression family is no bijective -- to use mathematical terms. Meaning different compressed file input data can have the same uncompressed output. (However, most given implentations are deterministic, that is they will always produce the same compressed data for the same uncompressed input as they don't call rand() or have parallel processing race conditions. Thus to be clear to resume a compressed upload, there may be an argument to use a oid/checksum on the compressed file as well, so not to mix up different encodings. On the other hand, in practical terms, as long the client software doesn't change it's not going to change and a server could simply check if the resulting length:oid is correct after the upload finished.

    To be honest, I'm quite surprised how much we enter uncharted territory here, for me this was more of a quick and easy fix/addon. Should I open a ticked at "tus" (I guess here https://github.com/tus/tus-resumable-upload-protocol/issues ) to ask them what they opinion is on resumable content-encoding or transfer-encoding (I'm fine with both)

  7. axkibe commented on May 24, 2022

    @axkibe
    Author

    The last point is the most important to me, Upload-Token, this elegantly eliminates all discussions about non bijective compression. One client is deterministic, and as long it uses the same Token to resume an upload everything will be fine.

    Content-Length is by HTTP definition always the body length (thus in compressed case, the compressed data, and in resumed case, the remaining, compressed data).

    Upload-Length, in the case of LFS in my opinion we don't care, we got the actual uncompressed length from the batch request anyway, I'd argue for compressed length, but since clients implementations may not bother to precalculate it, tus says:

    The Upload-Length header MUST be included if the length of the final resource can be calculated at the time of the request.
    Thus if you want to follow that literally you'd have to encode the whole file ahead of the actual upload, or at least run it through an in memory gzip stream and count the length. But on the hand IMO it is not actually needed.

    Upload-Offset for any practical means would also be best in compressed data, considering in uncompressed that that could be in the bitstream putting it the middle a byte in compressed data, not practical feasible.

    So in this regard, from a practical stand point, I don't see any issues with it, treat the compressed data just like a data object in it's own right. Also I'd suggest in the batch reply to hint to the client, if the server can handle resumes, but follow the tus protocol in the actual object upload (since contrary to my mini-implementation, the object server does not have to be the batch server at the same time)

  8. bk2204 commented on May 24, 2022

    @bk2204
    Member

    You are right, it shouldn't be in the header field but one level up. Then the clients uses that info to set Content-Encoding (and encode the content accordingly. (Setting Content-Encoding in the header field wouldn't be right too, since the client still should have choice from the list provided, while setting it, would give it no freedom, and would break backward compatibility (since older clients would just set it, but not actually encode it.)

    Instead of writing Accept-Encoding: "gzip, identity" at that level, let's write encodings: ["gzip", "identity"] there. All the other keys are downcase and that would make more sense, I think.

  9. axkibe commented on May 25, 2022

    @axkibe
    Author

    Instead of writing Accept-Encoding: "gzip, identity" at that level, let's write encodings: ["gzip", "identity"] there. All the other keys are downcase and that would make more sense, I think.

    Fair enough for me. I was thinking something similar. It wouldn't be bad to have it more distinguish that way from the HTTP protocol itself, since I confused it in the wrong part above as well.

    However, there is still Content-Encoding vs. (chunked) Transfer-Encoding (which are different ways to do the same thing). While I'm totally okay with only supporting one of these, the API should think forward an object server may only offer one of these, or both to a client.

  10. bk2204 commented on May 25, 2022

    @bk2204
    Member

    I think I'd prefer that we specified that Transfer-Encoding is to be used in this case, since that's typically supported better here in this client.

  11. KalleOlaviNiemitalo commented on May 28, 2022

    @KalleOlaviNiemitalo

    br and zstd have been registered for Content-Encoding but not for Transfer-Encoding.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions