Skip to content
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

support send large file to workload #532

Merged
merged 32 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
873e6b8
Add send large file
hongjijun233 Jan 7, 2022
ef9967f
Finished the code and can compile successfully, but it hasn't been te…
hongjijun233 Jan 16, 2022
afceac3
now it can send large file chunk by chunk, but still need to make som…
hongjijun233 Jan 18, 2022
f373ad4
add file size in metadata
hongjijun233 Jan 19, 2022
27cd492
now it can send servel large file, but sometimes they faild and i don…
hongjijun233 Feb 1, 2022
9c1d310
modify return parameters for `SendLargeFile`
hongjijun233 Feb 1, 2022
a7212f8
bug fix
hongjijun233 Feb 1, 2022
123ac04
modify the definition of grpc
hongjijun233 Feb 14, 2022
bcd421a
delete some annotation and modify the code
hongjijun233 Feb 14, 2022
ffddadf
fix dead lock, modify chunk size
hongjijun233 Feb 16, 2022
a2a04b0
bug fix
hongjijun233 Feb 17, 2022
70fc740
add some annotation
hongjijun233 Feb 18, 2022
05d309a
rollback makefile
hongjijun233 Feb 18, 2022
9094e0a
modify the function 'newWorkloadExecutor' and let it just send one file
hongjijun233 Mar 1, 2022
20e8d54
modify the RPC method 'send', let it call 'SendLargeFile'
hongjijun233 Mar 1, 2022
3be2694
modify the struct of 'SendLargeFileOptions'
hongjijun233 Mar 1, 2022
8b886ee
clean up the code
hongjijun233 Mar 8, 2022
3ac63ab
clean up the code
hongjijun233 Mar 8, 2022
3f17845
regenerate mock
hongjijun233 Jul 11, 2023
ffa34d6
delete debug code
hongjijun233 Jul 11, 2023
e6a60ba
add test for SendLarge in cluster
hongjijun233 Jul 11, 2023
ed1e040
replace copychunk to copy in send
hongjijun233 Jul 11, 2023
345bd6d
Merge remote-tracking branch 'upstream/master' into add-send-large-file
hongjijun233 Jul 11, 2023
9e32ac4
let chunkSize as a const
hongjijun233 Jul 12, 2023
924a9a8
bug fix: need to add waitgroup in the very beginning
hongjijun233 Jul 19, 2023
865e710
lint
hongjijun233 Jul 19, 2023
4361481
use defer
hongjijun233 Jul 27, 2023
4947e90
resolve conflict
hongjijun233 Aug 1, 2023
be656de
fix bug that cannot return err in goroutine
hongjijun233 Aug 17, 2023
583ad87
use different err parameter in function
hongjijun233 Aug 17, 2023
2f65017
adjust the code for CR
hongjijun233 Aug 29, 2023
d8da978
Merge remote-tracking branch 'upstream/master' into add-send-large-file
hongjijun233 Sep 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions 3rdmocks/ServerStream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cluster/calcium/replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func TestReplaceWorkload(t *testing.T) {

engine.On("VirtualizationCreate", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationCreated{ID: "new"}, nil)
engine.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
engine.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
engine.On("VirtualizationCopyChunkTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
engine.On("VirtualizationInspect", mock.Anything, mock.Anything).Return(&enginetypes.VirtualizationInfo{User: "test"}, nil)
store.On("AddWorkload", mock.Anything, mock.Anything, mock.Anything).Return(nil)
// failed by remove workload
Expand Down
3 changes: 2 additions & 1 deletion cluster/calcium/send.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package calcium

import (
"bytes"
"context"
"sync"

Expand Down Expand Up @@ -48,5 +49,5 @@ func (c *Calcium) Send(ctx context.Context, opts *types.SendOptions) (chan *type

func (c *Calcium) doSendFileToWorkload(ctx context.Context, engine engine.API, ID string, file types.LinuxFile) error {
log.WithFunc("calcium.doSendFileToWorkload").Infof(ctx, "Send file to %s:%s", ID, file.Filename)
return engine.VirtualizationCopyTo(ctx, ID, file.Filename, file.Clone().Content, file.UID, file.GID, file.Mode)
return engine.VirtualizationCopyChunkTo(ctx, ID, file.Filename, int64(len(file.Content)), bytes.NewReader(file.Clone().Content), file.UID, file.GID, file.Mode)
}
9 changes: 5 additions & 4 deletions cluster/calcium/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestSend(t *testing.T) {
c := NewTestCluster()
ctx := context.Background()

// 这部分是在测试参数合法性
// failed by validating
_, err := c.Send(ctx, &types.SendOptions{IDs: []string{}, Files: []types.LinuxFile{{Content: []byte("xxx")}}})
assert.Error(t, err)
Expand Down Expand Up @@ -57,21 +58,21 @@ func TestSend(t *testing.T) {
// failed by engine
content, _ := io.ReadAll(tmpfile)
opts.Files[0].Content = content
engine.On("VirtualizationCopyTo",
engine.On("VirtualizationCopyChunkTo",
mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything, mock.Anything,
mock.Anything,
mock.Anything, mock.Anything,
).Return(types.ErrMockError).Once()
ch, err = c.Send(ctx, opts)
assert.NoError(t, err)
for r := range ch {
assert.Error(t, r.Error)
}
// success
engine.On("VirtualizationCopyTo",
engine.On("VirtualizationCopyChunkTo",
mock.Anything, mock.Anything, mock.Anything,
mock.Anything, mock.Anything, mock.Anything,
mock.Anything,
mock.Anything, mock.Anything,
).Return(nil)
ch, err = c.Send(ctx, opts)
assert.NoError(t, err)
Expand Down
102 changes: 102 additions & 0 deletions cluster/calcium/sendlarge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
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
wg.Add(1)
CMGS marked this conversation as resolved.
Show resolved Hide resolved
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)
}
86 changes: 86 additions & 0 deletions cluster/calcium/sendlarge_test.go
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 := ioutil.TempFile("", "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)
}
}
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Cluster interface {
// file methods
Copy(ctx context.Context, opts *types.CopyOptions) (chan *types.CopyMessage, error)
Send(ctx context.Context, opts *types.SendOptions) (chan *types.SendMessage, error)
SendLargeFile(ctx context.Context, opts chan *types.SendLargeFileOptions) chan *types.SendMessage
// image methods
BuildImage(ctx context.Context, opts *types.BuildOptions) (chan *types.BuildImageMessage, error)
CacheImage(ctx context.Context, opts *types.ImageOptions) (chan *types.CacheImageMessage, error)
Expand Down
Loading
Loading