Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions blob/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,7 +1214,7 @@ func (b *Bucket) WriteAll(ctx context.Context, key string, p []byte, opts *Write
sum := md5.Sum(p)
optsToUse.ContentMD5 = sum[:]
}
w, err := b.NewWriter(ctx, key, &optsToUse)
w, err := b.newWriter(ctx, key, &optsToUse, max(len(p), 1))
if err != nil {
return err
}
Expand Down Expand Up @@ -1255,6 +1255,11 @@ func (b *Bucket) Upload(ctx context.Context, key string, r io.Reader, opts *Writ
// The caller must call Close on the returned Writer, even if the write is
// aborted.
func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions) (_ *Writer, err error) {
return b.newWriter(ctx, key, opts, 0)
}

// newWriter is NewWriter with a driver.WriterOptions.SizeHint.
func (b *Bucket) newWriter(ctx context.Context, key string, opts *WriterOptions, sizeHint int) (_ *Writer, err error) {
if !utf8.ValidString(key) {
return nil, gcerr.Newf(gcerr.InvalidArgument, nil, "blob: NewWriter key must be a valid UTF-8 string: %q", key)
}
Expand All @@ -1269,6 +1274,7 @@ func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions)
ContentMD5: opts.ContentMD5,
BufferSize: opts.BufferSize,
MaxConcurrency: opts.MaxConcurrency,
SizeHint: sizeHint,
BeforeWrite: opts.BeforeWrite,
DisableContentTypeDetection: opts.DisableContentTypeDetection,
IfNotExist: opts.IfNotExist,
Expand Down Expand Up @@ -1340,7 +1346,7 @@ func (b *Bucket) NewWriter(ctx context.Context, key string, opts *WriterOptions)
w.opts = dopts
w.buf = bytes.NewBuffer([]byte{})
}
_, file, lineno, ok := runtime.Caller(1)
_, file, lineno, ok := runtime.Caller(2)
runtime.SetFinalizer(w, func(w *Writer) {
if !w.closed {
var caller string
Expand Down
4 changes: 4 additions & 0 deletions blob/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ type WriterOptions struct {
BufferSize int
// MaxConcurrency changes the default concurrency for uploading parts.
MaxConcurrency int
// SizeHint, if positive, is an upper bound on the number of bytes that
// will be written. Drivers may use it to avoid allocating buffers larger
// than the blob. Bucket.WriteAll sets it.
SizeHint int
// CacheControl specifies caching attributes that services may use
// when serving the blob.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
Expand Down
10 changes: 8 additions & 2 deletions blob/gcsblob/gcsblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ func (b *bucket) NewTypedWriter(ctx context.Context, key, contentType string, op
w.ContentEncoding = opts.ContentEncoding
w.ContentLanguage = opts.ContentLanguage
w.ContentType = contentType
w.ChunkSize = bufferSize(opts.BufferSize)
w.ChunkSize = bufferSize(opts.BufferSize, opts.SizeHint)
w.Metadata = opts.Metadata
w.MD5 = opts.ContentMD5
w.ForceEmptyContentType = opts.DisableContentTypeDetection
Expand Down Expand Up @@ -1112,8 +1112,14 @@ func (b *bucket) SignedURL(ctx context.Context, key string, dopts *driver.Signed
return storage.SignedURL(b.name, key, opts)
}

func bufferSize(size int) int {
func bufferSize(size, sizeHint int) int {
if size == 0 {
// The Writer allocates a buffer of this size, so don't make it much
// larger than the blob. It must be larger than the blob for the upload
// to fit in a single request.
if sizeHint > 0 && sizeHint < googleapi.DefaultUploadChunkSize {
return sizeHint + 1
}
return googleapi.DefaultUploadChunkSize
} else if size > 0 {
return size
Expand Down
97 changes: 94 additions & 3 deletions blob/gcsblob/gcsblob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,9 @@ func (verifyContentLanguage) ListObjectCheck(o *blob.ListObject) error {
// GCS-specific unit tests.
func TestBufferSize(t *testing.T) {
tests := []struct {
size int
want int
size int
sizeHint int
want int
}{
{
size: 5 * 1024 * 1024,
Expand All @@ -412,15 +413,105 @@ func TestBufferSize(t *testing.T) {
size: -1024,
want: 0,
},
{
sizeHint: 10 * 1024,
want: 10*1024 + 1,
},
{
sizeHint: googleapi.DefaultUploadChunkSize - 1,
want: googleapi.DefaultUploadChunkSize,
},
{
sizeHint: googleapi.DefaultUploadChunkSize,
want: googleapi.DefaultUploadChunkSize,
},
{
size: 5 * 1024 * 1024,
sizeHint: 10 * 1024,
want: 5 * 1024 * 1024,
},
{
size: -1024,
sizeHint: 10 * 1024,
want: 0,
},
}
for i, test := range tests {
got := bufferSize(test.size)
got := bufferSize(test.size, test.sizeHint)
if got != test.want {
t.Errorf("%d) got buffer size %d, want %d", i, got, test.want)
}
}
}

// TestWriteAllChunkSize checks that WriteAll doesn't give the storage.Writer
// a buffer larger than the blob.
func TestWriteAllChunkSize(t *testing.T) {
ctx := context.Background()
b, err := OpenBucket(ctx, &gcp.HTTPClient{}, "bucket", nil)
if err != nil {
t.Fatal(err)
}
defer closeWithErrorCheck(t, b)

errStop := errors.New("stop before upload")
chunkSize := func(t *testing.T, write func(*blob.WriterOptions) error, bufferSize int) int {
got := -1
opts := &blob.WriterOptions{
BufferSize: bufferSize,
ContentType: "application/octet-stream",
BeforeWrite: func(as func(any) bool) error {
var sw *storage.Writer
if as(&sw) {
got = sw.ChunkSize
}
return errStop
},
}
if err := write(opts); !errors.Is(err, errStop) {
t.Fatalf("got err %v, want %v", err, errStop)
}
return got
}
writeAll := func(n int) func(*blob.WriterOptions) error {
return func(opts *blob.WriterOptions) error {
return b.WriteAll(ctx, "key", make([]byte, n), opts)
}
}
// Without ContentType, the driver writer is created after content type detection.
writeAllDetect := func(n int) func(*blob.WriterOptions) error {
return func(opts *blob.WriterOptions) error {
opts.ContentType = ""
return b.WriteAll(ctx, "key", make([]byte, n), opts)
}
}
newWriter := func(opts *blob.WriterOptions) error {
_, err := b.NewWriter(ctx, "key", opts)
return err
}

tests := []struct {
description string
write func(*blob.WriterOptions) error
bufferSize int
want int
}{
{"WriteAll small", writeAll(10 * 1024), 0, 10*1024 + 1},
{"WriteAll empty", writeAll(0), 0, 2},
{"WriteAll small without ContentType", writeAllDetect(10 * 1024), 0, 10*1024 + 1},
{"WriteAll default size", writeAll(googleapi.DefaultUploadChunkSize), 0, googleapi.DefaultUploadChunkSize},
{"WriteAll with BufferSize", writeAll(10 * 1024), 5 * 1024 * 1024, 5 * 1024 * 1024},
{"NewWriter", newWriter, 0, googleapi.DefaultUploadChunkSize},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
if got := chunkSize(t, test.write, test.bufferSize); got != test.want {
t.Errorf("got ChunkSize %d, want %d", got, test.want)
}
})
}
}

func TestOpenBucket(t *testing.T) {
tests := []struct {
description string
Expand Down