feat(alert): A2 告警引擎分布式安全 — Redis 分布式锁(SetNX)
- Cache 接口新增 SetNX 方法(go-redis 原子 / redka check-then-set 单副本安全) - AlertEngine 改造为支持分布式锁: tryAcquireLock/releaseLock/GetEngineStatus - 多副本部署时仅 Leader 执行告警检查, 60s TTL 防死锁, fail-open 容错 - 新增 GET /alerts/engine/status 监控端点(is_leader/instance_id/lock_holder) - 修复 GrafanaDashboard.tsx Panel.options 类型不匹配导致构建中断 - 同步未提交的前端类型安全迁移(140+ 文件) - ROADMAP/TDD_PLAN/PROJECT_EVALUATION 三文档同步更新 验证: go build 零错误, Docker 部署 healthy, 引擎状态端点正常, 锁竞争测试通过(非 Leader 跳过检查), 5/5 核心 API 回归通过
This commit is contained in:
1 parent
5ca0e6d83a
commit
c68aef4444
150 files changed
+1038
-690
No files matched your search
+46
@@ -340,6 +340,52 @@ curl -s -X POST "http://127.0.0.1:8080/api/v1/servers" \
|
||||
|
||||
---
|
||||
|
||||
## P2-A2: 告警引擎分布式安全
|
||||
|
||||
### 验证用例
|
||||
|
||||
```bash
|
||||
TOKEN=$(python3 -c "
|
||||
import urllib.request, json
|
||||
login_data = json.dumps({'username': 'admin', 'password': '123456'}).encode()
|
||||
req = urllib.request.Request('http://127.0.0.1:8080/api/v1/auth/login', data=login_data, method='POST')
|
||||
req.add_header('Content-Type', 'application/json')
|
||||
resp = urllib.request.urlopen(req)
|
||||
print(json.loads(resp.read())['data']['token'])
|
||||
")
|
||||
|
||||
# U1: 引擎状态端点
|
||||
curl -s http://127.0.0.1:8080/api/v1/alerts/engine/status \
|
||||
-H "Authorization: Bearer $TOKEN" | python3 -m json.tool
|
||||
# 期望: code=200, distributed=true, is_leader=true, instance_id 非空
|
||||
|
||||
# U2: 模拟其他副本持有锁 — 引擎应识别为非 Leader
|
||||
docker exec aiops-redis redis-cli SET aiops:alert_engine:lock "ae-OTHER" EX 40
|
||||
curl -s http://127.0.0.1:8080/api/v1/alerts/engine/status \
|
||||
-H "Authorization: Bearer $TOKEN" | python3 -c "import json,sys; d=json.load(sys.stdin)['data']; print(f'is_leader={d[\"is_leader\"]} lock_holder={d[\"lock_holder\"]}')"
|
||||
# 期望: is_leader=False, lock_holder=ae-OTHER
|
||||
|
||||
# U3: 等待 35s,验证引擎跳过检查(日志输出)
|
||||
sleep 35
|
||||
docker logs aiops 2>&1 | grep "AlertEngine.*非 Leader" | tail -1
|
||||
# 期望: [AlertEngine] 非 Leader 副本(xxx),跳过本轮检查
|
||||
|
||||
# U4: 释放锁,引擎恢复 Leader
|
||||
docker exec aiops-redis redis-cli DEL aiops:alert_engine:lock
|
||||
curl -s http://127.0.0.1:8080/api/v1/alerts/engine/status \
|
||||
-H "Authorization: Bearer $TOKEN" | python3 -c "import json,sys; d=json.load(sys.stdin)['data']; print(f'is_leader={d[\"is_leader\"]}')"
|
||||
# 期望: is_leader=True
|
||||
```
|
||||
|
||||
### 通过标准
|
||||
- [x] U1: 引擎状态端点返回 distributed/is_leader/instance_id/lock_ttl_sec
|
||||
- [x] U2: 其他副本持锁时正确识别为非 Leader
|
||||
- [x] U3: 日志确认引擎跳过检查(非 Leader 副本不执行告警扫描)
|
||||
- [x] U4: 锁释放后恢复 Leader 角色
|
||||
- [x] `go build` 零错误,5/5 核心 API 回归通过
|
||||
|
||||
---
|
||||
|
||||
## 回归测试基线(每次改动后必跑)
|
||||
|
||||
```bash
|
||||
|
||||
Reference in new issue
Block a user