forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
137 lines (122 loc) · 3.86 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
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
// 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
}
func NewGoogleClient(ctx context.Context, bucket string) (Client, error) {
return newGoogleClient(ctx, bucket)
}
func NewAmazonClient(bucket string, id string, secret string, token string,
region string) (Client, error) {
return newAmazonClient(bucket, id, secret, token, region)
}
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
}
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 b.client.IsRetryable(err) {
return err
}
return nil
}, b.backoffConfig, func(err error, d time.Duration) {
protolion.Infof("Error reading (retrying): %#v", RetryError{
err: err.Error(),
timeTillNextRetry: d.String(),
bytesProcessed: bytesRead,
})
})
return bytesRead, err
}
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 b.client.IsRetryable(err) {
return err
}
return nil
}, b.backoffConfig, func(err error, d time.Duration) {
protolion.Infof("Error writing (retrying): %#v", RetryError{
err: err.Error(),
timeTillNextRetry: d.String(),
bytesProcessed: bytesWritten,
})
})
return bytesWritten, err
}
func (b *BackoffWriteCloser) Close() error {
return b.writer.Close()
}