1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
25 "git.curoverse.com/arvados.git/sdk/go/arvados"
26 "github.com/AdRoll/goamz/aws"
27 "github.com/AdRoll/goamz/s3"
31 s3DefaultReadTimeout = arvados.Duration(10 * time.Minute)
32 s3DefaultConnectTimeout = arvados.Duration(time.Minute)
36 // ErrS3TrashDisabled is returned by Trash if that operation
37 // is impossible with the current config.
38 ErrS3TrashDisabled = fmt.Errorf("trash function is disabled because -trash-lifetime=0 and -s3-unsafe-delete=false")
40 s3AccessKeyFile string
41 s3SecretKeyFile string
46 s3RaceWindow time.Duration
54 maxClockSkew = 600 * time.Second
55 nearlyRFC1123 = "Mon, 2 Jan 2006 15:04:05 GMT"
58 type s3VolumeAdder struct {
62 // String implements flag.Value
63 func (s *s3VolumeAdder) String() string {
67 func (s *s3VolumeAdder) Set(bucketName string) error {
69 return fmt.Errorf("no container name given")
71 if s3AccessKeyFile == "" || s3SecretKeyFile == "" {
72 return fmt.Errorf("-s3-access-key-file and -s3-secret-key-file arguments must given before -s3-bucket-volume")
74 if deprecated.flagSerializeIO {
75 log.Print("Notice: -serialize is not supported by s3-bucket volumes.")
77 s.Config.Volumes = append(s.Config.Volumes, &S3Volume{
79 AccessKeyFile: s3AccessKeyFile,
80 SecretKeyFile: s3SecretKeyFile,
83 RaceWindow: arvados.Duration(s3RaceWindow),
84 S3Replication: s3Replication,
85 UnsafeDelete: s3UnsafeDelete,
86 ReadOnly: deprecated.flagReadonly,
92 func s3regions() (okList []string) {
93 for r := range aws.Regions {
94 okList = append(okList, r)
100 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &S3Volume{} })
102 flag.Var(&s3VolumeAdder{theConfig},
104 "Use the given bucket as a storage volume. Can be given multiple times.")
109 fmt.Sprintf("AWS region used for subsequent -s3-bucket-volume arguments. Allowed values are %+q.", s3regions()))
114 "Endpoint URL used for subsequent -s3-bucket-volume arguments. If blank, use the AWS endpoint corresponding to the -s3-region argument. For Google Storage, use \"https://storage.googleapis.com\".")
117 "s3-access-key-file",
119 "`File` containing the access key used for subsequent -s3-bucket-volume arguments.")
122 "s3-secret-key-file",
124 "`File` containing the secret key used for subsequent -s3-bucket-volume arguments.")
129 "Maximum eventual consistency latency for subsequent -s3-bucket-volume arguments.")
134 "Replication level reported to clients for subsequent -s3-bucket-volume arguments.")
139 "EXPERIMENTAL. Enable deletion (garbage collection) even when trash lifetime is zero, even though there are known race conditions that can cause data loss.")
142 // S3Volume implements Volume using an S3 bucket.
143 type S3Volume struct {
149 LocationConstraint bool
152 ConnectTimeout arvados.Duration
153 ReadTimeout arvados.Duration
154 RaceWindow arvados.Duration
157 StorageClasses []string
164 // Examples implements VolumeWithExamples.
165 func (*S3Volume) Examples() []Volume {
168 AccessKeyFile: "/etc/aws_s3_access_key.txt",
169 SecretKeyFile: "/etc/aws_s3_secret_key.txt",
172 Bucket: "example-bucket-name",
175 RaceWindow: arvados.Duration(24 * time.Hour),
176 ConnectTimeout: arvados.Duration(time.Minute),
177 ReadTimeout: arvados.Duration(5 * time.Minute),
180 AccessKeyFile: "/etc/gce_s3_access_key.txt",
181 SecretKeyFile: "/etc/gce_s3_secret_key.txt",
182 Endpoint: "https://storage.googleapis.com",
184 Bucket: "example-bucket-name",
187 RaceWindow: arvados.Duration(24 * time.Hour),
188 ConnectTimeout: arvados.Duration(time.Minute),
189 ReadTimeout: arvados.Duration(5 * time.Minute),
194 // Type implements Volume.
195 func (*S3Volume) Type() string {
199 // Start populates private fields and verifies the configuration is
201 func (v *S3Volume) Start() error {
202 region, ok := aws.Regions[v.Region]
203 if v.Endpoint == "" {
205 return fmt.Errorf("unrecognized region %+q; try specifying -s3-endpoint instead", v.Region)
208 return fmt.Errorf("refusing to use AWS region name %+q with endpoint %+q; "+
209 "specify empty endpoint (\"-s3-endpoint=\") or use a different region name", v.Region, v.Endpoint)
213 S3Endpoint: v.Endpoint,
214 S3LocationConstraint: v.LocationConstraint,
220 auth.AccessKey, err = readKeyFromFile(v.AccessKeyFile)
224 auth.SecretKey, err = readKeyFromFile(v.SecretKeyFile)
229 // Zero timeouts mean "wait forever", which is a bad
230 // default. Default to long timeouts instead.
231 if v.ConnectTimeout == 0 {
232 v.ConnectTimeout = s3DefaultConnectTimeout
234 if v.ReadTimeout == 0 {
235 v.ReadTimeout = s3DefaultReadTimeout
238 client := s3.New(auth, region)
239 if region.EC2Endpoint.Signer == aws.V4Signature {
240 // Currently affects only eu-central-1
241 client.Signature = aws.V4Signature
243 client.ConnectTimeout = time.Duration(v.ConnectTimeout)
244 client.ReadTimeout = time.Duration(v.ReadTimeout)
245 v.bucket = &s3bucket{
254 // DeviceID returns a globally unique ID for the storage bucket.
255 func (v *S3Volume) DeviceID() string {
256 return "s3://" + v.Endpoint + "/" + v.Bucket
259 func (v *S3Volume) getReaderWithContext(ctx context.Context, loc string) (rdr io.ReadCloser, err error) {
260 ready := make(chan bool)
262 rdr, err = v.getReader(loc)
269 theConfig.debugLogf("s3: abandoning getReader(): %s", ctx.Err())
276 return nil, ctx.Err()
280 // getReader wraps (Bucket)GetReader.
282 // In situations where (Bucket)GetReader would fail because the block
283 // disappeared in a Trash race, getReader calls fixRace to recover the
284 // data, and tries again.
285 func (v *S3Volume) getReader(loc string) (rdr io.ReadCloser, err error) {
286 rdr, err = v.bucket.GetReader(loc)
287 err = v.translateError(err)
288 if err == nil || !os.IsNotExist(err) {
292 _, err = v.bucket.Head("recent/"+loc, nil)
293 err = v.translateError(err)
295 // If we can't read recent/X, there's no point in
296 // trying fixRace. Give up.
304 rdr, err = v.bucket.GetReader(loc)
306 log.Printf("warning: reading %s after successful fixRace: %s", loc, err)
307 err = v.translateError(err)
312 // Get a block: copy the block data into buf, and return the number of
314 func (v *S3Volume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
315 rdr, err := v.getReaderWithContext(ctx, loc)
321 ready := make(chan bool)
326 n, err = io.ReadFull(rdr, buf)
329 case nil, io.EOF, io.ErrUnexpectedEOF:
332 err = v.translateError(err)
337 theConfig.debugLogf("s3: interrupting ReadFull() with Close() because %s", ctx.Err())
339 // Must wait for ReadFull to return, to ensure it
340 // doesn't write to buf after we return.
341 theConfig.debugLogf("s3: waiting for ReadFull() to fail")
349 // Compare the given data with the stored data.
350 func (v *S3Volume) Compare(ctx context.Context, loc string, expect []byte) error {
351 errChan := make(chan error, 1)
353 _, err := v.bucket.Head("recent/"+loc, nil)
360 case err = <-errChan:
363 // Checking for "loc" itself here would interfere with
364 // future GET requests.
366 // On AWS, if X doesn't exist, a HEAD or GET request
367 // for X causes X's non-existence to be cached. Thus,
368 // if we test for X, then create X and return a
369 // signature to our client, the client might still get
370 // 404 from all keepstores when trying to read it.
372 // To avoid this, we avoid doing HEAD X or GET X until
373 // we know X has been written.
375 // Note that X might exist even though recent/X
376 // doesn't: for example, the response to HEAD recent/X
377 // might itself come from a stale cache. In such
378 // cases, we will return a false negative and
379 // PutHandler might needlessly create another replica
380 // on a different volume. That's not ideal, but it's
381 // better than passing the eventually-consistent
382 // problem on to our clients.
383 return v.translateError(err)
385 rdr, err := v.getReaderWithContext(ctx, loc)
390 return v.translateError(compareReaderWithBuf(ctx, rdr, expect, loc[:32]))
393 // Put writes a block.
394 func (v *S3Volume) Put(ctx context.Context, loc string, block []byte) error {
396 return MethodDisabledError
401 md5, err := hex.DecodeString(loc)
405 opts.ContentMD5 = base64.StdEncoding.EncodeToString(md5)
406 // In AWS regions that use V4 signatures, we need to
407 // provide ContentSHA256 up front. Otherwise, the S3
408 // library reads the request body (from our buffer)
409 // into another new buffer in order to compute the
410 // SHA256 before sending the request -- which would
411 // mean consuming 128 MiB of memory for the duration
412 // of a 64 MiB write.
413 opts.ContentSHA256 = fmt.Sprintf("%x", sha256.Sum256(block))
416 // Send the block data through a pipe, so that (if we need to)
417 // we can close the pipe early and abandon our PutReader()
418 // goroutine, without worrying about PutReader() accessing our
419 // block buffer after we release it.
420 bufr, bufw := io.Pipe()
422 io.Copy(bufw, bytes.NewReader(block))
427 ready := make(chan bool)
430 if ctx.Err() != nil {
431 theConfig.debugLogf("%s: abandoned PutReader goroutine finished with err: %s", v, err)
435 err = v.bucket.PutReader(loc, bufr, int64(size), "application/octet-stream", s3ACL, opts)
439 err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{})
443 theConfig.debugLogf("%s: taking PutReader's input away: %s", v, ctx.Err())
444 // Our pipe might be stuck in Write(), waiting for
445 // PutReader() to read. If so, un-stick it. This means
446 // PutReader will get corrupt data, but that's OK: the
447 // size and MD5 won't match, so the write will fail.
448 go io.Copy(ioutil.Discard, bufr)
449 // CloseWithError() will return once pending I/O is done.
450 bufw.CloseWithError(ctx.Err())
451 theConfig.debugLogf("%s: abandoning PutReader goroutine", v)
454 // Unblock pipe in case PutReader did not consume it.
455 io.Copy(ioutil.Discard, bufr)
456 return v.translateError(err)
460 // Touch sets the timestamp for the given locator to the current time.
461 func (v *S3Volume) Touch(loc string) error {
463 return MethodDisabledError
465 _, err := v.bucket.Head(loc, nil)
466 err = v.translateError(err)
467 if os.IsNotExist(err) && v.fixRace(loc) {
468 // The data object got trashed in a race, but fixRace
470 } else if err != nil {
473 err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{})
474 return v.translateError(err)
477 // Mtime returns the stored timestamp for the given locator.
478 func (v *S3Volume) Mtime(loc string) (time.Time, error) {
479 _, err := v.bucket.Head(loc, nil)
481 return zeroTime, v.translateError(err)
483 resp, err := v.bucket.Head("recent/"+loc, nil)
484 err = v.translateError(err)
485 if os.IsNotExist(err) {
486 // The data object X exists, but recent/X is missing.
487 err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{})
489 log.Printf("error: creating %q: %s", "recent/"+loc, err)
490 return zeroTime, v.translateError(err)
492 log.Printf("info: created %q to migrate existing block to new storage scheme", "recent/"+loc)
493 resp, err = v.bucket.Head("recent/"+loc, nil)
495 log.Printf("error: created %q but HEAD failed: %s", "recent/"+loc, err)
496 return zeroTime, v.translateError(err)
498 } else if err != nil {
499 // HEAD recent/X failed for some other reason.
502 return v.lastModified(resp)
505 // IndexTo writes a complete list of locators with the given prefix
506 // for which Get() can retrieve data.
507 func (v *S3Volume) IndexTo(prefix string, writer io.Writer) error {
508 // Use a merge sort to find matching sets of X and recent/X.
510 Bucket: v.bucket.Bucket,
512 PageSize: v.IndexPageSize,
513 Stats: &v.bucket.stats,
516 Bucket: v.bucket.Bucket,
517 Prefix: "recent/" + prefix,
518 PageSize: v.IndexPageSize,
519 Stats: &v.bucket.stats,
521 for data, recent := dataL.First(), recentL.First(); data != nil && dataL.Error() == nil; data = dataL.Next() {
523 // Conveniently, "recent/*" and "trash/*" are
524 // lexically greater than all hex-encoded data
525 // hashes, so stopping here avoids iterating
526 // over all of them needlessly with dataL.
529 if !v.isKeepBlock(data.Key) {
533 // stamp is the list entry we should use to report the
534 // last-modified time for this data block: it will be
535 // the recent/X entry if one exists, otherwise the
536 // entry for the data block itself.
539 // Advance to the corresponding recent/X marker, if any
540 for recent != nil && recentL.Error() == nil {
541 if cmp := strings.Compare(recent.Key[7:], data.Key); cmp < 0 {
542 recent = recentL.Next()
546 recent = recentL.Next()
549 // recent/X marker is missing: we'll
550 // use the timestamp on the data
555 if err := recentL.Error(); err != nil {
558 t, err := time.Parse(time.RFC3339, stamp.LastModified)
562 fmt.Fprintf(writer, "%s+%d %d\n", data.Key, data.Size, t.UnixNano())
567 // Trash a Keep block.
568 func (v *S3Volume) Trash(loc string) error {
570 return MethodDisabledError
572 if t, err := v.Mtime(loc); err != nil {
574 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
577 if theConfig.TrashLifetime == 0 {
579 return ErrS3TrashDisabled
581 return v.translateError(v.bucket.Del(loc))
583 err := v.checkRaceWindow(loc)
587 err = v.safeCopy("trash/"+loc, loc)
591 return v.translateError(v.bucket.Del(loc))
594 // checkRaceWindow returns a non-nil error if trash/loc is, or might
595 // be, in the race window (i.e., it's not safe to trash loc).
596 func (v *S3Volume) checkRaceWindow(loc string) error {
597 resp, err := v.bucket.Head("trash/"+loc, nil)
598 err = v.translateError(err)
599 if os.IsNotExist(err) {
600 // OK, trash/X doesn't exist so we're not in the race
603 } else if err != nil {
604 // Error looking up trash/X. We don't know whether
605 // we're in the race window
608 t, err := v.lastModified(resp)
610 // Can't parse timestamp
613 safeWindow := t.Add(theConfig.TrashLifetime.Duration()).Sub(time.Now().Add(time.Duration(v.RaceWindow)))
615 // We can't count on "touch trash/X" to prolong
616 // trash/X's lifetime. The new timestamp might not
617 // become visible until now+raceWindow, and EmptyTrash
618 // is allowed to delete trash/X before then.
619 return fmt.Errorf("same block is already in trash, and safe window ended %s ago", -safeWindow)
621 // trash/X exists, but it won't be eligible for deletion until
622 // after now+raceWindow, so it's safe to overwrite it.
626 // safeCopy calls PutCopy, and checks the response to make sure the
627 // copy succeeded and updated the timestamp on the destination object
628 // (PutCopy returns 200 OK if the request was received, even if the
630 func (v *S3Volume) safeCopy(dst, src string) error {
631 resp, err := v.bucket.PutCopy(dst, s3ACL, s3.CopyOptions{
632 ContentType: "application/octet-stream",
633 MetadataDirective: "REPLACE",
634 }, v.bucket.Name+"/"+src)
635 err = v.translateError(err)
636 if os.IsNotExist(err) {
638 } else if err != nil {
639 return fmt.Errorf("PutCopy(%q ← %q): %s", dst, v.bucket.Name+"/"+src, err)
641 if t, err := time.Parse(time.RFC3339Nano, resp.LastModified); err != nil {
642 return fmt.Errorf("PutCopy succeeded but did not return a timestamp: %q: %s", resp.LastModified, err)
643 } else if time.Now().Sub(t) > maxClockSkew {
644 return fmt.Errorf("PutCopy succeeded but returned an old timestamp: %q: %s", resp.LastModified, t)
649 // Get the LastModified header from resp, and parse it as RFC1123 or
650 // -- if it isn't valid RFC1123 -- as Amazon's variant of RFC1123.
651 func (v *S3Volume) lastModified(resp *http.Response) (t time.Time, err error) {
652 s := resp.Header.Get("Last-Modified")
653 t, err = time.Parse(time.RFC1123, s)
654 if err != nil && s != "" {
655 // AWS example is "Sun, 1 Jan 2006 12:00:00 GMT",
656 // which isn't quite "Sun, 01 Jan 2006 12:00:00 GMT"
657 // as required by HTTP spec. If it's not a valid HTTP
658 // header value, it's probably AWS (or s3test) giving
659 // us a nearly-RFC1123 timestamp.
660 t, err = time.Parse(nearlyRFC1123, s)
665 // Untrash moves block from trash back into store
666 func (v *S3Volume) Untrash(loc string) error {
667 err := v.safeCopy(loc, "trash/"+loc)
671 err = v.bucket.PutReader("recent/"+loc, nil, 0, "application/octet-stream", s3ACL, s3.Options{})
672 return v.translateError(err)
675 // Status returns a *VolumeStatus representing the current in-use
676 // storage capacity and a fake available capacity that doesn't make
677 // the volume seem full or nearly-full.
678 func (v *S3Volume) Status() *VolumeStatus {
679 return &VolumeStatus{
681 BytesFree: BlockSize * 1000,
686 // InternalStats returns bucket I/O and API call counters.
687 func (v *S3Volume) InternalStats() interface{} {
688 return &v.bucket.stats
691 // String implements fmt.Stringer.
692 func (v *S3Volume) String() string {
693 return fmt.Sprintf("s3-bucket:%+q", v.Bucket)
696 // Writable returns false if all future Put, Mtime, and Delete calls
697 // are expected to fail.
698 func (v *S3Volume) Writable() bool {
702 // Replication returns the storage redundancy of the underlying
703 // device. Configured via command line flag.
704 func (v *S3Volume) Replication() int {
705 return v.S3Replication
708 // GetStorageClasses implements Volume
709 func (v *S3Volume) GetStorageClasses() []string {
710 return v.StorageClasses
713 var s3KeepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
715 func (v *S3Volume) isKeepBlock(s string) bool {
716 return s3KeepBlockRegexp.MatchString(s)
719 // fixRace(X) is called when "recent/X" exists but "X" doesn't
720 // exist. If the timestamps on "recent/"+loc and "trash/"+loc indicate
721 // there was a race between Put and Trash, fixRace recovers from the
722 // race by Untrashing the block.
723 func (v *S3Volume) fixRace(loc string) bool {
724 trash, err := v.bucket.Head("trash/"+loc, nil)
726 if !os.IsNotExist(v.translateError(err)) {
727 log.Printf("error: fixRace: HEAD %q: %s", "trash/"+loc, err)
731 trashTime, err := v.lastModified(trash)
733 log.Printf("error: fixRace: parse %q: %s", trash.Header.Get("Last-Modified"), err)
737 recent, err := v.bucket.Head("recent/"+loc, nil)
739 log.Printf("error: fixRace: HEAD %q: %s", "recent/"+loc, err)
742 recentTime, err := v.lastModified(recent)
744 log.Printf("error: fixRace: parse %q: %s", recent.Header.Get("Last-Modified"), err)
748 ageWhenTrashed := trashTime.Sub(recentTime)
749 if ageWhenTrashed >= theConfig.BlobSignatureTTL.Duration() {
750 // No evidence of a race: block hasn't been written
751 // since it became eligible for Trash. No fix needed.
755 log.Printf("notice: fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", loc, trashTime, recentTime, ageWhenTrashed, theConfig.BlobSignatureTTL)
756 log.Printf("notice: fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+loc, loc)
757 err = v.safeCopy(loc, "trash/"+loc)
759 log.Printf("error: fixRace: %s", err)
765 func (v *S3Volume) translateError(err error) error {
766 switch err := err.(type) {
768 if (err.StatusCode == http.StatusNotFound && err.Code == "NoSuchKey") ||
769 strings.Contains(err.Error(), "Not Found") {
770 return os.ErrNotExist
772 // Other 404 errors like NoSuchVersion and
773 // NoSuchBucket are different problems which should
774 // get called out downstream, so we don't convert them
775 // to os.ErrNotExist.
780 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
781 // and deletes them from the volume.
782 func (v *S3Volume) EmptyTrash() {
783 var bytesInTrash, blocksInTrash, bytesDeleted, blocksDeleted int64
785 // Define "ready to delete" as "...when EmptyTrash started".
788 emptyOneKey := func(trash *s3.Key) {
790 if !v.isKeepBlock(loc) {
793 atomic.AddInt64(&bytesInTrash, trash.Size)
794 atomic.AddInt64(&blocksInTrash, 1)
796 trashT, err := time.Parse(time.RFC3339, trash.LastModified)
798 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, trash.Key, trash.LastModified, err)
801 recent, err := v.bucket.Head("recent/"+loc, nil)
802 if err != nil && os.IsNotExist(v.translateError(err)) {
803 log.Printf("warning: %s: EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", v, trash.Key, "recent/"+loc, err)
806 log.Printf("error: %s: EmptyTrash: Untrash(%q): %s", v, loc, err)
809 } else if err != nil {
810 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
813 recentT, err := v.lastModified(recent)
815 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, "recent/"+loc, recent.Header.Get("Last-Modified"), err)
818 if trashT.Sub(recentT) < theConfig.BlobSignatureTTL.Duration() {
819 if age := startT.Sub(recentT); age >= theConfig.BlobSignatureTTL.Duration()-time.Duration(v.RaceWindow) {
820 // recent/loc is too old to protect
821 // loc from being Trashed again during
822 // the raceWindow that starts if we
823 // delete trash/X now.
825 // Note this means (TrashCheckInterval
826 // < BlobSignatureTTL - raceWindow) is
827 // necessary to avoid starvation.
828 log.Printf("notice: %s: EmptyTrash: detected old race for %q, calling fixRace + Touch", v, loc)
833 _, err := v.bucket.Head(loc, nil)
834 if os.IsNotExist(err) {
835 log.Printf("notice: %s: EmptyTrash: detected recent race for %q, calling fixRace", v, loc)
838 } else if err != nil {
839 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, loc, err)
843 if startT.Sub(trashT) < theConfig.TrashLifetime.Duration() {
846 err = v.bucket.Del(trash.Key)
848 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, trash.Key, err)
851 atomic.AddInt64(&bytesDeleted, trash.Size)
852 atomic.AddInt64(&blocksDeleted, 1)
854 _, err = v.bucket.Head(loc, nil)
856 log.Printf("warning: %s: EmptyTrash: HEAD %q succeeded immediately after deleting %q", v, loc, loc)
859 if !os.IsNotExist(v.translateError(err)) {
860 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, loc, err)
863 err = v.bucket.Del("recent/" + loc)
865 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, "recent/"+loc, err)
869 var wg sync.WaitGroup
870 todo := make(chan *s3.Key, theConfig.EmptyTrashWorkers)
871 for i := 0; i < 1 || i < theConfig.EmptyTrashWorkers; i++ {
875 for key := range todo {
882 Bucket: v.bucket.Bucket,
884 PageSize: v.IndexPageSize,
885 Stats: &v.bucket.stats,
887 for trash := trashL.First(); trash != nil; trash = trashL.Next() {
893 if err := trashL.Error(); err != nil {
894 log.Printf("error: %s: EmptyTrash: lister: %s", v, err)
896 log.Printf("EmptyTrash stats for %v: Deleted %v bytes in %v blocks. Remaining in trash: %v bytes in %v blocks.", v.String(), bytesDeleted, blocksDeleted, bytesInTrash-bytesDeleted, blocksInTrash-blocksDeleted)
899 type s3Lister struct {
909 // First fetches the first page and returns the first item. It returns
910 // nil if the response is the empty set or an error occurs.
911 func (lister *s3Lister) First() *s3.Key {
916 // Next returns the next item, fetching the next page if necessary. It
917 // returns nil if the last available item has already been fetched, or
919 func (lister *s3Lister) Next() *s3.Key {
920 if len(lister.buf) == 0 && lister.nextMarker != "" {
926 // Return the most recent error encountered by First or Next.
927 func (lister *s3Lister) Error() error {
931 func (lister *s3Lister) getPage() {
932 lister.Stats.Tick(&lister.Stats.Ops, &lister.Stats.ListOps)
933 resp, err := lister.Bucket.List(lister.Prefix, "", lister.nextMarker, lister.PageSize)
934 lister.nextMarker = ""
939 if resp.IsTruncated {
940 lister.nextMarker = resp.NextMarker
942 lister.buf = make([]s3.Key, 0, len(resp.Contents))
943 for _, key := range resp.Contents {
944 if !strings.HasPrefix(key.Key, lister.Prefix) {
945 log.Printf("warning: s3Lister: S3 Bucket.List(prefix=%q) returned key %q", lister.Prefix, key.Key)
948 lister.buf = append(lister.buf, key)
952 func (lister *s3Lister) pop() (k *s3.Key) {
953 if len(lister.buf) > 0 {
955 lister.buf = lister.buf[1:]
960 // s3bucket wraps s3.bucket and counts I/O and API usage stats.
961 type s3bucket struct {
966 func (b *s3bucket) GetReader(path string) (io.ReadCloser, error) {
967 rdr, err := b.Bucket.GetReader(path)
968 b.stats.Tick(&b.stats.Ops, &b.stats.GetOps)
970 return NewCountingReader(rdr, b.stats.TickInBytes), err
973 func (b *s3bucket) Head(path string, headers map[string][]string) (*http.Response, error) {
974 resp, err := b.Bucket.Head(path, headers)
975 b.stats.Tick(&b.stats.Ops, &b.stats.HeadOps)
980 func (b *s3bucket) PutReader(path string, r io.Reader, length int64, contType string, perm s3.ACL, options s3.Options) error {
982 // goamz will only send Content-Length: 0 when reader
983 // is nil due to net.http.Request.ContentLength
984 // behavior. Otherwise, Content-Length header is
985 // omitted which will cause some S3 services
986 // (including AWS and Ceph RadosGW) to fail to create
990 r = NewCountingReader(r, b.stats.TickOutBytes)
992 err := b.Bucket.PutReader(path, r, length, contType, perm, options)
993 b.stats.Tick(&b.stats.Ops, &b.stats.PutOps)
998 func (b *s3bucket) Del(path string) error {
999 err := b.Bucket.Del(path)
1000 b.stats.Tick(&b.stats.Ops, &b.stats.DelOps)
1001 b.stats.TickErr(err)
1005 type s3bucketStats struct {
1015 func (s *s3bucketStats) TickErr(err error) {
1019 errType := fmt.Sprintf("%T", err)
1020 if err, ok := err.(*s3.Error); ok {
1021 errType = errType + fmt.Sprintf(" %d %s", err.StatusCode, err.Code)
1023 s.statsTicker.TickErr(err, errType)