19 "git.curoverse.com/arvados.git/sdk/go/arvados"
20 "github.com/curoverse/azure-sdk-for-go/storage"
23 const azureDefaultRequestTimeout = arvados.Duration(10 * time.Minute)
27 azureStorageAccountName string
28 azureStorageAccountKeyFile string
29 azureStorageReplication int
30 azureWriteRaceInterval = 15 * time.Second
31 azureWriteRacePollTime = time.Second
34 func readKeyFromFile(file string) (string, error) {
35 buf, err := ioutil.ReadFile(file)
37 return "", errors.New("reading key from " + file + ": " + err.Error())
39 accountKey := strings.TrimSpace(string(buf))
41 return "", errors.New("empty account key in " + file)
43 return accountKey, nil
46 type azureVolumeAdder struct {
50 // String implements flag.Value
51 func (s *azureVolumeAdder) String() string {
55 func (s *azureVolumeAdder) Set(containerName string) error {
56 s.Config.Volumes = append(s.Config.Volumes, &AzureBlobVolume{
57 ContainerName: containerName,
58 StorageAccountName: azureStorageAccountName,
59 StorageAccountKeyFile: azureStorageAccountKeyFile,
60 AzureReplication: azureStorageReplication,
61 ReadOnly: deprecated.flagReadonly,
67 VolumeTypes = append(VolumeTypes, func() VolumeWithExamples { return &AzureBlobVolume{} })
69 flag.Var(&azureVolumeAdder{theConfig},
70 "azure-storage-container-volume",
71 "Use the given container as a storage volume. Can be given multiple times.")
73 &azureStorageAccountName,
74 "azure-storage-account-name",
76 "Azure storage account name used for subsequent --azure-storage-container-volume arguments.")
78 &azureStorageAccountKeyFile,
79 "azure-storage-account-key-file",
81 "`File` containing the account key used for subsequent --azure-storage-container-volume arguments.")
83 &azureStorageReplication,
84 "azure-storage-replication",
86 "Replication level to report to clients when data is stored in an Azure container.")
89 "azure-max-get-bytes",
91 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))
94 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
96 type AzureBlobVolume struct {
97 StorageAccountName string
98 StorageAccountKeyFile string
102 RequestTimeout arvados.Duration
104 azClient storage.Client
105 bsClient storage.BlobStorageClient
108 // Examples implements VolumeWithExamples.
109 func (*AzureBlobVolume) Examples() []Volume {
112 StorageAccountName: "example-account-name",
113 StorageAccountKeyFile: "/etc/azure_storage_account_key.txt",
114 ContainerName: "example-container-name",
116 RequestTimeout: azureDefaultRequestTimeout,
121 // Type implements Volume.
122 func (v *AzureBlobVolume) Type() string {
126 // Start implements Volume.
127 func (v *AzureBlobVolume) Start() error {
128 if v.ContainerName == "" {
129 return errors.New("no container name given")
131 if v.StorageAccountName == "" || v.StorageAccountKeyFile == "" {
132 return errors.New("StorageAccountName and StorageAccountKeyFile must be given")
134 accountKey, err := readKeyFromFile(v.StorageAccountKeyFile)
138 v.azClient, err = storage.NewBasicClient(v.StorageAccountName, accountKey)
140 return fmt.Errorf("creating Azure storage client: %s", err)
143 if v.RequestTimeout == 0 {
144 v.RequestTimeout = azureDefaultRequestTimeout
146 v.azClient.HTTPClient = &http.Client{
147 Timeout: time.Duration(v.RequestTimeout),
149 v.bsClient = v.azClient.GetBlobService()
151 ok, err := v.bsClient.ContainerExists(v.ContainerName)
156 return fmt.Errorf("Azure container %q does not exist", v.ContainerName)
161 // Return true if expires_at metadata attribute is found on the block
162 func (v *AzureBlobVolume) checkTrashed(loc string) (bool, map[string]string, error) {
163 metadata, err := v.bsClient.GetBlobMetadata(v.ContainerName, loc)
165 return false, metadata, v.translateError(err)
167 if metadata["expires_at"] != "" {
168 return true, metadata, nil
170 return false, metadata, nil
173 // Get reads a Keep block that has been stored as a block blob in the
176 // If the block is younger than azureWriteRaceInterval and is
177 // unexpectedly empty, assume a PutBlob operation is in progress, and
178 // wait for it to finish writing.
179 func (v *AzureBlobVolume) Get(loc string, buf []byte) (int, error) {
180 trashed, _, err := v.checkTrashed(loc)
185 return 0, os.ErrNotExist
187 var deadline time.Time
188 haveDeadline := false
189 size, err := v.get(loc, buf)
190 for err == nil && size == 0 && loc != "d41d8cd98f00b204e9800998ecf8427e" {
191 // Seeing a brand new empty block probably means we're
192 // in a race with CreateBlob, which under the hood
193 // (apparently) does "CreateEmpty" and "CommitData"
194 // with no additional transaction locking.
196 t, err := v.Mtime(loc)
198 log.Print("Got empty block (possible race) but Mtime failed: ", err)
201 deadline = t.Add(azureWriteRaceInterval)
202 if time.Now().After(deadline) {
205 log.Printf("Race? Block %s is 0 bytes, %s old. Polling until %s", loc, time.Since(t), deadline)
207 } else if time.Now().After(deadline) {
210 time.Sleep(azureWriteRacePollTime)
211 size, err = v.get(loc, buf)
214 log.Printf("Race ended with size==%d", size)
219 func (v *AzureBlobVolume) get(loc string, buf []byte) (int, error) {
220 expectSize := len(buf)
221 if azureMaxGetBytes < BlockSize {
222 // Unfortunately the handler doesn't tell us how long the blob
223 // is expected to be, so we have to ask Azure.
224 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
226 return 0, v.translateError(err)
228 if props.ContentLength > int64(BlockSize) || props.ContentLength < 0 {
229 return 0, fmt.Errorf("block %s invalid size %d (max %d)", loc, props.ContentLength, BlockSize)
231 expectSize = int(props.ContentLength)
238 // We'll update this actualSize if/when we get the last piece.
240 pieces := (expectSize + azureMaxGetBytes - 1) / azureMaxGetBytes
241 errors := make([]error, pieces)
242 var wg sync.WaitGroup
244 for p := 0; p < pieces; p++ {
247 startPos := p * azureMaxGetBytes
248 endPos := startPos + azureMaxGetBytes
249 if endPos > expectSize {
252 var rdr io.ReadCloser
254 if startPos == 0 && endPos == expectSize {
255 rdr, err = v.bsClient.GetBlob(v.ContainerName, loc)
257 rdr, err = v.bsClient.GetBlobRange(v.ContainerName, loc, fmt.Sprintf("%d-%d", startPos, endPos-1), nil)
264 n, err := io.ReadFull(rdr, buf[startPos:endPos])
265 if pieces == 1 && (err == io.ErrUnexpectedEOF || err == io.EOF) {
266 // If we don't know the actual size,
267 // and just tried reading 64 MiB, it's
268 // normal to encounter EOF.
269 } else if err != nil {
273 actualSize = startPos + n
278 for _, err := range errors {
280 return 0, v.translateError(err)
283 return actualSize, nil
286 // Compare the given data with existing stored data.
287 func (v *AzureBlobVolume) Compare(loc string, expect []byte) error {
288 trashed, _, err := v.checkTrashed(loc)
293 return os.ErrNotExist
295 rdr, err := v.bsClient.GetBlob(v.ContainerName, loc)
297 return v.translateError(err)
300 return compareReaderWithBuf(rdr, expect, loc[:32])
303 // Put stores a Keep block as a block blob in the container.
304 func (v *AzureBlobVolume) Put(loc string, block []byte) error {
306 return MethodDisabledError
308 return v.bsClient.CreateBlockBlobFromReader(v.ContainerName, loc, uint64(len(block)), bytes.NewReader(block), nil)
311 // Touch updates the last-modified property of a block blob.
312 func (v *AzureBlobVolume) Touch(loc string) error {
314 return MethodDisabledError
316 trashed, metadata, err := v.checkTrashed(loc)
321 return os.ErrNotExist
324 metadata["touch"] = fmt.Sprintf("%d", time.Now())
325 return v.bsClient.SetBlobMetadata(v.ContainerName, loc, metadata, nil)
328 // Mtime returns the last-modified property of a block blob.
329 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
330 trashed, _, err := v.checkTrashed(loc)
332 return time.Time{}, err
335 return time.Time{}, os.ErrNotExist
338 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
340 return time.Time{}, err
342 return time.Parse(time.RFC1123, props.LastModified)
345 // IndexTo writes a list of Keep blocks that are stored in the
347 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
348 params := storage.ListBlobsParameters{
353 resp, err := v.bsClient.ListBlobs(v.ContainerName, params)
357 for _, b := range resp.Blobs {
358 t, err := time.Parse(time.RFC1123, b.Properties.LastModified)
362 if !v.isKeepBlock(b.Name) {
365 if b.Properties.ContentLength == 0 && t.Add(azureWriteRaceInterval).After(time.Now()) {
366 // A new zero-length blob is probably
367 // just a new non-empty blob that
368 // hasn't committed its data yet (see
369 // Get()), and in any case has no
373 if b.Metadata["expires_at"] != "" {
374 // Trashed blob; exclude it from response
377 fmt.Fprintf(writer, "%s+%d %d\n", b.Name, b.Properties.ContentLength, t.UnixNano())
379 if resp.NextMarker == "" {
382 params.Marker = resp.NextMarker
386 // Trash a Keep block.
387 func (v *AzureBlobVolume) Trash(loc string) error {
389 return MethodDisabledError
392 // Ideally we would use If-Unmodified-Since, but that
393 // particular condition seems to be ignored by Azure. Instead,
394 // we get the Etag before checking Mtime, and use If-Match to
395 // ensure we don't delete data if Put() or Touch() happens
396 // between our calls to Mtime() and DeleteBlob().
397 props, err := v.bsClient.GetBlobProperties(v.ContainerName, loc)
401 if t, err := v.Mtime(loc); err != nil {
403 } else if time.Since(t) < theConfig.BlobSignatureTTL.Duration() {
407 // If TrashLifetime == 0, just delete it
408 if theConfig.TrashLifetime == 0 {
409 return v.bsClient.DeleteBlob(v.ContainerName, loc, map[string]string{
410 "If-Match": props.Etag,
414 // Otherwise, mark as trash
415 return v.bsClient.SetBlobMetadata(v.ContainerName, loc, map[string]string{
416 "expires_at": fmt.Sprintf("%d", time.Now().Add(theConfig.TrashLifetime.Duration()).Unix()),
417 }, map[string]string{
418 "If-Match": props.Etag,
422 // Untrash a Keep block.
423 // Delete the expires_at metadata attribute
424 func (v *AzureBlobVolume) Untrash(loc string) error {
425 // if expires_at does not exist, return NotFoundError
426 metadata, err := v.bsClient.GetBlobMetadata(v.ContainerName, loc)
428 return v.translateError(err)
430 if metadata["expires_at"] == "" {
431 return os.ErrNotExist
434 // reset expires_at metadata attribute
435 metadata["expires_at"] = ""
436 err = v.bsClient.SetBlobMetadata(v.ContainerName, loc, metadata, nil)
437 return v.translateError(err)
440 // Status returns a VolumeStatus struct with placeholder data.
441 func (v *AzureBlobVolume) Status() *VolumeStatus {
442 return &VolumeStatus{
444 BytesFree: BlockSize * 1000,
449 // String returns a volume label, including the container name.
450 func (v *AzureBlobVolume) String() string {
451 return fmt.Sprintf("azure-storage-container:%+q", v.ContainerName)
454 // Writable returns true, unless the -readonly flag was on when the
456 func (v *AzureBlobVolume) Writable() bool {
460 // Replication returns the replication level of the container, as
461 // specified by the -azure-storage-replication argument.
462 func (v *AzureBlobVolume) Replication() int {
463 return v.AzureReplication
466 // If possible, translate an Azure SDK error to a recognizable error
467 // like os.ErrNotExist.
468 func (v *AzureBlobVolume) translateError(err error) error {
472 case strings.Contains(err.Error(), "Not Found"):
473 // "storage: service returned without a response body (404 Not Found)"
474 return os.ErrNotExist
480 var keepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
482 func (v *AzureBlobVolume) isKeepBlock(s string) bool {
483 return keepBlockRegexp.MatchString(s)
486 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
487 // and deletes them from the volume.
488 func (v *AzureBlobVolume) EmptyTrash() {
489 var bytesDeleted, bytesInTrash int64
490 var blocksDeleted, blocksInTrash int
491 params := storage.ListBlobsParameters{Include: "metadata"}
494 resp, err := v.bsClient.ListBlobs(v.ContainerName, params)
496 log.Printf("EmptyTrash: ListBlobs: %v", err)
499 for _, b := range resp.Blobs {
500 // Check if the block is expired
501 if b.Metadata["expires_at"] == "" {
506 bytesInTrash += b.Properties.ContentLength
508 expiresAt, err := strconv.ParseInt(b.Metadata["expires_at"], 10, 64)
510 log.Printf("EmptyTrash: ParseInt(%v): %v", b.Metadata["expires_at"], err)
514 if expiresAt > time.Now().Unix() {
518 err = v.bsClient.DeleteBlob(v.ContainerName, b.Name, map[string]string{
519 "If-Match": b.Properties.Etag,
522 log.Printf("EmptyTrash: DeleteBlob(%v): %v", b.Name, err)
526 bytesDeleted += b.Properties.ContentLength
528 if resp.NextMarker == "" {
531 params.Marker = resp.NextMarker
534 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)