任务
对照阈值评估候选环境的健康检查结果、错误率与 p95 延迟,给出切换、保持或回滚的结论及理由。
契约
- 输入:JSON 对象:{ candidate: { healthChecks, errorRate, p95Ms }, thresholds: { maxErrorRate, maxP95Ms } }
- 输出:JSON 对象:{ action, reason }
样例
- 输入
{"candidate":{"healthChecks":"pass","errorRate":0.002,"p95Ms":180},"thresholds":{"maxErrorRate":0.01,"maxP95Ms":250}}-> 期望{"action":"switch","reason":"all-gates-passed"}。健康检查、错误率与延迟全部达标。 - 输入
{"candidate":{"healthChecks":"fail","errorRate":0.002,"p95Ms":180},"thresholds":{"maxErrorRate":0.01,"maxP95Ms":250}}-> 期望{"action":"rollback","reason":"health-check-failed"}。健康检查不通过会直接阻止晋升。
参考实现
function solve({ candidate, thresholds }) {
if (candidate.healthChecks !== 'pass') return { action: 'rollback', reason: 'health-check-failed' };
if (candidate.errorRate > thresholds.maxErrorRate) return { action: 'rollback', reason: 'error-rate-exceeded' };
if (candidate.p95Ms > thresholds.maxP95Ms) return { action: 'hold', reason: 'latency-above-threshold' };
return { action: 'switch', reason: 'all-gates-passed' };
}
为什么有效
蓝绿发布只有在决策明确时才安全。临时起意的晋升会把可回滚的切换变成需要手工回滚的故障。
健康检查失败要回滚;延迟超阈值则保持现状,而不是继续切换。