-
Notifications
You must be signed in to change notification settings - Fork 180
/
copy-move.go
475 lines (424 loc) · 15.3 KB
/
copy-move.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
package views
import (
"context"
"fmt"
"io"
"math"
"path"
"strings"
"sync"
"time"
"github.com/micro/go-micro/client"
"github.com/nicksnyder/go-i18n/i18n"
"github.com/pborman/uuid"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/proto/tree"
context2 "github.com/pydio/cells/common/utils/context"
"github.com/pydio/cells/common/utils/permissions"
)
type (
sessionIDKey struct{}
)
const (
// Consider move takes 1s per 100MB of data to copy
lockExpirationRatioSize = 1024 * 1024 * 100
lockExpirationRatioNumber = 25
)
// WithSessionID returns a context which knows its service assigned color
func WithSessionID(ctx context.Context, session string) context.Context {
return context.WithValue(ctx, sessionIDKey{}, session)
}
// GetSessionID returns the session ID in context
func GetSessionID(ctx context.Context) (string, bool) {
res, ok := ctx.Value(sessionIDKey{}).(string)
return res, ok
}
// CopyMoveNodes performs a recursive copy or move operation of a node to a new location. It can be inter- or intra-datasources.
// It will eventually pass contextual metadata like X-Pydio-Session (to batch event inside the SYNC) or X-Pydio-Move (to
// reconciliate creates and deletes when move is done between two differing datasources).
func CopyMoveNodes(ctx context.Context, router Handler, sourceNode *tree.Node, targetNode *tree.Node, move bool, recursive bool, isTask bool, statusChan chan string, progressChan chan float32, tFunc ...i18n.TranslateFunc) (oErr error) {
sessionPrefix := common.SyncSessionPrefixCopy
if move {
sessionPrefix = common.SyncSessionPrefixMove
}
session := sessionPrefix + uuid.New()
childrenMoved := 0
var totalSize int64
var locker permissions.SessionLocker
ctx = WithSessionID(ctx, session)
// Make sure all sessions are purged !
defer func() {
if p := recover(); p != nil {
log.Logger(ctx).Error("Error during copy/move", zap.Error(p.(error)))
oErr = p.(error)
go func() {
<-time.After(10 * time.Second)
log.Logger(ctx).Info("Forcing close session " + session + " and unlock")
client.Publish(context.Background(), client.NewPublication(common.TOPIC_INDEX_EVENT, &tree.IndexEvent{
SessionForceClose: session,
}))
if locker != nil {
locker.Unlock(ctx)
}
}()
}
}()
// TODO - should be its own function really ?
publishError := func(dsName, errorPath string) {
client.Publish(context.Background(), client.NewPublication(common.TOPIC_INDEX_EVENT, &tree.IndexEvent{
ErrorDetected: true,
DataSourceName: dsName,
ErrorPath: errorPath,
}))
}
// Setting up logger
logger := log.Logger(ctx)
var taskLogger *zap.Logger
if isTask {
taskLogger = log.TasksLogger(ctx)
} else {
taskLogger = zap.New(zapcore.NewNopCore())
}
// Read root of target to detect if it is on the same datasource as sourceNode
var crossDs bool
var sourceDs, targetDs string
sourceDs = sourceNode.GetStringMeta(common.META_NAMESPACE_DATASOURCE_NAME)
if move {
if tDs := targetNode.GetStringMeta(common.META_NAMESPACE_DATASOURCE_NAME); tDs != "" {
targetDs = tDs
crossDs = targetDs != sourceDs
} else {
parts := strings.Split(strings.Trim(targetNode.Path, "/"), "/")
if len(parts) > 0 {
if testRoot, e := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: parts[0]}}); e == nil {
targetDs = testRoot.Node.GetStringMeta(common.META_NAMESPACE_DATASOURCE_NAME)
crossDs = targetDs != sourceDs
}
}
}
}
var childLockDir, childLockBase string
if parentTarget, e := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: &tree.Node{Path: path.Dir(targetNode.Path)}}); e == nil {
childLockDir = parentTarget.GetNode().Uuid
childLockBase = path.Base(targetNode.Path)
}
if !IsUnitTestEnv { // Do not trigger during unit tests as it calls ACL service
if move {
log.Logger(ctx).Info("Setting Lock on SourceNode with session " + session)
locker = permissions.NewLockSession(sourceNode.Uuid, session, time.Second*30)
} else {
locker = permissions.NewLockSession("", session, time.Second*30)
}
if childLockDir != "" {
log.Logger(ctx).Info("Setting ChildLock on TargetNode Parent")
locker.AddChildTarget(childLockDir, childLockBase)
}
// Will be unlocked by sync process
if err := locker.Lock(ctx); err != nil {
log.Logger(ctx).Warn("Could not init lockSession", zap.Error(err))
}
}
if recursive && !sourceNode.IsLeaf() {
prefixPathSrc := strings.TrimRight(sourceNode.Path, "/")
prefixPathTarget := strings.TrimRight(targetNode.Path, "/")
targetDsPath := targetNode.GetStringMeta(common.META_NAMESPACE_DATASOURCE_PATH)
// List all children and move them all
streamer, err := router.ListNodes(ctx, &tree.ListNodesRequest{
Node: sourceNode,
Recursive: true,
})
if err != nil {
logger.Error("Copy/move - List Nodes", zap.Error(err))
return err
}
var children []*tree.Node
defer streamer.Close()
var statErrors int
for {
child, cE := streamer.Recv()
if cE != nil {
break
}
if child == nil {
continue
}
if child.Node.IsLeaf() {
if _, statErr := router.ReadNode(ctx, &tree.ReadNodeRequest{Node: child.Node, ObjectStats: true}); statErr != nil {
statErrors++
}
totalSize += child.Node.Size
}
children = append(children, child.Node)
}
if statErrors > 0 {
// There are some missing childrens, this copy/move operation will fail - interrupt now
publishError(sourceDs, sourceNode.Path)
return fmt.Errorf("Errors found while copy/move node, stopping")
}
if len(children) > 0 {
cMess := fmt.Sprintf("There are %v children to move", len(children))
logger.Info(cMess)
taskLogger.Info(cMess)
}
//total := len(children)
if !move {
// For Copy case, first create new folders with fresh UUID
for _, childNode := range children {
if childNode.IsLeaf() {
continue
}
childPath := childNode.Path
relativePath := strings.TrimPrefix(childPath, prefixPathSrc+"/")
targetPath := prefixPathTarget + "/" + relativePath
statusChan <- copyMoveStatusKey(relativePath, move, tFunc...)
folderNode := childNode.Clone()
folderNode.Path = targetPath
folderNode.Uuid = uuid.New()
if targetDsPath != "" {
folderNode.SetMeta(common.META_NAMESPACE_DATASOURCE_PATH, path.Join(targetDsPath, relativePath))
}
_, e := router.CreateNode(ctx, &tree.CreateNodeRequest{Node: folderNode, IndexationSession: session, UpdateIfExists: true})
if e != nil {
logger.Error("-- Create Folder ERROR", zap.Error(e), zap.Any("from", childNode.Path), zap.Any("to", targetPath))
publishError(targetDs, folderNode.Path)
panic(e)
}
logger.Debug("-- Copy Folder Success ", zap.String("to", targetPath), childNode.Zap())
taskLogger.Info("-- Copied Folder To " + targetPath)
}
} else {
// For move, update lock expiration based on total size
updateLockerForByteSize(ctx, locker, totalSize, len(children))
}
wg := &sync.WaitGroup{}
queue := make(chan struct{}, 4)
t := time.Now()
var lastNode *tree.Node
var errors []error
progress := ©PgReader{
progressChan: progressChan,
total: totalSize,
}
for idx, childNode := range children {
if idx == len(children)-1 {
lastNode = childNode
continue
}
// copy for inner function
childNode := childNode
wg.Add(1)
queue <- struct{}{}
go func() {
defer func() {
<-queue
defer wg.Done()
}()
e := processCopyMove(ctx, router, session, move, crossDs, sourceDs, targetDs, false, childNode, prefixPathSrc, prefixPathTarget, targetDsPath, logger, publishError, statusChan, progress, tFunc...)
if e != nil {
errors = append(errors, e)
} else {
childrenMoved++
taskLogger.Info("-- Copy/Move Success for " + childNode.Path)
}
}()
}
wg.Wait()
if len(errors) > 0 {
panic(errors[0])
}
if lastNode != nil {
// Now process very last node
e := processCopyMove(ctx, router, session, move, crossDs, sourceDs, targetDs, true, lastNode, prefixPathSrc, prefixPathTarget, targetDsPath, logger, publishError, statusChan, progress, tFunc...)
if e != nil {
panic(e)
}
childrenMoved++
taskLogger.Info("-- Copy/Move Success for " + lastNode.Path)
}
log.Logger(ctx).Info("Recursive copy operation timing", zap.Duration("duration", time.Now().Sub(t)))
}
if childrenMoved > 0 {
log.Logger(ctx).Info(fmt.Sprintf("Successfully copied or moved %v, now moving parent node", childrenMoved))
}
// Now Copy/Move initial node
if sourceNode.IsLeaf() {
updateLockerForByteSize(ctx, locker, sourceNode.Size, 1)
// Prepare Meta for Copy/Delete operations. If Move accross DS or Copy, we send directly the close- session
// as this will be a one shot operation on each datasource.
copyMeta := make(map[string]string)
deleteMeta := make(map[string]string)
closeSession := common.SyncSessionClose_ + session
if move {
copyMeta[common.X_AMZ_META_DIRECTIVE] = "COPY"
deleteMeta[common.XPydioSessionUuid] = closeSession
if crossDs {
copyMeta[common.XPydioSessionUuid] = closeSession
// Identify copy/delete across 2 datasources
copyMeta[common.XPydioMoveUuid] = sourceNode.Uuid
deleteMeta[common.XPydioMoveUuid] = sourceNode.Uuid
} else {
copyMeta[common.XPydioSessionUuid] = session
}
} else {
copyMeta[common.X_AMZ_META_DIRECTIVE] = "REPLACE"
copyMeta[common.XPydioSessionUuid] = closeSession
}
statusChan <- copyMoveStatusKey(sourceNode.Path, move, tFunc...)
_, e := router.CopyObject(ctx, sourceNode, targetNode, &CopyRequestData{
Metadata: copyMeta,
Progress: ©PgReader{offset: 0, total: sourceNode.Size, progressChan: progressChan},
})
if e != nil {
publishError(sourceDs, sourceNode.Path)
publishError(targetDs, targetNode.Path)
panic(e)
}
// Remove Source Node
if move {
ctx = context2.WithAdditionalMetadata(ctx, deleteMeta)
_, moveErr := router.DeleteNode(ctx, &tree.DeleteNodeRequest{Node: sourceNode})
if moveErr != nil {
logger.Error("-- Delete Source Error / Reverting Copy", zap.Error(moveErr), sourceNode.Zap())
router.DeleteNode(ctx, &tree.DeleteNodeRequest{Node: targetNode})
publishError(sourceDs, sourceNode.Path)
panic(moveErr)
}
}
} else if !move {
session = common.SyncSessionClose_ + session
logger.Debug("-- Copying sourceNode with empty Uuid - Close Session")
targetNode.Type = tree.NodeType_COLLECTION
_, e := router.CreateNode(ctx, &tree.CreateNodeRequest{Node: targetNode, IndexationSession: session, UpdateIfExists: true})
if e != nil {
panic(e)
}
taskLogger.Info("-- Copied sourceNode with empty Uuid - Close Session")
}
if move {
// Send an optimistic event => s3 operations are done, let's update UX before indexation is finished
optimisticTarget := sourceNode.Clone()
optimisticTarget.Path = targetNode.Path
optimisticTarget.SetMeta("name", path.Base(targetNode.Path))
log.Logger(ctx).Debug("Finished move - Sending Optimistic Event", sourceNode.Zap("from"), optimisticTarget.Zap("to"))
client.Publish(ctx, client.NewPublication(common.TOPIC_TREE_CHANGES, &tree.NodeChangeEvent{
Optimistic: true,
Type: tree.NodeChangeEvent_UPDATE_PATH,
Source: sourceNode,
Target: optimisticTarget,
}))
}
return
}
func updateLockerForByteSize(ctx context.Context, locker permissions.SessionLocker, totalSize int64, numberOfFiles int) {
if locker == nil {
return
}
countRatio := math.Ceil(float64(numberOfFiles) / float64(lockExpirationRatioNumber))
ratio := math.Ceil(float64(totalSize) / float64(lockExpirationRatioSize))
r := math.Max(countRatio, ratio) + 2
newD := time.Duration(int64(r) * int64(time.Second))
log.Logger(ctx).Info("Updating lock expiration to ", zap.Duration("duration", newD))
locker.UpdateExpiration(ctx, newD)
}
func copyMoveStatusKey(keyPath string, move bool, tFunc ...i18n.TranslateFunc) string {
var statusPath = keyPath
if path.Base(statusPath) == common.PYDIO_SYNC_HIDDEN_FILE_META {
statusPath = path.Dir(statusPath)
}
statusPath = path.Base(statusPath)
status := "Copying " + statusPath
tKey := "Jobs.User.CopyingItem"
if move {
status = "Moving " + statusPath
tKey = "Jobs.User.MovingItem"
}
if len(tFunc) > 0 {
status = strings.Replace(tFunc[0](tKey), "%s", statusPath, -1)
}
return status
}
func processCopyMove(ctx context.Context, handler Handler, session string, move, crossDs bool, sourceDs, targetDs string, closeSession bool, childNode *tree.Node, prefixPathSrc, prefixPathTarget, targetDsPath string, logger *zap.Logger, publishError func(string, string), statusChan chan string, progress io.Reader, tFunc ...i18n.TranslateFunc) error {
childPath := childNode.Path
relativePath := strings.TrimPrefix(childPath, prefixPathSrc+"/")
targetPath := prefixPathTarget + "/" + relativePath
targetNode := &tree.Node{Path: targetPath}
if targetDsPath != "" {
targetNode.SetMeta(common.META_NAMESPACE_DATASOURCE_PATH, path.Join(targetDsPath, relativePath))
}
var justCopied *tree.Node
justCopied = nil
// Copy files - For "Copy" operation, do NOT copy .pydio files
if childNode.IsLeaf() && (move || path.Base(childPath) != common.PYDIO_SYNC_HIDDEN_FILE_META) {
logger.Debug("Copy " + childNode.Path + " to " + targetPath)
statusChan <- copyMoveStatusKey(relativePath, move, tFunc...)
meta := make(map[string]string, 1)
if move {
meta[common.X_AMZ_META_DIRECTIVE] = "COPY"
} else {
meta[common.X_AMZ_META_DIRECTIVE] = "REPLACE"
}
meta[common.XPydioSessionUuid] = session
if crossDs {
/*
if idx == len(children)-1 {
meta[common.XPydioSessionUuid] = "close-" + session
}
*/
if closeSession {
meta[common.XPydioSessionUuid] = common.SyncSessionClose_ + session
}
if move {
meta[common.XPydioMoveUuid] = childNode.Uuid
}
}
_, e := handler.CopyObject(ctx, childNode, targetNode, &CopyRequestData{
Metadata: meta,
Progress: progress,
})
if e != nil {
logger.Error("-- Copy ERROR", zap.Error(e), zap.Any("from", childNode.Path), zap.Any("to", targetPath))
publishError(sourceDs, childNode.Path)
publishError(targetDs, targetPath)
return e
}
justCopied = targetNode
logger.Debug("-- Copy Success: ", zap.String("to", targetPath), childNode.Zap())
}
// Remove original for move case
if move {
// If we're sending the last Delete here - then we close the session at the same time
if closeSession {
session = common.SyncSessionClose_ + session
}
delCtx := ctx
if crossDs {
delCtx = context2.WithAdditionalMetadata(ctx, map[string]string{common.XPydioMoveUuid: childNode.Uuid})
}
_, moveErr := handler.DeleteNode(delCtx, &tree.DeleteNodeRequest{Node: childNode, IndexationSession: session})
if moveErr != nil {
log.Logger(ctx).Error("-- Delete Error / Reverting Copy", zap.Error(moveErr), childNode.Zap())
if justCopied != nil {
if _, revertErr := handler.DeleteNode(delCtx, &tree.DeleteNodeRequest{Node: justCopied}); revertErr != nil {
log.Logger(ctx).Error("---- Could not Revert", zap.Error(revertErr), justCopied.Zap())
}
}
publishError(sourceDs, childNode.Path)
return moveErr
}
logger.Debug("-- Delete Success " + childNode.Path)
}
return nil
}
type copyPgReader struct {
offset int64
total int64
progressChan chan float32
}
func (c *copyPgReader) Read(p []byte) (n int, err error) {
c.offset += int64(len(p))
c.progressChan <- float32(c.offset) / float32(c.total)
return len(p), nil
}