Skip to content

fix(gate-hub): notify operators of data-centre and watcher failures - #166

Merged
TN019 merged 9 commits into
mainfrom
fix/data-centre-and-watcher-alerts
Dec 1, 2026
Merged

TN019 merged 9 commits into
mainfrom
fix/data-centre-and-watcher-alerts

Conversation

@TN019

@TN019 TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Why

The hub logged data-centre failures without telling anyone, and the uplink watcher could exit silently, leaving the route choice unattended. The new lab/dc-alert.sh drill shows that neither fault produces a mail.

What changed

  • Add lab/dc-alert.sh: it cuts the data-centre-facing interfaces on both routers, restores them, then kills the hub's uplink watcher, and expects exactly one mail per fault (Data centre unreachable, Uplink watcher stopped) from alerts@elsworne.example to manager@elsworne.example within 20 s. Exit 0 means both arrive, 1 a failed judgement, 2 an environment or cleanup failure.
  • gate-hub/alert.c gains the two alerts. The data-centre probe reports each round to the alert watch; the watcher alert fires when the modification time of uplink_heartbeat_file is older than alert_after_seconds. Each alert is sent once per incident after that threshold, sends no recovery mail and re-arms on recovery. Sent incidents are kept in state_file as :data-centre and :uplink-watcher, so a hub restart does not resend them; a restarted hub gives the watcher a fresh threshold of grace.
  • lab/uplink-watch.sh touches GATEPOST_UPLINK_HEARTBEAT on every loop; lab/compose.yaml sets it and lab/hub.conf points uplink_heartbeat_file at the same /tmp/gatepost-uplink.heartbeat. config/hub.conf.example documents the option.
  • Add tests/test_dc_alerts.py (ctest dc_alerts): a probe failure that recovers before the threshold sends nothing, repeated outages send one mail each with distinct Message-IDs, the watcher alert re-arms after heartbeats resume, sent incidents survive restarts, and a restarted hub waits one threshold before reporting a missing heartbeat.
  • Document the alerts and log lines (event=mail_alert alert=data_centre|uplink_watcher state=failed|sent) in README.md and lab/README.md.

How to verify

On Linux with the build dependencies listed in Dockerfile.build, run from the repository root:

cmake --preset debug
cmake --build --preset debug
ctest --preset debug -R '^(dc_alerts|mail_alert|alert_state_fsync|uplink_watch)$' --output-on-failure

For the venue drill, with Docker Compose and a Linux container engine that supports the lab network capabilities, run it alone:

./scripts/dev.sh build-image
sh lab/keys.sh
docker compose -f lab/compose.yaml up -d --build
sh lab/dc-alert.sh

The drill should exit 0 with one mail for each fault.


背景

hub 只把数据中心故障写进日志,没有通知任何人;上行监视脚本退出时也悄无声息,线路切换就没人管了。新加的 lab/dc-alert.sh 演练表明,这两种故障都不会发出邮件。

改动

  • 新增 lab/dc-alert.sh:先切断两台路由器朝向数据中心的接口再恢复,然后杀掉 hub 的上行监视脚本,要求每种故障在 20 秒内各有一封邮件(Data centre unreachable、Uplink watcher stopped),由 alerts@elsworne.example 发给 manager@elsworne.example。退出码 0 表示都收到,1 表示判定失败,2 表示环境或清理出错。
  • gate-hub/alert.c 加入这两类告警。数据中心探测每一轮都把结果交给告警模块;uplink_heartbeat_file 的修改时间超过 alert_after_seconds 没有更新,就触发监视脚本告警。每起事件过了阈值只发一封,恢复时不发邮件,而是重新待命。已发送的事件以 :data-centre 和 :uplink-watcher 记在 state_file 里,hub 重启后不会重发;重启后的 hub 会给监视脚本重新留出一个阈值的宽限。
  • lab/uplink-watch.sh 每轮循环都刷新 GATEPOST_UPLINK_HEARTBEAT 指向的文件;lab/compose.yaml 设置这个变量,lab/hub.conf 的 uplink_heartbeat_file 指向同一个 /tmp/gatepost-uplink.heartbeat。config/hub.conf.example 补上这个选项的说明。
  • 新增 tests/test_dc_alerts.py(ctest 名 dc_alerts):探测失败后在阈值内恢复不发邮件;多次中断各发一封,Message-ID 互不相同;心跳恢复后告警重新待命;已发送的事件跨重启保留;重启后的 hub 等满一个阈值才报告心跳缺失。
  • 在 README.md 和 lab/README.md 中说明这两类告警和对应日志(event=mail_alert alert=data_centre|uplink_watcher state=failed|sent)。

怎么验证

在 Linux 环境安装 Dockerfile.build 列出的构建依赖后,从仓库根目录运行:

cmake --preset debug
cmake --build --preset debug
ctest --preset debug -R '^(dc_alerts|mail_alert|alert_state_fsync|uplink_watch)$' --output-on-failure

场馆演练需要 Docker Compose,以及支持场馆网络权限的 Linux 容器环境,请单独运行:

./scripts/dev.sh build-image
sh lab/keys.sh
docker compose -f lab/compose.yaml up -d --build
sh lab/dc-alert.sh

演练应以 0 退出,每种故障各收到一封邮件。

Mateo Rossi and others added 9 commits October 27, 2026 13:30
Exercise a stalled HTTPS list body after 4096 of 65536 bytes and a
TLS handshake that never answers after TCP connect.

The failed fetch line should append phase, received body bytes, and
the response Content-Length after its existing fields. Today both
cases report only curl=28 reason=timeout.

CTest accepts this run only when both cases fail in that current way.
A stalled body, a stalled handshake, and an unreachable hub could
all log curl=28 reason=timeout. The failure line now reports how far
the attempt got and how many body bytes of how many arrived. The
phase comes from curl's connection count, the status line and end of
headers seen, and bytes handed to the write callback. A phase change
is logged like a reason change.

The two exact log-line checks in test_gated_tls.py now include the
new fields.
Serve a large ticket list, shrink the client's receive buffer, read
only the response headers, then reset the connection during the body.

The complete download should add one tickets/200 count, and the
aborted download should add one tickets/incomplete count. Today both
are counted as 200. CTest accepts this run only when it fails that way.
A body write that failed part-way still counted as 200, so aborted
transfers never appeared in the ticket request metric. Count those
replies, and replies whose headers could not be written, once as
status="incomplete" on the tickets route.

The header-write path is not covered by the test: headers are too
small to make that write fail reliably.
…s heartbeat

The hub could not tell which list each gate had installed. List age
based on file time also kept growing through 304 confirmations.

Keep the verified SHA-256 and monotonic confirmation time at each gate.
Send the installed version and time since confirmation in CONFIG, then
compare it with the list the hub currently serves. Gates reporting no
version have unknown status, which is not an error. A gate retries an
empty CONFIG on the same connection for a hub not yet upgraded.

The hub trusts the reported list state as sent: CONFIG has no MAC, so
whoever holds a gate's certificate can report a false state for that
gate.

Co-authored-by: Ben Carter <ben.carter@elsworne.example>
On-call staff could only read raw /metrics. Provision a dashboard for
the existing hub series: incomplete ticket replies, TLS handshake
failures, and per-gate list status.

Use a separate Compose file so the venue lab and its drills need no
Grafana password. Pin both images by tag and digest.

Keep the earlier sync-metrics regression test for the existing sync
accounting fix, and check dashboard metrics against the hub.

Co-authored-by: Yuki Tanaka <yuki.tanaka@elsworne.example>
The hub recorded connections that had not identified themselves under
the name "unknown". Writing list status for every record made that
pseudo-gate look like a gate reporting no version.

Write list status only for gates that identified themselves and sent
a heartbeat. A CONFIG from an unidentified connection records nothing.
… the uplink watcher has stopped

Cut both data-centre exits, restore them, then kill the hub's uplink
watcher. Compare Mailpit message IDs across each separate fault.

Each case expects exactly one mail to manager@elsworne.example from
alerts@elsworne.example through the venue's own mail server, which
remains available without the data-centre link. The subjects are
"Data centre unreachable" and "Uplink watcher stopped". Allow 20 s:
5 s for the probe, 10 s for the alert threshold, 1 s for the alert
loop, and 4 s for mail submission; the watcher checks every second.

Today neither mail is sent, so both judgements fail. Exit 0 means both
pass, 1 means a judgement fails, and 2 means an environment or cleanup
failure.
…he uplink watcher stops

The hub logged data-centre failures, and the uplink watcher could exit
without notifying the venue.

Submit mail after the shared alert threshold, once per incident. Keep
sent incidents in the atomic state file across hub restarts, and re-arm
each alert when its condition recovers without sending recovery mail.

Have the watcher update a heartbeat file each loop. Its modification
time uses the clock shared with the hub, since a shell cannot portably
read a monotonic clock. Give restarted hubs a fresh threshold of grace.

Add a CI test for transient and repeated faults, recovery, mail headers,
restart persistence, and the watcher grace period.

Co-authored-by: Yuki Tanaka <yuki.tanaka@elsworne.example>
@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Wang Hao · reviewer

Does a stopped watcher become indistinguishable from a healthy idle route?


监视脚本停了以后,会不会被当成一条正常、无需切换的线路?

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ben Carter · author

No. The hub checks the heartbeat file's modification time on its own, independent of route changes. Once it is older than alert_after_seconds, the hub sends Uplink watcher stopped through the venue's mail server.


不会。hub 自己检查心跳文件的修改时间,跟线路有没有变化无关;超过 alert_after_seconds 没更新,就经场馆邮件服务器发出 Uplink watcher stopped。

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ingrid Larsen · reviewer

If the hub restarts in the middle of a data-centre outage, does the manager get a second mail for the same incident?


如果数据中心中断期间 hub 重启了,经理会不会为同一起事件再收到一封邮件?

@TN019

TN019 commented Dec 1, 2026

Copy link
Copy Markdown
Contributor Author

Ben Carter · author

No. The sent flag is stored in state_file under :data-centre, and test_dc_alerts.py restarts the hub during an outage and checks that the count stays the same.


不会。已发送标记存在 state_file 的 :data-centre 条目里;test_dc_alerts.py 会在中断期间重启 hub,检查邮件数量不变。

@TN019
TN019 merged commit f859ab6 into main Dec 1, 2026
8 of 10 checks passed
@TN019
TN019 deleted the fix/data-centre-and-watcher-alerts branch December 1, 2026 02:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant