X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/159db45a4fb728ed659e9c6cda9e7f2d117dcff9..d0f3483739a0140802374e6a9f5d0ab5972bd951:/services/keepstore/s3aws_volume.go diff --git a/services/keepstore/s3aws_volume.go b/services/keepstore/s3aws_volume.go index baded41163..d068dde074 100644 --- a/services/keepstore/s3aws_volume.go +++ b/services/keepstore/s3aws_volume.go @@ -2,7 +2,7 @@ // // SPDX-License-Identifier: AGPL-3.0 -package main +package keepstore import ( "bytes" @@ -62,6 +62,8 @@ type s3AWSbucket struct { // aws-sdk-go based on the UseAWSS3v2Driver feature flag func chooseS3VolumeDriver(cluster *arvados.Cluster, volume arvados.Volume, logger logrus.FieldLogger, metrics *volumeMetricsVecs) (Volume, error) { v := &S3Volume{cluster: cluster, volume: volume, metrics: metrics} + // Default value will be overriden if it happens to be defined in the config + v.S3VolumeDriverParameters.UseAWSS3v2Driver = true err := json.Unmarshal(volume.DriverParameters, v) if err != nil { return nil, err @@ -83,8 +85,22 @@ const ( var s3AWSKeepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`) var s3AWSZeroTime time.Time -func (v *S3AWSVolume) isKeepBlock(s string) bool { - return s3AWSKeepBlockRegexp.MatchString(s) +func (v *S3AWSVolume) isKeepBlock(s string) (string, bool) { + if v.PrefixLength > 0 && len(s) == v.PrefixLength+33 && s[:v.PrefixLength] == s[v.PrefixLength+1:v.PrefixLength*2+1] { + s = s[v.PrefixLength+1:] + } + return s, s3AWSKeepBlockRegexp.MatchString(s) +} + +// Return the key used for a given loc. If PrefixLength==0 then +// key("abcdef0123") is "abcdef0123", if PrefixLength==3 then key is +// "abc/abcdef0123", etc. +func (v *S3AWSVolume) key(loc string) string { + if v.PrefixLength > 0 && v.PrefixLength < len(loc)-1 { + return loc[:v.PrefixLength] + "/" + loc + } else { + return loc + } } func newS3AWSVolume(cluster *arvados.Cluster, volume arvados.Volume, logger logrus.FieldLogger, metrics *volumeMetricsVecs) (Volume, error) { @@ -98,22 +114,24 @@ func newS3AWSVolume(cluster *arvados.Cluster, volume arvados.Volume, logger logr } func (v *S3AWSVolume) translateError(err error) error { - if aerr, ok := err.(awserr.Error); ok { - switch aerr.Code() { - case "NotFound": + if _, ok := err.(*aws.RequestCanceledError); ok { + return context.Canceled + } else if aerr, ok := err.(awserr.Error); ok { + if aerr.Code() == "NotFound" { return os.ErrNotExist - case "NoSuchKey": + } else if aerr.Code() == "NoSuchKey" { return os.ErrNotExist } } return err } -// safeCopy calls CopyObjectRequest, and checks the response to make sure the -// copy succeeded and updated the timestamp on the destination object +// safeCopy calls CopyObjectRequest, and checks the response to make +// sure the copy succeeded and updated the timestamp on the +// destination object // -// (If something goes wrong during the copy, the error will be embedded in the -// 200 OK response) +// (If something goes wrong during the copy, the error will be +// embedded in the 200 OK response) func (v *S3AWSVolume) safeCopy(dst, src string) error { input := &s3.CopyObjectInput{ Bucket: aws.String(v.bucket.bucket), @@ -166,19 +184,25 @@ func (v *S3AWSVolume) check(ec2metadataHostname string) error { if v.Endpoint != "" && service == "s3" { return aws.Endpoint{ URL: v.Endpoint, - SigningRegion: v.Region, + SigningRegion: region, }, nil } else if service == "ec2metadata" && ec2metadataHostname != "" { return aws.Endpoint{ URL: ec2metadataHostname, }, nil + } else { + return defaultResolver.ResolveEndpoint(service, region) } - - return defaultResolver.ResolveEndpoint(service, region) } cfg.EndpointResolver = aws.EndpointResolverFunc(myCustomResolver) } - + if v.Region == "" { + // Endpoint is already specified (otherwise we would + // have errored out above), but Region is also + // required by the aws sdk, in order to determine + // SignatureVersions. + v.Region = "us-east-1" + } cfg.Region = v.Region // Zero timeouts mean "wait forever", which is a bad @@ -222,9 +246,10 @@ func (v *S3AWSVolume) GetDeviceID() string { // Compare the given data with the stored data. func (v *S3AWSVolume) Compare(ctx context.Context, loc string, expect []byte) error { + key := v.key(loc) errChan := make(chan error, 1) go func() { - _, err := v.Head("recent/" + loc) + _, err := v.head("recent/" + key) errChan <- err }() var err error @@ -234,8 +259,8 @@ func (v *S3AWSVolume) Compare(ctx context.Context, loc string, expect []byte) er case err = <-errChan: } if err != nil { - // Checking for "loc" itself here would interfere with - // future GET requests. + // Checking for the key 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, @@ -259,7 +284,7 @@ func (v *S3AWSVolume) Compare(ctx context.Context, loc string, expect []byte) er input := &s3.GetObjectInput{ Bucket: aws.String(v.bucket.bucket), - Key: aws.String(loc), + Key: aws.String(key), } req := v.bucket.svc.GetObjectRequest(input) @@ -283,29 +308,30 @@ func (v *S3AWSVolume) EmptyTrash() { startT := time.Now() emptyOneKey := func(trash *s3.Object) { - loc := strings.TrimPrefix(*trash.Key, "trash/") - if !v.isKeepBlock(loc) { + key := strings.TrimPrefix(*trash.Key, "trash/") + loc, isblk := v.isKeepBlock(key) + if !isblk { return } atomic.AddInt64(&bytesInTrash, *trash.Size) atomic.AddInt64(&blocksInTrash, 1) - trashT := *(trash.LastModified) - recent, err := v.Head("recent/" + loc) + trashT := *trash.LastModified + recent, err := v.head("recent/" + key) if err != nil && os.IsNotExist(v.translateError(err)) { - v.logger.Warnf("EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", trash.Key, "recent/"+loc, err) + v.logger.Warnf("EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", *trash.Key, "recent/"+key, err) err = v.Untrash(loc) if err != nil { v.logger.WithError(err).Errorf("EmptyTrash: Untrash(%q) failed", loc) } return } else if err != nil { - v.logger.WithError(err).Warnf("EmptyTrash: HEAD %q failed", "recent/"+loc) + v.logger.WithError(err).Warnf("EmptyTrash: HEAD %q failed", "recent/"+key) return } if trashT.Sub(*recent.LastModified) < v.cluster.Collections.BlobSigningTTL.Duration() { if age := startT.Sub(*recent.LastModified); age >= v.cluster.Collections.BlobSigningTTL.Duration()-time.Duration(v.RaceWindow) { - // recent/loc is too old to protect + // recent/key is too old to protect // loc from being Trashed again during // the raceWindow that starts if we // delete trash/X now. @@ -314,14 +340,14 @@ func (v *S3AWSVolume) EmptyTrash() { // < BlobSigningTTL - raceWindow) is // necessary to avoid starvation. v.logger.Infof("EmptyTrash: detected old race for %q, calling fixRace + Touch", loc) - v.fixRace(loc) + v.fixRace(key) v.Touch(loc) return } - _, err := v.Head(loc) + _, err := v.head(key) if os.IsNotExist(err) { v.logger.Infof("EmptyTrash: detected recent race for %q, calling fixRace", loc) - v.fixRace(loc) + v.fixRace(key) return } else if err != nil { v.logger.WithError(err).Warnf("EmptyTrash: HEAD %q failed", loc) @@ -339,18 +365,18 @@ func (v *S3AWSVolume) EmptyTrash() { atomic.AddInt64(&bytesDeleted, *trash.Size) atomic.AddInt64(&blocksDeleted, 1) - _, err = v.Head(loc) + _, err = v.head(*trash.Key) if err == nil { v.logger.Warnf("EmptyTrash: HEAD %q succeeded immediately after deleting %q", loc, loc) return } if !os.IsNotExist(v.translateError(err)) { - v.logger.WithError(err).Warnf("EmptyTrash: HEAD %q failed", loc) + v.logger.WithError(err).Warnf("EmptyTrash: HEAD %q failed", key) return } - err = v.bucket.Del("recent/" + loc) + err = v.bucket.Del("recent/" + key) if err != nil { - v.logger.WithError(err).Warnf("EmptyTrash: error deleting %q", "recent/"+loc) + v.logger.WithError(err).Warnf("EmptyTrash: error deleting %q", "recent/"+key) } } @@ -386,21 +412,21 @@ func (v *S3AWSVolume) EmptyTrash() { } // fixRace(X) is called when "recent/X" exists but "X" doesn't -// exist. If the timestamps on "recent/"+loc and "trash/"+loc indicate -// there was a race between Put and Trash, fixRace recovers from the -// race by Untrashing the block. -func (v *S3AWSVolume) fixRace(loc string) bool { - trash, err := v.Head("trash/" + loc) +// exist. If the timestamps on "recent/X" and "trash/X" indicate there +// was a race between Put and Trash, fixRace recovers from the race by +// Untrashing the block. +func (v *S3AWSVolume) fixRace(key string) bool { + trash, err := v.head("trash/" + key) if err != nil { if !os.IsNotExist(v.translateError(err)) { - v.logger.WithError(err).Errorf("fixRace: HEAD %q failed", "trash/"+loc) + v.logger.WithError(err).Errorf("fixRace: HEAD %q failed", "trash/"+key) } return false } - recent, err := v.Head("recent/" + loc) + recent, err := v.head("recent/" + key) if err != nil { - v.logger.WithError(err).Errorf("fixRace: HEAD %q failed", "recent/"+loc) + v.logger.WithError(err).Errorf("fixRace: HEAD %q failed", "recent/"+key) return false } @@ -413,9 +439,9 @@ func (v *S3AWSVolume) fixRace(loc string) bool { return false } - v.logger.Infof("fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", loc, trashTime, recentTime, ageWhenTrashed, v.cluster.Collections.BlobSigningTTL) - v.logger.Infof("fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+loc, loc) - err = v.safeCopy(loc, "trash/"+loc) + v.logger.Infof("fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", key, trashTime, recentTime, ageWhenTrashed, v.cluster.Collections.BlobSigningTTL) + v.logger.Infof("fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+key, key) + err = v.safeCopy(key, "trash/"+key) if err != nil { v.logger.WithError(err).Error("fixRace: copy failed") return false @@ -423,10 +449,10 @@ func (v *S3AWSVolume) fixRace(loc string) bool { return true } -func (v *S3AWSVolume) Head(loc string) (result *s3.HeadObjectOutput, err error) { +func (v *S3AWSVolume) head(key string) (result *s3.HeadObjectOutput, err error) { input := &s3.HeadObjectInput{ Bucket: aws.String(v.bucket.bucket), - Key: aws.String(loc), + Key: aws.String(key), } req := v.bucket.svc.HeadObjectRequest(input) @@ -446,78 +472,63 @@ func (v *S3AWSVolume) Head(loc string) (result *s3.HeadObjectOutput, err error) // Get a block: copy the block data into buf, and return the number of // bytes copied. func (v *S3AWSVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) { - return getWithPipe(ctx, loc, buf, v) -} - -func (v *S3AWSVolume) readWorker(ctx context.Context, loc string) (rdr io.ReadCloser, err error) { - buf := make([]byte, 0, 67108864) - awsBuf := aws.NewWriteAtBuffer(buf) - - downloader := s3manager.NewDownloaderWithClient(v.bucket.svc, func(u *s3manager.Downloader) { - u.PartSize = PartSize - u.Concurrency = ReadConcurrency - }) - - v.logger.Debugf("Partsize: %d; Concurrency: %d\n", downloader.PartSize, downloader.Concurrency) - - _, err = downloader.DownloadWithContext(ctx, awsBuf, &s3.GetObjectInput{ - Bucket: aws.String(v.bucket.bucket), - Key: aws.String(loc), - }) - v.bucket.stats.TickOps("get") - v.bucket.stats.Tick(&v.bucket.stats.Ops, &v.bucket.stats.GetOps) - v.bucket.stats.TickErr(err) - if err != nil { - return nil, v.translateError(err) - } - buf = awsBuf.Bytes() - - rdr = NewCountingReader(bytes.NewReader(buf), v.bucket.stats.TickInBytes) - return -} - -// ReadBlock implements BlockReader. -func (v *S3AWSVolume) ReadBlock(ctx context.Context, loc string, w io.Writer) error { - rdr, err := v.readWorker(ctx, loc) - + // Do not use getWithPipe here: the BlockReader interface does not pass + // through 'buf []byte', and we don't want to allocate two buffers for each + // read request. Instead, use a version of ReadBlock that accepts 'buf []byte' + // as an input. + key := v.key(loc) + count, err := v.readWorker(ctx, key, buf) if err == nil { - _, err2 := io.Copy(w, rdr) - if err2 != nil { - return err2 - } - return err + return count, err } err = v.translateError(err) if !os.IsNotExist(err) { - return err + return 0, err } - _, err = v.Head("recent/" + loc) + _, err = v.head("recent/" + key) err = v.translateError(err) if err != nil { // If we can't read recent/X, there's no point in // trying fixRace. Give up. - return err + return 0, err } - if !v.fixRace(loc) { + if !v.fixRace(key) { err = os.ErrNotExist - return err + return 0, err } - rdr, err = v.readWorker(ctx, loc) + count, err = v.readWorker(ctx, key, buf) if err != nil { v.logger.Warnf("reading %s after successful fixRace: %s", loc, err) err = v.translateError(err) - return err + return 0, err } + return count, err +} - _, err = io.Copy(w, rdr) +func (v *S3AWSVolume) readWorker(ctx context.Context, key string, buf []byte) (int, error) { + awsBuf := aws.NewWriteAtBuffer(buf) + downloader := s3manager.NewDownloaderWithClient(v.bucket.svc, func(u *s3manager.Downloader) { + u.PartSize = PartSize + u.Concurrency = ReadConcurrency + }) - return err + v.logger.Debugf("Partsize: %d; Concurrency: %d\n", downloader.PartSize, downloader.Concurrency) + + count, err := downloader.DownloadWithContext(ctx, awsBuf, &s3.GetObjectInput{ + Bucket: aws.String(v.bucket.bucket), + Key: aws.String(key), + }) + v.bucket.stats.TickOps("get") + v.bucket.stats.Tick(&v.bucket.stats.Ops, &v.bucket.stats.GetOps) + v.bucket.stats.TickErr(err) + v.bucket.stats.TickInBytes(uint64(count)) + return int(count), v.translateError(err) } -func (v *S3AWSVolume) writeObject(ctx context.Context, name string, r io.Reader) error { +func (v *S3AWSVolume) writeObject(ctx context.Context, key string, r io.Reader) error { if r == nil { // r == nil leads to a memory violation in func readFillBuf in // aws-sdk-go-v2@v0.23.0/service/s3/s3manager/upload.go @@ -526,15 +537,15 @@ func (v *S3AWSVolume) writeObject(ctx context.Context, name string, r io.Reader) uploadInput := s3manager.UploadInput{ Bucket: aws.String(v.bucket.bucket), - Key: aws.String(name), + Key: aws.String(key), Body: r, } - if len(name) == 32 { + if loc, ok := v.isKeepBlock(key); ok { var contentMD5 string - md5, err := hex.DecodeString(name) + md5, err := hex.DecodeString(loc) if err != nil { - return err + return v.translateError(err) } contentMD5 = base64.StdEncoding.EncodeToString(md5) uploadInput.ContentMD5 = &contentMD5 @@ -563,26 +574,25 @@ func (v *S3AWSVolume) writeObject(ctx context.Context, name string, r io.Reader) v.bucket.stats.Tick(&v.bucket.stats.Ops, &v.bucket.stats.PutOps) v.bucket.stats.TickErr(err) - return err + return v.translateError(err) } // Put writes a block. func (v *S3AWSVolume) Put(ctx context.Context, loc string, block []byte) error { - return putWithPipe(ctx, loc, block, v) -} - -// WriteBlock implements BlockWriter. -func (v *S3AWSVolume) WriteBlock(ctx context.Context, loc string, rdr io.Reader) error { + // Do not use putWithPipe here; we want to pass an io.ReadSeeker to the S3 + // sdk to avoid memory allocation there. See #17339 for more information. if v.volume.ReadOnly { return MethodDisabledError } - r := NewCountingReader(rdr, v.bucket.stats.TickOutBytes) - err := v.writeObject(ctx, loc, r) + rdr := bytes.NewReader(block) + r := NewCountingReaderAtSeeker(rdr, v.bucket.stats.TickOutBytes) + key := v.key(loc) + err := v.writeObject(ctx, key, r) if err != nil { return err } - return v.writeObject(ctx, "recent/"+loc, nil) + return v.writeObject(ctx, "recent/"+key, nil) } type s3awsLister struct { @@ -675,6 +685,7 @@ func (lister *s3awsLister) pop() (k *s3.Object) { // IndexTo writes a complete list of locators with the given prefix // for which Get() can retrieve data. func (v *S3AWSVolume) IndexTo(prefix string, writer io.Writer) error { + prefix = v.key(prefix) // Use a merge sort to find matching sets of X and recent/X. dataL := s3awsLister{ Logger: v.logger, @@ -698,7 +709,8 @@ func (v *S3AWSVolume) IndexTo(prefix string, writer io.Writer) error { // over all of them needlessly with dataL. break } - if !v.isKeepBlock(*data.Key) { + loc, isblk := v.isKeepBlock(*data.Key) + if !isblk { continue } @@ -730,30 +742,31 @@ func (v *S3AWSVolume) IndexTo(prefix string, writer io.Writer) error { // We truncate sub-second precision here. Otherwise // timestamps will never match the RFC1123-formatted // Last-Modified values parsed by Mtime(). - fmt.Fprintf(writer, "%s+%d %d\n", *data.Key, *data.Size, stamp.LastModified.Unix()*1000000000) + fmt.Fprintf(writer, "%s+%d %d\n", loc, *data.Size, stamp.LastModified.Unix()*1000000000) } return dataL.Error() } // Mtime returns the stored timestamp for the given locator. func (v *S3AWSVolume) Mtime(loc string) (time.Time, error) { - _, err := v.Head(loc) + key := v.key(loc) + _, err := v.head(key) if err != nil { return s3AWSZeroTime, v.translateError(err) } - resp, err := v.Head("recent/" + loc) + resp, err := v.head("recent/" + key) err = v.translateError(err) if os.IsNotExist(err) { // The data object X exists, but recent/X is missing. - err = v.writeObject(context.Background(), "recent/"+loc, nil) + err = v.writeObject(context.Background(), "recent/"+key, nil) if err != nil { - v.logger.WithError(err).Errorf("error creating %q", "recent/"+loc) + v.logger.WithError(err).Errorf("error creating %q", "recent/"+key) return s3AWSZeroTime, v.translateError(err) } - v.logger.Infof("Mtime: created %q to migrate existing block to new storage scheme", "recent/"+loc) - resp, err = v.Head("recent/" + loc) + v.logger.Infof("Mtime: created %q to migrate existing block to new storage scheme", "recent/"+key) + resp, err = v.head("recent/" + key) if err != nil { - v.logger.WithError(err).Errorf("HEAD failed after creating %q", "recent/"+loc) + v.logger.WithError(err).Errorf("HEAD failed after creating %q", "recent/"+key) return s3AWSZeroTime, v.translateError(err) } } else if err != nil { @@ -784,22 +797,23 @@ func (v *S3AWSVolume) Touch(loc string) error { if v.volume.ReadOnly { return MethodDisabledError } - _, err := v.Head(loc) + key := v.key(loc) + _, err := v.head(key) err = v.translateError(err) - if os.IsNotExist(err) && v.fixRace(loc) { + if os.IsNotExist(err) && v.fixRace(key) { // The data object got trashed in a race, but fixRace // rescued it. } else if err != nil { return err } - err = v.writeObject(context.Background(), "recent/"+loc, nil) + err = v.writeObject(context.Background(), "recent/"+key, nil) return v.translateError(err) } -// checkRaceWindow returns a non-nil error if trash/loc is, or might -// be, in the race window (i.e., it's not safe to trash loc). -func (v *S3AWSVolume) checkRaceWindow(loc string) error { - resp, err := v.Head("trash/" + loc) +// checkRaceWindow returns a non-nil error if trash/key is, or might +// be, in the race window (i.e., it's not safe to trash key). +func (v *S3AWSVolume) checkRaceWindow(key string) error { + resp, err := v.head("trash/" + key) err = v.translateError(err) if os.IsNotExist(err) { // OK, trash/X doesn't exist so we're not in the race @@ -817,7 +831,7 @@ func (v *S3AWSVolume) checkRaceWindow(loc string) error { // trash/X's lifetime. The new timestamp might not // become visible until now+raceWindow, and EmptyTrash // is allowed to delete trash/X before then. - return fmt.Errorf("same block is already in trash, and safe window ended %s ago", -safeWindow) + return fmt.Errorf("%s: same block is already in trash, and safe window ended %s ago", key, -safeWindow) } // trash/X exists, but it won't be eligible for deletion until // after now+raceWindow, so it's safe to overwrite it. @@ -831,7 +845,6 @@ func (b *s3AWSbucket) Del(path string) error { } req := b.svc.DeleteObjectRequest(input) _, err := req.Send(context.Background()) - //err := b.Bucket().Del(path) b.stats.TickOps("delete") b.stats.Tick(&b.stats.Ops, &b.stats.DelOps) b.stats.TickErr(err) @@ -848,30 +861,32 @@ func (v *S3AWSVolume) Trash(loc string) error { } else if time.Since(t) < v.cluster.Collections.BlobSigningTTL.Duration() { return nil } + key := v.key(loc) if v.cluster.Collections.BlobTrashLifetime == 0 { if !v.UnsafeDelete { return ErrS3TrashDisabled } - return v.translateError(v.bucket.Del(loc)) + return v.translateError(v.bucket.Del(key)) } - err := v.checkRaceWindow(loc) + err := v.checkRaceWindow(key) if err != nil { return err } - err = v.safeCopy("trash/"+loc, loc) + err = v.safeCopy("trash/"+key, key) if err != nil { return err } - return v.translateError(v.bucket.Del(loc)) + return v.translateError(v.bucket.Del(key)) } // Untrash moves block from trash back into store func (v *S3AWSVolume) Untrash(loc string) error { - err := v.safeCopy(loc, "trash/"+loc) + key := v.key(loc) + err := v.safeCopy(key, "trash/"+key) if err != nil { return err } - err = v.writeObject(context.Background(), "recent/"+loc, nil) + err = v.writeObject(context.Background(), "recent/"+key, nil) return v.translateError(err) }