-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync.go
386 lines (341 loc) · 10 KB
/
sync.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* Copyright (c) 2018. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// Package task defines a synchronization task
package task
import (
"context"
"fmt"
"path/filepath"
"runtime/debug"
"sync"
"time"
"github.com/pkg/errors"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/sync/endpoints/memory"
"github.com/gobwas/glob"
"github.com/pydio/cells/common/log"
"go.uber.org/zap"
"github.com/pydio/cells/common/sync/filters"
"github.com/pydio/cells/common/sync/merger"
"github.com/pydio/cells/common/sync/model"
"github.com/pydio/cells/common/sync/proc"
)
type Sync struct {
Source model.Endpoint
Target model.Endpoint
Direction model.DirectionType
Roots []string
Ignores []glob.Glob
SkipTargetChecks bool
FailsafeDeletes bool
snapshotFactory model.SnapshotFactory
echoFilter *filters.EchoFilter
eventsBatchers []*filters.EventsBatcher
processor *proc.ConnectedProcessor
patchListener merger.PatchListener
watch bool
watchersChan []chan bool
watchConn chan *model.EndpointStatus
statuses chan model.Status
runDone chan interface{}
cmd *model.Command
patchChan chan merger.Patch
}
// NewSync creates a new sync task
func NewSync(left model.Endpoint, right model.Endpoint, direction model.DirectionType) *Sync {
return &Sync{
Source: left,
Target: right,
Direction: direction,
}
}
// SetFilters allows passing selective roots (includes) and ignores (excludes).
// Ignores are a list of patterns conforming to the glob standard, with support for double star
func (s *Sync) SetFilters(roots []string, excludes []string) {
if roots != nil {
s.Roots = roots
}
for _, i := range excludes {
if g, e := glob.Compile(i, '/'); e == nil {
s.Ignores = append(s.Ignores, g)
} else {
log.Logger(context.Background()).Error("Unsupported glob pattern format!", zap.Error(e))
}
}
}
// SetPatchListener adds a listener on the Patch channel to do something with patches
// before they are processed
func (s *Sync) SetPatchListener(listener merger.PatchListener) {
s.patchListener = listener
}
// Start makes a first sync and setup watchers
func (s *Sync) Start(ctx context.Context, withWatches bool) {
// Init processor
s.processor = proc.NewConnectedProcessor(ctx, s.cmd)
if s.SkipTargetChecks {
s.processor.SkipTargetChecks = true
}
s.patchChan = s.processor.PatchChan
if s.patchListener != nil {
s.processor.PatchListener = s.patchListener
}
s.processor.Ignores = s.Ignores
s.processor.Start()
// Init EchoFilter
if s.Direction == model.DirectionBi {
s.echoFilter = filters.NewEchoFilter()
s.processor.SetLocksChan(s.echoFilter.GetLocksChan())
s.echoFilter.Start()
}
if withWatches {
s.startWatchers(ctx)
} else if s.watchConn != nil {
go func() {
<-time.After(2 * time.Second)
s.watchConn <- &model.EndpointStatus{
WatchConnection: model.WatchConnected,
EndpointInfo: s.Source.GetEndpointInfo(),
}
s.watchConn <- &model.EndpointStatus{
WatchConnection: model.WatchConnected,
EndpointInfo: s.Target.GetEndpointInfo(),
}
}()
}
}
// Pause should pause the sync
func (s *Sync) Pause(ctx context.Context) {
s.stopWatchers()
}
// Resume should resume the sync
func (s *Sync) Resume(ctx context.Context) {
s.startWatchers(ctx)
}
// Shutdown closes channels
func (s *Sync) Shutdown() {
defer func() {
// ignore 'close on closed channel'
recover()
}()
s.stopWatchers()
if s.watchConn != nil {
close(s.watchConn)
s.watchConn = nil
}
s.processor.Stop()
if s.echoFilter != nil {
s.echoFilter.Stop()
}
}
// Run runs the sync with panic recovery
func (s *Sync) Run(ctx context.Context, dryRun bool, force bool) (model.Stater, error) {
var err error
defer func() {
if e := recover(); e != nil {
fmt.Println("stacktrace from panic: \n" + string(debug.Stack()))
if er, ok := e.(error); ok {
err = er
if s.statuses != nil {
s.statuses <- model.NewProcessingStatus(err.Error()).SetError(err).SetProgress(1)
}
if s.runDone != nil {
s.runDone <- 0
}
}
}
}()
stater, err := s.run(ctx, dryRun, force)
if err != nil && s.statuses != nil {
s.statuses <- model.NewProcessingStatus(err.Error()).SetError(err).SetProgress(1)
if s.runDone != nil {
if stater != nil {
s.runDone <- stater
} else {
s.runDone <- 0
}
}
}
return stater, err
}
func (s *Sync) ReApplyPatch(ctx context.Context, patch merger.Patch) {
patch.SkipFilterToTarget(false)
patch.CleanErrors()
patch.SetupChannels(s.statuses, s.runDone, s.cmd)
s.patchChan <- patch
}
// SetSnapshotFactory set up a factory for loading/saving snapshots
func (s *Sync) SetSnapshotFactory(factory model.SnapshotFactory) {
s.snapshotFactory = factory
}
// SetupEventsChan wires internal sync event to external status channels
func (s *Sync) SetupEventsChan(statusChan chan model.Status, batchDone chan interface{}, events chan interface{}) {
s.statuses = statusChan
s.runDone = batchDone
if events != nil {
// Forward internal events to sync event
s.watchConn = make(chan *model.EndpointStatus)
go func() {
for {
select {
case e, ok := <-s.watchConn:
if !ok {
return
}
events <- e
}
}
}()
}
}
func (s *Sync) SetupCmd(cmd *model.Command) {
s.cmd = cmd
}
// BroadcastCloseSession forwards session id to underlying batchers
func (s *Sync) BroadcastCloseSession(sessionUuid string) {
for _, b := range s.eventsBatchers {
b.ForceCloseSession(sessionUuid)
}
}
func (s *Sync) Capture(ctx context.Context, targetFolder string) error {
source, _ := model.AsPathSyncSource(s.Source)
targetAsSource, _ := model.AsPathSyncSource(s.Target)
if s.Direction == model.DirectionBi && s.snapshotFactory != nil {
leftSnap, err := s.snapshotFactory.Load(source)
if err != nil {
return err
}
if e := s.walkToJSON(ctx, leftSnap, filepath.Join(targetFolder, "snap-source.json")); e != nil {
return e
}
rightSnap, err := s.snapshotFactory.Load(targetAsSource)
if err != nil {
return err
}
if e := s.walkToJSON(ctx, rightSnap, filepath.Join(targetFolder, "snap-target.json")); e != nil {
return e
}
}
if e := s.walkToJSON(ctx, source, filepath.Join(targetFolder, "source.json")); e != nil {
return e
}
if e := s.walkToJSON(ctx, targetAsSource, filepath.Join(targetFolder, "target.json")); e != nil {
return e
}
return nil
}
func (s *Sync) RootStats(ctx context.Context, useSnapshots bool) (map[string]*model.EndpointRootStat, error) {
endpoints := map[string]model.Endpoint{
s.Source.GetEndpointInfo().URI: s.Source,
s.Target.GetEndpointInfo().URI: s.Target,
}
if useSnapshots && s.Direction == model.DirectionBi && s.snapshotFactory != nil {
source, _ := model.AsPathSyncSource(s.Source)
targetAsSource, _ := model.AsPathSyncSource(s.Target)
if leftSnap, err := s.snapshotFactory.Load(source); err == nil {
endpoints[source.GetEndpointInfo().URI] = leftSnap
}
if rightSnap, err := s.snapshotFactory.Load(targetAsSource); err == nil {
endpoints[s.Target.GetEndpointInfo().URI] = rightSnap
}
}
lock := sync.Mutex{}
result := make(map[string]*model.EndpointRootStat, len(endpoints))
wg := &sync.WaitGroup{}
wg.Add(len(endpoints))
var errs []error
for key, ep := range endpoints {
epCopy := ep
keyCopy := key
go func() {
defer wg.Done()
if sourceRoots, e := s.statRoots(ctx, epCopy); e == nil {
lock.Lock()
log.Logger(ctx).Info("Got Stats for "+keyCopy, zap.Any("stats", sourceRoots))
result[keyCopy] = sourceRoots
lock.Unlock()
if !useSnapshots && s.watchConn != nil {
go func() {
s.watchConn <- &model.EndpointStatus{
WatchConnection: model.WatchStats,
EndpointInfo: epCopy.GetEndpointInfo(),
Stats: sourceRoots,
}
}()
}
} else {
errs = append(errs, e)
if !useSnapshots && s.watchConn != nil {
go func() {
s.watchConn <- &model.EndpointStatus{
WatchConnection: model.WatchDisconnected,
EndpointInfo: epCopy.GetEndpointInfo(),
}
}()
}
}
}()
}
wg.Wait()
if len(errs) > 0 {
return nil, errs[0]
}
return result, nil
}
func (s *Sync) walkToJSON(ctx context.Context, source model.PathSyncSource, jsonFile string) error {
db := memory.NewMemDB()
source.Walk(func(path string, node *tree.Node, err error) {
db.CreateNode(ctx, node, false)
}, "/", true)
return db.ToJSON(jsonFile)
}
func (s *Sync) statRoots(ctx context.Context, source model.Endpoint) (stat *model.EndpointRootStat, e error) {
stat = &model.EndpointRootStat{}
var roots []string
for _, r := range s.Roots {
roots = append(roots, r)
}
if len(roots) == 0 {
roots = append(roots, "/")
}
for _, r := range roots {
node, err := source.LoadNode(ctx, r, true)
if err != nil {
return stat, errors.WithMessage(err, "Cannot Stat Root")
}
if node.HasMetaKey("RecursiveChildrenSize") {
stat.HasSizeInfo = true
var s int64
if e := node.GetMeta("RecursiveChildrenSize", &s); e == nil {
stat.Size += s
}
}
if node.HasMetaKey("RecursiveChildrenFolders") && node.HasMetaKey("RecursiveChildrenFiles") {
stat.HasChildrenInfo = true
var folders, files int64
if e := node.GetMeta("RecursiveChildrenFolders", &folders); e == nil {
stat.Folders += folders
}
if e := node.GetMeta("RecursiveChildrenFiles", &files); e == nil {
stat.Files += files
}
}
}
return
}