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 3 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
74 changes: 74 additions & 0 deletions cluster/calcium/sendlarge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package calcium

import (
"context"
"github.com/pkg/errors"
"github.com/projecteru2/core/engine"
"github.com/projecteru2/core/log"
"github.com/projecteru2/core/types"
"github.com/projecteru2/core/utils"
"github.com/sirupsen/logrus"
"io"
"sync"
)

// SendLargeFile send large files by stream to workload
func (c *Calcium) SendLargeFile(ctx context.Context, opts chan *types.SendLargeFileOptions, resp chan *types.SendMessage) error {
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
logger := log.WithField("Calcium", "SendLargeFile").WithField("opts", opts)
utils.SentryGo(func() {
defer close(resp)
wg := &sync.WaitGroup{}

writerMap := make(map[string]map[string]*io.PipeWriter)
for data := range opts {
logrus.Debugf("[SendLargeFile] receive some msg, len: %d", len(data.Data))
if err := data.Validate(); err != nil {
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
continue
}

dst := data.FileMetadataOptions.Dst
for _, id := range data.FileMetadataOptions.Ids {
if _, ok := writerMap[id]; !ok {
logrus.Debugf("[SendLargeFile] gen writerMap[%s]", id)
writerMap[id] = make(map[string]*io.PipeWriter)
}
if _, ok := writerMap[id][dst]; !ok {
wg.Add(1)
pr, pw := io.Pipe()
writerMap[id][dst] = pw
utils.SentryGo(func(id string) func() {
return func() {
defer wg.Done()
logrus.Debugf("[SendLargeFile] gen goroutine for id:%s", id)
if err := c.withWorkloadLocked(ctx, id, func(ctx context.Context, workload *types.Workload) error {
err := c.doSendChunkFileToWorkload(ctx, workload.Engine, workload.ID, dst, pr, data.Uid, data.Gid, data.Mode)
resp <- &types.SendMessage{ID: id, Path: dst, Error: logger.Err(ctx, err)}
return nil
}); err != nil {
resp <- &types.SendMessage{ID: id, Error: logger.Err(ctx, err)}
}
}
}(id))
}
logrus.Debugf("[SendLargeFile] send somethine id: %s; dst: %s", id, dst)
writerMap[id][dst].Write(data.Data)
}
}
logrus.Debugf("[SendLargeFile] send to the end")
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
for _, m := range writerMap {
for _, w := range m{
err := w.Close()
if err != nil {
logrus.Debugf("[SendLargeFile] close writer error: %v", err)
}
}
}
wg.Wait()
})
return nil
}

func (c *Calcium) doSendChunkFileToWorkload(ctx context.Context, engine engine.API, ID, name string, content io.Reader, uid, gid int, mode int64) error {
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
log.Infof(ctx, "[doSendChunkFileToWorkload] Send file to %s:%s", ID, name)
return errors.WithStack(engine.VirtualizationCopyChunkTo(ctx, ID, name, content, uid, gid, mode))
}
1 change: 1 addition & 0 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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, resp chan *types.SendMessage) error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实不用加新的 interface, 直接改旧的 Send 就可以了.

// 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
59 changes: 59 additions & 0 deletions engine/docker/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"math"
Expand Down Expand Up @@ -331,6 +332,64 @@ func (e *Engine) VirtualizationCopyTo(ctx context.Context, ID, target string, co
})
}

// VirtualizationCopyChunkTo copy chunk to virtualization
func (e *Engine) VirtualizationCopyChunkTo(ctx context.Context, ID, target string, content io.Reader, uid, gid int, mode int64) error {
// todo err 怎么抛出
pr, pw := io.Pipe()
//bf := new(bytes.Buffer)
tw := tar.NewWriter(pw)
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
defer tw.Close()
utils.SentryGo(func(writer io.Writer, reader io.Reader) func() {
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
return func() {
hdr := &tar.Header{
Name: filepath.Base(target),
// todo client 传过来
Size: 26357760,
Mode: mode,
Uid: uid,
Gid: gid,
}
logrus.Debugf("[VirtualizationCopyChunkTo] write header, target: %s, id: %s", target, ID)
if err := tw.WriteHeader(hdr); err != nil {
logrus.Debugf("[VirtualizationCopyChunkTo] write header err, err: %v", err)
return
}
logrus.Debugf("[VirtualizationCopyChunkTo] write header done, target: %s, id: %s", target, ID)
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
for {
data := make([]byte, 10 * 1024 * 1024)
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
logrus.Debugf("try to read.....")
n, err := reader.Read(data)
logrus.Debugf("[VirtualizationCopyChunkTo] read something, len: %d", n)
if err != nil {
if err == io.EOF {
logrus.Debugf("[VirtualizationCopyChunkTo] end!!, target: %s, id: %s", target, ID)
}
logrus.Debugf("[VirtualizationCopyChunkTo] read something error, len: %d", n)
err := writer.(*io.PipeWriter).Close()
if err != nil {
logrus.Debugf("[VirtualizationCopyChunkTo] close pipe writer, err: %v", err)
}
return
}
if n < len(data) {
data = data[:n]
}
_, err = tw.Write(data)
logrus.Debugf("[VirtualizationCopyChunkTo] write something, len: %d", len(data))
if err != nil {
logrus.Debugf("[VirtualizationCopyChunkTo] write something err, err: %v", err)
err := writer.(*io.PipeWriter).Close()
hongjijun233 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
logrus.Debugf("[VirtualizationCopyChunkTo] close pipe writer, err: %v", err)
}
return
}
}
}
}(pw, content))
return e.client.CopyToContainer(ctx, ID, filepath.Dir(target), pr, dockertypes.CopyToContainerOptions{AllowOverwriteDirWithFile: true, CopyUIDGID: false})
}

// VirtualizationStart start virtualization
func (e *Engine) VirtualizationStart(ctx context.Context, ID string) error {
return e.client.ContainerStart(ctx, ID, dockertypes.ContainerStartOptions{})
Expand Down
1 change: 1 addition & 0 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type API interface {
VirtualizationCreate(ctx context.Context, opts *enginetypes.VirtualizationCreateOptions) (*enginetypes.VirtualizationCreated, error)
VirtualizationResourceRemap(context.Context, *enginetypes.VirtualizationRemapOptions) (<-chan enginetypes.VirtualizationRemapMessage, error)
VirtualizationCopyTo(ctx context.Context, ID, target string, content []byte, uid, gid int, mode int64) error
VirtualizationCopyChunkTo(ctx context.Context, ID, target string, content io.Reader, uid, gid int, mode int64) error
VirtualizationStart(ctx context.Context, ID string) error
VirtualizationStop(ctx context.Context, ID string, gracefulTimeout time.Duration) error
VirtualizationRemove(ctx context.Context, ID string, volumes, force bool) error
Expand Down
5 changes: 5 additions & 0 deletions engine/fake/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (f *Engine) VirtualizationCopyTo(ctx context.Context, ID, target string, co
return types.ErrNilEngine
}

// VirtualizationCopyChunkTo .
func (f *Engine) VirtualizationCopyChunkTo(ctx context.Context, ID, target string, content io.Reader, uid, gid int, mode int64) error {
return types.ErrNilEngine
}

// VirtualizationStart .
func (f *Engine) VirtualizationStart(ctx context.Context, ID string) error {
return types.ErrNilEngine
Expand Down
14 changes: 14 additions & 0 deletions engine/mocks/API.go

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

1 change: 1 addition & 0 deletions engine/mocks/fakeengine/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func MakeClient(ctx context.Context, config coretypes.Config, nodename, endpoint
close(ch)
e.On("VirtualizationResourceRemap", mock.Anything, mock.Anything).Return((<-chan enginetypes.VirtualizationRemapMessage)(ch), nil)
e.On("VirtualizationCopyTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("VVirtualizationCopyChunkTo", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("VirtualizationStart", mock.Anything, mock.Anything).Return(nil)
e.On("VirtualizationStop", mock.Anything, mock.Anything, mock.Anything).Return(nil)
e.On("VirtualizationRemove", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
Expand Down
7 changes: 7 additions & 0 deletions engine/types/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,10 @@ type VirtualizationRemapMessage struct {
ID string
Error error
}

// SendMessage returns from engine
type SendMessage struct {
ID string
Path string
Error error
}
5 changes: 5 additions & 0 deletions engine/virt/virt.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ func (v *Virt) VirtualizationCopyTo(ctx context.Context, ID, dest string, conten
return v.client.CopyToGuest(ctx, ID, dest, bytes.NewReader(content), true, true)
}

// VirtualizationCopyChunkTo copies one.
func (v *Virt) VirtualizationCopyChunkTo(ctx context.Context, ID, dest string, content io.Reader, uid, gid int, mode int64) error {
return v.client.CopyToGuest(ctx, ID, dest, content, true, true)
}

// VirtualizationStart boots a guest.
func (v *Virt) VirtualizationStart(ctx context.Context, ID string) (err error) {
_, err = v.client.StartGuest(ctx, ID)
Expand Down
2 changes: 2 additions & 0 deletions rpc/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const (
Copy codes.Code = 1061
// Send .
Send codes.Code = 1062
// SendLargeFile .
SendLargeFile codes.Code = 1063

// BuildImage .
BuildImage codes.Code = 1071
Expand Down
Loading