-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparallel.go
221 lines (191 loc) · 4.81 KB
/
parallel.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
package pail
import (
"context"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/mongodb/grip"
"github.com/mongodb/grip/message"
"github.com/pkg/errors"
)
type parallelBucketImpl struct {
Bucket
size int
deleteOnPush bool
deleteOnPull bool
dryRun bool
}
// ParallelBucketOptions support the use and creation of parallel sync buckets.
type ParallelBucketOptions struct {
// Workers sets the number of worker threads.
Workers int
// DryRun enables running in a mode that will not execute any
// operations that modify the bucket.
DryRun bool
// DeleteOnSync will delete all objects from the target that do not
// exist in the source after the completion of a sync operation
// (Push/Pull).
DeleteOnSync bool
// DeleteOnPush will delete all objects from the target that do not
// exist in the source after the completion of Push.
DeleteOnPush bool
// DeleteOnPull will delete all objects from the target that do not
// exist in the source after the completion of Pull.
DeleteOnPull bool
}
// NewParallelSyncBucket returns a layered bucket implemenation that supports
// parallel sync operations.
func NewParallelSyncBucket(opts ParallelBucketOptions, b Bucket) (Bucket, error) {
if (opts.DeleteOnPush != opts.DeleteOnPull) && opts.DeleteOnSync {
return nil, errors.New("ambiguous delete on sync options set")
}
return ¶llelBucketImpl{
size: opts.Workers,
deleteOnPush: opts.DeleteOnPush || opts.DeleteOnSync,
deleteOnPull: opts.DeleteOnPull || opts.DeleteOnSync,
dryRun: opts.DryRun,
Bucket: b,
}, nil
}
func (b *parallelBucketImpl) Push(ctx context.Context, opts SyncOptions) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var re *regexp.Regexp
var err error
if opts.Exclude != "" {
re, err = regexp.Compile(opts.Exclude)
if err != nil {
return errors.Wrap(err, "compiling exclude regex")
}
}
files, err := walkLocalTree(ctx, opts.Local)
if err != nil {
return errors.WithStack(err)
}
in := make(chan string, len(files))
for i := range files {
if re != nil && re.MatchString(files[i]) {
continue
}
in <- files[i]
}
close(in)
wg := &sync.WaitGroup{}
catcher := grip.NewBasicCatcher()
for i := 0; i < b.size; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for fn := range in {
select {
case <-ctx.Done():
return
default:
}
if b.dryRun {
continue
}
if err := b.Bucket.Upload(ctx, filepath.Join(opts.Remote, fn), filepath.Join(opts.Local, fn)); err != nil {
catcher.Add(err)
cancel()
}
}
}()
}
wg.Wait()
if ctx.Err() == nil && b.deleteOnPush && !b.dryRun {
catcher.Wrap(deleteOnPush(ctx, files, opts.Remote, b), "deleting on sync after push")
}
return catcher.Resolve()
}
func (b *parallelBucketImpl) Pull(ctx context.Context, opts SyncOptions) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var re *regexp.Regexp
var err error
if opts.Exclude != "" {
re, err = regexp.Compile(opts.Exclude)
if err != nil {
return errors.Wrap(err, "compiling exclude regex")
}
}
iter, err := b.List(ctx, opts.Remote)
if err != nil {
return errors.WithStack(err)
}
catcher := grip.NewBasicCatcher()
items := make(chan BucketItem)
toDelete := make(chan string)
go func() {
defer close(items)
for iter.Next(ctx) {
if iter.Err() != nil {
cancel()
catcher.Wrap(iter.Err(), "iterating bucket")
}
if re != nil && re.MatchString(iter.Item().Name()) {
continue
}
select {
case <-ctx.Done():
catcher.Add(ctx.Err())
return
case items <- iter.Item():
}
}
}()
wg := &sync.WaitGroup{}
for i := 0; i < b.size; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for item := range items {
name, err := filepath.Rel(opts.Remote, item.Name())
if err != nil {
catcher.Wrap(err, "getting relative filepath")
cancel()
}
localName := filepath.Join(opts.Local, name)
if err := b.Download(ctx, item.Name(), localName); err != nil {
catcher.Add(err)
cancel()
}
fn := strings.TrimPrefix(item.Name(), opts.Remote)
fn = strings.TrimPrefix(fn, "/")
fn = strings.TrimPrefix(fn, "\\") // cause windows...
select {
case <-ctx.Done():
catcher.Add(ctx.Err())
return
case toDelete <- fn:
}
}
}()
}
go func() {
wg.Wait()
close(toDelete)
}()
deleteSignal := make(chan struct{})
go func() {
defer close(deleteSignal)
keys := []string{}
for key := range toDelete {
keys = append(keys, key)
}
if b.deleteOnPull && b.dryRun {
grip.Debug(message.Fields{
"dry_run": true,
"message": "would delete after push",
})
} else if ctx.Err() == nil && b.deleteOnPull {
catcher.Wrap(deleteOnPull(ctx, keys, opts.Local), "deleting on sync after pull")
}
}()
select {
case <-ctx.Done():
case <-deleteSignal:
}
return catcher.Resolve()
}