mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-08-07 07:14:50 +00:00
a5e6cae92c
* Add agent address to run responses The manager service's Run method now retrieves the agent address upon successful computation execution, providing more informative responses across gRPC, HTTP, and logging endpoints. This change improves service transparency by returning the agent's address to be used by client services, making the manager service's external communication more comprehensive. Updated the `RunResponse` structure in the corresponding protocol buffers definition and response handling in gRPC and HTTP APIs, ensuring that agent address information is serialized appropriately. It also necessitates a slight adjustment in the QEMU configuration to manage port forwarding rules more dynamically, simplifying the process as only agent-relevant ports are incremented with each new computation. This extra detail in responses aids in debugging and offers better integration capabilities for clients. Signed-off-by: SammyOina <sammyoina@gmail.com> * Allocate dynamic ports for VM guests and expand error handling Refactored service initialization to accept host IP and incorporated dynamic port allocation for VM guests, replacing the prior static increment method. Introduced a new error type 'ErrFailedToAllocatePort' to capture instances where the system is unable to find a free port. Integrated a third-party error package for improved error wrapping and context. These changes prevent port conflicts between VM guests and enhance error diagnostics for service operations. Resolves issue with static port allocation leading to conflicts. Signed-off-by: SammyOina <sammyoina@gmail.com> * Add HOST_IP to service configuration Extend service configuration to include the host machine's IP address, allowing instances to be aware of their deployment environment. This update passes the new HostIP field to the service constructor, ensuring the service can now operate with host-specific logic. Signed-off-by: SammyOina <sammyoina@gmail.com> * Populate AgentAddress in gRPC Responses Enhanced the gRPC encode/decode functions to properly populate the 'AgentAddress' field in 'RunResponse' objects. This ensures that consumers of the gRPC interface receive complete response data, which previously omitted the important 'AgentAddress' information. The change impacts both server-side response encoding and client-side response decoding, aligning the implementation with the expected interface contract. Signed-off-by: SammyOina <sammyoina@gmail.com> * fix ci Signed-off-by: SammyOina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com>
87 lines
2.1 KiB
Go
87 lines
2.1 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package grpc
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/endpoint"
|
|
kitgrpc "github.com/go-kit/kit/transport/grpc"
|
|
"github.com/ultravioletrs/cocos/manager"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
const svcName = "manager.ManagerService"
|
|
|
|
type grpcClient struct {
|
|
run endpoint.Endpoint
|
|
timeout time.Duration
|
|
}
|
|
|
|
// NewClient returns new gRPC client instance.
|
|
func NewClient(conn *grpc.ClientConn, timeout time.Duration) manager.ManagerServiceClient {
|
|
return &grpcClient{
|
|
run: kitgrpc.NewClient(
|
|
conn,
|
|
svcName,
|
|
"Run",
|
|
encodeRunRequest,
|
|
decodeRunResponse,
|
|
manager.RunResponse{},
|
|
).Endpoint(),
|
|
timeout: timeout,
|
|
}
|
|
}
|
|
|
|
// encodeRunRequest is a transport/grpc.EncodeRequestFunc that
|
|
// converts a user-domain runReq to a gRPC request.
|
|
func encodeRunRequest(_ context.Context, request interface{}) (interface{}, error) {
|
|
req, ok := request.(runReq)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid request type: %T", request)
|
|
}
|
|
return &manager.RunRequest{
|
|
Computation: req.Computation,
|
|
CaCerts: req.CACerts,
|
|
ClientTls: req.ClientTLS,
|
|
Timeout: req.Timeout.String(),
|
|
}, nil
|
|
}
|
|
|
|
// decodeRunResponse is a transport/grpc.DecodeResponseFunc that
|
|
// converts a gRPC RunResponse to a user-domain response.
|
|
func decodeRunResponse(_ context.Context, grpcResponse interface{}) (interface{}, error) {
|
|
res, ok := grpcResponse.(*manager.RunResponse)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid response type: %T", grpcResponse)
|
|
}
|
|
return runRes{AgentAddress: res.AgentAddress}, nil
|
|
}
|
|
|
|
func (client grpcClient) Run(ctx context.Context, req *manager.RunRequest, _ ...grpc.CallOption) (*manager.RunResponse, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, client.timeout)
|
|
defer cancel()
|
|
|
|
dur, err := time.ParseDuration(req.GetTimeout())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
runReq := runReq{
|
|
Computation: req.GetComputation(),
|
|
ClientTLS: req.GetClientTls(),
|
|
CACerts: req.GetCaCerts(),
|
|
Timeout: dur,
|
|
}
|
|
|
|
res, err := client.run(ctx, runReq)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
runRes := res.(runRes)
|
|
return &manager.RunResponse{AgentAddress: runRes.AgentAddress}, nil
|
|
}
|