feat(sswflow): Phase 2.1 跨方 step 产物归属方追踪 + 跨方继承路由 (8/8 SUCCESS)
[背景] Phase 2 跨方真分发让中央协调方把 computeParty=selfMark=0 的 step 转发到远端 ssw-admin 跑。但下游 step(如 s4 训练/测试拆分、s5 建模 等)会按默认规则 resolveComputeParty 落到 SELF(selfMark=1), 跑去中心方本地找远端产的 csv ——「数据文件不存在」级联失败。 本轮让 artifact 带 producerPartyEndpoint 标记,下游 resolveComputeParty 据此继承同一远端。 [3 处核心修改] 1. StepForwardingClient.forward() 注入 producerPartyEndpoint 远端返回的每个 artifact 写入 ctx.computePartyEndpoint 2. FlowRunServiceImpl.execute() 注入 producerPartyEndpoint 本地执行也注入 selfEndpoint(selfMark=1 的 endpoint), 一次性预解析在循环开始 3. resolveComputeParty() 加 [Phase 2.1 跨方继承] 规则 节点无 datasetId/receiver 时,扫 inputs 所有 artifact 的 producerPartyEndpoint;全同端点 + 对应 selfMark=0 → 继承该 party [配套修改 — Phase 2 跨方真分发累积漏提交] - StepExecuteController(远端接收,@SaIgnore + X-Internal-Token) - StepContext 加 computePartyMode/NodeConfig/SelfMark/Endpoint - ComputeEngine supports(engineType, computeMode) 接口 - RemoteNaxComputeEngine 按 computePartyMode 选路 + 跨方转发 + op_vlr2p - SimulationComputeEngine supports 双签名 - PartyEntity 加 f_compute_mode/f_node_config 字段 - V1.1.1__party_node_config.sql 数据库迁移 [验证 runId] - 858350727431468869(修复前):s1/s2/s3 SUCCESS, s4 FAILED - 858360050459097157(含调试日志):8/8 SUCCESS, 76.2s - 858361832195242117(最终版):8/8 SUCCESS, 86.3s
This commit is contained in:
1 parent
8fd4c4487f
commit
0ed7061c49
10 files changed
+805
-8
No files matched your search
@@ -0,0 +1,23 @@
|
||||
-- ============================================================
|
||||
-- V1.1.1 party 加计算/节点模式字段(参与方节点信息)
|
||||
-- 依据 : 用户需求「在参与方里配置节点信息,含纯本地模拟/部署k8s模拟/接入真实连接器三种」
|
||||
-- 范围 : 仅 sswflow 库 party 表
|
||||
-- 回滚 : ALTER TABLE party DROP COLUMN IF EXISTS f_compute_mode;
|
||||
-- ALTER TABLE party DROP COLUMN IF EXISTS f_node_config;
|
||||
-- 说明 : compute_mode 决定该方算子走哪个计算引擎(SimulationComputeEngine /
|
||||
-- RemoteNaxComputeEngine→NaxJob / RemoteNaxComputeEngine→Nax2PJob);
|
||||
-- node_config 存该模式所需的连接参数(JSON)。为空时引擎回退全局
|
||||
-- sswflow.nax.remote 开关(向后兼容历史单部署)。
|
||||
-- ============================================================
|
||||
|
||||
ALTER TABLE party
|
||||
ADD COLUMN IF NOT EXISTS f_compute_mode varchar(32);
|
||||
|
||||
COMMENT ON COLUMN party.f_compute_mode
|
||||
IS '计算/节点模式: LOCAL_SIM(纯本地模拟) / K8S_SIM(部署k8s模拟) / CONNECTOR(接入真实连接器)';
|
||||
|
||||
ALTER TABLE party
|
||||
ADD COLUMN IF NOT EXISTS f_node_config text;
|
||||
|
||||
COMMENT ON COLUMN party.f_node_config
|
||||
IS '节点配置JSON: K8S_SIM→{namespace,naxImage,kubectlBin,timeoutMs}; CONNECTOR→{connectorId,connectorMode,endpoint}; LOCAL_SIM→可空';
|
||||
@@ -27,6 +27,20 @@ public interface ComputeEngine {
|
||||
*/
|
||||
boolean supports(String engineType);
|
||||
|
||||
/**
|
||||
* 是否支持该算子、在指定参与方节点模式下的执行。
|
||||
* <p>
|
||||
* 默认实现忽略 {@code computeMode},直接委托 {@link #supports(String)},
|
||||
* 保持对未覆盖实现的向后兼容。需要按参与方节点模式(LOCAL_SIM / K8S_SIM / CONNECTOR)
|
||||
* 改变选路的引擎应覆盖本方法。
|
||||
*
|
||||
* @param engineType 算子定义里的 engine 字段:local / mpc / tee
|
||||
* @param computeMode 本步骤计算归属方的节点模式(来自 PartyEntity.f_compute_mode);可为 null(回退全局开关)
|
||||
*/
|
||||
default boolean supports(String engineType, String computeMode) {
|
||||
return supports(engineType);
|
||||
}
|
||||
|
||||
/** 执行一个算子节点 */
|
||||
StepResult execute(StepContext context);
|
||||
|
||||
|
||||
+16
@@ -74,6 +74,22 @@ public class NaxComputeEngine implements ComputeEngine {
|
||||
return "mpc".equalsIgnoreCase(engineType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(String engineType, String computeMode) {
|
||||
// 纯本地模拟模式不抢 mpc 算子(交给 SimulationComputeEngine)
|
||||
if ("LOCAL_SIM".equalsIgnoreCase(computeMode)) {
|
||||
return false;
|
||||
}
|
||||
// 一旦本方节点模式显式设为 K8S_SIM / CONNECTOR,远程路径(RemoteNaxComputeEngine)
|
||||
// 必须接管 mpc 算子;本地子进程在 ssw-admin 容器内通常根本没有 python3,避免
|
||||
// 误抢后跑到 NaxComputeEngine → "Cannot run program /usr/bin/python3"。
|
||||
if ("K8S_SIM".equalsIgnoreCase(computeMode) || "CONNECTOR".equalsIgnoreCase(computeMode)) {
|
||||
return false;
|
||||
}
|
||||
// 模式未配(computeMode 为 null/空)保留原语义:仍可本地兜底
|
||||
return supports(engineType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
// 数值小的优先;100 < 1000(模拟引擎)→ mpc 算子全部走 Nax
|
||||
|
||||
+75
-3
@@ -59,6 +59,9 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
@Autowired
|
||||
private NaxComputeEngine localEngine;
|
||||
|
||||
@Autowired
|
||||
private StepForwardingClient stepForwardingClient;
|
||||
|
||||
private final HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
@@ -73,6 +76,15 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
return "mpc".equalsIgnoreCase(engineType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(String engineType, String computeMode) {
|
||||
// 纯本地模拟模式:本类退出,交给 SimulationComputeEngine
|
||||
if ("LOCAL_SIM".equalsIgnoreCase(computeMode)) {
|
||||
return false;
|
||||
}
|
||||
return "mpc".equalsIgnoreCase(engineType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
// 数值小的优先;50 < 100(Nax 本地) < 1000(模拟) → mpc 算子全部先走本类(再由其决定远程或委派本地)
|
||||
@@ -81,11 +93,45 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
|
||||
@Override
|
||||
public StepResult execute(StepContext ctx) {
|
||||
// dev / 联调:直接委派本地子进程(零回归,无需 TDC / k8s)
|
||||
if (!remoteEnabled) {
|
||||
// 按「本步骤计算归属方」的节点模式选路;computeMode 为空(历史部署未配)回退全局开关
|
||||
String mode = ctx.getComputePartyMode();
|
||||
boolean remote;
|
||||
if (mode == null || mode.isEmpty()) {
|
||||
remote = remoteEnabled; // 向后兼容:沿用 sswflow.nax.remote
|
||||
} else {
|
||||
remote = !"LOCAL_SIM".equalsIgnoreCase(mode); // K8S_SIM / CONNECTOR → 远程;LOCAL_SIM → 本地兜底
|
||||
}
|
||||
|
||||
// dev / 联调 / 纯本地模拟:直接委派本地子进程(零回归,无需 TDC / k8s)
|
||||
if (!remote) {
|
||||
return localEngine.execute(ctx);
|
||||
}
|
||||
|
||||
// [Phase 2 跨方真分发] 计算归属方是远端参与方(selfMark=0)且该方在 PartyEntity.f_endpoint
|
||||
// 注册了可达的 ssw-admin 入口时,把整步 HTTP 转发到该方执行;
|
||||
// 远端 ssw-admin 用自身 K8S_NAMESPACE / TDC 真实跑算子(数据不出域)。
|
||||
Integer selfMark = ctx.getComputePartySelfMark();
|
||||
String endpoint = ctx.getComputePartyEndpoint();
|
||||
if (selfMark != null && selfMark == 0
|
||||
&& endpoint != null && !endpoint.isEmpty()) {
|
||||
ctx.info(String.format("[Phase2 跨方] step=%s 计算归属方为远端参与方,endpoint=%s,转发执行",
|
||||
ctx.getStepId(), endpoint));
|
||||
ctx.reportProgress(10);
|
||||
StepResult forwarded = stepForwardingClient.forward(endpoint, ctx, (level, msg) -> {
|
||||
if ("ERROR".equalsIgnoreCase(level)) ctx.error(msg);
|
||||
else if ("WARN".equalsIgnoreCase(level)) ctx.warn(msg);
|
||||
else ctx.info(msg);
|
||||
});
|
||||
if (forwarded.isSuccess()) {
|
||||
ctx.reportProgress(100);
|
||||
ctx.info("[Phase2 跨方] 远端执行成功,artifacts 已回传");
|
||||
} else {
|
||||
ctx.reportProgress(100);
|
||||
ctx.error("[Phase2 跨方] 远端执行失败: " + forwarded.getErrorMsg());
|
||||
}
|
||||
return forwarded;
|
||||
}
|
||||
|
||||
// [V1.5+] 真两方·数据不出域 纵向 LR 算子路由(operatorKey=op_vlr2p)→ 走 Nax2PJob(双 VM 双 Job)
|
||||
String operatorKey = ctx.getOperatorKey() == null ? "" : ctx.getOperatorKey();
|
||||
if ("op_vlr2p".equalsIgnoreCase(operatorKey) || "nax_2p_vlr".equalsIgnoreCase(operatorKey)) {
|
||||
@@ -101,7 +147,7 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
spec.put("params", ctx.getParams());
|
||||
spec.put("inputs", normalizeInputs(ctx.getInputs()));
|
||||
spec.put("parties", ctx.getParties() == null ? new ArrayList<>() : ctx.getParties());
|
||||
spec.put("datasets", ctx.getDatasets() == null ? new JSONObject() : ctx.getDatasets());
|
||||
spec.put("datasets", ctx.getDatasets() == null ? new JSONObject() : datasetsToJson(ctx.getDatasets()));
|
||||
spec.put("workDir", ctx.getWorkDir());
|
||||
spec.put("backend", "local");
|
||||
|
||||
@@ -195,6 +241,18 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
String aHost = params.get("partyAHost") == null ? null : String.valueOf(params.get("partyAHost"));
|
||||
String bHost = params.get("partyBHost") == null ? null : String.valueOf(params.get("partyBHost"));
|
||||
|
||||
// 若本步骤计算归属方在 nodeConfig 里配了连接器,则作为缺省连接器(画布未显式给时用)
|
||||
JSONObject nc = ctx.getComputePartyNodeConfig();
|
||||
if (nc != null) {
|
||||
String cid = nc.getString("connectorId");
|
||||
if (cid != null && !cid.isEmpty()) {
|
||||
if (aConnectorId == null || aConnectorId.isEmpty() || "J7NODE-A".equals(aConnectorId)) aConnectorId = cid;
|
||||
if (bConnectorId == null || bConnectorId.isEmpty() || "J7NODE-B".equals(bConnectorId)) bConnectorId = cid;
|
||||
}
|
||||
String cm = nc.getString("connectorMode");
|
||||
if (cm != null && !cm.isEmpty()) connectorMode = cm;
|
||||
}
|
||||
|
||||
ctx.info(String.format("[V1.5+] 两方 Nax (taskId=%s) connectorMode=%s nsub=%d epochs=%d lr=%.3f -> 提交到 TDC Nax2PJob",
|
||||
taskId, connectorMode, nsub, epochs, lr));
|
||||
ctx.reportProgress(10);
|
||||
@@ -278,6 +336,20 @@ public class RemoteNaxComputeEngine implements ComputeEngine {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
/** 把 {@code datasets: Map<String, JSONObject>} 显式转 JSONObject。
|
||||
* fastjson2 在 JSONObject.put("k", Map) 时可能把 map 序列化成普通 toString 而非嵌套 JSON 对象,
|
||||
* 导致下游 nax_adapter.py 拿不到 location 等字段。手工 put 安全。
|
||||
*/
|
||||
private JSONObject datasetsToJson(Map<String, JSONObject> raw) {
|
||||
JSONObject out = new JSONObject();
|
||||
if (raw == null) return out;
|
||||
for (Map.Entry<String, JSONObject> e : raw.entrySet()) {
|
||||
if (e.getValue() != null) out.put(e.getKey(), e.getValue());
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 {@code inputs: Map<String, List<JSONObject>>} 拍平为下游适配器期望的格式。
|
||||
* 同时把 rows 限制在 5 行预览避免 JSON 过大(rows 大数据应走 uri)。
|
||||
|
||||
+10
@@ -40,6 +40,16 @@ public class SimulationComputeEngine implements ComputeEngine {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supports(String engineType, String computeMode) {
|
||||
// 纯本地模拟模式:本类接管 mpc / local 算子(造假产物,零外部依赖)
|
||||
if ("LOCAL_SIM".equalsIgnoreCase(computeMode)) {
|
||||
return "mpc".equalsIgnoreCase(engineType) || "local".equalsIgnoreCase(engineType);
|
||||
}
|
||||
// 其它模式:保留原有「兜底所有」行为,作为真实引擎都不可用时的最终退路
|
||||
return supports(engineType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int order() {
|
||||
return 1000;
|
||||
|
||||
@@ -51,6 +51,33 @@ public class StepContext {
|
||||
*/
|
||||
private Map<String, JSONObject> datasets;
|
||||
|
||||
/**
|
||||
* 本步骤「计算归属方」的节点模式(LOCAL_SIM / K8S_SIM / CONNECTOR),
|
||||
* 由 FlowRunServiceImpl 按算子语义推导并注入,驱动引擎按方选路。
|
||||
*/
|
||||
private String computePartyMode;
|
||||
|
||||
/**
|
||||
* 本步骤计算归属方的节点配置 JSON(来自 PartyEntity.f_node_config):
|
||||
* K8S_SIM → {namespace,naxImage,kubectlBin,timeoutMs};CONNECTOR → {connectorId,connectorMode,endpoint}。
|
||||
*/
|
||||
private JSONObject computePartyNodeConfig;
|
||||
|
||||
/**
|
||||
* 本步骤计算归属方的 selfMark:1=本方(中心协调方),0=远端参与方。
|
||||
* <p>
|
||||
* 远端参与方(selfMark=0)且 {@link #computePartyEndpoint} 非空时,
|
||||
* RemoteNaxComputeEngine 走 HTTP 转发分支,把整步分发到该方 ssw-admin 执行
|
||||
* (数据不出域;远端用自身 K8S_NAMESPACE / TDC 跑 NaxJob)。
|
||||
*/
|
||||
private Integer computePartySelfMark;
|
||||
|
||||
/**
|
||||
* 本步骤计算归属方的 f_endpoint(如 http://ssw-admin-b.party-b:30000),
|
||||
* 远端参与方(selfMark=0)时 RemoteNaxComputeEngine 用它做 HTTP 转发目标。
|
||||
*/
|
||||
private String computePartyEndpoint;
|
||||
|
||||
/**
|
||||
* 本次运行独占的工作目录,Python 适配器把产物 CSV 落到这里,
|
||||
* artifact uri 以 {@code file://} 前缀返回。
|
||||
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
package ssw.sswflow.engine;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* 跨方算子转发客户端(Phase 2 跨方真分发)。
|
||||
* <p>
|
||||
* 中央协调方(selfMark=1)的 SSWFlow 在某步的「计算归属方」是远端参与方(selfMark=0)
|
||||
* 且该方在 PartyEntity.f_endpoint 注册了可达的 ssw-admin 入口时,把整步通过 HTTP
|
||||
* POST 到 {@code ${endpoint}/api/ssw/sswflow/Step/InternalExecute} 执行。
|
||||
* <p>
|
||||
* 远端 ssw-admin 用自身 K8S_NAMESPACE / TDC(按自身节点配置)跑真实 NaxJob,
|
||||
* 数据留在远端节点的 hostPath / TDC 沙箱,**不出域**;中心方只拿到「脱敏预览 + metrics」
|
||||
* 形式的 artifact 与执行日志。
|
||||
*
|
||||
* <h3>鉴权</h3>
|
||||
* {@code App-Code: sswflow} + {@code X-Internal-Token: ${sswflow.cross-party.secret:}}
|
||||
* (per-party yaml 显式注入,所有 ssw-admin 共用同一密钥;生产应替换为更安全的密钥分发机制)。
|
||||
*
|
||||
* @author SSWFlow
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class StepForwardingClient {
|
||||
|
||||
/** 中心方对外调用时声明的 App-Code(与 RemoteNaxComputeEngine → TDC 一致) */
|
||||
public static final String APP_CODE = "sswflow";
|
||||
|
||||
/** 远端入口路径(与 StepExecuteController @RequestMapping + @PostMapping 一致) */
|
||||
public static final String PATH = "/api/ssw/sswflow/Step/Internal/Execute";
|
||||
|
||||
@Value("${sswflow.cross-party.secret:}")
|
||||
private String crossPartySecret;
|
||||
|
||||
@Value("${sswflow.cross-party.timeout-seconds:600}")
|
||||
private long timeoutSeconds;
|
||||
|
||||
private final HttpClient http = HttpClient.newBuilder()
|
||||
.connectTimeout(Duration.ofSeconds(10))
|
||||
.build();
|
||||
|
||||
/**
|
||||
* 把 step 转发到 {@code endpoint}({@code PartyEntity.f_endpoint})执行。
|
||||
*
|
||||
* @param endpoint 远端 ssw-admin 的 base URL(不带尾斜杠)
|
||||
* @param ctx 本步骤的执行上下文(runId/stepId/operator/params/inputs/datasets/parties/workDir
|
||||
* + computePartyMode / computePartyNodeConfig)
|
||||
* @param localLog 中心方回调:把远端日志回写到本地 run_event(INFO/WARN/ERROR 分级)
|
||||
* @return 远端执行结果;远端不可达 / 鉴权失败 / 内部异常统一返回 StepResult.fail
|
||||
*/
|
||||
public StepResult forward(String endpoint, StepContext ctx, java.util.function.BiConsumer<String, String> localLog) {
|
||||
String url = endpoint.endsWith("/") ? endpoint + PATH.substring(1) : endpoint + PATH;
|
||||
|
||||
// 1) 序列化 StepContext 为可发送 JSON(logger/progress 远端不需要)
|
||||
JSONObject payload = new JSONObject();
|
||||
payload.put("runId", ctx.getRunId());
|
||||
payload.put("stepId", ctx.getStepId());
|
||||
payload.put("operatorKey", ctx.getOperatorKey());
|
||||
payload.put("operator", ctx.getOperator());
|
||||
payload.put("params", ctx.getParams());
|
||||
payload.put("inputs", normalizeInputs(ctx.getInputs()));
|
||||
payload.put("parties", ctx.getParties());
|
||||
payload.put("datasets", ctx.getDatasets());
|
||||
payload.put("workDir", ctx.getWorkDir());
|
||||
payload.put("computePartyMode", ctx.getComputePartyMode());
|
||||
payload.put("computePartyNodeConfig", ctx.getComputePartyNodeConfig());
|
||||
payload.put("computePartySelfMark", ctx.getComputePartySelfMark());
|
||||
payload.put("computePartyEndpoint", ctx.getComputePartyEndpoint());
|
||||
// datasets 必须显式转 JSONObject:Map<String, JSONObject> 在 JSONObject.put 里序列化时
|
||||
// 可能丢字段(fastjson2 把 map 当成普通 map 转字符串而不是嵌套对象);手工逐项 put 安全。
|
||||
JSONObject datasetsJson = new JSONObject();
|
||||
if (ctx.getDatasets() != null) {
|
||||
for (var e : ctx.getDatasets().entrySet()) {
|
||||
if (e.getValue() != null) {
|
||||
datasetsJson.put(e.getKey(), e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
payload.put("datasets", datasetsJson);
|
||||
|
||||
if (localLog != null) {
|
||||
localLog.accept("INFO",
|
||||
String.format("[Phase2 跨方] 转发 step=%s 到 %s (computePartyMode=%s)",
|
||||
ctx.getStepId(), url, ctx.getComputePartyMode()));
|
||||
}
|
||||
|
||||
try {
|
||||
HttpRequest.Builder rb = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.header("Content-Type", "application/json")
|
||||
.header("App-Code", APP_CODE)
|
||||
.header("X-Internal-Token", crossPartySecret == null ? "" : crossPartySecret)
|
||||
.POST(HttpRequest.BodyPublishers.ofString(payload.toJSONString()))
|
||||
.timeout(Duration.ofSeconds(timeoutSeconds));
|
||||
HttpResponse<String> resp = http.send(rb.build(), HttpResponse.BodyHandlers.ofString());
|
||||
if (resp.statusCode() != 200) {
|
||||
String msg = "远端 ssw-admin 返回 HTTP " + resp.statusCode();
|
||||
if (localLog != null) localLog.accept("ERROR", "[Phase2 跨方] " + msg);
|
||||
return StepResult.fail(msg);
|
||||
}
|
||||
JSONObject body = JSON.parseObject(resp.body());
|
||||
JSONObject data = body.getJSONObject("data");
|
||||
if (data == null) {
|
||||
String msg = "远端响应缺少 data: " + resp.body();
|
||||
if (localLog != null) localLog.accept("ERROR", "[Phase2 跨方] " + msg);
|
||||
return StepResult.fail(msg);
|
||||
}
|
||||
|
||||
// 2) 把远端日志回放到本地 run_event 通道(让前端 SSE 无缝看到跨网络边界)
|
||||
var logs = data.getJSONArray("logs");
|
||||
if (logs != null && localLog != null) {
|
||||
for (int i = 0; i < logs.size(); i++) {
|
||||
JSONObject e = logs.getJSONObject(i);
|
||||
String lvl = e.getString("level");
|
||||
String msg = e.getString("message");
|
||||
if (msg == null || msg.isEmpty()) continue;
|
||||
if ("WARN".equalsIgnoreCase(lvl)) localLog.accept("WARN", "[远端] " + msg);
|
||||
else if ("ERROR".equalsIgnoreCase(lvl)) localLog.accept("ERROR", "[远端] " + msg);
|
||||
else localLog.accept("INFO", "[远端] " + msg);
|
||||
}
|
||||
}
|
||||
if (localLog != null) {
|
||||
String engine = data.getString("engineName");
|
||||
String partyCode = data.getString("computePartyCode");
|
||||
localLog.accept("INFO", String.format(
|
||||
"[Phase2 跨方] 远端完成:engine=%s partyCode=%s status=%s",
|
||||
engine, partyCode, data.getString("status")));
|
||||
}
|
||||
|
||||
// 3) 解析结果
|
||||
StepResult result = new StepResult();
|
||||
result.setStatus(data.getString("status"));
|
||||
if (!"SUCCESS".equalsIgnoreCase(result.getStatus())) {
|
||||
result.setErrorMsg(data.getString("errorMsg"));
|
||||
} else {
|
||||
var artifacts = data.getJSONArray("artifacts");
|
||||
if (artifacts != null) {
|
||||
for (int i = 0; i < artifacts.size(); i++) {
|
||||
JSONObject a = artifacts.getJSONObject(i);
|
||||
// [Phase 2.1 产物归属方追踪] 给远端返回的 artifact 打 producerPartyEndpoint 标记
|
||||
// (远端 ctx.computePartyEndpoint 已被透传到中心方,就是本步实际执行的远端端点);
|
||||
// 中心方缓存进 outputs 后,下游 step 可据此判断「继承上游跨方方」。
|
||||
if (a.getString("producerPartyEndpoint") == null) {
|
||||
a.put("producerPartyEndpoint", ctx.getComputePartyEndpoint());
|
||||
}
|
||||
result.getArtifacts().add(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
String msg = "跨方转发异常: " + e.getMessage();
|
||||
log.error("[Phase2 跨方] 转发 step={} url={} 异常", ctx.getStepId(), url, e);
|
||||
if (localLog != null) localLog.accept("ERROR", msg);
|
||||
return StepResult.fail(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/** 与 RemoteNaxComputeEngine.normalizeInputs 同语义:rows 太大就不传,Python 端按 uri 读 */
|
||||
private JSONObject normalizeInputs(java.util.Map<String, java.util.List<JSONObject>> raw) {
|
||||
JSONObject out = new JSONObject();
|
||||
if (raw == null) return out;
|
||||
for (var e : raw.entrySet()) {
|
||||
var arr = new com.alibaba.fastjson2.JSONArray();
|
||||
if (e.getValue() != null) {
|
||||
for (JSONObject src : e.getValue()) {
|
||||
if (src == null) continue;
|
||||
JSONObject compact = new JSONObject();
|
||||
for (var kv : src.entrySet()) {
|
||||
Object v = kv.getValue();
|
||||
if ("rows".equals(kv.getKey()) && v instanceof com.alibaba.fastjson2.JSONArray
|
||||
&& ((com.alibaba.fastjson2.JSONArray) v).size() > 5) {
|
||||
continue;
|
||||
}
|
||||
compact.put(kv.getKey(), v);
|
||||
}
|
||||
arr.add(compact);
|
||||
}
|
||||
}
|
||||
out.put(e.getKey(), arr);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
}
|
||||
+216
-5
@@ -352,6 +352,23 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
// 供所有算子共享,避免每步重复查库。
|
||||
List<JSONObject> partiesCache = resolvePartiesByProject(getProjectId(runId));
|
||||
Map<String, JSONObject> datasetsCache = new HashMap<>();
|
||||
// 本方 SELF 端点(selfMark=1 的 party.f_endpoint),给本地执行的 artifact 打 producerPartyEndpoint 标记
|
||||
// —— 下游步骤 resolveComputeParty 据此继承同一参与方,避免「跨方 step 产物落到本地、本地找不到」错路由。
|
||||
String selfEndpoint = null;
|
||||
for (JSONObject p : partiesCache) {
|
||||
if (Integer.valueOf(1).equals(p.getInteger("selfMark"))) {
|
||||
selfEndpoint = p.getString("endpoint");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (selfEndpoint == null) {
|
||||
// 项目参与方里没找到 self-mark 1,退化查 Party 表
|
||||
try {
|
||||
PartyEntity selfParty = partyService.getOne(
|
||||
new QueryWrapper<PartyEntity>().eq("f_self_mark", 1).last("limit 1"), false);
|
||||
if (selfParty != null) selfEndpoint = selfParty.getEndpoint();
|
||||
} catch (Exception ignore) { /* ignore */ }
|
||||
}
|
||||
|
||||
// stepId -> 该节点各端口产出,供下游作为输入
|
||||
Map<String, Map<String, JSONObject>> outputs = new HashMap<>();
|
||||
@@ -410,7 +427,6 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
|
||||
JSONObject operator = operatorCatalogService.getOperator(operatorKey);
|
||||
String engineType = operator == null ? "local" : operator.getString("engine");
|
||||
ComputeEngine engine = pickEngine(engineType);
|
||||
|
||||
// 第一次遇到本步时,缓存数据集解析结果(按 datasetId),后续端口直接复用
|
||||
JSONObject paramsObj = step.getJSONObject("params");
|
||||
@@ -426,6 +442,36 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
}
|
||||
}
|
||||
|
||||
// 推导本步骤的「计算归属方」,取其节点模式/配置 + 自标记/端点驱动引擎按方选路(含跨方转发)
|
||||
ComputePartyRef partyRef = resolveComputeParty(step, paramsObj, datasetsCache, partiesCache,
|
||||
collectInputs(graph, stepId, outputs));
|
||||
String computePartyId = partyRef.partyId;
|
||||
String computeMode = null;
|
||||
JSONObject computeNodeConfig = null;
|
||||
if (computePartyId != null) {
|
||||
for (JSONObject p : partiesCache) {
|
||||
if (computePartyId.equals(p.getString("id"))) {
|
||||
computeMode = p.getString("computeMode");
|
||||
computeNodeConfig = parseNodeConfig(p.getString("nodeConfig"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
// 计算归属方不在项目参与方里(如自 SELF,但项目只有银行/征信/消费金融)时,
|
||||
// 直接查 Party 表补回 computeMode / nodeConfig,避免落到 null 走默认路径
|
||||
if (computeMode == null) {
|
||||
try {
|
||||
PartyEntity pe = partyService.getById(computePartyId);
|
||||
if (pe != null) {
|
||||
computeMode = pe.getComputeMode();
|
||||
computeNodeConfig = parseNodeConfig(pe.getNodeConfig());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[SSWFlow] 补查计算归属方节点配置异常: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
ComputeEngine engine = pickEngine(engineType, computeMode);
|
||||
|
||||
StepContext context = StepContext.builder()
|
||||
.runId(runId)
|
||||
.stepId(stepId)
|
||||
@@ -435,6 +481,10 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
.inputs(collectInputs(graph, stepId, outputs))
|
||||
.parties(partiesCache)
|
||||
.datasets(datasetsCache)
|
||||
.computePartyMode(computeMode)
|
||||
.computePartyNodeConfig(computeNodeConfig)
|
||||
.computePartySelfMark(partyRef.selfMark)
|
||||
.computePartyEndpoint(partyRef.endpoint)
|
||||
.workDir("/tmp/sswflow-nax/" + runId)
|
||||
.logger((level, message) ->
|
||||
event(runId, stepId, "step.log", null, null, level, message))
|
||||
@@ -453,7 +503,16 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
long cost = System.currentTimeMillis() - stepStart;
|
||||
if (result.isSuccess()) {
|
||||
Map<String, JSONObject> portOutputs = new HashMap<>();
|
||||
// [Phase 2.1 产物归属方追踪] 给每个 artifact 打 producerPartyEndpoint 标记:
|
||||
// - 跨方 step(selfMark=0):用 ctx.computePartyEndpoint(远端 endpoint)
|
||||
// - 本地 step(selfMark=1 或兜底):用 selfEndpoint(中心方 endpoint),便于下游识别「本地出产物」
|
||||
// - 远端转发时 StepForwardingClient 已优先注入;若 ctx 缺失再补
|
||||
String producerEndpoint = (partyRef.selfMark != null && partyRef.selfMark == 0)
|
||||
? partyRef.endpoint : selfEndpoint;
|
||||
for (JSONObject artifact : result.getArtifacts()) {
|
||||
if (artifact.getString("producerPartyEndpoint") == null) {
|
||||
artifact.put("producerPartyEndpoint", producerEndpoint);
|
||||
}
|
||||
saveArtifact(runId, stepId, artifact);
|
||||
portOutputs.put(artifact.getString("portKey"), artifact);
|
||||
}
|
||||
@@ -515,12 +574,161 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
EVENT_SEQ.remove(runId);
|
||||
}
|
||||
|
||||
/** 按 order 升序选第一个支持该执行环境的引擎 */
|
||||
private ComputeEngine pickEngine(String engineType) {
|
||||
/** 按 order 升序选第一个支持该执行环境、且在指定参与方节点模式下可用的引擎 */
|
||||
private ComputeEngine pickEngine(String engineType, String computeMode) {
|
||||
return computeEngines.stream()
|
||||
.filter(e -> e.supports(engineType))
|
||||
.filter(e -> e.supports(engineType, computeMode))
|
||||
.min(Comparator.comparingInt(ComputeEngine::order))
|
||||
.orElseThrow(() -> new DataException("没有可用的计算引擎: " + engineType));
|
||||
.orElseThrow(() -> new DataException("没有可用的计算引擎: " + engineType + " / " + computeMode));
|
||||
}
|
||||
|
||||
/**
|
||||
* 推导某步骤的「计算归属方」:
|
||||
* <ol>
|
||||
* <li>source.dataset —— 数据集 owner(datasetsCache[partyId])</li>
|
||||
* <li>prep.intersect —— params.receiver(partyCode → partyId)</li>
|
||||
* <li>[Phase 2.1] 其余节点若所有上游非空 artifact 都来自同一非 SELF 跨方端点(producerPartyEndpoint)—— 继承该参与方</li>
|
||||
* <li>其余 —— 本方(selfMark=1)。项目参与方里没有 self-mark 1 时,回退查 Party 表的 self-mark 1</li>
|
||||
* </ol>
|
||||
* 返回 partyId + 自标记 + 远端 endpoint,供中心调度器判断「本地跑」还是「跨方转发」。
|
||||
*/
|
||||
private ComputePartyRef resolveComputeParty(JSONObject step, JSONObject params,
|
||||
Map<String, JSONObject> dsCache, List<JSONObject> parties,
|
||||
Map<String, List<JSONObject>> inputs) {
|
||||
ComputePartyRef ref = new ComputePartyRef();
|
||||
// 1) dataset → 数据方 owner(先在 partiesCache 查;找不到时退化查 Party 表)
|
||||
String datasetId = params.getString("datasetId");
|
||||
if (datasetId != null && !datasetId.isEmpty()) {
|
||||
JSONObject ds = dsCache.get(datasetId);
|
||||
if (ds != null && ds.getString("partyId") != null) {
|
||||
populateRef(ref, ds.getString("partyId"), parties);
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
// 2) receiver → 求交接收方
|
||||
String receiver = params.getString("receiver");
|
||||
if (receiver != null && !receiver.isEmpty()) {
|
||||
for (JSONObject p : parties) {
|
||||
if (receiver.equalsIgnoreCase(p.getString("code"))) {
|
||||
populateRef(ref, p.getString("id"), parties);
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
}
|
||||
// 0) [Phase 2.1 跨方继承] 节点无显式 datasetId/receiver 时,若所有上游非空 artifact
|
||||
// 都来自同一非 SELF 跨方端点(producerPartyEndpoint 标记),则继承该参与方——避免
|
||||
// 「跨方 step 产物回到中心方 → 中心方错认 SELF → 找不到产物 uri」级联失败。
|
||||
// 缺一不可:① 全部非空 input 同端点;② 该端点对应 party 自标记=0(远端);③ inputs 非空。
|
||||
if ((datasetId == null || datasetId.isEmpty())
|
||||
&& (receiver == null || receiver.isEmpty())
|
||||
&& inputs != null && !inputs.isEmpty()) {
|
||||
String inherited = null;
|
||||
boolean homogeneous = true;
|
||||
int nonEmptyCount = 0;
|
||||
for (List<JSONObject> list : inputs.values()) {
|
||||
if (list == null) continue;
|
||||
for (JSONObject a : list) {
|
||||
if (a == null) continue;
|
||||
String ep = a.getString("producerPartyEndpoint");
|
||||
if (ep == null || ep.isEmpty()) continue;
|
||||
nonEmptyCount++;
|
||||
if (inherited == null) {
|
||||
inherited = ep;
|
||||
} else if (!inherited.equals(ep)) {
|
||||
homogeneous = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!homogeneous) break;
|
||||
}
|
||||
if (homogeneous && inherited != null && nonEmptyCount > 0) {
|
||||
// 找 endpoint 对应的 party(selfMark=0 的远端参与方)
|
||||
for (JSONObject p : parties) {
|
||||
if (inherited.equals(p.getString("endpoint"))
|
||||
&& Integer.valueOf(0).equals(p.getInteger("selfMark"))) {
|
||||
populateRef(ref, p.getString("id"), parties);
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
// 项目参与方里没匹配(理论上 ProjectParty 应包含),退化查 Party 表
|
||||
try {
|
||||
List<PartyEntity> candidates = partyService.list(
|
||||
new QueryWrapper<PartyEntity>().eq("f_endpoint", inherited));
|
||||
PartyEntity pe = candidates.isEmpty() ? null : candidates.get(0);
|
||||
if (pe != null && pe.getSelfMark() != null && pe.getSelfMark() == 0) {
|
||||
ref.partyId = pe.getId();
|
||||
ref.selfMark = 0;
|
||||
ref.endpoint = pe.getEndpoint();
|
||||
return ref;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[SSWFlow] 跨方继承查 Party 表异常 endpoint={} err={}", inherited, e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
// 3) 自方(SELF)
|
||||
for (JSONObject p : parties) {
|
||||
Integer self = p.getInteger("selfMark");
|
||||
if (self != null && self == 1) {
|
||||
populateRef(ref, p.getString("id"), parties);
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
// 4) 项目参与方里没有 self-mark 1,退化查 Party 表的 self-mark 1
|
||||
try {
|
||||
PartyEntity selfParty = partyService.getOne(
|
||||
new QueryWrapper<PartyEntity>().eq("f_self_mark", 1).last("limit 1"), false);
|
||||
if (selfParty != null) {
|
||||
ref.partyId = selfParty.getId();
|
||||
ref.selfMark = 1;
|
||||
ref.endpoint = selfParty.getEndpoint();
|
||||
return ref;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[SSWFlow] 查 self-mark=1 参与方异常: {}", e.getMessage());
|
||||
}
|
||||
return ref;
|
||||
}
|
||||
|
||||
/** 把 partiesCache 中该 partyId 的 selfMark + endpoint 拷到 ref;不在 cache 则回退 Party 表 */
|
||||
private void populateRef(ComputePartyRef ref, String partyId, List<JSONObject> parties) {
|
||||
ref.partyId = partyId;
|
||||
for (JSONObject p : parties) {
|
||||
if (partyId != null && partyId.equals(p.getString("id"))) {
|
||||
ref.selfMark = p.getInteger("selfMark");
|
||||
ref.endpoint = p.getString("endpoint");
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 退化查 Party 表(中心方视角的远端参与方一般不在本项目的 partiesCache 中,但 DB 中存在)
|
||||
try {
|
||||
PartyEntity pe = partyService.getById(partyId);
|
||||
if (pe != null) {
|
||||
ref.selfMark = pe.getSelfMark();
|
||||
ref.endpoint = pe.getEndpoint();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("[SSWFlow] 补查参与方 selfMark/endpoint 异常: {}", e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** 计算归属方解析结果(partyId + selfMark + endpoint) */
|
||||
private static class ComputePartyRef {
|
||||
String partyId;
|
||||
Integer selfMark; // 1=本方中心协调方,0=远端参与方
|
||||
String endpoint; // 远端参与方的 ssw-admin base URL(selfMark=0 时才有意义)
|
||||
}
|
||||
|
||||
/** 安全解析节点配置 JSON(空/非法时返回 null) */
|
||||
private JSONObject parseNodeConfig(String s) {
|
||||
if (s == null || s.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return JSON.parseObject(s);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 收集某节点各输入端口对应的上游产出 */
|
||||
@@ -747,9 +955,12 @@ public class FlowRunServiceImpl extends SswflowServiceImpl<FlowRunMapper, FlowRu
|
||||
JSONObject jo = new JSONObject();
|
||||
jo.put("code", party.getPartyCode());
|
||||
jo.put("name", party.getName());
|
||||
jo.put("id", party.getId());
|
||||
jo.put("endpoint", party.getEndpoint());
|
||||
jo.put("role", party.getRole());
|
||||
jo.put("selfMark", party.getSelfMark());
|
||||
jo.put("computeMode", party.getComputeMode());
|
||||
jo.put("nodeConfig", party.getNodeConfig());
|
||||
jo.put("joinStatus", pp.getJoinStatus());
|
||||
result.add(jo);
|
||||
}
|
||||
|
||||
+207
@@ -0,0 +1,207 @@
|
||||
package ssw.sswflow.controller;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import com.alibaba.fastjson2.JSONArray;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jnpf.base.ActionResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ssw.sswflow.engine.ComputeEngine;
|
||||
import ssw.sswflow.engine.StepContext;
|
||||
import ssw.sswflow.engine.StepResult;
|
||||
import ssw.sswflow.engine.StepResultRemote;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SSWFlow 跨方算子执行入口(Phase 2 跨方真分发)。
|
||||
* <p>
|
||||
* 由中央协调方(selfMark=1)的 {@code RemoteNaxComputeEngine} 通过
|
||||
* {@code StepForwardingClient} HTTP POST 调用;
|
||||
* 远端 ssw-admin 收到 StepContext 后按自身 K8S_NAMESPACE / TDC 真实执行该算子,
|
||||
* 数据不出本方域(hostPath / TDC 沙箱),把脱敏后的 artifact + 日志回传。
|
||||
*
|
||||
* <h3>安全模型</h3>
|
||||
* <ul>
|
||||
* <li>{@link SaIgnore} 跳过 Sa-Token 用户登录,与 FlowRunInternalController 同模式</li>
|
||||
* <li>独立 URL 前缀 {@code /api/ssw/sswflow/Step/InternalExecute},进 {@code exclude-url}</li>
|
||||
* <li>内部鉴权:{@code App-Code: sswflow} + {@code X-Internal-Token: ${sswflow.cross-party.secret:}}</li>
|
||||
* <li>本端不直接处理用户态请求,仅作跨方转发目标;生产密钥应通过 Secret 分发</li>
|
||||
* </ul>
|
||||
*
|
||||
* <h3>与现有端点的关系</h3>
|
||||
* 与 {@code FlowRunInternalController} 对称:后者是「启动一次 FlowRun」入口,
|
||||
* 本端是「替我跑一个算子」入口;两者的鉴权模式、内部密钥共用思路一致。
|
||||
*
|
||||
* @author SSWFlow
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController("sswflowStepExecuteController")
|
||||
@RequestMapping("/api/ssw/sswflow/Step/Internal")
|
||||
@Tag(name = "Step 跨方执行入口", description = "Phase 2 跨方真分发:中央协调方把远端参与方的算子转调到本端执行")
|
||||
public class StepExecuteController {
|
||||
|
||||
/** 与 StepForwardingClient.APP_CODE 对齐 */
|
||||
public static final String EXPECTED_APP_CODE = "sswflow";
|
||||
|
||||
@Autowired
|
||||
private List<ComputeEngine> computeEngines;
|
||||
|
||||
@Value("${sswflow.cross-party.secret:}")
|
||||
private String crossPartySecret;
|
||||
|
||||
@SaIgnore
|
||||
@PostMapping("/Execute")
|
||||
@Operation(summary = "跨方算子执行入口",
|
||||
description = "中央协调方把「计算归属方=远端参与方」的算子转调本端执行;本端按自身节点模式真实跑算子")
|
||||
public ActionResult<StepResultRemote> execute(
|
||||
@RequestHeader(value = "App-Code", required = false) String appCode,
|
||||
@RequestHeader(value = "X-Internal-Token", required = false) String token,
|
||||
@RequestBody JSONObject payload) {
|
||||
// 1) 鉴权
|
||||
if (appCode == null || !EXPECTED_APP_CODE.equalsIgnoreCase(appCode)) {
|
||||
log.warn("[SSWFlow-STEP-INT] 拒绝跨方调用:App-Code 不匹配 actual={}", appCode);
|
||||
return ActionResult.fail("App-Code invalid");
|
||||
}
|
||||
if (crossPartySecret == null || crossPartySecret.isEmpty()) {
|
||||
log.debug("[SSWFlow-STEP-INT] sswflow.cross-party.secret 未配置,仅 App-Code 校验");
|
||||
} else if (!crossPartySecret.equals(token)) {
|
||||
log.warn("[SSWFlow-STEP-INT] 拒绝跨方调用:X-Internal-Token 不匹配");
|
||||
return ActionResult.fail("X-Internal-Token invalid");
|
||||
}
|
||||
if (payload == null) {
|
||||
return ActionResult.fail("payload 必填");
|
||||
}
|
||||
|
||||
// 2) 重建 StepContext(logger/progress 在远端为收集器,不向原 ctx 回传)
|
||||
StepContext ctx = toStepContext(payload);
|
||||
// [Phase 2 关键修复] 远端清掉 computePartySelfMark / computePartyEndpoint:
|
||||
// 转发过来的 ctx 仍带着「自己是远端、目标 endpoint=本方」的元数据,
|
||||
// 若不清掉,本方 RemoteNaxComputeEngine.execute 会再次转发到本方 → 递归死循环。
|
||||
// 本方既然被中央协调方转发到,本方就当 SELF 看待,按本方自己的节点模式本地实算即可。
|
||||
ctx.setComputePartySelfMark(1);
|
||||
ctx.setComputePartyEndpoint(null);
|
||||
List<StepResultRemote.LogEntry> collectedLogs = new ArrayList<>();
|
||||
ctx.setLogger((level, msg) -> {
|
||||
collectedLogs.add(StepResultRemote.LogEntry.of(level, msg));
|
||||
if ("INFO".equalsIgnoreCase(level)) {
|
||||
log.info("[SSWFlow-STEP-INT] step={} {}", ctx.getStepId(), msg);
|
||||
} else if ("WARN".equalsIgnoreCase(level)) {
|
||||
log.warn("[SSWFlow-STEP-INT] step={} {}", ctx.getStepId(), msg);
|
||||
} else if ("ERROR".equalsIgnoreCase(level)) {
|
||||
log.error("[SSWFlow-STEP-INT] step={} {}", ctx.getStepId(), msg);
|
||||
}
|
||||
});
|
||||
ctx.setProgress(p -> { /* 远端 progress 暂不向中心回传,避免 HTTP 双向流复杂度 */ });
|
||||
|
||||
// 3) 选引擎
|
||||
String operatorEngineType = ctx.getOperator() == null ? null : ctx.getOperator().getString("engine");
|
||||
final String engineType = (operatorEngineType == null || operatorEngineType.isEmpty()) ? "mpc" : operatorEngineType;
|
||||
final String mode = ctx.getComputePartyMode();
|
||||
ComputeEngine engine = computeEngines.stream()
|
||||
.filter(e -> e.supports(engineType, mode))
|
||||
.min(Comparator.comparingInt(ComputeEngine::order))
|
||||
.orElse(null);
|
||||
if (engine == null) {
|
||||
log.error("[SSWFlow-STEP-INT] 远端无可用引擎 engineType={} computeMode={}", engineType, mode);
|
||||
StepResult fail = StepResult.fail("远端无可用引擎: " + engineType + "/" + mode);
|
||||
return ActionResult.success(StepResultRemote.from(fail, collectedLogs, null, null));
|
||||
}
|
||||
|
||||
ctx.info(String.format("远端执行(Phase2 跨方)engine=%s operator=%s computePartyMode=%s",
|
||||
engine.name(), ctx.getOperatorKey(), mode));
|
||||
|
||||
// 4) 执行
|
||||
StepResult result;
|
||||
try {
|
||||
result = engine.execute(ctx);
|
||||
} catch (Exception e) {
|
||||
log.error("[SSWFlow-STEP-INT] 远端执行异常 step={} op={}", ctx.getStepId(), ctx.getOperatorKey(), e);
|
||||
result = StepResult.fail("远端执行异常: " + e.getMessage());
|
||||
collectedLogs.add(StepResultRemote.LogEntry.of("ERROR", result.getErrorMsg()));
|
||||
}
|
||||
|
||||
return ActionResult.success(StepResultRemote.from(
|
||||
result, collectedLogs,
|
||||
ctx.getParams() == null ? null : ctx.getParams().getString("__remotePartyCode__"),
|
||||
engine.name()));
|
||||
}
|
||||
|
||||
/** payload → StepContext(远端 logger/progress 单独在 execute() 内安装) */
|
||||
private StepContext toStepContext(JSONObject p) {
|
||||
JSONObject op = p.getJSONObject("operator");
|
||||
JSONObject params = p.getJSONObject("params");
|
||||
Map<String, List<JSONObject>> inputs = new HashMap<>();
|
||||
JSONObject inputsRaw = p.getJSONObject("inputs");
|
||||
if (inputsRaw != null) {
|
||||
for (var e : inputsRaw.entrySet()) {
|
||||
List<JSONObject> list = new ArrayList<>();
|
||||
Object rawVal = e.getValue();
|
||||
// [Phase 2.1 修复] fastjson2 反序列化嵌套数组有时落到 java.util.List
|
||||
// 而非 JSONArray(取决于 AutoType 配置),用 instanceof 双兜底
|
||||
if (rawVal instanceof JSONArray) {
|
||||
JSONArray arr = (JSONArray) rawVal;
|
||||
for (int i = 0; i < arr.size(); i++) {
|
||||
list.add(arr.getJSONObject(i));
|
||||
}
|
||||
} else if (rawVal instanceof java.util.List) {
|
||||
for (Object item : (java.util.List<?>) rawVal) {
|
||||
if (item instanceof JSONObject) {
|
||||
list.add((JSONObject) item);
|
||||
} else if (item instanceof java.util.Map) {
|
||||
list.add(new JSONObject((java.util.Map<?, ?>) item));
|
||||
}
|
||||
}
|
||||
}
|
||||
inputs.put(e.getKey(), list);
|
||||
}
|
||||
}
|
||||
Map<String, JSONObject> datasets = new HashMap<>();
|
||||
JSONObject datasetsRaw = p.getJSONObject("datasets");
|
||||
if (datasetsRaw != null) {
|
||||
for (var e : datasetsRaw.entrySet()) {
|
||||
Object v = e.getValue();
|
||||
if (v instanceof JSONObject) {
|
||||
datasets.put(e.getKey(), (JSONObject) v);
|
||||
} else if (v instanceof java.util.Map) {
|
||||
// fastjson2 默认反序列化嵌套对象为 Map 时显式转 JSONObject
|
||||
datasets.put(e.getKey(), new JSONObject((java.util.Map<?, ?>) v));
|
||||
}
|
||||
}
|
||||
}
|
||||
List<JSONObject> parties = new ArrayList<>();
|
||||
JSONArray partiesArr = p.getJSONArray("parties");
|
||||
if (partiesArr != null) {
|
||||
for (int i = 0; i < partiesArr.size(); i++) {
|
||||
parties.add(partiesArr.getJSONObject(i));
|
||||
}
|
||||
}
|
||||
return StepContext.builder()
|
||||
.runId(p.getString("runId"))
|
||||
.stepId(p.getString("stepId"))
|
||||
.operatorKey(p.getString("operatorKey"))
|
||||
.operator(op)
|
||||
.params(params)
|
||||
.inputs(inputs)
|
||||
.parties(parties)
|
||||
.datasets(datasets)
|
||||
.workDir(p.getString("workDir"))
|
||||
.computePartyMode(p.getString("computePartyMode"))
|
||||
.computePartyNodeConfig(p.getJSONObject("computePartyNodeConfig"))
|
||||
.computePartySelfMark(p.getInteger("computePartySelfMark"))
|
||||
.computePartyEndpoint(p.getString("computePartyEndpoint"))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -61,6 +61,29 @@ public class PartyEntity extends SuperExtendEntity<String> {
|
||||
@TableField("f_cert_pem")
|
||||
private String certPem;
|
||||
|
||||
/**
|
||||
* 计算/节点模式(参与方节点信息核心字段):
|
||||
* <ul>
|
||||
* <li>{@code LOCAL_SIM} —— 纯本地模拟(无真实隐私计算框架,由 SimulationComputeEngine 造假产物)</li>
|
||||
* <li>{@code K8S_SIM} —— 部署 k8s 模拟(本方 TDC 沙盒 NaxJob 跑真实 Nax,数据不出域)</li>
|
||||
* <li>{@code CONNECTOR} —— 接入真实连接器(scard 连接器 + Nax2PJob 两方真算)</li>
|
||||
* </ul>
|
||||
* 为空时引擎回退到全局 {@code sswflow.nax.remote} 开关(向后兼容历史部署)。
|
||||
*/
|
||||
@TableField("f_compute_mode")
|
||||
private String computeMode;
|
||||
|
||||
/**
|
||||
* 节点配置 JSON,按 {@link #computeMode} 填不同内容:
|
||||
* <ul>
|
||||
* <li>K8S_SIM → {"namespace","naxImage","kubectlBin","timeoutMs"}</li>
|
||||
* <li>CONNECTOR → {"connectorId","connectorMode","endpoint"}</li>
|
||||
* <li>LOCAL_SIM → 可空</li>
|
||||
* </ul>
|
||||
*/
|
||||
@TableField("f_node_config")
|
||||
private String nodeConfig;
|
||||
|
||||
/** 描述 */
|
||||
@TableField("f_desc")
|
||||
private String desc;
|
||||
|
||||
Reference in new issue
Block a user