-
Notifications
You must be signed in to change notification settings - Fork 10
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
NOISSUE - Handle larger manifests exceeding the default grpc limit #161
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1cb1ef
Handle larger manifests exceeding the default grpc limit
smithjilks 38598bd
Update manager tests
smithjilks 42275dd
Update manager tests
smithjilks c4576e2
Update manager client.go
smithjilks a40e2ed
Update manager client.go
smithjilks f858f2e
Update manager client.go
smithjilks c44b3dc
Update manager grpc server.go
smithjilks c94d0bd
Update manager grpc server and client
smithjilks 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
This file contains 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 |
---|---|---|
|
@@ -3,15 +3,20 @@ | |
package grpc | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
|
||
"github.com/absmach/magistrala/pkg/errors" | ||
"github.com/ultravioletrs/cocos/manager" | ||
pkgmanager "github.com/ultravioletrs/cocos/pkg/manager" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
var errTerminationFromServer = errors.New("server requested client termination") | ||
var ( | ||
errTerminationFromServer = errors.New("server requested client termination") | ||
errCorruptedManifest = errors.New("received manifest may be corrupted") | ||
) | ||
|
||
type ManagerClient struct { | ||
stream pkgmanager.ManagerService_ProcessClient | ||
|
@@ -32,24 +37,40 @@ func (client ManagerClient) Process(ctx context.Context, cancel context.CancelFu | |
eg, ctx := errgroup.WithContext(ctx) | ||
|
||
eg.Go(func() error { | ||
var runReqBuffer bytes.Buffer | ||
for { | ||
req, err := client.stream.Recv() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
switch mes := req.Message.(type) { | ||
case *pkgmanager.ServerStreamMessage_RunReq: | ||
port, err := client.svc.Run(ctx, mes.RunReq) | ||
if err != nil { | ||
return err | ||
} | ||
runRes := &pkgmanager.ClientStreamMessage_RunRes{RunRes: &pkgmanager.RunResponse{AgentPort: port, ComputationId: mes.RunReq.Id}} | ||
if err := client.stream.Send(&pkgmanager.ClientStreamMessage{Message: runRes}); err != nil { | ||
return err | ||
case *pkgmanager.ServerStreamMessage_RunReqChunks: | ||
if len(mes.RunReqChunks.Data) == 0 { | ||
var runReq pkgmanager.ComputationRunReq | ||
if err = proto.Unmarshal(runReqBuffer.Bytes(), &runReq); err != nil { | ||
return errors.Wrap(err, errCorruptedManifest) | ||
} | ||
port, err := client.svc.Run(ctx, &runReq) | ||
if err != nil { | ||
return err | ||
} | ||
runRes := &pkgmanager.ClientStreamMessage_RunRes{ | ||
RunRes: &pkgmanager.RunResponse{ | ||
AgentPort: port, | ||
ComputationId: runReq.Id, | ||
}, | ||
} | ||
if err := client.stream.Send(&pkgmanager.ClientStreamMessage{Message: runRes}); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
runReqBuffer.Write(mes.RunReqChunks.Data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle error |
||
|
||
case *pkgmanager.ServerStreamMessage_TerminateReq: | ||
cancel() | ||
return errors.Join(errTerminationFromServer, errors.New(mes.TerminateReq.Message)) | ||
return errors.Wrap(errTerminationFromServer, errors.New(mes.TerminateReq.Message)) | ||
case *pkgmanager.ServerStreamMessage_StopComputation: | ||
if err := client.svc.Stop(ctx, mes.StopComputation.ComputationId); err != nil { | ||
return err | ||
|
This file contains 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 |
---|---|---|
|
@@ -3,13 +3,23 @@ | |
package grpc | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
|
||
"github.com/ultravioletrs/cocos/pkg/manager" | ||
"golang.org/x/sync/errgroup" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/peer" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
var ( | ||
_ manager.ManagerServiceServer = (*grpcServer)(nil) | ||
ErrUnexpectedMsg = errors.New("unknown message type") | ||
) | ||
|
||
var _ manager.ManagerServiceServer = (*grpcServer)(nil) | ||
const bufferSize = 1024 * 1024 // 1 MB | ||
|
||
type grpcServer struct { | ||
manager.UnimplementedManagerServiceServer | ||
|
@@ -54,8 +64,45 @@ func (s *grpcServer) Process(stream manager.ManagerService_ProcessServer) error | |
case <-ctx.Done(): | ||
return nil | ||
case req := <-runReqChan: | ||
if err := stream.Send(req); err != nil { | ||
return err | ||
switch msg := req.Message.(type) { | ||
case *manager.ServerStreamMessage_RunReq: | ||
data, err := proto.Marshal(msg.RunReq) | ||
if err != nil { | ||
return err | ||
} | ||
dataBuffer := bytes.NewBuffer(data) | ||
buf := make([]byte, bufferSize) | ||
for { | ||
n, err := dataBuffer.Read(buf) | ||
chunk := &manager.ServerStreamMessage{ | ||
Message: &manager.ServerStreamMessage_RunReqChunks{ | ||
RunReqChunks: &manager.RunReqChunks{ | ||
Data: buf[:n], | ||
}, | ||
}, | ||
} | ||
|
||
if err := stream.Send(chunk); err != nil { | ||
return err | ||
} | ||
|
||
if err == io.EOF { | ||
break | ||
} | ||
} | ||
|
||
case *manager.ServerStreamMessage_TerminateReq: | ||
terminate := &manager.ServerStreamMessage{ | ||
Message: &manager.ServerStreamMessage_TerminateReq{ | ||
TerminateReq: msg.TerminateReq, | ||
}, | ||
} | ||
if err := stream.Send(terminate); err != nil { | ||
return err | ||
} | ||
|
||
default: | ||
return ErrUnexpectedMsg | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle all message types as in main |
||
} | ||
} | ||
|
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is a goroutine on client and should not return here