forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
157 lines (142 loc) · 4.67 KB
/
obj.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
package obj
import (
"io"
"time"
"github.com/cenkalti/backoff"
"go.pedge.io/lion/proto"
"golang.org/x/net/context"
)
// Client is an interface to object storage.
type Client interface {
// Writer returns a writer which writes to an object.
// It should error if the object already exists or we don't have sufficient
// permissions to write it.
Writer(name string) (io.WriteCloser, error)
// Reader returns a reader which reads from an object.
// If `size == 0`, the reader should read from the offset till the end of the object.
// It should error if the object doesn't exist or we don't have sufficient
// permission to read it.
Reader(name string, offset uint64, size uint64) (io.ReadCloser, error)
// Delete deletes an object.
// It should error if the object doesn't exist or we don't have sufficient
// permission to delete it.
Delete(name string) error
// Walk calls `fn` with the names of objects which can be found under `prefix`.
Walk(prefix string, fn func(name string) error) error
// Exsits checks if a given object already exists
Exists(name string) bool
// IsRetryable determines if an operation should be retried given an error
IsRetryable(err error) bool
// IsNotExist returns true if err is a non existence error
IsNotExist(err error) bool
// IsIgnorable returns true if the error can be ignored
IsIgnorable(err error) bool
}
// NewGoogleClient creates a google client with the given bucket name.
func NewGoogleClient(ctx context.Context, bucket string) (Client, error) {
return newGoogleClient(ctx, bucket)
}
// NewAmazonClient creates an amazon client with the following credentials:
// bucket - S3 bucket name
// id - AWS access key id
// secret - AWS secret access key
// token - AWS access token
// region - AWS region
func NewAmazonClient(bucket string, id string, secret string, token string,
region string) (Client, error) {
return newAmazonClient(bucket, id, secret, token, region)
}
// NewExponentialBackOffConfig creates an exponential back-off config with
// longer wait times than the default.
func NewExponentialBackOffConfig() *backoff.ExponentialBackOff {
config := backoff.NewExponentialBackOff()
// We want to backoff more aggressively (i.e. wait longer) than the default
config.InitialInterval = 1 * time.Second
config.Multiplier = 2
config.MaxElapsedTime = 5 * time.Minute
return config
}
// RetryError is used to log retry attempts.
type RetryError struct {
Err string
TimeTillNextRetry string
BytesProcessed int
}
// BackoffReadCloser retries with exponential backoff in the case of failures
type BackoffReadCloser struct {
client Client
reader io.ReadCloser
backoffConfig *backoff.ExponentialBackOff
}
func newBackoffReadCloser(client Client, reader io.ReadCloser) io.ReadCloser {
return &BackoffReadCloser{
client: client,
reader: reader,
backoffConfig: NewExponentialBackOffConfig(),
}
}
func (b *BackoffReadCloser) Read(data []byte) (int, error) {
bytesRead := 0
var n int
var err error
backoff.RetryNotify(func() error {
n, err = b.reader.Read(data[bytesRead:])
bytesRead += n
if err != nil && b.client.IsRetryable(err) {
return err
}
return nil
}, b.backoffConfig, func(err error, d time.Duration) {
protolion.Infof("Error reading; retrying in %s: %#v", d, RetryError{
Err: err.Error(),
TimeTillNextRetry: d.String(),
BytesProcessed: bytesRead,
})
})
return bytesRead, err
}
// Close closes the ReaderCloser contained in b.
func (b *BackoffReadCloser) Close() error {
return b.reader.Close()
}
// BackoffWriteCloser retries with exponential backoff in the case of failures
type BackoffWriteCloser struct {
client Client
writer io.WriteCloser
backoffConfig *backoff.ExponentialBackOff
}
func newBackoffWriteCloser(client Client, writer io.WriteCloser) io.WriteCloser {
return &BackoffWriteCloser{
client: client,
writer: writer,
backoffConfig: NewExponentialBackOffConfig(),
}
}
func (b *BackoffWriteCloser) Write(data []byte) (int, error) {
bytesWritten := 0
var n int
var err error
backoff.RetryNotify(func() error {
n, err = b.writer.Write(data[bytesWritten:])
bytesWritten += n
if err != nil && b.client.IsRetryable(err) {
return err
}
return nil
}, b.backoffConfig, func(err error, d time.Duration) {
protolion.Infof("Error writing; retrying in %s: %#v", d, RetryError{
Err: err.Error(),
TimeTillNextRetry: d.String(),
BytesProcessed: bytesWritten,
})
})
return bytesWritten, err
}
// Close closes the WriteCloser contained in b.
func (b *BackoffWriteCloser) Close() error {
err := b.writer.Close()
if b.client.IsIgnorable(err) {
return nil
}
return err
}