20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "github.com/AdRoll/goamz/aws"
22 "github.com/AdRoll/goamz/s3"
26 // ErrS3TrashDisabled is returned by Trash if that operation
27 // is impossible with the current config.
28 ErrS3TrashDisabled = fmt.Errorf("trash function is disabled because -trash-lifetime=0 and -s3-unsafe-delete=false")
30 s3AccessKeyFile string
31 s3SecretKeyFile string
36 s3RaceWindow time.Duration
42 maxClockSkew = 600 * time.Second
43 nearlyRFC1123 = "Mon, 2 Jan 2006 15:04:05 GMT"
46 type s3VolumeAdder struct {
50 // String implements flag.Value
51 func (s *s3VolumeAdder) String() string {
55 func (s *s3VolumeAdder) Set(bucketName string) error {
57 return fmt.Errorf("no container name given")
59 if s3AccessKeyFile == "" || s3SecretKeyFile == "" {
60 return fmt.Errorf("-s3-access-key-file and -s3-secret-key-file arguments must given before -s3-bucket-volume")
62 if deprecated.flagSerializeIO {
63 log.Print("Notice: -serialize is not supported by s3-bucket volumes.")
65 s.Config.Volumes = append(s.Config.Volumes, &S3Volume{
67 AccessKeyFile: s3AccessKeyFile,
68 SecretKeyFile: s3SecretKeyFile,
71 RaceWindow: arvados.Duration(s3RaceWindow),
72 S3Replication: s3Replication,
73 UnsafeDelete: s3UnsafeDelete,
74 ReadOnly: deprecated.flagReadonly,
80 func s3regions() (okList []string) {
81 for r := range aws.Regions {
82 okList = append(okList, r)
88 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &S3Volume{} })
90 flag.Var(&s3VolumeAdder{theConfig},
92 "Use the given bucket as a storage volume. Can be given multiple times.")
97 fmt.Sprintf("AWS region used for subsequent -s3-bucket-volume arguments. Allowed values are %+q.", s3regions()))
102 "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\".")
105 "s3-access-key-file",
107 "`File` containing the access key used for subsequent -s3-bucket-volume arguments.")
110 "s3-secret-key-file",
112 "`File` containing the secret key used for subsequent -s3-bucket-volume arguments.")
117 "Maximum eventual consistency latency for subsequent -s3-bucket-volume arguments.")
122 "Replication level reported to clients for subsequent -s3-bucket-volume arguments.")
127 "EXPERIMENTAL. Enable deletion (garbage collection), even though there are known race conditions that can cause data loss.")
130 // S3Volume implements Volume using an S3 bucket.
131 type S3Volume struct {
137 LocationConstraint bool
140 ConnectTimeout arvados.Duration
141 ReadTimeout arvados.Duration
142 RaceWindow arvados.Duration
151 // Examples implements VolumeWithExamples.
152 func (*S3Volume) Examples() []Volume {
155 AccessKeyFile: "/etc/aws_s3_access_key.txt",
156 SecretKeyFile: "/etc/aws_s3_secret_key.txt",
159 Bucket: "example-bucket-name",
162 RaceWindow: arvados.Duration(24 * time.Hour),
163 ConnectTimeout: arvados.Duration(time.Minute),
164 ReadTimeout: arvados.Duration(5 * time.Minute),
167 AccessKeyFile: "/etc/gce_s3_access_key.txt",
168 SecretKeyFile: "/etc/gce_s3_secret_key.txt",
169 Endpoint: "https://storage.googleapis.com",
171 Bucket: "example-bucket-name",
174 RaceWindow: arvados.Duration(24 * time.Hour),
175 ConnectTimeout: arvados.Duration(time.Minute),
176 ReadTimeout: arvados.Duration(5 * time.Minute),
181 // Type implements Volume.
182 func (*S3Volume) Type() string {
186 // Start populates private fields and verifies the configuration is
188 func (v *S3Volume) Start() error {
189 region, ok := aws.Regions[v.Region]
190 if v.Endpoint == "" {
192 return fmt.Errorf("unrecognized region %+q; try specifying -s3-endpoint instead", v.Region)
195 return fmt.Errorf("refusing to use AWS region name %+q with endpoint %+q; "+
196 "specify empty endpoint (\"-s3-endpoint=\") or use a different region name", v.Region, v.Endpoint)
200 S3Endpoint: v.Endpoint,
201 S3LocationConstraint: v.LocationConstraint,
207 auth.AccessKey, err = readKeyFromFile(v.AccessKeyFile)
211 auth.SecretKey, err = readKeyFromFile(v.SecretKeyFile)
216 // Zero timeouts mean "wait forever", which is a bad
217 // default. Default to long timeouts instead.
218 if v.ConnectTimeout == 0 {
219 v.ConnectTimeout = arvados.Duration(time.Minute)
221 if v.ReadTimeout == 0 {
222 v.ReadTimeout = arvados.Duration(10 * time.Minute)
225 client := s3.New(auth, region)
226 client.ConnectTimeout = time.Duration(v.ConnectTimeout)
227 client.ReadTimeout = time.Duration(v.ReadTimeout)
228 v.bucket = &s3.Bucket{
235 // getReader wraps (Bucket)GetReader.
237 // In situations where (Bucket)GetReader would fail because the block
238 // disappeared in a Trash race, getReader calls fixRace to recover the
239 // data, and tries again.
240 func (v *S3Volume) getReader(loc string) (rdr io.ReadCloser, err error) {
241 rdr, err = v.bucket.GetReader(loc)
242 err = v.translateError(err)
243 if err == nil || !os.IsNotExist(err) {
246 _, err = v.bucket.Head("recent/"+loc, nil)
247 err = v.translateError(err)
249 // If we can't read recent/X, there's no point in
250 // trying fixRace. Give up.
257 rdr, err = v.bucket.GetReader(loc)
259 log.Printf("warning: reading %s after successful fixRace: %s", loc, err)
260 err = v.translateError(err)
265 // Get a block: copy the block data into buf, and return the number of
267 func (v *S3Volume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
268 ready := make(chan bool)
269 var rdr io.ReadCloser
272 rdr, err = v.getReader(loc)
277 theConfig.debugLogf("s3: abandoning getReader() because %s", ctx.Err())
286 ready = make(chan bool)
291 n, err = io.ReadFull(rdr, buf)
294 case nil, io.EOF, io.ErrUnexpectedEOF:
297 err = v.translateError(err)
302 theConfig.debugLogf("s3: interrupting ReadFull() with Close() because %s", ctx.Err())
304 // Must wait for ReadFull to return, to ensure it
305 // doesn't write to buf after we return.
306 theConfig.debugLogf("s3: waiting for ReadFull() to fail")
314 // Compare the given data with the stored data.
315 func (v *S3Volume) Compare(loc string, expect []byte) error {
316 rdr, err := v.getReader(loc)
321 return v.translateError(compareReaderWithBuf(rdr, expect, loc[:32]))
324 // Put writes a block.
325 func (v *S3Volume) Put(ctx context.Context, loc string, block []byte) error {
327 return MethodDisabledError
332 md5, err := hex.DecodeString(loc)
336 opts.ContentMD5 = base64.StdEncoding.EncodeToString(md5)
339 // Send the block data through a pipe, so that (if we need to)
340 // we can close the pipe early and abandon our PutReader()
341 // goroutine, without worrying about PutReader() accessing our
342 // block buffer after we release it.
343 bufr, bufw := io.Pipe()
345 io.Copy(bufw, bytes.NewReader(block))
350 ready := make(chan bool)
353 if ctx.Err() != nil {
354 theConfig.debugLogf("%s: abandoned PutReader goroutine finished with err: %s", v, err)
358 err = v.bucket.PutReader(loc, bufr, int64(size), "application/octet-stream", s3ACL, opts)
362 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
366 theConfig.debugLogf("%s: taking PutReader's input away: %s", v, ctx.Err())
367 // Our pipe might be stuck in Write(), waiting for
368 // io.Copy() to read. If so, un-stick it. This means
369 // PutReader will get corrupt data, but that's OK: the
370 // size and MD5 won't match, so the write will fail.
371 go io.Copy(ioutil.Discard, bufr)
372 // CloseWithError() will return once pending I/O is done.
373 bufw.CloseWithError(ctx.Err())
374 theConfig.debugLogf("%s: abandoning PutReader goroutine", v)
377 return v.translateError(err)
381 // Touch sets the timestamp for the given locator to the current time.
382 func (v *S3Volume) Touch(loc string) error {
384 return MethodDisabledError
386 _, err := v.bucket.Head(loc, nil)
387 err = v.translateError(err)
388 if os.IsNotExist(err) && v.fixRace(loc) {
389 // The data object got trashed in a race, but fixRace
391 } else if err != nil {
394 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
395 return v.translateError(err)
398 // Mtime returns the stored timestamp for the given locator.
399 func (v *S3Volume) Mtime(loc string) (time.Time, error) {
400 _, err := v.bucket.Head(loc, nil)
402 return zeroTime, v.translateError(err)
404 resp, err := v.bucket.Head("recent/"+loc, nil)
405 err = v.translateError(err)
406 if os.IsNotExist(err) {
407 // The data object X exists, but recent/X is missing.
408 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
410 log.Printf("error: creating %q: %s", "recent/"+loc, err)
411 return zeroTime, v.translateError(err)
413 log.Printf("info: created %q to migrate existing block to new storage scheme", "recent/"+loc)
414 resp, err = v.bucket.Head("recent/"+loc, nil)
416 log.Printf("error: created %q but HEAD failed: %s", "recent/"+loc, err)
417 return zeroTime, v.translateError(err)
419 } else if err != nil {
420 // HEAD recent/X failed for some other reason.
423 return v.lastModified(resp)
426 // IndexTo writes a complete list of locators with the given prefix
427 // for which Get() can retrieve data.
428 func (v *S3Volume) IndexTo(prefix string, writer io.Writer) error {
429 // Use a merge sort to find matching sets of X and recent/X.
433 PageSize: v.IndexPageSize,
437 Prefix: "recent/" + prefix,
438 PageSize: v.IndexPageSize,
440 for data, recent := dataL.First(), recentL.First(); data != nil; data = dataL.Next() {
442 // Conveniently, "recent/*" and "trash/*" are
443 // lexically greater than all hex-encoded data
444 // hashes, so stopping here avoids iterating
445 // over all of them needlessly with dataL.
448 if !v.isKeepBlock(data.Key) {
452 // stamp is the list entry we should use to report the
453 // last-modified time for this data block: it will be
454 // the recent/X entry if one exists, otherwise the
455 // entry for the data block itself.
458 // Advance to the corresponding recent/X marker, if any
460 if cmp := strings.Compare(recent.Key[7:], data.Key); cmp < 0 {
461 recent = recentL.Next()
465 recent = recentL.Next()
468 // recent/X marker is missing: we'll
469 // use the timestamp on the data
474 t, err := time.Parse(time.RFC3339, stamp.LastModified)
478 fmt.Fprintf(writer, "%s+%d %d\n", data.Key, data.Size, t.UnixNano())
483 // Trash a Keep block.
484 func (v *S3Volume) Trash(loc string) error {
486 return MethodDisabledError
488 if t, err := v.Mtime(loc); err != nil {
490 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
493 if theConfig.TrashLifetime == 0 {
495 return ErrS3TrashDisabled
497 return v.bucket.Del(loc)
499 err := v.checkRaceWindow(loc)
503 err = v.safeCopy("trash/"+loc, loc)
507 return v.translateError(v.bucket.Del(loc))
510 // checkRaceWindow returns a non-nil error if trash/loc is, or might
511 // be, in the race window (i.e., it's not safe to trash loc).
512 func (v *S3Volume) checkRaceWindow(loc string) error {
513 resp, err := v.bucket.Head("trash/"+loc, nil)
514 err = v.translateError(err)
515 if os.IsNotExist(err) {
516 // OK, trash/X doesn't exist so we're not in the race
519 } else if err != nil {
520 // Error looking up trash/X. We don't know whether
521 // we're in the race window
524 t, err := v.lastModified(resp)
526 // Can't parse timestamp
529 safeWindow := t.Add(theConfig.TrashLifetime.Duration()).Sub(time.Now().Add(time.Duration(v.RaceWindow)))
531 // We can't count on "touch trash/X" to prolong
532 // trash/X's lifetime. The new timestamp might not
533 // become visible until now+raceWindow, and EmptyTrash
534 // is allowed to delete trash/X before then.
535 return fmt.Errorf("same block is already in trash, and safe window ended %s ago", -safeWindow)
537 // trash/X exists, but it won't be eligible for deletion until
538 // after now+raceWindow, so it's safe to overwrite it.
542 // safeCopy calls PutCopy, and checks the response to make sure the
543 // copy succeeded and updated the timestamp on the destination object
544 // (PutCopy returns 200 OK if the request was received, even if the
546 func (v *S3Volume) safeCopy(dst, src string) error {
547 resp, err := v.bucket.PutCopy(dst, s3ACL, s3.CopyOptions{
548 ContentType: "application/octet-stream",
549 MetadataDirective: "REPLACE",
550 }, v.bucket.Name+"/"+src)
551 err = v.translateError(err)
555 if t, err := time.Parse(time.RFC3339Nano, resp.LastModified); err != nil {
556 return fmt.Errorf("PutCopy succeeded but did not return a timestamp: %q: %s", resp.LastModified, err)
557 } else if time.Now().Sub(t) > maxClockSkew {
558 return fmt.Errorf("PutCopy succeeded but returned an old timestamp: %q: %s", resp.LastModified, t)
563 // Get the LastModified header from resp, and parse it as RFC1123 or
564 // -- if it isn't valid RFC1123 -- as Amazon's variant of RFC1123.
565 func (v *S3Volume) lastModified(resp *http.Response) (t time.Time, err error) {
566 s := resp.Header.Get("Last-Modified")
567 t, err = time.Parse(time.RFC1123, s)
568 if err != nil && s != "" {
569 // AWS example is "Sun, 1 Jan 2006 12:00:00 GMT",
570 // which isn't quite "Sun, 01 Jan 2006 12:00:00 GMT"
571 // as required by HTTP spec. If it's not a valid HTTP
572 // header value, it's probably AWS (or s3test) giving
573 // us a nearly-RFC1123 timestamp.
574 t, err = time.Parse(nearlyRFC1123, s)
579 // Untrash moves block from trash back into store
580 func (v *S3Volume) Untrash(loc string) error {
581 err := v.safeCopy(loc, "trash/"+loc)
585 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
586 return v.translateError(err)
589 // Status returns a *VolumeStatus representing the current in-use
590 // storage capacity and a fake available capacity that doesn't make
591 // the volume seem full or nearly-full.
592 func (v *S3Volume) Status() *VolumeStatus {
593 return &VolumeStatus{
595 BytesFree: BlockSize * 1000,
600 // String implements fmt.Stringer.
601 func (v *S3Volume) String() string {
602 return fmt.Sprintf("s3-bucket:%+q", v.bucket.Name)
605 // Writable returns false if all future Put, Mtime, and Delete calls
606 // are expected to fail.
607 func (v *S3Volume) Writable() bool {
611 // Replication returns the storage redundancy of the underlying
612 // device. Configured via command line flag.
613 func (v *S3Volume) Replication() int {
614 return v.S3Replication
617 var s3KeepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
619 func (v *S3Volume) isKeepBlock(s string) bool {
620 return s3KeepBlockRegexp.MatchString(s)
623 // fixRace(X) is called when "recent/X" exists but "X" doesn't
624 // exist. If the timestamps on "recent/"+loc and "trash/"+loc indicate
625 // there was a race between Put and Trash, fixRace recovers from the
626 // race by Untrashing the block.
627 func (v *S3Volume) fixRace(loc string) bool {
628 trash, err := v.bucket.Head("trash/"+loc, nil)
630 if !os.IsNotExist(v.translateError(err)) {
631 log.Printf("error: fixRace: HEAD %q: %s", "trash/"+loc, err)
635 trashTime, err := v.lastModified(trash)
637 log.Printf("error: fixRace: parse %q: %s", trash.Header.Get("Last-Modified"), err)
641 recent, err := v.bucket.Head("recent/"+loc, nil)
643 log.Printf("error: fixRace: HEAD %q: %s", "recent/"+loc, err)
646 recentTime, err := v.lastModified(recent)
648 log.Printf("error: fixRace: parse %q: %s", recent.Header.Get("Last-Modified"), err)
652 ageWhenTrashed := trashTime.Sub(recentTime)
653 if ageWhenTrashed >= theConfig.BlobSignatureTTL.Duration() {
654 // No evidence of a race: block hasn't been written
655 // since it became eligible for Trash. No fix needed.
659 log.Printf("notice: fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", loc, trashTime, recentTime, ageWhenTrashed, theConfig.BlobSignatureTTL)
660 log.Printf("notice: fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+loc, loc)
661 err = v.safeCopy(loc, "trash/"+loc)
663 log.Printf("error: fixRace: %s", err)
669 func (v *S3Volume) translateError(err error) error {
670 switch err := err.(type) {
672 if (err.StatusCode == http.StatusNotFound && err.Code == "NoSuchKey") ||
673 strings.Contains(err.Error(), "Not Found") {
674 return os.ErrNotExist
676 // Other 404 errors like NoSuchVersion and
677 // NoSuchBucket are different problems which should
678 // get called out downstream, so we don't convert them
679 // to os.ErrNotExist.
684 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
685 // and deletes them from the volume.
686 func (v *S3Volume) EmptyTrash() {
687 var bytesInTrash, blocksInTrash, bytesDeleted, blocksDeleted int64
689 // Use a merge sort to find matching sets of trash/X and recent/X.
693 PageSize: v.IndexPageSize,
695 // Define "ready to delete" as "...when EmptyTrash started".
697 for trash := trashL.First(); trash != nil; trash = trashL.Next() {
699 if !v.isKeepBlock(loc) {
702 bytesInTrash += trash.Size
705 trashT, err := time.Parse(time.RFC3339, trash.LastModified)
707 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, trash.Key, trash.LastModified, err)
710 recent, err := v.bucket.Head("recent/"+loc, nil)
711 if err != nil && os.IsNotExist(v.translateError(err)) {
712 log.Printf("warning: %s: EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", v, trash.Key, "recent/"+loc, err)
715 log.Printf("error: %s: EmptyTrash: Untrash(%q): %s", v, loc, err)
718 } else if err != nil {
719 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
722 recentT, err := v.lastModified(recent)
724 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, "recent/"+loc, recent.Header.Get("Last-Modified"), err)
727 if trashT.Sub(recentT) < theConfig.BlobSignatureTTL.Duration() {
728 if age := startT.Sub(recentT); age >= theConfig.BlobSignatureTTL.Duration()-time.Duration(v.RaceWindow) {
729 // recent/loc is too old to protect
730 // loc from being Trashed again during
731 // the raceWindow that starts if we
732 // delete trash/X now.
734 // Note this means (TrashCheckInterval
735 // < BlobSignatureTTL - raceWindow) is
736 // necessary to avoid starvation.
737 log.Printf("notice: %s: EmptyTrash: detected old race for %q, calling fixRace + Touch", v, loc)
741 } else if _, err := v.bucket.Head(loc, nil); os.IsNotExist(err) {
742 log.Printf("notice: %s: EmptyTrash: detected recent race for %q, calling fixRace", v, loc)
745 } else if err != nil {
746 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, loc, err)
750 if startT.Sub(trashT) < theConfig.TrashLifetime.Duration() {
753 err = v.bucket.Del(trash.Key)
755 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, trash.Key, err)
758 bytesDeleted += trash.Size
761 _, err = v.bucket.Head(loc, nil)
762 if os.IsNotExist(err) {
763 err = v.bucket.Del("recent/" + loc)
765 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, "recent/"+loc, err)
767 } else if err != nil {
768 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
771 if err := trashL.Error(); err != nil {
772 log.Printf("error: %s: EmptyTrash: lister: %s", v, err)
774 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)
777 type s3Lister struct {
786 // First fetches the first page and returns the first item. It returns
787 // nil if the response is the empty set or an error occurs.
788 func (lister *s3Lister) First() *s3.Key {
793 // Next returns the next item, fetching the next page if necessary. It
794 // returns nil if the last available item has already been fetched, or
796 func (lister *s3Lister) Next() *s3.Key {
797 if len(lister.buf) == 0 && lister.nextMarker != "" {
803 // Return the most recent error encountered by First or Next.
804 func (lister *s3Lister) Error() error {
808 func (lister *s3Lister) getPage() {
809 resp, err := lister.Bucket.List(lister.Prefix, "", lister.nextMarker, lister.PageSize)
810 lister.nextMarker = ""
815 if resp.IsTruncated {
816 lister.nextMarker = resp.NextMarker
818 lister.buf = make([]s3.Key, 0, len(resp.Contents))
819 for _, key := range resp.Contents {
820 if !strings.HasPrefix(key.Key, lister.Prefix) {
821 log.Printf("warning: s3Lister: S3 Bucket.List(prefix=%q) returned key %q", lister.Prefix, key.Key)
824 lister.buf = append(lister.buf, key)
828 func (lister *s3Lister) pop() (k *s3.Key) {
829 if len(lister.buf) > 0 {
831 lister.buf = lister.buf[1:]