Repository navigation
Description
After discussions in #1322 and #1382, it seems like we need to add some sort of method for explicitly closing/committing a write on the storage backend. We also should ensure that writing to a storage driver is compatible with resumable digests for the purpose of resumable uploads.
To facilitate digested writes to the storage backend, we currently construct a FileWriter wrapping a StorageDriver, with a given fixed path. We then call io.Copy to copy the data through a digester into this FileWriter, which ends up calling FileWriter.ReadFrom as defined by io.Copy.
The current StorageDriver.WriteStream method takes an offset, a reader, and returns the number of bytes read from the reader. If the number of bytes read is ever different from the number of bytes written to the storage backend (a short write occurs), we are left with a resumable digest state that is inconsistent with the stored file state, and could thus only resume from a previously known consistent position.
It's more idiomatic go and allows greater flexibility to the driver writer/maintainer to have the storage driver return an io.Writer for a given path. This writer can manage its own internal buffer and flushing logic, allowing for a much cleaner interface for writing and copying data into the storage backend. We also need a Close method, which must be called when current writing is finished, which will flush the written data to the storage backend. Because we want to support resumable writes without them being unnecessarily expensive on the backend, we need one more method on this writer to commit the data to the backend into a readable state. For instance, we can use io.Copy's default behavior to write 32KB chunks to an S3 driver's writer and buffer up chunks matching the user-defined chunksize parameter before writing these to various parts in a multipart upload, which is then synced to the backend storage on the call to Close. Later on, we can open up a new writer at the last written offset, using the same multipart upload, copy in some more data, and finally call Commit to complete the multipart upload and create the object in S3.
To ensure that we have a consistent digest state with what is written to the storage backend, we can issue all of our writes to an io.MultiWriter containing the storage driver's writer and the digesting hash function, saving the resumable digest state alongside the number of bytes written only on a successful call to the writer's Close or Commit function.
For interface consistency, I'd like to name this new method StorageDriver.Writer and rename StorageDriver.ReadStream to StorageDriver.Reader.
The full amended StorageDriver interface as proposed is as follows:
type StorageDriver interface {
// Name returns the human-readable "name" of the driver, useful in error
// messages and logging. By convention, this will just be the registration
// name, but drivers may provide other information here.
Name() string
// GetContent retrieves the content stored at "path" as a []byte.
// This should primarily be used for small objects.
GetContent(ctx context.Context, path string) ([]byte, error)
// PutContent stores the []byte content at a location designated by "path".
// This should primarily be used for small objects.
PutContent(ctx context.Context, path string, content []byte) error
// Reader retrieves an io.ReadCloser for the content stored at "path"
// with a given byte offset.
// May be used to resume reading a stream by providing a nonzero offset.
Reader(ctx context.Context, path string, offset int64) (io.ReadCloser, error)
// Writer returns a FileWriter which will store the content written to it
// at the location designated by "path" after the call to Commit.
Writer(ctx context.Context, path string, append bool) (FileWriter, error)
// Stat retrieves the FileInfo for the given path, including the current
// size in bytes and the creation time.
Stat(ctx context.Context, path string) (FileInfo, error)
// List returns a list of the objects that are direct descendants of the
//given path.
List(ctx context.Context, path string) ([]string, error)
// Move moves an object stored at sourcePath to destPath, removing the
// original object.
// Note: This may be no more efficient than a copy followed by a delete for
// many implementations.
Move(ctx context.Context, sourcePath string, destPath string) error
// Delete recursively deletes all objects stored at "path" and its subpaths.
Delete(ctx context.Context, path string) error
// URLFor returns a URL which may be used to retrieve the content stored at
// the given path, possibly using the given options.
// May return an ErrUnsupportedMethod in certain StorageDriver
// implementations.
URLFor(ctx context.Context, path string, options map[string]interface{}) (string, error)
}
// FileWriter provides an abstraction for an opened writable file-like object in
// the storage backend. The FileWriter must flush all content written to it on
// the call to Close, but is only required to make its content readable on a
// call to Commit.
type FileWriter interface {
io.WriteCloser
// Size returns the number of bytes written to this FileWriter.
Size() int64
// Cancel removes any written content from this FileWriter.
Cancel() error
// Commit flushes all content written to this FileWriter and makes it
// available for future calls to StorageDriver.GetContent and
// StorageDriver.Reader.
Commit() error
}I have one open question in this proposal, and that is the behavior of the StorageDriver.Stat method with respect to partially-written files. It is currently not possible with the way the registry uses the storage backend, but if a file were to exist at path abc and another partial write at path abc, would Stat(abc) return the size of the complete file, the size of the partial file, some offset less than the size of the partial file where it may be resumed, or would the partial file mask the complete file and instead return a PathNotFoundError? Given that this is unlikely to be relevant to the actual registry code, I'm leaving this open to discussion but without a high degree of urgency.
Activity
dmp42 commentedon Jan 27, 2016
cc @docker/distribution-maintainers
aaronlehmann commentedon Jan 27, 2016
This mostly looks good to me, per our discussion Monday evening.
I'm wondering if
Commitdoesn't belong in theFileWriterinterface. It sounds likeCommitmay combine the data written by multipleFileWriters. Perhaps there needs to be an intermediate data type to manage a multipart upload session.In general, we need to be careful about buffering. It would be bad to make small writes to the backend storage if those writes are expensive. But it would also be bad to wait until we accumulate a significant amount of data before we start handing it off to the backend storage. I think it's a good idea to give the storage driver control over buffering like you suggest. We can tune the behavior based on benchmarks.
stevvooe commentedon Jan 27, 2016
Nice proposal. This covers the consistency issues present in the
WriteStreammethod.This is somewhat a question of consistency. Do we want writers to see the partially written data or only see committed data? As it is
Statis a little expensive for its purpose. Ideally, it would provide file sizing, types and timestamps of committed data. Looking at it from this perspective, perhaps we add anSize()method toFileWriterto make the role ofStatclear in providing information about committed writes.aibaars commentedon Jan 29, 2016
This looks really good. In the PRs #1322 and #1382, I tried to implement GCS resumable uploads, with as little as possible changes to the storage driver interface. I hadn't expected that you are willing to do such a big change. Thanks!
I think the
Movefunction can be removed, this could be taken care of byCommit. For the GCS driver it would be easiest if the FileWriter would know the destination path of the blob, instead of the temporary upload path. The GCS resumable upload api already has atomic behaviour. This would save a Copy and Delete operation after each upload.Would it make sense to add the following methods to
FileWriter:func SaveState() []bytefunc RestoreState(data []byte)This would give the storage driver implementors a way of storing a bit of extra data that allows them to recover the state of the FileWriter (e.g. for GCS the this could include the upload-session-url, the current size of the upload, and perhaps some other metadata) .
What is the purpose of the
FileWriter.Close()function? Is it meant to be called once at the end of the upload, or after every chunk?BrianBland commentedon Feb 2, 2016
@aibaars unfortunately I don't see a way to remove the
Movefunction from the driver interface. Specifically, we don't know the destination of the blob until it has been fully received, as we store it based on its digest and cannot blindly trust the client.The intent is currently that the
FileWriterreturned fromStorageDriver.Writershould already contain the upload session uri, as a caller of theStorageDrivermethods should not know about this driver-specific information. This could be detected on the call toWriterby checking existing multipart/resumable uploads.FileWriter.Close()is intended to be used like any otherio.ClosersClosemethod, in that it should be called to indicate that whoever is currently using thisWriteris done with any writing operations. In other words, this can signal to the storage driver that any buffered content needs to be flushed and active connections closed.aibaars commentedon Feb 2, 2016
@BrianBland That makes perfect sense. You're right that it is not possible to know the destination file name (ie digest) beforehand.
aibaars commentedon Feb 5, 2016
@BrianBland Currently the storage drivers use the ReadStream interface, your proposal seems to move to using the Writer interface instead. Is this on purpose?
BrianBland commentedon Feb 11, 2016
Yes, with the
Writerinterface, we can support nice operations such asio.Copydirectly into the storage backend and also we won't have a discrepancy between the amount of content read by theReaderpassed intoReadStreamand the amount of content actually written to the backend.aibaars commentedon Feb 11, 2016
@BrianBland I agree that the
Writerinterface is nicer, however, if I remember correctly, @stevvooe noted in a discussion of another PR thatReadStreamhas the advantage that the storage driver implementations can choose the buffer sizes that suit them best.BrianBland commentedon Feb 11, 2016
That's why we have
CloseandCommitin theFileWriterinterface. If you look at some of the reference implementations in #1438 you'll see that theWriteris generally intended to use internal buffering.aibaars commentedon Feb 11, 2016
@BrianBland Ah great, I was just wondering whether the change in strategy was deliberate or accidental. Thanks for clarifying.
Shall I have a go at implementing this for GCS, or is it too early for that?
BrianBland commentedon Feb 11, 2016
You can go ahead and do it if you want. I'm currently trying to validate that this is the correct approach, so there still could be some changes here and there. On the flip-side, implementing this for GCS could help ensure the interface correctness.
aibaars commentedon Feb 12, 2016
@BrianBland What should FileWriter.Size return:
I think 2. would be the more interesting one. Option 1 would include any bytes that are still in a buffer. Furthermore I think the test cases should not assume anything about the value of Size before Close has been called.
aibaars commentedon Feb 12, 2016
@BrianBland The relation between
Commit,CloseandCancelmight need a bit more clarification. From the tests it seems that the expected usage is:I had not expected it to be OK to call Close after Commit, because the description states that Close will flush the written data to the storage backend, which makes no sense once the data has been committed.
It might also be nice to define some errors in the storage driver interface that should be returned in case the FileWriter methods are called in the wrong order.
aibaars commentedon Feb 12, 2016
For an implementation of this interface using GCS resumable uploads see: BrianBland#1
RichardScothern commentedon Feb 12, 2016
#1395 (comment)
On reflection, I'm not a big fan of
Size()as a name in this context. I thinkWritten()would be more intuitive. Were this value to reflect the number of bytes written to theFileWriterthenBuffered()would be more appropriate.aibaars commentedon Feb 14, 2016
@BrianBland
I think the signature of the
storagedriver.Writermethod should be simplified to:This way it is much clearer that the interface is append only. In addition storage driver implementations do no longer have to check for invalid offset values.
BrianBland commentedon Feb 16, 2016
@aibaars I debated making this change earlier, but I'd rather the caller not assume some offset, pass in
append=true, and have the backend storage system resume from a different offset.aibaars commentedon Feb 16, 2016
@BrianBland My suggestion is to let the caller pass in
append=true, and then check whether theSizereported by the returnedFileWritermakes sense wrt the offset the caller wants to resume at.This would make the storage driver interface much simpler, as it is not clear to me what a driver is supposed to do if the offset is less than the size. For example should it just fail, should it truncate the file if the new chunk very small, or should it replace a region of the file?
BrianBland commentedon Feb 16, 2016
Alright, I think this may give us more flexibility with regards to how we handle resumable uploads, and actually reduces the number of interface changes in the rest of the registry code.
RichardScothern commentedon Apr 4, 2016
closed by #1438