-
Notifications
You must be signed in to change notification settings - Fork 41
support send large file to workload #532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
873e6b8
Add send large file
hongjijun233 ef9967f
Finished the code and can compile successfully, but it hasn't been te…
hongjijun233 afceac3
now it can send large file chunk by chunk, but still need to make som…
hongjijun233 f373ad4
add file size in metadata
hongjijun233 27cd492
now it can send servel large file, but sometimes they faild and i don…
hongjijun233 9c1d310
modify return parameters for `SendLargeFile`
hongjijun233 a7212f8
bug fix
hongjijun233 123ac04
modify the definition of grpc
hongjijun233 bcd421a
delete some annotation and modify the code
hongjijun233 ffddadf
fix dead lock, modify chunk size
hongjijun233 a2a04b0
bug fix
hongjijun233 70fc740
add some annotation
hongjijun233 05d309a
rollback makefile
hongjijun233 9094e0a
modify the function 'newWorkloadExecutor' and let it just send one file
hongjijun233 20e8d54
modify the RPC method 'send', let it call 'SendLargeFile'
hongjijun233 3be2694
modify the struct of 'SendLargeFileOptions'
hongjijun233 8b886ee
clean up the code
hongjijun233 3ac63ab
clean up the code
hongjijun233 3f17845
regenerate mock
hongjijun233 ffa34d6
delete debug code
hongjijun233 e6a60ba
add test for SendLarge in cluster
hongjijun233 ed1e040
replace copychunk to copy in send
hongjijun233 345bd6d
Merge remote-tracking branch 'upstream/master' into add-send-large-file
hongjijun233 9e32ac4
let chunkSize as a const
hongjijun233 924a9a8
bug fix: need to add waitgroup in the very beginning
hongjijun233 865e710
lint
hongjijun233 4361481
use defer
hongjijun233 4947e90
resolve conflict
hongjijun233 be656de
fix bug that cannot return err in goroutine
hongjijun233 583ad87
use different err parameter in function
hongjijun233 2f65017
adjust the code for CR
hongjijun233 d8da978
Merge remote-tracking branch 'upstream/master' into add-send-large-file
hongjijun233 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| package calcium | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "sync" | ||
|
|
||
| "github.com/pkg/errors" | ||
| "github.com/projecteru2/core/log" | ||
| "github.com/projecteru2/core/types" | ||
| "github.com/projecteru2/core/utils" | ||
| ) | ||
|
|
||
| // SendLargeFile send large files by stream to workload | ||
| func (c *Calcium) SendLargeFile(ctx context.Context, inputChan chan *types.SendLargeFileOptions) chan *types.SendMessage { | ||
| resp := make(chan *types.SendMessage) | ||
| wg := &sync.WaitGroup{} | ||
| utils.SentryGo(func() { | ||
| defer close(resp) | ||
| senders := make(map[string]*workloadSender) | ||
| // for each file | ||
| for data := range inputChan { | ||
| for _, id := range data.Ids { | ||
| if _, ok := senders[id]; !ok { | ||
| log.Debugf(ctx, "[SendLargeFile] create sender for %s", id) | ||
| // for each container, let's create a new sender to send identical file chunk, each chunk will include the metadata of this file | ||
| // we need to add `waitGroup out of the `newWorkloadSender` because we need to avoid `wg.Wait()` be executing before `wg.Add()`, | ||
| // which will cause the goroutine in `c.newWorkloadSender` to be killed. | ||
| wg.Add(1) | ||
| sender := c.newWorkloadSender(ctx, id, resp, wg) | ||
| senders[id] = sender | ||
| } | ||
| senders[id].send(data) | ||
| } | ||
| } | ||
| for _, sender := range senders { | ||
| sender.close() | ||
| } | ||
| wg.Wait() | ||
| }) | ||
| return resp | ||
| } | ||
|
|
||
| type workloadSender struct { | ||
| calcium *Calcium | ||
| id string | ||
| wg *sync.WaitGroup | ||
| buffer chan *types.SendLargeFileOptions | ||
| resp chan *types.SendMessage | ||
| } | ||
|
|
||
| func (c *Calcium) newWorkloadSender(ctx context.Context, ID string, resp chan *types.SendMessage, wg *sync.WaitGroup) *workloadSender { | ||
| sender := &workloadSender{ | ||
| calcium: c, | ||
| id: ID, | ||
| wg: wg, | ||
| buffer: make(chan *types.SendLargeFileOptions, 10), | ||
| resp: resp, | ||
| } | ||
| utils.SentryGo(func() { | ||
| var writer *io.PipeWriter | ||
| curFile := "" | ||
| for data := range sender.buffer { | ||
| if curFile != "" && curFile != data.Dst { | ||
| log.Warnf(ctx, "[newWorkloadExecutor] receive different files %s, %s", curFile, data.Dst) | ||
| break | ||
| } | ||
| // ready to send | ||
| if curFile == "" { | ||
| log.Debugf(ctx, "[newWorkloadExecutor]Receive new file %s to %s", curFile, sender.id) | ||
| curFile = data.Dst | ||
| pr, pw := io.Pipe() | ||
| writer = pw | ||
| utils.SentryGo(func(ID, name string, size int64, content io.Reader, uid, gid int, mode int64) func() { | ||
| return func() { | ||
| defer wg.Done() | ||
| if err := sender.calcium.withWorkloadLocked(ctx, ID, func(ctx context.Context, workload *types.Workload) error { | ||
| err := errors.WithStack(workload.Engine.VirtualizationCopyChunkTo(ctx, ID, name, size, content, uid, gid, mode)) | ||
| resp <- &types.SendMessage{ID: ID, Path: name, Error: err} | ||
| return nil | ||
| }); err != nil { | ||
| resp <- &types.SendMessage{ID: ID, Error: err} | ||
| } | ||
| } | ||
| }(ID, curFile, data.Size, pr, data.UID, data.GID, data.Mode)) | ||
| } | ||
| n, err := writer.Write(data.Chunk) | ||
| if err != nil || n != len(data.Chunk) { | ||
| log.Errorf(ctx, err, "[newWorkloadExecutor] send file to engine err, file = %s", curFile) | ||
| break | ||
| } | ||
| } | ||
| writer.Close() | ||
| }) | ||
| return sender | ||
| } | ||
|
|
||
| func (s *workloadSender) send(chunk *types.SendLargeFileOptions) { | ||
| s.buffer <- chunk | ||
| } | ||
|
|
||
| func (s *workloadSender) close() { | ||
| close(s.buffer) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package calcium | ||
|
|
||
| import ( | ||
| "context" | ||
| "io/ioutil" | ||
| "os" | ||
| "testing" | ||
|
|
||
| enginemocks "github.com/projecteru2/core/engine/mocks" | ||
| lockmocks "github.com/projecteru2/core/lock/mocks" | ||
| storemocks "github.com/projecteru2/core/store/mocks" | ||
| "github.com/projecteru2/core/types" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/mock" | ||
| ) | ||
|
|
||
| func TestSendLarge(t *testing.T) { | ||
| c := NewTestCluster() | ||
| ctx := context.Background() | ||
|
|
||
| tmpfile, err := os.CreateTemp("", "example") | ||
| assert.NoError(t, err) | ||
| defer os.RemoveAll(tmpfile.Name()) | ||
| defer tmpfile.Close() | ||
| opts := &types.SendLargeFileOptions{ | ||
| Ids: []string{"cid"}, | ||
| Size: 1, | ||
| Dst: "/tmp/1", | ||
| Chunk: []byte{}, | ||
| } | ||
| optsChan := make(chan *types.SendLargeFileOptions) | ||
| store := &storemocks.Store{} | ||
| c.store = store | ||
| lock := &lockmocks.DistributedLock{} | ||
| lock.On("Lock", mock.Anything).Return(context.TODO(), nil) | ||
| lock.On("Unlock", mock.Anything).Return(nil) | ||
| store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil) | ||
| store.On("GetWorkloads", mock.Anything, mock.Anything).Return(nil, types.ErrMockError).Once() | ||
| ch := c.SendLargeFile(ctx, optsChan) | ||
| go func() { | ||
| optsChan <- opts | ||
| close(optsChan) | ||
| }() | ||
| for r := range ch { | ||
| assert.Error(t, r.Error) | ||
| } | ||
| engine := &enginemocks.API{} | ||
| store.On("GetWorkloads", mock.Anything, mock.Anything).Return( | ||
| []*types.Workload{{ID: "cid", Engine: engine}}, nil, | ||
| ) | ||
| // failed by engine | ||
| content, _ := ioutil.ReadAll(tmpfile) | ||
| opts.Chunk = content | ||
| engine.On("VirtualizationCopyChunkTo", | ||
| mock.Anything, mock.Anything, mock.Anything, | ||
| mock.Anything, mock.Anything, mock.Anything, | ||
| mock.Anything, mock.Anything, | ||
| ).Return(types.ErrMockError).Once() | ||
| optsChan = make(chan *types.SendLargeFileOptions) | ||
| ch = c.SendLargeFile(ctx, optsChan) | ||
| go func() { | ||
| optsChan <- opts | ||
| close(optsChan) | ||
| }() | ||
| for r := range ch { | ||
| t.Log(r.Error) | ||
| assert.Error(t, r.Error) | ||
| } | ||
| // success | ||
| engine.On("VirtualizationCopyChunkTo", | ||
| mock.Anything, mock.Anything, mock.Anything, | ||
| mock.Anything, mock.Anything, mock.Anything, | ||
| mock.Anything, mock.Anything, | ||
| ).Return(nil) | ||
| optsChan = make(chan *types.SendLargeFileOptions) | ||
| ch = c.SendLargeFile(ctx, optsChan) | ||
| go func() { | ||
| optsChan <- opts | ||
| close(optsChan) | ||
| }() | ||
| for r := range ch { | ||
| assert.Equal(t, r.ID, "cid") | ||
| assert.Equal(t, r.Path, "/tmp/1") | ||
| assert.NoError(t, r.Error) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.