Skip to content

Proposal: New StorageDriver Writer interface #1395

Closed

Description

@BrianBland

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

  1. added this to the Registry/2.4 milestone on Jan 27, 2016
  2. self-assigned this
    on Jan 27, 2016
  3. dmp42 commented on Jan 27, 2016

    @dmp42
    Contributor

    cc @docker/distribution-maintainers

  4. aaronlehmann commented on Jan 27, 2016

    @aaronlehmann

    This mostly looks good to me, per our discussion Monday evening.

    I'm wondering if Commit doesn't belong in the FileWriter interface. It sounds like Commit may combine the data written by multiple FileWriters. 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.

  5. stevvooe commented on Jan 27, 2016

    @stevvooe
    Collaborator

    Nice proposal. This covers the consistency issues present in the WriteStream method.

    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?

    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 Stat is 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 an Size() method to FileWriter to make the role of Stat clear in providing information about committed writes.

  6. aibaars commented on Jan 29, 2016

    @aibaars
    Contributor

    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 Move function can be removed, this could be taken care of by Commit. 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() []byte
    • func 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?

  7. BrianBland commented on Feb 2, 2016

    @BrianBland
    ContributorAuthor

    @aibaars unfortunately I don't see a way to remove the Move function 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 FileWriter returned from StorageDriver.Writer should already contain the upload session uri, as a caller of the StorageDriver methods should not know about this driver-specific information. This could be detected on the call to Writer by checking existing multipart/resumable uploads.

    FileWriter.Close() is intended to be used like any other io.Closers Close method, in that it should be called to indicate that whoever is currently using this Writer is 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.

  8. aibaars commented on Feb 2, 2016

    @aibaars
    Contributor

    @BrianBland That makes perfect sense. You're right that it is not possible to know the destination file name (ie digest) beforehand.

  9. aibaars commented on Feb 5, 2016

    @aibaars
    Contributor

    @BrianBland Currently the storage drivers use the ReadStream interface, your proposal seems to move to using the Writer interface instead. Is this on purpose?

  10. BrianBland commented on Feb 11, 2016

    @BrianBland
    ContributorAuthor

    Yes, with the Writer interface, we can support nice operations such as io.Copy directly into the storage backend and also we won't have a discrepancy between the amount of content read by the Reader passed into ReadStream and the amount of content actually written to the backend.

  11. aibaars commented on Feb 11, 2016

    @aibaars
    Contributor

    @BrianBland I agree that the Writer interface is nicer, however, if I remember correctly, @stevvooe noted in a discussion of another PR that ReadStream has the advantage that the storage driver implementations can choose the buffer sizes that suit them best.

  12. BrianBland commented on Feb 11, 2016

    @BrianBland
    ContributorAuthor

    That's why we have Close and Commit in the FileWriter interface. If you look at some of the reference implementations in #1438 you'll see that the Writer is generally intended to use internal buffering.

  13. aibaars commented on Feb 11, 2016

    @aibaars
    Contributor

    @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?

  14. BrianBland commented on Feb 11, 2016

    @BrianBland
    ContributorAuthor

    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.

  15. aibaars commented on Feb 12, 2016

    @aibaars
    Contributor

    @BrianBland What should FileWriter.Size return:

    1. the number of bytes received by the FileWriter, or
    2. the number of bytes that have been written to the backing storage
      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.
  16. aibaars commented on Feb 12, 2016

    @aibaars
    Contributor

    @BrianBland The relation between Commit, Close and Cancel might need a bit more clarification. From the tests it seems that the expected usage is:

    1. StorageDriver.Writer
    2. zero or more calls to FileWriter.Write
    3. optionally one of [FileWriter.Commit, FileWriter.Cancel]
    4. FileWriter.Close

    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.

  17. aibaars commented on Feb 12, 2016

    @aibaars
    Contributor

    For an implementation of this interface using GCS resumable uploads see: BrianBland#1

  18. RichardScothern commented on Feb 12, 2016

    @RichardScothern

    #1395 (comment)

    On reflection, I'm not a big fan of Size() as a name in this context. I think Written() would be more intuitive. Were this value to reflect the number of bytes written to the FileWriter then Buffered() would be more appropriate.

  19. aibaars commented on Feb 14, 2016

    @aibaars
    Contributor

    @BrianBland

    I think the signature of the storagedriver.Writer method should be simplified to:

    func (d *driver) Writer(context ctx.Context, path string, append boolean) (storagedriver.FileWriter, error)
    

    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.

  20. BrianBland commented on Feb 16, 2016

    @BrianBland
    ContributorAuthor

    @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.

  21. aibaars commented on Feb 16, 2016

    @aibaars
    Contributor

    @BrianBland My suggestion is to let the caller pass in append=true, and then check whether the Size reported by the returned FileWriter makes 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?

  22. BrianBland commented on Feb 16, 2016

    @BrianBland
    ContributorAuthor

    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.

  23. RichardScothern commented on Apr 4, 2016

    @RichardScothern

    closed by #1438

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions