19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 log "github.com/Sirupsen/logrus"
21 "github.com/curoverse/azure-sdk-for-go/storage"
24 const azureDefaultRequestTimeout = arvados.Duration(10 * time.Minute)
28 azureStorageAccountName string
29 azureStorageAccountKeyFile string
30 azureStorageReplication int
31 azureWriteRaceInterval = 15 * time.Second
32 azureWriteRacePollTime = time.Second
35 func readKeyFromFile(file string) (string, error) {
36 buf, err := ioutil.ReadFile(file)
38 return "", errors.New("reading key from " + file + ": " + err.Error())
40 accountKey := strings.TrimSpace(string(buf))
42 return "", errors.New("empty account key in " + file)
44 return accountKey, nil
47 type azureVolumeAdder struct {
51 // String implements flag.Value
52 func (s *azureVolumeAdder) String() string {
56 func (s *azureVolumeAdder) Set(containerName string) error {
57 s.Config.Volumes = append(s.Config.Volumes, &AzureBlobVolume{
58 ContainerName: containerName,
59 StorageAccountName: azureStorageAccountName,
60 StorageAccountKeyFile: azureStorageAccountKeyFile,
61 AzureReplication: azureStorageReplication,
62 ReadOnly: deprecated.flagReadonly,
68 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &AzureBlobVolume{} })
70 flag.Var(&azureVolumeAdder{theConfig},
71 "azure-storage-container-volume",
72 "Use the given container as a storage volume. Can be given multiple times.")
74 &azureStorageAccountName,
75 "azure-storage-account-name",
77 "Azure storage account name used for subsequent --azure-storage-container-volume arguments.")
79 &azureStorageAccountKeyFile,
80 "azure-storage-account-key-file",
82 "`File` containing the account key used for subsequent --azure-storage-container-volume arguments.")
84 &azureStorageReplication,
85 "azure-storage-replication",
87 "Replication level to report to clients when data is stored in an Azure container.")
90 "azure-max-get-bytes",
92 fmt.Sprintf("Maximum bytes to request in a single GET request. If smaller than %d, use multiple concurrent range requests to retrieve a block.", BlockSize))
95 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
97 type AzureBlobVolume struct {
98 StorageAccountName string
99 StorageAccountKeyFile string
103 RequestTimeout arvados.Duration
105 azClient storage.Client
106 bsClient *azureBlobClient
109 // Examples implements VolumeWithExamples.
110 func (*AzureBlobVolume) Examples() []Volume {
113 StorageAccountName: "example-account-name",
114 StorageAccountKeyFile: "/etc/azure_storage_account_key.txt",
115 ContainerName: "example-container-name",
117 RequestTimeout: azureDefaultRequestTimeout,
122 // Type implements Volume.
123 func (v *AzureBlobVolume) Type() string {
127 // Start implements Volume.
128 func (v *AzureBlobVolume) Start() error {
129 if v.ContainerName == "" {
130 return errors.New("no container name given")
132 if v.StorageAccountName == "" || v.StorageAccountKeyFile == "" {
133 return errors.New("StorageAccountName and StorageAccountKeyFile must be given")
135 accountKey, err := readKeyFromFile(v.StorageAccountKeyFile)
139 v.azClient, err = storage.NewBasicClient(v.StorageAccountName, accountKey)
141 return fmt.Errorf("creating Azure storage client: %s", err)
144 if v.RequestTimeout == 0 {
145 v.RequestTimeout = azureDefaultRequestTimeout
147 v.azClient.HTTPClient = &http.Client{
148 Timeout: time.Duration(v.RequestTimeout),
150 bs := v.azClient.GetBlobService()
151 v.bsClient = &azureBlobClient{
155 ok, err := v.bsClient.ContainerExists(v.ContainerName)
160 return fmt.Errorf("Azure container %q does not exist", v.ContainerName)
165 // Return true if expires_at metadata attribute is found on the block
166 func (v *AzureBlobVolume) checkTrashed(loc string) (bool, map[string]string, error) {
167 metadata, err := v.bsClient.GetBlobMetadata(v.ContainerName, loc)
169 return false, metadata, v.translateError(err)
171 if metadata["expires_at"] != "" {
172 return true, metadata, nil
174 return false, metadata, nil
177 // Get reads a Keep block that has been stored as a block blob in the
180 // If the block is younger than azureWriteRaceInterval and is
181 // unexpectedly empty, assume a PutBlob operation is in progress, and
182 // wait for it to finish writing.
183 func (v *AzureBlobVolume) Get(ctx context.Context, loc string, buf []byte) (int, error) {
184 trashed, _, err := v.checkTrashed(loc)
189 return 0, os.ErrNotExist
191 var deadline time.Time
192 haveDeadline := false
193 size, err := v.get(ctx, loc, buf)
194 for err == nil && size == 0 && loc != "d41d8cd98f00b204e9800998ecf8427e" {
195 // Seeing a brand new empty block probably means we're
196 // in a race with CreateBlob, which under the hood
197 // (apparently) does "CreateEmpty" and "CommitData"
198 // with no additional transaction locking.
200 t, err := v.Mtime(loc)
202 log.Print("Got empty block (possible race) but Mtime failed: ", err)
205 deadline = t.Add(azureWriteRaceInterval)
206 if time.Now().After(deadline) {
209 log.Printf("Race? Block %s is 0 bytes, %s old. Polling until %s", loc, time.Since(t), deadline)
211 } else if time.Now().After(deadline) {
217 case <-time.After(azureWriteRacePollTime):
219 size, err = v.get(ctx, loc, buf)
222 log.Printf("Race ended with size==%d", size)
227 func (v *AzureBlobVolume) get(ctx context.Context, loc string, buf []byte) (int, error) {
228 ctx, cancel := context.WithCancel(ctx)
230 expectSize := len(buf)
231 if azureMaxGetBytes < BlockSize {
232 // Unfortunately the handler doesn't tell us how long the blob
233 // is expected to be, so we have to ask Azure.
234 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
236 return 0, v.translateError(err)
238 if props.ContentLength > int64(BlockSize) || props.ContentLength < 0 {
239 return 0, fmt.Errorf("block %s invalid size %d (max %d)", loc, props.ContentLength, BlockSize)
241 expectSize = int(props.ContentLength)
248 // We'll update this actualSize if/when we get the last piece.
250 pieces := (expectSize + azureMaxGetBytes - 1) / azureMaxGetBytes
251 errors := make(chan error, pieces)
252 var wg sync.WaitGroup
254 for p := 0; p < pieces; p++ {
255 // Each goroutine retrieves one piece. If we hit an
256 // error, it is sent to the errors chan so get() can
257 // return it -- but only if the error happens before
258 // ctx is done. This way, if ctx is done before we hit
259 // any other error (e.g., requesting client has hung
260 // up), we return the original ctx.Err() instead of
261 // the secondary errors from the transfers that got
262 // interrupted as a result.
265 startPos := p * azureMaxGetBytes
266 endPos := startPos + azureMaxGetBytes
267 if endPos > expectSize {
270 var rdr io.ReadCloser
272 gotRdr := make(chan struct{})
275 if startPos == 0 && endPos == expectSize {
276 rdr, err = v.bsClient.GetBlob(v.ContainerName, loc)
278 rdr, err = v.bsClient.GetBlobRange(v.ContainerName, loc, fmt.Sprintf("%d-%d", startPos, endPos-1), nil)
298 // Close the reader when the client
299 // hangs up or another piece fails
300 // (possibly interrupting ReadFull())
301 // or when all pieces succeed and
306 n, err := io.ReadFull(rdr, buf[startPos:endPos])
307 if pieces == 1 && (err == io.ErrUnexpectedEOF || err == io.EOF) {
308 // If we don't know the actual size,
309 // and just tried reading 64 MiB, it's
310 // normal to encounter EOF.
311 } else if err != nil {
312 if ctx.Err() == nil {
319 actualSize = startPos + n
326 return 0, v.translateError(<-errors)
328 if ctx.Err() != nil {
331 return actualSize, nil
334 // Compare the given data with existing stored data.
335 func (v *AzureBlobVolume) Compare(ctx context.Context, loc string, expect []byte) error {
336 trashed, _, err := v.checkTrashed(loc)
341 return os.ErrNotExist
343 var rdr io.ReadCloser
344 gotRdr := make(chan struct{})
347 rdr, err = v.bsClient.GetBlob(v.ContainerName, loc)
361 return v.translateError(err)
364 return compareReaderWithBuf(ctx, rdr, expect, loc[:32])
367 // Put stores a Keep block as a block blob in the container.
368 func (v *AzureBlobVolume) Put(ctx context.Context, loc string, block []byte) error {
370 return MethodDisabledError
372 // Send the block data through a pipe, so that (if we need to)
373 // we can close the pipe early and abandon our
374 // CreateBlockBlobFromReader() goroutine, without worrying
375 // about CreateBlockBlobFromReader() accessing our block
376 // buffer after we release it.
377 bufr, bufw := io.Pipe()
379 io.Copy(bufw, bytes.NewReader(block))
382 errChan := make(chan error)
384 errChan <- v.bsClient.CreateBlockBlobFromReader(v.ContainerName, loc, uint64(len(block)), bufr, nil)
388 theConfig.debugLogf("%s: taking CreateBlockBlobFromReader's input away: %s", v, ctx.Err())
389 // Our pipe might be stuck in Write(), waiting for
390 // io.Copy() to read. If so, un-stick it. This means
391 // CreateBlockBlobFromReader will get corrupt data,
392 // but that's OK: the size won't match, so the write
394 go io.Copy(ioutil.Discard, bufr)
395 // CloseWithError() will return once pending I/O is done.
396 bufw.CloseWithError(ctx.Err())
397 theConfig.debugLogf("%s: abandoning CreateBlockBlobFromReader goroutine", v)
399 case err := <-errChan:
404 // Touch updates the last-modified property of a block blob.
405 func (v *AzureBlobVolume) Touch(loc string) error {
407 return MethodDisabledError
409 trashed, metadata, err := v.checkTrashed(loc)
414 return os.ErrNotExist
417 metadata["touch"] = fmt.Sprintf("%d", time.Now())
418 return v.bsClient.SetBlobMetadata(v.ContainerName, loc, metadata, nil)
421 // Mtime returns the last-modified property of a block blob.
422 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
423 trashed, _, err := v.checkTrashed(loc)
425 return time.Time{}, err
428 return time.Time{}, os.ErrNotExist
431 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
433 return time.Time{}, err
435 return time.Parse(time.RFC1123, props.LastModified)
438 // IndexTo writes a list of Keep blocks that are stored in the
440 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
441 params := storage.ListBlobsParameters{
446 resp, err := v.bsClient.ListBlobs(v.ContainerName, params)
450 for _, b := range resp.Blobs {
451 t, err := time.Parse(time.RFC1123, b.Properties.LastModified)
455 if !v.isKeepBlock(b.Name) {
458 if b.Properties.ContentLength == 0 && t.Add(azureWriteRaceInterval).After(time.Now()) {
459 // A new zero-length blob is probably
460 // just a new non-empty blob that
461 // hasn't committed its data yet (see
462 // Get()), and in any case has no
466 if b.Metadata["expires_at"] != "" {
467 // Trashed blob; exclude it from response
470 fmt.Fprintf(writer, "%s+%d %d\n", b.Name, b.Properties.ContentLength, t.UnixNano())
472 if resp.NextMarker == "" {
475 params.Marker = resp.NextMarker
479 // Trash a Keep block.
480 func (v *AzureBlobVolume) Trash(loc string) error {
482 return MethodDisabledError
485 // Ideally we would use If-Unmodified-Since, but that
486 // particular condition seems to be ignored by Azure. Instead,
487 // we get the Etag before checking Mtime, and use If-Match to
488 // ensure we don't delete data if Put() or Touch() happens
489 // between our calls to Mtime() and DeleteBlob().
490 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
494 if t, err := v.Mtime(loc); err != nil {
496 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
500 // If TrashLifetime == 0, just delete it
501 if theConfig.TrashLifetime == 0 {
502 return v.bsClient.DeleteBlob(v.ContainerName, loc, map[string]string{
503 "If-Match": props.Etag,
507 // Otherwise, mark as trash
508 return v.bsClient.SetBlobMetadata(v.ContainerName, loc, map[string]string{
509 "expires_at": fmt.Sprintf("%d", time.Now().Add(theConfig.TrashLifetime.Duration()).Unix()),
510 }, map[string]string{
511 "If-Match": props.Etag,
515 // Untrash a Keep block.
516 // Delete the expires_at metadata attribute
517 func (v *AzureBlobVolume) Untrash(loc string) error {
518 // if expires_at does not exist, return NotFoundError
519 metadata, err := v.bsClient.GetBlobMetadata(v.ContainerName, loc)
521 return v.translateError(err)
523 if metadata["expires_at"] == "" {
524 return os.ErrNotExist
527 // reset expires_at metadata attribute
528 metadata["expires_at"] = ""
529 err = v.bsClient.SetBlobMetadata(v.ContainerName, loc, metadata, nil)
530 return v.translateError(err)
533 // Status returns a VolumeStatus struct with placeholder data.
534 func (v *AzureBlobVolume) Status() *VolumeStatus {
535 return &VolumeStatus{
537 BytesFree: BlockSize * 1000,
542 // String returns a volume label, including the container name.
543 func (v *AzureBlobVolume) String() string {
544 return fmt.Sprintf("azure-storage-container:%+q", v.ContainerName)
547 // Writable returns true, unless the -readonly flag was on when the
549 func (v *AzureBlobVolume) Writable() bool {
553 // Replication returns the replication level of the container, as
554 // specified by the -azure-storage-replication argument.
555 func (v *AzureBlobVolume) Replication() int {
556 return v.AzureReplication
559 // If possible, translate an Azure SDK error to a recognizable error
560 // like os.ErrNotExist.
561 func (v *AzureBlobVolume) translateError(err error) error {
565 case strings.Contains(err.Error(), "Not Found"):
566 // "storage: service returned without a response body (404 Not Found)"
567 return os.ErrNotExist
573 var keepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
575 func (v *AzureBlobVolume) isKeepBlock(s string) bool {
576 return keepBlockRegexp.MatchString(s)
579 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
580 // and deletes them from the volume.
581 func (v *AzureBlobVolume) EmptyTrash() {
582 var bytesDeleted, bytesInTrash int64
583 var blocksDeleted, blocksInTrash int
584 params := storage.ListBlobsParameters{Include: "metadata"}
587 resp, err := v.bsClient.ListBlobs(v.ContainerName, params)
589 log.Printf("EmptyTrash: ListBlobs: %v", err)
592 for _, b := range resp.Blobs {
593 // Check if the block is expired
594 if b.Metadata["expires_at"] == "" {
599 bytesInTrash += b.Properties.ContentLength
601 expiresAt, err := strconv.ParseInt(b.Metadata["expires_at"], 10, 64)
603 log.Printf("EmptyTrash: ParseInt(%v): %v", b.Metadata["expires_at"], err)
607 if expiresAt > time.Now().Unix() {
611 err = v.bsClient.DeleteBlob(v.ContainerName, b.Name, map[string]string{
612 "If-Match": b.Properties.Etag,
615 log.Printf("EmptyTrash: DeleteBlob(%v): %v", b.Name, err)
619 bytesDeleted += b.Properties.ContentLength
621 if resp.NextMarker == "" {
624 params.Marker = resp.NextMarker
627 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)
630 // InternalStats returns bucket I/O and API call counters.
631 func (v *AzureBlobVolume) InternalStats() interface{} {
632 return &v.bsClient.stats
635 type azureBlobStats struct {
640 GetMetadataOps uint64
641 GetPropertiesOps uint64
643 SetMetadataOps uint64
648 func (s *azureBlobStats) TickErr(err error) {
652 errType := fmt.Sprintf("%T", err)
653 if err, ok := err.(storage.AzureStorageServiceError); ok {
654 errType = errType + fmt.Sprintf(" %d (%s)", err.StatusCode, err.Code)
656 log.Printf("errType %T, err %s", err, err)
657 s.statsTicker.TickErr(err, errType)
660 // azureBlobClient wraps storage.BlobStorageClient in order to count
661 // I/O and API usage stats.
662 type azureBlobClient struct {
663 client *storage.BlobStorageClient
667 func (c *azureBlobClient) ContainerExists(cname string) (bool, error) {
668 c.stats.Tick(&c.stats.Ops)
669 ok, err := c.client.ContainerExists(cname)
674 func (c *azureBlobClient) GetBlobMetadata(cname, bname string) (map[string]string, error) {
675 c.stats.Tick(&c.stats.Ops, &c.stats.GetMetadataOps)
676 m, err := c.client.GetBlobMetadata(cname, bname)
681 func (c *azureBlobClient) GetBlobProperties(cname, bname string) (*storage.BlobProperties, error) {
682 c.stats.Tick(&c.stats.Ops, &c.stats.GetPropertiesOps)
683 p, err := c.client.GetBlobProperties(cname, bname)
688 func (c *azureBlobClient) GetBlob(cname, bname string) (io.ReadCloser, error) {
689 c.stats.Tick(&c.stats.Ops, &c.stats.GetOps)
690 rdr, err := c.client.GetBlob(cname, bname)
692 return NewCountingReader(rdr, c.stats.TickInBytes), err
695 func (c *azureBlobClient) GetBlobRange(cname, bname, byterange string, hdrs map[string]string) (io.ReadCloser, error) {
696 c.stats.Tick(&c.stats.Ops, &c.stats.GetRangeOps)
697 rdr, err := c.client.GetBlobRange(cname, bname, byterange, hdrs)
699 return NewCountingReader(rdr, c.stats.TickInBytes), err
702 func (c *azureBlobClient) CreateBlockBlobFromReader(cname, bname string, size uint64, rdr io.Reader, hdrs map[string]string) error {
703 c.stats.Tick(&c.stats.Ops, &c.stats.CreateOps)
704 rdr = NewCountingReader(rdr, c.stats.TickOutBytes)
705 err := c.client.CreateBlockBlobFromReader(cname, bname, size, rdr, hdrs)
710 func (c *azureBlobClient) SetBlobMetadata(cname, bname string, m, hdrs map[string]string) error {
711 c.stats.Tick(&c.stats.Ops, &c.stats.SetMetadataOps)
712 err := c.client.SetBlobMetadata(cname, bname, m, hdrs)
717 func (c *azureBlobClient) ListBlobs(cname string, params storage.ListBlobsParameters) (storage.BlobListResponse, error) {
718 c.stats.Tick(&c.stats.Ops, &c.stats.ListOps)
719 resp, err := c.client.ListBlobs(cname, params)
724 func (c *azureBlobClient) DeleteBlob(cname, bname string, hdrs map[string]string) error {
725 c.stats.Tick(&c.stats.Ops, &c.stats.DelOps)
726 err := c.client.DeleteBlob(cname, bname, hdrs)