18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "github.com/AdRoll/goamz/aws"
20 "github.com/AdRoll/goamz/s3"
24 // ErrS3TrashDisabled is returned by Trash if that operation
25 // is impossible with the current config.
26 ErrS3TrashDisabled = fmt.Errorf("trash function is disabled because -trash-lifetime=0 and -s3-unsafe-delete=false")
28 s3AccessKeyFile string
29 s3SecretKeyFile string
34 s3RaceWindow time.Duration
40 maxClockSkew = 600 * time.Second
41 nearlyRFC1123 = "Mon, 2 Jan 2006 15:04:05 GMT"
44 type s3VolumeAdder struct {
48 // String implements flag.Value
49 func (s *s3VolumeAdder) String() string {
53 func (s *s3VolumeAdder) Set(bucketName string) error {
55 return fmt.Errorf("no container name given")
57 if s3AccessKeyFile == "" || s3SecretKeyFile == "" {
58 return fmt.Errorf("-s3-access-key-file and -s3-secret-key-file arguments must given before -s3-bucket-volume")
60 if deprecated.flagSerializeIO {
61 log.Print("Notice: -serialize is not supported by s3-bucket volumes.")
63 s.Config.Volumes = append(s.Config.Volumes, &S3Volume{
65 AccessKeyFile: s3AccessKeyFile,
66 SecretKeyFile: s3SecretKeyFile,
69 RaceWindow: arvados.Duration(s3RaceWindow),
70 S3Replication: s3Replication,
71 UnsafeDelete: s3UnsafeDelete,
72 ReadOnly: deprecated.flagReadonly,
78 func s3regions() (okList []string) {
79 for r := range aws.Regions {
80 okList = append(okList, r)
86 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &S3Volume{} })
88 flag.Var(&s3VolumeAdder{theConfig},
90 "Use the given bucket as a storage volume. Can be given multiple times.")
95 fmt.Sprintf("AWS region used for subsequent -s3-bucket-volume arguments. Allowed values are %+q.", s3regions()))
100 "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\".")
103 "s3-access-key-file",
105 "`File` containing the access key used for subsequent -s3-bucket-volume arguments.")
108 "s3-secret-key-file",
110 "`File` containing the secret key used for subsequent -s3-bucket-volume arguments.")
115 "Maximum eventual consistency latency for subsequent -s3-bucket-volume arguments.")
120 "Replication level reported to clients for subsequent -s3-bucket-volume arguments.")
125 "EXPERIMENTAL. Enable deletion (garbage collection), even though there are known race conditions that can cause data loss.")
128 // S3Volume implements Volume using an S3 bucket.
129 type S3Volume struct {
135 LocationConstraint bool
138 ConnectTimeout arvados.Duration
139 ReadTimeout arvados.Duration
140 RaceWindow arvados.Duration
149 // Examples implements VolumeWithExamples.
150 func (*S3Volume) Examples() []Volume {
153 AccessKeyFile: "/etc/aws_s3_access_key.txt",
154 SecretKeyFile: "/etc/aws_s3_secret_key.txt",
157 Bucket: "example-bucket-name",
160 RaceWindow: arvados.Duration(24 * time.Hour),
161 ConnectTimeout: arvados.Duration(time.Minute),
162 ReadTimeout: arvados.Duration(5 * time.Minute),
165 AccessKeyFile: "/etc/gce_s3_access_key.txt",
166 SecretKeyFile: "/etc/gce_s3_secret_key.txt",
167 Endpoint: "https://storage.googleapis.com",
169 Bucket: "example-bucket-name",
172 RaceWindow: arvados.Duration(24 * time.Hour),
173 ConnectTimeout: arvados.Duration(time.Minute),
174 ReadTimeout: arvados.Duration(5 * time.Minute),
179 // Type implements Volume.
180 func (*S3Volume) Type() string {
184 // Start populates private fields and verifies the configuration is
186 func (v *S3Volume) Start() error {
187 region, ok := aws.Regions[v.Region]
188 if v.Endpoint == "" {
190 return fmt.Errorf("unrecognized region %+q; try specifying -s3-endpoint instead", v.Region)
193 return fmt.Errorf("refusing to use AWS region name %+q with endpoint %+q; "+
194 "specify empty endpoint (\"-s3-endpoint=\") or use a different region name", v.Region, v.Endpoint)
198 S3Endpoint: v.Endpoint,
199 S3LocationConstraint: v.LocationConstraint,
205 auth.AccessKey, err = readKeyFromFile(v.AccessKeyFile)
209 auth.SecretKey, err = readKeyFromFile(v.SecretKeyFile)
214 // Zero timeouts mean "wait forever", which is a bad
215 // default. Default to long timeouts instead.
216 if v.ConnectTimeout == 0 {
217 v.ConnectTimeout = arvados.Duration(time.Minute)
219 if v.ReadTimeout == 0 {
220 v.ReadTimeout = arvados.Duration(10 * time.Minute)
223 client := s3.New(auth, region)
224 client.ConnectTimeout = time.Duration(v.ConnectTimeout)
225 client.ReadTimeout = time.Duration(v.ReadTimeout)
226 v.bucket = &s3.Bucket{
233 // getReader wraps (Bucket)GetReader.
235 // In situations where (Bucket)GetReader would fail because the block
236 // disappeared in a Trash race, getReader calls fixRace to recover the
237 // data, and tries again.
238 func (v *S3Volume) getReader(loc string) (rdr io.ReadCloser, err error) {
239 rdr, err = v.bucket.GetReader(loc)
240 err = v.translateError(err)
241 if err == nil || !os.IsNotExist(err) {
244 _, err = v.bucket.Head("recent/"+loc, nil)
245 err = v.translateError(err)
247 // If we can't read recent/X, there's no point in
248 // trying fixRace. Give up.
255 rdr, err = v.bucket.GetReader(loc)
257 log.Printf("warning: reading %s after successful fixRace: %s", loc, err)
258 err = v.translateError(err)
263 // Get a block: copy the block data into buf, and return the number of
265 func (v *S3Volume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
266 ready := make(chan bool)
267 var rdr io.ReadCloser
270 rdr, err = v.getReader(loc)
275 // Client hung up before we could even send our S3 request
284 ready = make(chan bool)
289 n, err = io.ReadFull(rdr, buf)
292 case nil, io.EOF, io.ErrUnexpectedEOF:
295 err = v.translateError(err)
301 // Must wait for ReadFull to return, to ensure it
302 // doesn't write to buf after we return.
310 // Compare the given data with the stored data.
311 func (v *S3Volume) Compare(loc string, expect []byte) error {
312 rdr, err := v.getReader(loc)
317 return v.translateError(compareReaderWithBuf(rdr, expect, loc[:32]))
320 // Put writes a block.
321 func (v *S3Volume) Put(loc string, block []byte) error {
323 return MethodDisabledError
327 md5, err := hex.DecodeString(loc)
331 opts.ContentMD5 = base64.StdEncoding.EncodeToString(md5)
333 err := v.bucket.Put(loc, block, "application/octet-stream", s3ACL, opts)
335 return v.translateError(err)
337 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
338 return v.translateError(err)
341 // Touch sets the timestamp for the given locator to the current time.
342 func (v *S3Volume) Touch(loc string) error {
344 return MethodDisabledError
346 _, err := v.bucket.Head(loc, nil)
347 err = v.translateError(err)
348 if os.IsNotExist(err) && v.fixRace(loc) {
349 // The data object got trashed in a race, but fixRace
351 } else if err != nil {
354 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
355 return v.translateError(err)
358 // Mtime returns the stored timestamp for the given locator.
359 func (v *S3Volume) Mtime(loc string) (time.Time, error) {
360 _, err := v.bucket.Head(loc, nil)
362 return zeroTime, v.translateError(err)
364 resp, err := v.bucket.Head("recent/"+loc, nil)
365 err = v.translateError(err)
366 if os.IsNotExist(err) {
367 // The data object X exists, but recent/X is missing.
368 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
370 log.Printf("error: creating %q: %s", "recent/"+loc, err)
371 return zeroTime, v.translateError(err)
373 log.Printf("info: created %q to migrate existing block to new storage scheme", "recent/"+loc)
374 resp, err = v.bucket.Head("recent/"+loc, nil)
376 log.Printf("error: created %q but HEAD failed: %s", "recent/"+loc, err)
377 return zeroTime, v.translateError(err)
379 } else if err != nil {
380 // HEAD recent/X failed for some other reason.
383 return v.lastModified(resp)
386 // IndexTo writes a complete list of locators with the given prefix
387 // for which Get() can retrieve data.
388 func (v *S3Volume) IndexTo(prefix string, writer io.Writer) error {
389 // Use a merge sort to find matching sets of X and recent/X.
393 PageSize: v.IndexPageSize,
397 Prefix: "recent/" + prefix,
398 PageSize: v.IndexPageSize,
400 for data, recent := dataL.First(), recentL.First(); data != nil; data = dataL.Next() {
402 // Conveniently, "recent/*" and "trash/*" are
403 // lexically greater than all hex-encoded data
404 // hashes, so stopping here avoids iterating
405 // over all of them needlessly with dataL.
408 if !v.isKeepBlock(data.Key) {
412 // stamp is the list entry we should use to report the
413 // last-modified time for this data block: it will be
414 // the recent/X entry if one exists, otherwise the
415 // entry for the data block itself.
418 // Advance to the corresponding recent/X marker, if any
420 if cmp := strings.Compare(recent.Key[7:], data.Key); cmp < 0 {
421 recent = recentL.Next()
425 recent = recentL.Next()
428 // recent/X marker is missing: we'll
429 // use the timestamp on the data
434 t, err := time.Parse(time.RFC3339, stamp.LastModified)
438 fmt.Fprintf(writer, "%s+%d %d\n", data.Key, data.Size, t.UnixNano())
443 // Trash a Keep block.
444 func (v *S3Volume) Trash(loc string) error {
446 return MethodDisabledError
448 if t, err := v.Mtime(loc); err != nil {
450 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
453 if theConfig.TrashLifetime == 0 {
455 return ErrS3TrashDisabled
457 return v.bucket.Del(loc)
459 err := v.checkRaceWindow(loc)
463 err = v.safeCopy("trash/"+loc, loc)
467 return v.translateError(v.bucket.Del(loc))
470 // checkRaceWindow returns a non-nil error if trash/loc is, or might
471 // be, in the race window (i.e., it's not safe to trash loc).
472 func (v *S3Volume) checkRaceWindow(loc string) error {
473 resp, err := v.bucket.Head("trash/"+loc, nil)
474 err = v.translateError(err)
475 if os.IsNotExist(err) {
476 // OK, trash/X doesn't exist so we're not in the race
479 } else if err != nil {
480 // Error looking up trash/X. We don't know whether
481 // we're in the race window
484 t, err := v.lastModified(resp)
486 // Can't parse timestamp
489 safeWindow := t.Add(theConfig.TrashLifetime.Duration()).Sub(time.Now().Add(time.Duration(v.RaceWindow)))
491 // We can't count on "touch trash/X" to prolong
492 // trash/X's lifetime. The new timestamp might not
493 // become visible until now+raceWindow, and EmptyTrash
494 // is allowed to delete trash/X before then.
495 return fmt.Errorf("same block is already in trash, and safe window ended %s ago", -safeWindow)
497 // trash/X exists, but it won't be eligible for deletion until
498 // after now+raceWindow, so it's safe to overwrite it.
502 // safeCopy calls PutCopy, and checks the response to make sure the
503 // copy succeeded and updated the timestamp on the destination object
504 // (PutCopy returns 200 OK if the request was received, even if the
506 func (v *S3Volume) safeCopy(dst, src string) error {
507 resp, err := v.bucket.PutCopy(dst, s3ACL, s3.CopyOptions{
508 ContentType: "application/octet-stream",
509 MetadataDirective: "REPLACE",
510 }, v.bucket.Name+"/"+src)
511 err = v.translateError(err)
515 if t, err := time.Parse(time.RFC3339Nano, resp.LastModified); err != nil {
516 return fmt.Errorf("PutCopy succeeded but did not return a timestamp: %q: %s", resp.LastModified, err)
517 } else if time.Now().Sub(t) > maxClockSkew {
518 return fmt.Errorf("PutCopy succeeded but returned an old timestamp: %q: %s", resp.LastModified, t)
523 // Get the LastModified header from resp, and parse it as RFC1123 or
524 // -- if it isn't valid RFC1123 -- as Amazon's variant of RFC1123.
525 func (v *S3Volume) lastModified(resp *http.Response) (t time.Time, err error) {
526 s := resp.Header.Get("Last-Modified")
527 t, err = time.Parse(time.RFC1123, s)
528 if err != nil && s != "" {
529 // AWS example is "Sun, 1 Jan 2006 12:00:00 GMT",
530 // which isn't quite "Sun, 01 Jan 2006 12:00:00 GMT"
531 // as required by HTTP spec. If it's not a valid HTTP
532 // header value, it's probably AWS (or s3test) giving
533 // us a nearly-RFC1123 timestamp.
534 t, err = time.Parse(nearlyRFC1123, s)
539 // Untrash moves block from trash back into store
540 func (v *S3Volume) Untrash(loc string) error {
541 err := v.safeCopy(loc, "trash/"+loc)
545 err = v.bucket.Put("recent/"+loc, nil, "application/octet-stream", s3ACL, s3.Options{})
546 return v.translateError(err)
549 // Status returns a *VolumeStatus representing the current in-use
550 // storage capacity and a fake available capacity that doesn't make
551 // the volume seem full or nearly-full.
552 func (v *S3Volume) Status() *VolumeStatus {
553 return &VolumeStatus{
555 BytesFree: BlockSize * 1000,
560 // String implements fmt.Stringer.
561 func (v *S3Volume) String() string {
562 return fmt.Sprintf("s3-bucket:%+q", v.bucket.Name)
565 // Writable returns false if all future Put, Mtime, and Delete calls
566 // are expected to fail.
567 func (v *S3Volume) Writable() bool {
571 // Replication returns the storage redundancy of the underlying
572 // device. Configured via command line flag.
573 func (v *S3Volume) Replication() int {
574 return v.S3Replication
577 var s3KeepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
579 func (v *S3Volume) isKeepBlock(s string) bool {
580 return s3KeepBlockRegexp.MatchString(s)
583 // fixRace(X) is called when "recent/X" exists but "X" doesn't
584 // exist. If the timestamps on "recent/"+loc and "trash/"+loc indicate
585 // there was a race between Put and Trash, fixRace recovers from the
586 // race by Untrashing the block.
587 func (v *S3Volume) fixRace(loc string) bool {
588 trash, err := v.bucket.Head("trash/"+loc, nil)
590 if !os.IsNotExist(v.translateError(err)) {
591 log.Printf("error: fixRace: HEAD %q: %s", "trash/"+loc, err)
595 trashTime, err := v.lastModified(trash)
597 log.Printf("error: fixRace: parse %q: %s", trash.Header.Get("Last-Modified"), err)
601 recent, err := v.bucket.Head("recent/"+loc, nil)
603 log.Printf("error: fixRace: HEAD %q: %s", "recent/"+loc, err)
606 recentTime, err := v.lastModified(recent)
608 log.Printf("error: fixRace: parse %q: %s", recent.Header.Get("Last-Modified"), err)
612 ageWhenTrashed := trashTime.Sub(recentTime)
613 if ageWhenTrashed >= theConfig.BlobSignatureTTL.Duration() {
614 // No evidence of a race: block hasn't been written
615 // since it became eligible for Trash. No fix needed.
619 log.Printf("notice: fixRace: %q: trashed at %s but touched at %s (age when trashed = %s < %s)", loc, trashTime, recentTime, ageWhenTrashed, theConfig.BlobSignatureTTL)
620 log.Printf("notice: fixRace: copying %q to %q to recover from race between Put/Touch and Trash", "recent/"+loc, loc)
621 err = v.safeCopy(loc, "trash/"+loc)
623 log.Printf("error: fixRace: %s", err)
629 func (v *S3Volume) translateError(err error) error {
630 switch err := err.(type) {
632 if (err.StatusCode == http.StatusNotFound && err.Code == "NoSuchKey") ||
633 strings.Contains(err.Error(), "Not Found") {
634 return os.ErrNotExist
636 // Other 404 errors like NoSuchVersion and
637 // NoSuchBucket are different problems which should
638 // get called out downstream, so we don't convert them
639 // to os.ErrNotExist.
644 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
645 // and deletes them from the volume.
646 func (v *S3Volume) EmptyTrash() {
647 var bytesInTrash, blocksInTrash, bytesDeleted, blocksDeleted int64
649 // Use a merge sort to find matching sets of trash/X and recent/X.
653 PageSize: v.IndexPageSize,
655 // Define "ready to delete" as "...when EmptyTrash started".
657 for trash := trashL.First(); trash != nil; trash = trashL.Next() {
659 if !v.isKeepBlock(loc) {
662 bytesInTrash += trash.Size
665 trashT, err := time.Parse(time.RFC3339, trash.LastModified)
667 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, trash.Key, trash.LastModified, err)
670 recent, err := v.bucket.Head("recent/"+loc, nil)
671 if err != nil && os.IsNotExist(v.translateError(err)) {
672 log.Printf("warning: %s: EmptyTrash: found trash marker %q but no %q (%s); calling Untrash", v, trash.Key, "recent/"+loc, err)
675 log.Printf("error: %s: EmptyTrash: Untrash(%q): %s", v, loc, err)
678 } else if err != nil {
679 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
682 recentT, err := v.lastModified(recent)
684 log.Printf("warning: %s: EmptyTrash: %q: parse %q: %s", v, "recent/"+loc, recent.Header.Get("Last-Modified"), err)
687 if trashT.Sub(recentT) < theConfig.BlobSignatureTTL.Duration() {
688 if age := startT.Sub(recentT); age >= theConfig.BlobSignatureTTL.Duration()-time.Duration(v.RaceWindow) {
689 // recent/loc is too old to protect
690 // loc from being Trashed again during
691 // the raceWindow that starts if we
692 // delete trash/X now.
694 // Note this means (TrashCheckInterval
695 // < BlobSignatureTTL - raceWindow) is
696 // necessary to avoid starvation.
697 log.Printf("notice: %s: EmptyTrash: detected old race for %q, calling fixRace + Touch", v, loc)
701 } else if _, err := v.bucket.Head(loc, nil); os.IsNotExist(err) {
702 log.Printf("notice: %s: EmptyTrash: detected recent race for %q, calling fixRace", v, loc)
705 } else if err != nil {
706 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, loc, err)
710 if startT.Sub(trashT) < theConfig.TrashLifetime.Duration() {
713 err = v.bucket.Del(trash.Key)
715 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, trash.Key, err)
718 bytesDeleted += trash.Size
721 _, err = v.bucket.Head(loc, nil)
722 if os.IsNotExist(err) {
723 err = v.bucket.Del("recent/" + loc)
725 log.Printf("warning: %s: EmptyTrash: deleting %q: %s", v, "recent/"+loc, err)
727 } else if err != nil {
728 log.Printf("warning: %s: EmptyTrash: HEAD %q: %s", v, "recent/"+loc, err)
731 if err := trashL.Error(); err != nil {
732 log.Printf("error: %s: EmptyTrash: lister: %s", v, err)
734 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)
737 type s3Lister struct {
746 // First fetches the first page and returns the first item. It returns
747 // nil if the response is the empty set or an error occurs.
748 func (lister *s3Lister) First() *s3.Key {
753 // Next returns the next item, fetching the next page if necessary. It
754 // returns nil if the last available item has already been fetched, or
756 func (lister *s3Lister) Next() *s3.Key {
757 if len(lister.buf) == 0 && lister.nextMarker != "" {
763 // Return the most recent error encountered by First or Next.
764 func (lister *s3Lister) Error() error {
768 func (lister *s3Lister) getPage() {
769 resp, err := lister.Bucket.List(lister.Prefix, "", lister.nextMarker, lister.PageSize)
770 lister.nextMarker = ""
775 if resp.IsTruncated {
776 lister.nextMarker = resp.NextMarker
778 lister.buf = make([]s3.Key, 0, len(resp.Contents))
779 for _, key := range resp.Contents {
780 if !strings.HasPrefix(key.Key, lister.Prefix) {
781 log.Printf("warning: s3Lister: S3 Bucket.List(prefix=%q) returned key %q", lister.Prefix, key.Key)
784 lister.buf = append(lister.buf, key)
788 func (lister *s3Lister) pop() (k *s3.Key) {
789 if len(lister.buf) > 0 {
791 lister.buf = lister.buf[1:]