forked from s3git/s3git-go
/
push.go
348 lines (277 loc) · 7.66 KB
/
push.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
/*
* Copyright 2016 Frank Wessels <fwessels@xs4all.nl>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package s3git
import (
"errors"
"fmt"
"github.com/s3git/s3git-go/internal/backend"
"github.com/s3git/s3git-go/internal/cas"
"github.com/s3git/s3git-go/internal/core"
"github.com/s3git/s3git-go/internal/kv"
"bytes"
"encoding/hex"
"sync"
)
// Perform a push to the back end for the repository
func (repo Repository) Push(hydrated bool, progress func(maxTicks int64)) error {
list, err := kv.ListLevel1Prefixes()
if err != nil {
return err
}
return push(list, hydrated, progress)
}
// Push any new commit objects including all added objects to the back end store
func push(prefixChan <-chan []byte, hydrated bool, progress func(maxTicks int64)) error {
client, err := backend.GetDefaultClient()
if err != nil {
return err
}
// Get map of prefixes already in store
prefixesInBackend, err := listPrefixes(client)
if err != nil {
return err
}
prefixesToPush := []string{}
for prefixByte := range prefixChan {
prefix := hex.EncodeToString(prefixByte)
_, verified := prefixesInBackend[prefix]
// We can safely skip in case a prefix object is verified (pushed as last object)
if !verified {
prefixesToPush = append(prefixesToPush, prefix)
}
}
if len(prefixesToPush) == 0 {
return nil
}
progress(int64(len(prefixesToPush)))
for _, prefix := range prefixesToPush {
// Get prefix object
po, err := core.GetPrefixObject(prefix)
if err != nil {
return err
}
// Get commit object
co, err := core.GetCommitObject(po.S3gitFollowMe)
if err != nil {
return err
}
// Check if there is a tree object
if co.S3gitTree != "" {
// Get tree object
to, err := core.GetTreeObject(co.S3gitTree)
if err != nil {
return err
}
// first push all added blobs in this commit ...
err = pushBlobRange(to.S3gitAdded, nil, hydrated, client)
if err != nil {
return err
}
// then push tree object
_, err = pushBlob(co.S3gitTree, nil, client)
if err != nil {
return err
}
}
// Check if there is a snapshot object
if co.S3gitSnapshot != "" {
err = pushSnapshotWithChildren(co.S3gitSnapshot, client)
if err != nil {
return err
}
}
// then push commit object
_, err = pushBlob(po.S3gitFollowMe, nil, client)
if err != nil {
return err
}
// ... finally push prefix object itself
// (if something goes in chain above, the prefix object will be missing so
// will be (attempted to) uploaded again during the next push)
_, err = pushBlob(prefix, nil, client)
if err != nil {
return err
}
progress(int64(len(prefixesToPush)))
}
return nil
}
func pushSnapshotWithChildren(hash string, client backend.Backend) error {
// TODO: [perf] Push snapshot objects in parallel
// Get snapshot object
so, err := core.GetSnapshotObject(hash)
if err != nil {
return err
}
for _, entry := range so.S3gitEntries {
if entry.IsDirectory() {
err = pushSnapshotWithChildren(entry.Blob, client)
}
}
// Push snapshot object
_, err = pushBlob(hash, nil, client)
if err != nil {
return err
}
return nil
}
// Push a blob to the back end store
func pushBlob(hash string, size *uint64, client backend.Backend) (newlyUploaded bool, err error) {
// TODO: Consider whether we want to verify again...
if false {
verified, err := client.VerifyHash(hash)
if err != nil {
return false, err
} else if verified { // Resource already in back-end
return false, nil
}
}
// TODO: [perf] for back ends storing whole files: consider multipart upload?
cr := cas.MakeReader(hash)
if cr == nil {
panic(errors.New("Failed to create cas reader"))
}
err = client.UploadWithReader(hash, cr)
if err != nil {
return false, err
}
// Move blob from .stage to .cache directory upon successful upload
err = cas.MoveBlobToCache(hash)
if err != nil {
return false, err
}
return true, nil
}
// Push a blob to the back end store in deduplicated format
func PushBlobDeduped(hash string, size *uint64, client backend.Backend) (newlyUploaded bool, err error) {
// TODO: [perf] for back ends storing chunks: upload chunks in parallel
hx, err := hex.DecodeString(hash)
if err != nil {
return false, err
}
// Get hashes for leaves
leafHashes, _, err := kv.GetLevel1(hx)
if err != nil {
return false, err
} else if len(leafHashes) == 0 {
return false, errors.New(fmt.Sprintf("Unable to push an empty blob: %s", hash))
}
// Iterate over the leaves and push up to remote
for i := 0; i < len(leafHashes); i += cas.KeySize {
// TODO: verify whether leaf blob is already in back end, and skip if so
err := cas.PushLeafBlob(hex.EncodeToString(leafHashes[i:i+cas.KeySize]), client)
if err != nil {
return false, err
}
}
// Finally upload root hash
b := bytes.NewBuffer(leafHashes)
err = client.UploadWithReader(hash, b)
if err != nil {
return false, err
}
// Move blob from .stage to .cache directory upon successful upload
err = cas.MoveBlobToCache(hash)
if err != nil {
return false, err
}
return true, nil
}
// Push a range of blobs to the back end store in parallel
//
// See https://github.com/adonovan/gopl.io/blob/master/ch8/thumbnail/thumbnail_test.go
//
func pushBlobRange(hashes []string, size *uint64, hydrated bool, client backend.Backend) error {
var wg sync.WaitGroup
var msgs = make(chan string)
var results = make(chan error)
for i := 0; i < min(len(hashes), 100); i++ {
wg.Add(1)
go func() {
defer wg.Done()
for hash := range msgs {
pushHydratedToRemote := hydrated
if !checkIfLeavesAreEqualSize(hash) {
pushHydratedToRemote = false // Cannot push hydrated to remote back end when eg rolling hash is used (as we do not know where the boundaries are)
}
var err error
if pushHydratedToRemote {
_, err = pushBlob(hash, size, client)
} else {
_, err = PushBlobDeduped(hash, size, client)
}
results <- err
}
}()
}
go func() {
for _, hash := range hashes {
msgs <- hash
}
close(msgs)
}()
go func() {
wg.Wait()
close(results)
}()
var err error
for e := range results {
if e != nil {
err = e
}
}
return err
}
func checkIfLeavesAreEqualSize(hash string) bool {
// TODO: Implement: iterate over all leaves, check whether (except for last node) all sizes are equal
return true
}
// List prefixes at back end store, doing 16 lists in parallel
func listPrefixes(client backend.Backend) (map[string]bool, error) {
var wg sync.WaitGroup
var results = make(chan []string)
for i := 0x0; i <= 0xf; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
result := make([]string, 0, 1000)
client.List(fmt.Sprintf("%s%x", core.Prefix(), i), func(key string) {
result = append(result, key)
// TODO: WE NEED TO wg.Done() HERE WHEN LAST KEY HAS BEEN RECEIVED
// -- SEE /Users/frankw/golang/src/github.com/fwessels/listperf/listperf.go
// IMPORTANT: WE WILL BE MISSING OBJECTS HERE
})
results <- result
}(i)
}
go func() {
wg.Wait()
close(results)
}()
prefixHash := make(map[string]bool)
for result := range results {
for _, r := range result {
prefixHash[r] = true
}
}
return prefixHash, nil
}
func min(a, b int) int {
if a < b {
return a
}
return b
}