工作流中的状态检查
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
在工作流中,状态检查可对外部系统(如应用系统和监控系统)执行指定操作以获取其状态,并在发现系统不健康时自动终止工作流。该概念类似于 Kubernetes 中的 Container Probes。本文介绍如何通过 YAML 文件在工作流中执行状态检查。
Chaos Mesh 当前暂不支持在 Chaos Dashboard 上创建 StatusCheck 节点,因此您只能通过 YAML 文件创建 StatusCheck 节点。
状态检查类型
Chaos Mesh 目前仅支持通过 HTTP 类型执行状态检查。
定义 HTTP 类型的 StatusCheck 节点
StatusCheck 节点会向指定 URL 发送 GET 或 POST HTTP 请求(可自定义请求头和请求体),并通过 criteria 字段中的条件判定请求结果。
- name: workflow-status-check
templateType: StatusCheck
deadline: 20s
statusCheck:
mode: Continuous
type: HTTP
intervalSeconds: 1
timeoutSeconds: 1
http:
url: http://123.123.123.123
method: GET
criteria:
statusCode: '200'
在配置示例中,您将看到 HTTP 类型的 StatusCheck 节点。其中 deadline 字段指定该节点最长执行时间为 20 秒;mode 字段声明节点将持续执行状态检查;intervalSeconds 字段设定每秒重复执行一次;timeoutSeconds 字段定义每次执行的超时时间。
当工作流执行到此 StatusCheck 节点时,系统将每秒执行一次状态检查:通过 GET 方法向 URL http://123.123.123.123 发送 HTTP 请求。若在 1 秒内收到响应且状态码为 200,则判定本次执行成功,否则判定为失败。
状态检查结果判定
每次状态检查会生成一个 execution result(Success 或 Failure)。由于单次 execution result 可能因系统波动无法反映真实状态,最终 status check result 不基于单次 execution result 判定。
StatusCheck 节点包含 failureThreshold 和 successThreshold 两个关键字段:
-
当连续失败的
execution results次数超过failureThreshold阈值时,status check result被判定为Failure -
当连续成功的
execution results次数超过successThreshold阈值时,status check result被判定为Success
- name: workflow-status-check
templateType: StatusCheck
deadline: 20s
statusCheck:
mode: Continuous
type: HTTP
successThreshold: 1
failureThreshold: 3
http:
url: http://123.123.123.123
method: GET
criteria:
statusCode: '200'
在配置示例中,StatusCheck 节点将持续执行状态检查:
-
若
execution result连续出现 1 次或多次Success,则status check result判定为Success -
若连续出现 3 次及以上
execution result为Failure,则status check result判定为Failure
下文中的 status check fails 指的是 status check result 为 Failure,而非单次 execution result 为 Failure。
状态检查失败时终止工作流
StatusCheck 节点仅支持在状态检查失败时自动终止工作流,不支持暂停或恢复操作。
执行混沌实验时,应用系统可能进入 unhealthy 状态。此功能可通过快速终止实验来恢复系统。若需在状态检查失败时自动终止工作流,请在 StatusCheck 节点中将 abortWithStatusCheck 字段设为 true。
- name: workflow-status-check
templateType: StatusCheck
deadline: 20s
abortWithStatusCheck: true
statusCheck:
mode: Continuous
type: HTTP
http:
url: http://123.123.123.123
method: GET
criteria:
statusCode: '200'
当满足以下任一条件时,状态检查被视为失败:
-
状态检查失败
-
当
StatusCheck节点超时且status check result未达到成功条件时。例如:successThreshold为1、failureThreshold为3,若超时时刻连续失败2次且成功0次。虽然不符合"状态检查失败"的条件,但在此情况下仍视为不成功。
状态检查模式
持续状态检查
当mode字段设为Continuous时,表示该StatusCheck节点将持续执行状态检查,直至节点超时或状态检查失败。
- name: workflow-status-check
templateType: StatusCheck
deadline: 20s
statusCheck:
mode: Continuous
type: HTTP
intervalSeconds: 1
successThreshold: 1
failureThreshold: 3
http:
url: http://123.123.123.123
method: GET
criteria:
statusCode: '200'
在配置中,该StatusCheck节点将每秒执行状态检查,并在满足以下任一条件时退出:
-
状态检查失败(即连续出现3次及以上
execution results失败) -
20秒后触发节点超时
单次状态检查
当mode字段设为Synchronous时,表示该StatusCheck节点将在status check result明确或节点超时时立即退出。
- name: workflow-status-check
templateType: StatusCheck
deadline: 20s
statusCheck:
mode: Synchronous
type: HTTP
intervalSeconds: 1
successThreshold: 1
failureThreshold: 3
http:
url: http://123.123.123.123
method: GET
criteria:
statusCode: '200'
在配置中,该StatusCheck节点将每秒执行状态检查,并在满足以下任一条件时退出:
-
状态检查成功(即连续出现1次及以上成功的
execution results) -
状态检查失败(即连续出现3次及以上
execution results失败) -
20秒后触发节点超时
StatusCheck 与 HTTP 请求任务对比
共同点:
-
StatusCheck节点和HTTP Request Task节点(执行HTTP请求的Task节点)均为工作流节点类型 -
StatusCheck节点和HTTP Request Task节点均可通过 HTTP 请求获取外部系统的信息。
差异点:
-
HTTP Request Task节点仅能发送单次HTTP请求,无法持续发送 -
HTTP Request Task节点在请求失败时无法影响工作流状态(例如中止工作流)
字段说明
有关工作流和模板的详细信息,请参阅创建 Chaos Mesh 工作流。
StatusCheck 字段说明
| Parameter | Type | Description | Default value | Required | Example |
|---|---|---|---|---|---|
| mode | string | The execution mode of the status check. Support value: Synchronous/Continuous. | None | Yes | Synchronous |
| type | string | The type of the status check. Support value: HTTP. | HTTP | Yes | HTTP |
| duration | string | The duration of the whole status check if the number of failed execution does not exceed the failureThreshold. It is available in both Synchronous and Continuous modes. | None | No | 100s |
| timeoutSeconds | int | The timeout seconds when the status check fails. | 1 | No | 1 |
| intervalSeconds | int | Defines how often (in seconds) to perform an execution of status check. | 1 | No | 1 |
| failureThreshold | int | The minimum consecutive failure for the status check to be considered failed. | 3 | No | 3 |
| successThreshold | int | The minimum consecutive successes for the status check to be considered successful. | 1 | No | 1 |
| recordsHistoryLimit | int | The number of records to retain. | 100 | No | 100 |
| http | HTTPStatusCheck | Configure the detail of the HTTP request to execute. | None | No |
HTTPStatusCheck 字段说明
| Parameter | Type | Description | Default value | Required | Example |
|---|---|---|---|---|---|
| url | string | The URL of the HTTP request. | None | Yes | http://123.123.123.123 |
| method | string | The method of the HTTP request. Support value: GET/POST. | GET | No | GET |
| headers | map[string][]string | The headers of the HTTP request. | None | No | |
| body | string | The body of the HTTP request. | None | No | {"a":"b"} |
| criteria | HTTPCriteria | Defines how to determine the result of the HTTP StatusCheck. | None | Yes |
HTTPCriteria 字段说明
| Parameter | Type | Description | Default value | Required | Example |
|---|---|---|---|---|---|
| statusCode | string | The expected http status code for the request. A statusCode string could be a single code (e.g. 200), or an inclusive range (e.g. 200-400, both 200 and 400 are included). | None | Yes | 200 |