mirror of
https://github.com/ultravioletrs/cocos.git
synced 2026-08-07 07:14:50 +00:00
ee7159a406
* Refactor RunRequest to use structured Computation The protobuf and associated service implementations for the RunRequest message were refactored to replace the raw Computation byte slice with a structured ComputationReq object. This allows clearer and more type-safe manipulation of computation requests. The grpc, http, and agent service layers were updated to build and parse ComputationReq accordingly. The ComputationReq structure includes details like IDs, names, time stamps, and metadata, forming a well-defined contract for computation tasks. This change aligns with efforts to standardize request formats and improve clarity in inter-service communication. It impacts all systems interfacing with the RunRequest service and thus requires coordinated updates to the entire stack. Signed-off-by: SammyOina <sammyoina@gmail.com> * Initialize metadata maps and handle nil values Improved the robustness of metadata handling in gRPC endpoints and SDK by initializing metadata maps and explicitly checking for nil values before converting them. This ensures that both the agent's gRPC endpoint and the SDK properly handle cases where metadata fields may be uninitialized or contain nil values, preventing potential null pointer exceptions. Signed-off-by: SammyOina <sammyoina@gmail.com> * Refactor computation request handling Refactored the endpoint to construct Computation object from gRPC request, incorporating structpb for metadata handling and timestamppb for StartTime and EndTime fields. The management service and API requests are also updated to align with these changes, improving type safety and ensuring data is correctly marshalled when making service calls. Resolves data marshalling issues for computation requests. Signed-off-by: SammyOina <sammyoina@gmail.com> * use singular Signed-off-by: SammyOina <sammyoina@gmail.com> * remove unuse fields Signed-off-by: SammyOina <sammyoina@gmail.com> * remove unused fields Signed-off-by: SammyOina <sammyoina@gmail.com> --------- Signed-off-by: SammyOina <sammyoina@gmail.com>
97 lines
2.7 KiB
Go
97 lines
2.7 KiB
Go
// Copyright (c) Ultraviolet
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
package manager
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/cenkalti/backoff/v4"
|
|
"github.com/ultravioletrs/cocos/agent"
|
|
"github.com/ultravioletrs/cocos/manager/qemu"
|
|
"github.com/ultravioletrs/cocos/pkg/clients/grpc"
|
|
agentgrpc "github.com/ultravioletrs/cocos/pkg/clients/grpc/agent"
|
|
)
|
|
|
|
var (
|
|
// ErrMalformedEntity indicates malformed entity specification (e.g.
|
|
// invalid username or password).
|
|
ErrMalformedEntity = errors.New("malformed entity specification")
|
|
|
|
// ErrUnauthorizedAccess indicates missing or invalid credentials provided
|
|
// when accessing a protected resource.
|
|
ErrUnauthorizedAccess = errors.New("missing or invalid credentials provided")
|
|
|
|
// ErrNotFound indicates a non-existent entity request.
|
|
ErrNotFound = errors.New("entity not found")
|
|
)
|
|
|
|
// Service specifies an API that must be fulfilled by the domain service
|
|
// implementation, and all of its decorators (e.g. logging & metrics).
|
|
type Service interface {
|
|
Run(ctx context.Context, computation *Computation, agentConfig grpc.Config) (string, error)
|
|
}
|
|
|
|
type managerService struct {
|
|
qemuCfg qemu.Config
|
|
}
|
|
|
|
var _ Service = (*managerService)(nil)
|
|
|
|
// New instantiates the manager service implementation.
|
|
func New(qemuCfg qemu.Config) Service {
|
|
return &managerService{
|
|
qemuCfg: qemuCfg,
|
|
}
|
|
}
|
|
|
|
func (ms *managerService) Run(ctx context.Context, computation *Computation, agentConfig grpc.Config) (string, error) {
|
|
_, err := qemu.CreateVM(ctx, ms.qemuCfg)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// different VM guests can't forward ports to the same ports on the same host
|
|
defer func() {
|
|
ms.qemuCfg.HostFwd1++
|
|
ms.qemuCfg.NetDevConfig.HostFwd2++
|
|
ms.qemuCfg.NetDevConfig.HostFwd3++
|
|
}()
|
|
|
|
runReq := &agent.ComputationReq{
|
|
Id: computation.Id,
|
|
Name: computation.Name,
|
|
Description: computation.Description,
|
|
ResultConsumers: computation.ResultConsumers,
|
|
Timeout: computation.Timeout,
|
|
}
|
|
|
|
for _, algo := range computation.Algorithms {
|
|
runReq.Algorithms = append(runReq.Algorithms, &agent.AlgorithmReq{Id: algo.Id, Provider: algo.Provider})
|
|
}
|
|
for _, data := range computation.Datasets {
|
|
runReq.Datasets = append(runReq.Datasets, &agent.DatasetReq{Id: data.Id, Provider: data.Provider})
|
|
}
|
|
|
|
var res *agent.RunResponse
|
|
|
|
agentConfig.URL = fmt.Sprintf("localhost:%d", ms.qemuCfg.HostFwd3)
|
|
|
|
err = backoff.Retry(func() error {
|
|
agentGRPCClient, agentClient, err := agentgrpc.NewAgentClient(agentConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer agentGRPCClient.Close()
|
|
res, err = agentClient.Run(ctx, &agent.RunRequest{
|
|
Computation: runReq,
|
|
})
|
|
return err
|
|
}, backoff.NewExponentialBackOff())
|
|
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return res.Computation, nil
|
|
}
|