forked from xitongsys/parquet-go-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
356 lines (304 loc) · 7.73 KB
/
s3.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package s3
//go:generate mockgen -destination=../mocks/mock_s3.go -package=mocks github.com/aws/aws-sdk-go/service/s3/s3iface S3API
import (
"context"
"errors"
"fmt"
"io"
"sync"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/xitongsys/parquet-go/source"
)
// S3File is ParquetFile for AWS S3
type S3File struct {
ctx context.Context
client s3iface.S3API
offset int64
whence int
// write-related fields
writeOpened bool
writeDone chan error
pipeReader *io.PipeReader
pipeWriter *io.PipeWriter
uploader *s3manager.Uploader
// read-related fields
readOpened bool
fileSize int64
downloader *s3manager.Downloader
lock sync.RWMutex
err error
BucketName string
Key string
}
const (
rangeHeader = "bytes=%d-%d"
rangeHeaderSuffix = "bytes=%d"
)
var (
errWhence = errors.New("Seek: invalid whence")
errInvalidOffset = errors.New("Seek: invalid offset")
errFailedUpload = errors.New("Write: failed upload")
activeS3Session *session.Session
sessLock sync.Mutex
)
// NewS3FileWriter creates an S3 FileWriter, to be used with NewParquetWriter
func NewS3FileWriter(ctx context.Context, bucket string, key string, cfgs ...*aws.Config) (source.ParquetFile, error) {
if activeS3Session == nil {
sessLock.Lock()
if activeS3Session == nil {
activeS3Session = session.Must(session.NewSession())
}
sessLock.Unlock()
}
file := &S3File{
ctx: ctx,
client: s3.New(activeS3Session, cfgs...),
writeDone: make(chan error),
BucketName: bucket,
Key: key,
}
return file.Create(key)
}
// NewS3FileReader creates an S3 FileReader, to be used with NewParquetReader
func NewS3FileReader(ctx context.Context, bucket string, key string, cfgs ...*aws.Config) (source.ParquetFile, error) {
if activeS3Session == nil {
sessLock.Lock()
if activeS3Session == nil {
activeS3Session = session.Must(session.NewSession())
}
sessLock.Unlock()
}
s3Client := s3.New(activeS3Session, cfgs...)
s3Downloader := s3manager.NewDownloaderWithClient(s3Client)
file := &S3File{
ctx: ctx,
client: s3Client,
downloader: s3Downloader,
BucketName: bucket,
Key: key,
}
return file.Open(key)
}
// Seek tracks the offset for the next Read. Has no effect on Write.
func (s *S3File) Seek(offset int64, whence int) (int64, error) {
if whence < io.SeekStart || whence > io.SeekEnd {
return 0, errWhence
}
if s.fileSize > 0 {
switch whence {
case io.SeekStart:
if offset < 0 || offset > s.fileSize {
return 0, errInvalidOffset
}
case io.SeekCurrent:
offset += s.offset
if offset < 0 || offset > s.fileSize {
return 0, errInvalidOffset
}
case io.SeekEnd:
if offset > -1 || -offset > s.fileSize {
return 0, errInvalidOffset
}
}
}
s.offset = offset
s.whence = whence
return s.offset, nil
}
// Read up to len(p) bytes into p and return the number of bytes read
func (s *S3File) Read(p []byte) (n int, err error) {
if s.fileSize > 0 && s.offset >= s.fileSize {
return 0, io.EOF
}
numBytes := len(p)
getObjRange := s.getBytesRange(numBytes)
getObj := &s3.GetObjectInput{
Bucket: aws.String(s.BucketName),
Key: aws.String(s.Key),
}
if len(getObjRange) > 0 {
getObj.Range = aws.String(getObjRange)
}
wab := aws.NewWriteAtBuffer(p)
bytesDownloaded, err := s.downloader.Download(wab, getObj)
if err != nil {
return 0, err
}
s.offset += bytesDownloaded
if buf := wab.Bytes(); len(buf) > numBytes {
// backing buffer reassigned, copy over some of the data
copy(p, buf)
bytesDownloaded = int64(len(p))
}
return int(bytesDownloaded), err
}
// Write len(p) bytes from p to the S3 data stream
func (s *S3File) Write(p []byte) (n int, err error) {
s.lock.RLock()
writeOpened := s.writeOpened
s.lock.RUnlock()
if !writeOpened {
s.openWrite()
}
s.lock.RLock()
writeError := s.err
s.lock.RUnlock()
if writeError != nil {
return 0, writeError
}
// prevent further writes upon error
bytesWritten, writeError := s.pipeWriter.Write(p)
if writeError != nil {
s.lock.Lock()
s.err = writeError
s.lock.Unlock()
s.pipeWriter.CloseWithError(err)
return 0, writeError
}
return bytesWritten, nil
}
// Close signals write completion and cleans up any
// open streams. Will block until pending uploads are complete.
func (s *S3File) Close() error {
var err error
if s.pipeWriter != nil {
if err = s.pipeWriter.Close(); err != nil {
return err
}
}
// wait for pending uploads
if s.writeDone != nil {
err = <-s.writeDone
}
return err
}
// Open creates a new S3 File instance to perform concurrent reads
func (s *S3File) Open(name string) (source.ParquetFile, error) {
s.lock.RLock()
readOpened := s.readOpened
s.lock.RUnlock()
if !readOpened {
if err := s.openRead(); err != nil {
return nil, err
}
}
// ColumBuffer passes in an empty string for name
if len(name) == 0 {
name = s.Key
}
// create a new instance
pf := &S3File{
ctx: s.ctx,
client: s.client,
downloader: s.downloader,
BucketName: s.BucketName,
Key: name,
readOpened: s.readOpened,
fileSize: s.fileSize,
offset: 0,
}
return pf, nil
}
// Create creates a new S3 File instance to perform writes
func (s *S3File) Create(key string) (source.ParquetFile, error) {
pf := &S3File{
ctx: s.ctx,
client: s.client,
BucketName: s.BucketName,
Key: key,
writeDone: make(chan error),
}
pf.openWrite()
return pf, nil
}
// openWrite creates an S3 uploader that consumes the Reader end of an io.Pipe.
// Calling Close signals write completion.
func (s *S3File) openWrite() {
pr, pw := io.Pipe()
uploader := s3manager.NewUploaderWithClient(s.client)
s.lock.Lock()
s.pipeReader = pr
s.pipeWriter = pw
s.writeOpened = true
s.uploader = uploader
s.lock.Unlock()
uploadParams := &s3manager.UploadInput{
Bucket: aws.String(s.BucketName),
Key: aws.String(s.Key),
Body: s.pipeReader,
}
go func(uploader *s3manager.Uploader, params *s3manager.UploadInput, done chan error) {
defer close(done)
// upload data and signal done when complete
_, err := uploader.Upload(params)
if err != nil {
s.lock.Lock()
s.err = err
s.lock.Unlock()
if s.writeOpened {
s.pipeWriter.CloseWithError(err)
}
}
done <- err
}(s.uploader, uploadParams, s.writeDone)
}
// openRead verifies the requested file is accessible and
// tracks the file size
func (s *S3File) openRead() error {
hoi := &s3.HeadObjectInput{
Bucket: aws.String(s.BucketName),
Key: aws.String(s.Key),
}
hoo, err := s.client.HeadObject(hoi)
if err != nil {
return err
}
s.lock.Lock()
s.readOpened = true
if hoo.ContentLength != nil {
s.fileSize = *hoo.ContentLength
}
s.lock.Unlock()
return nil
}
// getBytesRange returns the range request header string
func (s *S3File) getBytesRange(numBytes int) string {
var (
byteRange string
begin int64
end int64
)
// Processing for unknown file size relies on the requestor to
// know which ranges are valid. May occur if caller is missing HEAD permissions.
if s.fileSize < 1 {
switch s.whence {
case io.SeekStart, io.SeekCurrent:
byteRange = fmt.Sprintf(rangeHeader, s.offset, s.offset+int64(numBytes)-1)
case io.SeekEnd:
byteRange = fmt.Sprintf(rangeHeaderSuffix, s.offset)
}
return byteRange
}
switch s.whence {
case io.SeekStart, io.SeekCurrent:
begin = s.offset
case io.SeekEnd:
begin = s.fileSize + s.offset
default:
return byteRange
}
endIndex := s.fileSize - 1
if begin < 0 {
begin = 0
}
end = begin + int64(numBytes) - 1
if end > endIndex {
end = endIndex
}
byteRange = fmt.Sprintf(rangeHeader, begin, end)
return byteRange
}