Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69d8178a83 |
@@ -1,37 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""iAOP-Core · AI 模型框架(AI Model Framework)—— 配置驱动的可模板化模型内核。
|
|
||||||
|
|
||||||
对应 PRD 5.3「③ AI 模型框架」与 EPIC #5(平台化改造 RISK 项):
|
|
||||||
固定主干网络 + 可配置超参;Model Recipe 插件注册;FeatureSpec 声明式特征;
|
|
||||||
所有可变量外置为 **JSON 超参包**,同一框架切换模板仅改此包(零改码)。
|
|
||||||
|
|
||||||
模块组成(按 EPIC #5 拆分的 ≤0.5d 子任务逐步落地):
|
|
||||||
- hyperparam 超参包 JSON Schema 校验器(Issue #39):加载 + 校验超参包,
|
|
||||||
返回逐条校验报告,供配置台与训练/推理流水线复用。
|
|
||||||
- (后续)recipe Model Recipe 插件接口与样例协议(Issue #34)
|
|
||||||
- (后续)feature FeatureSpec 声明式特征定义引擎(Issue #35)
|
|
||||||
- (后续)registry 模型模板注册 / 加载 / 版本机制(Issue #41)
|
|
||||||
|
|
||||||
超参包 JSON 结构见 PRD 5.3「超参包 JSON 完整示例」与 ``config/`` 下样例。
|
|
||||||
|
|
||||||
测试:`python -m unittest discover -s tests -v`(在 core/model-framework 目录下执行)。
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
from .hyperparam import (
|
|
||||||
HyperparamPack,
|
|
||||||
ValidationIssue,
|
|
||||||
ValidationReport,
|
|
||||||
load_pack,
|
|
||||||
validate_pack,
|
|
||||||
validate_pack_file,
|
|
||||||
)
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"HyperparamPack",
|
|
||||||
"ValidationIssue",
|
|
||||||
"ValidationReport",
|
|
||||||
"load_pack",
|
|
||||||
"validate_pack",
|
|
||||||
"validate_pack_file",
|
|
||||||
]
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
{
|
|
||||||
"model_id": "quality_predict_ti",
|
|
||||||
"template": "iAOP-Template-Ti",
|
|
||||||
"algorithm": "xgboost",
|
|
||||||
"features": [
|
|
||||||
{"name": "EMA_CLF_TEMP_5m", "spec": "EMA(CLF-01.TEMP, 5m)"},
|
|
||||||
{"name": "RollingStd_CL2_10", "spec": "RollingStd(CLF-01.CL2, 10)"},
|
|
||||||
{"name": "ROC_FURNACE_PRESS", "spec": "RateOfChange(CLF-01.PRES)"}
|
|
||||||
],
|
|
||||||
"target": "Ti_purity",
|
|
||||||
"objective": "reg:squarederror",
|
|
||||||
"hyperparams": {"max_depth": 6, "eta": 0.1, "n_estimators": 300},
|
|
||||||
"train_window": "180d",
|
|
||||||
"alarm_threshold": {"type": "zscore", "k": 3.0},
|
|
||||||
"drift_check": {"method": "psi", "limit": 0.2}
|
|
||||||
}
|
|
||||||
@@ -1,339 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""超参包(Hyperparam Pack)JSON Schema 校验器。
|
|
||||||
|
|
||||||
对应 issue #39(父 EPIC #5「③ AI 模型框架 配置化重构」)与 PRD 5.3
|
|
||||||
「超参包驱动」:所有可变量(输入特征清单、算法选型、超参、训练窗口、告警阈值、
|
|
||||||
目标函数)外置为 JSON 超参包,同一框架切换模板仅改此包。
|
|
||||||
|
|
||||||
本模块对超参包做 **结构化校验**(不依赖 jsonschema 第三方库,零外部依赖,
|
|
||||||
便于边缘 / 离线环境运行),返回逐条问题的校验报告,便于配置台一次性展示全部问题、
|
|
||||||
也便于训练/推理流水线在加载包时 fail-fast。
|
|
||||||
|
|
||||||
校验维度(对齐 PRD 5.3 超参包示例与 5.3「配置点」):
|
|
||||||
1. 顶层结构:必填字段 model_id / template / algorithm / features / target;
|
|
||||||
2. model_id / template:非空字符串;
|
|
||||||
3. algorithm:必须在合法算法集合内(xgboost / lightgbm / dnn / lstm / gnn /
|
|
||||||
isolation_forest / zscore / linear / ridge);未知算法报错并提示已知项
|
|
||||||
(对齐 PRD「新增结构走插件注册」——校验期即暴露非法选型);
|
|
||||||
4. features:非空列表;每项含 name(非空、包内唯一)与 spec(非空字符串,
|
|
||||||
FeatureSpec 文本,由 issue #35 引擎解释,此处只做存在性校验);
|
|
||||||
5. target:非空字符串;
|
|
||||||
6. objective(可选):若填写必须为已知目标函数(reg:squarederror /
|
|
||||||
binary:logistic / multi:softmax / regression / classification 等);
|
|
||||||
7. hyperparams(可选):若提供必须为对象(dict),且不含空键;
|
|
||||||
8. train_window(可选):若填写必须形如 ``<正整数><单位>``(单位 d/w/h/m),
|
|
||||||
如 ``180d`` / ``4w``;
|
|
||||||
9. alarm_threshold(可选):若提供必须为对象,且含 ``type``
|
|
||||||
(zscore / quantile / absolute)与对应阈值键;
|
|
||||||
10. drift_check(可选):若提供必须为对象,含 ``method``(psi / ks / chi2)
|
|
||||||
与 ``limit``(0~1 之间)。
|
|
||||||
|
|
||||||
注:返回报告而非抛异常,便于配置台聚合展示;``load_pack`` 在校验失败时抛
|
|
||||||
``ValueError`` 供流水线 fail-fast。
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
import re
|
|
||||||
from dataclasses import dataclass, field
|
|
||||||
from typing import Any, Dict, List, Optional, Tuple
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 合法取值集合(对齐 PRD 5.3「四类模型模板」+ 默认主干网络 + Recipe 插件扩展点)
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
#: 已注册算法(默认主干 + Recipe 可选结构)。新增结构应走插件注册(issue #34/#41),
|
|
||||||
#: 此处枚举的是内核自带项;校验期遇到未知 algorithm 会报错并列出已知项,避免
|
|
||||||
#: 静默落到错误分支。
|
|
||||||
KNOWN_ALGORITHMS: Tuple[str, ...] = (
|
|
||||||
# 监督回归 / 分类(质量预测)
|
|
||||||
"xgboost",
|
|
||||||
"lightgbm",
|
|
||||||
"linear",
|
|
||||||
"ridge",
|
|
||||||
# 神经网络主干(默认 DNN;Recipe 可选 LSTM/GNN)
|
|
||||||
"dnn",
|
|
||||||
"lstm",
|
|
||||||
"gnn",
|
|
||||||
# 无监督异常 / 杂质预警
|
|
||||||
"isolation_forest",
|
|
||||||
"zscore",
|
|
||||||
)
|
|
||||||
|
|
||||||
#: 已知目标函数(xgboost 风格 + 通用风格)。
|
|
||||||
KNOWN_OBJECTIVES: Tuple[str, ...] = (
|
|
||||||
"reg:squarederror",
|
|
||||||
"reg:squaredlogerror",
|
|
||||||
"binary:logistic",
|
|
||||||
"multi:softmax",
|
|
||||||
"multi:softprob",
|
|
||||||
"regression",
|
|
||||||
"classification",
|
|
||||||
)
|
|
||||||
|
|
||||||
#: 训练窗口合法时间单位。
|
|
||||||
_TRAIN_WINDOW_RE = re.compile(r"^\d+(\.\d+)?[dwhm]$")
|
|
||||||
#: FeatureSpec 仅校验非空文本(具体语法由 issue #35 引擎解释)。
|
|
||||||
_FEATURE_NAME_RE = re.compile(r"^[A-Za-z_][A-Za-z0-9_]*$")
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ValidationIssue:
|
|
||||||
"""单条校验问题。"""
|
|
||||||
|
|
||||||
code: str # 错误码:missing_field / bad_type / unknown_algorithm / ...
|
|
||||||
path: str # JSON 路径,如 ``features[1].name`` / ``algorithm``
|
|
||||||
message: str # 人类可读描述
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class ValidationReport:
|
|
||||||
"""校验报告。"""
|
|
||||||
|
|
||||||
issues: List[ValidationIssue] = field(default_factory=list)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def ok(self) -> bool:
|
|
||||||
return not self.issues
|
|
||||||
|
|
||||||
def summary(self) -> str:
|
|
||||||
if self.ok:
|
|
||||||
return "超参包校验通过"
|
|
||||||
by_code: Dict[str, int] = {}
|
|
||||||
for it in self.issues:
|
|
||||||
by_code[it.code] = by_code.get(it.code, 0) + 1
|
|
||||||
parts = [f"{code}×{n}" for code, n in sorted(by_code.items())]
|
|
||||||
return f"超参包校验未通过({len(self.issues)} 个问题:" + ",".join(parts) + ")"
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class HyperparamPack:
|
|
||||||
"""解析后的超参包(校验通过的视图)。
|
|
||||||
|
|
||||||
字段对齐 PRD 5.3 超参包示例;可选字段缺失时为 ``None``。
|
|
||||||
"""
|
|
||||||
|
|
||||||
model_id: str
|
|
||||||
template: str
|
|
||||||
algorithm: str
|
|
||||||
features: List[Dict[str, str]]
|
|
||||||
target: str
|
|
||||||
objective: Optional[str] = None
|
|
||||||
hyperparams: Optional[Dict[str, Any]] = None
|
|
||||||
train_window: Optional[str] = None
|
|
||||||
alarm_threshold: Optional[Dict[str, Any]] = None
|
|
||||||
drift_check: Optional[Dict[str, Any]] = None
|
|
||||||
raw: Dict[str, Any] = field(default_factory=dict)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def feature_names(self) -> List[str]:
|
|
||||||
return [f["name"] for f in self.features]
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# 校验主逻辑
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
def validate_pack(pack: Dict[str, Any]) -> ValidationReport:
|
|
||||||
"""校验一个已解析为 dict 的超参包,返回报告(不抛异常)。"""
|
|
||||||
report = ValidationReport()
|
|
||||||
|
|
||||||
def add(code: str, path: str, message: str) -> None:
|
|
||||||
report.issues.append(ValidationIssue(code, path, message))
|
|
||||||
|
|
||||||
# 0. 顶层必须是对象
|
|
||||||
if not isinstance(pack, dict):
|
|
||||||
add("bad_type", "$", f"超参包根节点必须为 JSON 对象,实际为 {type(pack).__name__}")
|
|
||||||
return report
|
|
||||||
|
|
||||||
# 1. 必填字段
|
|
||||||
required: Tuple[str, ...] = ("model_id", "template", "algorithm", "features", "target")
|
|
||||||
for key in required:
|
|
||||||
if key not in pack:
|
|
||||||
add("missing_field", key, f"缺少必填字段:{key}")
|
|
||||||
|
|
||||||
# 2. 标量字段类型与取值
|
|
||||||
model_id = pack.get("model_id")
|
|
||||||
if "model_id" in pack:
|
|
||||||
if not isinstance(model_id, str) or not model_id.strip():
|
|
||||||
add("bad_type", "model_id", "model_id 必须为非空字符串")
|
|
||||||
elif not _FEATURE_NAME_RE.match(model_id):
|
|
||||||
add("bad_format", "model_id", "model_id 含非法字符(仅字母/数字/下划线,首字符非数字)")
|
|
||||||
|
|
||||||
template = pack.get("template")
|
|
||||||
if "template" in pack and (not isinstance(template, str) or not template.strip()):
|
|
||||||
add("bad_type", "template", "template 必须为非空字符串")
|
|
||||||
|
|
||||||
algorithm = pack.get("algorithm")
|
|
||||||
if "algorithm" in pack:
|
|
||||||
if not isinstance(algorithm, str) or not algorithm.strip():
|
|
||||||
add("bad_type", "algorithm", "algorithm 必须为非空字符串")
|
|
||||||
elif algorithm not in KNOWN_ALGORITHMS:
|
|
||||||
known = "、".join(KNOWN_ALGORITHMS)
|
|
||||||
add(
|
|
||||||
"unknown_algorithm",
|
|
||||||
"algorithm",
|
|
||||||
f"未知算法 '{algorithm}';已知项:{known}(新增结构请走 Model Recipe 插件注册)",
|
|
||||||
)
|
|
||||||
|
|
||||||
target = pack.get("target")
|
|
||||||
if "target" in pack and (not isinstance(target, str) or not target.strip()):
|
|
||||||
add("bad_type", "target", "target 必须为非空字符串")
|
|
||||||
|
|
||||||
# 3. objective(可选)
|
|
||||||
objective = pack.get("objective")
|
|
||||||
if objective is not None:
|
|
||||||
if not isinstance(objective, str) or not objective.strip():
|
|
||||||
add("bad_type", "objective", "objective 若填写必须为非空字符串")
|
|
||||||
elif objective not in KNOWN_OBJECTIVES:
|
|
||||||
known = "、".join(KNOWN_OBJECTIVES)
|
|
||||||
add("unknown_objective", "objective", f"未知目标函数 '{objective}';已知项:{known}")
|
|
||||||
|
|
||||||
# 4. features(必填,非空列表)
|
|
||||||
features = pack.get("features")
|
|
||||||
if features is None:
|
|
||||||
# missing_field 已在步骤 1 记录
|
|
||||||
pass
|
|
||||||
elif not isinstance(features, list):
|
|
||||||
add("bad_type", "features", f"features 必须为数组,实际为 {type(features).__name__}")
|
|
||||||
elif len(features) == 0:
|
|
||||||
add("empty_features", "features", "features 不能为空(模型至少需要一个输入特征)")
|
|
||||||
else:
|
|
||||||
seen_names: Dict[str, int] = {}
|
|
||||||
for i, feat in enumerate(features):
|
|
||||||
fpath = f"features[{i}]"
|
|
||||||
if not isinstance(feat, dict):
|
|
||||||
add("bad_type", fpath, f"特征项必须为对象,实际为 {type(feat).__name__}")
|
|
||||||
continue
|
|
||||||
name = feat.get("name")
|
|
||||||
spec = feat.get("spec")
|
|
||||||
if not isinstance(name, str) or not name.strip():
|
|
||||||
add("missing_field", f"{fpath}.name", "特征缺少 name 或为空")
|
|
||||||
else:
|
|
||||||
if not _FEATURE_NAME_RE.match(name):
|
|
||||||
add("bad_format", f"{fpath}.name", f"特征名 '{name}' 含非法字符")
|
|
||||||
if name in seen_names:
|
|
||||||
add(
|
|
||||||
"dup_feature",
|
|
||||||
f"{fpath}.name",
|
|
||||||
f"特征名 '{name}' 重复(首次出现在 features[{seen_names[name]}])",
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
seen_names[name] = i
|
|
||||||
if not isinstance(spec, str) or not spec.strip():
|
|
||||||
add("missing_field", f"{fpath}.spec", f"特征 '{name}' 缺少 spec(FeatureSpec 声明)或为空")
|
|
||||||
|
|
||||||
# 5. hyperparams(可选,对象)
|
|
||||||
hyperparams = pack.get("hyperparams")
|
|
||||||
if hyperparams is not None:
|
|
||||||
if not isinstance(hyperparams, dict):
|
|
||||||
add("bad_type", "hyperparams", f"hyperparams 必须为对象,实际为 {type(hyperparams).__name__}")
|
|
||||||
else:
|
|
||||||
for k, v in hyperparams.items():
|
|
||||||
if not isinstance(k, str) or not k.strip():
|
|
||||||
add("bad_format", "hyperparams", "hyperparams 含空键")
|
|
||||||
|
|
||||||
# 6. train_window(可选,<数><单位>)
|
|
||||||
train_window = pack.get("train_window")
|
|
||||||
if train_window is not None:
|
|
||||||
if not isinstance(train_window, str) or not _TRAIN_WINDOW_RE.match(train_window):
|
|
||||||
add(
|
|
||||||
"bad_format",
|
|
||||||
"train_window",
|
|
||||||
"train_window 必须形如 '<正数><单位>'(单位 d/w/h/m),如 '180d'、'4w'",
|
|
||||||
)
|
|
||||||
|
|
||||||
# 7. alarm_threshold(可选,对象,含 type)
|
|
||||||
alarm = pack.get("alarm_threshold")
|
|
||||||
if alarm is not None:
|
|
||||||
if not isinstance(alarm, dict):
|
|
||||||
add("bad_type", "alarm_threshold", f"alarm_threshold 必须为对象,实际为 {type(alarm).__name__}")
|
|
||||||
else:
|
|
||||||
atype = alarm.get("type")
|
|
||||||
known_alarm_types = ("zscore", "quantile", "absolute")
|
|
||||||
if not isinstance(atype, str) or atype not in known_alarm_types:
|
|
||||||
add(
|
|
||||||
"unknown_alarm_type",
|
|
||||||
"alarm_threshold.type",
|
|
||||||
f"alarm_threshold.type 必须为 {known_alarm_types} 之一",
|
|
||||||
)
|
|
||||||
if atype == "zscore" and "k" not in alarm:
|
|
||||||
add("missing_field", "alarm_threshold.k", "zscore 阈值缺少 k")
|
|
||||||
if atype == "quantile" and "q" not in alarm:
|
|
||||||
add("missing_field", "alarm_threshold.q", "quantile 阈值缺少 q")
|
|
||||||
if atype == "absolute" and "value" not in alarm:
|
|
||||||
add("missing_field", "alarm_threshold.value", "absolute 阈值缺少 value")
|
|
||||||
|
|
||||||
# 8. drift_check(可选,对象,method + limit)
|
|
||||||
drift = pack.get("drift_check")
|
|
||||||
if drift is not None:
|
|
||||||
if not isinstance(drift, dict):
|
|
||||||
add("bad_type", "drift_check", f"drift_check 必须为对象,实际为 {type(drift).__name__}")
|
|
||||||
else:
|
|
||||||
method = drift.get("method")
|
|
||||||
known_methods = ("psi", "ks", "chi2")
|
|
||||||
if not isinstance(method, str) or method not in known_methods:
|
|
||||||
add(
|
|
||||||
"unknown_drift_method",
|
|
||||||
"drift_check.method",
|
|
||||||
f"drift_check.method 必须为 {known_methods} 之一",
|
|
||||||
)
|
|
||||||
limit = drift.get("limit")
|
|
||||||
if limit is None:
|
|
||||||
add("missing_field", "drift_check.limit", "drift_check 缺少 limit")
|
|
||||||
elif not isinstance(limit, (int, float)) or isinstance(limit, bool):
|
|
||||||
add("bad_type", "drift_check.limit", "drift_check.limit 必须为数值")
|
|
||||||
elif not (0 < limit <= 1):
|
|
||||||
add("bad_range", "drift_check.limit", "drift_check.limit 必须在 (0, 1] 范围内")
|
|
||||||
|
|
||||||
return report
|
|
||||||
|
|
||||||
|
|
||||||
def load_pack(pack: Dict[str, Any]) -> HyperparamPack:
|
|
||||||
"""校验并把 dict 装配为 :class:`HyperparamPack`;校验失败抛 ``ValueError``。
|
|
||||||
|
|
||||||
供训练 / 推理流水线在加载超参包时 fail-fast 使用。
|
|
||||||
"""
|
|
||||||
report = validate_pack(pack)
|
|
||||||
if not report.ok:
|
|
||||||
raise ValueError(report.summary())
|
|
||||||
|
|
||||||
return HyperparamPack(
|
|
||||||
model_id=pack["model_id"],
|
|
||||||
template=pack["template"],
|
|
||||||
algorithm=pack["algorithm"],
|
|
||||||
features=list(pack["features"]),
|
|
||||||
target=pack["target"],
|
|
||||||
objective=pack.get("objective"),
|
|
||||||
hyperparams=pack.get("hyperparams"),
|
|
||||||
train_window=pack.get("train_window"),
|
|
||||||
alarm_threshold=pack.get("alarm_threshold"),
|
|
||||||
drift_check=pack.get("drift_check"),
|
|
||||||
raw=dict(pack),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def validate_pack_file(path: str) -> ValidationReport:
|
|
||||||
"""读取 JSON 文件并校验;文件 / JSON 解析错误也记入报告(不抛异常)。"""
|
|
||||||
report = ValidationReport()
|
|
||||||
|
|
||||||
def add(code: str, msg: str) -> None:
|
|
||||||
report.issues.append(ValidationIssue(code, "$", msg))
|
|
||||||
|
|
||||||
if not os.path.exists(path):
|
|
||||||
add("file_not_found", f"超参包文件不存在:{path}")
|
|
||||||
return report
|
|
||||||
try:
|
|
||||||
with open(path, "r", encoding="utf-8") as fh:
|
|
||||||
text = fh.read()
|
|
||||||
except OSError as exc:
|
|
||||||
add("file_read_error", f"读取超参包失败:{exc}")
|
|
||||||
return report
|
|
||||||
try:
|
|
||||||
pack = json.loads(text)
|
|
||||||
except json.JSONDecodeError as exc:
|
|
||||||
add("json_parse_error", f"超参包不是合法 JSON:{exc}")
|
|
||||||
return report
|
|
||||||
|
|
||||||
inner = validate_pack(pack)
|
|
||||||
report.issues.extend(inner.issues)
|
|
||||||
return report
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""测试引导:把 `core/model-framework` 以包名 `model_framework` 挂载到 sys.modules。
|
|
||||||
|
|
||||||
目录名 `model-framework` 含连字符,无法直接以包名 import;挂载后模块内相对导入
|
|
||||||
(`from .hyperparam import ...`)在 unittest 发现机制下可正常解析。
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import types
|
|
||||||
|
|
||||||
MF_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
||||||
sys.path.insert(0, MF_DIR)
|
|
||||||
if "model_framework" not in sys.modules:
|
|
||||||
pkg = types.ModuleType("model_framework")
|
|
||||||
pkg.__path__ = [MF_DIR]
|
|
||||||
sys.modules["model_framework"] = pkg
|
|
||||||
@@ -1,239 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""超参包 JSON Schema 校验器测试(issue #39)。
|
|
||||||
|
|
||||||
覆盖:合法包通过 / 必填缺失 / 未知算法 / 特征重复与缺 spec / 目标函数 /
|
|
||||||
train_window 格式 / alarm_threshold / drift_check / 文件加载与 JSON 解析错误。
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
import _bootstrap # noqa: F401 挂载包名
|
|
||||||
|
|
||||||
from model_framework.hyperparam import (
|
|
||||||
HyperparamPack,
|
|
||||||
load_pack,
|
|
||||||
validate_pack,
|
|
||||||
validate_pack_file,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _good_pack():
|
|
||||||
"""PRD 5.3 示例的合法超参包。"""
|
|
||||||
return {
|
|
||||||
"model_id": "quality_predict_ti",
|
|
||||||
"template": "iAOP-Template-Ti",
|
|
||||||
"algorithm": "xgboost",
|
|
||||||
"features": [
|
|
||||||
{"name": "EMA_CLF_TEMP_5m", "spec": "EMA(CLF-01.TEMP, 5m)"},
|
|
||||||
{"name": "RollingStd_CL2_10", "spec": "RollingStd(CLF-01.CL2, 10)"},
|
|
||||||
],
|
|
||||||
"target": "Ti_purity",
|
|
||||||
"objective": "reg:squarederror",
|
|
||||||
"hyperparams": {"max_depth": 6, "eta": 0.1, "n_estimators": 300},
|
|
||||||
"train_window": "180d",
|
|
||||||
"alarm_threshold": {"type": "zscore", "k": 3.0},
|
|
||||||
"drift_check": {"method": "psi", "limit": 0.2},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class ValidatePackTest(unittest.TestCase):
|
|
||||||
def test_good_pack_passes(self):
|
|
||||||
report = validate_pack(_good_pack())
|
|
||||||
self.assertTrue(report.ok, report.summary())
|
|
||||||
self.assertEqual(report.issues, [])
|
|
||||||
|
|
||||||
def test_minimal_pack_passes(self):
|
|
||||||
report = validate_pack(
|
|
||||||
{
|
|
||||||
"model_id": "anomaly_resin",
|
|
||||||
"template": "iAOP-Template-Resin",
|
|
||||||
"algorithm": "isolation_forest",
|
|
||||||
"features": [{"name": "F1", "spec": "RateOfChange(炉压)"}],
|
|
||||||
"target": "anomaly_score",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
self.assertTrue(report.ok, report.summary())
|
|
||||||
|
|
||||||
def test_missing_required_field(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
del pack["algorithm"]
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
self.assertIn("missing_field", codes)
|
|
||||||
self.assertTrue(any(i.path == "algorithm" for i in report.issues))
|
|
||||||
|
|
||||||
def test_missing_all_required(self):
|
|
||||||
report = validate_pack({})
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
# 5 个必填字段全部缺失
|
|
||||||
self.assertEqual(codes.count("missing_field"), 5)
|
|
||||||
|
|
||||||
def test_unknown_algorithm_lists_known(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["algorithm"] = "magic_boost"
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
issue = next(i for i in report.issues if i.code == "unknown_algorithm")
|
|
||||||
self.assertIn("xgboost", issue.message)
|
|
||||||
self.assertIn("magic_boost", issue.message)
|
|
||||||
|
|
||||||
def test_empty_features_rejected(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["features"] = []
|
|
||||||
report = validate_pack(pack)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
self.assertIn("empty_features", codes)
|
|
||||||
|
|
||||||
def test_duplicate_feature_name(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["features"] = [
|
|
||||||
{"name": "F1", "spec": "EMA(A, 5m)"},
|
|
||||||
{"name": "F1", "spec": "EMA(B, 5m)"},
|
|
||||||
]
|
|
||||||
report = validate_pack(pack)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
self.assertIn("dup_feature", codes)
|
|
||||||
|
|
||||||
def test_feature_missing_spec(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["features"] = [{"name": "F1", "spec": ""}]
|
|
||||||
report = validate_pack(pack)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
self.assertIn("missing_field", codes)
|
|
||||||
|
|
||||||
def test_feature_bad_name_format(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["features"] = [{"name": "1bad", "spec": "EMA(A,5m)"}]
|
|
||||||
report = validate_pack(pack)
|
|
||||||
codes = [i.code for i in report.issues]
|
|
||||||
self.assertIn("bad_format", codes)
|
|
||||||
|
|
||||||
def test_unknown_objective(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["objective"] = "reg:magic"
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(any(i.code == "unknown_objective" for i in report.issues))
|
|
||||||
|
|
||||||
def test_bad_train_window_format(self):
|
|
||||||
for bad in ["180", "180days", "abc", "", "1y"]:
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["train_window"] = bad
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(
|
|
||||||
any(i.code == "bad_format" and i.path == "train_window" for i in report.issues),
|
|
||||||
f"应拒绝非法 train_window: {bad!r}",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_good_train_window_units(self):
|
|
||||||
for good in ["180d", "4w", "72h", "30m"]:
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["train_window"] = good
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(report.ok, f"应接受合法 train_window: {good!r} -> {report.summary()}")
|
|
||||||
|
|
||||||
def test_alarm_threshold_zscore_missing_k(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["alarm_threshold"] = {"type": "zscore"}
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(any(i.code == "missing_field" and i.path == "alarm_threshold.k" for i in report.issues))
|
|
||||||
|
|
||||||
def test_alarm_threshold_unknown_type(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["alarm_threshold"] = {"type": "voodoo"}
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(any(i.code == "unknown_alarm_type" for i in report.issues))
|
|
||||||
|
|
||||||
def test_drift_check_limit_range(self):
|
|
||||||
for bad_limit in [0, 1.5, -0.1]:
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["drift_check"] = {"method": "psi", "limit": bad_limit}
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(
|
|
||||||
any(i.code == "bad_range" for i in report.issues),
|
|
||||||
f"应拒绝非法 drift_check.limit: {bad_limit}",
|
|
||||||
)
|
|
||||||
|
|
||||||
def test_drift_check_unknown_method(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
pack["drift_check"] = {"method": "magic", "limit": 0.2}
|
|
||||||
report = validate_pack(pack)
|
|
||||||
self.assertTrue(any(i.code == "unknown_drift_method" for i in report.issues))
|
|
||||||
|
|
||||||
def test_root_not_object(self):
|
|
||||||
report = validate_pack([1, 2, 3]) # type: ignore[arg-type]
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
self.assertEqual(report.issues[0].code, "bad_type")
|
|
||||||
|
|
||||||
def test_report_summary_ok(self):
|
|
||||||
self.assertEqual(validate_pack(_good_pack()).summary(), "超参包校验通过")
|
|
||||||
|
|
||||||
def test_report_summary_aggregates(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
del pack["algorithm"]
|
|
||||||
del pack["target"]
|
|
||||||
summary = validate_pack(pack).summary()
|
|
||||||
self.assertIn("2 个问题", summary)
|
|
||||||
self.assertIn("missing_field", summary)
|
|
||||||
|
|
||||||
|
|
||||||
class LoadPackTest(unittest.TestCase):
|
|
||||||
def test_load_good_returns_pack(self):
|
|
||||||
pack = load_pack(_good_pack())
|
|
||||||
self.assertIsInstance(pack, HyperparamPack)
|
|
||||||
self.assertEqual(pack.model_id, "quality_predict_ti")
|
|
||||||
self.assertEqual(pack.algorithm, "xgboost")
|
|
||||||
self.assertEqual(pack.feature_names, ["EMA_CLF_TEMP_5m", "RollingStd_CL2_10"])
|
|
||||||
self.assertEqual(pack.hyperparams["max_depth"], 6)
|
|
||||||
|
|
||||||
def test_load_bad_raises(self):
|
|
||||||
pack = _good_pack()
|
|
||||||
del pack["features"]
|
|
||||||
with self.assertRaises(ValueError) as cm:
|
|
||||||
load_pack(pack)
|
|
||||||
self.assertIn("超参包校验未通过", str(cm.exception))
|
|
||||||
|
|
||||||
|
|
||||||
class ValidatePackFileTest(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
self._tmp = tempfile.mkdtemp()
|
|
||||||
self.path = os.path.join(self._tmp, "pack.json")
|
|
||||||
|
|
||||||
def _write(self, text):
|
|
||||||
with open(self.path, "w", encoding="utf-8") as fh:
|
|
||||||
fh.write(text)
|
|
||||||
return self.path
|
|
||||||
|
|
||||||
def test_valid_file_passes(self):
|
|
||||||
import json
|
|
||||||
|
|
||||||
self._write(json.dumps(_good_pack()))
|
|
||||||
report = validate_pack_file(self.path)
|
|
||||||
self.assertTrue(report.ok, report.summary())
|
|
||||||
|
|
||||||
def test_missing_file(self):
|
|
||||||
report = validate_pack_file(os.path.join(self._tmp, "nope.json"))
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
self.assertEqual(report.issues[0].code, "file_not_found")
|
|
||||||
|
|
||||||
def test_bad_json(self):
|
|
||||||
self._write("{ not json ")
|
|
||||||
report = validate_pack_file(self.path)
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
self.assertEqual(report.issues[0].code, "json_parse_error")
|
|
||||||
|
|
||||||
def test_file_with_schema_errors(self):
|
|
||||||
import json
|
|
||||||
|
|
||||||
bad = _good_pack()
|
|
||||||
bad["algorithm"] = "unknown_thing"
|
|
||||||
self._write(json.dumps(bad))
|
|
||||||
report = validate_pack_file(self.path)
|
|
||||||
self.assertFalse(report.ok)
|
|
||||||
self.assertEqual(report.issues[0].code, "unknown_algorithm")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
# iAOP 可用性 / 容灾演练套件(tests/ha)
|
||||||
|
|
||||||
|
> 对应 Issue **#89 [M4] 可用性/容灾演练**,验证 PRD 第 9 章 NFR「可用性 ≥ 99.8%」
|
||||||
|
> 以及断点续传 / 故障隔离等容灾能力。
|
||||||
|
|
||||||
|
## 背景
|
||||||
|
|
||||||
|
PRD 第 9 章 SLA 对边缘采集网关提出可用性 ≥ 99.8%、丢失率 ≤ 0.02% 的硬指标。
|
||||||
|
这两项指标的工程保障来自三个机制:
|
||||||
|
|
||||||
|
1. **spool 断点续传**:样本先落本地磁盘,上行确认(ack)后才删除;
|
||||||
|
网关重启时扫描 spool 目录,未确认记录全部重发(RPO=0)。
|
||||||
|
2. **驱动故障隔离**:单驱动异常按「本轮整体失败」计入 HealthMetrics,
|
||||||
|
不抛出、不崩溃,正常轮次继续产出。
|
||||||
|
3. **上行通道背压**:Kafka 抖动期间样本驻留 spool,仅增加本地缓存占用,
|
||||||
|
恢复后断点续传,数据不丢不重。
|
||||||
|
|
||||||
|
本套件以**可重复的合成数据集 + 故障注入驱动**对上述机制做规模化演练,
|
||||||
|
全部基于内核可注入接口运行,零外部依赖,CI 可直接执行。
|
||||||
|
|
||||||
|
## 演练场景
|
||||||
|
|
||||||
|
| 场景 | 演练口径 | 验证目标 |
|
||||||
|
|------|----------|----------|
|
||||||
|
| 可用性基线 | 600 点位 30 轮健康采集 | 可用性 ≥ 99.8% / 丢失率 ≤ 0.02% / P99 ≤ 1.8s |
|
||||||
|
| 驱动故障隔离 | 30 轮中注入 5 轮设备掉线 | 引擎未崩溃;可用性受控下降(1−5/30);健康轮次仍产出样本 |
|
||||||
|
| 上行抖动恢复 | 上行连续中断 5 轮后恢复 | 中断期零丢失;恢复后断点续传落库 = 采集数(RPO=0) |
|
||||||
|
| 进程重启容灾 | 采集 8 轮后销毁引擎模拟重启 | 重启后扫描同一 spool,未确认样本全部重发落库(RPO=0) |
|
||||||
|
|
||||||
|
## 运行
|
||||||
|
|
||||||
|
在**仓库根目录**执行(`_bootstrap.py` 会自动把四个 core 模块加载到 `sys.path`):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
python -m unittest discover -s tests/ha -v
|
||||||
|
```
|
||||||
|
|
||||||
|
## 文件
|
||||||
|
|
||||||
|
- `_bootstrap.py` —— 内核模块加载引导(edge-gateway 裸导入;data-bus / rag-kb /
|
||||||
|
llm-gateway 注册为下划线包名)。
|
||||||
|
- `test_availability_dr.py` —— 四个演练场景的 unittest 用例,含 `_FlakyDriver`
|
||||||
|
故障注入驱动。
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""iAOP 可用性 / 容灾演练套件(Issue #89,对应 PRD 第 9 章 NFR)。
|
||||||
|
|
||||||
|
验证目标(PRD 第 9 章 SLA「可用性 ≥ 99.8%」+ 容灾/断点续传):
|
||||||
|
- #89 可用性 / 容灾演练:注入驱动级、上行级、进程级故障,验证
|
||||||
|
- 采集可用性 ≥ 99.8%(轮次级健康度);
|
||||||
|
- 进程重启后 spool 断点续传:未确认样本零丢失重发;
|
||||||
|
- 上行通道抖动不丢数据,仅增加本地缓存占用;
|
||||||
|
- 单驱动故障被隔离,不拖垮网关,SLA 仍达标。
|
||||||
|
|
||||||
|
全部基于各模块可注入接口运行,零外部依赖,CI 可直接执行。
|
||||||
|
运行:在仓库根目录执行 `python -m unittest discover -s tests/ha -v`
|
||||||
|
(_bootstrap.py 会自动加载四个 core 模块)。
|
||||||
|
"""
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""容灾演练套件引导:复用内核模块加载逻辑。
|
||||||
|
|
||||||
|
与 tests/perf/_bootstrap.py / tests/e2e/_bootstrap.py 等价:把 edge-gateway
|
||||||
|
加入 sys.path(裸导入),把 data-bus / rag-kb / llm-gateway 注册为下划线
|
||||||
|
包名(带连字符目录)。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import importlib.util
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
_THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
_REPO_ROOT = os.path.abspath(os.path.join(_THIS_DIR, "..", ".."))
|
||||||
|
_CORE_DIR = os.path.join(_REPO_ROOT, "core")
|
||||||
|
|
||||||
|
_EDGE_DIR = os.path.join(_CORE_DIR, "edge-gateway")
|
||||||
|
if os.path.isdir(_EDGE_DIR) and _EDGE_DIR not in sys.path:
|
||||||
|
sys.path.insert(0, _EDGE_DIR)
|
||||||
|
|
||||||
|
|
||||||
|
def _register_dashed_package(pkg_name: str, dir_path: str) -> None:
|
||||||
|
init_file = os.path.join(dir_path, "__init__.py")
|
||||||
|
if not os.path.isfile(init_file) or pkg_name in sys.modules:
|
||||||
|
return
|
||||||
|
spec = importlib.util.spec_from_file_location(
|
||||||
|
pkg_name, init_file, submodule_search_locations=[dir_path]
|
||||||
|
)
|
||||||
|
if spec is None or spec.loader is None:
|
||||||
|
return
|
||||||
|
module = importlib.util.module_from_spec(spec)
|
||||||
|
sys.modules[pkg_name] = module
|
||||||
|
spec.loader.exec_module(module)
|
||||||
|
|
||||||
|
|
||||||
|
_register_dashed_package("data_bus", os.path.join(_CORE_DIR, "data-bus"))
|
||||||
|
_register_dashed_package("rag_kb", os.path.join(_CORE_DIR, "rag-kb"))
|
||||||
|
_register_dashed_package("llm_gateway", os.path.join(_CORE_DIR, "llm-gateway"))
|
||||||
@@ -0,0 +1,278 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""可用性 / 容灾演练(Issue #89,对应 PRD 第 9 章 NFR)。
|
||||||
|
|
||||||
|
演练场景与验证目标:
|
||||||
|
1. 可用性基线:600 点位 30 轮健康采集,可用性 ≥ 99.8%、丢失率 ≤ 0.02%;
|
||||||
|
2. 驱动级故障注入:单驱动按计划间歇抛异常,引擎记失败轮但**不中断**,
|
||||||
|
跨足够轮次后整体可用性仍受控(故障被隔离,正常轮次仍产出样本);
|
||||||
|
3. 上行通道抖动:Kafka 不可用期间样本全部驻留 spool,恢复后断点续传,
|
||||||
|
全量样本零丢失(断点续传 = 丢失率保障的实现机制);
|
||||||
|
4. 进程重启容灾:模拟网关崩溃重启 —— 新建 SpoolStore 扫描同一目录,
|
||||||
|
未确认记录全部重发,落库样本数 = 采集样本数(RPO=0)。
|
||||||
|
|
||||||
|
全部基于内核可注入接口(Driver / SpoolStore / HealthMetrics / BatchWriter)
|
||||||
|
合成执行,零外部依赖,CI 可重复运行。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
# 引导加载内核模块
|
||||||
|
import tests.ha._bootstrap # noqa: F401
|
||||||
|
|
||||||
|
import os
|
||||||
|
import tempfile
|
||||||
|
import time
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from collector.engine import CollectorEngine
|
||||||
|
from collector.metrics import HealthMetrics
|
||||||
|
from collector.spool import SpoolStore
|
||||||
|
from drivers import SimulatorDriver
|
||||||
|
from drivers.base import Driver, SampleValue
|
||||||
|
from point_dict.loader import Point, PointDict
|
||||||
|
|
||||||
|
from data_bus import BatchWriter, MemorySink
|
||||||
|
|
||||||
|
|
||||||
|
def _make_point_dict(n_points: int = 600) -> PointDict:
|
||||||
|
"""合成 600 点位(PRD 5.1 基线:600 点位 1Hz),分布到 12 台设备。"""
|
||||||
|
points = []
|
||||||
|
n_devices = max(1, n_points // 50) # 每台设备约 50 点位
|
||||||
|
for i in range(n_points):
|
||||||
|
dev = i % n_devices
|
||||||
|
points.append(Point(
|
||||||
|
device_id=f"CLF-{dev+1:02d}",
|
||||||
|
point_id=f"CLF-{dev+1:02d}.P{i:04d}",
|
||||||
|
name=f"测点{i}",
|
||||||
|
unit="℃",
|
||||||
|
data_type="float",
|
||||||
|
sample_rate=1000,
|
||||||
|
quality_code=True,
|
||||||
|
row_number=i + 2,
|
||||||
|
))
|
||||||
|
return PointDict(points)
|
||||||
|
|
||||||
|
|
||||||
|
class _FlakyDriver(Driver):
|
||||||
|
"""按计划间歇抛异常的故障驱动(容灾演练用)。
|
||||||
|
|
||||||
|
每轮读取根据注入策略决定是否抛 ConnectionError,模拟现场设备掉线 /
|
||||||
|
通讯中断。`fail_rounds` 为一个 set,元素为「本轮要抛异常」的轮次序号
|
||||||
|
(从 0 起计)。其余轮次正常返回模拟值。
|
||||||
|
"""
|
||||||
|
|
||||||
|
protocol = "simulator"
|
||||||
|
|
||||||
|
def __init__(self, fail_rounds=None):
|
||||||
|
super().__init__({})
|
||||||
|
self._inner = SimulatorDriver()
|
||||||
|
self.fail_rounds = set(fail_rounds or [])
|
||||||
|
self._tick = -1
|
||||||
|
|
||||||
|
def connect(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def read_points(self, points):
|
||||||
|
self._tick += 1
|
||||||
|
if self._tick in self.fail_rounds:
|
||||||
|
# 模拟设备掉线 / 通讯中断
|
||||||
|
raise ConnectionError(f"演练:第 {self._tick} 轮设备通讯中断")
|
||||||
|
return self._inner.read_points(points)
|
||||||
|
|
||||||
|
def close(self) -> None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class AvailabilityBaselineDrillTest(unittest.TestCase):
|
||||||
|
"""场景 1:健康采集可用性基线(PRD 9 章 ≥ 99.8%)。"""
|
||||||
|
|
||||||
|
def setUp(self) -> None:
|
||||||
|
self._tmp = tempfile.mkdtemp(prefix="iaop_ha_baseline_")
|
||||||
|
self.point_dict = _make_point_dict(600)
|
||||||
|
self.spool = SpoolStore(spool_dir=self._tmp, cache_limit_bytes=64 * 1024 * 1024)
|
||||||
|
self.metrics = HealthMetrics()
|
||||||
|
self.engine = CollectorEngine(
|
||||||
|
point_dict=self.point_dict,
|
||||||
|
driver_slots=[("simulator", SimulatorDriver(), [])],
|
||||||
|
spool=self.spool, metrics=self.metrics,
|
||||||
|
interval_ms=1000, max_pending=500_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_availability_under_threshold(self) -> None:
|
||||||
|
"""30 轮健康采集,可用性 ≥ 99.8%、丢失率 ≤ 0.02%、P99 ≤ 1.8s。"""
|
||||||
|
rounds = 30
|
||||||
|
for _ in range(rounds):
|
||||||
|
self.engine.collect_once()
|
||||||
|
|
||||||
|
self.assertGreaterEqual(self.metrics.availability, 0.998,
|
||||||
|
f"可用性 {self.metrics.availability:.4%} < 99.8%(PRD 9 章)")
|
||||||
|
self.assertLessEqual(self.metrics.loss_rate, 0.0002,
|
||||||
|
f"丢失率 {self.metrics.loss_rate:.4%} > 0.02%")
|
||||||
|
self.assertLessEqual(self.metrics.p99_latency(), 1.8,
|
||||||
|
f"P99 {self.metrics.p99_latency():.4f}s > 1.8s")
|
||||||
|
self.assertTrue(self.metrics.meets_sla())
|
||||||
|
print(f"\n[可用性基线] {rounds} 轮健康采集,"
|
||||||
|
f"可用性={self.metrics.availability:.4%},"
|
||||||
|
f"丢失率={self.metrics.loss_rate:.6%},"
|
||||||
|
f"P99={self.metrics.p99_latency()*1000:.1f}ms → SLA 达标")
|
||||||
|
|
||||||
|
|
||||||
|
class DriverFaultIsolationDrillTest(unittest.TestCase):
|
||||||
|
"""场景 2:驱动级故障注入 —— 单驱动间歇掉线,引擎记失败轮但不中断。
|
||||||
|
|
||||||
|
演练口径:30 轮中注入 5 轮驱动异常(掉线)。引擎对驱动异常按「本轮整体
|
||||||
|
失败」计入 HealthMetrics(failed_rounds +1)并立即返回,不抛出、不崩溃。
|
||||||
|
验证:
|
||||||
|
- 引擎本身未被故障拖垮(异常被隔离,无未捕获异常抛出);
|
||||||
|
- 可用性 = 1 - failed/total = 1 - 5/30 ≈ 83.3%(受控下降,符合预期,
|
||||||
|
说明统计口径正确反映故障);故障占比与可用性下降幅度一致;
|
||||||
|
- 剩余 25 个健康轮次仍正常产出样本(25 × 600 = 15000 条驻留 spool)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_driver_fault_isolated_and_metrics_accurate(self) -> None:
|
||||||
|
tmp = tempfile.mkdtemp(prefix="iaop_ha_fault_")
|
||||||
|
point_dict = _make_point_dict(600)
|
||||||
|
spool = SpoolStore(spool_dir=tmp, cache_limit_bytes=64 * 1024 * 1024)
|
||||||
|
metrics = HealthMetrics()
|
||||||
|
fail_rounds = {3, 7, 12, 18, 24} # 30 轮中注入 5 轮设备掉线
|
||||||
|
flaky = _FlakyDriver(fail_rounds=fail_rounds)
|
||||||
|
engine = CollectorEngine(
|
||||||
|
point_dict=point_dict,
|
||||||
|
driver_slots=[("simulator", flaky, [])],
|
||||||
|
spool=spool, metrics=metrics,
|
||||||
|
interval_ms=1000, max_pending=500_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
rounds = 30
|
||||||
|
for _ in range(rounds):
|
||||||
|
# 故障被隔离:不应抛出未捕获异常
|
||||||
|
engine.collect_once()
|
||||||
|
|
||||||
|
# 统计口径正确:失败轮次 = 注入轮次数
|
||||||
|
self.assertEqual(metrics.failed_rounds, len(fail_rounds))
|
||||||
|
self.assertEqual(metrics.total_rounds, rounds)
|
||||||
|
expected_avail = 1.0 - len(fail_rounds) / rounds
|
||||||
|
self.assertAlmostEqual(metrics.availability, expected_avail, places=6)
|
||||||
|
# 健康轮次仍正常产出样本(5 个掉线轮不产出,25 轮 × 600 = 15000)
|
||||||
|
pending = spool.total_pending()
|
||||||
|
self.assertEqual(pending, (rounds - len(fail_rounds)) * len(point_dict))
|
||||||
|
# 故障期间丢失率口径:掉线轮的应采样本计入丢失
|
||||||
|
# 丢失样本 = 5 轮 × 600 = 3000,应采 = 30 × 600 = 18000
|
||||||
|
self.assertEqual(metrics.lost_samples, len(fail_rounds) * len(point_dict))
|
||||||
|
print(f"\n[驱动故障隔离] 注入 {len(fail_rounds)} 轮设备掉线,"
|
||||||
|
f"引擎未崩溃;可用性={metrics.availability:.2%}"
|
||||||
|
f"(受控下降,故障被隔离),健康轮次产出 {pending} 条样本")
|
||||||
|
|
||||||
|
|
||||||
|
class UpstreamOutageResumeDrillTest(unittest.TestCase):
|
||||||
|
"""场景 3:上行通道抖动 —— Kafka 不可用期间样本驻留 spool,恢复后断点续传。
|
||||||
|
|
||||||
|
演练口径:模拟上行(Kafka)连续不可用 N 轮,期间 collect_once 不传 sink
|
||||||
|
(离线采集模式,样本全部落 spool)。恢复后把 spool 全量重发到 BatchWriter。
|
||||||
|
验证:
|
||||||
|
- 抖动期间零丢失(样本全部驻留本地 spool);
|
||||||
|
- 恢复后断点续传落库数 = 采集数(不丢不重)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_upstream_outage_then_resume_no_loss(self) -> None:
|
||||||
|
tmp = tempfile.mkdtemp(prefix="iaop_ha_upstream_")
|
||||||
|
point_dict = _make_point_dict(600)
|
||||||
|
spool = SpoolStore(spool_dir=tmp, cache_limit_bytes=64 * 1024 * 1024)
|
||||||
|
metrics = HealthMetrics()
|
||||||
|
engine = CollectorEngine(
|
||||||
|
point_dict=point_dict,
|
||||||
|
driver_slots=[("simulator", SimulatorDriver(), [])],
|
||||||
|
spool=spool, metrics=metrics,
|
||||||
|
interval_ms=1000, max_pending=500_000,
|
||||||
|
)
|
||||||
|
|
||||||
|
outage_rounds = 5 # 上行连续中断 5 轮
|
||||||
|
total_rounds = 10
|
||||||
|
# 中断期:离线采集(不传 sink),样本全部驻留 spool
|
||||||
|
for _ in range(outage_rounds):
|
||||||
|
engine.collect_once(sink=None)
|
||||||
|
# 恢复后继续采集 5 轮(仍离线,便于精确比对驻留量)
|
||||||
|
for _ in range(total_rounds - outage_rounds):
|
||||||
|
engine.collect_once(sink=None)
|
||||||
|
|
||||||
|
# 抖动期间零丢失:采集可用性 100%
|
||||||
|
self.assertEqual(metrics.failed_rounds, 0)
|
||||||
|
self.assertGreaterEqual(metrics.availability, 0.998)
|
||||||
|
|
||||||
|
# 恢复:spool 全量重发到 BatchWriter,断点续传落库
|
||||||
|
sink = MemorySink()
|
||||||
|
writer = BatchWriter(sink=sink, batch_size=1000, flush_interval=0.0)
|
||||||
|
accepted = 0
|
||||||
|
for sample in spool.pending_records():
|
||||||
|
if writer.push(sample):
|
||||||
|
accepted += 1
|
||||||
|
spool.ack(sample)
|
||||||
|
writer.flush()
|
||||||
|
|
||||||
|
expected = total_rounds * len(point_dict)
|
||||||
|
self.assertEqual(accepted, expected)
|
||||||
|
self.assertEqual(len(sink.rows), expected)
|
||||||
|
# 重发后 spool 清空(全部 ack)
|
||||||
|
self.assertEqual(spool.total_pending(), 0)
|
||||||
|
print(f"\n[上行抖动恢复] 上行中断 {outage_rounds} 轮期间样本全部驻留 spool,"
|
||||||
|
f"恢复后断点续传落库 {accepted} 条(零丢失,RPO=0)")
|
||||||
|
|
||||||
|
|
||||||
|
class ProcessRestartResumeDrillTest(unittest.TestCase):
|
||||||
|
"""场景 4:进程重启容灾 —— 网关崩溃重启后断点续传。
|
||||||
|
|
||||||
|
演练口径:
|
||||||
|
1. 采集若干轮(样本写入 spool,模拟尚未上行确认);
|
||||||
|
2. **销毁引擎对象**(模拟进程崩溃 / 重启)—— 丢弃内存中的引擎/spool 句柄,
|
||||||
|
仅保留磁盘上的 spool 文件;
|
||||||
|
3. 新建 SpoolStore 指向同一目录(模拟重启后扫描 spool),把未确认记录
|
||||||
|
全部重发落库。
|
||||||
|
|
||||||
|
验证:RPO=0 —— 重启未丢任何未确认样本,落库数 = 崩溃前采集数。
|
||||||
|
这正是 spool 断点续传机制(PRD 9 章「丢失率 ≤ 0.02%」的实现保障)的
|
||||||
|
容灾价值。
|
||||||
|
"""
|
||||||
|
|
||||||
|
def test_restart_resumes_unacked_samples(self) -> None:
|
||||||
|
tmp = tempfile.mkdtemp(prefix="iaop_ha_restart_")
|
||||||
|
point_dict = _make_point_dict(600)
|
||||||
|
rounds_before_crash = 8
|
||||||
|
|
||||||
|
# ---- 阶段 1:崩溃前采集(样本落 spool,全部未 ack)----
|
||||||
|
spool1 = SpoolStore(spool_dir=tmp, cache_limit_bytes=64 * 1024 * 1024)
|
||||||
|
metrics1 = HealthMetrics()
|
||||||
|
engine1 = CollectorEngine(
|
||||||
|
point_dict=point_dict,
|
||||||
|
driver_slots=[("simulator", SimulatorDriver(), [])],
|
||||||
|
spool=spool1, metrics=metrics1,
|
||||||
|
interval_ms=1000, max_pending=500_000,
|
||||||
|
)
|
||||||
|
for _ in range(rounds_before_crash):
|
||||||
|
engine1.collect_once(sink=None) # 离线采集,样本待上行
|
||||||
|
collected = rounds_before_crash * len(point_dict)
|
||||||
|
self.assertEqual(spool1.total_pending(), collected)
|
||||||
|
|
||||||
|
# ---- 阶段 2:模拟进程崩溃 —— 丢弃内存对象,仅留磁盘 spool ----
|
||||||
|
del engine1, spool1, metrics1
|
||||||
|
|
||||||
|
# ---- 阶段 3:重启后扫描同一 spool 目录,断点续传重发 ----
|
||||||
|
spool2 = SpoolStore(spool_dir=tmp, cache_limit_bytes=64 * 1024 * 1024)
|
||||||
|
sink = MemorySink()
|
||||||
|
writer = BatchWriter(sink=sink, batch_size=1000, flush_interval=0.0)
|
||||||
|
accepted = 0
|
||||||
|
for sample in spool2.pending_records():
|
||||||
|
if writer.push(sample):
|
||||||
|
accepted += 1
|
||||||
|
spool2.ack(sample)
|
||||||
|
writer.flush()
|
||||||
|
|
||||||
|
# RPO=0:重启未丢任何未确认样本
|
||||||
|
self.assertEqual(accepted, collected)
|
||||||
|
self.assertEqual(len(sink.rows), collected)
|
||||||
|
# 全部确认后 spool 清空
|
||||||
|
self.assertEqual(spool2.total_pending(), 0)
|
||||||
|
print(f"\n[进程重启容灾] 崩溃前采集 {collected} 条未确认样本,"
|
||||||
|
f"重启后断点续传全部重发落库(RPO=0,零丢失)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Reference in New Issue
Block a user