-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathazure.go
More file actions
209 lines (188 loc) · 6.2 KB
/
azure.go
File metadata and controls
209 lines (188 loc) · 6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package azure
import (
"context"
"fmt"
"io"
"net/url"
"time"
"github.com/Azure/azure-storage-blob-go/azblob"
"github.com/buildbuddy-io/buildbuddy/server/backends/blobstore/util"
"github.com/buildbuddy-io/buildbuddy/server/interfaces"
"github.com/buildbuddy-io/buildbuddy/server/util/flag"
"github.com/buildbuddy-io/buildbuddy/server/util/ioutil"
"github.com/buildbuddy-io/buildbuddy/server/util/log"
"github.com/buildbuddy-io/buildbuddy/server/util/status"
"github.com/buildbuddy-io/buildbuddy/server/util/tracing"
)
var (
// Azure flags
azureAccountName = flag.String("storage.azure.account_name", "", "The name of the Azure storage account")
azureAccountKey = flag.String("storage.azure.account_key", "", "The key for the Azure storage account", flag.Secret)
azureContainerName = flag.String("storage.azure.container_name", "", "The name of the Azure storage container")
)
const (
// Prometheus BlobstoreTypeLabel values
azureLabel = "azure"
)
const azureURLTemplate = "https://%s.blob.core.windows.net/%s"
// GCSBlobStore implements the blobstore API on top of the google cloud storage API.
type AzureBlobStore struct {
credential *azblob.SharedKeyCredential
containerName string
containerURL *azblob.ContainerURL
}
func UseAzureBlobStore() bool {
return *azureAccountName != ""
}
func NewAzureBlobStore(ctx context.Context) (*AzureBlobStore, error) {
credential, err := azblob.NewSharedKeyCredential(*azureAccountName, *azureAccountKey)
if err != nil {
return nil, err
}
pipeline := azblob.NewPipeline(credential, azblob.PipelineOptions{})
portalURL, err := url.Parse(fmt.Sprintf(azureURLTemplate, *azureAccountName, *azureContainerName))
if err != nil {
return nil, err
}
containerURL := azblob.NewContainerURL(*portalURL, pipeline)
z := &AzureBlobStore{
credential: credential,
containerName: *azureContainerName,
containerURL: &containerURL,
}
if err := z.createContainerIfNotExists(ctx); err != nil {
return nil, err
}
log.Debugf("Azure blobstore configured (container: %q)", *azureContainerName)
return z, nil
}
func (z *AzureBlobStore) isAzureError(err error, code azblob.ServiceCodeType) bool {
if serr, ok := err.(azblob.StorageError); ok {
if serr.ServiceCode() == code {
return true
}
}
return false
}
func (z *AzureBlobStore) containerExists(ctx context.Context) (bool, error) {
ctx, spn := tracing.StartSpan(ctx)
_, err := z.containerURL.GetProperties(ctx, azblob.LeaseAccessConditions{})
spn.End()
if err == nil {
return true, nil
}
if !z.isAzureError(err, azblob.ServiceCodeContainerNotFound) {
return false, err
}
return false, nil
}
func (z *AzureBlobStore) createContainerIfNotExists(ctx context.Context) error {
if exists, err := z.containerExists(ctx); err != nil {
return err
} else if !exists {
ctx, spn := tracing.StartSpan(ctx)
defer spn.End()
_, err = z.containerURL.Create(ctx, azblob.Metadata{}, azblob.PublicAccessNone)
return err
}
return nil
}
func (z *AzureBlobStore) ReadBlob(ctx context.Context, blobName string) ([]byte, error) {
start := time.Now()
_, spn := tracing.StartSpan(ctx)
blobURL := z.containerURL.NewBlockBlobURL(blobName)
response, err := blobURL.Download(ctx, 0 /*=offset*/, azblob.CountToEnd, azblob.BlobAccessConditions{}, false /*=rangeGetContentMD5*/, azblob.ClientProvidedKeyOptions{})
if err != nil {
if z.isAzureError(err, azblob.ServiceCodeBlobNotFound) {
return nil, status.NotFoundError(err.Error())
}
return nil, err
}
readCloser := response.Body(azblob.RetryReaderOptions{})
defer readCloser.Close()
b, err := io.ReadAll(readCloser)
spn.End()
if err != nil {
return nil, err
}
util.RecordReadMetrics(azureLabel, start, len(b), err)
return util.Decompress(b, err)
}
func (z *AzureBlobStore) WriteBlob(ctx context.Context, blobName string, data []byte) (int, error) {
compressedData, err := util.Compress(data)
if err != nil {
return 0, err
}
n := len(compressedData)
start := time.Now()
blobURL := z.containerURL.NewBlockBlobURL(blobName)
ctx, spn := tracing.StartSpan(ctx)
_, err = azblob.UploadBufferToBlockBlob(ctx, compressedData, blobURL, azblob.UploadToBlockBlobOptions{})
spn.End()
util.RecordWriteMetrics(azureLabel, start, n, err)
return n, err
}
func (z *AzureBlobStore) DeleteBlob(ctx context.Context, blobName string) error {
start := time.Now()
blobURL := z.containerURL.NewBlockBlobURL(blobName)
ctx, spn := tracing.StartSpan(ctx)
_, err := blobURL.Delete(ctx, azblob.DeleteSnapshotsOptionNone, azblob.BlobAccessConditions{})
spn.End()
util.RecordDeleteMetrics(azureLabel, start, err)
return err
}
func (z *AzureBlobStore) BlobExists(ctx context.Context, blobName string) (bool, error) {
blobURL := z.containerURL.NewBlockBlobURL(blobName)
start := time.Now()
ctx, spn := tracing.StartSpan(ctx)
defer spn.End()
_, err := blobURL.GetProperties(ctx, azblob.BlobAccessConditions{}, azblob.ClientProvidedKeyOptions{})
util.RecordExistsMetrics(azureLabel, start, err)
if err != nil {
if z.isAzureError(err, azblob.ServiceCodeBlobNotFound) {
return false, nil
}
return false, err
}
return true, nil
}
func (z *AzureBlobStore) Writer(ctx context.Context, blobName string) (interfaces.CommittedWriteCloser, error) {
// Open a pipe.
pr, pw := io.Pipe()
errch := make(chan error, 1)
ctx, cancel := context.WithCancel(ctx)
// Upload from pr in a separate Go routine.
go func() {
defer pr.Close()
_, err := azblob.UploadStreamToBlockBlob(
ctx,
pr,
z.containerURL.NewBlockBlobURL(blobName),
azblob.UploadStreamToBlockBlobOptions{},
)
errch <- err
close(errch)
}()
zw := util.NewCompressWriter(pw)
cwc := ioutil.NewCustomCommitWriteCloser(zw)
cwc.SetCommitFn(func(int64) error {
if compresserCloseErr := zw.Close(); compresserCloseErr != nil {
cancel() // Don't try to finish the commit op if Close() failed.
if pipeCloseErr := pw.Close(); pipeCloseErr != nil {
log.Errorf("Error closing the pipe for %s: %s", blobName, pipeCloseErr)
}
// Canceling the context makes any error in errch meaningless; don't
// bother to read it.
return compresserCloseErr
}
if writerCloseErr := pw.Close(); writerCloseErr != nil {
return writerCloseErr
}
return <-errch
})
cwc.SetCloseFn(func() error {
cancel()
return nil
})
return cwc, nil
}