Skip to content

Commit 5f7636b

Browse files
authored
Add docs for OpenSergo fault-tolerance spec v1alpha1 (#10)
Signed-off-by: Eric Zhao <[email protected]>
1 parent 0c37efe commit 5f7636b

File tree

4 files changed

+248
-0
lines changed

4 files changed

+248
-0
lines changed

specification/en/fault-tolerance.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Fault-tolerance spec v1alpha1
2+
3+
* domain: fault-tolerance
4+
* version: v1alpha1
5+
6+
![image](./images/fault-tolerance-rule-overview.png)
7+
8+
## FaultToleranceRule
9+
10+
`FaultToleranceRule` YAML sample:
11+
12+
```yaml
13+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
14+
kind: RateLimitStrategy
15+
metadata:
16+
name: rate-limit-foo
17+
spec:
18+
metricType: RequestAmount
19+
limitMode: Global
20+
threshold: 10
21+
statDuration: "1s"
22+
---
23+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
24+
kind: HttpRequestFallbackAction
25+
metadata:
26+
name: fallback-foo
27+
spec:
28+
behavior: ReturnProvidedResponse
29+
behaviorDesc:
30+
responseStatusCode: 429
31+
responseContentBody: "Blocked by Sentinel"
32+
responseAdditionalHeaders:
33+
- key: X-Sentinel-Limit
34+
value: "foo"
35+
---
36+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
37+
kind: FaultToleranceRule
38+
metadata:
39+
name: my-rule
40+
namespace: prod
41+
labels:
42+
app: my-app
43+
spec:
44+
targets:
45+
- targetResourceName: '/foo'
46+
strategies:
47+
- name: rate-limit-foo
48+
fallbackAction: fallback-foo
49+
```
Loading
+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# 流控降级与容错标准 v1alpha1
2+
3+
* domain: fault-tolerance
4+
* version: v1alpha1
5+
6+
流控降级与容错是服务流量治理中关键的一环,以流量为切入点,通过流控、熔断降级、流量平滑、自适应过载保护等手段来保障服务的稳定性。在 OpenSergo 中,我们期望结合 [Sentinel](https://sentinelguard.io/) 等框架组件的场景实践对流控降级与容错抽出标准 CRD。一个容错治理规则 (`FaultToleranceRule`) 由以下三部分组成:
7+
8+
* Target: 针对什么样的请求
9+
* Strategy: 容错或控制策略,如流控、熔断、并发控制、自适应过载保护、离群实例摘除等
10+
* FallbackAction: 触发后的 fallback 行为,如返回某个错误或状态码
11+
12+
![image](./images/fault-tolerance-rule-overview.png)
13+
14+
## Target
15+
16+
Target 定义该规则针对什么样的请求,如某个 key(可以类比 Sentinel 中的资源名的概念),或者包含某类参数的 HTTP 请求等等。v1alpha1 版本中,Target 先通过 targetResourceName 的方式直接配置资源 key。具体的用法可以参考最后的例子。
17+
18+
在后期版本的标准中,Target 会支持根据 HTTP、gRPC 等流量进行细分。如 HTTP target。
19+
20+
## Strategy
21+
22+
Strategy 定义该规则对应的容错或控制策略。在 v1alpha1 版本中,Strategy 支持流控、匀速排队、并发控制、熔断、系统过载保护等策略。在后续版本中,Strategy 还会支持过载实例摘除/调度、参数流控等能力。
23+
24+
### 流量控制
25+
26+
流量控制策略 (RateLimitStrategy),即控制单位时长内的请求量在一定范围内。多适用于激增流量下保护服务承载能力在容量之内,避免过多流量将服务打垮。RateLimitStrategy 包含以下要素:
27+
28+
| 字段名 | 是否必填 | 类型 | 描述 |
29+
| -------- | -------- | -------- | -------- |
30+
| metricType | required | string (enum) | 指标类型,取值范围 `RequestAmount` |
31+
| limitMode | required | string (enum) | 控制模式,单机 `Local`, 集群总体 `Global`, 集群按实例数转单机 `GlobalToLocal` |
32+
| threshold | required | double | 阈值,单位统计时长内最多允许的量 |
33+
| statDuration | required | string (int+timeUnit) | 统计时长,如 `1s`, `5min`;也可考虑 timeUnit 形式 |
34+
35+
以下示例定义了一个集群流控的策略,集群总体维度每秒不超过 10个请求。示例 CR YAML:
36+
37+
```yaml
38+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
39+
kind: RateLimitStrategy
40+
metadata:
41+
name: rate-limit-foo
42+
spec:
43+
metricType: RequestAmount
44+
limitMode: Global
45+
threshold: 10
46+
statDuration: "1s"
47+
```
48+
49+
### 流量平滑
50+
51+
流量平滑策略 (ThrottlingStrategy),以匀速+排队等待控制效果,对并发请求进行平滑。多适用于异步后台任务(如消息 consumer 批量处理)或对延时不敏感的请求场景。ThrottlingStrategy 包含以下要素:
52+
53+
| 字段名 | 是否必填 | 类型 | 描述 |
54+
| -------- | -------- | -------- | -------- |
55+
| minIntervalOfRequests | required | string (int+timeUnit) | 相邻两个并发请求之间的最短时间间隔 |
56+
| queueTimeout | required | string (int+timeUnit) | 最大排队等待时长 |
57+
58+
以下示例定义了一个匀速排队的策略,相邻两个并发请求的时间间隔不小于 20ms,同时排队平滑的等待时长不超过 500ms。示例 CR YAML:
59+
60+
```yaml
61+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
62+
kind: ThrottlingStrategy
63+
metadata:
64+
name: throttling-foo
65+
spec:
66+
minIntervalOfRequests: '20ms'
67+
queueTimeout: '500ms'
68+
```
69+
70+
### 并发控制
71+
72+
并发控制 (ConcurrencyLimitStrategy),即控制同时并发调用请求的数目。多适用于慢调用场景下的软隔离保护,避免调用端线程池被某些慢调用占满,导致服务不可用甚至链路不可用。ConcurrencyLimitStrategy 包含以下要素:
73+
74+
| 字段名 | 是否必填 | 类型 | 描述 |
75+
| -------- | -------- | -------- | -------- |
76+
| maxConcurrency | required | int | 最大并发 |
77+
| limitMode | required | string (enum) | 控制模式,单机 `Local`, 集群总体 `Global` |
78+
79+
示例 CR YAML:
80+
81+
```yaml
82+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
83+
kind: ConcurrencyLimitStrategy
84+
metadata:
85+
name: concurrency-limit-foo
86+
spec:
87+
maxConcurrency: 8
88+
limitMode: 'Local'
89+
```
90+
91+
### 熔断保护
92+
93+
CircuitBreakerStrategy 对应微服务设计中标准的断路器模式,单机维度生效。CircuitBreakerStrategy 包含以下要素:
94+
95+
* strategy: 熔断策略,目前支持 慢调用比例 `SlowRequestRatio`、错误比例 `ErrorRequestRatio`
96+
* triggerRatio: 触发比例
97+
* statDuration: 统计时长,如 `1s`, `5min`;也可考虑 timeUnit 形式
98+
* recoveryTimeout: 进入熔断状态后的等待时长,等待后会进入半开启恢复模式
99+
* minRequestAmount: 单位统计时长内,最小请求数
100+
* slowConditions: 慢调用策略下的条件,若熔断策略为“慢调用比例”则必填
101+
* maxAllowedRt: 慢调用策略下,超出该响应时长的请求认为是慢调用
102+
* errorConditions: 错误策略下的条件,若熔断策略为“错误比例”则必填
103+
* errorType: 错误类型
104+
105+
以下示例定义了一个慢调用比例熔断策略(在 30s 内请求超过 500ms 的比例达到 60% 时,且请求数达到5个,则会自动触发熔断,熔断恢复时长为 5s),示例 CR YAML:
106+
107+
```yaml
108+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
109+
kind: CircuitBreakerStrategy
110+
metadata:
111+
name: circuit-breaker-slow-foo
112+
spec:
113+
strategy: SlowRequestRatio
114+
triggerRatio: '60%'
115+
statDuration: '30s'
116+
recoveryTimeout: '5s'
117+
minRequestAmount: 5
118+
slowConditions:
119+
maxAllowedRt: '500ms'
120+
```
121+
122+
### 自适应过载保护
123+
124+
实例维度自适应过载保护策略 (AdaptiveOverloadProtectionStrategy),基于某些系统指标与自适应策略结合来对实例维度的稳定性进行整体兜底保护。注意该策略的维度为某个服务的每个 pod 维度,分别生效,不区分具体条件。
125+
126+
AdaptiveOverloadProtectionStrategy 包含以下要素:
127+
128+
* metricType: 过载保护针对的指标类型,如 CPU usage percentage, system load, memory 等
129+
* triggerThreshold: 触发值,超出此值则按条件进行限制
130+
* adaptiveStrategy: 自适应策略,若不支持或不开启则填 `NONE`;目前 CPU usage 指标支持 `BBR` 策略
131+
132+
示例 CR YAML:
133+
134+
```yaml
135+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
136+
kind: AdaptiveOverloadProtectionStrategy
137+
metadata:
138+
name: system-overload-foo
139+
spec:
140+
metricType: 'CpuPercentage'
141+
triggerThreshold: '70%'
142+
adaptiveStrategy: 'BBR
143+
```
144+
145+
## FallbackAction
146+
147+
v1alpha1 版本中,由于 target 先针对泛化的 resourceName,这里先忽略 FallbackAction 这一项。
148+
149+
在后期版本中,再根据 HTTP、gRPC 等流量进行细分。针对 HTTP 请求的 fallbackAction 可以参考下面的示例。
150+
151+
## 容错治理规则示例
152+
153+
一个 YAML 示例:
154+
155+
```yaml
156+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
157+
kind: RateLimitStrategy
158+
metadata:
159+
name: rate-limit-foo
160+
spec:
161+
metricType: RequestAmount
162+
limitMode: Global
163+
threshold: 10
164+
statDuration: "1s"
165+
---
166+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
167+
kind: HttpRequestFallbackAction
168+
metadata:
169+
name: fallback-foo
170+
spec:
171+
behavior: ReturnProvidedResponse
172+
behaviorDesc:
173+
# 触发策略控制后,HTTP 请求返回 429 状态码,同时携带指定的内容和 header.
174+
responseStatusCode: 429
175+
responseContentBody: "Blocked by Sentinel"
176+
responseAdditionalHeaders:
177+
- key: X-Sentinel-Limit
178+
value: "foo"
179+
---
180+
apiVersion: fault-tolerance.opensergo.io/v1alpha1
181+
kind: FaultToleranceRule
182+
metadata:
183+
name: my-rule
184+
namespace: prod
185+
labels:
186+
app: my-app # 规则配置生效的应用名
187+
spec:
188+
targets:
189+
- targetResourceName: '/foo'
190+
strategies:
191+
- name: rate-limit-foo
192+
fallbackAction: fallback-foo
193+
```
194+
195+
这个规则相当于为 key 为 `/foo` 的请求配置了一个策略(以下假定该资源对应 HTTP 请求),这个策略对应流控策略,全局不超过 10 QPS。当策略触发时,被拒绝的请求将根据配置的 fallback 返回 429 状态码,返回信息为 `Blocked by Sentinel`,同时返回 header 中增加一个 header,key 为 `X-Sentinel-Limit`, value 为 foo。
196+
197+
## References
198+
199+
* [Sentinel: OpenSergo 流量治理的标准实现](https://sentinelguard.io/)
Loading

0 commit comments

Comments
 (0)