Skip to content

Commit

Permalink
copy doesn't need this lock
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed Jun 21, 2021
1 parent 314d3a2 commit 6647f33
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
20 changes: 13 additions & 7 deletions cluster/calcium/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ func (c *Calcium) Copy(ctx context.Context, opts *types.CopyOptions) (chan *type
if err := opts.Validate(); err != nil {
return nil, logger.Err(ctx, err)
}

ch := make(chan *types.CopyMessage)
utils.SentryGo(func() {
defer close(ch)

wg := sync.WaitGroup{}
log.Infof(ctx, "[Copy] Copy %d workloads files", len(opts.Targets))

// workload one by one
for id, paths := range opts.Targets {
wg.Add(1)

utils.SentryGo(func(id string, paths []string) func() {
return func() {
defer wg.Done()
if err := c.withWorkloadLocked(ctx, id, func(ctx context.Context, workload *types.Workload) error {
for _, path := range paths {
resp, name, err := workload.Engine.VirtualizationCopyFrom(ctx, workload.ID, path)
ch <- makeCopyMessage(id, name, path, err, resp)
}
return nil
}); err != nil {

workload, err := c.GetWorkload(ctx, id)
if err != nil {
ch <- makeCopyMessage(id, "", "", logger.Err(ctx, err), nil)
return
}

for _, path := range paths {
resp, name, err := workload.Engine.VirtualizationCopyFrom(ctx, workload.ID, path)
ch <- makeCopyMessage(id, name, path, err, resp)
}
}
}(id, paths))
Expand Down
5 changes: 2 additions & 3 deletions cluster/calcium/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,16 @@ func TestCopy(t *testing.T) {
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
c.store = store
// failed by GetWorkload
store.On("GetWorkloads", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
store.On("GetWorkload", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
ch, err := c.Copy(ctx, opts)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
workload := &types.Workload{ID: "cid"}
workloads := []*types.Workload{workload}
engine := &enginemocks.API{}
workload.Engine = engine
store.On("GetWorkloads", mock.Anything, mock.Anything).Return(workloads, nil)
store.On("GetWorkload", mock.Anything, mock.Anything).Return(workload, nil)
// failed by VirtualizationCopyFrom
engine.On("VirtualizationCopyFrom", mock.Anything, mock.Anything, mock.Anything).Return(nil, "", types.ErrNilEngine).Twice()
ch, err = c.Copy(ctx, opts)
Expand Down

0 comments on commit 6647f33

Please sign in to comment.