69888621cd
v9.1 — Distributed Computing (超越 Parasolid): - cluster_engine: ClusterManager, TaskScheduler(DAG+Kahn), 4 load-balance strategies - distributed_boolean, distributed_marching_cubes, distributed_ray_tracing - grpc_service: BrepOps/MeshOps/SdfOps RPC, streaming, TLS, connection pool - ~750 lines v9.2 — Cloud-Native + KBE + WASM + Digital Twin (34/34 tests passing): - cloud_native: CloudSession, OperationalTransform, DeltaSync, Serverless, ObjectStorage - knowledge_engine: CheckMate(13 rules), RuleEngine, DesignTable, GA+Adam optimizer - vde_wasm: WasmBridge, WebWorkerPool, SharedArrayBuffer, IndexedDB - dt_engine: DigitalTwin, MQTT/OPC-UA, RealTimeSync, PredictiveMaintenance(RUL) - 3950 lines, 34 tests all passing Pending: AI/ML integration (retrying) 18 files, ~4700 lines
431 lines
14 KiB
C++
431 lines
14 KiB
C++
#pragma once
|
||
/**
|
||
* @file grpc_service.h
|
||
* @brief gRPC 分布式服务接口 — VdeService (BrepOps, MeshOps, SdfOps)
|
||
*
|
||
* 定义基于 gRPC 的远程过程调用服务,支持:
|
||
* - BrepOps: 远程 B-Rep 操作(布尔、特征识别、修复)
|
||
* - MeshOps: 远程网格操作(简化、平滑、质量分析)
|
||
* - SdfOps: 远程 SDF 操作(求值、梯度、网格化)
|
||
* - 流式传输: 大模型分块传输(流式请求/响应)
|
||
* - 健康检查 + 自动重连
|
||
* - TLS 加密通信
|
||
*
|
||
* ## 服务架构
|
||
*
|
||
* ```
|
||
* VdeService (gRPC)
|
||
* ├─ BrepOps
|
||
* │ ├─ Boolean (请求/响应)
|
||
* │ ├─ Validate (请求/响应)
|
||
* │ └─ Heal (请求/响应)
|
||
* ├─ MeshOps
|
||
* │ ├─ Simplify (请求/响应)
|
||
* │ ├─ Smooth (请求/响应)
|
||
* │ ├─ MarchingCubes (流式响应: 大网格)
|
||
* │ └─ QualityReport (请求/响应)
|
||
* └─ SdfOps
|
||
* ├─ Evaluate (请求/响应)
|
||
* ├─ Gradient (请求/响应)
|
||
* └─ ToMesh (流式响应: 大网格)
|
||
* ```
|
||
*
|
||
* @ingroup distributed
|
||
*/
|
||
|
||
#include "vde/distributed/cluster_engine.h"
|
||
|
||
#include <atomic>
|
||
#include <chrono>
|
||
#include <cstdint>
|
||
#include <functional>
|
||
#include <memory>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
namespace vde::distributed {
|
||
|
||
// ══════════════════════════════════════════════════════════════════
|
||
// gRPC 服务定义(接口层,可与真实 gRPC 生成代码互换)
|
||
// ══════════════════════════════════════════════════════════════════
|
||
|
||
// ── 请求/响应类型 ─────────────────────────────────────────────────
|
||
|
||
/** Boolean 操作请求 */
|
||
struct BrepBooleanRequest {
|
||
std::string brep_a; ///< 序列化的 BrepModel A
|
||
std::string brep_b; ///< 序列化的 BrepModel B
|
||
int op_type = 0; ///< 0=union, 1=intersection, 2=difference(A-B), 3=sym_diff
|
||
double tolerance = 1e-6; ///< 布尔运算容差
|
||
};
|
||
|
||
/** Boolean 操作响应 */
|
||
struct BrepBooleanResponse {
|
||
bool success = false;
|
||
std::string result_brep; ///< 序列化的 BrepModel 结果
|
||
std::string error_message;
|
||
int64_t elapsed_ms = 0;
|
||
};
|
||
|
||
/** B-Rep 验证请求 */
|
||
struct BrepValidateRequest {
|
||
std::string brep_data; ///< 序列化的 BrepModel
|
||
bool check_topology = true;
|
||
bool check_geometry = true;
|
||
double tolerance = 1e-6;
|
||
};
|
||
|
||
/** B-Rep 验证响应 */
|
||
struct BrepValidateResponse {
|
||
bool is_valid = false;
|
||
std::vector<std::string> issues;
|
||
int num_faces = 0;
|
||
int num_edges = 0;
|
||
int num_vertices = 0;
|
||
};
|
||
|
||
/** B-Rep 修复请求 */
|
||
struct BrepHealRequest {
|
||
std::string brep_data;
|
||
bool close_gaps = true;
|
||
bool fix_orientation = true;
|
||
double tolerance = 1e-5;
|
||
};
|
||
|
||
/** B-Rep 修复响应 */
|
||
struct BrepHealResponse {
|
||
bool success = false;
|
||
std::string healed_brep;
|
||
int issues_fixed = 0;
|
||
std::string error_message;
|
||
};
|
||
|
||
/** 网格简化请求 */
|
||
struct MeshSimplifyRequest {
|
||
std::string mesh_data; ///< 序列化的 HalfedgeMesh
|
||
int target_faces = 0; ///< 目标面数(0=自动)
|
||
double reduction_ratio = 0.5; ///< 减面比例
|
||
bool preserve_boundaries = true;
|
||
};
|
||
|
||
/** 网格简化响应 */
|
||
struct MeshSimplifyResponse {
|
||
bool success = false;
|
||
std::string simplified_mesh;
|
||
int original_faces = 0;
|
||
int result_faces = 0;
|
||
std::string error_message;
|
||
};
|
||
|
||
/** 网格平滑请求 */
|
||
struct MeshSmoothRequest {
|
||
std::string mesh_data;
|
||
int iterations = 10;
|
||
double lambda = 0.5;
|
||
bool preserve_volume = true;
|
||
};
|
||
|
||
/** 网格平滑响应 */
|
||
struct MeshSmoothResponse {
|
||
bool success = false;
|
||
std::string smoothed_mesh;
|
||
std::string error_message;
|
||
};
|
||
|
||
/** Marching Cubes 请求 */
|
||
struct MarchingCubesRequest {
|
||
int resolution = 64;
|
||
double iso_level = 0.0;
|
||
std::array<double, 3> bmin = {-1, -1, -1};
|
||
std::array<double, 3> bmax = {1, 1, 1};
|
||
int chunk_index = 0; ///< 分块索引(流式传输用)
|
||
int total_chunks = 1; ///< 总分块数
|
||
};
|
||
|
||
/** Marching Cubes 块响应(流式) */
|
||
struct MarchingCubesChunk {
|
||
int chunk_index = 0;
|
||
int total_chunks = 1;
|
||
bool is_last = false;
|
||
std::vector<uint8_t> vertex_data; ///< 顶点数据(序列化字节)
|
||
std::vector<uint8_t> index_data; ///< 索引数据(序列化字节)
|
||
int vertex_count = 0;
|
||
int triangle_count = 0;
|
||
};
|
||
|
||
/** SDF 求值请求 */
|
||
struct SdfEvaluateRequest {
|
||
std::string sdf_tree_data; ///< 序列化的 SDF 树
|
||
std::vector<double> points; ///< 待求值点 (x1,y1,z1, x2,y2,z2, ...)
|
||
};
|
||
|
||
/** SDF 求值响应 */
|
||
struct SdfEvaluateResponse {
|
||
std::vector<double> values; ///< 每个点的 SDF 值
|
||
bool success = false;
|
||
std::string error_message;
|
||
};
|
||
|
||
/** SDF 梯度请求 */
|
||
struct SdfGradientRequest {
|
||
std::string sdf_tree_data;
|
||
std::vector<double> points;
|
||
double epsilon = 1e-5; ///< 有限差分步长
|
||
};
|
||
|
||
/** SDF 梯度响应 */
|
||
struct SdfGradientResponse {
|
||
std::vector<double> gradients; ///< (gx1,gy1,gz1, gx2,gy2,gz2, ...)
|
||
bool success = false;
|
||
std::string error_message;
|
||
};
|
||
|
||
/** 健康检查请求 */
|
||
struct HealthCheckRequest {
|
||
std::string node_id;
|
||
int64_t timestamp_ms = 0;
|
||
};
|
||
|
||
/** 健康检查响应 */
|
||
struct HealthCheckResponse {
|
||
bool healthy = false;
|
||
std::string node_id;
|
||
int64_t server_time_ms = 0;
|
||
int active_connections = 0;
|
||
int64_t uptime_ms = 0;
|
||
double cpu_load = 0.0;
|
||
int64_t memory_used_mb = 0;
|
||
};
|
||
|
||
// ══════════════════════════════════════════════════════════════════
|
||
// VdeService — gRPC 服务接口
|
||
// ══════════════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief TLS 配置
|
||
*/
|
||
struct TlsConfig {
|
||
bool enabled = false; ///< 是否启用 TLS
|
||
std::string cert_chain_path; ///< 证书链文件路径 (PEM)
|
||
std::string private_key_path; ///< 私钥文件路径 (PEM)
|
||
std::string ca_cert_path; ///< CA 证书路径(双向 TLS)
|
||
bool verify_client = false; ///< 是否验证客户端证书
|
||
std::string ciphers; ///< 自定义密码套件
|
||
};
|
||
|
||
/**
|
||
* @brief gRPC 服务配置
|
||
*/
|
||
struct GrpcServiceConfig {
|
||
std::string host = "0.0.0.0";
|
||
int port = 50051;
|
||
int max_message_size_mb = 256; ///< 最大消息大小 (MB)
|
||
int keepalive_time_ms = 30000; ///< keepalive 间隔
|
||
int keepalive_timeout_ms = 10000; ///< keepalive 超时
|
||
bool enable_reflection = true; ///< 启用 gRPC 反射
|
||
TlsConfig tls;
|
||
};
|
||
|
||
/**
|
||
* @brief gRPC 客户端配置
|
||
*/
|
||
struct GrpcClientConfig {
|
||
std::string target; ///< "host:port"
|
||
int connect_timeout_ms = 5000;
|
||
int max_reconnect_attempts = 5;
|
||
int reconnect_backoff_ms = 1000;
|
||
int max_message_size_mb = 256;
|
||
TlsConfig tls;
|
||
};
|
||
|
||
/**
|
||
* @brief VdeService — 分布式几何计算 gRPC 服务
|
||
*
|
||
* 提供 BrepOps、MeshOps、SdfOps 三大类远程过程调用。
|
||
* 支持大模型流式分块传输和健康检查/自动重连。
|
||
*
|
||
* 使用模式:
|
||
* @code{.cpp}
|
||
* // 服务端
|
||
* VdeService server(cfg);
|
||
* server.start();
|
||
*
|
||
* // 客户端
|
||
* VdeServiceClient client({"localhost:50051"});
|
||
* auto resp = client.brep_boolean(req);
|
||
* @endcode
|
||
*/
|
||
class VdeService {
|
||
public:
|
||
explicit VdeService(const GrpcServiceConfig& cfg = GrpcServiceConfig{});
|
||
~VdeService();
|
||
|
||
// ── 生命周期 ──
|
||
/** 启动 gRPC 服务(阻塞当前线程) */
|
||
bool start();
|
||
|
||
/** 在后台线程启动 */
|
||
bool start_async();
|
||
|
||
/** 优雅关闭 */
|
||
void shutdown();
|
||
|
||
/** 服务是否正在运行 */
|
||
[[nodiscard]] bool is_running() const noexcept;
|
||
|
||
// ── BrepOps 处理器注册 ──
|
||
using BrepBooleanHandler = std::function<BrepBooleanResponse(const BrepBooleanRequest&)>;
|
||
using BrepValidateHandler = std::function<BrepValidateResponse(const BrepValidateRequest&)>;
|
||
using BrepHealHandler = std::function<BrepHealResponse(const BrepHealRequest&)>;
|
||
|
||
void set_brep_boolean_handler(BrepBooleanHandler h);
|
||
void set_brep_validate_handler(BrepValidateHandler h);
|
||
void set_brep_heal_handler(BrepHealHandler h);
|
||
|
||
// ── MeshOps 处理器注册 ──
|
||
using MeshSimplifyHandler = std::function<MeshSimplifyResponse(const MeshSimplifyRequest&)>;
|
||
using MeshSmoothHandler = std::function<MeshSmoothResponse(const MeshSmoothRequest&)>;
|
||
using MeshMarchingCubesHandler = std::function<MarchingCubesChunk(const MarchingCubesRequest&)>;
|
||
|
||
void set_mesh_simplify_handler(MeshSimplifyHandler h);
|
||
void set_mesh_smooth_handler(MeshSmoothHandler h);
|
||
void set_mesh_marching_cubes_handler(MeshMarchingCubesHandler h);
|
||
|
||
// ── SdfOps 处理器注册 ──
|
||
using SdfEvaluateHandler = std::function<SdfEvaluateResponse(const SdfEvaluateRequest&)>;
|
||
using SdfGradientHandler = std::function<SdfGradientResponse(const SdfGradientRequest&)>;
|
||
|
||
void set_sdf_evaluate_handler(SdfEvaluateHandler h);
|
||
void set_sdf_gradient_handler(SdfGradientHandler h);
|
||
|
||
// ── 健康检查 ──
|
||
[[nodiscard]] HealthCheckResponse health_check() const;
|
||
|
||
// ── 服务端统计 ──
|
||
struct ServerStats {
|
||
int64_t total_requests = 0;
|
||
int64_t total_errors = 0;
|
||
int active_connections = 0;
|
||
int64_t uptime_ms = 0;
|
||
};
|
||
[[nodiscard]] ServerStats server_stats() const;
|
||
|
||
private:
|
||
struct Impl;
|
||
std::unique_ptr<Impl> impl_;
|
||
};
|
||
|
||
// ══════════════════════════════════════════════════════════════════
|
||
// VdeServiceClient — gRPC 客户端(自动重连)
|
||
// ══════════════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief gRPC 客户端 — 自动重连 + 健康检查
|
||
*
|
||
* 封装 gRPC stub,提供自动重连和超时重试机制。
|
||
*/
|
||
class VdeServiceClient {
|
||
public:
|
||
explicit VdeServiceClient(const GrpcClientConfig& cfg);
|
||
~VdeServiceClient();
|
||
|
||
// ── 连接管理 ──
|
||
/** 建立连接 */
|
||
bool connect();
|
||
|
||
/** 断开连接 */
|
||
void disconnect();
|
||
|
||
/** 是否已连接 */
|
||
[[nodiscard]] bool is_connected() const noexcept;
|
||
|
||
/** 健康检查 */
|
||
[[nodiscard]] HealthCheckResponse health_check();
|
||
|
||
// ── BrepOps ──
|
||
BrepBooleanResponse brep_boolean(const BrepBooleanRequest& req);
|
||
BrepValidateResponse brep_validate(const BrepValidateRequest& req);
|
||
BrepHealResponse brep_heal(const BrepHealRequest& req);
|
||
|
||
// ── MeshOps ──
|
||
MeshSimplifyResponse mesh_simplify(const MeshSimplifyRequest& req);
|
||
MeshSmoothResponse mesh_smooth(const MeshSmoothRequest& req);
|
||
std::vector<MarchingCubesChunk> mesh_marching_cubes(const MarchingCubesRequest& req);
|
||
|
||
// ── SdfOps ──
|
||
SdfEvaluateResponse sdf_evaluate(const SdfEvaluateRequest& req);
|
||
SdfGradientResponse sdf_gradient(const SdfGradientRequest& req);
|
||
|
||
// ── 流式传输:大模型分块 ──
|
||
/**
|
||
* @brief 分块上传大网格
|
||
*
|
||
* 将大网格切分为多个块,通过多次 RPC 调用发送。
|
||
* 客户端负责分块,服务端负责重组。
|
||
*
|
||
* @param mesh 待上传网格
|
||
* @param chunk_size_mb 每块大小 (MB)
|
||
* @return true 上传成功
|
||
*/
|
||
bool upload_large_mesh(const HalfedgeMesh& mesh, size_t chunk_size_mb = 8);
|
||
|
||
/**
|
||
* @brief 分块下载大网格
|
||
*
|
||
* 通过多次 MarchingCubesChunk 响应重组完整网格。
|
||
*/
|
||
HalfedgeMesh download_large_mesh(int resolution, double iso_level,
|
||
const AABB3D& bounds);
|
||
|
||
private:
|
||
struct Impl;
|
||
std::unique_ptr<Impl> impl_;
|
||
};
|
||
|
||
// ══════════════════════════════════════════════════════════════════
|
||
// 连接池 — 多节点连接管理
|
||
// ══════════════════════════════════════════════════════════════════
|
||
|
||
/**
|
||
* @brief gRPC 连接池 — 管理到多个节点的连接
|
||
*
|
||
* 维护一个连接池,按需创建和回收连接。
|
||
* 支持连接的惰性初始化和空闲回收。
|
||
*/
|
||
class GrpcConnectionPool {
|
||
public:
|
||
struct PoolConfig {
|
||
int max_connections = 32;
|
||
int idle_timeout_ms = 60000; ///< 空闲超时 (ms),超时后回收连接
|
||
int max_retries_per_node = 3;
|
||
};
|
||
|
||
explicit GrpcConnectionPool(const PoolConfig& cfg = PoolConfig{});
|
||
~GrpcConnectionPool();
|
||
|
||
/**
|
||
* @brief 获取到指定节点的客户端(若不存在则创建)
|
||
* @param host 节点地址 "host:port"
|
||
* @return 连接成功返回客户端,失败返回 nullptr
|
||
*/
|
||
std::shared_ptr<VdeServiceClient> get_client(const std::string& host);
|
||
|
||
/** 释放指定节点的连接 */
|
||
void release_client(const std::string& host);
|
||
|
||
/** 回收所有空闲连接 */
|
||
void reap_idle();
|
||
|
||
/** 获取当前活跃连接数 */
|
||
[[nodiscard]] size_t active_count() const;
|
||
|
||
/** 关闭所有连接 */
|
||
void close_all();
|
||
|
||
private:
|
||
struct Impl;
|
||
std::unique_ptr<Impl> impl_;
|
||
};
|
||
|
||
} // namespace vde::distributed
|