Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
69d8178a83 |
@@ -1,55 +0,0 @@
|
|||||||
# iAOP 端到端联调用例(Issue #86)
|
|
||||||
|
|
||||||
对应 PRD 第 8 章「端到端联调」与 EPIC #14(M4)子任务 #86。
|
|
||||||
跨四个 iAOP-Core 内核模块的全链路联调测试,覆盖「数据流」与「问答流」两条端到端链路,
|
|
||||||
全部基于各模块的**可注入接口**运行,零外部依赖(不依赖 Kafka / TDengine / PostgreSQL /
|
|
||||||
MinIO / 真实 LLM / 真实设备),可在 CI 中直接执行。
|
|
||||||
|
|
||||||
## 链路覆盖
|
|
||||||
|
|
||||||
### 数据流(PRD 5.1 → 5.2)
|
|
||||||
`edge-gateway` 只读采集(模拟驱动)→ `spool` 断点续传缓存 → `data-bus` BatchWriter
|
|
||||||
批量写入(MemorySink,幂等去重,「不丢不重」)→ 健康度上报(P99 / 丢失率 / 可用性)。
|
|
||||||
|
|
||||||
| 用例 | 验证点 |
|
|
||||||
|------|--------|
|
|
||||||
| `test_collect_once_all_points_read_into_spool` | 单轮采集:全部点位读出并落 spool |
|
|
||||||
| `test_collect_multiple_rounds_accumulate` | 多轮采集:spool 累计 = 轮数 × 点位数(不丢) |
|
|
||||||
| `test_spool_drain_into_batchwriter_no_loss_no_dup` | spool → 批量写入:不丢不重 |
|
|
||||||
| `test_batchwriter_idempotent_on_replay` | 断点续传重发:幂等去重 |
|
|
||||||
| `test_sample_fields_preserved_end_to_end` | 样本字段端到端完整 |
|
|
||||||
| `test_health_metrics_reflect_collection` | 健康度随轮次累计,满足 SLA(P99≤1.8s / 丢失率≤0.02% / 可用性≥99.8%) |
|
|
||||||
|
|
||||||
### 问答流(PRD 5.4)
|
|
||||||
`rag-kb` 模板化知识库检索(命中文档片段 + 来源)→ `llm-gateway` 混合网关
|
|
||||||
(敏感度路由 → 生成 → 幻觉/溯源校验 → DLP 出站防线)。
|
|
||||||
|
|
||||||
| 用例 | 验证点 |
|
|
||||||
|------|--------|
|
|
||||||
| `test_kb_retrieval_returns_relevant_chunks_with_source` | RAG 检索命中并带来源 |
|
|
||||||
| `test_kb_category_filter` | 类别过滤(工艺规范 / SOP / 国标) |
|
|
||||||
| `test_normal_question_routes_local_with_rag_context` | 普通问题 → 本地后端 + 来源引用 |
|
|
||||||
| `test_sensitive_query_blocked_by_dlp` | 敏感数据 → DLP 拦截 → block → 转人工 |
|
|
||||||
| `test_generic_question_routes_cloud` | 通用问题 DLP 放行闭环正常 |
|
|
||||||
| `test_high_stakes_low_confidence_to_human` | 高利害 + 低信度 → 转人工复核 |
|
|
||||||
| `test_audit_drain_after_query` | 各组件审计记录可统一导出 |
|
|
||||||
|
|
||||||
### 跨链路集成(业务闭环)
|
|
||||||
采集 → 落库 → 知识沉淀 → 安全问答的完整业务闭环(`test_cross_pipeline.py`)。
|
|
||||||
|
|
||||||
## 运行方式
|
|
||||||
|
|
||||||
```bash
|
|
||||||
# 在仓库根目录执行(需 Python 3.8+,仅标准库依赖)
|
|
||||||
python -m unittest discover -s tests/e2e -v
|
|
||||||
```
|
|
||||||
|
|
||||||
## 设计说明
|
|
||||||
|
|
||||||
- **零外部依赖**:四个内核模块的存储 / 推理 / 协议接入均设计为可注入接口
|
|
||||||
(`StoreSink` / `InferenceBackend` / `Driver`),联调用内存桩替换真实后端,
|
|
||||||
保证 CI 可重复执行且不污染环境。
|
|
||||||
- **模块加载**:四个 core 模块的源码组织方式不一致(edge-gateway 为裸导入,
|
|
||||||
其余为带连字符目录名的包),由 `_bootstrap.py` 统一加载(详见该文件注释)。
|
|
||||||
- **断言对齐 PRD 验收基线**:健康度断言直接引用 PRD 第 9 章 NFR 阈值
|
|
||||||
(P99≤1.8s / 丢失率≤0.02% / 可用性≥99.8%),DLP 断言验证 100% 拦截目标。
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""iAOP 端到端联调用例(Issue #86)。
|
|
||||||
|
|
||||||
跨四个 iAOP-Core 内核模块的全链路联调测试,验证「数据流」与「问答流」
|
|
||||||
两条端到端链路在内存桩(不依赖 Kafka/TDengine/MinIO/真实 LLM)下完整闭环:
|
|
||||||
|
|
||||||
数据流(PRD 5.1 → 5.2):
|
|
||||||
edge-gateway 只读采集(模拟驱动)→ spool 缓存 → data-bus 批量写入
|
|
||||||
(MemorySink,幂等去重,不丢不重)。
|
|
||||||
|
|
||||||
问答流(PRD 5.4):
|
|
||||||
rag-kb 模板化知识库检索(命中文档片段 + 来源)→ llm-gateway 混合网关
|
|
||||||
(路由 → 生成 → 溯源校验 → DLP 出站防线)。
|
|
||||||
|
|
||||||
本套件全部基于各模块的「可注入接口」运行,零外部依赖,可在 CI 中直接执行。
|
|
||||||
运行:在仓库根目录执行 `python -m unittest discover -s tests/e2e -v`
|
|
||||||
(脚本会自动把四个 core 模块加入 sys.path,见 _bootstrap.py)。
|
|
||||||
"""
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""端到端联调测试引导:统一加载四个 iAOP-Core 内核模块。
|
|
||||||
|
|
||||||
四个 core 模块的源码组织方式不一致:
|
|
||||||
- edge-gateway:**裸导入**风格(无 ``__init__.py``,模块内 ``from point_dict.loader import ...``);
|
|
||||||
- data-bus / rag-kb / llm-gateway:**包**风格(有 ``__init__.py`` + 相对导入),
|
|
||||||
但目录名带连字符(``data-bus``)不是合法 Python 标识符,无法直接 ``import``。
|
|
||||||
|
|
||||||
这里在 import 早期:
|
|
||||||
1. 把 ``edge-gateway`` 目录加入 ``sys.path``,让裸导入生效;
|
|
||||||
2. 用 ``importlib`` 把三个带连字符的包目录注册为合法包名
|
|
||||||
(``data_bus`` / ``rag_kb`` / ``llm_gateway``),让相对导入在其子模块内生效。
|
|
||||||
|
|
||||||
之后测试用例即可:
|
|
||||||
from point_dict.loader import Point, PointDict # edge(裸导入)
|
|
||||||
from data_bus import BatchWriter, MemorySink # 包
|
|
||||||
from rag_kb import RagKnowledgeBase, build_document # 包
|
|
||||||
from llm_gateway import LLMGateway, LocalBackend # 包
|
|
||||||
"""
|
|
||||||
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")
|
|
||||||
|
|
||||||
# 1) edge-gateway:裸导入,把目录加入 sys.path
|
|
||||||
_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:
|
|
||||||
"""把带连字符目录注册为合法包名(如 data-bus → data_bus)。
|
|
||||||
|
|
||||||
通过 spec_from_file_location 显式指定 submodule_search_locations,
|
|
||||||
使包内相对导入(``from .tdengine_schema import ...``)能正常解析。
|
|
||||||
"""
|
|
||||||
init_file = os.path.join(dir_path, "__init__.py")
|
|
||||||
if not os.path.isfile(init_file):
|
|
||||||
return
|
|
||||||
if 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)
|
|
||||||
|
|
||||||
|
|
||||||
# 2) 三个带连字符的包:注册为下划线包名
|
|
||||||
_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"))
|
|
||||||
@@ -1,107 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""端到端联调用例(Issue #86)—— 跨链路集成场景。
|
|
||||||
|
|
||||||
验证数据流(采集 → 落库)与问答流(RAG → LLM 网关)的协同:
|
|
||||||
基于真实采集到的工艺数据,构建 RAG 知识并完成一次安全问答闭环,
|
|
||||||
模拟「采集 → 沉淀知识 → 智能问答」的完整业务闭环。
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
# 引导加载四个内核模块(必须在测试导入前执行)
|
|
||||||
import tests.e2e._bootstrap # noqa: F401
|
|
||||||
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from collector.engine import CollectorEngine
|
|
||||||
from collector.metrics import HealthMetrics
|
|
||||||
from collector.spool import SpoolStore
|
|
||||||
from drivers import SimulatorDriver
|
|
||||||
from point_dict.loader import Point, PointDict
|
|
||||||
|
|
||||||
from data_bus import BatchWriter, MemorySink
|
|
||||||
|
|
||||||
from rag_kb import RagKnowledgeBase, build_document
|
|
||||||
from llm_gateway import (
|
|
||||||
GatewayResult,
|
|
||||||
LLMGateway,
|
|
||||||
LocalBackend,
|
|
||||||
PromptRegistry,
|
|
||||||
RouteTarget,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _collect_samples(tmpdir: str):
|
|
||||||
"""跑一轮采集,返回(落库样本, 引擎健康度)。"""
|
|
||||||
points = [
|
|
||||||
Point(device_id="CLF-01", point_id="CLF-01.TEMP", name="1#炉温",
|
|
||||||
unit="℃", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=2),
|
|
||||||
Point(device_id="CLF-01", point_id="CLF-01.PRES", name="1#炉压",
|
|
||||||
unit="kPa", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=3),
|
|
||||||
]
|
|
||||||
pd = PointDict(points)
|
|
||||||
spool = SpoolStore(spool_dir=tmpdir, cache_limit_bytes=8 * 1024 * 1024)
|
|
||||||
metrics = HealthMetrics()
|
|
||||||
engine = CollectorEngine(
|
|
||||||
point_dict=pd,
|
|
||||||
driver_slots=[("simulator", SimulatorDriver(), [])],
|
|
||||||
spool=spool, metrics=metrics, interval_ms=1000, max_pending=100_000,
|
|
||||||
)
|
|
||||||
engine.collect_once()
|
|
||||||
|
|
||||||
sink = MemorySink()
|
|
||||||
writer = BatchWriter(sink=sink, batch_size=10, flush_interval=0.0)
|
|
||||||
for s in spool.pending_records():
|
|
||||||
writer.push(s)
|
|
||||||
spool.ack(s)
|
|
||||||
writer.flush()
|
|
||||||
return sink.rows, metrics
|
|
||||||
|
|
||||||
|
|
||||||
class CrossPipelineE2ETest(unittest.TestCase):
|
|
||||||
"""采集 → 落库 → 知识沉淀 → 安全问答 全业务闭环。"""
|
|
||||||
|
|
||||||
def test_collect_then_query_closed_loop(self) -> None:
|
|
||||||
"""采集数据落库后,结合工艺知识库完成一次安全问答。"""
|
|
||||||
tmpdir = tempfile.mkdtemp(prefix="iaop_cross_")
|
|
||||||
# 1) 数据流:采集并落库
|
|
||||||
rows, metrics = _collect_samples(tmpdir)
|
|
||||||
self.assertEqual(len(rows), 2) # 2 点位全部落库
|
|
||||||
self.assertTrue(metrics.meets_sla()) # 采集健康度达标
|
|
||||||
point_ids = {r["point_id"] for r in rows}
|
|
||||||
self.assertEqual(point_ids, {"CLF-01.TEMP", "CLF-01.PRES"})
|
|
||||||
|
|
||||||
# 2) 知识沉淀:工艺知识库(模拟由采集数据衍生的工艺规范)
|
|
||||||
kb = RagKnowledgeBase()
|
|
||||||
kb.add_documents([
|
|
||||||
build_document(
|
|
||||||
title="1#氯化炉工艺卡",
|
|
||||||
text="1#氯化炉(CLF-01)正常炉温 850~920℃,炉压 0.2~0.4MPa。"
|
|
||||||
"CLF-01.TEMP 与 CLF-01.PRES 为关键监控点位。",
|
|
||||||
category="process",
|
|
||||||
),
|
|
||||||
])
|
|
||||||
|
|
||||||
# 3) 问答流:基于知识库回答工艺问题
|
|
||||||
prompts = PromptRegistry()
|
|
||||||
prompts.update(name="qa", version="1.0.0",
|
|
||||||
text="工业 AI 助手回答:{query}")
|
|
||||||
gateway = LLMGateway(prompts=prompts, prompt_name="qa",
|
|
||||||
local=LocalBackend(echo_context=True))
|
|
||||||
query = "CLF-01 的关键监控点位和正常炉温范围?"
|
|
||||||
hits = kb.search(query, top_k=3)
|
|
||||||
sources = [h.chunk.title for h in hits]
|
|
||||||
self.assertGreater(len(sources), 0)
|
|
||||||
|
|
||||||
result = gateway.ask(query, rag_context=sources, confidence=0.95)
|
|
||||||
self.assertIsInstance(result, GatewayResult)
|
|
||||||
self.assertEqual(result.route.target, RouteTarget.LOCAL)
|
|
||||||
self.assertFalse(result.needs_human)
|
|
||||||
# 回答引用了知识库来源
|
|
||||||
self.assertIn(sources[0], result.answer)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,183 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""端到端联调用例(Issue #86)—— 数据流链路。
|
|
||||||
|
|
||||||
验证 PRD 5.1(边缘采集网关)→ PRD 5.2(数据总线 + 时序库)的端到端协作:
|
|
||||||
edge-gateway 只读采集(模拟驱动)→ spool 缓存 → BatchWriter 批量写入
|
|
||||||
(MemorySink 幂等去重,「不丢不重」)。
|
|
||||||
|
|
||||||
不依赖 Kafka / TDengine / 真实设备:全部走内存桩,可在 CI 直接执行。
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
# 引导加载四个内核模块(必须在测试导入前执行)
|
|
||||||
import tests.e2e._bootstrap # noqa: F401
|
|
||||||
|
|
||||||
import os
|
|
||||||
import tempfile
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from collector.engine import CollectorEngine
|
|
||||||
from collector.metrics import HealthMetrics
|
|
||||||
from collector.spool import SpoolStore
|
|
||||||
from drivers import SimulatorDriver
|
|
||||||
from point_dict.loader import Point, PointDict
|
|
||||||
|
|
||||||
from data_bus import BatchWriter, MemorySink
|
|
||||||
|
|
||||||
|
|
||||||
def _make_point_dict() -> PointDict:
|
|
||||||
"""模拟氯化车间(Template-Ti)点位集:2 台设备 × 3 测点。"""
|
|
||||||
points = [
|
|
||||||
Point(device_id="CLF-01", point_id="CLF-01.TEMP", name="1#炉温",
|
|
||||||
unit="℃", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=2),
|
|
||||||
Point(device_id="CLF-01", point_id="CLF-01.PRES", name="1#炉压",
|
|
||||||
unit="kPa", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=3),
|
|
||||||
Point(device_id="CLF-01", point_id="CLF-01.FLOW", name="1#氯气流量",
|
|
||||||
unit="m³/h", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=4),
|
|
||||||
Point(device_id="CLF-02", point_id="CLF-02.TEMP", name="2#炉温",
|
|
||||||
unit="℃", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=5),
|
|
||||||
Point(device_id="CLF-02", point_id="CLF-02.PRES", name="2#炉压",
|
|
||||||
unit="kPa", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=6),
|
|
||||||
Point(device_id="CLF-02", point_id="CLF-02.FLOW", name="2#氯气流量",
|
|
||||||
unit="m³/h", data_type="float", sample_rate=1000,
|
|
||||||
quality_code=True, row_number=7),
|
|
||||||
]
|
|
||||||
return PointDict(points)
|
|
||||||
|
|
||||||
|
|
||||||
def _make_engine(tmpdir: str, point_dict: PointDict):
|
|
||||||
"""组装一个最小可运行的采集引擎(模拟驱动,spool 落临时目录)。"""
|
|
||||||
spool = SpoolStore(spool_dir=tmpdir, cache_limit_bytes=16 * 1024 * 1024)
|
|
||||||
metrics = HealthMetrics()
|
|
||||||
engine = CollectorEngine(
|
|
||||||
point_dict=point_dict,
|
|
||||||
driver_slots=[("simulator", SimulatorDriver(), [])],
|
|
||||||
spool=spool,
|
|
||||||
metrics=metrics,
|
|
||||||
interval_ms=1000,
|
|
||||||
max_pending=100_000,
|
|
||||||
)
|
|
||||||
return engine, spool, metrics
|
|
||||||
|
|
||||||
|
|
||||||
class DataPipelineE2ETest(unittest.TestCase):
|
|
||||||
"""采集 → spool → 批量写入 全链路联调。"""
|
|
||||||
|
|
||||||
def setUp(self) -> None:
|
|
||||||
self._tmp = tempfile.mkdtemp(prefix="iaop_e2e_")
|
|
||||||
self.point_dict = _make_point_dict()
|
|
||||||
self.engine, self.spool, self.metrics = _make_engine(
|
|
||||||
self._tmp, self.point_dict
|
|
||||||
)
|
|
||||||
|
|
||||||
# -- 采集 → spool ------------------------------------------------------
|
|
||||||
def test_collect_once_all_points_read_into_spool(self) -> None:
|
|
||||||
"""一轮采集:6 点位全部读出并落入 spool(expected == got)。"""
|
|
||||||
got = self.engine.collect_once()
|
|
||||||
self.assertEqual(got, len(self.point_dict))
|
|
||||||
# spool 待上行记录数应等于本轮样本数
|
|
||||||
self.assertEqual(self.spool.total_pending(), got)
|
|
||||||
# 健康度:一轮全成功,失败计 0
|
|
||||||
self.assertEqual(self.metrics.total_rounds, 1)
|
|
||||||
self.assertEqual(self.metrics.failed_rounds, 0)
|
|
||||||
|
|
||||||
def test_collect_multiple_rounds_accumulate(self) -> None:
|
|
||||||
"""多轮采集:spool 累计样本数 = 轮数 × 点位数(未上行前不丢)。"""
|
|
||||||
rounds = 5
|
|
||||||
total = 0
|
|
||||||
for _ in range(rounds):
|
|
||||||
total += self.engine.collect_once()
|
|
||||||
self.assertEqual(total, rounds * len(self.point_dict))
|
|
||||||
self.assertEqual(self.spool.total_pending(), total)
|
|
||||||
self.assertEqual(self.metrics.total_rounds, rounds)
|
|
||||||
|
|
||||||
# -- spool → BatchWriter(幂等去重,不丢不重)-------------------------
|
|
||||||
def test_spool_drain_into_batchwriter_no_loss_no_dup(self) -> None:
|
|
||||||
"""采集 → spool → 批量写入:样本不丢、重复不重。"""
|
|
||||||
rounds = 3
|
|
||||||
for _ in range(rounds):
|
|
||||||
self.engine.collect_once()
|
|
||||||
|
|
||||||
sink = MemorySink()
|
|
||||||
writer = BatchWriter(sink=sink, batch_size=len(self.point_dict),
|
|
||||||
flush_interval=0.0)
|
|
||||||
accepted = 0
|
|
||||||
# 从 spool 取出待上行样本(pending_records 读取,ack 确认删除)
|
|
||||||
for sample in self.spool.pending_records():
|
|
||||||
if writer.push(sample):
|
|
||||||
accepted += 1
|
|
||||||
self.spool.ack(sample) # 上行确认
|
|
||||||
writer.flush()
|
|
||||||
|
|
||||||
expected = rounds * len(self.point_dict)
|
|
||||||
self.assertEqual(accepted, expected) # 全部接受(无重复)
|
|
||||||
self.assertEqual(len(sink.rows), expected) # 全部落库(不丢)
|
|
||||||
self.assertEqual(sink.write_count, expected)
|
|
||||||
# spool 已全部确认,待上行归零
|
|
||||||
self.assertEqual(self.spool.total_pending(), 0)
|
|
||||||
|
|
||||||
def test_batchwriter_idempotent_on_replay(self) -> None:
|
|
||||||
"""断点续传重发同一批样本:落库不重复(幂等去重)。"""
|
|
||||||
sink = MemorySink()
|
|
||||||
writer = BatchWriter(sink=sink, batch_size=10, flush_interval=0.0)
|
|
||||||
|
|
||||||
self.engine.collect_once()
|
|
||||||
samples = self.spool.pending_records()
|
|
||||||
# 第一次写入
|
|
||||||
for s in samples:
|
|
||||||
writer.push(s)
|
|
||||||
writer.flush()
|
|
||||||
first_count = len(sink.rows)
|
|
||||||
|
|
||||||
# 模拟断点续传:重发同一批样本
|
|
||||||
for s in samples:
|
|
||||||
writer.push(s)
|
|
||||||
writer.flush()
|
|
||||||
|
|
||||||
self.assertEqual(len(sink.rows), first_count) # 重发不产生重复
|
|
||||||
# BatchWriter 统计:duplicates 应等于重发条数
|
|
||||||
stats = writer.stats()
|
|
||||||
self.assertEqual(stats["duplicates"], len(samples))
|
|
||||||
|
|
||||||
# -- 全链路数据完整性 --------------------------------------------------
|
|
||||||
def test_sample_fields_preserved_end_to_end(self) -> None:
|
|
||||||
"""端到端:样本字段(device/point/value/unit/ts)完整落库。"""
|
|
||||||
self.engine.collect_once()
|
|
||||||
sink = MemorySink()
|
|
||||||
writer = BatchWriter(sink=sink, batch_size=10, flush_interval=0.0)
|
|
||||||
for s in self.spool.pending_records():
|
|
||||||
writer.push(s)
|
|
||||||
writer.flush()
|
|
||||||
|
|
||||||
point_ids = {p.point_id for p in self.point_dict.points}
|
|
||||||
written_ids = {r["point_id"] for r in sink.rows}
|
|
||||||
self.assertEqual(written_ids, point_ids) # 所有点位都落库
|
|
||||||
for row in sink.rows:
|
|
||||||
# 字段完整性
|
|
||||||
for key in ("device_id", "point_id", "value", "ts"):
|
|
||||||
self.assertIn(key, row)
|
|
||||||
# 设备与点位字典一致
|
|
||||||
src = next(p for p in self.point_dict.points
|
|
||||||
if p.point_id == row["point_id"])
|
|
||||||
self.assertEqual(row["device_id"], src.device_id)
|
|
||||||
|
|
||||||
# -- 健康度上报贯穿链路 ------------------------------------------------
|
|
||||||
def test_health_metrics_reflect_collection(self) -> None:
|
|
||||||
"""采集健康度(成功率/丢失率)随采集轮次正确累计。"""
|
|
||||||
for _ in range(10):
|
|
||||||
self.engine.collect_once()
|
|
||||||
self.assertEqual(self.metrics.total_rounds, 10)
|
|
||||||
self.assertEqual(self.metrics.failed_rounds, 0)
|
|
||||||
# 可用性 = 1 - 失败轮次/总轮次,目标 ≥ 99.8%(此处 100%)
|
|
||||||
availability = self.metrics.availability
|
|
||||||
self.assertGreaterEqual(availability, 0.998)
|
|
||||||
self.assertTrue(self.metrics.meets_sla())
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
unittest.main()
|
|
||||||
@@ -1,179 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""端到端联调用例(Issue #86)—— 问答流链路。
|
|
||||||
|
|
||||||
验证 PRD 5.4(LLM 网关 + RAG)的端到端协作:
|
|
||||||
rag-kb 模板化知识库检索(命中文档片段 + 来源)→ llm-gateway 混合网关
|
|
||||||
(路由 → 生成 → 溯源校验 → DLP 出站防线)。
|
|
||||||
|
|
||||||
不依赖真实 LLM / 向量库:全部走内存桩,可在 CI 直接执行。
|
|
||||||
"""
|
|
||||||
from __future__ import annotations
|
|
||||||
|
|
||||||
# 引导加载四个内核模块(必须在测试导入前执行)
|
|
||||||
import tests.e2e._bootstrap # noqa: F401
|
|
||||||
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from rag_kb import RagKnowledgeBase, build_document
|
|
||||||
from llm_gateway import (
|
|
||||||
DLP_DEFAULT_RULES,
|
|
||||||
CloudBackend,
|
|
||||||
DlpEngine,
|
|
||||||
GatewayResult,
|
|
||||||
HallucinationGuard,
|
|
||||||
LLMGateway,
|
|
||||||
LocalBackend,
|
|
||||||
PromptRegistry,
|
|
||||||
RouteTarget,
|
|
||||||
SensitivityRouter,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def _build_kb() -> RagKnowledgeBase:
|
|
||||||
"""构造一个模板化知识库:工艺规范 / SOP / 国标 三类知识源。"""
|
|
||||||
kb = RagKnowledgeBase()
|
|
||||||
docs = [
|
|
||||||
build_document(
|
|
||||||
title="氯化车间操作规程",
|
|
||||||
text="氯化车间 1#炉正常运行温度区间 850~920℃,超过 950℃ 属于超温,"
|
|
||||||
"应立即减少通氯量并检查冷却系统。停机检修须挂牌上锁。",
|
|
||||||
category="process",
|
|
||||||
),
|
|
||||||
build_document(
|
|
||||||
title="海绵钛氯化工序 SOP",
|
|
||||||
text="氯化工序标准作业指导书:开机前确认氯气流量计归零,"
|
|
||||||
"升温阶段按 50℃/h 速率升温至 850℃。异常停机时按紧急停机程序处置。",
|
|
||||||
category="sop",
|
|
||||||
),
|
|
||||||
build_document(
|
|
||||||
title="工业氯化工艺国家标准",
|
|
||||||
text="GB/T XXXX 工业氯化工艺安全规范:氯化炉设计压力不低于 0.6MPa,"
|
|
||||||
"操作人员持证上岗,关键参数实时记录留存不少于 3 年。",
|
|
||||||
category="standard",
|
|
||||||
),
|
|
||||||
]
|
|
||||||
kb.add_documents(docs)
|
|
||||||
return kb
|
|
||||||
|
|
||||||
|
|
||||||
def _build_gateway() -> LLMGateway:
|
|
||||||
"""构造一个可运行的混合网关:注册 qa 提示词 + 默认 DLP/路由/校验。"""
|
|
||||||
prompts = PromptRegistry()
|
|
||||||
prompts.update(
|
|
||||||
name="qa",
|
|
||||||
version="1.0.0",
|
|
||||||
text="你是工业 AI 优化助手。基于知识库回答:{query}",
|
|
||||||
description="问答主提示词 v1.0.0",
|
|
||||||
)
|
|
||||||
return LLMGateway(
|
|
||||||
dlp=DlpEngine(),
|
|
||||||
router=SensitivityRouter(),
|
|
||||||
prompts=prompts,
|
|
||||||
guard=HallucinationGuard(),
|
|
||||||
local=LocalBackend(echo_context=True),
|
|
||||||
cloud=CloudBackend(echo_context=True),
|
|
||||||
prompt_name="qa",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class QueryPipelineE2ETest(unittest.TestCase):
|
|
||||||
"""RAG 检索 → LLM 网关编排 全链路联调。"""
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def setUpClass(cls) -> None:
|
|
||||||
cls.kb = _build_kb()
|
|
||||||
cls.gateway = _build_gateway()
|
|
||||||
|
|
||||||
# -- RAG 检索 ----------------------------------------------------------
|
|
||||||
def test_kb_retrieval_returns_relevant_chunks_with_source(self) -> None:
|
|
||||||
"""检索命中文档片段并带来源(引用溯源基础)。"""
|
|
||||||
hits = self.kb.search("氯化炉温度", top_k=3)
|
|
||||||
self.assertGreater(len(hits), 0)
|
|
||||||
# 命中片段必须携带来源信息(文档标题 / 类别)
|
|
||||||
for hit in hits:
|
|
||||||
self.assertIsNotNone(hit.chunk.title)
|
|
||||||
self.assertIn(hit.chunk.category, ("process", "sop", "standard"))
|
|
||||||
# 相关片段应命中"温度"相关内容
|
|
||||||
joined = " ".join(h.chunk.text for h in hits)
|
|
||||||
self.assertIn("温度", joined)
|
|
||||||
|
|
||||||
def test_kb_category_filter(self) -> None:
|
|
||||||
"""类别过滤:仅检索 SOP 知识源。"""
|
|
||||||
from rag_kb import KnowledgeSourceKind
|
|
||||||
hits = self.kb.search("升温", top_k=5, categories=[KnowledgeSourceKind.SOP])
|
|
||||||
self.assertGreater(len(hits), 0)
|
|
||||||
for hit in hits:
|
|
||||||
self.assertEqual(hit.chunk.category, "sop")
|
|
||||||
|
|
||||||
# -- 问答闭环(普通问题 → 本地后端)------------------------------------
|
|
||||||
def test_normal_question_routes_local_with_rag_context(self) -> None:
|
|
||||||
"""普通工艺问题:路由到本地后端,回答含 RAG 来源引用。"""
|
|
||||||
query = "氯化车间 1#炉的正常运行温度是多少?"
|
|
||||||
hits = self.kb.search(query, top_k=3)
|
|
||||||
sources = [h.chunk.title for h in hits]
|
|
||||||
|
|
||||||
result = self.gateway.ask(query, rag_context=sources, confidence=0.95)
|
|
||||||
|
|
||||||
self.assertIsInstance(result, GatewayResult)
|
|
||||||
self.assertEqual(result.route.target, RouteTarget.LOCAL)
|
|
||||||
# 本地后端 echo_context 时输出含来源标记
|
|
||||||
self.assertGreater(len(result.answer), 0)
|
|
||||||
if sources:
|
|
||||||
self.assertIn(sources[0], result.answer)
|
|
||||||
# 溯源校验通过(有来源支撑)
|
|
||||||
self.assertTrue(result.verdict.supported)
|
|
||||||
self.assertFalse(result.needs_human)
|
|
||||||
|
|
||||||
# -- 敏感数据 DLP 拦截(fail-closed)-----------------------------------
|
|
||||||
def test_sensitive_query_blocked_by_dlp(self) -> None:
|
|
||||||
"""含敏感数据的问题被 DLP 拦截 → 路由 block → 转人工。"""
|
|
||||||
# 身份证号(DLP 默认规则命中)
|
|
||||||
query = "请查询员工 110101199003078834 的工资"
|
|
||||||
result = self.gateway.ask(query, confidence=1.0)
|
|
||||||
|
|
||||||
self.assertEqual(result.route.target, RouteTarget.BLOCK)
|
|
||||||
self.assertTrue(result.needs_human)
|
|
||||||
# block 时不调用后端,给出人工确认占位
|
|
||||||
self.assertIn("人工", result.answer)
|
|
||||||
|
|
||||||
# -- 脱敏/通用问题路由到云端 ------------------------------------------
|
|
||||||
def test_generic_question_routes_cloud(self) -> None:
|
|
||||||
"""通用(非敏感)问题经 DLP 放行后可路由云端(这里默认路由 local,
|
|
||||||
需显式配置 cloud 规则才走云端)。验证默认 local 闭环正常。"""
|
|
||||||
query = "今天的天气如何?"
|
|
||||||
result = self.gateway.ask(query, confidence=0.9)
|
|
||||||
# 默认无规则命中 → local
|
|
||||||
self.assertEqual(result.route.target, RouteTarget.LOCAL)
|
|
||||||
self.assertFalse(result.needs_human)
|
|
||||||
|
|
||||||
# -- 高利害低信度转人工 ------------------------------------------------
|
|
||||||
def test_high_stakes_low_confidence_to_human(self) -> None:
|
|
||||||
"""高利害提示词 + 低信度 → 转人工复核(幻觉防线)。"""
|
|
||||||
prompts = PromptRegistry()
|
|
||||||
prompts.update(name="alarm_explain", version="1.0.0",
|
|
||||||
text="解释报警:{query}", description="报警解释(高利害)")
|
|
||||||
gateway = LLMGateway(
|
|
||||||
prompts=prompts,
|
|
||||||
prompt_name="alarm_explain",
|
|
||||||
high_stakes_names=["alarm_explain"],
|
|
||||||
)
|
|
||||||
result = gateway.ask("1#炉超温报警", rag_context=["氯化车间操作规程"],
|
|
||||||
confidence=0.3) # 低信度
|
|
||||||
self.assertTrue(result.needs_human)
|
|
||||||
self.assertEqual(result.verdict.action, "human_review")
|
|
||||||
|
|
||||||
# -- 审计可追溯 --------------------------------------------------------
|
|
||||||
def test_audit_drain_after_query(self) -> None:
|
|
||||||
"""每次 ask() 后各组件审计记录可统一导出(DLP/路由/Prompt/幻觉)。"""
|
|
||||||
self.gateway.ask("氯化炉温度区间", confidence=0.9)
|
|
||||||
audits = self.gateway.drain_audits()
|
|
||||||
self.assertIn("dlp", audits)
|
|
||||||
self.assertIn("router", audits)
|
|
||||||
self.assertIn("prompts", audits)
|
|
||||||
self.assertIn("guard", audits)
|
|
||||||
# 路由审计应有记录
|
|
||||||
self.assertGreater(len(audits["router"]), 0)
|
|
||||||
|
|
||||||
|
|
||||||
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