20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "github.com/AdRoll/goamz/aws"
22 "github.com/AdRoll/goamz/s3"
23 log "github.com/Sirupsen/logrus"
27 s3DefaultReadTimeout = arvados.Duration(10 * time.Minute)
28 s3DefaultConnectTimeout = arvados.Duration(time.Minute)
32 // ErrS3TrashDisabled is returned by Trash if that operation
33 // is impossible with the current config.
34 ErrS3TrashDisabled = fmt.Errorf("trash function is disabled because -trash-lifetime=0 and -s3-unsafe-delete=false")
36 s3AccessKeyFile string
37 s3SecretKeyFile string
42 s3RaceWindow time.Duration
48 maxClockSkew = 600 * time.Second
49 nearlyRFC1123 = "Mon, 2 Jan 2006 15:04:05 GMT"
52 type s3VolumeAdder struct {
56 // String implements flag.Value
57 func (s *s3VolumeAdder) String() string {
61 func (s *s3VolumeAdder) Set(bucketName string) error {
63 return fmt.Errorf("no container name given")
65 if s3AccessKeyFile == "" || s3SecretKeyFile == "" {
66 return fmt.Errorf("-s3-access-key-file and -s3-secret-key-file arguments must given before -s3-bucket-volume")
68 if deprecated.flagSerializeIO {
69 log.Print("Notice: -serialize is not supported by s3-bucket volumes.")
71 s.Config.Volumes = append(s.Config.Volumes, &S3Volume{
73 AccessKeyFile: s3AccessKeyFile,
74 SecretKeyFile: s3SecretKeyFile,
77 RaceWindow: arvados.Duration(s3RaceWindow),
78 S3Replication: s3Replication,
79 UnsafeDelete: s3UnsafeDelete,
80 ReadOnly: deprecated.flagReadonly,
86 func s3regions() (okList []string) {
87 for r := range aws.Regions {
88 okList = append(okList, r)
94 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &S3Volume{} })
96 flag.Var(&s3VolumeAdder{theConfig},
98 "Use the given bucket as a storage volume. Can be given multiple times.")
103 fmt.Sprintf("AWS region used for subsequent -s3-bucket-volume arguments. Allowed values are %+q.", s3regions()))
108 "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\".")
111 "s3-access-key-file",
113 "`File` containing the access key used for subsequent -s3-bucket-volume arguments.")
116 "s3-secret-key-file",
118 "`File` containing the secret key used for subsequent -s3-bucket-volume arguments.")
123 "Maximum eventual consistency latency for subsequent -s3-bucket-volume arguments.")
128 "Replication level reported to clients for subsequent -s3-bucket-volume arguments.")
133 "EXPERIMENTAL. Enable deletion (garbage collection), even though there are known race conditions that can cause data loss.")
136 // S3Volume implements Volume using an S3 bucket.
137 type S3Volume struct {
143 LocationConstraint bool
146 ConnectTimeout arvados.Duration
147 ReadTimeout arvados.Duration
148 RaceWindow arvados.Duration
157 // Examples implements VolumeWithExamples.
158 func (*S3Volume) Examples() []Volume {
161 AccessKeyFile: "/etc/aws_s3_access_key.txt",
162 SecretKeyFile: "/etc/aws_s3_secret_key.txt",
165 Bucket: "example-bucket-name",
168 RaceWindow: arvados.Duration(24 * time.Hour),
169 ConnectTimeout: arvados.Duration(time.Minute),
170 ReadTimeout: arvados.Duration(5 * time.Minute),
173 AccessKeyFile: "/etc/gce_s3_access_key.txt",
174 SecretKeyFile: "/etc/gce_s3_secret_key.txt",
175 Endpoint: "https://storage.googleapis.com",
177 Bucket: "example-bucket-name",
180 RaceWindow: arvados.Duration(24 * time.Hour),
181 ConnectTimeout: arvados.Duration(time.Minute),
182 ReadTimeout: arvados.Duration(5 * time.Minute),
187 // Type implements Volume.
188 func (*S3Volume) Type() string {
192 // Start populates private fields and verifies the configuration is
194 func (v *S3Volume) Start() error {
195 region, ok := aws.Regions[v.Region]
196 if v.Endpoint == "" {
198 return fmt.Errorf("unrecognized region %+q; try specifying -s3-endpoint instead", v.Region)
201 return fmt.Errorf("refusing to use AWS region name %+q with endpoint %+q; "+
202 "specify empty endpoint (\"-s3-endpoint=\") or use a different region name", v.Region, v.Endpoint)
206 S3Endpoint: v.Endpoint,
207 S3LocationConstraint: v.LocationConstraint,
213 auth.AccessKey, err = readKeyFromFile(v.AccessKeyFile)
217 auth.SecretKey, err = readKeyFromFile(v.SecretKeyFile)
222 // Zero timeouts mean "wait forever", which is a bad
223 // default. Default to long timeouts instead.
224 if v.ConnectTimeout == 0 {
225 v.ConnectTimeout = s3DefaultConnectTimeout
227 if v.ReadTimeout == 0 {
228 v.ReadTimeout = s3DefaultReadTimeout
231 client := s3.New(auth, region)
232 client.ConnectTimeout = time.Duration(v.ConnectTimeout)
233 client.ReadTimeout = time.Duration(v.ReadTimeout)
234 v.bucket = &s3bucket{
243 func (v *S3Volume) getReaderWithContext(ctx context.Context, loc string) (rdr io.ReadCloser, err error) {
244 ready := make(chan bool)
246 rdr, err = v.getReader(loc)
253 theConfig.debugLogf("s3: abandoning getReader(): %s", ctx.Err())
260 return nil, ctx.Err()
264 // getReader wraps (Bucket)GetReader.
266 // In situations where (Bucket)GetReader would fail because the block
267 // disappeared in a Trash race, getReader calls fixRace to recover the
268 // data, and tries again.
269 func (v *S3Volume) getReader(loc string) (rdr io.ReadCloser, err error) {
270 rdr, err = v.bucket.GetReader(loc)
271 err = v.translateError(err)
272 if err == nil || !os.IsNotExist(err) {
276 _, err = v.bucket.Head("recent/"+loc, nil)
277 err = v.translateError(err)
279 // If we can't read recent/X, there's no point in
280 // trying fixRace. Give up.
288 rdr, err = v.bucket.GetReader(loc)
290 log.Printf("warning: reading %s after successful fixRace: %s", loc, err)
291 err = v.translateError(err)
296 // Get a block: copy the block data into buf, and return the number of
298 func (v *S3Volume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
299 rdr, err := v.getReaderWithContext(ctx, loc)
305 ready := make(chan bool)
310 n, err = io.ReadFull(rdr, buf)
313 case nil, io.EOF, io.ErrUnexpectedEOF:
316 err = v.translateError(err)
321 theConfig.debugLogf("s3: interrupting ReadFull() with Close() because %s", ctx.Err())
323 // Must wait for ReadFull to return, to ensure it
324 // doesn't write to buf after we return.
325 theConfig.debugLogf("s3: waiting for ReadFull() to fail")
333 // Compare the given data with the stored data.
334 func (v *S3Volume) Compare(ctx context.Context, loc string, expect []byte) error {
335 rdr, err := v.getReaderWithContext(ctx, loc)
340 return v.translateError(compareReaderWithBuf(ctx, rdr, expect, loc[:32]))
343 // Put writes a block.
344 func (v *S3Volume) Put(ctx context.Context, loc string, block []byte) error {
346 return MethodDisabledError
351 md5, err := hex.DecodeString(loc)
355 opts.ContentMD5 = base64.StdEncoding.EncodeToString(md5)
358 // Send the block data through a pipe, so that (if we need to)
359 // we can close the pipe early and abandon our PutReader()
360 // goroutine, without worrying about PutReader() accessing our
361 // block buffer after we release it.
362 bufr, bufw := io.Pipe()
364 io.Copy(bufw, bytes.NewReader(block))
369 ready := make(chan bool)
372 if ctx.Err() != nil {
373 theConfig.debugLogf("%s: abandoned PutReader goroutine finished with err: %s", v, err)
377 err = v.bucket.PutReader(loc, bufr, int64(size), "application/octet-stream", s3ACL, opts)
381 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
385 theConfig.debugLogf("%s: taking PutReader's input away: %s", v, ctx.Err())
386 // Our pipe might be stuck in Write(), waiting for
387 // io.Copy() to read. If so, un-stick it. This means
388 // PutReader will get corrupt data, but that's OK: the
389 // size and MD5 won't match, so the write will fail.
390 go io.Copy(ioutil.Discard, bufr)
391 // CloseWithError() will return once pending I/O is done.
392 bufw.CloseWithError(ctx.Err())
393 theConfig.debugLogf("%s: abandoning PutReader goroutine", v)
396 return v.translateError(err)
400 // Touch sets the timestamp for the given locator to the current time.
401 func (v *S3Volume) Touch(loc string) error {
403 return MethodDisabledError
405 _, err := v.bucket.Head(loc, nil)
406 err = v.translateError(err)
407 if os.IsNotExist(err) && v.fixRace(loc) {
408 // The data object got trashed in a race, but fixRace
410 } else if err != nil {
413 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
414 return v.translateError(err)
417 // Mtime returns the stored timestamp for the given locator.
418 func (v *S3Volume) Mtime(loc string) (time.Time, error) {
419 _, err := v.bucket.Head(loc, nil)
421 return zeroTime, v.translateError(err)
423 resp, err := v.bucket.Head("recent/"+loc, nil)
424 err = v.translateError(err)
425 if os.IsNotExist(err) {
426 // The data object X exists, but recent/X is missing.
427 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
429 log.Printf("error: creating %q: %s", "recent/"+loc, err)
430 return zeroTime, v.translateError(err)
432 log.Printf("info: created %q to migrate existing block to new storage scheme", "recent/"+loc)
433 resp, err = v.bucket.Head("recent/"+loc, nil)
435 log.Printf("error: created %q but HEAD failed: %s", "recent/"+loc, err)
436 return zeroTime, v.translateError(err)
438 } else if err != nil {
439 // HEAD recent/X failed for some other reason.
442 return v.lastModified(resp)
445 // IndexTo writes a complete list of locators with the given prefix
446 // for which Get() can retrieve data.
447 func (v *S3Volume) IndexTo(prefix string, writer io.Writer) error {
448 // Use a merge sort to find matching sets of X and recent/X.
450 Bucket: v.bucket.Bucket,
452 PageSize: v.IndexPageSize,
455 Bucket: v.bucket.Bucket,
456 Prefix: "recent/" + prefix,
457 PageSize: v.IndexPageSize,
459 v.bucket.stats.tick(&v.bucket.stats.Ops, &v.bucket.stats.ListOps)
460 v.bucket.stats.tick(&v.bucket.stats.Ops, &v.bucket.stats.ListOps)
461 for data, recent := dataL.First(), recentL.First(); data != nil; data = dataL.Next() {
462 v.bucket.stats.tick(&v.bucket.stats.Ops, &v.bucket.stats.ListOps)
464 // Conveniently, "recent/*" and "trash/*" are
465 // lexically greater than all hex-encoded data
466 // hashes, so stopping here avoids iterating
467 // over all of them needlessly with dataL.
470 if !v.isKeepBlock(data.Key) {
474 // stamp is the list entry we should use to report the
475 // last-modified time for this data block: it will be
476 // the recent/X entry if one exists, otherwise the
477 // entry for the data block itself.
480 // Advance to the corresponding recent/X marker, if any
482 if cmp := strings.Compare(recent.Key[7:], data.Key); cmp < 0 {
483 recent = recentL.Next()
484 v.bucket.stats.tick(&v.bucket.stats.Ops, &v.bucket.stats.ListOps)
488 recent = recentL.Next()
489 v.bucket.stats.tick(&v.bucket.stats.Ops, &v.bucket.stats.ListOps)
492 // recent/X marker is missing: we'll
493 // use the timestamp on the data
498 t, err := time.Parse(time.RFC3339, stamp.LastModified)
502 fmt.Fprintf(writer, "%s+%d %d\n", data.Key, data.Size, t.UnixNano())
507 // Trash a Keep block.
508 func (v *S3Volume) Trash(loc string) error {
510 return MethodDisabledError
512 if t, err := v.Mtime(loc); err != nil {
514 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
517 if theConfig.TrashLifetime == 0 {
519 return ErrS3TrashDisabled
521 return v.translateError(v.bucket.Del(loc))
523 err := v.checkRaceWindow(loc)
527 err = v.safeCopy("trash/"+loc, loc)
531 return v.translateError(v.bucket.Del(loc))
534 // checkRaceWindow returns a non-nil error if trash/loc is, or might
535 // be, in the race window (i.e., it's not safe to trash loc).
536 func (v *S3Volume) checkRaceWindow(loc string) error {
537 resp, err := v.bucket.Head("trash/"+loc, nil)
538 err = v.translateError(err)
539 if os.IsNotExist(err) {
540 // OK, trash/X doesn't exist so we're not in the race
543 } else if err != nil {
544 // Error looking up trash/X. We don't know whether
545 // we're in the race window
548 t, err := v.lastModified(resp)
550 // Can't parse timestamp
553 safeWindow := t.Add(theConfig.TrashLifetime.Duration()).Sub(time.Now().Add(time.Duration(v.RaceWindow)))
555 // We can't count on "touch trash/X" to prolong
556 // trash/X's lifetime. The new timestamp might not
557 // become visible until now+raceWindow, and EmptyTrash
558 // is allowed to delete trash/X before then.
559 return fmt.Errorf("same block is already in trash, and safe window ended %s ago", -safeWindow)
561 // trash/X exists, but it won't be eligible for deletion until
562 // after now+raceWindow, so it's safe to overwrite it.
566 // safeCopy calls PutCopy, and checks the response to make sure the
567 // copy succeeded and updated the timestamp on the destination object
568 // (PutCopy returns 200 OK if the request was received, even if the
570 func (v *S3Volume) safeCopy(dst, src string) error {
571 resp, err := v.bucket.PutCopy(dst, s3ACL, s3.CopyOptions{
572 ContentType: "application/octet-stream",
573 MetadataDirective: "REPLACE",
574 }, v.bucket.Name+"/"+src)
575 err = v.translateError(err)
579 if t, err := time.Parse(time.RFC3339Nano, resp.LastModified); err != nil {
580 return fmt.Errorf("PutCopy succeeded but did not return a timestamp: %q: %s", resp.LastModified, err)
581 } else if time.Now().Sub(t) > maxClockSkew {
582 return fmt.Errorf("PutCopy succeeded but returned an old timestamp: %q: %s", resp.LastModified, t)
587 // Get the LastModified header from resp, and parse it as RFC1123 or
588 // -- if it isn't valid RFC1123 -- as Amazon's variant of RFC1123.
589 func (v *S3Volume) lastModified(resp *http.Response) (t time.Time, err error) {
590 s := resp.Header.Get("Last-Modified")
591 t, err = time.Parse(time.RFC1123, s)
592 if err != nil && s != "" {
593 // AWS example is "Sun, 1 Jan 2006 12:00:00 GMT",
594 // which isn't quite "Sun, 01 Jan 2006 12:00:00 GMT"
595 // as required by HTTP spec. If it's not a valid HTTP
596 // header value, it's probably AWS (or s3test) giving
597 // us a nearly-RFC1123 timestamp.
598 t, err = time.Parse(nearlyRFC1123, s)
603 // Untrash moves block from trash back into store
604 func (v *S3Volume) Untrash(loc string) error {
605 err := v.safeCopy(loc, "trash/"+loc)
609 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
610 return v.translateError(err)
613 // Status returns a *VolumeStatus representing the current in-use
614 // storage capacity and a fake available capacity that doesn't make
615 // the volume seem full or nearly-full.
616 func (v *S3Volume) Status() *VolumeStatus {
617 return &VolumeStatus{
619 BytesFree: BlockSize * 1000,
624 // InternalStats returns bucket I/O and API call counters.
625 func (v *S3Volume) InternalStats() interface{} {
626 return &v.bucket.stats
629 // String implements fmt.Stringer.
630 func (v *S3Volume) String() string {
631 return fmt.Sprintf("s3-bucket:%+q", v.Bucket)
634 // Writable returns false if all future Put, Mtime, and Delete calls
635 // are expected to fail.
636 func (v *S3Volume) Writable() bool {
640 // Replication returns the storage redundancy of the underlying
641 // device. Configured via command line flag.
642 func (v *S3Volume) Replication() int {
643 return v.S3Replication
646 var s3KeepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
648 func (v *S3Volume) isKeepBlock(s string) bool {
649 return s3KeepBlockRegexp.MatchString(s)
652 // fixRace(X) is called when "recent/X" exists but "X" doesn't
653 // exist. If the timestamps on "recent/"+loc and "trash/"+loc indicate
654 // there was a race between Put and Trash, fixRace recovers from the
655 // race by Untrashing the block.
656 func (v *S3Volume) fixRace(loc string) bool {
657 trash, err := v.bucket.Head("trash/"+loc, nil)
659 if !os.IsNotExist(v.translateError(err)) {
660 log.Printf("error: fixRace: HEAD %q: %s", "trash/"+loc, err)
664 trashTime, err := v.lastModified(trash)
666 log.Printf("error: fixRace: parse %q: %s", trash.Header.Get("Last-Modified"), err)
670 recent, err := v.bucket.Head("recent/"+loc, nil)
672 log.Printf("error: fixRace: HEAD %q: %s", "recent/"+loc, err)
675 recentTime, err := v.lastModified(recent)
677 log.Printf("error: fixRace: parse %q: %s", recent.Header.Get("Last-Modified"), err)
681 ageWhenTrashed := trashTime.Sub(recentTime)
682 if ageWhenTrashed >= theConfig.BlobSignatureTTL.Duration() {
683 // No evidence of a race: block hasn't been written
684 // since it became eligible for Trash. No fix needed.
688 log.Printf("notice: fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", loc, trashTime, recentTime, ageWhenTrashed, theConfig.BlobSignatureTTL)
689 log.Printf("notice: fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+loc, loc)
690 err = v.safeCopy(loc, "trash/"+loc)
692 log.Printf("error: fixRace: %s", err)
698 func (v *S3Volume) translateError(err error) error {
699 switch err := err.(type) {
701 if (err.StatusCode == http.StatusNotFound && err.Code == "NoSuchKey") ||
702 strings.Contains(err.Error(), "Not Found") {
703 return os.ErrNotExist
705 // Other 404 errors like NoSuchVersion and
706 // NoSuchBucket are different problems which should
707 // get called out downstream, so we don't convert them
708 // to os.ErrNotExist.
713 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
714 // and deletes them from the volume.
715 func (v *S3Volume) EmptyTrash() {
716 var bytesInTrash, blocksInTrash, bytesDeleted, blocksDeleted int64
718 // Use a merge sort to find matching sets of trash/X and recent/X.
720 Bucket: v.bucket.Bucket,
722 PageSize: v.IndexPageSize,
724 // Define "ready to delete" as "...when EmptyTrash started".
726 for trash := trashL.First(); trash != nil; trash = trashL.Next() {
728 if !v.isKeepBlock(loc) {
731 bytesInTrash += trash.Size
734 trashT, err := time.Parse(time.RFC3339, trash.LastModified)
736 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, trash.Key, trash.LastModified, err)
739 recent, err := v.bucket.Head("recent/"+loc, nil)
740 if err != nil && os.IsNotExist(v.translateError(err)) {
741 log.Printf("warning: %s: EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", v, trash.Key, "recent/"+loc, err)
744 log.Printf("error: %s: EmptyTrash: Untrash(%q): %s", v, loc, err)
747 } else if err != nil {
748 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
751 recentT, err := v.lastModified(recent)
753 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, "recent/"+loc, recent.Header.Get("Last-Modified"), err)
756 if trashT.Sub(recentT) < theConfig.BlobSignatureTTL.Duration() {
757 if age := startT.Sub(recentT); age >= theConfig.BlobSignatureTTL.Duration()-time.Duration(v.RaceWindow) {
758 // recent/loc is too old to protect
759 // loc from being Trashed again during
760 // the raceWindow that starts if we
761 // delete trash/X now.
763 // Note this means (TrashCheckInterval
764 // < BlobSignatureTTL - raceWindow) is
765 // necessary to avoid starvation.
766 log.Printf("notice: %s: EmptyTrash: detected old race for %q, calling fixRace + Touch", v, loc)
771 _, err := v.bucket.Head(loc, nil)
772 if os.IsNotExist(err) {
773 log.Printf("notice: %s: EmptyTrash: detected recent race for %q, calling fixRace", v, loc)
776 } else if err != nil {
777 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, loc, err)
781 if startT.Sub(trashT) < theConfig.TrashLifetime.Duration() {
784 err = v.bucket.Del(trash.Key)
786 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, trash.Key, err)
789 bytesDeleted += trash.Size
792 _, err = v.bucket.Head(loc, nil)
793 if os.IsNotExist(err) {
794 err = v.bucket.Del("recent/" + loc)
796 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, "recent/"+loc, err)
798 } else if err != nil {
799 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
802 if err := trashL.Error(); err != nil {
803 log.Printf("error: %s: EmptyTrash: lister: %s", v, err)
805 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)
808 type s3Lister struct {
817 // First fetches the first page and returns the first item. It returns
818 // nil if the response is the empty set or an error occurs.
819 func (lister *s3Lister) First() *s3.Key {
824 // Next returns the next item, fetching the next page if necessary. It
825 // returns nil if the last available item has already been fetched, or
827 func (lister *s3Lister) Next() *s3.Key {
828 if len(lister.buf) == 0 && lister.nextMarker != "" {
834 // Return the most recent error encountered by First or Next.
835 func (lister *s3Lister) Error() error {
839 func (lister *s3Lister) getPage() {
840 resp, err := lister.Bucket.List(lister.Prefix, "", lister.nextMarker, lister.PageSize)
841 lister.nextMarker = ""
846 if resp.IsTruncated {
847 lister.nextMarker = resp.NextMarker
849 lister.buf = make([]s3.Key, 0, len(resp.Contents))
850 for _, key := range resp.Contents {
851 if !strings.HasPrefix(key.Key, lister.Prefix) {
852 log.Printf("warning: s3Lister: S3 Bucket.List(prefix=%q) returned key %q", lister.Prefix, key.Key)
855 lister.buf = append(lister.buf, key)
859 func (lister *s3Lister) pop() (k *s3.Key) {
860 if len(lister.buf) > 0 {
862 lister.buf = lister.buf[1:]
867 // s3bucket wraps s3.bucket and counts I/O and API usage stats.
868 type s3bucket struct {
873 func (b *s3bucket) GetReader(path string) (io.ReadCloser, error) {
874 rdr, err := b.Bucket.GetReader(path)
875 b.stats.tick(&b.stats.Ops, &b.stats.GetOps)
877 return NewCountingReader(rdr, b.stats.tickInBytes), err
880 func (b *s3bucket) Head(path string, headers map[string][]string) (*http.Response, error) {
881 resp, err := b.Bucket.Head(path, headers)
882 b.stats.tick(&b.stats.Ops, &b.stats.HeadOps)
887 func (b *s3bucket) PutReader(path string, r io.Reader, length int64, contType string, perm s3.ACL, options s3.Options) error {
888 err := b.Bucket.PutReader(path, NewCountingReader(r, b.stats.tickOutBytes), length, contType, perm, options)
889 b.stats.tick(&b.stats.Ops, &b.stats.PutOps)
894 func (b *s3bucket) Put(path string, data []byte, contType string, perm s3.ACL, options s3.Options) error {
895 err := b.Bucket.PutReader(path, NewCountingReader(bytes.NewBuffer(data), b.stats.tickOutBytes), int64(len(data)), contType, perm, options)
896 b.stats.tick(&b.stats.Ops, &b.stats.PutOps)
901 func (b *s3bucket) Del(path string) error {
902 err := b.Bucket.Del(path)
903 b.stats.tick(&b.stats.Ops, &b.stats.DelOps)
908 type s3bucketStats struct {
919 ErrorCodes map[string]uint64 `json:",omitempty"`
924 func (s *s3bucketStats) tickInBytes(n uint64) {
925 atomic.AddUint64(&s.InBytes, n)
928 func (s *s3bucketStats) tickOutBytes(n uint64) {
929 atomic.AddUint64(&s.OutBytes, n)
932 func (s *s3bucketStats) tick(counters ...*uint64) {
933 for _, counter := range counters {
934 atomic.AddUint64(counter, 1)
938 func (s *s3bucketStats) tickErr(err error) {
942 atomic.AddUint64(&s.Errors, 1)
943 errStr := fmt.Sprintf("%T", err)
944 if err, ok := err.(*s3.Error); ok {
945 errStr = errStr + fmt.Sprintf(" %d %s", err.StatusCode, err.Code)
948 if s.ErrorCodes == nil {
949 s.ErrorCodes = make(map[string]uint64)
951 s.ErrorCodes[errStr]++