Skip to content

Gcs resume write - #1322

Closed
aibaars wants to merge 8 commits into
distribution:masterfrom
aibaars:gcs-resume-write
Closed

aibaars wants to merge 8 commits into
distribution:masterfrom
aibaars:gcs-resume-write

Conversation

@aibaars

@aibaars aibaars commented Jan 6, 2016

Copy link
Copy Markdown
Contributor

This PR changes the implementation of WriteStream to use Google's resumable upload api instead of using the compose operator to join parts of uploaded content. The problem with using the compose operation is that this operation has several limitations:

  • Only 1024 parts can be joined, requiring a full rewrite (ie download and upload) of a file if that limit is exceeded
  • The operation is rate-limited (at most 1 operation every 1 or 2 seconds), requiring lots of back-off and retries if things get a bit busy.

The Resumable upload api has some limitations:

  • The size of each chunk (except the last) must be a multiple of 256KB, this is taken care of in the driver
  • The contents of an incomplete upload are not available for download
  • An upload session must be explicitly closed. After closing the uploaded contents become available, and no further contents can be appended

The second point required some small changes to the test suite, as it assume in two places that partial data is available for download. The third point is a bit of a problem as there is (as far as I know) no way of telling whether a particular call to WriteStream is the last. This is solved by adding a new function CloseStream to the storage driver interface.

@stevvooe @RichardScothern

@codecov-io

Copy link
Copy Markdown

Current coverage is 53.55%

Merging #1322 into master will decrease coverage by -4.36% as of 0e60d0b

@@            master   #1322   diff @@
======================================
  Files          123     123       
  Stmts        11156   11235    +79
  Branches       778     707    -71
  Methods          0       0       
======================================
- Hit           6461    6017   -444
+ Partial        778     707    -71
- Missed        3917    4511   +594

Review entire Coverage Diff as of 0e60d0b


Uncovered Suggestions

  1. +0.41% via ...ge/driver/gcs/gcs.go#716...761
  2. +0.29% via ...ge/driver/gcs/gcs.go#399...430
  3. +0.24% via ...ge/driver/gcs/gcs.go#218...244
  4. See 7 more...

Powered by Codecov. Updated on successful CI builds.

@stevvooe

stevvooe commented Jan 6, 2016

Copy link
Copy Markdown
Collaborator

@aibaars Thanks for the great PR!

A few concerns here:

  1. It's odd to add a new storage driver method that is stubbed out for other implementations. This API has been stable for a year so I'm a little reticent to accept an addition that is only useful for a single driver. It might be better to just make it clear that the path is not readable until ReadStream is called. We'd need to relax the requirements around reading back on an ongoing upload. See item 3 for more info.
  2. The session lookup mechanism requires repeated requests to go to the same node, meaning users of GCS either need sticky sessions or a single instance of the registry to work properly. Is there anyway to look up active sessions via API? It might be sufficient to store the session URI on GCS next to the ongoing upload.
  3. Right now, we require the ability to read back a file for an ongoing upload. This is critical for syncing hash state during an existing upload. We may be able to make the assumption that all chunk uploads are ordered by offset, reducing the requirement here. Essentially, we just need to ensure that ReadStream is not called until Commit is called and that the hash state is always updated to the offset that would be returned by the google resumable API.

If we can't make the above work, rather than using the resumable upload API, it might be better to use a logical file implementation. Each call to writestream would result in a single file that would be joined during ReadStream. If the logical file is copied, it could be assembled into a single file at that time.

Ultimately, I think we can make this work with resumable API, as the two APIs are somewhat similar (Registry V2 and google resumable upload).

@aibaars

aibaars commented Jan 6, 2016

Copy link
Copy Markdown
Contributor Author

@stevvooe Performing the close operation on the first call to ReadStream would be possible. However, this only works if it is guaranteed that ReadStream is called soon after the last call to WriteStream, otherwise the upload session might expire and the file will simply never exist. In addition the sessions take 256KB of memory each, and having many of them dangling could take quite a bit of memory.

As far as I know you cannot query the API for active sessions. Storing the sessions on GCS is a good solution. I thought about that myself, but for an initial version keeping them in memory was easier.

Good point about 3. Calling ReadStream on an incomplete file won't work and would cause a 404 error, because the file does not exist until its complete contents is committed. Would be great if the storage driver does not assume that ReadStream can be called on partial uploads.

You're right that both resumable API are fairly similar, however, it is quite tricky to make the two work together. Actually even more tricky than I expected. Hopefully you can give me a hand with this.

The current implementation using composite objects are in essence quite similar to what you describe as logical files. The problem is that composite objects have some annoying restrictions and are a bit slow to create.

@stevvooe

stevvooe commented Jan 6, 2016

Copy link
Copy Markdown
Collaborator

@aibaars It sounds like a combination of a guaranteed call to ReadStream after WriteStream and some session cleanup might make this work. Uploads are already cleaned up by the upload purger, so having a session cleanup hook in delete for child paths would be sufficient to cleanup danglers.

Would be great if the storage driver does not assume that ReadStream can be called on partial uploads.

Just give the storage package a solid read through. I think we can shore this up to ensure that no reads need to happen on unfinished files. You'll have to dig around in the storage driver unit tests to relax this, but I think it is doable.

@aibaars

aibaars commented Jan 7, 2016

Copy link
Copy Markdown
Contributor Author

@stevvooe I just realised that the blobwriter.Commit function calls storagedriver.Move. Would I be right in assuming that Move is guaranteed to be called after WriteStream? My implementation of Move uses the google equivalent of "create hard-link + delete original" and does not involve calling ReadStream so triggering on ReadStream might not work.

@aibaars

aibaars commented Jan 7, 2016

Copy link
Copy Markdown
Contributor Author

@stevvooe @RichardScothern I always wondered why the test suite insisted on filling gaps with zeros instead of complaining about offsets that are larger than the file's length. Is this to support writing chunks out-of-order, so that if a chunk with a larger offset is written first a gap is made, which is filled by a later chunk with smaller offset? GCS does not support overwriting existing data. Resumable uploads are strictly append only, and normal blobs are completely immutable, so filling in a gap later is not possible. Would it be OK for the GCS driver to return an InvalidOffset error when the offset is larger than the file's length, instead of stupidly creating a gap which can never be filled?

@aibaars
aibaars force-pushed the gcs-resume-write branch 2 times, most recently from f4e2e77 to 3d3a348 Compare January 7, 2016 16:47
@aibaars

aibaars commented Jan 7, 2016

Copy link
Copy Markdown
Contributor Author

@stevvooe I updated the PR. Now there is only one tweak to the test suite and no changes to the storage driver interface.

  1. It's odd to add a new storage driver method that is stubbed out for other implementations. This API has been stable for a year so I'm a little reticent to accept an addition that is only useful for a single driver. It might be better to just make it clear that the path is not readable until ReadStream is called. We'd need to relax the requirements around reading back on an ongoing upload. See item 3 for more info.

The upload sessions are now automatically closed on Move and ReadStream. I don't think this is a very pretty solution but it works, and does not require any changes to the storage driver interface. An alternative would be to add an extra parameter to the WriteStream function with either the content length of the file that is being uploaded (not sure if that number is know at the start of the upload) or a boolean flag indicating whether a chunk is the last one, so the driver knows whether to expect more calls to WriteStream. I think this would be a less intrusive change to the storage driver interface and does not require closing the streams in slightly surprising places.

  1. The session lookup mechanism requires repeated requests to go to the same node, meaning users of GCS either need sticky sessions or a single instance of the registry to work properly. Is there anyway to look up active sessions via API? It might be sufficient to store the session URI on GCS next to the ongoing upload.

I stored the upload sessions as files with a special content-type and the session uri and current offset as metadata. As a result they get automatically picked up by Delete so we get session cleaning for free.

I am not sure what to do exactly about point 3. My preference would be to simply disallow creating gaps.

@RichardScothern

Copy link
Copy Markdown

@aibaars : do you have numbers re: this API and the rate-limited current implementation?

I like the idea behind the change; I think it is relevant to other drivers (Azure look like it would benefit).

Comment thread registry/storage/driver/gcs/gcs.go Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Go ahead and use a const block here:

const (
    myConst = 1
    myOtherConst = 2
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah yes indeed, didn't know that was possible.

@stevvooe

stevvooe commented Jan 7, 2016

Copy link
Copy Markdown
Collaborator

@aibaars Great work!

An alternative would be to add an extra parameter to the WriteStream function with either the content length of the file that is being uploaded (not sure if that number is know at the start of the upload) or a boolean flag indicating whether a chunk is the last one, so the driver knows whether to expect more calls to WriteStream.

We actually designed the API to ensure that the client doesn't need to know the full content length before uploading content. This allows layer generation to be directly piped into the http request. The length is posted at the end of the upload and is part of the call to BlobWriter.Commit. Having a CloseStream or Commit with a content length would be a decent approach, but I wouldn't want to add this without validating all other drivers.

To fix number 3, you'll have to add or modify a test case to ensure that registry/storage.(*blobWriter) doesn't issue a ReadStream call until the commit phase. We may need to add code to ensure that offsets are append only to enforce this properly. A careful read of the resumable digest code may help with understanding this better.

@aibaars

aibaars commented Jan 7, 2016

Copy link
Copy Markdown
Contributor Author

I’ll produce some numbers tomorrow or Monday.

Good to hear you like the idea. If Azure would benefit too, then perhaps we could convince @stevvooe to accept a small extension to the storage driver interface ;-) For example an extra parameter to WriteStream indicating whether a chunk is the last, or an explicit CloseStream function, instead of having to deal with closing upload sessions in Move and ReadStream/ReadContent.

On 7 Jan 2016, at 21:52, Richard Scothern [email protected] wrote:

@aibaars : do you have numbers re: this API and the rate-limited current implementation?

I like the idea behind the change; I think it is relevant to other drivers (Azure look like it would benefit).

—
Reply to this email directly or view it on GitHub.

@stevvooe

stevvooe commented Jan 7, 2016

Copy link
Copy Markdown
Collaborator

For example an extra parameter to WriteStream indicating whether a chunk is the last, or an explicit CloseStream function, instead of having to deal with closing upload sessions in Move and ReadStream/ReadContent.

I'm coming around on this 😉. We just need to make sure the other drivers benefit from this change.

@stevvooe

stevvooe commented Jan 8, 2016

Copy link
Copy Markdown
Collaborator

cc @BrianBland

@aibaars
aibaars force-pushed the gcs-resume-write branch 4 times, most recently from 19e3e0e to 3d41086 Compare January 8, 2016 11:44
@aibaars

aibaars commented Jan 8, 2016

Copy link
Copy Markdown
Contributor Author

@RichardScothern
I made a small test that appends 1024 chunks of 256KB each and ran it with the old implementation and the resumable upload implementation of this PR.

HEAD is now at 9f83bfc... Merge pull request #1321 from aibaars/gcs-fix-retry
distribution# time go run -tags "${DOCKER_BUILDTAGS}" test.go 
count:  1023, size:     268435456
err: <nil>

real    55m48.704s
user    0m15.968s
sys 0m2.528s
HEAD is now at 3d41086... StorageDriver: GCS: implement WriteStream/CloseStream as resumable uploads
distribution# time go run -tags "${DOCKER_BUILDTAGS}" test.go 
count:  1023, size:     268435456
err: <nil>

real    4m43.813s
user    0m10.304s
sys 0m1.960s

I hadn't expected the performance difference to be over a factor 10. I guess Google Cloud Storage gradually degrades the allowed rate if you perform many of them. For example when appending only 100 chunks the difference is 69s for the old versus 33s for the implementation in this PR. A factor 2 is still a nice win though. The nice thing about the performance of the resumable upload is that it is predictable (ie 10 times more chunks takes 10 times longer).

@stevvooe

stevvooe commented Jan 8, 2016

Copy link
Copy Markdown
Collaborator

@aibaars I would think a 256KB chunk size would be a little small. Could you experiment with a larger chunksize to see how it affects performance? My guess 1MB-4MB would be optimal.

@aibaars

aibaars commented Jan 9, 2016

Copy link
Copy Markdown
Contributor Author

@stevvooe I tried with chunks of 5MB and results are similar:

HEAD is now at 9f83bfc... Merge pull request #1321 from aibaars/gcs-fix-retry
root@docker-migrate:/go/src/github.com/docker/distribution# time go run -tags "${DOCKER_BUILDTAGS}" test.go
count: 1024, size:5368709120
real    52m15.916s
user    3m12.660s
sys 0m38.000s

and

HEAD is now at 3d41086... StorageDriver: GCS: implement WriteStream/CloseStream as resumable uploads
distribution# time go run -tags "${DOCKER_BUILDTAGS}" test.go
count: 1024, size:5368709120
real    6m9.089s
user    1m17.820s
sys 0m19.600s

@aibaars
aibaars force-pushed the gcs-resume-write branch 2 times, most recently from ebc5914 to 1f48e73 Compare January 11, 2016 19:07
Comment thread registry/storage/driver/gcs/gcs.go Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This could be Sprint(s.offset), or better yet, strconv.FormatInt(s.offset, 10). It's just nice to avoid format strings and reflection when possible.

@aibaars
aibaars force-pushed the gcs-resume-write branch 6 times, most recently from f183d02 to 9954686 Compare January 15, 2016 17:58
@aibaars

aibaars commented Jan 15, 2016

Copy link
Copy Markdown
Contributor Author

@RichardScothern I improved WriteStream a bit and added comments, hopefully it is more readable now.

@aibaars
aibaars force-pushed the gcs-resume-write branch 4 times, most recently from beba424 to a94e4fe Compare January 20, 2016 12:53
@BrianBland

Copy link
Copy Markdown
Contributor

After reviewing the other storage drivers, it looks like having a CloseStream-type driver method makes sense, although maybe it should be more explicitly named CloseWriteStream or Commit. Also, restricting writes to be append-only (offset at 0 or current file size) also seems beneficial. I'm willing to rework the S3, OSS, and Azure drivers to fit this new behavior.

In alphabetical order:

  • Azure:
    • Supports Append Block which is immediately consistent, no commit necessary
  • Filesystem:
    • Probably no real benefits here
  • GCS:
    • Resumable upload API is append-only, commit needed
  • Inmemory:
    • ¯_(ツ)_/¯
  • OSS:
    • Can initiate a multipart upload and commit when done, no PutPartCopy nonsense required
  • Rados:
    • Current driver uses fixed-size chunks for reads and writes, forming logical multipart files. Maybe this would allow us to use variable-sized chunks for appends, maybe we don't need to use chunks at all? Commit seems irrelevant here
  • S3:
    • Can initiate a multipart upload and commit when done, no PutPartCopy nonsense required

@aibaars

aibaars commented Jan 21, 2016

Copy link
Copy Markdown
Contributor Author

@BrianBland thanks for reviewing the other drivers. I agree CloseWriteStream is better name.

I think it would make sense to allow a client to call WriteStream with an offset less than the current file size. In this case I would propose the semantics to be that the already content of the file is left unchanged, simply discarding the first (file_size - offset) of the chunk and only appending the remainder. This avoids having to "rewind" the digester (difficult or impossible to support). The ability to write chunks with a smaller offset make implementing the client easier. For example in case a putChunk operation fails due to network issues the data might actually have arrived on the storage but the client is unaware of that. It would be fine for the client to retry the putChunk call in an attempt to recover; there is no need for this to be considered an error. In case of "eventually consistent" storage, the client might be receive the a smaller file size when Stat is called.

I think this PR is getting a bit long, so I'll split it in two. One for the addition of CloseWriteStream and making sure no data is read from unclosed streams, and another for the Google Cloud Storage implementation of it.

…digested bytes is equal to the current file size

Signed-off-by: Arthur Baars <[email protected]>
… call ReadStream on partial uploads

Signed-off-by: Arthur Baars <[email protected]>
@aibaars

aibaars commented Feb 11, 2016

Copy link
Copy Markdown
Contributor Author

Closing this for now. Will rebase and re-open when #1395 has been implemented.

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.

7 participants