X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/3cbe55d440788b0b9b1a9d9e642103929b57e8fd..cd0e7f06116925d66cc370a03cef1b8c19d0002d:/services/keepstore/s3_volume.go diff --git a/services/keepstore/s3_volume.go b/services/keepstore/s3_volume.go index d34b8772c5..e6a53d06c6 100644 --- a/services/keepstore/s3_volume.go +++ b/services/keepstore/s3_volume.go @@ -1,3 +1,7 @@ +// Copyright (C) The Arvados Authors. All rights reserved. +// +// SPDX-License-Identifier: AGPL-3.0 + package main import ( @@ -129,7 +133,7 @@ func init() { &s3UnsafeDelete, "s3-unsafe-delete", false, - "EXPERIMENTAL. Enable deletion (garbage collection), even though there are known race conditions that can cause data loss.") + "EXPERIMENTAL. Enable deletion (garbage collection) even when trash lifetime is zero, even though there are known race conditions that can cause data loss.") } // S3Volume implements Volume using an S3 bucket. @@ -239,6 +243,11 @@ func (v *S3Volume) Start() error { return nil } +// DeviceID returns a globally unique ID for the storage bucket. +func (v *S3Volume) DeviceID() string { + return "s3://" + v.Endpoint + "/" + v.Bucket +} + func (v *S3Volume) getReaderWithContext(ctx context.Context, loc string) (rdr io.ReadCloser, err error) { ready := make(chan bool) go func() { @@ -331,6 +340,40 @@ func (v *S3Volume) Get(ctx context.Context, loc string, buf []byte) (int, error) // Compare the given data with the stored data. func (v *S3Volume) Compare(ctx context.Context, loc string, expect []byte) error { + errChan := make(chan error, 1) + go func() { + _, err := v.bucket.Head("recent/"+loc, nil) + errChan <- err + }() + var err error + select { + case <-ctx.Done(): + return ctx.Err() + case err = <-errChan: + } + if err != nil { + // Checking for "loc" itself here would interfere with + // future GET requests. + // + // On AWS, if X doesn't exist, a HEAD or GET request + // for X causes X's non-existence to be cached. Thus, + // if we test for X, then create X and return a + // signature to our client, the client might still get + // 404 from all keepstores when trying to read it. + // + // To avoid this, we avoid doing HEAD X or GET X until + // we know X has been written. + // + // Note that X might exist even though recent/X + // doesn't: for example, the response to HEAD recent/X + // might itself come from a stale cache. In such + // cases, we will return a false negative and + // PutHandler might needlessly create another replica + // on a different volume. That's not ideal, but it's + // better than passing the eventually-consistent + // problem on to our clients. + return v.translateError(err) + } rdr, err := v.getReaderWithContext(ctx, loc) if err != nil { return err @@ -377,7 +420,7 @@ func (v *S3Volume) Put(ctx context.Context, loc string, block []byte) error { if err != nil { return } - err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{}) + err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{}) }() select { case <-ctx.Done(): @@ -409,7 +452,7 @@ func (v *S3Volume) Touch(loc string) error { } else if err != nil { return err } - err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{}) + err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{}) return v.translateError(err) } @@ -423,7 +466,7 @@ func (v *S3Volume) Mtime(loc string) (time.Time, error) { err = v.translateError(err) if os.IsNotExist(err) { // The data object X exists, but recent/X is missing. - err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{}) + err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{}) if err != nil { log.Printf("error: creating %q: %s", "recent/"+loc, err) return zeroTime, v.translateError(err) @@ -605,7 +648,7 @@ func (v *S3Volume) Untrash(loc string) error { if err != nil { return err } - err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{}) + err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{}) return v.translateError(err) } @@ -884,14 +927,18 @@ func (b *s3bucket) Head(path string, headers map[string][]string) (*http.Respons } func (b *s3bucket) PutReader(path string, r io.Reader, length int64, contType string, perm s3.ACL, options s3.Options) error { - err := b.Bucket.PutReader(path, NewCountingReader(r, b.stats.TickOutBytes), length, contType, perm, options) - b.stats.Tick(&b.stats.Ops, &b.stats.PutOps) - b.stats.TickErr(err) - return err -} - -func (b *s3bucket) Put(path string, data []byte, contType string, perm s3.ACL, options s3.Options) error { - err := b.Bucket.PutReader(path, NewCountingReader(bytes.NewBuffer(data), b.stats.TickOutBytes), int64(len(data)), contType, perm, options) + if length == 0 { + // goamz will only send Content-Length: 0 when reader + // is nil due to net.http.Request.ContentLength + // behavior. Otherwise, Content-Length header is + // omitted which will cause some S3 services + // (including AWS and Ceph RadosGW) to fail to create + // empty objects. + r = nil + } else { + r = NewCountingReader(r, b.stats.TickOutBytes) + } + err := b.Bucket.PutReader(path, r, length, contType, perm, options) b.stats.Tick(&b.stats.Ops, &b.stats.PutOps) b.stats.TickErr(err) return err