Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03ae81a217 |
@@ -16,6 +16,9 @@
|
|||||||
高利害信度阈值 → 人工确认。
|
高利害信度阈值 → 人工确认。
|
||||||
- gateway 混合网关主编排(EPIC #6 主体):路由 → 生成 → 溯源校验 →
|
- gateway 混合网关主编排(EPIC #6 主体):路由 → 生成 → 溯源校验 →
|
||||||
DLP 出站防线,端到端闭环。
|
DLP 出站防线,端到端闭环。
|
||||||
|
- backends 推理后端抽象(Issue #57,PRD 5.6):``InferenceBackend`` 抽象接口
|
||||||
|
(``load_model / infer / health_check / unload``),5090 实现(Triton/ONNX)
|
||||||
|
与昇腾实现(ACL/CANN)均实现该接口;业务代码仅依赖接口,不感知硬件。
|
||||||
|
|
||||||
测试:`python -m unittest discover -s tests -v`(在 core/llm-gateway 目录下执行)。
|
测试:`python -m unittest discover -s tests -v`(在 core/llm-gateway 目录下执行)。
|
||||||
"""
|
"""
|
||||||
@@ -45,10 +48,17 @@ from .hallucination import (
|
|||||||
GuardVerdict,
|
GuardVerdict,
|
||||||
HallucinationGuard,
|
HallucinationGuard,
|
||||||
)
|
)
|
||||||
|
from .backends import (
|
||||||
|
BackendCapabilities,
|
||||||
|
BackendHealth,
|
||||||
|
InferResult,
|
||||||
|
InferenceBackend,
|
||||||
|
build_backend,
|
||||||
|
default_registry,
|
||||||
|
)
|
||||||
from .gateway import (
|
from .gateway import (
|
||||||
CloudBackend,
|
CloudBackend,
|
||||||
GatewayResult,
|
GatewayResult,
|
||||||
InferenceBackend,
|
|
||||||
LLMGateway,
|
LLMGateway,
|
||||||
LocalBackend,
|
LocalBackend,
|
||||||
)
|
)
|
||||||
@@ -62,7 +72,10 @@ __all__ = [
|
|||||||
"PromptVersion", "PromptChange", "PromptRegistry", "validate_semver",
|
"PromptVersion", "PromptChange", "PromptRegistry", "validate_semver",
|
||||||
# hallucination
|
# hallucination
|
||||||
"GuardVerdict", "HallucinationGuard",
|
"GuardVerdict", "HallucinationGuard",
|
||||||
|
# backends (Issue #57)
|
||||||
|
"BackendCapabilities", "BackendHealth", "InferResult", "InferenceBackend",
|
||||||
|
"build_backend", "default_registry",
|
||||||
# gateway
|
# gateway
|
||||||
"InferenceBackend", "LocalBackend", "CloudBackend",
|
"LocalBackend", "CloudBackend",
|
||||||
"GatewayResult", "LLMGateway",
|
"GatewayResult", "LLMGateway",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1,301 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""iAOP-Core · LLM 网关 —— 推理后端抽象接口(Issue #57,PRD 5.6)。
|
||||||
|
|
||||||
|
PRD 5.6「⑥ 部署底座」明确要求:
|
||||||
|
|
||||||
|
定义统一 ``InferenceBackend`` 接口(``loadModel / infer / health / unload``),
|
||||||
|
5090 实现(Triton/ONNX)与昇腾实现(ACL/CANN)均实现该接口;
|
||||||
|
**业务代码仅依赖接口,不感知硬件**;切换后端 = 改适配层配置,不动业务代码。
|
||||||
|
|
||||||
|
本模块把原先内联在 ``gateway.py`` 里的薄弱 ``InferenceBackend`` 提炼为正式的
|
||||||
|
抽象基类(ABC),并补齐 PRD 要求的生命周期方法与能力声明,使后续子任务:
|
||||||
|
|
||||||
|
- #44 本地 70B 模型接入与推理封装(vLLM/TGI)
|
||||||
|
- #45 云端 API(Qwen/DeepSeek)接入与安全网关
|
||||||
|
- #58 GPU 后端实现(NVIDIA,Triton/ONNX)
|
||||||
|
- #59 昇腾 NPU 后端适配(CANN/ACL)
|
||||||
|
|
||||||
|
都能在**同一契约**下落地,业务编排(``LLMGateway``)零改动。
|
||||||
|
|
||||||
|
设计要点
|
||||||
|
--------
|
||||||
|
1. **接口最小且完备**:仅约束 PRD 列出的四个生命周期动作 ``load_model / infer /
|
||||||
|
health_check / unload``,外加能力声明 ``BackendCapabilities``(流式 / 最大并发 /
|
||||||
|
是否出厂内闭环),供路由与调度决策。
|
||||||
|
2. **向后兼容**:保留 ``generate(prompt, context)`` 便捷方法(默认转发到
|
||||||
|
``infer``),既有 ``LLMGateway.ask()`` 调用路径不变;老测试不受影响。
|
||||||
|
3. **可注入 / 可 mock**:所有方法纯逻辑、无外部 IO 依赖;真实硬件/网络交互由
|
||||||
|
各子类在 ``infer`` 内部完成(子类负责导入厂商 SDK 并做 ``ImportError`` 容错)。
|
||||||
|
4. **健康探针**:``health_check`` 返回结构化 ``BackendHealth``,供可用性监控探针
|
||||||
|
(Issue #61)与灰度发布(PRD 5.6 配置点)判定后端是否就绪。
|
||||||
|
|
||||||
|
测试:``python -m unittest discover -s tests -v``(在 core/llm-gateway 目录下执行)。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
from typing import Dict, Iterator, List, Optional, Sequence
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 值对象:能力声明 / 健康状态 / 推理结果
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class BackendCapabilities:
|
||||||
|
"""后端能力声明,供路由 / 调度 / 灰度决策。
|
||||||
|
|
||||||
|
Attributes:
|
||||||
|
streaming: 是否支持流式输出(逐 token 返回)。
|
||||||
|
max_concurrency: 最大并发推理数(None 表示不限 / 由外部限流)。
|
||||||
|
on_premises: 是否数据出厂内闭环(本地后端 True,云端 False)。
|
||||||
|
modalities: 支持的输出形态,如 ``("text",)``。
|
||||||
|
"""
|
||||||
|
|
||||||
|
streaming: bool = False
|
||||||
|
max_concurrency: Optional[int] = None
|
||||||
|
on_premises: bool = False
|
||||||
|
modalities: Sequence[str] = ("text",)
|
||||||
|
|
||||||
|
def supports(self, modality: str) -> bool:
|
||||||
|
"""是否支持某种输出形态(text / image / ...)。"""
|
||||||
|
return modality in self.modalities
|
||||||
|
|
||||||
|
def to_dict(self) -> Dict[str, object]:
|
||||||
|
return {
|
||||||
|
"streaming": self.streaming,
|
||||||
|
"max_concurrency": self.max_concurrency,
|
||||||
|
"on_premises": self.on_premises,
|
||||||
|
"modalities": list(self.modalities),
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class BackendHealth:
|
||||||
|
"""后端健康探针结果(Issue #61 可用性监控探针消费)。"""
|
||||||
|
|
||||||
|
healthy: bool
|
||||||
|
detail: str = ""
|
||||||
|
checked_at: str = field(
|
||||||
|
default_factory=lambda: datetime.now(timezone.utc).isoformat())
|
||||||
|
|
||||||
|
def to_dict(self) -> Dict[str, object]:
|
||||||
|
return {
|
||||||
|
"healthy": self.healthy,
|
||||||
|
"detail": self.detail,
|
||||||
|
"checked_at": self.checked_at,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class InferResult:
|
||||||
|
"""一次 ``infer`` 的结构化结果(含审计所需元信息)。
|
||||||
|
|
||||||
|
保留 ``text`` 主输出以兼容旧 ``generate`` 返回 ``str`` 的调用方;
|
||||||
|
``prompt_tokens`` / ``completion_tokens`` 供计费与配额(PRD 5.6 配置点)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
text: str
|
||||||
|
backend_name: str
|
||||||
|
model_id: str = ""
|
||||||
|
prompt_tokens: Optional[int] = None
|
||||||
|
completion_tokens: Optional[int] = None
|
||||||
|
latency_ms: Optional[float] = None
|
||||||
|
|
||||||
|
def to_dict(self) -> Dict[str, object]:
|
||||||
|
return {
|
||||||
|
"text": self.text,
|
||||||
|
"backend_name": self.backend_name,
|
||||||
|
"model_id": self.model_id,
|
||||||
|
"prompt_tokens": self.prompt_tokens,
|
||||||
|
"completion_tokens": self.completion_tokens,
|
||||||
|
"latency_ms": self.latency_ms,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 抽象接口(PRD 5.6:loadModel / infer / health / unload)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class InferenceBackend(ABC):
|
||||||
|
"""推理后端抽象接口(对齐 PRD 5.6 ``InferenceBackend`` 契约)。
|
||||||
|
|
||||||
|
业务编排(``LLMGateway``)只依赖本接口,**不感知**具体硬件 / 厂商;
|
||||||
|
切换后端 = 换实现类 + 改配置,业务代码不动。子类必须实现四个生命周期方法:
|
||||||
|
|
||||||
|
- :meth:`load_model`:加载 / 绑定模型(可幂等,重复加载返回已加载实例)。
|
||||||
|
- :meth:`infer`:给定 prompt 与 RAG 上下文生成回答(核心推理动作)。
|
||||||
|
- :meth:`health_check`:探针,返回 :class:`BackendHealth`。
|
||||||
|
- :meth:`unload`:释放模型资源(可幂等)。
|
||||||
|
|
||||||
|
便捷方法 :meth:`generate` 默认转发到 :meth:`infer` 并只取 ``text``,
|
||||||
|
保留与旧 ``LLMGateway.ask()`` 的二进制兼容。
|
||||||
|
"""
|
||||||
|
|
||||||
|
#: 后端短名(local-70b / cloud-api / gpu-triton / npu-cann ...),子类覆盖。
|
||||||
|
name: str = "base"
|
||||||
|
|
||||||
|
@property
|
||||||
|
def capabilities(self) -> BackendCapabilities:
|
||||||
|
"""后端能力声明,子类按需覆盖。默认:非流式、出厂外、仅文本。"""
|
||||||
|
return BackendCapabilities()
|
||||||
|
|
||||||
|
# -- 生命周期(子类必须实现)------------------------------------------
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def load_model(self, model_id: str) -> None:
|
||||||
|
"""加载 / 绑定指定模型。幂等:重复加载同一 model_id 不报错。"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def infer(self, prompt: str,
|
||||||
|
context: Optional[Sequence[str]] = None) -> InferResult:
|
||||||
|
"""根据 prompt 与 RAG 上下文生成回答(核心推理动作)。"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def health_check(self) -> BackendHealth:
|
||||||
|
"""健康探针,返回结构化健康状态。"""
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def unload(self) -> None:
|
||||||
|
"""释放模型资源。幂等:未加载时调用不报错。"""
|
||||||
|
|
||||||
|
# -- 向后兼容便捷方法 --------------------------------------------------
|
||||||
|
|
||||||
|
def generate(self, prompt: str, context: Sequence[str]) -> str:
|
||||||
|
"""旧调用入口:等价于 ``infer(prompt, context).text``。
|
||||||
|
|
||||||
|
保留是为了不破坏 ``LLMGateway.ask()`` 既有的 ``backend.generate(...)``
|
||||||
|
调用路径;新代码应直接使用 :meth:`infer` 拿到完整 :class:`InferResult`。
|
||||||
|
"""
|
||||||
|
return self.infer(prompt, context).text
|
||||||
|
|
||||||
|
def __repr__(self) -> str: # pragma: no cover - 调试辅助
|
||||||
|
return f"<{type(self).__name__} name={self.name!r}>"
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 占位实现(子任务 #44 / #45 / #58 / #59 将各自替换为真实后端)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class _PlaceholderBackend(InferenceBackend):
|
||||||
|
"""占位后端公共骨架:固定回显答案 + 引用溯源回显,供端到端测试与演示。
|
||||||
|
|
||||||
|
真实后端(#44 本地 70B / #45 云端 API / #58 GPU / #59 昇腾)继承本类后,
|
||||||
|
只需覆盖 :meth:`infer` 的生成逻辑与 :meth:`health_check` 的探针实现即可;
|
||||||
|
生命周期与能力声明已由本类 / 子类提供。
|
||||||
|
"""
|
||||||
|
|
||||||
|
placeholder_prefix = "[占位]"
|
||||||
|
|
||||||
|
def __init__(self, model_id: str, echo_context: bool = True) -> None:
|
||||||
|
self._model_id = model_id
|
||||||
|
self._loaded = False
|
||||||
|
self._loaded_model_id: Optional[str] = None
|
||||||
|
self.echo_context = echo_context
|
||||||
|
|
||||||
|
# 生命周期
|
||||||
|
def load_model(self, model_id: str) -> None:
|
||||||
|
# 幂等:重复加载同一 model_id 视作成功;换模型也允许(演示用)。
|
||||||
|
self._loaded = True
|
||||||
|
self._loaded_model_id = model_id or self._model_id
|
||||||
|
|
||||||
|
def infer(self, prompt: str,
|
||||||
|
context: Optional[Sequence[str]] = None) -> InferResult:
|
||||||
|
if not self._loaded:
|
||||||
|
# 演示态允许惰性自加载,真实后端可改为 raise RuntimeError("未加载模型")
|
||||||
|
self.load_model(self._model_id)
|
||||||
|
ctx = list(context or [])
|
||||||
|
head = f"{self.placeholder_prefix} {prompt[:40]}"
|
||||||
|
refs = ""
|
||||||
|
if self.echo_context:
|
||||||
|
for src in ctx[:3]:
|
||||||
|
refs += f"\n[来源: {src}]"
|
||||||
|
return InferResult(
|
||||||
|
text=head + refs,
|
||||||
|
backend_name=self.name,
|
||||||
|
model_id=self._loaded_model_id or self._model_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
def health_check(self) -> BackendHealth:
|
||||||
|
return BackendHealth(
|
||||||
|
healthy=self._loaded,
|
||||||
|
detail="loaded" if self._loaded else "not_loaded",
|
||||||
|
)
|
||||||
|
|
||||||
|
def unload(self) -> None:
|
||||||
|
# 幂等:未加载也安全
|
||||||
|
self._loaded = False
|
||||||
|
self._loaded_model_id = None
|
||||||
|
|
||||||
|
|
||||||
|
class LocalBackend(_PlaceholderBackend):
|
||||||
|
"""本地 70B 后端占位实现:数据不出厂(敏感 / 核心走此通道)。
|
||||||
|
|
||||||
|
子任务 #44 / #58 将替换 ``infer`` 为真实本地模型推理封装(vLLM/TGI/Triton)。
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = "local-70b"
|
||||||
|
placeholder_prefix = "[本地70B占位]"
|
||||||
|
|
||||||
|
def __init__(self, echo_context: bool = True,
|
||||||
|
model_id: str = "local-70b-base") -> None:
|
||||||
|
super().__init__(model_id=model_id, echo_context=echo_context)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def capabilities(self) -> BackendCapabilities:
|
||||||
|
# 本地后端:出厂内闭环、可流式、单卡典型并发 8(演示默认值)
|
||||||
|
return BackendCapabilities(
|
||||||
|
streaming=True, max_concurrency=8, on_premises=True,
|
||||||
|
modalities=("text",))
|
||||||
|
|
||||||
|
|
||||||
|
class CloudBackend(_PlaceholderBackend):
|
||||||
|
"""云端 API 后端占位实现:仅接收 DLP 放行的脱敏 / 通用内容。
|
||||||
|
|
||||||
|
子任务 #45 将替换为 Qwen / DeepSeek API 接入 + 安全网关。
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = "cloud-api"
|
||||||
|
placeholder_prefix = "[云端API占位]"
|
||||||
|
|
||||||
|
def __init__(self, echo_context: bool = True,
|
||||||
|
model_id: str = "cloud-qwen-plus") -> None:
|
||||||
|
super().__init__(model_id=model_id, echo_context=echo_context)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def capabilities(self) -> BackendCapabilities:
|
||||||
|
# 云端后端:数据出厂、支持流式、并发受厂商配额限制(演示默认 4)
|
||||||
|
return BackendCapabilities(
|
||||||
|
streaming=True, max_concurrency=4, on_premises=False,
|
||||||
|
modalities=("text",))
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 后端注册表(配置驱动切换,对齐 PRD「切换后端 = 改适配层配置」)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
def default_registry() -> Dict[str, type]:
|
||||||
|
"""默认后端注册表:name → 实现类。新增后端在此登记一行即可被配置选用。"""
|
||||||
|
return {
|
||||||
|
"local-70b": LocalBackend,
|
||||||
|
"cloud-api": CloudBackend,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def build_backend(name: str, **kwargs) -> InferenceBackend:
|
||||||
|
"""按 name 从默认注册表构造后端实例(配置驱动切换的入口)。
|
||||||
|
|
||||||
|
未知 name 抛 ``ValueError``,列出已知项便于排错。
|
||||||
|
"""
|
||||||
|
registry = default_registry()
|
||||||
|
cls = registry.get(name)
|
||||||
|
if cls is None:
|
||||||
|
known = ", ".join(sorted(registry))
|
||||||
|
raise ValueError(f"未知推理后端 {name!r},已知: {known}")
|
||||||
|
return cls(**kwargs)
|
||||||
+18
-61
@@ -12,12 +12,15 @@
|
|||||||
- `router`(SensitivityRouter):敏感度分级路由(local / cloud / block);
|
- `router`(SensitivityRouter):敏感度分级路由(local / cloud / block);
|
||||||
- `prompts`(PromptRegistry):提示词模板版本绑定(可复现);
|
- `prompts`(PromptRegistry):提示词模板版本绑定(可复现);
|
||||||
- `guard`(HallucinationGuard):引用溯源 + 信度阈值 → 人工确认;
|
- `guard`(HallucinationGuard):引用溯源 + 信度阈值 → 人工确认;
|
||||||
- `backends`(LocalBackend / CloudBackend):推理后端抽象(可注入)。
|
- `backends`(InferenceBackend / LocalBackend / CloudBackend):推理后端抽象
|
||||||
|
(可注入)。接口定义已提炼到 `backends.py`(Issue #57,对齐 PRD 5.6)。
|
||||||
|
|
||||||
设计说明:
|
设计说明:
|
||||||
- 本版提供**编排闭环 + 后端抽象接口**,本地 70B / 云端 API 的具体接入
|
- 本版提供**编排闭环 + 后端抽象接口**,本地 70B / 云端 API 的具体接入
|
||||||
由子任务 #44 / #45 实现;`LocalBackend` / `CloudBackend` 默认内置一个
|
由子任务 #44 / #45 实现;`LocalBackend` / `CloudBackend` 默认内置一个
|
||||||
最小实现(返回固定占位答案 + 回显引用),供端到端测试与演示。
|
最小实现(返回固定占位答案 + 回显引用),供端到端测试与演示。
|
||||||
|
- 推理后端契约(`loadModel / infer / health_check / unload`)见 `backends.py`,
|
||||||
|
本模块仅消费其 `generate` / `name`,业务代码不感知具体硬件。
|
||||||
|
|
||||||
测试:`python -m unittest discover -s tests -v`(在 core/llm-gateway 目录下执行)。
|
测试:`python -m unittest discover -s tests -v`(在 core/llm-gateway 目录下执行)。
|
||||||
"""
|
"""
|
||||||
@@ -26,71 +29,25 @@ from __future__ import annotations
|
|||||||
import uuid
|
import uuid
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from typing import Callable, Dict, List, Optional, Sequence
|
from typing import Dict, List, Optional, Sequence
|
||||||
|
|
||||||
from .dlp import DlpEngine
|
from .dlp import DlpEngine
|
||||||
from .router import RouteDecision, RouteTarget, SensitivityRouter
|
from .router import RouteDecision, RouteTarget, SensitivityRouter
|
||||||
from .prompts import PromptRegistry
|
from .prompts import PromptRegistry
|
||||||
from .hallucination import GuardVerdict, HallucinationGuard
|
from .hallucination import GuardVerdict, HallucinationGuard
|
||||||
|
# 推理后端抽象(Issue #57):契约定义在 backends.py,这里仅做再导出,
|
||||||
# ---------------------------------------------------------------------------
|
# 保持 ``from .gateway import InferenceBackend/LocalBackend/CloudBackend`` 的
|
||||||
# 推理后端抽象(Issue #44 / #45 将实现具体后端,业务代码只依赖本接口)
|
# 向后兼容(既有 import 路径与 ``LLMGateway`` 依赖均不变)。
|
||||||
# ---------------------------------------------------------------------------
|
from .backends import (
|
||||||
|
BackendCapabilities,
|
||||||
|
BackendHealth,
|
||||||
class InferenceBackend:
|
CloudBackend,
|
||||||
"""推理后端接口抽象(对齐 PRD 5.6 InferenceBackend 思想)。
|
InferResult,
|
||||||
|
InferenceBackend,
|
||||||
业务代码只依赖本接口,不感知具体硬件/厂商;切换后端 = 换实现。
|
LocalBackend,
|
||||||
子任务 #44(本地 70B)、#45(云端 Qwen/DeepSeek)将各自实现本接口。
|
build_backend,
|
||||||
"""
|
default_registry,
|
||||||
|
)
|
||||||
name: str = "base"
|
|
||||||
|
|
||||||
def generate(self, prompt: str, context: Sequence[str]) -> str:
|
|
||||||
"""根据 prompt 与 RAG 上下文生成回答。子类实现。"""
|
|
||||||
raise NotImplementedError
|
|
||||||
|
|
||||||
|
|
||||||
class LocalBackend(InferenceBackend):
|
|
||||||
"""本地 70B 后端占位实现:数据不出厂(敏感/核心走此通道)。
|
|
||||||
|
|
||||||
子任务 #44 将替换为真实本地模型推理封装(vLLM/TGI 等)。
|
|
||||||
"""
|
|
||||||
|
|
||||||
name = "local-70b"
|
|
||||||
|
|
||||||
def __init__(self, echo_context: bool = True) -> None:
|
|
||||||
self.echo_context = echo_context
|
|
||||||
|
|
||||||
def generate(self, prompt: str, context: Sequence[str]) -> str:
|
|
||||||
head = f"[本地70B占位] {prompt[:40]}"
|
|
||||||
refs = ""
|
|
||||||
if self.echo_context:
|
|
||||||
for i, src in enumerate(context[:3], 1):
|
|
||||||
refs += f"\n[来源: {src}]"
|
|
||||||
return head + refs
|
|
||||||
|
|
||||||
|
|
||||||
class CloudBackend(InferenceBackend):
|
|
||||||
"""云端 API 后端占位实现:仅接收 DLP 放行的脱敏/通用内容。
|
|
||||||
|
|
||||||
子任务 #45 将替换为 Qwen/DeepSeek API 接入 + 安全网关。
|
|
||||||
"""
|
|
||||||
|
|
||||||
name = "cloud-api"
|
|
||||||
|
|
||||||
def __init__(self, echo_context: bool = True) -> None:
|
|
||||||
self.echo_context = echo_context
|
|
||||||
|
|
||||||
def generate(self, prompt: str, context: Sequence[str]) -> str:
|
|
||||||
head = f"[云端API占位] {prompt[:40]}"
|
|
||||||
refs = ""
|
|
||||||
if self.echo_context:
|
|
||||||
for i, src in enumerate(context[:3], 1):
|
|
||||||
refs += f"\n[来源: {src}]"
|
|
||||||
return head + refs
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# 网关输出
|
# 网关输出
|
||||||
|
|||||||
@@ -0,0 +1,301 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""推理后端抽象接口(backends,Issue #57,PRD 5.6)单元测试。
|
||||||
|
|
||||||
|
覆盖:
|
||||||
|
- 抽象基类不可直接实例化(必须由子类实现四个生命周期方法);
|
||||||
|
- 值对象 BackendCapabilities / BackendHealth / InferResult 的字段与序列化;
|
||||||
|
- LocalBackend / CloudBackend 占位实现的生命周期(load/infer/health/unload)与幂等;
|
||||||
|
- 向后兼容:``generate`` 转发到 ``infer`` 并返回 ``text``;
|
||||||
|
- 能力声明差异(本地出厂内闭环 / 云端出厂外);
|
||||||
|
- 注册表与 ``build_backend`` 的配置驱动构造 + 未知后端报错。
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import unittest
|
||||||
|
from abc import ABC
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
import _bootstrap # noqa: F401
|
||||||
|
|
||||||
|
from llm_gateway.backends import ( # noqa: E402
|
||||||
|
BackendCapabilities,
|
||||||
|
BackendHealth,
|
||||||
|
CloudBackend,
|
||||||
|
InferResult,
|
||||||
|
InferenceBackend,
|
||||||
|
LocalBackend,
|
||||||
|
_PlaceholderBackend,
|
||||||
|
build_backend,
|
||||||
|
default_registry,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 抽象基类契约
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class AbstractionContractTest(unittest.TestCase):
|
||||||
|
"""PRD 5.6:InferenceBackend 是抽象接口,业务代码只依赖它。"""
|
||||||
|
|
||||||
|
def test_cannot_instantiate_abstract_base(self):
|
||||||
|
# 缺少四个抽象方法 → 不能实例化
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
InferenceBackend() # noqa: E721
|
||||||
|
|
||||||
|
def test_is_abc_subclass(self):
|
||||||
|
self.assertTrue(issubclass(InferenceBackend, ABC))
|
||||||
|
|
||||||
|
def test_required_abstract_methods(self):
|
||||||
|
# PRD 5.6 明列的生命周期动作
|
||||||
|
abstract = InferenceBackend.__abstractmethods__
|
||||||
|
for name in ("load_model", "infer", "health_check", "unload"):
|
||||||
|
self.assertIn(name, abstract)
|
||||||
|
|
||||||
|
def test_concrete_backends_are_inference_backends(self):
|
||||||
|
for cls in (LocalBackend, CloudBackend):
|
||||||
|
self.assertTrue(issubclass(cls, InferenceBackend),
|
||||||
|
f"{cls.__name__} 必须实现 InferenceBackend")
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 值对象
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class BackendCapabilitiesTest(unittest.TestCase):
|
||||||
|
def test_defaults(self):
|
||||||
|
cap = BackendCapabilities()
|
||||||
|
self.assertFalse(cap.streaming)
|
||||||
|
self.assertIsNone(cap.max_concurrency)
|
||||||
|
self.assertFalse(cap.on_premises)
|
||||||
|
self.assertEqual(cap.modalities, ("text",))
|
||||||
|
|
||||||
|
def test_supports_modality(self):
|
||||||
|
cap = BackendCapabilities(modalities=("text", "image"))
|
||||||
|
self.assertTrue(cap.supports("text"))
|
||||||
|
self.assertTrue(cap.supports("image"))
|
||||||
|
self.assertFalse(cap.supports("audio"))
|
||||||
|
|
||||||
|
def test_to_dict_roundtrip(self):
|
||||||
|
cap = BackendCapabilities(streaming=True, max_concurrency=4,
|
||||||
|
on_premises=False, modalities=("text",))
|
||||||
|
d = cap.to_dict()
|
||||||
|
self.assertEqual(d["streaming"], True)
|
||||||
|
self.assertEqual(d["max_concurrency"], 4)
|
||||||
|
self.assertEqual(d["modalities"], ["text"])
|
||||||
|
|
||||||
|
|
||||||
|
class BackendHealthTest(unittest.TestCase):
|
||||||
|
def test_fields(self):
|
||||||
|
h = BackendHealth(healthy=True, detail="ok")
|
||||||
|
self.assertTrue(h.healthy)
|
||||||
|
self.assertEqual(h.detail, "ok")
|
||||||
|
self.assertTrue(h.checked_at) # 自动生成时间戳
|
||||||
|
|
||||||
|
def test_to_dict(self):
|
||||||
|
d = BackendHealth(healthy=False, detail="down").to_dict()
|
||||||
|
self.assertEqual(d["healthy"], False)
|
||||||
|
self.assertIn("checked_at", d)
|
||||||
|
|
||||||
|
|
||||||
|
class InferResultTest(unittest.TestCase):
|
||||||
|
def test_required_fields(self):
|
||||||
|
r = InferResult(text="hello", backend_name="local-70b")
|
||||||
|
self.assertEqual(r.text, "hello")
|
||||||
|
self.assertEqual(r.backend_name, "local-70b")
|
||||||
|
self.assertIsNone(r.prompt_tokens)
|
||||||
|
|
||||||
|
def test_to_dict(self):
|
||||||
|
r = InferResult(text="a", backend_name="b", model_id="m",
|
||||||
|
prompt_tokens=3, completion_tokens=5)
|
||||||
|
d = r.to_dict()
|
||||||
|
self.assertEqual(d["text"], "a")
|
||||||
|
self.assertEqual(d["prompt_tokens"], 3)
|
||||||
|
self.assertEqual(d["completion_tokens"], 5)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 占位实现生命周期
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class PlaceholderLifecycleTest(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.b = LocalBackend()
|
||||||
|
|
||||||
|
def test_health_reflects_load_state(self):
|
||||||
|
# 未加载 → 不健康
|
||||||
|
self.assertFalse(self.b.health_check().healthy)
|
||||||
|
self.b.load_model("local-70b-base")
|
||||||
|
self.assertTrue(self.b.health_check().healthy)
|
||||||
|
|
||||||
|
def test_load_is_idempotent(self):
|
||||||
|
self.b.load_model("local-70b-base")
|
||||||
|
# 重复加载同一 model_id 不报错
|
||||||
|
self.b.load_model("local-70b-base")
|
||||||
|
self.assertTrue(self.b.health_check().healthy)
|
||||||
|
|
||||||
|
def test_infer_lazy_loads_when_not_loaded(self):
|
||||||
|
# 演示态:未显式 load_model 也能 infer(惰性自加载)
|
||||||
|
r = self.b.infer("炉温是多少", context=["SOP-炉温"])
|
||||||
|
self.assertIsInstance(r, InferResult)
|
||||||
|
self.assertEqual(r.backend_name, "local-70b")
|
||||||
|
self.assertIn("炉温是多少", r.text)
|
||||||
|
self.assertIn("[来源: SOP-炉温]", r.text)
|
||||||
|
|
||||||
|
def test_infer_after_explicit_load(self):
|
||||||
|
self.b.load_model("local-70b-base")
|
||||||
|
r = self.b.infer("hello")
|
||||||
|
self.assertEqual(r.model_id, "local-70b-base")
|
||||||
|
self.assertIn("hello", r.text)
|
||||||
|
|
||||||
|
def test_unload_is_idempotent(self):
|
||||||
|
self.b.load_model("local-70b-base")
|
||||||
|
self.b.unload()
|
||||||
|
self.assertFalse(self.b.health_check().healthy)
|
||||||
|
# 未加载再 unload 也不报错
|
||||||
|
self.b.unload()
|
||||||
|
|
||||||
|
def test_echo_context_disabled(self):
|
||||||
|
b = LocalBackend(echo_context=False)
|
||||||
|
b.load_model("m")
|
||||||
|
r = b.infer("q", context=["src1", "src2"])
|
||||||
|
self.assertNotIn("[来源:", r.text)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 向后兼容:generate 转发到 infer
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class BackwardCompatGenerateTest(unittest.TestCase):
|
||||||
|
def test_generate_returns_text_of_infer(self):
|
||||||
|
b = CloudBackend()
|
||||||
|
b.load_model("cloud-qwen-plus")
|
||||||
|
txt = b.generate("海绵钛是什么", context=["科普手册"])
|
||||||
|
# 与 infer().text 一致
|
||||||
|
self.assertEqual(txt, b.infer("海绵钛是什么", context=["科普手册"]).text)
|
||||||
|
self.assertIn("云端API占位", txt)
|
||||||
|
self.assertIn("[来源: 科普手册]", txt)
|
||||||
|
|
||||||
|
def test_gateway_still_works_with_new_backends(self):
|
||||||
|
# 集成校验:LLMGateway.ask() 经 generate 路径仍正常(不导入失败)。
|
||||||
|
# 复用 test_gateway.py 的模板配置加载 prompts,避免默认空注册表 KeyError。
|
||||||
|
from llm_gateway.dlp import DlpEngine
|
||||||
|
from llm_gateway.gateway import LLMGateway
|
||||||
|
from llm_gateway.prompts import PromptRegistry
|
||||||
|
from llm_gateway.router import SensitivityRouter
|
||||||
|
cfg_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||||
|
prompts = PromptRegistry.from_template_config(
|
||||||
|
os.path.join(cfg_dir, "config", "prompts.template.yaml"))
|
||||||
|
router = SensitivityRouter.from_template_config(
|
||||||
|
os.path.join(cfg_dir, "config", "router.template.yaml"))
|
||||||
|
gw = LLMGateway(
|
||||||
|
dlp=DlpEngine(), router=router, prompts=prompts,
|
||||||
|
local=LocalBackend(), cloud=CloudBackend())
|
||||||
|
result = gw.ask("海绵钛是什么", rag_context=["科普手册"])
|
||||||
|
self.assertTrue(result.answer)
|
||||||
|
# 后端占位回显特征仍在(证明走的是新 backends 的 generate 路径)
|
||||||
|
self.assertIn("云端API占位", result.answer)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 能力声明差异(本地 vs 云端)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class CapabilitiesDifferenceTest(unittest.TestCase):
|
||||||
|
def test_local_is_on_premises(self):
|
||||||
|
cap = LocalBackend().capabilities
|
||||||
|
self.assertTrue(cap.on_premises)
|
||||||
|
self.assertTrue(cap.streaming)
|
||||||
|
self.assertGreater(cap.max_concurrency, 0)
|
||||||
|
|
||||||
|
def test_cloud_is_off_premises(self):
|
||||||
|
cap = CloudBackend().capabilities
|
||||||
|
self.assertFalse(cap.on_premises)
|
||||||
|
self.assertTrue(cap.streaming)
|
||||||
|
|
||||||
|
def test_local_and_cloud_differ_on_premises(self):
|
||||||
|
# 关键差异:本地出厂内闭环,云端数据出厂
|
||||||
|
self.assertNotEqual(
|
||||||
|
LocalBackend().capabilities.on_premises,
|
||||||
|
CloudBackend().capabilities.on_premises,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 注册表与配置驱动构造
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class RegistryTest(unittest.TestCase):
|
||||||
|
def test_default_registry_has_known_backends(self):
|
||||||
|
reg = default_registry()
|
||||||
|
self.assertIn("local-70b", reg)
|
||||||
|
self.assertIn("cloud-api", reg)
|
||||||
|
self.assertIs(reg["local-70b"], LocalBackend)
|
||||||
|
self.assertIs(reg["cloud-api"], CloudBackend)
|
||||||
|
|
||||||
|
def test_build_backend_by_name(self):
|
||||||
|
b = build_backend("local-70b")
|
||||||
|
self.assertIsInstance(b, LocalBackend)
|
||||||
|
self.assertIsInstance(b, InferenceBackend)
|
||||||
|
self.assertEqual(b.name, "local-70b")
|
||||||
|
|
||||||
|
def test_build_unknown_backend_raises_with_hint(self):
|
||||||
|
with self.assertRaises(ValueError) as ctx:
|
||||||
|
build_backend("npu-cann") # 尚未实现(#59 才接入)
|
||||||
|
self.assertIn("npu-cann", str(ctx.exception))
|
||||||
|
self.assertIn("local-70b", str(ctx.exception)) # 提示已知项
|
||||||
|
|
||||||
|
def test_build_passes_kwargs(self):
|
||||||
|
b = build_backend("cloud-api", echo_context=False)
|
||||||
|
self.assertIsInstance(b, CloudBackend)
|
||||||
|
self.assertFalse(b.echo_context)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# 自定义后端通过实现接口接入(证明「业务代码不感知硬件」)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class CustomBackendImplementationTest(unittest.TestCase):
|
||||||
|
"""模拟 #59 昇腾后端:只需实现四个方法即可被当作 InferenceBackend 使用。"""
|
||||||
|
|
||||||
|
def test_custom_backend_satisfies_interface(self):
|
||||||
|
class NpuCannBackend(InferenceBackend):
|
||||||
|
name = "npu-cann"
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._loaded = False
|
||||||
|
|
||||||
|
def load_model(self, model_id):
|
||||||
|
self._loaded = True
|
||||||
|
|
||||||
|
def infer(self, prompt, context=None):
|
||||||
|
if not self._loaded:
|
||||||
|
self.load_model("ascend-cann")
|
||||||
|
return InferResult(text=f"[NPU] {prompt}", backend_name=self.name)
|
||||||
|
|
||||||
|
def health_check(self):
|
||||||
|
return BackendHealth(healthy=self._loaded)
|
||||||
|
|
||||||
|
def unload(self):
|
||||||
|
self._loaded = False
|
||||||
|
|
||||||
|
b = NpuCannBackend()
|
||||||
|
self.assertIsInstance(b, InferenceBackend)
|
||||||
|
self.assertFalse(b.health_check().healthy)
|
||||||
|
b.load_model("ascend-cann")
|
||||||
|
self.assertTrue(b.health_check().healthy)
|
||||||
|
self.assertEqual(b.infer("q").text, "[NPU] q")
|
||||||
|
# generate 兼容路径
|
||||||
|
self.assertEqual(b.generate("q", context=[]), "[NPU] q")
|
||||||
|
b.unload()
|
||||||
|
self.assertFalse(b.health_check().healthy)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
@@ -1,43 +0,0 @@
|
|||||||
# 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`
|
|
||||||
故障注入驱动。
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
# -*- 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 模块)。
|
|
||||||
"""
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
# -*- 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"))
|
|
||||||
@@ -1,278 +0,0 @@
|
|||||||
# -*- 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