ci / gate (push) Waiting to run
- 项目与文档标题:nax-rs → ssw-mpc(尚数网-多方安全计算),二进制 nax → mpc - 18 个 crate 及目录 nax-* → mpc-*,Rust 引用 nax_* → mpc_* - 环境变量、C 符号、构建/部署脚本:NAX_* → MPC_*、libnax_ray.so → libmpc_ray.so - 保留协议域分隔常量与 refs/nax 参考实现路径,维持 golden 向量与历史数据兼容 - 新增 [profile.dev] incremental=false
260 lines
8.4 KiB
C++
260 lines
8.4 KiB
C++
// mpc_ray_shim.cc —— 把 Ray 的 C++ core(core_worker)暴露成 C ABI,给 mpc 的 Rust 侧用。
|
||
//
|
||
// 来源:ray-2.59.0/cpp/src/ray/util/process_helper.cc::ProcessHelper::RayStart
|
||
// 我们不做 bazel 构建,直接链接 wheel 里预编译的 _raylet.so(它自包含,无 libpython 依赖)。
|
||
//
|
||
// 数据交换只用「key=value\n」文本,避免跨 C++/Rust 边界传结构体。
|
||
|
||
#include <cstdio>
|
||
#include <cstring>
|
||
#include <optional>
|
||
#include <string>
|
||
#include <unordered_map>
|
||
#include <utility>
|
||
#include <vector>
|
||
|
||
#include "ray/common/id.h"
|
||
#include "ray/common/status.h"
|
||
#include "ray/core_worker/core_worker.h"
|
||
#include "ray/core_worker/core_worker_process.h"
|
||
#include "ray/gcs_rpc_client/gcs_client.h"
|
||
#include "ray/gcs_rpc_client/global_state_accessor.h"
|
||
|
||
using namespace ray; // NOLINT
|
||
using namespace ray::core; // NOLINT
|
||
|
||
namespace {
|
||
|
||
std::string g_err;
|
||
bool g_inited = false;
|
||
|
||
std::string HexOf(const std::string &raw) {
|
||
static const char *d = "0123456789abcdef";
|
||
std::string out;
|
||
out.reserve(raw.size() * 2);
|
||
for (unsigned char c : raw) {
|
||
out.push_back(d[c >> 4]);
|
||
out.push_back(d[c & 0xf]);
|
||
}
|
||
return out;
|
||
}
|
||
|
||
|
||
// GcsNodeInfo 的手工解析(只取 mpc 需要的字段)。
|
||
// 之所以不用 protobuf:ray 的 C++ 用旧 ABI(_GLIBCXX_USE_CXX11_ABI=0),而系统的
|
||
// libprotobuf 是 cxx11 ABI,同一个 TU 里没法混。这里自己走一遍 wire format 即可。
|
||
namespace wire {
|
||
|
||
bool ReadVarint(const std::string &b, size_t *i, uint64_t *out) {
|
||
uint64_t v = 0;
|
||
int shift = 0;
|
||
while (*i < b.size() && shift < 64) {
|
||
unsigned char c = static_cast<unsigned char>(b[(*i)++]);
|
||
v |= static_cast<uint64_t>(c & 0x7f) << shift;
|
||
if (!(c & 0x80)) {
|
||
*out = v;
|
||
return true;
|
||
}
|
||
shift += 7;
|
||
}
|
||
return false;
|
||
}
|
||
|
||
struct NodeInfo {
|
||
std::string node_id;
|
||
std::string node_manager_address;
|
||
std::string raylet_socket_name;
|
||
std::string object_store_socket_name;
|
||
int32_t node_manager_port = 0;
|
||
std::string session_dir;
|
||
};
|
||
|
||
NodeInfo ParseGcsNodeInfo(const std::string &b) {
|
||
NodeInfo out;
|
||
size_t i = 0;
|
||
while (i < b.size()) {
|
||
uint64_t key = 0;
|
||
if (!ReadVarint(b, &i, &key)) break;
|
||
const int field = static_cast<int>(key >> 3);
|
||
const int wt = static_cast<int>(key & 7);
|
||
if (wt == 0) { // varint
|
||
uint64_t v = 0;
|
||
if (!ReadVarint(b, &i, &v)) break;
|
||
if (field == 5) out.node_manager_port = static_cast<int32_t>(v);
|
||
} else if (wt == 2) { // length-delimited
|
||
uint64_t len = 0;
|
||
if (!ReadVarint(b, &i, &len)) break;
|
||
if (i + len > b.size()) break;
|
||
std::string v = b.substr(i, static_cast<size_t>(len));
|
||
i += static_cast<size_t>(len);
|
||
switch (field) {
|
||
case 1: out.node_id = v; break;
|
||
case 2: out.node_manager_address = v; break;
|
||
case 3: out.raylet_socket_name = v; break;
|
||
case 4: out.object_store_socket_name = v; break;
|
||
default: break;
|
||
}
|
||
} else if (wt == 5) { // fixed32
|
||
i += 4;
|
||
} else if (wt == 1) { // fixed64
|
||
i += 8;
|
||
} else {
|
||
break;
|
||
}
|
||
}
|
||
return out;
|
||
}
|
||
|
||
} // namespace wire
|
||
|
||
CoreWorkerOptions::TaskExecutionCallback MakeNoopCallback() {
|
||
return [](const rpc::Address &,
|
||
TaskType,
|
||
const std::string,
|
||
const RayFunction &,
|
||
const std::unordered_map<std::string, double> &,
|
||
const std::vector<std::shared_ptr<RayObject>> &,
|
||
const std::vector<rpc::ObjectReference> &,
|
||
const std::string &,
|
||
const std::string &,
|
||
std::vector<std::pair<ObjectID, std::shared_ptr<RayObject>>> *,
|
||
std::vector<std::pair<ObjectID, std::shared_ptr<RayObject>>> *,
|
||
std::vector<std::pair<ObjectID, bool>> *,
|
||
std::shared_ptr<LocalMemoryBuffer> &,
|
||
bool *,
|
||
std::string *,
|
||
std::string *,
|
||
const std::vector<ConcurrencyGroup> &,
|
||
const std::string,
|
||
bool,
|
||
bool,
|
||
bool,
|
||
int64_t,
|
||
int64_t,
|
||
const std::optional<std::string> &) {
|
||
// mpc 只借 Ray 做集群目录与放置,任务体不在这里跑。
|
||
return Status::OK();
|
||
};
|
||
}
|
||
|
||
void WriteOut(char *out, int out_len, const std::string &kv) {
|
||
if (!out || out_len <= 0) return;
|
||
std::snprintf(out, static_cast<size_t>(out_len), "%s", kv.c_str());
|
||
}
|
||
|
||
} // namespace
|
||
|
||
extern "C" {
|
||
|
||
// 连上 GCS,取本节点的 raylet/plasma socket,初始化 core worker。
|
||
// 返回 0 成功;负数失败,可用 mpc_ray_last_error() 取原因。
|
||
int mpc_ray_init(const char *gcs_ip, int gcs_port, const char *node_ip, char *out,
|
||
int out_len) {
|
||
std::string kv;
|
||
try {
|
||
const std::string ip = gcs_ip ? gcs_ip : "";
|
||
const std::string nip = node_ip ? node_ip : "";
|
||
|
||
gcs::GcsClientOptions gcs_options(
|
||
ip, gcs_port, ClusterID::Nil(),
|
||
/*allow_cluster_id_nil=*/true,
|
||
/*fetch_cluster_id_if_nil=*/false);
|
||
gcs::GlobalStateAccessor accessor(gcs_options);
|
||
if (!accessor.Connect()) {
|
||
g_err = "连不上 GCS: " + ip + ":" + std::to_string(gcs_port);
|
||
return -1;
|
||
}
|
||
|
||
std::string node_to_connect;
|
||
auto st = accessor.GetNodeToConnectForDriver(nip, &node_to_connect);
|
||
if (!st.ok()) {
|
||
g_err = "GetNodeToConnectForDriver 失败: " + st.ToString();
|
||
return -2;
|
||
}
|
||
|
||
const wire::NodeInfo node_info = wire::ParseGcsNodeInfo(node_to_connect);
|
||
if (node_info.node_id.empty() || node_info.raylet_socket_name.empty()) {
|
||
g_err = "GcsNodeInfo 解析失败(拿不到 node_id / raylet socket)";
|
||
return -3;
|
||
}
|
||
|
||
CoreWorkerOptions options;
|
||
options.worker_type = WorkerType::DRIVER;
|
||
// 注意:pip wheel 里的 raylet 只编了 Python 的 worker pool,声明 CPP 会被
|
||
// "Required Language isn't supported: CPP" 直接拒掉。这里借用 PYTHON 身份
|
||
// 注册成 driver(driver 不执行任务,任务体由 mpc 自己跑)。
|
||
options.language = Language::PYTHON;
|
||
options.store_socket = node_info.object_store_socket_name;
|
||
options.raylet_socket = node_info.raylet_socket_name;
|
||
options.gcs_options = gcs_options;
|
||
options.enable_logging = false;
|
||
options.install_failure_signal_handler = false;
|
||
options.interactive = false;
|
||
options.node_ip_address = nip.empty() ? node_info.node_manager_address : nip;
|
||
options.node_manager_port = node_info.node_manager_port;
|
||
options.driver_name = "mpc_driver";
|
||
options.metrics_agent_port = -1;
|
||
options.task_execution_callback = MakeNoopCallback();
|
||
options.job_id = accessor.GetNextJobID();
|
||
|
||
|
||
CoreWorkerProcess::Initialize(options);
|
||
g_inited = true;
|
||
|
||
char buf[128];
|
||
kv += "node_id=" + HexOf(node_info.node_id) + "\n";
|
||
kv += "node_ip=" + options.node_ip_address + "\n";
|
||
std::snprintf(buf, sizeof buf, "node_manager_port=%d\n", options.node_manager_port);
|
||
kv += buf;
|
||
kv += "raylet_socket=" + options.raylet_socket + "\n";
|
||
kv += "plasma_socket=" + options.store_socket + "\n";
|
||
kv += "job_id=" + options.job_id.Hex() + "\n";
|
||
// 下面两个值只有真正跟 raylet 握手完成后才拿得到,是「连上了」的硬证据。
|
||
kv += "worker_id=" + CoreWorkerProcess::GetCoreWorker().GetWorkerID().Hex() + "\n";
|
||
kv += "raylet_assigned_node_id=" +
|
||
CoreWorkerProcess::GetCoreWorker().GetCurrentNodeId().Hex() + "\n";
|
||
WriteOut(out, out_len, kv);
|
||
return 0;
|
||
} catch (const std::exception &e) {
|
||
g_err = std::string("异常: ") + e.what();
|
||
return -9;
|
||
} catch (...) {
|
||
g_err = "未知异常";
|
||
return -9;
|
||
}
|
||
}
|
||
|
||
const char *mpc_ray_last_error(void) { return g_err.c_str(); }
|
||
|
||
int mpc_ray_is_initialized(void) {
|
||
return (g_inited || CoreWorkerProcess::IsInitialized()) ? 1 : 0;
|
||
}
|
||
|
||
// 再问一次 raylet:仅凭 core worker 自报的身份,证明进程与 raylet 的会话还活着。
|
||
int mpc_ray_probe(char *out, int out_len) {
|
||
try {
|
||
if (!CoreWorkerProcess::IsInitialized()) {
|
||
g_err = "core worker 未初始化";
|
||
return -1;
|
||
}
|
||
auto &worker = CoreWorkerProcess::GetCoreWorker();
|
||
std::string kv = "probe=ok\n";
|
||
kv += "worker_id=" + worker.GetWorkerID().Hex() + "\n";
|
||
kv += "node_id=" + worker.GetCurrentNodeId().Hex() + "\n";
|
||
WriteOut(out, out_len, kv);
|
||
return 0;
|
||
} catch (const std::exception &e) {
|
||
g_err = std::string("probe 异常: ") + e.what();
|
||
return -1;
|
||
}
|
||
}
|
||
|
||
void mpc_ray_shutdown(void) {
|
||
if (CoreWorkerProcess::IsInitialized()) {
|
||
CoreWorkerProcess::Shutdown();
|
||
}
|
||
g_inited = false;
|
||
}
|
||
|
||
} // extern "C"
|