-
Notifications
You must be signed in to change notification settings - Fork 0
/
bidirectional-patch.go
534 lines (488 loc) · 18.8 KB
/
bidirectional-patch.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright (c) 2019. 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 merger
import (
"context"
"fmt"
"net/url"
"path"
"strings"
json "github.com/pydio/cells/x/jsonx"
"github.com/gobwas/glob"
"go.uber.org/zap"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
"github.com/pydio/cells/common/sync/model"
)
const OpNone OperationType = 100
const OpMoveTarget OperationType = 101
type Solver func(left, right *TreeNode)
// BidirectionalPatch is a Patch that can handle operations going both left and right side.
// It handles conflicts detection and tries to solve them automatically if possible
type BidirectionalPatch struct {
TreePatch
matrix map[OperationType]map[OperationType]Solver
inputsModified bool
unexpected []error
ctx context.Context
ignoreUUIDConflicts bool
}
func NewBidirectionalPatch(ctx context.Context, source, target model.Endpoint) *BidirectionalPatch {
b := &BidirectionalPatch{
TreePatch: *newTreePatch(source.(model.PathSyncSource), target.(model.PathSyncTarget), PatchOptions{MoveDetection: false}),
ctx: ctx,
}
// If syncing on same server, do not trigger conflicts on .pydio
u1, _ := url.Parse(source.GetEndpointInfo().URI)
u2, _ := url.Parse(target.GetEndpointInfo().URI)
if u1.Scheme == "router" && u2.Scheme == "router" {
b.ignoreUUIDConflicts = true
}
if strings.HasPrefix(u1.Scheme, "http") && strings.HasPrefix(u2.Scheme, "http") && u1.Host == u2.Host {
b.ignoreUUIDConflicts = true
}
b.initMatrix()
return b
}
// ComputeBidirectionalPatch merges two unidirectional Patch into one BidirectionalPatch
func ComputeBidirectionalPatch(ctx context.Context, left, right Patch) (*BidirectionalPatch, error) {
source := left.Source()
target, _ := model.AsPathSyncTarget(right.Source())
b := &BidirectionalPatch{
TreePatch: *newTreePatch(source, target, PatchOptions{MoveDetection: false}),
ctx: ctx,
}
// If syncing on same server, do not trigger conflicts on .pydio
u1, _ := url.Parse(source.GetEndpointInfo().URI)
u2, _ := url.Parse(target.GetEndpointInfo().URI)
if u1.Scheme == "router" && u2.Scheme == "router" {
b.ignoreUUIDConflicts = true
}
if strings.HasPrefix(u1.Scheme, "http") && strings.HasPrefix(u2.Scheme, "http") && u1.Host == u2.Host {
b.ignoreUUIDConflicts = true
}
b.initMatrix()
left.Filter(ctx)
right.Filter(ctx)
l, ok1 := left.(*TreePatch)
r, ok2 := right.(*TreePatch)
var e error
if ok1 && ok2 {
b.mergeTrees(&l.TreeNode, &r.TreeNode)
if b.inputsModified {
// SECOND PASS WILL RESET PATCH TOTALLY
log.Logger(ctx).Info("Input trees modified, running a second pass")
b.TreePatch = *newTreePatch(source, target, PatchOptions{MoveDetection: false})
b.mergeTrees(&l.TreeNode, &r.TreeNode)
}
log.Logger(ctx).Info("Merged Patch", zap.Any("stats", b.Stats()))
}
if len(b.unexpected) > 0 {
var ss []string
for _, e := range b.unexpected {
ss = append(ss, e.Error())
}
b.patchError = fmt.Errorf("error during merge: %s", strings.Join(ss, "|"))
return b, fmt.Errorf("error during merge: %s", strings.Join(ss, "|"))
}
return b, e
}
// StartSession overrides AbstractPatch method to handle source and/or target as session provider
func (p *BidirectionalPatch) StartSession(rootNode *tree.Node) (*tree.IndexationSession, error) {
if p.sessionProviderContext == nil {
return &tree.IndexationSession{Uuid: "", Description: "no-op"}, nil
}
ids := make(map[string]*tree.IndexationSession)
if sessionProvider, ok := p.Source().(model.SessionProvider); ok {
if sourceSession, er := sessionProvider.StartSession(p.sessionProviderContext, rootNode, p.sessionSilent); er != nil {
return nil, er
} else {
ids["source"] = sourceSession
}
}
if sessionProvider, ok := p.Target().(model.SessionProvider); ok {
if targetSession, er := sessionProvider.StartSession(p.sessionProviderContext, rootNode, p.sessionSilent); er != nil {
return nil, er
} else {
ids["target"] = targetSession
}
}
session := &tree.IndexationSession{}
if len(ids) > 0 {
data, _ := json.Marshal(ids)
session.Uuid = string(data)
}
return session, nil
}
// FlushSession overrides AbstractPatch method to handle source and/or target as session provider
func (p *BidirectionalPatch) FlushSession(sessionUuid string) error {
if len(sessionUuid) == 0 {
return nil
}
var ids map[string]*tree.IndexationSession
if e := json.Unmarshal([]byte(sessionUuid), &ids); e != nil {
return nil
}
targetSession, o1 := ids["target"]
var e1, e2 error
if sessionProvider, ok := p.Target().(model.SessionProvider); ok && o1 {
e1 = sessionProvider.FlushSession(p.sessionProviderContext, targetSession.Uuid)
}
sourceSession, o2 := ids["source"]
if sessionProvider, ok := p.Source().(model.SessionProvider); ok && o2 {
e2 = sessionProvider.FlushSession(p.sessionProviderContext, sourceSession.Uuid)
}
if e1 != nil {
return e1
}
if e2 != nil {
return e2
}
return nil
}
// FinishSession overrides AbstractPatch method to handle source and/or target as session provider
func (p *BidirectionalPatch) FinishSession(sessionUuid string) error {
if len(sessionUuid) == 0 {
return nil
}
var ids map[string]*tree.IndexationSession
if e := json.Unmarshal([]byte(sessionUuid), &ids); e != nil {
return nil
}
targetSession, o1 := ids["target"]
var e1, e2 error
if sessionProvider, ok := p.Target().(model.SessionProvider); ok && o1 {
e1 = sessionProvider.FinishSession(p.sessionProviderContext, targetSession.Uuid)
}
sourceSession, o2 := ids["source"]
if sessionProvider, ok := p.Source().(model.SessionProvider); ok && o2 {
e2 = sessionProvider.FinishSession(p.sessionProviderContext, sourceSession.Uuid)
}
if e1 != nil {
return e1
}
if e2 != nil {
return e2
}
return nil
}
// Filter override TreePatch.Filter and does nothing. Left and right patches are
// filtered at BidirectionalPatch creation time.
func (p *BidirectionalPatch) Filter(ctx context.Context, ignores ...glob.Glob) {
return
}
// AppendBranch merges another bidir patch into this existing patch
func (p *BidirectionalPatch) AppendBranch(ctx context.Context, branch *BidirectionalPatch) {
p.enqueueOperations(&branch.TreeNode)
}
// initMatrix builds a matrix of left/right operations with Solver functions
func (p *BidirectionalPatch) initMatrix() {
// Use three chars symbols for nice display of the matrix
ign := p.ignore
eqb := p.enqueueBoth
eql := p.enqueueLeft
eqr := p.enqueueRight
rst := p.reSyncTarget
cmt := p.compareMoveTargets
cms := p.compareMoveSources
mrg := p.mergeDataOperations
cnT := func(left, right *TreeNode) { p.enqueueConflict(left, right, ConflictNodeType) }
cnP := func(left, right *TreeNode) { p.enqueueConflict(left, right, ConflictPathOperation) }
p.matrix = map[OperationType]map[OperationType]Solver{
OpNone: {OpNone: ign, OpCreateFolder: eqb, OpMoveFile: eqb, OpMoveFolder: eqb, OpDelete: eqb, OpCreateFile: eqb, OpUpdateFile: eqb, OpMoveTarget: eqb},
OpCreateFolder: {OpNone: nil, OpCreateFolder: ign, OpMoveFile: cnP, OpMoveFolder: cnP, OpDelete: eql, OpCreateFile: cnP, OpUpdateFile: cnP, OpMoveTarget: eqb},
OpMoveFile: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: cmt, OpMoveFolder: cnT, OpDelete: rst, OpCreateFile: cnP, OpUpdateFile: eqb, OpMoveTarget: eqb},
OpMoveFolder: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: nil, OpMoveFolder: cmt, OpDelete: rst, OpCreateFile: cnT, OpUpdateFile: cnT, OpMoveTarget: eqb},
OpDelete: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: nil, OpMoveFolder: nil, OpDelete: ign, OpCreateFile: eqr, OpUpdateFile: eqr, OpMoveTarget: eqb},
OpCreateFile: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: nil, OpMoveFolder: nil, OpDelete: nil, OpCreateFile: mrg, OpUpdateFile: mrg, OpMoveTarget: eqb},
OpUpdateFile: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: nil, OpMoveFolder: nil, OpDelete: nil, OpCreateFile: nil, OpUpdateFile: mrg, OpMoveTarget: eqb},
OpMoveTarget: {OpNone: nil, OpCreateFolder: nil, OpMoveFile: nil, OpMoveFolder: nil, OpDelete: nil, OpCreateFile: nil, OpUpdateFile: nil, OpMoveTarget: cms},
}
}
// mergeTrees performs the walk of left and right to enqueue operations to the current Patch
func (p *BidirectionalPatch) mergeTrees(left, right *TreeNode) {
cL := left.GetCursor()
cR := right.GetCursor()
a := cL.Next()
b := cR.Next()
for a != nil || b != nil {
if a != nil && b != nil {
switch strings.Compare(a.Label(), b.Label()) {
case 0:
// MAYBE A Conflict here!
p.merge(a, b)
p.mergeTrees(a, b)
a = cL.Next()
b = cR.Next()
continue
case 1:
p.enqueueOperations(b, OperationDirLeft)
b = cR.Next()
continue
case -1:
p.enqueueOperations(a, OperationDirRight)
a = cL.Next()
continue
}
} else if a == nil && b != nil {
p.enqueueOperations(b, OperationDirLeft)
b = cR.Next()
continue
} else if b == nil && a != nil {
p.enqueueOperations(a, OperationDirRight)
a = cL.Next()
continue
}
}
}
// enqueueOperation adds an operation to the patch, with an optional operation direction
func (p *BidirectionalPatch) enqueueOperations(branch *TreeNode, direction ...OperationDirection) {
branch.WalkOperations([]OperationType{}, func(operation Operation) {
toEnqueue := operation.Clone()
if len(direction) > 0 {
toEnqueue.SetDirection(direction[0])
}
p.Enqueue(toEnqueue)
})
}
// merge finds the correct solver for two similar nodes
func (p *BidirectionalPatch) merge(left, right *TreeNode) {
// Merge PATH or Data Ops
opLeft, opRight := OpNone, OpNone
if left.PathOperation != nil {
opLeft = left.PathOperation.Type()
} else if left.DataOperation != nil {
opLeft = left.DataOperation.Type()
} else if left.MoveSourcePath != "" {
opLeft = OpMoveTarget
}
if right.PathOperation != nil {
opRight = right.PathOperation.Type()
} else if right.DataOperation != nil {
opRight = right.DataOperation.Type()
} else if right.MoveSourcePath != "" {
opRight = OpMoveTarget
}
solver := p.matrix[opLeft][opRight]
if solver == nil {
solver = p.matrix[opRight][opLeft]
}
solver(left, right)
}
// ignore does not enqueue anything to patch
func (p *BidirectionalPatch) ignore(left, right *TreeNode) {
if left.PathOperation != nil || right.PathOperation != nil {
log.Logger(p.ctx).Debug("Ignoring Change", zap.Any("left", left.PathOperation), zap.Any("right", right.PathOperation))
}
}
// enqueueBoth enqueue both operations to the patch
func (p *BidirectionalPatch) enqueueBoth(left, right *TreeNode) {
p.enqueueLeft(left, right)
p.enqueueRight(left, right)
}
// enqueueLeft only enqueues the Left operation with DirRight
func (p *BidirectionalPatch) enqueueLeft(left, right *TreeNode) {
p.enqueueOperations(left, OperationDirRight)
}
// enqueueRight only enqueues the Right operation with DirLeft
func (p *BidirectionalPatch) enqueueRight(left, right *TreeNode) {
p.enqueueOperations(right, OperationDirLeft)
}
// enqueueConflict sets a Conflict flag on the the given path in side the patch. The Conflict has references to left and right operations
func (p *BidirectionalPatch) enqueueConflict(left, right *TreeNode, t ConflictType) {
log.Logger(p.ctx).Error("-- Unsolvable conflict!", zap.Any("left", left.PathOperation), zap.Any("right", right.PathOperation))
p.unexpected = append(p.unexpected, fmt.Errorf("registered conflict at path %s", left.Path))
var leftOp, rightOp Operation
if left.PathOperation != nil {
leftOp = left.PathOperation
} else if left.DataOperation != nil {
leftOp = left.DataOperation
}
if right.PathOperation != nil {
rightOp = right.PathOperation
} else if right.DataOperation != nil {
rightOp = right.DataOperation
}
op := NewConflictOperation(&left.Node, t, leftOp, rightOp)
p.QueueOperation(op)
}
// reSyncTarget tries to fix a Deleted resource that has been recreated on the other target by recreating it recursively
func (p *BidirectionalPatch) reSyncTarget(left, right *TreeNode) {
var requeueNode *TreeNode
if left.PathOperation.Type() == OpDelete {
requeueNode = right
} else {
requeueNode = left
}
source := requeueNode.PathOperation.Source()
log.Logger(p.ctx).Info("Delete + Move: should recreate target (file or folder) for " + requeueNode.OpMoveTarget.Path)
targetPath := requeueNode.OpMoveTarget.Path
n, e := source.LoadNode(p.ctx, targetPath)
if e != nil {
// Cannot find move target, ignore
log.Logger(p.ctx).Debug("Cannot find move target, ignoring")
return
}
newOp := requeueNode.PathOperation.Clone()
if n.IsLeaf() {
newOp.UpdateType(OpCreateFile)
} else {
newOp.UpdateType(OpCreateFolder)
}
newOp.UpdateRefPath(targetPath)
left.PathOperation = nil
left.OpMoveTarget = nil
right.PathOperation = nil
right.OpMoveTarget = nil
requeueNode.getRoot().QueueOperation(newOp)
if !n.IsLeaf() {
// Enqueue folder children as creates
source.Walk(func(path string, node *tree.Node, err error) {
oType := OpCreateFolder
if node.IsLeaf() {
oType = OpCreateFile
}
childOp := NewOperation(oType, model.NodeToEventInfo(p.ctx, node.GetPath(), node, model.EventCreate))
childOp.SetNode(node)
childOp.AttachToPatch(p)
requeueNode.getRoot().QueueOperation(childOp)
}, targetPath, true)
}
p.inputsModified = true
}
// compareMoveSources finds conflicts on "MoveTarget" nodes that don't have operation but that may have been moved
// from two different sources. No auto-solving there
func (p *BidirectionalPatch) compareMoveSources(left, right *TreeNode) {
if left.MoveSourcePath == right.MoveSourcePath {
log.Logger(p.ctx).Debug("-- Moved from the same source, do not trigger conflict")
} else {
log.Logger(p.ctx).Info("-- Different sources pointing to same target, trigger a conflict")
p.enqueueConflict(left, right, ConflictMoveSameSource)
}
}
// compareMoveTargets finds conflicts on Move operations where a source node would have been moved to two different targets.
// Auto-solving is performed by detecting the most recent operation.
func (p *BidirectionalPatch) compareMoveTargets(left, right *TreeNode) {
if left.OpMoveTarget.Path == right.OpMoveTarget.Path {
log.Logger(p.ctx).Debug("-- Moved toward the same path, ignore!")
} else {
l, err1 := left.PathOperation.Source().LoadNode(p.ctx, left.OpMoveTarget.Path)
r, err2 := right.PathOperation.Target().LoadNode(p.ctx, right.OpMoveTarget.Path)
if err1 != nil {
p.unexpected = append(p.unexpected, err1)
} else if err2 != nil {
p.unexpected = append(p.unexpected, err2)
} else if l.MTime > r.MTime {
log.Logger(p.ctx).Info("-- Moved toward different paths, left is more recent that right => left wins")
// Ignore Left - Change Right from Orig => OpMoveTarget.Path to RIGHT.OpMoveTarget.Path => LEFT.OpMoveTargetPath
newOp := right.PathOperation.Clone()
newOp.UpdateMoveOriginPath(right.OpMoveTarget.Path)
newOp.UpdateRefPath(left.OpMoveTarget.Path)
right.PathOperation = nil
right.OpMoveTarget = nil
left.PathOperation = nil
left.OpMoveTarget = nil
left.getRoot().QueueOperation(newOp)
p.inputsModified = true
} else {
log.Logger(p.ctx).Info("-- Moved toward different paths, right is more recent than left => right wins")
// Ignore Right - Change Left
newOp := left.PathOperation.Clone()
newOp.UpdateMoveOriginPath(left.OpMoveTarget.Path)
newOp.UpdateRefPath(right.OpMoveTarget.Path)
right.PathOperation = nil
right.OpMoveTarget = nil
left.PathOperation = nil
left.OpMoveTarget = nil
right.getRoot().QueueOperation(newOp)
p.inputsModified = true
}
}
}
// mergeDataOperations handles DataOperations (CreateFile, UpdateFile) by checking the nodes ETag. If they differ, auto-solving
// is done by creating a -left and -right version on both side, so that users can fix them manually afterward.
func (p *BidirectionalPatch) mergeDataOperations(left, right *TreeNode) {
if left.DataOperation != nil && right.DataOperation != nil {
lOp := left.DataOperation
rOp := right.DataOperation
initialPath := lOp.GetRefPath()
ext := path.Ext(initialPath)
basePath := strings.TrimSuffix(initialPath, ext)
if lOp.GetNode().GetEtag() == rOp.GetNode().GetEtag() {
log.Logger(p.ctx).Info("-- DataOperation detected on both sides, but same Etag, ignore")
return
}
log.Logger(p.ctx).Info("-- DataOperation detected on both sides, versions differ - keep both")
if path.Base(initialPath) == common.PydioSyncHiddenFile {
if p.ignoreUUIDConflicts {
log.Logger(p.ctx).Info("-- Conflict found on .pydio but patch must ignore")
return
} else {
log.Logger(p.ctx).Error("-- Conflict found on .pydio => One .pydio content should be refreshed!")
return
}
}
// TODO - FIND A CLEANER WAY ?
leftSuffix, rightSuffix := p.computeAutoFixSuffixes(left.DataOperation.Source().GetEndpointInfo(), right.DataOperation.Source().GetEndpointInfo())
leftSource, _ := model.AsPathSyncTarget(left.DataOperation.Source())
leftSource.MoveNode(p.ctx, initialPath, basePath+"-"+leftSuffix+ext)
lOp.UpdateRefPath(basePath + "-" + leftSuffix + ext)
lOp.UpdateType(OpCreateFile)
p.Enqueue(lOp.Clone().SetDirection(OperationDirRight))
rightSource, _ := model.AsPathSyncTarget(right.DataOperation.Source())
rightSource.MoveNode(p.ctx, initialPath, basePath+"-"+rightSuffix+ext)
rOp.UpdateRefPath(basePath + "-" + rightSuffix + ext)
rOp.UpdateType(OpCreateFile)
p.Enqueue(rOp.Clone().SetDirection(OperationDirLeft))
// Remove original ones
left.DataOperation = nil
right.DataOperation = nil
} else if left.DataOperation != nil {
p.Enqueue(left.DataOperation.Clone().SetDirection(OperationDirRight))
} else if right.DataOperation != nil {
p.Enqueue(right.DataOperation.Clone().SetDirection(OperationDirLeft))
}
}
func (p *BidirectionalPatch) computeAutoFixSuffixes(source, target model.EndpointInfo) (string, string) {
leftSuffix := "left"
rightSuffix := "right"
leftUri, _ := url.Parse(source.URI)
rightUri, _ := url.Parse(target.URI)
if leftUri.Scheme != rightUri.Scheme {
leftSuffix = p.autoFixSuffix(leftUri.Scheme)
rightSuffix = p.autoFixSuffix(rightUri.Scheme)
}
return leftSuffix, rightSuffix
}
func (p *BidirectionalPatch) autoFixSuffix(scheme string) string {
switch scheme {
case "fs":
return "local"
case "http", "https":
return "server"
case "s3":
return "s3"
case "local":
return "datasource"
default:
return scheme
}
}