17 "github.com/curoverse/azure-sdk-for-go/storage"
22 azureStorageAccountName string
23 azureStorageAccountKeyFile string
24 azureStorageReplication int
25 azureWriteRaceInterval = 15 * time.Second
26 azureWriteRacePollTime = time.Second
29 func readKeyFromFile(file string) (string, error) {
30 buf, err := ioutil.ReadFile(file)
32 return "", errors.New("reading key from " + file + ": " + err.Error())
34 accountKey := strings.TrimSpace(string(buf))
36 return "", errors.New("empty account key in " + file)
38 return accountKey, nil
41 type azureVolumeAdder struct {
45 func (s *azureVolumeAdder) Set(containerName string) error {
46 if trashLifetime != 0 {
47 return ErrNotImplemented
50 if containerName == "" {
51 return errors.New("no container name given")
53 if azureStorageAccountName == "" || azureStorageAccountKeyFile == "" {
54 return errors.New("-azure-storage-account-name and -azure-storage-account-key-file arguments must given before -azure-storage-container-volume")
56 accountKey, err := readKeyFromFile(azureStorageAccountKeyFile)
60 azClient, err := storage.NewBasicClient(azureStorageAccountName, accountKey)
62 return errors.New("creating Azure storage client: " + err.Error())
65 log.Print("Notice: -serialize is not supported by azure-blob-container volumes.")
67 v := NewAzureBlobVolume(azClient, containerName, flagReadonly, azureStorageReplication)
68 if err := v.Check(); err != nil {
71 *s.volumeSet = append(*s.volumeSet, v)
76 flag.Var(&azureVolumeAdder{&volumes},
77 "azure-storage-container-volume",
78 "Use the given container as a storage volume. Can be given multiple times.")
80 &azureStorageAccountName,
81 "azure-storage-account-name",
83 "Azure storage account name used for subsequent --azure-storage-container-volume arguments.")
85 &azureStorageAccountKeyFile,
86 "azure-storage-account-key-file",
88 "File containing the account key used for subsequent --azure-storage-container-volume arguments.")
90 &azureStorageReplication,
91 "azure-storage-replication",
93 "Replication level to report to clients when data is stored in an Azure container.")
96 "azure-max-get-bytes",
98 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))
101 // An AzureBlobVolume stores and retrieves blocks in an Azure Blob
103 type AzureBlobVolume struct {
104 azClient storage.Client
105 bsClient storage.BlobStorageClient
111 // NewAzureBlobVolume returns a new AzureBlobVolume using the given
112 // client and container name. The replication argument specifies the
113 // replication level to report when writing data.
114 func NewAzureBlobVolume(client storage.Client, containerName string, readonly bool, replication int) *AzureBlobVolume {
115 return &AzureBlobVolume{
117 bsClient: client.GetBlobService(),
118 containerName: containerName,
120 replication: replication,
124 // Check returns nil if the volume is usable.
125 func (v *AzureBlobVolume) Check() error {
126 ok, err := v.bsClient.ContainerExists(v.containerName)
131 return errors.New("container does not exist")
136 // Get reads a Keep block that has been stored as a block blob in the
139 // If the block is younger than azureWriteRaceInterval and is
140 // unexpectedly empty, assume a PutBlob operation is in progress, and
141 // wait for it to finish writing.
142 func (v *AzureBlobVolume) Get(loc string) ([]byte, error) {
143 var deadline time.Time
144 haveDeadline := false
145 buf, err := v.get(loc)
146 for err == nil && len(buf) == 0 && loc != "d41d8cd98f00b204e9800998ecf8427e" {
147 // Seeing a brand new empty block probably means we're
148 // in a race with CreateBlob, which under the hood
149 // (apparently) does "CreateEmpty" and "CommitData"
150 // with no additional transaction locking.
152 t, err := v.Mtime(loc)
154 log.Print("Got empty block (possible race) but Mtime failed: ", err)
157 deadline = t.Add(azureWriteRaceInterval)
158 if time.Now().After(deadline) {
161 log.Printf("Race? Block %s is 0 bytes, %s old. Polling until %s", loc, time.Since(t), deadline)
163 } else if time.Now().After(deadline) {
167 time.Sleep(azureWriteRacePollTime)
168 buf, err = v.get(loc)
171 log.Printf("Race ended with len(buf)==%d", len(buf))
176 func (v *AzureBlobVolume) get(loc string) ([]byte, error) {
177 expectSize := BlockSize
178 if azureMaxGetBytes < BlockSize {
179 // Unfortunately the handler doesn't tell us how long the blob
180 // is expected to be, so we have to ask Azure.
181 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
183 return nil, v.translateError(err)
185 if props.ContentLength > int64(BlockSize) || props.ContentLength < 0 {
186 return nil, fmt.Errorf("block %s invalid size %d (max %d)", loc, props.ContentLength, BlockSize)
188 expectSize = int(props.ContentLength)
191 buf := bufs.Get(expectSize)
196 // We'll update this actualSize if/when we get the last piece.
198 pieces := (expectSize + azureMaxGetBytes - 1) / azureMaxGetBytes
199 errors := make([]error, pieces)
200 var wg sync.WaitGroup
202 for p := 0; p < pieces; p++ {
205 startPos := p * azureMaxGetBytes
206 endPos := startPos + azureMaxGetBytes
207 if endPos > expectSize {
210 var rdr io.ReadCloser
212 if startPos == 0 && endPos == expectSize {
213 rdr, err = v.bsClient.GetBlob(v.containerName, loc)
215 rdr, err = v.bsClient.GetBlobRange(v.containerName, loc, fmt.Sprintf("%d-%d", startPos, endPos-1))
222 n, err := io.ReadFull(rdr, buf[startPos:endPos])
223 if pieces == 1 && (err == io.ErrUnexpectedEOF || err == io.EOF) {
224 // If we don't know the actual size,
225 // and just tried reading 64 MiB, it's
226 // normal to encounter EOF.
227 } else if err != nil {
231 actualSize = startPos + n
236 for _, err := range errors {
239 return nil, v.translateError(err)
242 return buf[:actualSize], nil
245 // Compare the given data with existing stored data.
246 func (v *AzureBlobVolume) Compare(loc string, expect []byte) error {
247 rdr, err := v.bsClient.GetBlob(v.containerName, loc)
249 return v.translateError(err)
252 return compareReaderWithBuf(rdr, expect, loc[:32])
255 // Put stores a Keep block as a block blob in the container.
256 func (v *AzureBlobVolume) Put(loc string, block []byte) error {
258 return MethodDisabledError
260 return v.bsClient.CreateBlockBlobFromReader(v.containerName, loc, uint64(len(block)), bytes.NewReader(block))
263 // Touch updates the last-modified property of a block blob.
264 func (v *AzureBlobVolume) Touch(loc string) error {
266 return MethodDisabledError
268 return v.bsClient.SetBlobMetadata(v.containerName, loc, map[string]string{
269 "touch": fmt.Sprintf("%d", time.Now()),
273 // Mtime returns the last-modified property of a block blob.
274 func (v *AzureBlobVolume) Mtime(loc string) (time.Time, error) {
275 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
277 return time.Time{}, err
279 return time.Parse(time.RFC1123, props.LastModified)
282 // IndexTo writes a list of Keep blocks that are stored in the
284 func (v *AzureBlobVolume) IndexTo(prefix string, writer io.Writer) error {
285 params := storage.ListBlobsParameters{
289 resp, err := v.bsClient.ListBlobs(v.containerName, params)
293 for _, b := range resp.Blobs {
294 t, err := time.Parse(time.RFC1123, b.Properties.LastModified)
298 if !v.isKeepBlock(b.Name) {
301 if b.Properties.ContentLength == 0 && t.Add(azureWriteRaceInterval).After(time.Now()) {
302 // A new zero-length blob is probably
303 // just a new non-empty blob that
304 // hasn't committed its data yet (see
305 // Get()), and in any case has no
309 fmt.Fprintf(writer, "%s+%d %d\n", b.Name, b.Properties.ContentLength, t.Unix())
311 if resp.NextMarker == "" {
314 params.Marker = resp.NextMarker
318 // Trash a Keep block.
319 func (v *AzureBlobVolume) Trash(loc string) error {
321 return MethodDisabledError
324 if trashLifetime != 0 {
325 return ErrNotImplemented
328 // Ideally we would use If-Unmodified-Since, but that
329 // particular condition seems to be ignored by Azure. Instead,
330 // we get the Etag before checking Mtime, and use If-Match to
331 // ensure we don't delete data if Put() or Touch() happens
332 // between our calls to Mtime() and DeleteBlob().
333 props, err := v.bsClient.GetBlobProperties(v.containerName, loc)
337 if t, err := v.Mtime(loc); err != nil {
339 } else if time.Since(t) < blobSignatureTTL {
342 return v.bsClient.DeleteBlob(v.containerName, loc, map[string]string{
343 "If-Match": props.Etag,
347 // Untrash a Keep block.
349 func (v *AzureBlobVolume) Untrash(loc string) error {
350 return ErrNotImplemented
353 // Status returns a VolumeStatus struct with placeholder data.
354 func (v *AzureBlobVolume) Status() *VolumeStatus {
355 return &VolumeStatus{
357 BytesFree: BlockSize * 1000,
362 // String returns a volume label, including the container name.
363 func (v *AzureBlobVolume) String() string {
364 return fmt.Sprintf("azure-storage-container:%+q", v.containerName)
367 // Writable returns true, unless the -readonly flag was on when the
369 func (v *AzureBlobVolume) Writable() bool {
373 // Replication returns the replication level of the container, as
374 // specified by the -azure-storage-replication argument.
375 func (v *AzureBlobVolume) Replication() int {
379 // If possible, translate an Azure SDK error to a recognizable error
380 // like os.ErrNotExist.
381 func (v *AzureBlobVolume) translateError(err error) error {
385 case strings.Contains(err.Error(), "404 Not Found"):
386 // "storage: service returned without a response body (404 Not Found)"
387 return os.ErrNotExist
393 var keepBlockRegexp = regexp.MustCompile(`^[0-9a-f]{32}$`)
395 func (v *AzureBlobVolume) isKeepBlock(s string) bool {
396 return keepBlockRegexp.MatchString(s)