-
Notifications
You must be signed in to change notification settings - Fork 567
/
uploader.go
209 lines (195 loc) · 6.35 KB
/
uploader.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
package chunk
import (
"context"
"io"
"github.com/gogo/protobuf/proto"
"github.com/pachyderm/pachyderm/v2/src/internal/errors"
"github.com/pachyderm/pachyderm/v2/src/internal/errutil"
"github.com/pachyderm/pachyderm/v2/src/internal/miscutil"
"github.com/pachyderm/pachyderm/v2/src/internal/pctx"
"github.com/pachyderm/pachyderm/v2/src/internal/taskchain"
"golang.org/x/sync/semaphore"
)
const (
taskParallelism = 100
)
// UploadFunc is a function that provides the metadata for a task and the corresponding set of chunk references.
type UploadFunc = func(interface{}, []*DataRef) error
// Uploader uploads chunks.
// Each upload call creates at least one upload task with the provided metadata.
// Upload tasks are performed asynchronously, which is why the interface is callback based.
// Callbacks will be executed with respect to the order the upload tasks are created.
type Uploader struct {
ctx context.Context
storage *Storage
client Client
taskChain *taskchain.TaskChain
noUpload bool
cb UploadFunc
}
func (s *Storage) NewUploader(ctx context.Context, name string, noUpload bool, cb UploadFunc) *Uploader {
client := NewClient(s.store, s.db, s.tracker, NewRenewer(ctx, s.tracker, name, defaultChunkTTL))
return &Uploader{
ctx: ctx,
storage: s,
client: client,
taskChain: taskchain.New(ctx, semaphore.NewWeighted(taskParallelism)),
noUpload: noUpload,
cb: cb,
}
}
func (u *Uploader) Upload(meta interface{}, r io.Reader) error {
var dataRefs []*DataRef
if err := ComputeChunks(r, func(chunkBytes []byte) error {
return u.taskChain.CreateTask(func(ctx context.Context) (func() error, error) {
dataRef, err := upload(ctx, u.client, chunkBytes, nil, u.noUpload)
if err != nil {
return nil, err
}
return func() error {
dataRefs = append(dataRefs, dataRef)
return nil
}, nil
})
}); err != nil {
return err
}
return u.taskChain.CreateTask(func(_ context.Context) (func() error, error) {
return func() error {
if len(dataRefs) > 0 {
dataRefs[0].Ref.Edge = true
dataRefs[len(dataRefs)-1].Ref.Edge = true
}
return u.cb(meta, dataRefs)
}, nil
})
}
// Copy performs an upload using a list of data references as the data source.
// Stable data references will be reused.
// Unstable data references will have their data downloaded and uploaded similar to a normal upload.
func (u *Uploader) Copy(meta interface{}, dataRefs []*DataRef) error {
var stableDataRefs, nextDataRefs []*DataRef
appendDataRefs := func(dataRefs []*DataRef) error {
return u.taskChain.CreateTask(func(_ context.Context) (func() error, error) {
return func() error {
for _, dataRef := range dataRefs {
stableDataRefs = append(stableDataRefs, proto.Clone(dataRef).(*DataRef))
}
return nil
}, nil
})
}
for len(dataRefs) > 0 {
if !isStableDataRef(dataRefs[0]) {
if err := appendDataRefs(nextDataRefs); err != nil {
return err
}
nextDataRefs = nil
var err error
dataRefs, err = u.align(u.ctx, dataRefs, func(chunk []byte) error {
return u.taskChain.CreateTask(func(ctx context.Context) (func() error, error) {
dataRef, err := upload(ctx, u.client, chunk, nil, u.noUpload)
if err != nil {
return nil, err
}
return func() error {
stableDataRefs = append(stableDataRefs, dataRef)
return nil
}, nil
})
})
if err != nil {
return err
}
continue
}
nextDataRefs = append(nextDataRefs, dataRefs[0])
dataRefs = dataRefs[1:]
}
if err := appendDataRefs(nextDataRefs); err != nil {
return err
}
return u.taskChain.CreateTask(func(_ context.Context) (func() error, error) {
return func() error {
if len(stableDataRefs) > 0 {
stableDataRefs[0].Ref.Edge = true
stableDataRefs[len(stableDataRefs)-1].Ref.Edge = true
}
return u.cb(meta, stableDataRefs)
}, nil
})
}
// A data reference is stable if it does not reference an edge chunk and references the full chunk.
func isStableDataRef(dataRef *DataRef) bool {
return !dataRef.Ref.Edge && dataRef.OffsetBytes == 0 && dataRef.SizeBytes == dataRef.Ref.SizeBytes
}
// align performs a chunk boundary alignment on a list of data references.
// Alignment involves iterating through the passed in list of data references
// until a data reference is found that begins at a chunk boundary. Chunks
// encountered up to that point will be returned through the provided callback.
// The returned list of data references is the slice that begins at the first
// data reference found that begins at a chunk boundary.
func (u *Uploader) align(ctx context.Context, dataRefs []*DataRef, cb func([]byte) error) ([]*DataRef, error) {
ctx, cancel := pctx.WithCancel(ctx)
defer cancel()
r := u.storage.NewReader(ctx, dataRefs, WithPrefetchLimit(3))
if err := miscutil.WithPipe(func(w2 io.Writer) error {
return r.Get(w2)
}, func(r io.Reader) error {
splitBytesLeft := dataRefs[0].SizeBytes
return ComputeChunks(r, func(chunk []byte) error {
chunkBytesLeft := int64(len(chunk))
if err := cb(chunk); err != nil {
return err
}
for chunkBytesLeft > splitBytesLeft {
chunkBytesLeft -= splitBytesLeft
dataRefs = dataRefs[1:]
splitBytesLeft = dataRefs[0].SizeBytes
}
if chunkBytesLeft == splitBytesLeft {
return errutil.ErrBreak
}
splitBytesLeft -= chunkBytesLeft
return nil
})
}); err != nil && !errors.Is(err, errutil.ErrBreak) {
return nil, err
}
return dataRefs[1:], nil
}
func (u *Uploader) CopyByReference(meta interface{}, dataRefs []*DataRef) error {
return u.taskChain.CreateTask(func(_ context.Context) (func() error, error) {
return func() error {
return u.cb(meta, dataRefs)
}, nil
})
}
func upload(ctx context.Context, client Client, chunkBytes []byte, pointsTo []ID, noUpload bool) (*DataRef, error) {
md := Metadata{
Size: len(chunkBytes),
PointsTo: pointsTo,
}
createFunc := func(ctx context.Context, data []byte) (ID, error) {
res, err := client.Create(ctx, md, data)
return res, errors.EnsureStack(err)
}
if noUpload {
createFunc = func(ctx context.Context, data []byte) (ID, error) {
return Hash(data), nil
}
}
ref, err := Create(ctx, CreateOptions{}, chunkBytes, createFunc)
if err != nil {
return nil, err
}
contentHash := Hash(chunkBytes)
return &DataRef{
Hash: contentHash,
Ref: ref,
SizeBytes: int64(len(chunkBytes)),
}, nil
}
func (u *Uploader) Close() error {
return u.taskChain.Wait()
}