forked from segmentio/analytics-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3client.go
492 lines (397 loc) · 10.2 KB
/
s3client.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package analytics
import (
"bufio"
"bytes"
"compress/gzip"
"encoding/json"
"io"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
type uploader interface {
Upload(input *s3manager.UploadInput, options ...func(*s3manager.Uploader)) (*s3manager.UploadOutput, error)
}
type s3Client struct {
*client
config S3ClientConfig
apiContext *apiContext
uploader uploader
//s3client works only with one type of msg
tagsOnlyMsg tagsOnlyMsg
}
// S3 is a configuration for s3Client.
type S3 struct {
Bucket string
Stage string
// Stream is an arbitrary name of the stream where messages will be delivered.
// Examples: tuna, salmon, haring, etc. Each system receives its own stream.
Stream string
// MaxBatchBytes size repsresents the size of buffer or file and when events are flushed
MaxBatchBytes int
// BufferFilePath if specified the temp file will be used to store the data
BufferFilePath string
// UnwrappedMessage if specified wraps message with context, but it useless for most of s3 messages
UnwrappedMessage bool
KeyConstructor func(now func() Time, uid func() string) string
UploaderOptions []func(*s3manager.Uploader)
Session *session.Session
}
// S3ClientConfig provides configuration for S3 Client.
type S3ClientConfig struct {
Config
S3 S3
}
// NewS3ClientWithConfig creates S3 client from provided configuration.
// Pass empty S3ClientConfig{} to use default config.
func NewS3ClientWithConfig(config S3ClientConfig) (Client, error) {
cfg, err := makeS3ClientConfig(config)
if err != nil {
return nil, err
}
client, err := newWithConfig("", cfg.Config)
if err != nil {
return nil, err
}
client.msgs = make(chan Message, 1024) // overrite the buffer
uploader := s3manager.NewUploader(cfg.S3.Session, cfg.S3.UploaderOptions...)
c := &s3Client{
client: client,
config: cfg,
apiContext: &apiContext{
APIID: uid(),
Stage: cfg.S3.Stage,
},
uploader: uploader,
}
go c.loop() // custom implementation
go c.loopMetrics() // reuse client's implementation
return c, nil
}
// a copy of client.loop() function.
func (c *s3Client) loop() {
defer close(c.shutdown)
wg := &sync.WaitGroup{}
defer wg.Wait()
tick := time.NewTicker(c.Interval)
defer tick.Stop()
ex := newExecutor(c.maxConcurrentRequests)
defer ex.close()
bw := bufferedEncoder{
maxBatchSize: c.BatchSize,
maxBatchBytes: int64(c.config.S3.MaxBatchBytes),
newBufFunc: func() encodedBuffer {
return c.newBuffer(c.config.S3.BufferFilePath, c.config.S3.MaxBatchBytes)
},
}
bw.init()
for {
select {
case msg := <-c.msgs:
c.push(&bw, msg, wg, ex)
case <-tick.C:
c.flush(&bw, wg, ex)
case <-c.quit:
c.debugf("exit requested – draining messages")
// Drain the msg channel, we have to close it first so no more
// messages can be pushed and otherwise the loop would never end.
close(c.msgs)
for msg := range c.msgs {
c.push(&bw, msg, wg, ex)
}
c.flush(&bw, wg, ex)
bw.buf.Close()
c.debugf("exit")
return
}
}
}
func (c *s3Client) newBuffer(path string, size int) encodedBuffer {
if path == "" {
return newMemBuffer(size)
}
buf, err := newFileBuffer(path)
if err != nil {
c.errorf("invalid file name", err)
// fallback to a small membuffer
return newMemBuffer(1024)
}
return buf
}
type apiContext struct {
APIID string `json:"apiId,omitempty"`
RequestID string `json:"requestId,omitempty"`
ResourcePath string `json:"resourcePath,omitempty"`
Stage string `json:"stage,omitempty"`
}
// targetMessage is a single non-batched message delivered to s3 in one row of json.
type targetMessage struct {
APIContext *apiContext `json:"context,omitempty"`
Event Message `json:"event"`
SentAt Time `json:"sentAt"`
ReceivedAt Time `json:"receivedAt"`
}
func (m *targetMessage) MarshalJSON() ([]byte, error) {
return json.Marshal(m)
}
func (m *targetMessage) Msg() Message {
return m.Event
}
func (m *targetMessage) size() int {
return -1
}
// dummy message to store flags
type tagsOnlyMsg struct {
t []string
}
func (m *tagsOnlyMsg) tags() []string {
return m.t
}
func (m *tagsOnlyMsg) validate() error {
return nil
}
func (c *s3Client) push(encoder *bufferedEncoder, m Message, wg *sync.WaitGroup, ex *executor) {
c.setTagsIfExsist(m)
ready, err := c.encodeMessage(encoder, m)
if err != nil {
c.errorf("cant encode message: ", err)
c.notifyFailureMsg(m, err, 1)
}
c.debugf("buffer (%d/%d) %v", encoder.messages, c.BatchSize, m)
if ready {
c.debugf("exceeded messages batch limit with batch of %d messages – flushing", encoder.messages)
c.sendAsync(encoder, wg, ex)
}
}
// we need this functio to send metrics
func (c *s3Client) setTagsIfExsist(m Message) {
if len(c.tagsOnlyMsg.t) == 0 {
c.tagsOnlyMsg.t = m.tags()
}
}
func (c *s3Client) encodeMessage(bw *bufferedEncoder, m Message) (ready bool, err error) {
if c.config.S3.UnwrappedMessage {
type unwrappedMessage struct {
Event Message `json:"event"`
}
return bw.Push(unwrappedMessage{Event: m})
}
ts := c.now()
msg := targetMessage{
APIContext: c.apiContext,
Event: m,
SentAt: ts,
ReceivedAt: ts,
}
type alias targetMessage // we won't use json.Marshaller implementation
return bw.Push(alias(msg))
}
// Asynchronously send a batched requests.
func (c *s3Client) sendAsync(bw *bufferedEncoder, wg *sync.WaitGroup, ex *executor) {
if bw.BytesLen() == 0 {
c.errorf("empty buffer, send is not possible")
return
}
msgs := bw.TotalMsgs()
buf, err := bw.CommitBuffer()
if err != nil {
c.errorf("can't flush gzip, send is not possible")
return
}
wg.Add(1)
if !ex.do(func() {
defer wg.Done()
defer func() {
// In case a bug is introduced in the send function that triggers
// a panic, we don't want this to ever crash the application so we
// catch it here and log it instead.
if err := recover(); err != nil {
c.errorf("panic - %s", err)
}
}()
c.send(buf, msgs)
}) {
wg.Done()
c.errorf("sending messages failed - %s", ErrTooManyRequests)
c.notifyFailureMsg(&c.tagsOnlyMsg, ErrTooManyRequests, int64(bw.TotalMsgs()))
}
}
func (c *s3Client) flush(bw *bufferedEncoder, wg *sync.WaitGroup, ex *executor) {
msgs := bw.TotalMsgs()
if msgs > 0 {
c.debugf("flushing %d messages", msgs)
c.sendAsync(bw, wg, ex)
}
}
// Send batch request.
func (c *s3Client) send(buf encodedBuffer, msgs int) {
const attempts = 10
defer buf.Close()
for i := 0; i != attempts; i++ {
reader, err := buf.Reader()
if err != nil {
c.errorf("can't get reader", err)
}
if err = c.upload(reader); err == nil {
c.notifySuccessMsg(&c.tagsOnlyMsg, int64(msgs))
return
}
// Wait for either a retry timeout or the client to be closed.
select {
case <-time.After(c.RetryAfter(i)):
c.errorf("%d messages dropped because of error: %s", msgs, err)
return
case <-c.quit:
c.errorf("%d messages dropped because they failed to be sent and the client was closed, upload error: %s", msgs, err)
return
}
}
c.errorf("%d messages dropped because they failed to be sent after %d attempts", msgs, attempts)
}
// Upload batch to S3.
func (c *s3Client) upload(r io.Reader) error {
key := c.config.S3.KeyConstructor(c.now, uid)
c.debugf("uploading to s3://%s/%s", c.config.S3.Bucket, key)
input := &s3manager.UploadInput{
Body: r,
Bucket: aws.String(c.config.S3.Bucket),
ACL: aws.String("public-read"),
Key: aws.String(key),
}
_, err := c.uploader.Upload(input)
return err
}
type bufferedEncoder struct {
maxBatchSize int
maxBatchBytes int64
newBufFunc func() encodedBuffer
buf encodedBuffer
encoder *json.Encoder
gziper *gzip.Writer
messages int
}
func (q *bufferedEncoder) BytesLen() int64 {
return q.buf.Size()
}
func (q *bufferedEncoder) TotalMsgs() int {
return q.messages
}
func (q *bufferedEncoder) CommitBuffer() (encodedBuffer, error) {
err := q.gziper.Close()
if err != nil {
return nil, err
}
oldbuff := q.buf
q.init()
return oldbuff, nil
}
func (q *bufferedEncoder) Push(v interface{}) (bool, error) {
if err := q.encoder.Encode(v); err != nil {
return false, err
}
q.messages++
if q.buf.Size() >= q.maxBatchBytes {
return true, nil
}
if q.messages >= q.maxBatchSize {
return true, nil
}
return false, nil
}
func (q *bufferedEncoder) init() {
q.buf = q.newBufFunc()
q.gziper = gzip.NewWriter(q.buf)
q.encoder = json.NewEncoder(q.gziper)
q.messages = 0
}
type encodedBuffer interface {
io.WriteCloser
Size() int64
Reader() (io.Reader, error)
Reset() error
}
type memBuffer struct {
buf *bytes.Buffer
}
func newMemBuffer(size int) *memBuffer {
var buf bytes.Buffer
if size > 0 {
buf.Grow(size)
}
return &memBuffer{
buf: &buf,
}
}
func (m *memBuffer) Write(p []byte) (n int, err error) {
return m.buf.Write(p)
}
func (m *memBuffer) Reader() (io.Reader, error) {
return bytes.NewReader(m.buf.Bytes()), nil
}
func (m *memBuffer) Reset() error {
m.buf.Reset()
return nil
}
func (m *memBuffer) Size() int64 {
return int64(m.buf.Len())
}
func (m *memBuffer) Close() error {
return nil
}
type fileBuffer struct {
fd *os.File
writer *bufio.Writer
reader *bufio.Reader
size int64
}
func newFileBuffer(path string) (*fileBuffer, error) {
dir, file := filepath.Split(path)
fd, err := ioutil.TempFile(dir, file)
if err != nil {
return nil, err
}
return &fileBuffer{
fd: fd,
writer: bufio.NewWriter(fd),
reader: bufio.NewReader(fd),
}, nil
}
func (m *fileBuffer) Write(p []byte) (n int, err error) {
n, err = m.writer.Write(p)
if err != nil {
return n, err
}
m.size += int64(n)
return n, nil
}
func (m *fileBuffer) Reader() (io.Reader, error) {
if err := m.writer.Flush(); err != nil {
return nil, err
}
if _, err := m.fd.Seek(0, io.SeekStart); err != nil {
return nil, err
}
return io.LimitReader(m.reader, m.size), nil
}
func (m *fileBuffer) Reset() error {
m.size = 0
if _, err := m.fd.Seek(0, io.SeekStart); err != nil {
return err
}
m.writer.Reset(m.fd)
m.reader.Reset(m.fd)
return nil
}
func (m *fileBuffer) Size() int64 {
return m.size
}
func (m *fileBuffer) Close() error {
fileName := m.fd.Name()
m.fd.Close()
return os.Remove(fileName)
}