/
swift.go
285 lines (244 loc) · 6.62 KB
/
swift.go
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package storage
import (
"context"
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/alecthomas/units"
"github.com/ncw/swift"
"github.com/ohsu-comp-bio/funnel/config"
"github.com/ohsu-comp-bio/funnel/util"
"github.com/ohsu-comp-bio/funnel/util/fsutil"
)
const swiftProtocol = "swift://"
// Swift provides access to an sw object store.
type Swift struct {
conn *swift.Connection
chunkSize int64
}
// NewSwift creates an Swift client instance, give an endpoint URL
// and a set of authentication credentials.
func NewSwift(conf config.SwiftStorage) (*Swift, error) {
// Create a connection
conn := &swift.Connection{
UserName: conf.UserName,
ApiKey: conf.Password,
AuthUrl: conf.AuthURL,
Tenant: conf.TenantName,
TenantId: conf.TenantID,
Region: conf.RegionName,
}
// Read environment variables and apply them to the Connection structure.
// Won't overwrite any parameters which are already set in the Connection struct.
err := conn.ApplyEnvironment()
if err != nil {
return nil, err
}
var chunkSize int64
if conf.ChunkSizeBytes < int64(100*units.MB) {
chunkSize = int64(500 * units.MB)
} else if conf.ChunkSizeBytes > int64(5*units.GB) {
chunkSize = int64(5 * units.GB)
} else {
chunkSize = conf.ChunkSizeBytes
}
return &Swift{conn, chunkSize}, nil
}
// NewSwiftRetrier returns a Swift storage client that retries operations on error.
func NewSwiftRetrier(conf config.SwiftStorage) (*Retrier, error) {
b, err := NewSwift(conf)
if err != nil {
return nil, err
}
return &Retrier{
Backend: b,
Retrier: &util.Retrier{
MaxTries: conf.MaxRetries,
InitialInterval: 500 * time.Millisecond,
MaxInterval: 5 * time.Minute,
Multiplier: 2.0,
RandomizationFactor: 0.5,
MaxElapsedTime: 0,
ShouldRetry: shouldRetry,
},
}, nil
}
func shouldRetry(err error) bool {
serr, ok := err.(*swiftError)
if !ok {
return false
}
err = serr.err
// Retry on errors that swift names specifically.
if err == swift.ObjectCorrupted || err == swift.TimeoutError {
return true
}
// Retry on service unavailable.
if se, ok := err.(*swift.Error); ok {
return se.StatusCode == http.StatusServiceUnavailable
}
return false
}
// Stat returns metadata about the given url, such as checksum.
func (sw *Swift) Stat(ctx context.Context, url string) (*Object, error) {
u, err := sw.parse(url)
if err != nil {
return nil, err
}
info, _, err := sw.conn.Object(u.bucket, u.path)
if err != nil {
return nil, &swiftError{"getting object info", url, err}
}
return &Object{
URL: url,
Name: info.Name,
Size: info.Bytes,
LastModified: info.LastModified,
ETag: info.Hash,
}, nil
}
// List lists the objects at the given url.
func (sw *Swift) List(ctx context.Context, url string) ([]*Object, error) {
u, err := sw.parse(url)
if err != nil {
return nil, err
}
objs, err := sw.conn.ObjectsAll(u.bucket, &swift.ObjectsOpts{
Prefix: u.path,
})
if err != nil {
return nil, &swiftError{"listing objects by prefix", url, err}
}
var objects []*Object
for _, obj := range objs {
objects = append(objects, &Object{
URL: swiftProtocol + u.bucket + "/" + obj.Name,
Name: obj.Name,
Size: obj.Bytes,
LastModified: obj.LastModified,
ETag: obj.Hash,
})
}
return objects, nil
}
// Get copies an object from storage to the host path.
func (sw *Swift) Get(ctx context.Context, url, path string) (*Object, error) {
u, err := sw.parse(url)
if err != nil {
return nil, err
}
var checkHash = true
var headers swift.Headers
obj, err := sw.Stat(ctx, url)
if err != nil {
return nil, err
}
f, _, err := sw.conn.ObjectOpen(u.bucket, u.path, checkHash, headers)
if err != nil {
return nil, &swiftError{"initiating download", url, err}
}
defer f.Close()
dest, err := os.Create(path)
if err != nil {
return nil, &swiftError{"creating file", url, err}
}
_, copyErr := io.Copy(dest, fsutil.Reader(ctx, f))
closeErr := dest.Close()
if copyErr != nil {
return nil, &swiftError{"copying file", url, copyErr}
}
if closeErr != nil {
return nil, &swiftError{"closing file", url, closeErr}
}
return obj, nil
}
// Put copies an object (file) from the host path to storage.
func (sw *Swift) Put(ctx context.Context, url, path string) (*Object, error) {
u, err := sw.parse(url)
if err != nil {
return nil, err
}
reader, err := os.Open(path)
if err != nil {
return nil, &swiftError{"opening host file", url, err}
}
defer reader.Close()
var writer io.WriteCloser
var checkHash = true
var hash string
var contentType string
var headers swift.Headers
fSize := fsutil.FileSize(path)
if fSize < int64(5*units.GB) {
writer, err = sw.conn.ObjectCreate(u.bucket, u.path, checkHash, hash, contentType, headers)
} else {
writer, err = sw.conn.StaticLargeObjectCreateFile(&swift.LargeObjectOpts{
Container: u.bucket,
ObjectName: u.path,
CheckHash: checkHash,
Hash: hash,
Headers: headers,
ChunkSize: sw.chunkSize,
})
}
if err != nil {
return nil, &swiftError{"creating object", url, err}
}
_, copyErr := io.Copy(writer, fsutil.Reader(ctx, reader))
// In order to do the Stat call below, the writer needs to be closed
// so that the object is created.
closeErr := writer.Close()
if copyErr != nil {
return nil, &swiftError{"copying file", url, copyErr}
}
if closeErr != nil {
return nil, &swiftError{"closing file", url, closeErr}
}
return sw.Stat(ctx, url)
}
// Join joins the given URL with the given subpath.
func (sw *Swift) Join(url, path string) (string, error) {
return strings.TrimSuffix(url, "/") + "/" + path, nil
}
// UnsupportedOperations describes which operations (Get, Put, etc) are not
// supported for the given URL.
func (sw *Swift) UnsupportedOperations(url string) UnsupportedOperations {
u, err := sw.parse(url)
if err != nil {
return AllUnsupported(err)
}
_, _, err = sw.conn.Container(u.bucket)
if err != nil {
return AllUnsupported(&swiftError{"looking for bucket", url, err})
}
return AllSupported()
}
func (sw *Swift) parse(rawurl string) (*urlparts, error) {
ok := strings.HasPrefix(rawurl, swiftProtocol)
if !ok {
return nil, &ErrUnsupportedProtocol{"swift"}
}
path := strings.TrimPrefix(rawurl, swiftProtocol)
if path == "" {
return nil, &ErrInvalidURL{"swift"}
}
split := strings.SplitN(path, "/", 2)
url := &urlparts{}
if len(split) > 0 {
url.bucket = split[0]
}
if len(split) == 2 {
url.path = split[1]
}
return url, nil
}
type swiftError struct {
msg, url string
err error
}
func (s *swiftError) Error() string {
return fmt.Sprintf("swift: %s for URL %q: %v", s.msg, s.url, s.err)
}